UNPKG

7.96 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 _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
15function _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
17function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
18
19var BlockMapBuilder = require("./BlockMapBuilder");
20
21var CharacterMetadata = require("./CharacterMetadata");
22
23var ContentBlock = require("./ContentBlock");
24
25var ContentBlockNode = require("./ContentBlockNode");
26
27var DraftEntity = require("./DraftEntity");
28
29var SelectionState = require("./SelectionState");
30
31var generateRandomKey = require("./generateRandomKey");
32
33var getOwnObjectValues = require("./getOwnObjectValues");
34
35var gkx = require("./gkx");
36
37var Immutable = require("immutable");
38
39var sanitizeDraftText = require("./sanitizeDraftText");
40
41var List = Immutable.List,
42 Record = Immutable.Record,
43 Repeat = Immutable.Repeat,
44 ImmutableMap = Immutable.Map,
45 OrderedMap = Immutable.OrderedMap;
46var defaultRecord = {
47 entityMap: null,
48 blockMap: null,
49 selectionBefore: null,
50 selectionAfter: null
51};
52var ContentStateRecord = Record(defaultRecord);
53/* $FlowFixMe[signature-verification-failure] Supressing a `signature-
54 * verification-failure` error here. TODO: T65949050 Clean up the branch for
55 * this GK */
56
57var ContentBlockNodeRecord = gkx('draft_tree_data_support') ? ContentBlockNode : ContentBlock;
58
59var ContentState = /*#__PURE__*/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 // TODO: update this when we fully remove DraftEntity
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 // TODO: update this when we fully remove DraftEntity
134 return DraftEntity.__getLastCreatedEntityKey();
135 };
136
137 _proto.hasText = function hasText() {
138 var blockMap = this.getBlockMap();
139 return blockMap.size > 1 || // make sure that there are no zero width space chars
140 escape(blockMap.first().getText()).replace(/%u200B/g, '').length > 0;
141 };
142
143 _proto.createEntity = function createEntity(type, mutability, data) {
144 // TODO: update this when we fully remove DraftEntity
145 DraftEntity.__create(type, mutability, data);
146
147 return this;
148 };
149
150 _proto.mergeEntityData = function mergeEntityData(key, toMerge) {
151 // TODO: update this when we fully remove DraftEntity
152 DraftEntity.__mergeData(key, toMerge);
153
154 return this;
155 };
156
157 _proto.replaceEntityData = function replaceEntityData(key, newData) {
158 // TODO: update this when we fully remove DraftEntity
159 DraftEntity.__replaceData(key, newData);
160
161 return this;
162 };
163
164 _proto.addEntity = function addEntity(instance) {
165 // TODO: update this when we fully remove DraftEntity
166 DraftEntity.__add(instance);
167
168 return this;
169 };
170
171 _proto.getEntity = function getEntity(key) {
172 // TODO: update this when we fully remove DraftEntity
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 // TODO: remove this when we completely deprecate the old entity API
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
234module.exports = ContentState;
\No newline at end of file