@lexical/react 0.1.16 → 0.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DEPRECATED_useLexicalAutoFormatter.dev.js +5 -729
- package/DEPRECATED_useLexicalAutoFormatter.prod.js +1 -19
- package/DEPRECATED_useLexicalCanShowPlaceholder.dev.js +1 -1
- package/DEPRECATED_useLexicalCanShowPlaceholder.prod.js +1 -1
- package/DEPRECATED_useLexicalCharacterLimit.dev.js +9 -10
- package/DEPRECATED_useLexicalCharacterLimit.prod.js +7 -7
- package/DEPRECATED_useLexicalDecorators.dev.js +1 -1
- package/DEPRECATED_useLexicalDecorators.prod.js +1 -1
- package/DEPRECATED_useLexicalEditorEvents.dev.js +1 -1
- package/DEPRECATED_useLexicalEditorEvents.prod.js +1 -1
- package/DEPRECATED_useLexicalHistory.dev.js +5 -307
- package/DEPRECATED_useLexicalHistory.prod.js +1 -7
- package/DEPRECATED_useLexicalList.dev.js +29 -25
- package/DEPRECATED_useLexicalList.prod.js +3 -1
- package/DEPRECATED_useLexicalPlainText.dev.js +8 -662
- package/DEPRECATED_useLexicalPlainText.prod.js +2 -14
- package/DEPRECATED_useLexicalRichText.dev.js +8 -747
- package/DEPRECATED_useLexicalRichText.prod.js +2 -17
- package/LICENSE +1 -1
- package/{withSubscriptions.prod.js → LexicalAutoFocusPlugin.d.ts} +3 -1
- package/{withSubscriptions.dev.js → LexicalAutoFocusPlugin.dev.js} +10 -5
- package/LexicalAutoFocusPlugin.js +9 -0
- package/{LexicalAutoFormatterPlugin.js.flow → LexicalAutoFocusPlugin.js.flow} +1 -1
- package/LexicalAutoFocusPlugin.prod.js +7 -0
- package/LexicalAutoLinkPlugin.dev.js +12 -15
- package/LexicalAutoLinkPlugin.prod.js +6 -6
- package/LexicalAutoScrollPlugin.d.ts +13 -0
- package/LexicalAutoScrollPlugin.dev.js +82 -0
- package/LexicalAutoScrollPlugin.js +9 -0
- package/{withSubscriptions.d.ts → LexicalAutoScrollPlugin.js.flow} +5 -5
- package/LexicalAutoScrollPlugin.prod.js +8 -0
- package/LexicalCharacterLimitPlugin.dev.js +9 -10
- package/LexicalCharacterLimitPlugin.prod.js +7 -7
- package/LexicalClearEditorPlugin.dev.js +15 -19
- package/LexicalClearEditorPlugin.prod.js +1 -1
- package/LexicalCollaborationPlugin.d.ts +6 -3
- package/LexicalCollaborationPlugin.dev.js +30 -41
- package/LexicalCollaborationPlugin.js.flow +7 -4
- package/LexicalCollaborationPlugin.prod.js +10 -9
- package/LexicalComposer.d.ts +2 -2
- package/LexicalComposer.dev.js +3 -19
- package/LexicalComposer.js.flow +2 -2
- package/LexicalComposer.prod.js +2 -3
- package/LexicalContentEditable.dev.js +3 -1
- package/LexicalContentEditable.prod.js +2 -2
- package/LexicalHashtagPlugin.dev.js +20 -94
- package/LexicalHashtagPlugin.prod.js +4 -7
- package/LexicalHistoryPlugin.dev.js +5 -307
- package/LexicalHistoryPlugin.prod.js +1 -7
- package/LexicalHorizontalRuleNode.d.ts +3 -1
- package/LexicalHorizontalRuleNode.dev.js +2 -0
- package/LexicalHorizontalRuleNode.js.flow +6 -2
- package/LexicalHorizontalRuleNode.prod.js +2 -2
- package/LexicalLinkPlugin.dev.js +19 -20
- package/LexicalLinkPlugin.prod.js +4 -3
- package/LexicalListPlugin.dev.js +29 -25
- package/LexicalListPlugin.prod.js +3 -2
- package/{LexicalAutoFormatterPlugin.d.ts → LexicalMarkdownShortcutPlugin.d.ts} +1 -1
- package/LexicalMarkdownShortcutPlugin.dev.js +42 -0
- package/LexicalMarkdownShortcutPlugin.js +9 -0
- package/{withSubscriptions.js.flow → LexicalMarkdownShortcutPlugin.js.flow} +1 -4
- package/LexicalMarkdownShortcutPlugin.prod.js +7 -0
- package/LexicalNestedComposer.dev.js +20 -15
- package/LexicalNestedComposer.prod.js +3 -3
- package/LexicalOnChangePlugin.dev.js +1 -1
- package/LexicalOnChangePlugin.prod.js +1 -1
- package/LexicalPlainTextPlugin.dev.js +6 -359
- package/LexicalPlainTextPlugin.prod.js +4 -11
- package/LexicalRichTextPlugin.dev.js +6 -444
- package/LexicalRichTextPlugin.prod.js +4 -12
- package/LexicalTablePlugin.dev.js +27 -30
- package/LexicalTablePlugin.prod.js +3 -3
- package/LexicalTreeView.dev.js +1 -1
- package/LexicalTreeView.prod.js +1 -1
- package/package.json +17 -14
- package/useLexicalIsTextContentEmpty.dev.js +1 -1
- package/useLexicalIsTextContentEmpty.prod.js +1 -1
- package/useLexicalNodeSelection.dev.js +1 -1
- package/useLexicalNodeSelection.prod.js +1 -1
- package/useLexicalTextEntity.d.ts +19 -0
- package/useLexicalTextEntity.dev.js +29 -0
- package/{withSubscriptions.js → useLexicalTextEntity.js} +2 -2
- package/useLexicalTextEntity.js.flow +18 -0
- package/useLexicalTextEntity.prod.js +7 -0
- package/LexicalAutoFormatterPlugin.dev.js +0 -766
- package/LexicalAutoFormatterPlugin.js +0 -9
- package/LexicalAutoFormatterPlugin.prod.js +0 -25
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
*/
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
-
var
|
|
10
|
-
var lexical = require('lexical');
|
|
9
|
+
var history = require('@lexical/history');
|
|
11
10
|
var react = require('react');
|
|
12
|
-
var
|
|
13
|
-
var
|
|
11
|
+
var dragon = require('@lexical/dragon');
|
|
12
|
+
var plainText = require('@lexical/plain-text');
|
|
13
|
+
var utils = require('@lexical/utils');
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
@@ -20,312 +20,11 @@ var selection = require('@lexical/selection');
|
|
|
20
20
|
*
|
|
21
21
|
*
|
|
22
22
|
*/
|
|
23
|
-
const HISTORY_MERGE = 0;
|
|
24
|
-
const HISTORY_PUSH = 1;
|
|
25
|
-
const DISCARD_HISTORY_CANDIDATE = 2;
|
|
26
|
-
const OTHER = 0;
|
|
27
|
-
const COMPOSING_CHARACTER = 1;
|
|
28
|
-
const INSERT_CHARACTER_AFTER_SELECTION = 2;
|
|
29
|
-
const DELETE_CHARACTER_BEFORE_SELECTION = 3;
|
|
30
|
-
const DELETE_CHARACTER_AFTER_SELECTION = 4;
|
|
31
|
-
const EditorPriority = 0;
|
|
32
|
-
|
|
33
|
-
function getDirtyNodes(editorState, dirtyLeavesSet, dirtyElementsSet) {
|
|
34
|
-
const dirtyLeaves = Array.from(dirtyLeavesSet);
|
|
35
|
-
const dirtyElements = Array.from(dirtyElementsSet);
|
|
36
|
-
const nodeMap = editorState._nodeMap;
|
|
37
|
-
const nodes = [];
|
|
38
|
-
|
|
39
|
-
for (let i = 0; i < dirtyLeaves.length; i++) {
|
|
40
|
-
const dirtyLeafKey = dirtyLeaves[i];
|
|
41
|
-
const dirtyLeaf = nodeMap.get(dirtyLeafKey);
|
|
42
|
-
|
|
43
|
-
if (dirtyLeaf !== undefined) {
|
|
44
|
-
nodes.push(dirtyLeaf);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
for (let i = 0; i < dirtyElements.length; i++) {
|
|
49
|
-
const intentionallyMarkedAsDirty = dirtyElements[i][1];
|
|
50
|
-
|
|
51
|
-
if (!intentionallyMarkedAsDirty) {
|
|
52
|
-
continue;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const dirtyElementKey = dirtyElements[i][0];
|
|
56
|
-
const dirtyElement = nodeMap.get(dirtyElementKey);
|
|
57
|
-
|
|
58
|
-
if (dirtyElement !== undefined && !lexical.$isRootNode(dirtyElement)) {
|
|
59
|
-
nodes.push(dirtyElement);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return nodes;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function getChangeType(prevEditorState, nextEditorState, dirtyLeavesSet, dirtyElementsSet, isComposing) {
|
|
67
|
-
if (prevEditorState === null || dirtyLeavesSet.size === 0 && dirtyElementsSet.size === 0) {
|
|
68
|
-
return OTHER;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const nextSelection = nextEditorState._selection;
|
|
72
|
-
const prevSelection = prevEditorState._selection;
|
|
73
|
-
|
|
74
|
-
if (isComposing) {
|
|
75
|
-
return COMPOSING_CHARACTER;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (!lexical.$isRangeSelection(nextSelection) || !lexical.$isRangeSelection(prevSelection) || !prevSelection.isCollapsed() || !nextSelection.isCollapsed()) {
|
|
79
|
-
return OTHER;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const dirtyNodes = getDirtyNodes(nextEditorState, dirtyLeavesSet, dirtyElementsSet);
|
|
83
|
-
|
|
84
|
-
if (dirtyNodes.length === 0) {
|
|
85
|
-
return OTHER;
|
|
86
|
-
} // Catching the case when inserting new text node into an element (e.g. first char in paragraph/list),
|
|
87
|
-
// or after existing node.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (dirtyNodes.length > 1) {
|
|
91
|
-
const nextNodeMap = nextEditorState._nodeMap;
|
|
92
|
-
const nextAnchorNode = nextNodeMap.get(nextSelection.anchor.key);
|
|
93
|
-
const prevAnchorNode = nextNodeMap.get(prevSelection.anchor.key);
|
|
94
|
-
|
|
95
|
-
if (nextAnchorNode && prevAnchorNode && !prevEditorState._nodeMap.has(nextAnchorNode.__key) && lexical.$isTextNode(nextAnchorNode) && nextAnchorNode.__text.length === 1 && nextSelection.anchor.offset === 1) {
|
|
96
|
-
return INSERT_CHARACTER_AFTER_SELECTION;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return OTHER;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const nextDirtyNode = dirtyNodes[0];
|
|
103
|
-
|
|
104
|
-
const prevDirtyNode = prevEditorState._nodeMap.get(nextDirtyNode.__key);
|
|
105
|
-
|
|
106
|
-
if (!lexical.$isTextNode(prevDirtyNode) || !lexical.$isTextNode(nextDirtyNode) || prevDirtyNode.__mode !== nextDirtyNode.__mode) {
|
|
107
|
-
return OTHER;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const prevText = prevDirtyNode.__text;
|
|
111
|
-
const nextText = nextDirtyNode.__text;
|
|
112
|
-
|
|
113
|
-
if (prevText === nextText) {
|
|
114
|
-
return OTHER;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const nextAnchor = nextSelection.anchor;
|
|
118
|
-
const prevAnchor = prevSelection.anchor;
|
|
119
|
-
|
|
120
|
-
if (nextAnchor.key !== prevAnchor.key || nextAnchor.type !== 'text') {
|
|
121
|
-
return OTHER;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const nextAnchorOffset = nextAnchor.offset;
|
|
125
|
-
const prevAnchorOffset = prevAnchor.offset;
|
|
126
|
-
const textDiff = nextText.length - prevText.length;
|
|
127
|
-
|
|
128
|
-
if (textDiff === 1 && prevAnchorOffset === nextAnchorOffset - 1) {
|
|
129
|
-
return INSERT_CHARACTER_AFTER_SELECTION;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (textDiff === -1 && prevAnchorOffset === nextAnchorOffset + 1) {
|
|
133
|
-
return DELETE_CHARACTER_BEFORE_SELECTION;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (textDiff === -1 && prevAnchorOffset === nextAnchorOffset) {
|
|
137
|
-
return DELETE_CHARACTER_AFTER_SELECTION;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return OTHER;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function createMergeActionGetter(editor, delay) {
|
|
144
|
-
let prevChangeTime = Date.now();
|
|
145
|
-
let prevChangeType = OTHER;
|
|
146
|
-
return (prevEditorState, nextEditorState, currentHistoryEntry, dirtyLeaves, dirtyElements, tags) => {
|
|
147
|
-
const changeTime = Date.now(); // If applying changes from history stack there's no need
|
|
148
|
-
// to run history logic again, as history entries already calculated
|
|
149
|
-
|
|
150
|
-
if (tags.has('historic')) {
|
|
151
|
-
prevChangeType = OTHER;
|
|
152
|
-
prevChangeTime = changeTime;
|
|
153
|
-
return DISCARD_HISTORY_CANDIDATE;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const changeType = getChangeType(prevEditorState, nextEditorState, dirtyLeaves, dirtyElements, editor.isComposing());
|
|
157
|
-
|
|
158
|
-
const mergeAction = (() => {
|
|
159
|
-
const shouldPushHistory = tags.has('history-push');
|
|
160
|
-
const shouldMergeHistory = !shouldPushHistory && tags.has('history-merge');
|
|
161
|
-
|
|
162
|
-
if (shouldMergeHistory) {
|
|
163
|
-
return HISTORY_MERGE;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (prevEditorState === null) {
|
|
167
|
-
return HISTORY_PUSH;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const selection = nextEditorState._selection;
|
|
171
|
-
const prevSelection = prevEditorState._selection;
|
|
172
|
-
const hasDirtyNodes = dirtyLeaves.size > 0 || dirtyElements.size > 0;
|
|
173
|
-
|
|
174
|
-
if (!hasDirtyNodes) {
|
|
175
|
-
if (prevSelection === null && selection !== null) {
|
|
176
|
-
return HISTORY_MERGE;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return DISCARD_HISTORY_CANDIDATE;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const isSameEditor = currentHistoryEntry === null || currentHistoryEntry.editor === editor;
|
|
183
|
-
|
|
184
|
-
if (shouldPushHistory === false && changeType !== OTHER && changeType === prevChangeType && changeTime < prevChangeTime + delay && isSameEditor) {
|
|
185
|
-
return HISTORY_MERGE;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return HISTORY_PUSH;
|
|
189
|
-
})();
|
|
190
|
-
|
|
191
|
-
prevChangeTime = changeTime;
|
|
192
|
-
prevChangeType = changeType;
|
|
193
|
-
return mergeAction;
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
|
|
197
23
|
function useHistory(editor, externalHistoryState, delay = 1000) {
|
|
198
|
-
const historyState = react.useMemo(() => externalHistoryState || createEmptyHistoryState(), [externalHistoryState]);
|
|
199
|
-
const clearHistory = react.useCallback(() => {
|
|
200
|
-
historyState.undoStack = [];
|
|
201
|
-
historyState.redoStack = [];
|
|
202
|
-
historyState.current = null;
|
|
203
|
-
}, [historyState]);
|
|
24
|
+
const historyState = react.useMemo(() => externalHistoryState || history.createEmptyHistoryState(), [externalHistoryState]);
|
|
204
25
|
react.useEffect(() => {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
const applyChange = ({
|
|
208
|
-
editorState,
|
|
209
|
-
prevEditorState,
|
|
210
|
-
dirtyLeaves,
|
|
211
|
-
dirtyElements,
|
|
212
|
-
tags
|
|
213
|
-
}) => {
|
|
214
|
-
const current = historyState.current;
|
|
215
|
-
const redoStack = historyState.redoStack;
|
|
216
|
-
const undoStack = historyState.undoStack;
|
|
217
|
-
const currentEditorState = current === null ? null : current.editorState;
|
|
218
|
-
|
|
219
|
-
if (current !== null && editorState === currentEditorState) {
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const mergeAction = getMergeAction(prevEditorState, editorState, current, dirtyLeaves, dirtyElements, tags);
|
|
224
|
-
|
|
225
|
-
if (mergeAction === HISTORY_PUSH) {
|
|
226
|
-
if (redoStack.length !== 0) {
|
|
227
|
-
historyState.redoStack = [];
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (current !== null) {
|
|
231
|
-
undoStack.push({ ...current,
|
|
232
|
-
undoSelection: prevEditorState.read(lexical.$getSelection)
|
|
233
|
-
});
|
|
234
|
-
editor.execCommand('canUndo', true);
|
|
235
|
-
}
|
|
236
|
-
} else if (mergeAction === DISCARD_HISTORY_CANDIDATE) {
|
|
237
|
-
return;
|
|
238
|
-
} // Else we merge
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
historyState.current = {
|
|
242
|
-
editor,
|
|
243
|
-
editorState
|
|
244
|
-
};
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
const undo = () => {
|
|
248
|
-
const redoStack = historyState.redoStack;
|
|
249
|
-
const undoStack = historyState.undoStack;
|
|
250
|
-
const undoStackLength = undoStack.length;
|
|
251
|
-
|
|
252
|
-
if (undoStackLength !== 0) {
|
|
253
|
-
const current = historyState.current;
|
|
254
|
-
const historyStateEntry = undoStack.pop();
|
|
255
|
-
|
|
256
|
-
if (current !== null) {
|
|
257
|
-
redoStack.push(current);
|
|
258
|
-
editor.execCommand('canRedo', true);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
if (undoStack.length === 0) {
|
|
262
|
-
editor.execCommand('canUndo', false);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
historyState.current = historyStateEntry;
|
|
266
|
-
historyStateEntry.editor.setEditorState(historyStateEntry.editorState.clone(historyStateEntry.undoSelection), {
|
|
267
|
-
tag: 'historic'
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
const redo = () => {
|
|
273
|
-
const redoStack = historyState.redoStack;
|
|
274
|
-
const undoStack = historyState.undoStack;
|
|
275
|
-
|
|
276
|
-
if (redoStack.length !== 0) {
|
|
277
|
-
const current = historyState.current;
|
|
278
|
-
|
|
279
|
-
if (current !== null) {
|
|
280
|
-
undoStack.push(current);
|
|
281
|
-
editor.execCommand('canUndo', true);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const historyStateEntry = redoStack.pop();
|
|
285
|
-
|
|
286
|
-
if (redoStack.length === 0) {
|
|
287
|
-
editor.execCommand('canRedo', false);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
historyState.current = historyStateEntry;
|
|
291
|
-
historyStateEntry.editor.setEditorState(historyStateEntry.editorState, {
|
|
292
|
-
tag: 'historic'
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
const applyCommand = type => {
|
|
298
|
-
switch (type) {
|
|
299
|
-
case 'undo':
|
|
300
|
-
undo();
|
|
301
|
-
return true;
|
|
302
|
-
|
|
303
|
-
case 'redo':
|
|
304
|
-
redo();
|
|
305
|
-
return true;
|
|
306
|
-
|
|
307
|
-
case 'clearEditor':
|
|
308
|
-
clearHistory();
|
|
309
|
-
return false;
|
|
310
|
-
|
|
311
|
-
case 'clearHistory':
|
|
312
|
-
clearHistory();
|
|
313
|
-
return true;
|
|
314
|
-
|
|
315
|
-
default:
|
|
316
|
-
return false;
|
|
317
|
-
}
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
return withSubscriptions(editor.addListener('command', applyCommand, EditorPriority), editor.addListener('update', applyChange));
|
|
321
|
-
}, [clearHistory, delay, editor, historyState]);
|
|
322
|
-
}
|
|
323
|
-
function createEmptyHistoryState() {
|
|
324
|
-
return {
|
|
325
|
-
current: null,
|
|
326
|
-
redoStack: [],
|
|
327
|
-
undoStack: []
|
|
328
|
-
};
|
|
26
|
+
return history.registerHistory(editor, historyState, delay);
|
|
27
|
+
}, [delay, editor, historyState]);
|
|
329
28
|
}
|
|
330
29
|
|
|
331
30
|
/**
|
|
@@ -361,199 +60,6 @@ const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !==
|
|
|
361
60
|
const useLayoutEffectImpl = CAN_USE_DOM ? react.useLayoutEffect : react.useEffect;
|
|
362
61
|
var useLayoutEffect = useLayoutEffectImpl;
|
|
363
62
|
|
|
364
|
-
/**
|
|
365
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
366
|
-
*
|
|
367
|
-
* This source code is licensed under the MIT license found in the
|
|
368
|
-
* LICENSE file in the root directory of this source tree.
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*/
|
|
372
|
-
function onPasteForPlainText(event, editor) {
|
|
373
|
-
event.preventDefault();
|
|
374
|
-
editor.update(() => {
|
|
375
|
-
const selection = lexical.$getSelection();
|
|
376
|
-
const clipboardData = event.clipboardData;
|
|
377
|
-
|
|
378
|
-
if (clipboardData != null && lexical.$isRangeSelection(selection)) {
|
|
379
|
-
clipboard.$insertDataTransferForPlainText(clipboardData, selection);
|
|
380
|
-
}
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
function onCutForPlainText(event, editor) {
|
|
384
|
-
onCopyForPlainText(event, editor);
|
|
385
|
-
editor.update(() => {
|
|
386
|
-
const selection = lexical.$getSelection();
|
|
387
|
-
|
|
388
|
-
if (lexical.$isRangeSelection(selection)) {
|
|
389
|
-
selection.removeText();
|
|
390
|
-
}
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
function onCopyForPlainText(event, editor) {
|
|
394
|
-
event.preventDefault();
|
|
395
|
-
editor.update(() => {
|
|
396
|
-
const clipboardData = event.clipboardData;
|
|
397
|
-
const selection = lexical.$getSelection();
|
|
398
|
-
|
|
399
|
-
if (selection !== null) {
|
|
400
|
-
if (clipboardData != null) {
|
|
401
|
-
const htmlString = clipboard.getHtmlContent(editor);
|
|
402
|
-
|
|
403
|
-
if (htmlString !== null) {
|
|
404
|
-
clipboardData.setData('text/html', htmlString);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
clipboardData.setData('text/plain', selection.getTextContent());
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
});
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
/**
|
|
414
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
415
|
-
*
|
|
416
|
-
* This source code is licensed under the MIT license found in the
|
|
417
|
-
* LICENSE file in the root directory of this source tree.
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
*/
|
|
421
|
-
// Convoluted logic to make this work with Flow. Order matters.
|
|
422
|
-
const options = {
|
|
423
|
-
tag: 'history-merge'
|
|
424
|
-
};
|
|
425
|
-
const setEditorOptions = options;
|
|
426
|
-
const updateOptions = options;
|
|
427
|
-
function initializeEditor(editor, initialEditorState) {
|
|
428
|
-
if (initialEditorState === null) {
|
|
429
|
-
return;
|
|
430
|
-
} else if (initialEditorState === undefined) {
|
|
431
|
-
editor.update(() => {
|
|
432
|
-
const root = lexical.$getRoot();
|
|
433
|
-
const firstChild = root.getFirstChild();
|
|
434
|
-
|
|
435
|
-
if (firstChild === null) {
|
|
436
|
-
const paragraph = lexical.$createParagraphNode();
|
|
437
|
-
root.append(paragraph);
|
|
438
|
-
const activeElement = document.activeElement;
|
|
439
|
-
|
|
440
|
-
if (lexical.$getSelection() !== null || activeElement !== null && activeElement === editor.getRootElement()) {
|
|
441
|
-
paragraph.select();
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
}, updateOptions);
|
|
445
|
-
} else if (initialEditorState !== null) {
|
|
446
|
-
switch (typeof initialEditorState) {
|
|
447
|
-
case 'string':
|
|
448
|
-
{
|
|
449
|
-
const parsedEditorState = editor.parseEditorState(initialEditorState);
|
|
450
|
-
editor.setEditorState(parsedEditorState, setEditorOptions);
|
|
451
|
-
break;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
case 'object':
|
|
455
|
-
{
|
|
456
|
-
editor.setEditorState(initialEditorState, setEditorOptions);
|
|
457
|
-
break;
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
case 'function':
|
|
461
|
-
{
|
|
462
|
-
editor.update(initialEditorState, updateOptions);
|
|
463
|
-
break;
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
/**
|
|
470
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
471
|
-
*
|
|
472
|
-
* This source code is licensed under the MIT license found in the
|
|
473
|
-
* LICENSE file in the root directory of this source tree.
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
*/
|
|
477
|
-
function useLexicalDragonSupport(editor) {
|
|
478
|
-
react.useEffect(() => {
|
|
479
|
-
const handler = event => {
|
|
480
|
-
const rootElement = editor.getRootElement();
|
|
481
|
-
|
|
482
|
-
if (document.activeElement !== rootElement) {
|
|
483
|
-
return;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
const data = event.data;
|
|
487
|
-
|
|
488
|
-
if (typeof data === 'string') {
|
|
489
|
-
let parsedData;
|
|
490
|
-
|
|
491
|
-
try {
|
|
492
|
-
parsedData = JSON.parse(data);
|
|
493
|
-
} catch (e) {
|
|
494
|
-
return;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
if (parsedData && parsedData.protocol === 'nuanria_messaging' && parsedData.type === 'request') {
|
|
498
|
-
const payload = parsedData.payload;
|
|
499
|
-
|
|
500
|
-
if (payload && payload.functionId === 'makeChanges') {
|
|
501
|
-
const args = payload.args;
|
|
502
|
-
|
|
503
|
-
if (args) {
|
|
504
|
-
const [elementStart, elementLength, text, selStart, selLength, formatCommand] = args; // TODO: we should probably handle formatCommand somehow?
|
|
505
|
-
editor.update(() => {
|
|
506
|
-
const selection = lexical.$getSelection();
|
|
507
|
-
|
|
508
|
-
if (lexical.$isRangeSelection(selection)) {
|
|
509
|
-
const anchor = selection.anchor;
|
|
510
|
-
let anchorNode = anchor.getNode();
|
|
511
|
-
let setSelStart = 0;
|
|
512
|
-
let setSelEnd = 0;
|
|
513
|
-
|
|
514
|
-
if (lexical.$isTextNode(anchorNode)) {
|
|
515
|
-
// set initial selection
|
|
516
|
-
if (elementStart >= 0 && elementLength >= 0) {
|
|
517
|
-
setSelStart = elementStart;
|
|
518
|
-
setSelEnd = elementStart + elementLength; // If the offset is more than the end, make it the end
|
|
519
|
-
|
|
520
|
-
selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
if (setSelStart !== setSelEnd || text !== '') {
|
|
525
|
-
selection.insertRawText(text);
|
|
526
|
-
anchorNode = anchor.getNode();
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
if (lexical.$isTextNode(anchorNode)) {
|
|
530
|
-
// set final selection
|
|
531
|
-
setSelStart = selStart;
|
|
532
|
-
setSelEnd = selStart + selLength;
|
|
533
|
-
const anchorNodeTextLength = anchorNode.getTextContentSize(); // If the offset is more than the end, make it the end
|
|
534
|
-
|
|
535
|
-
setSelStart = setSelStart > anchorNodeTextLength ? anchorNodeTextLength : setSelStart;
|
|
536
|
-
setSelEnd = setSelEnd > anchorNodeTextLength ? anchorNodeTextLength : setSelEnd;
|
|
537
|
-
selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
|
|
538
|
-
} // block the chrome extension from handling this event
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
event.stopImmediatePropagation();
|
|
542
|
-
}
|
|
543
|
-
});
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
};
|
|
549
|
-
|
|
550
|
-
window.addEventListener('message', handler, true);
|
|
551
|
-
return () => {
|
|
552
|
-
window.removeEventListener('message', handler, true);
|
|
553
|
-
};
|
|
554
|
-
}, [editor]);
|
|
555
|
-
}
|
|
556
|
-
|
|
557
63
|
/**
|
|
558
64
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
559
65
|
*
|
|
@@ -564,169 +70,9 @@ function useLexicalDragonSupport(editor) {
|
|
|
564
70
|
*/
|
|
565
71
|
function usePlainTextSetup(editor, initialEditorState) {
|
|
566
72
|
useLayoutEffect(() => {
|
|
567
|
-
|
|
568
|
-
const selection$1 = lexical.$getSelection();
|
|
569
|
-
|
|
570
|
-
if (!lexical.$isRangeSelection(selection$1)) {
|
|
571
|
-
return false;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
switch (type) {
|
|
575
|
-
case 'deleteCharacter':
|
|
576
|
-
{
|
|
577
|
-
const isBackward = payload;
|
|
578
|
-
selection$1.deleteCharacter(isBackward);
|
|
579
|
-
return true;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
case 'deleteWord':
|
|
583
|
-
{
|
|
584
|
-
const isBackward = payload;
|
|
585
|
-
selection$1.deleteWord(isBackward);
|
|
586
|
-
return true;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
case 'deleteLine':
|
|
590
|
-
{
|
|
591
|
-
const isBackward = payload;
|
|
592
|
-
selection$1.deleteLine(isBackward);
|
|
593
|
-
return true;
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
case 'insertText':
|
|
597
|
-
{
|
|
598
|
-
const eventOrText = payload;
|
|
599
|
-
|
|
600
|
-
if (typeof eventOrText === 'string') {
|
|
601
|
-
selection$1.insertText(eventOrText);
|
|
602
|
-
} else {
|
|
603
|
-
const dataTransfer = eventOrText.dataTransfer;
|
|
604
|
-
|
|
605
|
-
if (dataTransfer != null) {
|
|
606
|
-
clipboard.$insertDataTransferForPlainText(dataTransfer, selection$1);
|
|
607
|
-
} else {
|
|
608
|
-
const data = eventOrText.data;
|
|
609
|
-
|
|
610
|
-
if (data) {
|
|
611
|
-
selection$1.insertText(data);
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
return true;
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
case 'removeText':
|
|
620
|
-
selection$1.removeText();
|
|
621
|
-
return true;
|
|
622
|
-
|
|
623
|
-
case 'insertLineBreak':
|
|
624
|
-
const selectStart = payload;
|
|
625
|
-
selection$1.insertLineBreak(selectStart);
|
|
626
|
-
return true;
|
|
627
|
-
|
|
628
|
-
case 'insertParagraph':
|
|
629
|
-
selection$1.insertLineBreak();
|
|
630
|
-
return true;
|
|
631
|
-
|
|
632
|
-
case 'indentContent':
|
|
633
|
-
case 'outdentContent':
|
|
634
|
-
case 'insertHorizontalRule':
|
|
635
|
-
case 'insertImage':
|
|
636
|
-
case 'insertTable':
|
|
637
|
-
case 'formatElement':
|
|
638
|
-
case 'formatText':
|
|
639
|
-
{
|
|
640
|
-
return true;
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
case 'keyArrowLeft':
|
|
644
|
-
{
|
|
645
|
-
const event = payload;
|
|
646
|
-
const isHoldingShift = event.shiftKey;
|
|
647
|
-
|
|
648
|
-
if (selection.$shouldOverrideDefaultCharacterSelection(selection$1, true)) {
|
|
649
|
-
event.preventDefault();
|
|
650
|
-
selection.$moveCharacter(selection$1, isHoldingShift, true);
|
|
651
|
-
return true;
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
return false;
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
case 'keyArrowRight':
|
|
658
|
-
{
|
|
659
|
-
const event = payload;
|
|
660
|
-
const isHoldingShift = event.shiftKey;
|
|
661
|
-
|
|
662
|
-
if (selection.$shouldOverrideDefaultCharacterSelection(selection$1, false)) {
|
|
663
|
-
event.preventDefault();
|
|
664
|
-
selection.$moveCharacter(selection$1, isHoldingShift, false);
|
|
665
|
-
return true;
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
return false;
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
case 'keyBackspace':
|
|
672
|
-
{
|
|
673
|
-
const event = payload;
|
|
674
|
-
event.preventDefault();
|
|
675
|
-
return editor.execCommand('deleteCharacter', true);
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
case 'keyDelete':
|
|
679
|
-
{
|
|
680
|
-
const event = payload;
|
|
681
|
-
event.preventDefault();
|
|
682
|
-
return editor.execCommand('deleteCharacter', false);
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
case 'keyEnter':
|
|
686
|
-
{
|
|
687
|
-
const event = payload;
|
|
688
|
-
event.preventDefault();
|
|
689
|
-
return editor.execCommand('insertLineBreak');
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
case 'copy':
|
|
693
|
-
{
|
|
694
|
-
const event = payload;
|
|
695
|
-
onCopyForPlainText(event, editor);
|
|
696
|
-
return true;
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
case 'cut':
|
|
700
|
-
{
|
|
701
|
-
const event = payload;
|
|
702
|
-
onCutForPlainText(event, editor);
|
|
703
|
-
return true;
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
case 'paste':
|
|
707
|
-
{
|
|
708
|
-
const event = payload;
|
|
709
|
-
onPasteForPlainText(event, editor);
|
|
710
|
-
return true;
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
case 'drop':
|
|
714
|
-
case 'dragstart':
|
|
715
|
-
{
|
|
716
|
-
// TODO: Make drag and drop work at some point.
|
|
717
|
-
const event = payload;
|
|
718
|
-
event.preventDefault();
|
|
719
|
-
return true;
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
return false;
|
|
724
|
-
}, 0);
|
|
725
|
-
initializeEditor(editor, initialEditorState);
|
|
726
|
-
return removeListener; // We only do this for init
|
|
73
|
+
return utils.mergeRegister(plainText.registerPlainText(editor, initialEditorState), dragon.registerDragonSupport(editor)); // We only do this for init
|
|
727
74
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
728
75
|
}, [editor]);
|
|
729
|
-
useLexicalDragonSupport(editor);
|
|
730
76
|
}
|
|
731
77
|
|
|
732
78
|
/**
|