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

128 строки
3.2 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. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  13. var CharacterMetadata = require("./CharacterMetadata");
  14. var findRangesImmutable = require("./findRangesImmutable");
  15. var Immutable = require("immutable");
  16. var List = Immutable.List,
  17. Map = Immutable.Map,
  18. OrderedSet = Immutable.OrderedSet,
  19. Record = Immutable.Record,
  20. Repeat = Immutable.Repeat;
  21. var EMPTY_SET = OrderedSet();
  22. var defaultRecord = {
  23. key: '',
  24. type: 'unstyled',
  25. text: '',
  26. characterList: List(),
  27. depth: 0,
  28. data: Map()
  29. };
  30. var ContentBlockRecord = Record(defaultRecord);
  31. var decorateCharacterList = function decorateCharacterList(config) {
  32. if (!config) {
  33. return config;
  34. }
  35. var characterList = config.characterList,
  36. text = config.text;
  37. if (text && !characterList) {
  38. config.characterList = List(Repeat(CharacterMetadata.EMPTY, text.length));
  39. }
  40. return config;
  41. };
  42. var ContentBlock =
  43. /*#__PURE__*/
  44. function (_ContentBlockRecord) {
  45. _inheritsLoose(ContentBlock, _ContentBlockRecord);
  46. function ContentBlock(config) {
  47. return _ContentBlockRecord.call(this, decorateCharacterList(config)) || this;
  48. }
  49. var _proto = ContentBlock.prototype;
  50. _proto.getKey = function getKey() {
  51. return this.get('key');
  52. };
  53. _proto.getType = function getType() {
  54. return this.get('type');
  55. };
  56. _proto.getText = function getText() {
  57. return this.get('text');
  58. };
  59. _proto.getCharacterList = function getCharacterList() {
  60. return this.get('characterList');
  61. };
  62. _proto.getLength = function getLength() {
  63. return this.getText().length;
  64. };
  65. _proto.getDepth = function getDepth() {
  66. return this.get('depth');
  67. };
  68. _proto.getData = function getData() {
  69. return this.get('data');
  70. };
  71. _proto.getInlineStyleAt = function getInlineStyleAt(offset) {
  72. var character = this.getCharacterList().get(offset);
  73. return character ? character.getStyle() : EMPTY_SET;
  74. };
  75. _proto.getEntityAt = function getEntityAt(offset) {
  76. var character = this.getCharacterList().get(offset);
  77. return character ? character.getEntity() : null;
  78. }
  79. /**
  80. * Execute a callback for every contiguous range of styles within the block.
  81. */
  82. ;
  83. _proto.findStyleRanges = function findStyleRanges(filterFn, callback) {
  84. findRangesImmutable(this.getCharacterList(), haveEqualStyle, filterFn, callback);
  85. }
  86. /**
  87. * Execute a callback for every contiguous range of entities within the block.
  88. */
  89. ;
  90. _proto.findEntityRanges = function findEntityRanges(filterFn, callback) {
  91. findRangesImmutable(this.getCharacterList(), haveEqualEntity, filterFn, callback);
  92. };
  93. return ContentBlock;
  94. }(ContentBlockRecord);
  95. function haveEqualStyle(charA, charB) {
  96. return charA.getStyle() === charB.getStyle();
  97. }
  98. function haveEqualEntity(charA, charB) {
  99. return charA.getEntity() === charB.getEntity();
  100. }
  101. module.exports = ContentBlock;