Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

195 строки
6.9 KiB

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