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
/
DraftEditorBlockNode.react.js
387 lines (340 loc) · 11 KB
/
DraftEditorBlockNode.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
* @emails oncall+draft_js
*
* This file is a fork of DraftEditorBlock.react.js and DraftEditorContents.react.js
*
* This is unstable and not part of the public API and should not be used by
* production systems. This file may be update/removed without notice.
*/
'use strict';
import type {BlockNodeRecord} from 'BlockNodeRecord';
import type ContentState from 'ContentState';
import type {DraftBlockRenderMap} from 'DraftBlockRenderMap';
import type {DraftDecoratorType} from 'DraftDecoratorType';
import type {DraftInlineStyle} from 'DraftInlineStyle';
import type EditorState from 'EditorState';
import type SelectionState from 'SelectionState';
import type {BidiDirection} from 'UnicodeBidiDirection';
const DraftEditorNode = require('DraftEditorNode.react');
const DraftOffsetKey = require('DraftOffsetKey');
const React = require('React');
const ReactDOM = require('ReactDOM');
const Scroll = require('Scroll');
const Style = require('Style');
const getElementPosition = require('getElementPosition');
const getScrollPosition = require('getScrollPosition');
const getViewportDimensions = require('getViewportDimensions');
const Immutable = require('immutable');
const invariant = require('invariant');
const isHTMLElement = require('isHTMLElement');
const SCROLL_BUFFER = 10;
const {List} = Immutable;
// we should harden up the bellow flow types to make them more strict
type CustomRenderConfig = Object;
type DraftRenderConfig = Object;
type BlockRenderFn = (block: BlockNodeRecord) => ?Object;
type BlockStyleFn = (block: BlockNodeRecord) => string;
type Props = {
block: BlockNodeRecord,
blockProps?: Object,
blockRenderMap: DraftBlockRenderMap,
blockRendererFn: BlockRenderFn,
blockStyleFn: BlockStyleFn,
contentState: ContentState,
customStyleFn: (style: DraftInlineStyle, block: BlockNodeRecord) => ?Object,
customStyleMap: Object,
decorator: ?DraftDecoratorType,
direction: BidiDirection,
editorKey: string,
editorState: EditorState,
forceSelection: boolean,
selection: SelectionState,
startIndent?: boolean,
tree: List<any>,
...
};
/**
* Return whether a block overlaps with either edge of the `SelectionState`.
*/
const isBlockOnSelectionEdge = (
selection: SelectionState,
key: string,
): boolean => {
return selection.getAnchorKey() === key || selection.getFocusKey() === key;
};
/**
* We will use this helper to identify blocks that need to be wrapped but have siblings that
* also share the same wrapper element, this way we can do the wrapping once the last sibling
* is added.
*/
const shouldNotAddWrapperElement = (
block: BlockNodeRecord,
contentState: ContentState,
): boolean => {
const nextSiblingKey = block.getNextSiblingKey();
return nextSiblingKey
? contentState.getBlockForKey(nextSiblingKey).getType() === block.getType()
: false;
};
const applyWrapperElementToSiblings = (
wrapperTemplate: *,
Element: string,
nodes: Array<React.Node>,
): Array<React.Node> => {
const wrappedSiblings = [];
// we check back until we find a sibbling that does not have same wrapper
for (const sibling: any of nodes.reverse()) {
if (sibling.type !== Element) {
break;
}
wrappedSiblings.push(sibling);
}
// we now should remove from acc the wrappedSiblings and add them back under same wrap
nodes.splice(nodes.indexOf(wrappedSiblings[0]), wrappedSiblings.length + 1);
const childrenIs = wrappedSiblings.reverse();
const key = childrenIs[0].key;
nodes.push(
React.cloneElement(
wrapperTemplate,
{
key: `${key}-wrap`,
'data-offset-key': DraftOffsetKey.encode(key, 0, 0),
},
childrenIs,
),
);
return nodes;
};
const getDraftRenderConfig = (
block: BlockNodeRecord,
blockRenderMap: DraftBlockRenderMap,
): DraftRenderConfig => {
const configForType =
blockRenderMap.get(block.getType()) || blockRenderMap.get('unstyled');
const wrapperTemplate = configForType.wrapper;
const Element =
configForType.element || blockRenderMap.get('unstyled').element;
return {
Element,
wrapperTemplate,
};
};
const getCustomRenderConfig = (
block: BlockNodeRecord,
blockRendererFn: BlockRenderFn,
): CustomRenderConfig => {
const customRenderer = blockRendererFn(block);
if (!customRenderer) {
return {};
}
const {
component: CustomComponent,
props: customProps,
editable: customEditable,
} = customRenderer;
return {
CustomComponent,
customProps,
customEditable,
};
};
const getElementPropsConfig = (
block: BlockNodeRecord,
editorKey: string,
offsetKey: string,
blockStyleFn: BlockStyleFn,
customConfig: *,
): Object => {
let elementProps: Object = {
'data-block': true,
'data-editor': editorKey,
'data-offset-key': offsetKey,
key: block.getKey(),
};
const customClass = blockStyleFn(block);
if (customClass) {
elementProps.className = customClass;
}
if (customConfig.customEditable !== undefined) {
elementProps = {
...elementProps,
contentEditable: customConfig.customEditable,
suppressContentEditableWarning: true,
};
}
return elementProps;
};
class DraftEditorBlockNode extends React.Component<Props> {
shouldComponentUpdate(nextProps: Props): boolean {
const {block, direction, tree} = this.props;
const isContainerNode = !block.getChildKeys().isEmpty();
const blockHasChanged =
block !== nextProps.block ||
tree !== nextProps.tree ||
direction !== nextProps.direction ||
(isBlockOnSelectionEdge(nextProps.selection, nextProps.block.getKey()) &&
nextProps.forceSelection);
// if we have children at this stage we always re-render container nodes
// else if its a root node we avoid re-rendering by checking for block updates
return isContainerNode || blockHasChanged;
}
/**
* When a block is mounted and overlaps the selection state, we need to make
* sure that the cursor is visible to match native behavior. This may not
* be the case if the user has pressed `RETURN` or pasted some content, since
* programatically creating these new blocks and setting the DOM selection
* will miss out on the browser natively scrolling to that position.
*
* To replicate native behavior, if the block overlaps the selection state
* on mount, force the scroll position. Check the scroll state of the scroll
* parent, and adjust it to align the entire block to the bottom of the
* scroll parent.
*/
componentDidMount(): void {
const selection = this.props.selection;
const endKey = selection.getEndKey();
if (!selection.getHasFocus() || endKey !== this.props.block.getKey()) {
return;
}
const blockNode = ReactDOM.findDOMNode(this);
const scrollParent = Style.getScrollParent(blockNode);
const scrollPosition = getScrollPosition(scrollParent);
let scrollDelta;
if (scrollParent === window) {
const nodePosition = getElementPosition(blockNode);
const nodeBottom = nodePosition.y + nodePosition.height;
const viewportHeight = getViewportDimensions().height;
scrollDelta = nodeBottom - viewportHeight;
if (scrollDelta > 0) {
window.scrollTo(
scrollPosition.x,
scrollPosition.y + scrollDelta + SCROLL_BUFFER,
);
}
} else {
invariant(isHTMLElement(blockNode), 'blockNode is not an HTMLElement');
const htmlBlockNode: HTMLElement = (blockNode: any);
const blockBottom = htmlBlockNode.offsetHeight + htmlBlockNode.offsetTop;
const scrollBottom = scrollParent.offsetHeight + scrollPosition.y;
scrollDelta = blockBottom - scrollBottom;
if (scrollDelta > 0) {
Scroll.setTop(
scrollParent,
Scroll.getTop(scrollParent) + scrollDelta + SCROLL_BUFFER,
);
}
}
}
render(): React.Node {
const {
block,
blockRenderMap,
blockRendererFn,
blockStyleFn,
contentState,
decorator,
editorKey,
editorState,
customStyleFn,
customStyleMap,
direction,
forceSelection,
selection,
tree,
} = this.props;
let children = null;
if (block.children.size) {
children = block.children.reduce((acc, key) => {
const offsetKey = DraftOffsetKey.encode(key, 0, 0);
const child = contentState.getBlockForKey(key);
const customConfig = getCustomRenderConfig(child, blockRendererFn);
const Component = customConfig.CustomComponent || DraftEditorBlockNode;
const {Element, wrapperTemplate} = getDraftRenderConfig(
child,
blockRenderMap,
);
const elementProps = getElementPropsConfig(
child,
editorKey,
offsetKey,
blockStyleFn,
customConfig,
);
const childProps = {
...this.props,
tree: editorState.getBlockTree(key),
blockProps: customConfig.customProps,
offsetKey,
block: child,
};
acc.push(
React.createElement(
Element,
elementProps,
<Component {...childProps} />,
),
);
if (
!wrapperTemplate ||
shouldNotAddWrapperElement(child, contentState)
) {
return acc;
}
// if we are here it means we are the last block
// that has a wrapperTemplate so we should wrap itself
// and all other previous siblings that share the same wrapper
applyWrapperElementToSiblings(wrapperTemplate, Element, acc);
return acc;
}, []);
}
const blockKey = block.getKey();
const offsetKey = DraftOffsetKey.encode(blockKey, 0, 0);
const customConfig = getCustomRenderConfig(block, blockRendererFn);
const Component = customConfig.CustomComponent;
const blockNode =
Component != null ? (
<Component
{...this.props}
tree={editorState.getBlockTree(blockKey)}
blockProps={customConfig.customProps}
offsetKey={offsetKey}
block={block}
/>
) : (
<DraftEditorNode
block={block}
children={children}
contentState={contentState}
customStyleFn={customStyleFn}
customStyleMap={customStyleMap}
decorator={decorator}
direction={direction}
forceSelection={forceSelection}
hasSelection={isBlockOnSelectionEdge(selection, blockKey)}
selection={selection}
tree={tree}
/>
);
if (block.getParentKey()) {
return blockNode;
}
const {Element} = getDraftRenderConfig(block, blockRenderMap);
const elementProps = getElementPropsConfig(
block,
editorKey,
offsetKey,
blockStyleFn,
customConfig,
);
// root block nodes needs to be wrapped
return React.createElement(Element, elementProps, blockNode);
}
}
module.exports = DraftEditorBlockNode;