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

129 строки
4.5 KiB

  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. 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; }
  13. 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; }
  14. var ContentBlock = require("./ContentBlock");
  15. var ContentBlockNode = require("./ContentBlockNode");
  16. var DraftStringKey = require("./DraftStringKey");
  17. var encodeEntityRanges = require("./encodeEntityRanges");
  18. var encodeInlineStyleRanges = require("./encodeInlineStyleRanges");
  19. var invariant = require("fbjs/lib/invariant");
  20. var createRawBlock = function createRawBlock(block, entityStorageMap) {
  21. return {
  22. key: block.getKey(),
  23. text: block.getText(),
  24. type: block.getType(),
  25. depth: block.getDepth(),
  26. inlineStyleRanges: encodeInlineStyleRanges(block),
  27. entityRanges: encodeEntityRanges(block, entityStorageMap),
  28. data: block.getData().toObject()
  29. };
  30. };
  31. var insertRawBlock = function insertRawBlock(block, entityMap, rawBlocks, blockCacheRef) {
  32. if (block instanceof ContentBlock) {
  33. rawBlocks.push(createRawBlock(block, entityMap));
  34. return;
  35. }
  36. !(block instanceof ContentBlockNode) ? process.env.NODE_ENV !== "production" ? invariant(false, 'block is not a BlockNode') : invariant(false) : void 0;
  37. var parentKey = block.getParentKey();
  38. var rawBlock = blockCacheRef[block.getKey()] = _objectSpread({}, createRawBlock(block, entityMap), {
  39. children: []
  40. });
  41. if (parentKey) {
  42. blockCacheRef[parentKey].children.push(rawBlock);
  43. return;
  44. }
  45. rawBlocks.push(rawBlock);
  46. };
  47. var encodeRawBlocks = function encodeRawBlocks(contentState, rawState) {
  48. var entityMap = rawState.entityMap;
  49. var rawBlocks = [];
  50. var blockCacheRef = {};
  51. var entityCacheRef = {};
  52. var entityStorageKey = 0;
  53. contentState.getBlockMap().forEach(function (block) {
  54. block.findEntityRanges(function (character) {
  55. return character.getEntity() !== null;
  56. }, function (start) {
  57. var entityKey = block.getEntityAt(start); // Stringify to maintain order of otherwise numeric keys.
  58. var stringifiedEntityKey = DraftStringKey.stringify(entityKey); // This makes this function resilient to two entities
  59. // erroneously having the same key
  60. if (entityCacheRef[stringifiedEntityKey]) {
  61. return;
  62. }
  63. entityCacheRef[stringifiedEntityKey] = entityKey; // we need the `any` casting here since this is a temporary state
  64. // where we will later on flip the entity map and populate it with
  65. // real entity, at this stage we just need to map back the entity
  66. // key used by the BlockNode
  67. entityMap[stringifiedEntityKey] = "".concat(entityStorageKey);
  68. entityStorageKey++;
  69. });
  70. insertRawBlock(block, entityMap, rawBlocks, blockCacheRef);
  71. });
  72. return {
  73. blocks: rawBlocks,
  74. entityMap: entityMap
  75. };
  76. }; // Flip storage map so that our storage keys map to global
  77. // DraftEntity keys.
  78. var encodeRawEntityMap = function encodeRawEntityMap(contentState, rawState) {
  79. var blocks = rawState.blocks,
  80. entityMap = rawState.entityMap;
  81. var rawEntityMap = {};
  82. Object.keys(entityMap).forEach(function (key, index) {
  83. var entity = contentState.getEntity(DraftStringKey.unstringify(key));
  84. rawEntityMap[index] = {
  85. type: entity.getType(),
  86. mutability: entity.getMutability(),
  87. data: entity.getData()
  88. };
  89. });
  90. return {
  91. blocks: blocks,
  92. entityMap: rawEntityMap
  93. };
  94. };
  95. var convertFromDraftStateToRaw = function convertFromDraftStateToRaw(contentState) {
  96. var rawDraftContentState = {
  97. entityMap: {},
  98. blocks: []
  99. }; // add blocks
  100. rawDraftContentState = encodeRawBlocks(contentState, rawDraftContentState); // add entities
  101. rawDraftContentState = encodeRawEntityMap(contentState, rawDraftContentState);
  102. return rawDraftContentState;
  103. };
  104. module.exports = convertFromDraftStateToRaw;