You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContentBlockNode.js 3.8 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * This file is a fork of ContentBlock adding support for nesting references by
  12. * providing links to children, parent, prevSibling, and nextSibling.
  13. *
  14. * This is unstable and not part of the public API and should not be used by
  15. * production systems. This file may be update/removed without notice.
  16. */
  17. 'use strict';
  18. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  19. var CharacterMetadata = require("./CharacterMetadata");
  20. var findRangesImmutable = require("./findRangesImmutable");
  21. var Immutable = require("immutable");
  22. var List = Immutable.List,
  23. Map = Immutable.Map,
  24. OrderedSet = Immutable.OrderedSet,
  25. Record = Immutable.Record,
  26. Repeat = Immutable.Repeat;
  27. var EMPTY_SET = OrderedSet();
  28. var defaultRecord = {
  29. parent: null,
  30. characterList: List(),
  31. data: Map(),
  32. depth: 0,
  33. key: '',
  34. text: '',
  35. type: 'unstyled',
  36. children: List(),
  37. prevSibling: null,
  38. nextSibling: null
  39. };
  40. var haveEqualStyle = function haveEqualStyle(charA, charB) {
  41. return charA.getStyle() === charB.getStyle();
  42. };
  43. var haveEqualEntity = function haveEqualEntity(charA, charB) {
  44. return charA.getEntity() === charB.getEntity();
  45. };
  46. var decorateCharacterList = function decorateCharacterList(config) {
  47. if (!config) {
  48. return config;
  49. }
  50. var characterList = config.characterList,
  51. text = config.text;
  52. if (text && !characterList) {
  53. config.characterList = List(Repeat(CharacterMetadata.EMPTY, text.length));
  54. }
  55. return config;
  56. };
  57. var ContentBlockNode =
  58. /*#__PURE__*/
  59. function (_ref) {
  60. _inheritsLoose(ContentBlockNode, _ref);
  61. function ContentBlockNode() {
  62. var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultRecord;
  63. /* eslint-disable-next-line constructor-super */
  64. return _ref.call(this, decorateCharacterList(props)) || this;
  65. }
  66. var _proto = ContentBlockNode.prototype;
  67. _proto.getKey = function getKey() {
  68. return this.get('key');
  69. };
  70. _proto.getType = function getType() {
  71. return this.get('type');
  72. };
  73. _proto.getText = function getText() {
  74. return this.get('text');
  75. };
  76. _proto.getCharacterList = function getCharacterList() {
  77. return this.get('characterList');
  78. };
  79. _proto.getLength = function getLength() {
  80. return this.getText().length;
  81. };
  82. _proto.getDepth = function getDepth() {
  83. return this.get('depth');
  84. };
  85. _proto.getData = function getData() {
  86. return this.get('data');
  87. };
  88. _proto.getInlineStyleAt = function getInlineStyleAt(offset) {
  89. var character = this.getCharacterList().get(offset);
  90. return character ? character.getStyle() : EMPTY_SET;
  91. };
  92. _proto.getEntityAt = function getEntityAt(offset) {
  93. var character = this.getCharacterList().get(offset);
  94. return character ? character.getEntity() : null;
  95. };
  96. _proto.getChildKeys = function getChildKeys() {
  97. return this.get('children');
  98. };
  99. _proto.getParentKey = function getParentKey() {
  100. return this.get('parent');
  101. };
  102. _proto.getPrevSiblingKey = function getPrevSiblingKey() {
  103. return this.get('prevSibling');
  104. };
  105. _proto.getNextSiblingKey = function getNextSiblingKey() {
  106. return this.get('nextSibling');
  107. };
  108. _proto.findStyleRanges = function findStyleRanges(filterFn, callback) {
  109. findRangesImmutable(this.getCharacterList(), haveEqualStyle, filterFn, callback);
  110. };
  111. _proto.findEntityRanges = function findEntityRanges(filterFn, callback) {
  112. findRangesImmutable(this.getCharacterList(), haveEqualEntity, filterFn, callback);
  113. };
  114. return ContentBlockNode;
  115. }(Record(defaultRecord));
  116. module.exports = ContentBlockNode;