1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
|
14 |
|
15 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
16 |
|
17 | function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
18 |
|
19 | var BlockMapBuilder = require("./BlockMapBuilder");
|
20 |
|
21 | var CharacterMetadata = require("./CharacterMetadata");
|
22 |
|
23 | var ContentBlock = require("./ContentBlock");
|
24 |
|
25 | var ContentBlockNode = require("./ContentBlockNode");
|
26 |
|
27 | var DraftEntity = require("./DraftEntity");
|
28 |
|
29 | var SelectionState = require("./SelectionState");
|
30 |
|
31 | var generateRandomKey = require("./generateRandomKey");
|
32 |
|
33 | var getOwnObjectValues = require("./getOwnObjectValues");
|
34 |
|
35 | var gkx = require("./gkx");
|
36 |
|
37 | var Immutable = require("immutable");
|
38 |
|
39 | var sanitizeDraftText = require("./sanitizeDraftText");
|
40 |
|
41 | var List = Immutable.List,
|
42 | Record = Immutable.Record,
|
43 | Repeat = Immutable.Repeat,
|
44 | ImmutableMap = Immutable.Map,
|
45 | OrderedMap = Immutable.OrderedMap;
|
46 | var defaultRecord = {
|
47 | entityMap: null,
|
48 | blockMap: null,
|
49 | selectionBefore: null,
|
50 | selectionAfter: null
|
51 | };
|
52 | var ContentStateRecord = Record(defaultRecord);
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | var ContentBlockNodeRecord = gkx('draft_tree_data_support') ? ContentBlockNode : ContentBlock;
|
58 |
|
59 | var ContentState = function (_ContentStateRecord) {
|
60 | _inheritsLoose(ContentState, _ContentStateRecord);
|
61 |
|
62 | function ContentState() {
|
63 | return _ContentStateRecord.apply(this, arguments) || this;
|
64 | }
|
65 |
|
66 | var _proto = ContentState.prototype;
|
67 |
|
68 | _proto.getEntityMap = function getEntityMap() {
|
69 |
|
70 | return DraftEntity;
|
71 | };
|
72 |
|
73 | _proto.getBlockMap = function getBlockMap() {
|
74 | return this.get('blockMap');
|
75 | };
|
76 |
|
77 | _proto.getSelectionBefore = function getSelectionBefore() {
|
78 | return this.get('selectionBefore');
|
79 | };
|
80 |
|
81 | _proto.getSelectionAfter = function getSelectionAfter() {
|
82 | return this.get('selectionAfter');
|
83 | };
|
84 |
|
85 | _proto.getBlockForKey = function getBlockForKey(key) {
|
86 | var block = this.getBlockMap().get(key);
|
87 | return block;
|
88 | };
|
89 |
|
90 | _proto.getKeyBefore = function getKeyBefore(key) {
|
91 | return this.getBlockMap().reverse().keySeq().skipUntil(function (v) {
|
92 | return v === key;
|
93 | }).skip(1).first();
|
94 | };
|
95 |
|
96 | _proto.getKeyAfter = function getKeyAfter(key) {
|
97 | return this.getBlockMap().keySeq().skipUntil(function (v) {
|
98 | return v === key;
|
99 | }).skip(1).first();
|
100 | };
|
101 |
|
102 | _proto.getBlockAfter = function getBlockAfter(key) {
|
103 | return this.getBlockMap().skipUntil(function (_, k) {
|
104 | return k === key;
|
105 | }).skip(1).first();
|
106 | };
|
107 |
|
108 | _proto.getBlockBefore = function getBlockBefore(key) {
|
109 | return this.getBlockMap().reverse().skipUntil(function (_, k) {
|
110 | return k === key;
|
111 | }).skip(1).first();
|
112 | };
|
113 |
|
114 | _proto.getBlocksAsArray = function getBlocksAsArray() {
|
115 | return this.getBlockMap().toArray();
|
116 | };
|
117 |
|
118 | _proto.getFirstBlock = function getFirstBlock() {
|
119 | return this.getBlockMap().first();
|
120 | };
|
121 |
|
122 | _proto.getLastBlock = function getLastBlock() {
|
123 | return this.getBlockMap().last();
|
124 | };
|
125 |
|
126 | _proto.getPlainText = function getPlainText(delimiter) {
|
127 | return this.getBlockMap().map(function (block) {
|
128 | return block ? block.getText() : '';
|
129 | }).join(delimiter || '\n');
|
130 | };
|
131 |
|
132 | _proto.getLastCreatedEntityKey = function getLastCreatedEntityKey() {
|
133 |
|
134 | return DraftEntity.__getLastCreatedEntityKey();
|
135 | };
|
136 |
|
137 | _proto.hasText = function hasText() {
|
138 | var blockMap = this.getBlockMap();
|
139 | return blockMap.size > 1 ||
|
140 | escape(blockMap.first().getText()).replace(/%u200B/g, '').length > 0;
|
141 | };
|
142 |
|
143 | _proto.createEntity = function createEntity(type, mutability, data) {
|
144 |
|
145 | DraftEntity.__create(type, mutability, data);
|
146 |
|
147 | return this;
|
148 | };
|
149 |
|
150 | _proto.mergeEntityData = function mergeEntityData(key, toMerge) {
|
151 |
|
152 | DraftEntity.__mergeData(key, toMerge);
|
153 |
|
154 | return this;
|
155 | };
|
156 |
|
157 | _proto.replaceEntityData = function replaceEntityData(key, newData) {
|
158 |
|
159 | DraftEntity.__replaceData(key, newData);
|
160 |
|
161 | return this;
|
162 | };
|
163 |
|
164 | _proto.addEntity = function addEntity(instance) {
|
165 |
|
166 | DraftEntity.__add(instance);
|
167 |
|
168 | return this;
|
169 | };
|
170 |
|
171 | _proto.getEntity = function getEntity(key) {
|
172 |
|
173 | return DraftEntity.__get(key);
|
174 | };
|
175 |
|
176 | _proto.getAllEntities = function getAllEntities() {
|
177 | return DraftEntity.__getAll();
|
178 | };
|
179 |
|
180 | _proto.loadWithEntities = function loadWithEntities(entities) {
|
181 | return DraftEntity.__loadWithEntities(entities);
|
182 | };
|
183 |
|
184 | ContentState.createFromBlockArray = function createFromBlockArray( // TODO: update flow type when we completely deprecate the old entity API
|
185 | blocks, entityMap) {
|
186 |
|
187 | var theBlocks = Array.isArray(blocks) ? blocks : blocks.contentBlocks;
|
188 | var blockMap = BlockMapBuilder.createFromArray(theBlocks);
|
189 | var selectionState = blockMap.isEmpty() ? new SelectionState() : SelectionState.createEmpty(blockMap.first().getKey());
|
190 | return new ContentState({
|
191 | blockMap: blockMap,
|
192 | entityMap: entityMap || DraftEntity,
|
193 | selectionBefore: selectionState,
|
194 | selectionAfter: selectionState
|
195 | });
|
196 | };
|
197 |
|
198 | ContentState.createFromText = function createFromText(text) {
|
199 | var delimiter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : /\r\n?|\n/g;
|
200 | var strings = text.split(delimiter);
|
201 | var blocks = strings.map(function (block) {
|
202 | block = sanitizeDraftText(block);
|
203 | return new ContentBlockNodeRecord({
|
204 | key: generateRandomKey(),
|
205 | text: block,
|
206 | type: 'unstyled',
|
207 | characterList: List(Repeat(CharacterMetadata.EMPTY, block.length))
|
208 | });
|
209 | });
|
210 | return ContentState.createFromBlockArray(blocks);
|
211 | };
|
212 |
|
213 | ContentState.fromJS = function fromJS(state) {
|
214 | return new ContentState(_objectSpread({}, state, {
|
215 | blockMap: OrderedMap(state.blockMap).map(ContentState.createContentBlockFromJS),
|
216 | selectionBefore: new SelectionState(state.selectionBefore),
|
217 | selectionAfter: new SelectionState(state.selectionAfter)
|
218 | }));
|
219 | };
|
220 |
|
221 | ContentState.createContentBlockFromJS = function createContentBlockFromJS(block) {
|
222 | var characterList = block.characterList;
|
223 | return new ContentBlockNodeRecord(_objectSpread({}, block, {
|
224 | data: ImmutableMap(block.data),
|
225 | characterList: characterList != null ? List((Array.isArray(characterList) ? characterList : getOwnObjectValues(characterList)).map(function (c) {
|
226 | return CharacterMetadata.fromJS(c);
|
227 | })) : undefined
|
228 | }));
|
229 | };
|
230 |
|
231 | return ContentState;
|
232 | }(ContentStateRecord);
|
233 |
|
234 | module.exports = ContentState; |
\ | No newline at end of file |