Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

59 linhas
1.9 KiB

  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. * @flow strict-local
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. import type { BlockNodeRecord } from "./BlockNodeRecord";
  13. import type { DraftInlineStyle } from "./DraftInlineStyle";
  14. import type { InlineStyleRange } from "./InlineStyleRange";
  15. import type { List } from "immutable";
  16. const UnicodeUtils = require("fbjs/lib/UnicodeUtils");
  17. const findRangesImmutable = require("./findRangesImmutable");
  18. const areEqual = (a, b) => a === b;
  19. const isTruthy = a => !!a;
  20. const 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: BlockNodeRecord, styleList: List<DraftInlineStyle>, styleToEncode: string): Array<InlineStyleRange> {
  26. const ranges = []; // Obtain an array with ranges for only the specified style.
  27. const filteredInlines = styleList.map(style => style.has(styleToEncode)).toList();
  28. findRangesImmutable(filteredInlines, areEqual, // We only want to keep ranges with nonzero style values.
  29. isTruthy, (start, end) => {
  30. const text = block.getText();
  31. ranges.push({
  32. offset: UnicodeUtils.strlen(text.slice(0, start)),
  33. length: UnicodeUtils.strlen(text.slice(start, end)),
  34. style: styleToEncode
  35. });
  36. });
  37. return ranges;
  38. }
  39. /*
  40. * Retrieve the encoded arrays of inline styles, with each individual style
  41. * treated separately.
  42. */
  43. function encodeInlineStyleRanges(block: BlockNodeRecord): Array<InlineStyleRange> {
  44. const styleList = block.getCharacterList().map(c => c.getStyle()).toList();
  45. const ranges = styleList.flatten().toSet().map(style => getEncodedInlinesForType(block, styleList, style));
  46. return Array.prototype.concat.apply(EMPTY_ARRAY, ranges.toJS());
  47. }
  48. module.exports = encodeInlineStyleRanges;