UNPKG

3.24 kBJavaScriptView Raw
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
13function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
14
15var CharacterMetadata = require("./CharacterMetadata");
16
17var findRangesImmutable = require("./findRangesImmutable");
18
19var Immutable = require("immutable");
20
21var List = Immutable.List,
22 Map = Immutable.Map,
23 OrderedSet = Immutable.OrderedSet,
24 Record = Immutable.Record,
25 Repeat = Immutable.Repeat;
26var EMPTY_SET = OrderedSet();
27var defaultRecord = {
28 key: '',
29 type: 'unstyled',
30 text: '',
31 characterList: List(),
32 depth: 0,
33 data: Map()
34};
35var ContentBlockRecord = Record(defaultRecord);
36
37var decorateCharacterList = function decorateCharacterList(config) {
38 if (!config) {
39 return config;
40 }
41
42 var characterList = config.characterList,
43 text = config.text;
44
45 if (text && !characterList) {
46 config.characterList = List(Repeat(CharacterMetadata.EMPTY, text.length));
47 }
48
49 return config;
50};
51
52var ContentBlock = /*#__PURE__*/function (_ContentBlockRecord) {
53 _inheritsLoose(ContentBlock, _ContentBlockRecord);
54
55 function ContentBlock(config) {
56 return _ContentBlockRecord.call(this, decorateCharacterList(config)) || this;
57 }
58
59 var _proto = ContentBlock.prototype;
60
61 _proto.getKey = function getKey() {
62 return this.get('key');
63 };
64
65 _proto.getType = function getType() {
66 return this.get('type');
67 };
68
69 _proto.getText = function getText() {
70 return this.get('text');
71 };
72
73 _proto.getCharacterList = function getCharacterList() {
74 return this.get('characterList');
75 };
76
77 _proto.getLength = function getLength() {
78 return this.getText().length;
79 };
80
81 _proto.getDepth = function getDepth() {
82 return this.get('depth');
83 };
84
85 _proto.getData = function getData() {
86 return this.get('data');
87 };
88
89 _proto.getInlineStyleAt = function getInlineStyleAt(offset) {
90 var character = this.getCharacterList().get(offset);
91 return character ? character.getStyle() : EMPTY_SET;
92 };
93
94 _proto.getEntityAt = function getEntityAt(offset) {
95 var character = this.getCharacterList().get(offset);
96 return character ? character.getEntity() : null;
97 }
98 /**
99 * Execute a callback for every contiguous range of styles within the block.
100 */
101 ;
102
103 _proto.findStyleRanges = function findStyleRanges(filterFn, callback) {
104 findRangesImmutable(this.getCharacterList(), haveEqualStyle, filterFn, callback);
105 }
106 /**
107 * Execute a callback for every contiguous range of entities within the block.
108 */
109 ;
110
111 _proto.findEntityRanges = function findEntityRanges(filterFn, callback) {
112 findRangesImmutable(this.getCharacterList(), haveEqualEntity, filterFn, callback);
113 };
114
115 return ContentBlock;
116}(ContentBlockRecord);
117
118function haveEqualStyle(charA, charB) {
119 return charA.getStyle() === charB.getStyle();
120}
121
122function haveEqualEntity(charA, charB) {
123 return charA.getEntity() === charB.getEntity();
124}
125
126module.exports = ContentBlock;
\No newline at end of file