Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

getSampleSelectionMocksForTestingNestedBlocks.js 3.6 KiB

3 lat temu
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. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. var ContentBlockNode = require("./ContentBlockNode");
  13. var ContentState = require("./ContentState");
  14. var EditorState = require("./EditorState");
  15. var Immutable = require("immutable");
  16. var getSampleSelectionMocksForTestingNestedBlocks = function getSampleSelectionMocksForTestingNestedBlocks() {
  17. var root = document.createElement('div');
  18. var contents = document.createElement('div');
  19. contents.setAttribute('data-contents', 'true');
  20. root.appendChild(contents);
  21. var text = [null, 'beta', null, 'delta'];
  22. var offsetKeys = ['a-0-0', 'b-0-0', 'c-0-0', 'd-0-0'];
  23. var 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. var contentState = ContentState.createFromBlockArray(contentBlocks);
  41. var editorState = EditorState.createWithContent(contentState);
  42. var textNodes = text.map(function (text) {
  43. if (!text) {
  44. return null;
  45. }
  46. return document.createTextNode(text);
  47. });
  48. var leafChildren = textNodes.map(function (textNode) {
  49. if (!textNode) {
  50. return null;
  51. }
  52. var span = document.createElement('span');
  53. span.appendChild(textNode);
  54. return span;
  55. });
  56. var leafs = leafChildren.map(function (leafChild, index) {
  57. if (!leafChild) {
  58. return null;
  59. }
  60. var blockKey = offsetKeys[index];
  61. var span = document.createElement('span');
  62. span.setAttribute('data-offset-key', blockKey);
  63. span.appendChild(leafChild);
  64. return span;
  65. });
  66. var decorators = leafs.map(function (leaf, index) {
  67. if (!leaf) {
  68. return null;
  69. }
  70. var blockKey = offsetKeys[index];
  71. var span = document.createElement('span');
  72. span.setAttribute('data-offset-key', blockKey);
  73. span.appendChild(leaf);
  74. return span;
  75. });
  76. var blocks = offsetKeys.map(function (blockKey, index) {
  77. var outerBlockElement = document.createElement('div');
  78. var 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. var 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. var blockCacheRef = {};
  90. blocks.forEach(function (blockElem, index) {
  91. var currentBlock = contentBlocks[index];
  92. var 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: editorState,
  102. root: root,
  103. contents: contents,
  104. blocks: blocks,
  105. decorators: decorators,
  106. leafs: leafs,
  107. leafChildren: leafChildren,
  108. textNodes: textNodes
  109. };
  110. };
  111. module.exports = getSampleSelectionMocksForTestingNestedBlocks;