UNPKG

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