You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * This is unstable and not part of the public API and should not be used by
  15. * production systems. This file may be update/removed without notice.
  16. */
  17. var generateRandomKey = require("./generateRandomKey");
  18. var invariant = require("fbjs/lib/invariant");
  19. var traverseInDepthOrder = function traverseInDepthOrder(blocks, fn) {
  20. var stack = [].concat(blocks).reverse();
  21. while (stack.length) {
  22. var _block = stack.pop();
  23. fn(_block);
  24. var children = _block.children;
  25. !Array.isArray(children) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Invalid tree raw block') : invariant(false) : void 0;
  26. stack = stack.concat([].concat(children.reverse()));
  27. }
  28. };
  29. var isListBlock = function isListBlock(block) {
  30. if (!(block && block.type)) {
  31. return false;
  32. }
  33. var type = block.type;
  34. return type === 'unordered-list-item' || type === 'ordered-list-item';
  35. };
  36. var addDepthToChildren = function addDepthToChildren(block) {
  37. if (Array.isArray(block.children)) {
  38. block.children = block.children.map(function (child) {
  39. return child.type === block.type ? _objectSpread({}, child, {
  40. depth: (block.depth || 0) + 1
  41. }) : child;
  42. });
  43. }
  44. };
  45. /**
  46. * This adapter is intended to be be used as an adapter to draft tree data
  47. *
  48. * draft state <=====> draft tree state
  49. */
  50. var DraftTreeAdapter = {
  51. /**
  52. * Converts from a tree raw state back to draft raw state
  53. */
  54. fromRawTreeStateToRawState: function fromRawTreeStateToRawState(draftTreeState) {
  55. var blocks = draftTreeState.blocks;
  56. var transformedBlocks = [];
  57. !Array.isArray(blocks) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Invalid raw state') : invariant(false) : void 0;
  58. if (!Array.isArray(blocks) || !blocks.length) {
  59. return draftTreeState;
  60. }
  61. traverseInDepthOrder(blocks, function (block) {
  62. var newBlock = _objectSpread({}, block);
  63. if (isListBlock(block)) {
  64. newBlock.depth = newBlock.depth || 0;
  65. addDepthToChildren(block); // if it's a non-leaf node, we don't do anything else
  66. if (block.children != null && block.children.length > 0) {
  67. return;
  68. }
  69. }
  70. delete newBlock.children;
  71. transformedBlocks.push(newBlock);
  72. });
  73. draftTreeState.blocks = transformedBlocks;
  74. return _objectSpread({}, draftTreeState, {
  75. blocks: transformedBlocks
  76. });
  77. },
  78. /**
  79. * Converts from draft raw state to tree draft state
  80. */
  81. fromRawStateToRawTreeState: function fromRawStateToRawTreeState(draftState) {
  82. var transformedBlocks = [];
  83. var parentStack = [];
  84. draftState.blocks.forEach(function (block) {
  85. var isList = isListBlock(block);
  86. var depth = block.depth || 0;
  87. var treeBlock = _objectSpread({}, block, {
  88. children: []
  89. });
  90. if (!isList) {
  91. transformedBlocks.push(treeBlock);
  92. return;
  93. }
  94. var lastParent = parentStack[0]; // block is non-nested & there are no nested blocks, directly push block
  95. if (lastParent == null && depth === 0) {
  96. transformedBlocks.push(treeBlock); // block is first nested block or previous nested block is at a lower level
  97. } else if (lastParent == null || lastParent.depth < depth - 1) {
  98. // create new parent block
  99. var newParent = {
  100. key: generateRandomKey(),
  101. text: '',
  102. depth: depth - 1,
  103. type: block.type,
  104. children: [],
  105. entityRanges: [],
  106. inlineStyleRanges: []
  107. };
  108. parentStack.unshift(newParent);
  109. if (depth === 1) {
  110. // add as a root-level block
  111. transformedBlocks.push(newParent);
  112. } else if (lastParent != null) {
  113. // depth > 1 => also add as previous parent's child
  114. lastParent.children.push(newParent);
  115. }
  116. newParent.children.push(treeBlock);
  117. } else if (lastParent.depth === depth - 1) {
  118. // add as child of last parent
  119. lastParent.children.push(treeBlock);
  120. } else {
  121. // pop out parents at levels above this one from the parent stack
  122. while (lastParent != null && lastParent.depth >= depth) {
  123. parentStack.shift();
  124. lastParent = parentStack[0];
  125. }
  126. if (depth > 0) {
  127. lastParent.children.push(treeBlock);
  128. } else {
  129. transformedBlocks.push(treeBlock);
  130. }
  131. }
  132. });
  133. return _objectSpread({}, draftState, {
  134. blocks: transformedBlocks
  135. });
  136. }
  137. };
  138. module.exports = DraftTreeAdapter;