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.

getSampleSelectionMocksForTestingNestedBlocks.js.flow 3.5 KiB

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. 'use strict';
  12. const ContentBlockNode = require("./ContentBlockNode");
  13. const ContentState = require("./ContentState");
  14. const EditorState = require("./EditorState");
  15. const Immutable = require("immutable");
  16. const getSampleSelectionMocksForTestingNestedBlocks = (): Object => {
  17. const root = document.createElement('div');
  18. const contents = document.createElement('div');
  19. contents.setAttribute('data-contents', 'true');
  20. root.appendChild(contents);
  21. const text = [null, 'beta', null, 'delta'];
  22. const offsetKeys = ['a-0-0', 'b-0-0', 'c-0-0', 'd-0-0'];
  23. const contentBlocks = [new ContentBlockNode({
  24. key: 'a',
  25. nextSibling: 'c',
  26. children: Immutable.List.of('b')
  27. }), new ContentBlockNode({
  28. key: 'b',
  29. parent: 'a',
  30. text: text[1]
  31. }), new ContentBlockNode({
  32. key: 'c',
  33. prevSibling: 'a',
  34. children: Immutable.List.of('d')
  35. }), new ContentBlockNode({
  36. key: 'd',
  37. parent: 'c',
  38. text: text[3]
  39. })];
  40. const contentState = ContentState.createFromBlockArray(contentBlocks);
  41. const editorState = EditorState.createWithContent(contentState);
  42. const textNodes = text.map(text => {
  43. if (!text) {
  44. return null;
  45. }
  46. return document.createTextNode(text);
  47. });
  48. const leafChildren = textNodes.map(textNode => {
  49. if (!textNode) {
  50. return null;
  51. }
  52. const span = document.createElement('span');
  53. span.appendChild(textNode);
  54. return span;
  55. });
  56. const leafs = leafChildren.map((leafChild, index) => {
  57. if (!leafChild) {
  58. return null;
  59. }
  60. const blockKey = offsetKeys[index];
  61. const span = document.createElement('span');
  62. span.setAttribute('data-offset-key', blockKey);
  63. span.appendChild(leafChild);
  64. return span;
  65. });
  66. const decorators = leafs.map((leaf, index) => {
  67. if (!leaf) {
  68. return null;
  69. }
  70. const blockKey = offsetKeys[index];
  71. const span = document.createElement('span');
  72. span.setAttribute('data-offset-key', blockKey);
  73. span.appendChild(leaf);
  74. return span;
  75. });
  76. const blocks = offsetKeys.map((blockKey, index) => {
  77. const outerBlockElement = document.createElement('div');
  78. const innerBlockElement = document.createElement('div');
  79. innerBlockElement.setAttribute('data-offset-key', blockKey);
  80. outerBlockElement.setAttribute('data-offset-key', blockKey);
  81. outerBlockElement.setAttribute('data-block', 'true');
  82. const decorator = decorators[index]; // only leaf nodes can have text
  83. if (decorator) {
  84. innerBlockElement.appendChild(decorator);
  85. }
  86. outerBlockElement.appendChild(innerBlockElement);
  87. return outerBlockElement;
  88. });
  89. const blockCacheRef = {};
  90. blocks.forEach((blockElem, index) => {
  91. const currentBlock = contentBlocks[index];
  92. const parentKey = currentBlock.getParentKey(); // add this block reference to the cache lookup ref
  93. blockCacheRef[currentBlock.getKey()] = blockElem; // root nodes get appended directly to the contents block
  94. if (!parentKey) {
  95. contents.appendChild(blockElem);
  96. return;
  97. } // append to to the innerBlockElement of the parent block
  98. blockCacheRef[parentKey].firstChild.appendChild(blockElem);
  99. });
  100. return {
  101. editorState,
  102. root,
  103. contents,
  104. blocks,
  105. decorators,
  106. leafs,
  107. leafChildren,
  108. textNodes
  109. };
  110. };
  111. module.exports = getSampleSelectionMocksForTestingNestedBlocks;