@lexical/react 0.1.7 → 0.1.8
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_useLexical.dev.js +84 -0
- package/DEPRECATED_useLexical.js +9 -2
- package/DEPRECATED_useLexical.prod.js +8 -0
- package/DEPRECATED_useLexicalAutoFormatter.dev.js +642 -0
- package/DEPRECATED_useLexicalAutoFormatter.js +9 -11
- package/DEPRECATED_useLexicalAutoFormatter.prod.js +22 -0
- package/DEPRECATED_useLexicalCanShowPlaceholder.dev.js +136 -0
- package/DEPRECATED_useLexicalCanShowPlaceholder.js +9 -3
- package/DEPRECATED_useLexicalCanShowPlaceholder.prod.js +8 -0
- package/DEPRECATED_useLexicalCharacterLimit.dev.js +294 -0
- package/DEPRECATED_useLexicalCharacterLimit.js +9 -8
- package/DEPRECATED_useLexicalCharacterLimit.prod.js +14 -0
- package/DEPRECATED_useLexicalDecorators.dev.js +82 -0
- package/DEPRECATED_useLexicalDecorators.js +9 -2
- package/DEPRECATED_useLexicalDecorators.prod.js +8 -0
- package/DEPRECATED_useLexicalEditor.dev.js +52 -0
- package/DEPRECATED_useLexicalEditor.js +9 -1
- package/DEPRECATED_useLexicalEditor.prod.js +7 -0
- package/DEPRECATED_useLexicalEditorEvents.dev.js +96 -0
- package/DEPRECATED_useLexicalEditorEvents.js +9 -2
- package/DEPRECATED_useLexicalEditorEvents.prod.js +8 -0
- package/DEPRECATED_useLexicalHistory.dev.js +339 -0
- package/DEPRECATED_useLexicalHistory.js +9 -7
- package/DEPRECATED_useLexicalHistory.prod.js +13 -0
- package/DEPRECATED_useLexicalList.dev.js +64 -0
- package/DEPRECATED_useLexicalList.js +9 -1
- package/DEPRECATED_useLexicalList.prod.js +7 -0
- package/DEPRECATED_useLexicalPlainText.dev.js +755 -0
- package/DEPRECATED_useLexicalPlainText.js +9 -16
- package/DEPRECATED_useLexicalPlainText.prod.js +22 -0
- package/DEPRECATED_useLexicalRichText.dev.js +1326 -0
- package/DEPRECATED_useLexicalRichText.js +9 -30
- package/DEPRECATED_useLexicalRichText.prod.js +35 -0
- package/LexicalAutoFormatterPlugin.dev.js +645 -0
- package/LexicalAutoFormatterPlugin.js +9 -12
- package/LexicalAutoFormatterPlugin.prod.js +23 -0
- package/LexicalAutoLinkPlugin.dev.js +227 -0
- package/LexicalAutoLinkPlugin.js +9 -6
- package/LexicalAutoLinkPlugin.prod.js +12 -0
- package/LexicalBootstrapPlugin.dev.js +124 -0
- package/LexicalBootstrapPlugin.js +9 -0
- package/LexicalBootstrapPlugin.prod.js +8 -0
- package/LexicalCharacterLimitPlugin.dev.js +352 -0
- package/LexicalCharacterLimitPlugin.js +9 -8
- package/LexicalCharacterLimitPlugin.prod.js +14 -0
- package/LexicalCollaborationPlugin.dev.js +235 -0
- package/LexicalCollaborationPlugin.js +9 -8
- package/LexicalCollaborationPlugin.prod.js +14 -0
- package/LexicalComposer.dev.js +76 -0
- package/LexicalComposer.js +9 -3
- package/LexicalComposer.prod.js +9 -0
- package/LexicalComposerContext.dev.js +53 -0
- package/LexicalComposerContext.js +9 -1
- package/LexicalComposerContext.prod.js +7 -0
- package/LexicalContentEditable.dev.js +71 -0
- package/LexicalContentEditable.js +9 -3
- package/LexicalContentEditable.prod.js +9 -0
- package/LexicalHashtagPlugin.dev.js +152 -0
- package/LexicalHashtagPlugin.js +9 -4
- package/LexicalHashtagPlugin.prod.js +10 -0
- package/LexicalHistoryPlugin.dev.js +344 -0
- package/LexicalHistoryPlugin.js +9 -7
- package/LexicalHistoryPlugin.prod.js +13 -0
- package/LexicalHorizontalRulePlugin.dev.js +51 -0
- package/LexicalHorizontalRulePlugin.js +9 -1
- package/LexicalHorizontalRulePlugin.prod.js +7 -0
- package/LexicalLinkPlugin.dev.js +137 -0
- package/LexicalLinkPlugin.js +9 -3
- package/LexicalLinkPlugin.prod.js +9 -0
- package/LexicalListPlugin.dev.js +67 -0
- package/LexicalListPlugin.js +9 -2
- package/LexicalListPlugin.prod.js +8 -0
- package/LexicalNestedComposer.dev.js +60 -0
- package/LexicalNestedComposer.js +9 -2
- package/LexicalNestedComposer.prod.js +8 -0
- package/LexicalOnChangePlugin.dev.js +57 -0
- package/LexicalOnChangePlugin.js +9 -1
- package/LexicalOnChangePlugin.prod.js +7 -0
- package/LexicalPlainTextPlugin.dev.js +565 -0
- package/LexicalPlainTextPlugin.js +9 -13
- package/LexicalPlainTextPlugin.prod.js +17 -0
- package/LexicalRichTextPlugin.dev.js +1136 -0
- package/LexicalRichTextPlugin.js +9 -27
- package/LexicalRichTextPlugin.prod.js +31 -0
- package/LexicalTablePlugin.dev.js +95 -0
- package/LexicalTablePlugin.js +9 -3
- package/LexicalTablePlugin.prod.js +9 -0
- package/LexicalTreeView.dev.js +340 -0
- package/LexicalTreeView.js +9 -13
- package/LexicalTreeView.prod.js +19 -0
- package/README.md +1 -0
- package/package.json +4 -4
- package/useLexicalDecoratorMap.dev.js +83 -0
- package/useLexicalDecoratorMap.js +9 -2
- package/useLexicalDecoratorMap.prod.js +8 -0
- package/useLexicalIsTextContentEmpty.dev.js +84 -0
- package/useLexicalIsTextContentEmpty.js +9 -2
- package/useLexicalIsTextContentEmpty.prod.js +8 -0
- package/withSubscriptions.dev.js +23 -0
- package/withSubscriptions.js +9 -1
- package/withSubscriptions.prod.js +7 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
var useLexicalCanShowPlaceholder = require('@lexical/react/DEPRECATED_useLexicalCanShowPlaceholder');
|
|
10
|
+
var react = require('react');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14
|
+
*
|
|
15
|
+
* This source code is licensed under the MIT license found in the
|
|
16
|
+
* LICENSE file in the root directory of this source tree.
|
|
17
|
+
*
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
24
|
+
*
|
|
25
|
+
* This source code is licensed under the MIT license found in the
|
|
26
|
+
* LICENSE file in the root directory of this source tree.
|
|
27
|
+
*
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
const useLayoutEffectImpl = CAN_USE_DOM ? react.useLayoutEffect : react.useEffect;
|
|
31
|
+
var useLayoutEffect = useLayoutEffectImpl;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
35
|
+
*
|
|
36
|
+
* This source code is licensed under the MIT license found in the
|
|
37
|
+
* LICENSE file in the root directory of this source tree.
|
|
38
|
+
*
|
|
39
|
+
*
|
|
40
|
+
*/
|
|
41
|
+
function useLexicalEditor(editor, onError) {
|
|
42
|
+
const showPlaceholder = useLexicalCanShowPlaceholder(editor);
|
|
43
|
+
const rootElementRef = react.useCallback(rootElement => {
|
|
44
|
+
editor.setRootElement(rootElement);
|
|
45
|
+
}, [editor]);
|
|
46
|
+
useLayoutEffect(() => {
|
|
47
|
+
return editor.addListener('error', onError);
|
|
48
|
+
}, [editor, onError]);
|
|
49
|
+
return [rootElementRef, showPlaceholder];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
module.exports = useLexicalEditor;
|
|
@@ -1 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict'
|
|
8
|
+
const DEPRECATED_useLexicalEditor = process.env.NODE_ENV === 'development' ? require('./DEPRECATED_useLexicalEditor.dev.js') : require('./DEPRECATED_useLexicalEditor.prod.js')
|
|
9
|
+
module.exports = DEPRECATED_useLexicalEditor;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict';var b=require("@lexical/react/DEPRECATED_useLexicalCanShowPlaceholder"),d=require("react"),e="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement?d.useLayoutEffect:d.useEffect;module.exports=function(a,c){const f=b(a),h=d.useCallback(g=>{a.setRootElement(g)},[a]);e(()=>a.addListener("error",c),[a,c]);return[h,f]};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
var react = require('react');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
13
|
+
*
|
|
14
|
+
* This source code is licensed under the MIT license found in the
|
|
15
|
+
* LICENSE file in the root directory of this source tree.
|
|
16
|
+
*
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
23
|
+
*
|
|
24
|
+
* This source code is licensed under the MIT license found in the
|
|
25
|
+
* LICENSE file in the root directory of this source tree.
|
|
26
|
+
*
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
const useLayoutEffectImpl = CAN_USE_DOM ? react.useLayoutEffect : react.useEffect;
|
|
30
|
+
var useLayoutEffect = useLayoutEffectImpl;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
34
|
+
*
|
|
35
|
+
* This source code is licensed under the MIT license found in the
|
|
36
|
+
* LICENSE file in the root directory of this source tree.
|
|
37
|
+
*
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
function getTarget(eventName, rootElement) {
|
|
42
|
+
return eventName === 'selectionchange' || eventName === 'keyup' || eventName === 'pointerup' || eventName === 'pointercancel' ? rootElement.ownerDocument : rootElement;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isRootEditable(editor) {
|
|
46
|
+
const rootElement = editor.getRootElement();
|
|
47
|
+
return rootElement !== null && rootElement.contentEditable === 'true';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function useEditorEvents(events, editor) {
|
|
51
|
+
useLayoutEffect(() => {
|
|
52
|
+
const create = [];
|
|
53
|
+
const destroy = [];
|
|
54
|
+
|
|
55
|
+
for (let i = 0; i < events.length; i++) {
|
|
56
|
+
const [eventName, handler] = events[i];
|
|
57
|
+
|
|
58
|
+
const handlerWrapper = event => {
|
|
59
|
+
if (isRootEditable(editor)) {
|
|
60
|
+
handler(event, editor);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
create.push(rootElement => {
|
|
65
|
+
getTarget(eventName, rootElement).addEventListener(eventName, handlerWrapper);
|
|
66
|
+
});
|
|
67
|
+
destroy.push(rootElement => {
|
|
68
|
+
getTarget(eventName, rootElement).removeEventListener(eventName, handlerWrapper);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return editor.addListener('root', (rootElement, prevRootElement) => {
|
|
73
|
+
if (prevRootElement !== null) {
|
|
74
|
+
destroy.forEach(fn => fn(prevRootElement));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (rootElement !== null) {
|
|
78
|
+
create.forEach(fn => fn(rootElement));
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}, [editor, events]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
86
|
+
*
|
|
87
|
+
* This source code is licensed under the MIT license found in the
|
|
88
|
+
* LICENSE file in the root directory of this source tree.
|
|
89
|
+
*
|
|
90
|
+
*
|
|
91
|
+
*/
|
|
92
|
+
function useLexicalEditorEvents(events, editor) {
|
|
93
|
+
useEditorEvents(events, editor);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
module.exports = useLexicalEditorEvents;
|
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict'
|
|
8
|
+
const DEPRECATED_useLexicalEditorEvents = process.env.NODE_ENV === 'development' ? require('./DEPRECATED_useLexicalEditorEvents.dev.js') : require('./DEPRECATED_useLexicalEditorEvents.prod.js')
|
|
9
|
+
module.exports = DEPRECATED_useLexicalEditorEvents;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict';var g=require("react"),h="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement?g.useLayoutEffect:g.useEffect;function p(a,b){return"selectionchange"===a||"keyup"===a||"pointerup"===a||"pointercancel"===a?b.ownerDocument:b}
|
|
8
|
+
function q(a,b){h(()=>{const k=[],l=[];for(let d=0;d<a.length;d++){const [c,e]=a[d],n=f=>{const m=b.getRootElement();null!==m&&"true"===m.contentEditable&&e(f,b)};k.push(f=>{p(c,f).addEventListener(c,n)});l.push(f=>{p(c,f).removeEventListener(c,n)})}return b.addListener("root",(d,c)=>{null!==c&&l.forEach(e=>e(c));null!==d&&k.forEach(e=>e(d))})},[b,a])}module.exports=function(a,b){q(a,b)};
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
var withSubscriptions = require('@lexical/react/withSubscriptions');
|
|
10
|
+
var lexical = require('lexical');
|
|
11
|
+
var react = require('react');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
15
|
+
*
|
|
16
|
+
* This source code is licensed under the MIT license found in the
|
|
17
|
+
* LICENSE file in the root directory of this source tree.
|
|
18
|
+
*
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
const MERGE = 0;
|
|
22
|
+
const NO_MERGE = 1;
|
|
23
|
+
const DISCARD = 2;
|
|
24
|
+
const OTHER = 0;
|
|
25
|
+
const COMPOSING_CHARACTER = 1;
|
|
26
|
+
const INSERT_CHARACTER_AFTER_SELECTION = 2;
|
|
27
|
+
const DELETE_CHARACTER_BEFORE_SELECTION = 3;
|
|
28
|
+
const DELETE_CHARACTER_AFTER_SELECTION = 4;
|
|
29
|
+
const EditorPriority = 0;
|
|
30
|
+
|
|
31
|
+
function getDirtyNodes(editorState, dirtyLeavesSet, dirtyElementsSet) {
|
|
32
|
+
const dirtyLeaves = Array.from(dirtyLeavesSet);
|
|
33
|
+
const dirtyElements = Array.from(dirtyElementsSet);
|
|
34
|
+
const nodeMap = editorState._nodeMap;
|
|
35
|
+
const nodes = [];
|
|
36
|
+
|
|
37
|
+
for (let i = 0; i < dirtyLeaves.length; i++) {
|
|
38
|
+
const dirtyLeafKey = dirtyLeaves[i];
|
|
39
|
+
const dirtyLeaf = nodeMap.get(dirtyLeafKey);
|
|
40
|
+
|
|
41
|
+
if (dirtyLeaf !== undefined) {
|
|
42
|
+
nodes.push(dirtyLeaf);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
for (let i = 0; i < dirtyElements.length; i++) {
|
|
47
|
+
const intentionallyMarkedAsDirty = dirtyElements[i][1];
|
|
48
|
+
|
|
49
|
+
if (!intentionallyMarkedAsDirty) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const dirtyElementKey = dirtyElements[i][0];
|
|
54
|
+
const dirtyElement = nodeMap.get(dirtyElementKey);
|
|
55
|
+
|
|
56
|
+
if (dirtyElement !== undefined && !lexical.$isRootNode(dirtyElement)) {
|
|
57
|
+
nodes.push(dirtyElement);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return nodes;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getChangeType(prevEditorState, nextEditorState, dirtyLeavesSet, dirtyElementsSet, isComposing) {
|
|
65
|
+
if (prevEditorState === null || dirtyLeavesSet.size === 0 && dirtyElementsSet.size === 0) {
|
|
66
|
+
return OTHER;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const nextSelection = nextEditorState._selection;
|
|
70
|
+
const prevSelection = prevEditorState._selection;
|
|
71
|
+
|
|
72
|
+
if (isComposing) {
|
|
73
|
+
return COMPOSING_CHARACTER;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (nextSelection === null || prevSelection === null || !prevSelection.isCollapsed() || !nextSelection.isCollapsed()) {
|
|
77
|
+
return OTHER;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const dirtyNodes = getDirtyNodes(nextEditorState, dirtyLeavesSet, dirtyElementsSet);
|
|
81
|
+
|
|
82
|
+
if (dirtyNodes.length === 0) {
|
|
83
|
+
return OTHER;
|
|
84
|
+
} // Catching the case when inserting new text node into an element (e.g. first char in paragraph/list),
|
|
85
|
+
// or after existing node.
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
if (dirtyNodes.length > 1) {
|
|
89
|
+
const nextNodeMap = nextEditorState._nodeMap;
|
|
90
|
+
const nextAnchorNode = nextNodeMap.get(nextSelection.anchor.key);
|
|
91
|
+
const prevAnchorNode = nextNodeMap.get(prevSelection.anchor.key);
|
|
92
|
+
|
|
93
|
+
if (nextAnchorNode && prevAnchorNode && !prevEditorState._nodeMap.has(nextAnchorNode.__key) && lexical.$isTextNode(nextAnchorNode) && nextAnchorNode.__text.length === 1 && nextSelection.anchor.offset === 1) {
|
|
94
|
+
return INSERT_CHARACTER_AFTER_SELECTION;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return OTHER;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const nextDirtyNode = dirtyNodes[0];
|
|
101
|
+
|
|
102
|
+
const prevDirtyNode = prevEditorState._nodeMap.get(nextDirtyNode.__key);
|
|
103
|
+
|
|
104
|
+
if (!lexical.$isTextNode(prevDirtyNode) || !lexical.$isTextNode(nextDirtyNode) || prevDirtyNode.__mode !== nextDirtyNode.__mode) {
|
|
105
|
+
return OTHER;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const prevText = prevDirtyNode.__text;
|
|
109
|
+
const nextText = nextDirtyNode.__text;
|
|
110
|
+
|
|
111
|
+
if (prevText === nextText) {
|
|
112
|
+
return OTHER;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const nextAnchor = nextSelection.anchor;
|
|
116
|
+
const prevAnchor = prevSelection.anchor;
|
|
117
|
+
|
|
118
|
+
if (nextAnchor.key !== prevAnchor.key || nextAnchor.type !== 'text') {
|
|
119
|
+
return OTHER;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const nextAnchorOffset = nextAnchor.offset;
|
|
123
|
+
const prevAnchorOffset = prevAnchor.offset;
|
|
124
|
+
const textDiff = nextText.length - prevText.length;
|
|
125
|
+
|
|
126
|
+
if (textDiff === 1 && prevAnchorOffset === nextAnchorOffset - 1) {
|
|
127
|
+
return INSERT_CHARACTER_AFTER_SELECTION;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (textDiff === -1 && prevAnchorOffset === nextAnchorOffset + 1) {
|
|
131
|
+
return DELETE_CHARACTER_BEFORE_SELECTION;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (textDiff === -1 && prevAnchorOffset === nextAnchorOffset) {
|
|
135
|
+
return DELETE_CHARACTER_AFTER_SELECTION;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return OTHER;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function createMergeActionGetter(editor, delay) {
|
|
142
|
+
let prevChangeTime = Date.now();
|
|
143
|
+
let prevChangeType = OTHER;
|
|
144
|
+
return (prevEditorState, nextEditorState, currentHistoryEntry, dirtyLeaves, dirtyElements, tags) => {
|
|
145
|
+
const changeTime = Date.now(); // If applying changes from history stack there's no need
|
|
146
|
+
// to run history logic again, as history entries already calculated
|
|
147
|
+
|
|
148
|
+
if (tags.has('historic')) {
|
|
149
|
+
prevChangeType = OTHER;
|
|
150
|
+
prevChangeTime = changeTime;
|
|
151
|
+
return DISCARD;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const changeType = getChangeType(prevEditorState, nextEditorState, dirtyLeaves, dirtyElements, editor.isComposing());
|
|
155
|
+
|
|
156
|
+
const mergeAction = (() => {
|
|
157
|
+
if (tags.has('without-history')) {
|
|
158
|
+
return MERGE;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (prevEditorState === null) {
|
|
162
|
+
return NO_MERGE;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const selection = nextEditorState._selection;
|
|
166
|
+
const prevSelection = prevEditorState._selection;
|
|
167
|
+
const hasDirtyNodes = dirtyLeaves.size > 0 || dirtyElements.size > 0;
|
|
168
|
+
|
|
169
|
+
if (!hasDirtyNodes) {
|
|
170
|
+
if (prevSelection === null && selection !== null) {
|
|
171
|
+
return MERGE;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return DISCARD;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const isSameEditor = currentHistoryEntry === null || currentHistoryEntry.editor === editor;
|
|
178
|
+
|
|
179
|
+
if (changeType !== OTHER && changeType === prevChangeType && changeTime < prevChangeTime + delay && isSameEditor) {
|
|
180
|
+
return MERGE;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return NO_MERGE;
|
|
184
|
+
})();
|
|
185
|
+
|
|
186
|
+
prevChangeTime = changeTime;
|
|
187
|
+
prevChangeType = changeType;
|
|
188
|
+
return mergeAction;
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function useHistory(editor, externalHistoryState, delay = 1000) {
|
|
193
|
+
const historyState = react.useMemo(() => externalHistoryState || createEmptyHistoryState(), [externalHistoryState]);
|
|
194
|
+
const clearHistory = react.useCallback(() => {
|
|
195
|
+
historyState.undoStack = [];
|
|
196
|
+
historyState.redoStack = [];
|
|
197
|
+
historyState.current = null;
|
|
198
|
+
}, [historyState]);
|
|
199
|
+
react.useEffect(() => {
|
|
200
|
+
const getMergeAction = createMergeActionGetter(editor, delay);
|
|
201
|
+
|
|
202
|
+
const applyChange = ({
|
|
203
|
+
editorState,
|
|
204
|
+
prevEditorState,
|
|
205
|
+
dirtyLeaves,
|
|
206
|
+
dirtyElements,
|
|
207
|
+
tags
|
|
208
|
+
}) => {
|
|
209
|
+
const current = historyState.current;
|
|
210
|
+
const redoStack = historyState.redoStack;
|
|
211
|
+
const undoStack = historyState.undoStack;
|
|
212
|
+
const currentEditorState = current === null ? null : current.editorState;
|
|
213
|
+
|
|
214
|
+
if (current !== null && editorState === currentEditorState) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const mergeAction = getMergeAction(prevEditorState, editorState, current, dirtyLeaves, dirtyElements, tags);
|
|
219
|
+
|
|
220
|
+
if (mergeAction === NO_MERGE) {
|
|
221
|
+
if (redoStack.length !== 0) {
|
|
222
|
+
historyState.redoStack = [];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (current !== null) {
|
|
226
|
+
undoStack.push({ ...current,
|
|
227
|
+
undoSelection: prevEditorState.read(lexical.$getSelection)
|
|
228
|
+
});
|
|
229
|
+
editor.execCommand('canUndo', true);
|
|
230
|
+
}
|
|
231
|
+
} else if (mergeAction === DISCARD) {
|
|
232
|
+
return;
|
|
233
|
+
} // Else we merge
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
historyState.current = {
|
|
237
|
+
editor,
|
|
238
|
+
editorState
|
|
239
|
+
};
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const undo = () => {
|
|
243
|
+
const redoStack = historyState.redoStack;
|
|
244
|
+
const undoStack = historyState.undoStack;
|
|
245
|
+
const undoStackLength = undoStack.length;
|
|
246
|
+
|
|
247
|
+
if (undoStackLength !== 0) {
|
|
248
|
+
const current = historyState.current;
|
|
249
|
+
const historyStateEntry = undoStack.pop();
|
|
250
|
+
|
|
251
|
+
if (current !== null) {
|
|
252
|
+
redoStack.push(current);
|
|
253
|
+
editor.execCommand('canRedo', true);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (undoStack.length === 0) {
|
|
257
|
+
editor.execCommand('canUndo', false);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
historyState.current = historyStateEntry;
|
|
261
|
+
historyStateEntry.editor.setEditorState(historyStateEntry.editorState.clone(historyStateEntry.undoSelection), {
|
|
262
|
+
tag: 'historic'
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const redo = () => {
|
|
268
|
+
const redoStack = historyState.redoStack;
|
|
269
|
+
const undoStack = historyState.undoStack;
|
|
270
|
+
|
|
271
|
+
if (redoStack.length !== 0) {
|
|
272
|
+
const current = historyState.current;
|
|
273
|
+
|
|
274
|
+
if (current !== null) {
|
|
275
|
+
undoStack.push(current);
|
|
276
|
+
editor.execCommand('canUndo', true);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const historyStateEntry = redoStack.pop();
|
|
280
|
+
|
|
281
|
+
if (redoStack.length === 0) {
|
|
282
|
+
editor.execCommand('canRedo', false);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
historyState.current = historyStateEntry;
|
|
286
|
+
historyStateEntry.editor.setEditorState(historyStateEntry.editorState, {
|
|
287
|
+
tag: 'historic'
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
const applyCommand = type => {
|
|
293
|
+
switch (type) {
|
|
294
|
+
case 'undo':
|
|
295
|
+
undo();
|
|
296
|
+
return true;
|
|
297
|
+
|
|
298
|
+
case 'redo':
|
|
299
|
+
redo();
|
|
300
|
+
return true;
|
|
301
|
+
|
|
302
|
+
case 'clearEditor':
|
|
303
|
+
clearHistory();
|
|
304
|
+
return false;
|
|
305
|
+
|
|
306
|
+
case 'clearHistory':
|
|
307
|
+
clearHistory();
|
|
308
|
+
return true;
|
|
309
|
+
|
|
310
|
+
default:
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
return withSubscriptions(editor.addListener('command', applyCommand, EditorPriority), editor.addListener('update', applyChange));
|
|
316
|
+
}, [clearHistory, delay, editor, historyState]);
|
|
317
|
+
}
|
|
318
|
+
function createEmptyHistoryState() {
|
|
319
|
+
return {
|
|
320
|
+
current: null,
|
|
321
|
+
redoStack: [],
|
|
322
|
+
undoStack: []
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
328
|
+
*
|
|
329
|
+
* This source code is licensed under the MIT license found in the
|
|
330
|
+
* LICENSE file in the root directory of this source tree.
|
|
331
|
+
*
|
|
332
|
+
*
|
|
333
|
+
*/
|
|
334
|
+
function useLexicalHistory(editor, externalHistoryState, delay = 1000) {
|
|
335
|
+
return useHistory(editor, externalHistoryState, delay);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
exports.createEmptyHistoryState = createEmptyHistoryState;
|
|
339
|
+
exports.useLexicalHistory = useLexicalHistory;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict'
|
|
8
|
+
const DEPRECATED_useLexicalHistory = process.env.NODE_ENV === 'development' ? require('./DEPRECATED_useLexicalHistory.dev.js') : require('./DEPRECATED_useLexicalHistory.prod.js')
|
|
9
|
+
module.exports = DEPRECATED_useLexicalHistory;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict';var n=require("@lexical/react/withSubscriptions"),r=require("lexical"),v=require("react");
|
|
8
|
+
function w(b,d,k,a,h){if(null===b||0===k.size&&0===a.size)return 0;var g=d._selection,c=b._selection;if(h)return 1;if(null===g||null===c||!c.isCollapsed()||!g.isCollapsed())return 0;var e=Array.from(k);a=Array.from(a);k=d._nodeMap;h=[];for(var f=0;f<e.length;f++){const l=k.get(e[f]);void 0!==l&&h.push(l)}for(e=0;e<a.length;e++)a[e][1]&&(f=k.get(a[e][0]),void 0===f||r.$isRootNode(f)||h.push(f));if(0===h.length)return 0;if(1<h.length)return a=d._nodeMap,d=a.get(g.anchor.key),c=a.get(c.anchor.key),d&&
|
|
9
|
+
c&&!b._nodeMap.has(d.__key)&&r.$isTextNode(d)&&1===d.__text.length&&1===g.anchor.offset?2:0;d=h[0];b=b._nodeMap.get(d.__key);if(!r.$isTextNode(b)||!r.$isTextNode(d)||b.__mode!==d.__mode)return 0;b=b.__text;d=d.__text;if(b===d)return 0;g=g.anchor;c=c.anchor;if(g.key!==c.key||"text"!==g.type)return 0;g=g.offset;c=c.offset;b=d.length-b.length;return 1===b&&c===g-1?2:-1===b&&c===g+1?3:-1===b&&c===g?4:0}
|
|
10
|
+
function x(b,d){let k=Date.now(),a=0;return(h,g,c,e,f,l)=>{const p=Date.now();if(l.has("historic"))return a=0,k=p,2;const m=w(h,g,e,f,b.isComposing()),u=(()=>{if(l.has("without-history"))return 0;if(null===h)return 1;var q=g._selection;const t=h._selection;if(!(0<e.size||0<f.size))return null===t&&null!==q?0:2;q=null===c||c.editor===b;return 0!==m&&m===a&&p<k+d&&q?0:1})();k=p;a=m;return u}}
|
|
11
|
+
function y(b,d,k=1E3){const a=v.useMemo(()=>d||z(),[d]),h=v.useCallback(()=>{a.undoStack=[];a.redoStack=[];a.current=null},[a]);v.useEffect(()=>{const g=x(b,k);return n(b.addListener("command",c=>{switch(c){case "undo":c=a.redoStack;var e=a.undoStack;if(0!==e.length){var f=a.current;const l=e.pop();null!==f&&(c.push(f),b.execCommand("canRedo",!0));0===e.length&&b.execCommand("canUndo",!1);a.current=l;l.editor.setEditorState(l.editorState.clone(l.undoSelection),{tag:"historic"})}return!0;case "redo":return c=
|
|
12
|
+
a.redoStack,e=a.undoStack,0!==c.length&&(f=a.current,null!==f&&(e.push(f),b.execCommand("canUndo",!0)),e=c.pop(),0===c.length&&b.execCommand("canRedo",!1),a.current=e,e.editor.setEditorState(e.editorState,{tag:"historic"})),!0;case "clearEditor":return h(),!1;case "clearHistory":return h(),!0;default:return!1}},0),b.addListener("update",({editorState:c,prevEditorState:e,dirtyLeaves:f,dirtyElements:l,tags:p})=>{const m=a.current,u=a.redoStack,q=a.undoStack,t=null===m?null:m.editorState;if(null===m||
|
|
13
|
+
c!==t){f=g(e,c,m,f,l,p);if(1===f)0!==u.length&&(a.redoStack=[]),null!==m&&(q.push({...m,undoSelection:e.read(r.$getSelection)}),b.execCommand("canUndo",!0));else if(2===f)return;a.current={editor:b,editorState:c}}}))},[h,k,b,a])}function z(){return{current:null,redoStack:[],undoStack:[]}}exports.createEmptyHistoryState=z;exports.useLexicalHistory=function(b,d,k=1E3){return y(b,d,k)};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
var list = require('@lexical/list');
|
|
10
|
+
var react = require('react');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
14
|
+
*
|
|
15
|
+
* This source code is licensed under the MIT license found in the
|
|
16
|
+
* LICENSE file in the root directory of this source tree.
|
|
17
|
+
*
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
const LowPriority = 1;
|
|
21
|
+
function useList(editor) {
|
|
22
|
+
react.useEffect(() => {
|
|
23
|
+
return editor.addListener('command', type => {
|
|
24
|
+
if (type === 'indentContent') {
|
|
25
|
+
const hasHandledIndention = list.indentList();
|
|
26
|
+
|
|
27
|
+
if (hasHandledIndention) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
} else if (type === 'outdentContent') {
|
|
31
|
+
const hasHandledIndention = list.outdentList();
|
|
32
|
+
|
|
33
|
+
if (hasHandledIndention) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
} else if (type === 'insertOrderedList') {
|
|
37
|
+
list.insertList(editor, 'ol');
|
|
38
|
+
return true;
|
|
39
|
+
} else if (type === 'insertUnorderedList') {
|
|
40
|
+
list.insertList(editor, 'ul');
|
|
41
|
+
return true;
|
|
42
|
+
} else if (type === 'removeList') {
|
|
43
|
+
list.removeList(editor);
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return false;
|
|
48
|
+
}, LowPriority);
|
|
49
|
+
}, [editor]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
54
|
+
*
|
|
55
|
+
* This source code is licensed under the MIT license found in the
|
|
56
|
+
* LICENSE file in the root directory of this source tree.
|
|
57
|
+
*
|
|
58
|
+
*
|
|
59
|
+
*/
|
|
60
|
+
function useLexicalList(editor) {
|
|
61
|
+
useList(editor);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = useLexicalList;
|
|
@@ -1 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict'
|
|
8
|
+
const DEPRECATED_useLexicalList = process.env.NODE_ENV === 'development' ? require('./DEPRECATED_useLexicalList.dev.js') : require('./DEPRECATED_useLexicalList.prod.js')
|
|
9
|
+
module.exports = DEPRECATED_useLexicalList;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
'use strict';var b=require("@lexical/list"),d=require("react");function e(a){d.useEffect(()=>a.addListener("command",c=>{if("indentContent"===c){if(b.indentList())return!0}else if("outdentContent"===c){if(b.outdentList())return!0}else{if("insertOrderedList"===c)return b.insertList(a,"ol"),!0;if("insertUnorderedList"===c)return b.insertList(a,"ul"),!0;if("removeList"===c)return b.removeList(a),!0}return!1},1),[a])}module.exports=function(a){e(a)};
|