1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
14 |
|
15 | var CharacterMetadata = require("./CharacterMetadata");
|
16 |
|
17 | var findRangesImmutable = require("./findRangesImmutable");
|
18 |
|
19 | var Immutable = require("immutable");
|
20 |
|
21 | var List = Immutable.List,
|
22 | Map = Immutable.Map,
|
23 | OrderedSet = Immutable.OrderedSet,
|
24 | Record = Immutable.Record,
|
25 | Repeat = Immutable.Repeat;
|
26 | var EMPTY_SET = OrderedSet();
|
27 | var defaultRecord = {
|
28 | key: '',
|
29 | type: 'unstyled',
|
30 | text: '',
|
31 | characterList: List(),
|
32 | depth: 0,
|
33 | data: Map()
|
34 | };
|
35 | var ContentBlockRecord = Record(defaultRecord);
|
36 |
|
37 | var 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 |
|
52 | var ContentBlock = 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 |
|
100 |
|
101 | ;
|
102 |
|
103 | _proto.findStyleRanges = function findStyleRanges(filterFn, callback) {
|
104 | findRangesImmutable(this.getCharacterList(), haveEqualStyle, filterFn, callback);
|
105 | }
|
106 | |
107 |
|
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 |
|
118 | function haveEqualStyle(charA, charB) {
|
119 | return charA.getStyle() === charB.getStyle();
|
120 | }
|
121 |
|
122 | function haveEqualEntity(charA, charB) {
|
123 | return charA.getEntity() === charB.getEntity();
|
124 | }
|
125 |
|
126 | module.exports = ContentBlock; |
\ | No newline at end of file |