Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

encodeInlineStyleRanges.js 1.7 KiB

3 лет назад
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. var UnicodeUtils = require("fbjs/lib/UnicodeUtils");
  13. var findRangesImmutable = require("./findRangesImmutable");
  14. var areEqual = function areEqual(a, b) {
  15. return a === b;
  16. };
  17. var isTruthy = function isTruthy(a) {
  18. return !!a;
  19. };
  20. var EMPTY_ARRAY = [];
  21. /**
  22. * Helper function for getting encoded styles for each inline style. Convert
  23. * to UTF-8 character counts for storage.
  24. */
  25. function getEncodedInlinesForType(block, styleList, styleToEncode) {
  26. var ranges = []; // Obtain an array with ranges for only the specified style.
  27. var filteredInlines = styleList.map(function (style) {
  28. return style.has(styleToEncode);
  29. }).toList();
  30. findRangesImmutable(filteredInlines, areEqual, // We only want to keep ranges with nonzero style values.
  31. isTruthy, function (start, end) {
  32. var text = block.getText();
  33. ranges.push({
  34. offset: UnicodeUtils.strlen(text.slice(0, start)),
  35. length: UnicodeUtils.strlen(text.slice(start, end)),
  36. style: styleToEncode
  37. });
  38. });
  39. return ranges;
  40. }
  41. /*
  42. * Retrieve the encoded arrays of inline styles, with each individual style
  43. * treated separately.
  44. */
  45. function encodeInlineStyleRanges(block) {
  46. var styleList = block.getCharacterList().map(function (c) {
  47. return c.getStyle();
  48. }).toList();
  49. var ranges = styleList.flatten().toSet().map(function (style) {
  50. return getEncodedInlinesForType(block, styleList, style);
  51. });
  52. return Array.prototype.concat.apply(EMPTY_ARRAY, ranges.toJS());
  53. }
  54. module.exports = encodeInlineStyleRanges;