Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. import type { BlockNodeRecord } from "./BlockNodeRecord";
  13. import type CharacterMetadata from "./CharacterMetadata";
  14. import type ContentState from "./ContentState";
  15. import type { DraftDecoratorType } from "./DraftDecoratorType";
  16. const findRangesImmutable = require("./findRangesImmutable");
  17. const Immutable = require("immutable");
  18. const {
  19. List,
  20. Repeat,
  21. Record
  22. } = Immutable;
  23. const returnTrue = function () {
  24. return true;
  25. };
  26. const defaultLeafRange: {
  27. start: ?number,
  28. end: ?number,
  29. ...
  30. } = {
  31. start: null,
  32. end: null
  33. };
  34. const LeafRange = Record(defaultLeafRange);
  35. const defaultDecoratorRange: {
  36. start: ?number,
  37. end: ?number,
  38. decoratorKey: ?string,
  39. leaves: ?List<LeafRange>,
  40. ...
  41. } = {
  42. start: null,
  43. end: null,
  44. decoratorKey: null,
  45. leaves: null
  46. };
  47. const DecoratorRange = (Record(defaultDecoratorRange): any);
  48. const BlockTree = {
  49. /**
  50. * Generate a block tree for a given ContentBlock/decorator pair.
  51. */
  52. generate: function (contentState: ContentState, block: BlockNodeRecord, decorator: ?DraftDecoratorType): List<DecoratorRange> {
  53. const textLength = block.getLength();
  54. if (!textLength) {
  55. return List.of(new DecoratorRange({
  56. start: 0,
  57. end: 0,
  58. decoratorKey: null,
  59. leaves: List.of(new LeafRange({
  60. start: 0,
  61. end: 0
  62. }))
  63. }));
  64. }
  65. const leafSets = [];
  66. const decorations = decorator ? decorator.getDecorations(block, contentState) : List(Repeat(null, textLength));
  67. const chars = block.getCharacterList();
  68. findRangesImmutable(decorations, areEqual, returnTrue, (start, end) => {
  69. leafSets.push(new DecoratorRange({
  70. start,
  71. end,
  72. decoratorKey: decorations.get(start),
  73. leaves: generateLeaves(chars.slice(start, end).toList(), start)
  74. }));
  75. });
  76. return List(leafSets);
  77. }
  78. };
  79. /**
  80. * Generate LeafRange records for a given character list.
  81. */
  82. function generateLeaves(characters: List<CharacterMetadata>, offset: number): List<LeafRange> {
  83. const leaves = [];
  84. const inlineStyles = characters.map(c => c.getStyle()).toList();
  85. findRangesImmutable(inlineStyles, areEqual, returnTrue, (start, end) => {
  86. leaves.push(new LeafRange({
  87. start: start + offset,
  88. end: end + offset
  89. }));
  90. });
  91. return List(leaves);
  92. }
  93. function areEqual(a: any, b: any): boolean {
  94. return a === b;
  95. }
  96. module.exports = BlockTree;