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.
 
 
 
 

110 lines
3.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. * @flow
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. const CharacterMetadata = require("./CharacterMetadata");
  13. const ContentBlock = require("./ContentBlock");
  14. const ContentState = require("./ContentState");
  15. const EditorState = require("./EditorState");
  16. const {
  17. BOLD
  18. } = require("./SampleDraftInlineStyle");
  19. const Immutable = require("immutable");
  20. const {
  21. EMPTY
  22. } = CharacterMetadata;
  23. const getSampleSelectionMocksForTesting = (): Object => {
  24. const root = document.createElement('div');
  25. const contents = document.createElement('div');
  26. contents.setAttribute('data-contents', 'true');
  27. root.appendChild(contents);
  28. const text = ['Washington', 'Jefferson', 'Lincoln', 'Roosevelt', 'Kennedy', 'Obama'];
  29. const textA = text[0] + text[1];
  30. const textB = text[2] + text[3];
  31. const textC = text[4] + text[5];
  32. const boldChar = CharacterMetadata.create({
  33. style: BOLD
  34. });
  35. const aChars = Immutable.List(Immutable.Repeat(EMPTY, text[0].length).concat(Immutable.Repeat(boldChar, text[1].length)));
  36. const bChars = Immutable.List(Immutable.Repeat(EMPTY, text[2].length).concat(Immutable.Repeat(boldChar, text[3].length)));
  37. const cChars = Immutable.List(Immutable.Repeat(EMPTY, text[4].length).concat(Immutable.Repeat(boldChar, text[5].length)));
  38. const contentBlocks = [new ContentBlock({
  39. key: 'a',
  40. type: 'unstyled',
  41. text: textA,
  42. characterList: aChars
  43. }), new ContentBlock({
  44. key: 'b',
  45. type: 'unstyled',
  46. text: textB,
  47. characterList: bChars
  48. }), new ContentBlock({
  49. key: 'c',
  50. type: 'unstyled',
  51. text: textC,
  52. characterList: cChars
  53. })];
  54. const contentState = ContentState.createFromBlockArray(contentBlocks);
  55. const editorState = EditorState.createWithContent(contentState);
  56. const textNodes = text.map(text => {
  57. return document.createTextNode(text);
  58. });
  59. const leafChildren = textNodes.map(textNode => {
  60. const span = document.createElement('span');
  61. span.appendChild(textNode);
  62. return span;
  63. });
  64. const leafs = ['a-0-0', 'a-0-1', 'b-0-0', 'b-0-1', 'c-0-0', 'c-0-1'].map((blockKey, index) => {
  65. const span = document.createElement('span');
  66. span.setAttribute('data-offset-key', '' + blockKey);
  67. span.appendChild(leafChildren[index]);
  68. return span;
  69. });
  70. const decorators = ['a-0-0', 'b-0-0', 'c-0-0'].map((decoratorKey, index) => {
  71. const span = document.createElement('span');
  72. span.setAttribute('data-offset-key', '' + decoratorKey);
  73. span.appendChild(leafs[index * 2]);
  74. span.appendChild(leafs[index * 2 + 1]);
  75. return span;
  76. });
  77. const blocks = ['a-0-0', 'b-0-0', 'c-0-0'].map((blockKey, index) => {
  78. const outerBlockElement = document.createElement('div');
  79. const innerBlockElement = document.createElement('div');
  80. innerBlockElement.setAttribute('data-offset-key', '' + blockKey);
  81. innerBlockElement.appendChild(decorators[index]);
  82. outerBlockElement.setAttribute('data-offset-key', '' + blockKey);
  83. outerBlockElement.setAttribute('data-block', 'true');
  84. outerBlockElement.appendChild(innerBlockElement);
  85. return outerBlockElement;
  86. });
  87. blocks.forEach(blockElem => {
  88. contents.appendChild(blockElem);
  89. });
  90. return {
  91. editorState,
  92. root,
  93. contents,
  94. blocks,
  95. decorators,
  96. leafs,
  97. leafChildren,
  98. textNodes
  99. };
  100. };
  101. module.exports = getSampleSelectionMocksForTesting;