This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathDraftEditorContents-core.react.js
277 lines (244 loc) · 8.48 KB
/
DraftEditorContents-core.react.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule DraftEditorContents-core.react
* @format
* @flow
*/
'use strict';
import type {BlockNodeRecord} from 'BlockNodeRecord';
import type {DraftBlockRenderMap} from 'DraftBlockRenderMap';
import type {DraftInlineStyle} from 'DraftInlineStyle';
import type {BidiDirection} from 'UnicodeBidiDirection';
const DraftEditorBlock = require('DraftEditorBlock.react');
const DraftOffsetKey = require('DraftOffsetKey');
const EditorState = require('EditorState');
const React = require('React');
const cx = require('cx');
const joinClasses = require('joinClasses');
const nullthrows = require('nullthrows');
type Props = {
blockRenderMap: DraftBlockRenderMap,
blockRendererFn: (block: BlockNodeRecord) => ?Object,
blockStyleFn?: (block: BlockNodeRecord) => string,
customStyleFn?: (style: DraftInlineStyle, block: BlockNodeRecord) => ?Object,
customStyleMap?: Object,
editorKey?: string,
editorState: EditorState,
textDirectionality?: BidiDirection,
};
/**
* Provide default styling for list items. This way, lists will be styled with
* proper counters and indentation even if the caller does not specify
* their own styling at all. If more than five levels of nesting are needed,
* the necessary CSS classes can be provided via `blockStyleFn` configuration.
*/
const getListItemClasses = (
type: string,
depth: number,
shouldResetCount: boolean,
direction: BidiDirection,
): string => {
return cx({
'public/DraftStyleDefault/unorderedListItem':
type === 'unordered-list-item',
'public/DraftStyleDefault/orderedListItem': type === 'ordered-list-item',
'public/DraftStyleDefault/reset': shouldResetCount,
'public/DraftStyleDefault/depth0': depth === 0,
'public/DraftStyleDefault/depth1': depth === 1,
'public/DraftStyleDefault/depth2': depth === 2,
'public/DraftStyleDefault/depth3': depth === 3,
'public/DraftStyleDefault/depth4': depth === 4,
'public/DraftStyleDefault/listLTR': direction === 'LTR',
'public/DraftStyleDefault/listRTL': direction === 'RTL',
});
};
/**
* `DraftEditorContents` is the container component for all block components
* rendered for a `DraftEditor`. It is optimized to aggressively avoid
* re-rendering blocks whenever possible.
*
* This component is separate from `DraftEditor` because certain props
* (for instance, ARIA props) must be allowed to update without affecting
* the contents of the editor.
*/
class DraftEditorContents extends React.Component<Props> {
shouldComponentUpdate(nextProps: Props): boolean {
const prevEditorState = this.props.editorState;
const nextEditorState = nextProps.editorState;
const prevDirectionMap = prevEditorState.getDirectionMap();
const nextDirectionMap = nextEditorState.getDirectionMap();
// Text direction has changed for one or more blocks. We must re-render.
if (prevDirectionMap !== nextDirectionMap) {
return true;
}
const didHaveFocus = prevEditorState.getSelection().getHasFocus();
const nowHasFocus = nextEditorState.getSelection().getHasFocus();
if (didHaveFocus !== nowHasFocus) {
return true;
}
const nextNativeContent = nextEditorState.getNativelyRenderedContent();
const wasComposing = prevEditorState.isInCompositionMode();
const nowComposing = nextEditorState.isInCompositionMode();
// If the state is unchanged or we're currently rendering a natively
// rendered state, there's nothing new to be done.
if (
prevEditorState === nextEditorState ||
(nextNativeContent !== null &&
nextEditorState.getCurrentContent() === nextNativeContent) ||
(wasComposing && nowComposing)
) {
return false;
}
const prevContent = prevEditorState.getCurrentContent();
const nextContent = nextEditorState.getCurrentContent();
const prevDecorator = prevEditorState.getDecorator();
const nextDecorator = nextEditorState.getDecorator();
return (
wasComposing !== nowComposing ||
prevContent !== nextContent ||
prevDecorator !== nextDecorator ||
nextEditorState.mustForceSelection()
);
}
render(): React.Node {
const {
blockRenderMap,
blockRendererFn,
blockStyleFn,
customStyleMap,
customStyleFn,
editorState,
editorKey,
textDirectionality,
} = this.props;
const content = editorState.getCurrentContent();
const selection = editorState.getSelection();
const forceSelection = editorState.mustForceSelection();
const decorator = editorState.getDecorator();
const directionMap = nullthrows(editorState.getDirectionMap());
const blocksAsArray = content.getBlocksAsArray();
const processedBlocks = [];
let currentDepth = null;
let lastWrapperTemplate = null;
for (let ii = 0; ii < blocksAsArray.length; ii++) {
const block = blocksAsArray[ii];
const key = block.getKey();
const blockType = block.getType();
const customRenderer = blockRendererFn(block);
let CustomComponent, customProps, customEditable;
if (customRenderer) {
CustomComponent = customRenderer.component;
customProps = customRenderer.props;
customEditable = customRenderer.editable;
}
const direction = textDirectionality
? textDirectionality
: directionMap.get(key);
const offsetKey = DraftOffsetKey.encode(key, 0, 0);
const componentProps = {
contentState: content,
block,
blockProps: customProps,
blockStyleFn,
customStyleMap,
customStyleFn,
decorator,
direction,
forceSelection,
key,
offsetKey,
selection,
tree: editorState.getBlockTree(key),
};
const configForType =
blockRenderMap.get(blockType) || blockRenderMap.get('unstyled');
const wrapperTemplate = configForType.wrapper;
const Element =
configForType.element || blockRenderMap.get('unstyled').element;
const depth = block.getDepth();
let className = '';
if (blockStyleFn) {
className = blockStyleFn(block);
}
// List items are special snowflakes, since we handle nesting and
// counters manually.
if (Element === 'li') {
const shouldResetCount =
lastWrapperTemplate !== wrapperTemplate ||
currentDepth === null ||
depth > currentDepth;
className = joinClasses(
className,
getListItemClasses(blockType, depth, shouldResetCount, direction),
);
}
const Component = CustomComponent || DraftEditorBlock;
let childProps = {
className,
'data-block': true,
'data-editor': editorKey,
'data-offset-key': offsetKey,
key,
};
if (customEditable !== undefined) {
childProps = {
...childProps,
contentEditable: customEditable,
suppressContentEditableWarning: true,
};
}
const child = React.createElement(
Element,
childProps,
<Component {...componentProps} />,
);
processedBlocks.push({
block: child,
wrapperTemplate,
key,
offsetKey,
});
if (wrapperTemplate) {
currentDepth = block.getDepth();
} else {
currentDepth = null;
}
lastWrapperTemplate = wrapperTemplate;
}
// Group contiguous runs of blocks that have the same wrapperTemplate
const outputBlocks = [];
for (let ii = 0; ii < processedBlocks.length; ) {
const info: any = processedBlocks[ii];
if (info.wrapperTemplate) {
const blocks = [];
do {
blocks.push(processedBlocks[ii].block);
ii++;
} while (
ii < processedBlocks.length &&
processedBlocks[ii].wrapperTemplate === info.wrapperTemplate
);
const wrapperElement = React.cloneElement(
info.wrapperTemplate,
{
key: info.key + '-wrap',
'data-offset-key': info.offsetKey,
},
blocks,
);
outputBlocks.push(wrapperElement);
} else {
outputBlocks.push(info.block);
ii++;
}
}
return <div data-contents="true">{outputBlocks}</div>;
}
}
module.exports = DraftEditorContents;