No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

DraftEntity.js.flow 7.6 KiB

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