1 | ;
|
2 |
|
3 | 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; }
|
4 |
|
5 | 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; }
|
6 |
|
7 | /**
|
8 | * Copyright (c) Facebook, Inc. and its affiliates.
|
9 | *
|
10 | * This source code is licensed under the MIT license found in the
|
11 | * LICENSE file in the root directory of this source tree.
|
12 | *
|
13 | * @format
|
14 | *
|
15 | * @emails oncall+draft_js
|
16 | */
|
17 | var DraftEntityInstance = require("./DraftEntityInstance");
|
18 |
|
19 | var Immutable = require("immutable");
|
20 |
|
21 | var invariant = require("fbjs/lib/invariant");
|
22 |
|
23 | var uuid = require("./uuid");
|
24 |
|
25 | var Map = Immutable.Map;
|
26 | var instances = Map();
|
27 | var instanceKey = uuid();
|
28 | /**
|
29 | * Temporary utility for generating the warnings
|
30 | */
|
31 |
|
32 | function logWarning(oldMethodCall, newMethodCall) {
|
33 | console.warn('WARNING: ' + oldMethodCall + ' will be deprecated soon!\nPlease use "' + newMethodCall + '" instead.');
|
34 | }
|
35 |
|
36 | /**
|
37 | * A "document entity" is an object containing metadata associated with a
|
38 | * piece of text in a ContentBlock.
|
39 | *
|
40 | * For example, a `link` entity might include a `uri` property. When a
|
41 | * ContentBlock is rendered in the browser, text that refers to that link
|
42 | * entity may be rendered as an anchor, with the `uri` as the href value.
|
43 | *
|
44 | * In a ContentBlock, every position in the text may correspond to zero
|
45 | * or one entities. This correspondence is tracked using a key string,
|
46 | * generated via DraftEntity.create() and used to obtain entity metadata
|
47 | * via DraftEntity.get().
|
48 | */
|
49 | var DraftEntity = {
|
50 | /**
|
51 | * WARNING: This method will be deprecated soon!
|
52 | * Please use 'contentState.getLastCreatedEntityKey' instead.
|
53 | * ---
|
54 | * Get the random key string from whatever entity was last created.
|
55 | * We need this to support the new API, as part of transitioning to put Entity
|
56 | * storage in contentState.
|
57 | */
|
58 | getLastCreatedEntityKey: function getLastCreatedEntityKey() {
|
59 | logWarning('DraftEntity.getLastCreatedEntityKey', 'contentState.getLastCreatedEntityKey');
|
60 | return DraftEntity.__getLastCreatedEntityKey();
|
61 | },
|
62 |
|
63 | /**
|
64 | * WARNING: This method will be deprecated soon!
|
65 | * Please use 'contentState.createEntity' instead.
|
66 | * ---
|
67 | * Create a DraftEntityInstance and store it for later retrieval.
|
68 | *
|
69 | * A random key string will be generated and returned. This key may
|
70 | * be used to track the entity's usage in a ContentBlock, and for
|
71 | * retrieving data about the entity at render time.
|
72 | */
|
73 | create: function create(type, mutability, data) {
|
74 | logWarning('DraftEntity.create', 'contentState.createEntity');
|
75 | return DraftEntity.__create(type, mutability, data);
|
76 | },
|
77 |
|
78 | /**
|
79 | * WARNING: This method will be deprecated soon!
|
80 | * Please use 'contentState.addEntity' instead.
|
81 | * ---
|
82 | * Add an existing DraftEntityInstance to the DraftEntity map. This is
|
83 | * useful when restoring instances from the server.
|
84 | */
|
85 | add: function add(instance) {
|
86 | logWarning('DraftEntity.add', 'contentState.addEntity');
|
87 | return DraftEntity.__add(instance);
|
88 | },
|
89 |
|
90 | /**
|
91 | * WARNING: This method will be deprecated soon!
|
92 | * Please use 'contentState.getEntity' instead.
|
93 | * ---
|
94 | * Retrieve the entity corresponding to the supplied key string.
|
95 | */
|
96 | get: function get(key) {
|
97 | logWarning('DraftEntity.get', 'contentState.getEntity');
|
98 | return DraftEntity.__get(key);
|
99 | },
|
100 |
|
101 | /**
|
102 | * Get all the entities in the content state.
|
103 | */
|
104 | __getAll: function __getAll() {
|
105 | return instances;
|
106 | },
|
107 |
|
108 | /**
|
109 | * Load the entity map with the given set of entities.
|
110 | */
|
111 | __loadWithEntities: function __loadWithEntities(entities) {
|
112 | instances = entities;
|
113 | instanceKey = uuid();
|
114 | },
|
115 |
|
116 | /**
|
117 | * WARNING: This method will be deprecated soon!
|
118 | * Please use 'contentState.mergeEntityData' instead.
|
119 | * ---
|
120 | * Entity instances are immutable. If you need to update the data for an
|
121 | * instance, this method will merge your data updates and return a new
|
122 | * instance.
|
123 | */
|
124 | mergeData: function mergeData(key, toMerge) {
|
125 | logWarning('DraftEntity.mergeData', 'contentState.mergeEntityData');
|
126 | return DraftEntity.__mergeData(key, toMerge);
|
127 | },
|
128 |
|
129 | /**
|
130 | * WARNING: This method will be deprecated soon!
|
131 | * Please use 'contentState.replaceEntityData' instead.
|
132 | * ---
|
133 | * Completely replace the data for a given instance.
|
134 | */
|
135 | replaceData: function replaceData(key, newData) {
|
136 | logWarning('DraftEntity.replaceData', 'contentState.replaceEntityData');
|
137 | return DraftEntity.__replaceData(key, newData);
|
138 | },
|
139 | // ***********************************WARNING******************************
|
140 | // --- the above public API will be deprecated in the next version of Draft!
|
141 | // The methods below this line are private - don't call them directly.
|
142 |
|
143 | /**
|
144 | * Get the random key string from whatever entity was last created.
|
145 | * We need this to support the new API, as part of transitioning to put Entity
|
146 | * storage in contentState.
|
147 | */
|
148 | __getLastCreatedEntityKey: function __getLastCreatedEntityKey() {
|
149 | return instanceKey;
|
150 | },
|
151 |
|
152 | /**
|
153 | * Create a DraftEntityInstance and store it for later retrieval.
|
154 | *
|
155 | * A random key string will be generated and returned. This key may
|
156 | * be used to track the entity's usage in a ContentBlock, and for
|
157 | * retrieving data about the entity at render time.
|
158 | */
|
159 | __create: function __create(type, mutability, data) {
|
160 | return DraftEntity.__add(new DraftEntityInstance({
|
161 | type: type,
|
162 | mutability: mutability,
|
163 | data: data || {}
|
164 | }));
|
165 | },
|
166 |
|
167 | /**
|
168 | * Add an existing DraftEntityInstance to the DraftEntity map. This is
|
169 | * useful when restoring instances from the server.
|
170 | */
|
171 | __add: function __add(instance) {
|
172 | instanceKey = uuid();
|
173 | instances = instances.set(instanceKey, instance);
|
174 | return instanceKey;
|
175 | },
|
176 |
|
177 | /**
|
178 | * Retrieve the entity corresponding to the supplied key string.
|
179 | */
|
180 | __get: function __get(key) {
|
181 | var instance = instances.get(key);
|
182 | !!!instance ? process.env.NODE_ENV !== "production" ? invariant(false, 'Unknown DraftEntity key: %s.', key) : invariant(false) : void 0;
|
183 | return instance;
|
184 | },
|
185 |
|
186 | /**
|
187 | * Entity instances are immutable. If you need to update the data for an
|
188 | * instance, this method will merge your data updates and return a new
|
189 | * instance.
|
190 | */
|
191 | __mergeData: function __mergeData(key, toMerge) {
|
192 | var instance = DraftEntity.__get(key);
|
193 |
|
194 | var newData = _objectSpread({}, instance.getData(), toMerge);
|
195 |
|
196 | var newInstance = instance.set('data', newData);
|
197 | instances = instances.set(key, newInstance);
|
198 | return newInstance;
|
199 | },
|
200 |
|
201 | /**
|
202 | * Completely replace the data for a given instance.
|
203 | */
|
204 | __replaceData: function __replaceData(key, newData) {
|
205 | var instance = DraftEntity.__get(key);
|
206 |
|
207 | var newInstance = instance.set('data', newData);
|
208 | instances = instances.set(key, newInstance);
|
209 | return newInstance;
|
210 | }
|
211 | };
|
212 | module.exports = DraftEntity; |
\ | No newline at end of file |