@react-fabric/lexical 1.0.0-beta.9 → 1.0.1
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/{src/editor/Lexical.module.css → css/_lexical.css} +79 -13
- package/css/styles.css +24 -0
- package/dist/cjs/{packages/lexical/src/editor → editor}/LexicalEditor.js +98 -45
- package/dist/cjs/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +147 -140
- package/dist/cjs/i18n/ar/lexical.json.js +30 -0
- package/dist/cjs/i18n/en/lexical.json.js +30 -0
- package/{src/plugins/StickyPlugin.ts → dist/cjs/i18n/index.js} +10 -15
- package/dist/cjs/index.js +36 -0
- package/dist/cjs/plugins/CommentPlugin.js +481 -0
- package/dist/cjs/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +17 -20
- package/dist/cjs/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +58 -37
- package/dist/esm/{packages/lexical/src/editor → editor}/LexicalEditor.js +101 -48
- package/dist/esm/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +95 -88
- package/dist/esm/i18n/ar/lexical.json.js +25 -0
- package/dist/esm/i18n/en/lexical.json.js +25 -0
- package/dist/esm/i18n/index.js +33 -0
- package/dist/esm/index.js +34 -0
- package/dist/esm/plugins/CommentPlugin.js +481 -0
- package/dist/esm/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +15 -18
- package/dist/esm/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +59 -38
- package/dist/types/editor/LexicalEditor.d.ts +33 -4
- package/dist/types/hooks/useFormatWatcher.d.ts +7 -4
- package/dist/types/i18n/index.d.ts +1 -0
- package/dist/types/index.d.ts +27 -0
- package/dist/types/plugins/CommentPlugin.d.ts +5 -0
- package/dist/types/plugins/DraggableBlockPlugin.d.ts +3 -2
- package/dist/types/plugins/ToolbarPlugin.d.ts +1 -1
- package/dist/types/utils/index.d.ts +3 -6
- package/package.json +18 -18
- package/src/editor/LexicalEditor.tsx +95 -55
- package/src/hooks/useFormatWatcher.ts +50 -74
- package/src/i18n/ar/lexical.json +18 -0
- package/src/i18n/en/lexical.json +18 -0
- package/src/i18n/index.ts +28 -0
- package/src/index.ts +24 -17
- package/src/plugins/CommentPlugin.tsx +523 -0
- package/src/plugins/DraggableBlockPlugin.tsx +23 -68
- package/src/plugins/ToolbarPlugin.tsx +83 -131
- package/src/utils/index.ts +12 -108
- package/dist/cjs/_virtual/prism.js +0 -5
- package/dist/cjs/editor/Lexical.module.css +0 -1
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -432
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -21
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -27
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +0 -1139
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +0 -25
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +0 -50
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +0 -245
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +0 -19
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +0 -18
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +0 -471
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +0 -17
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +0 -24
- package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +0 -1275
- package/dist/cjs/node_modules/@lexical/list/LexicalList.js +0 -29
- package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +0 -29
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -789
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +0 -29
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -26
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -976
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +0 -35
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -35
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +0 -2792
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +0 -23
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +0 -55
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -888
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +0 -35
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -66
- package/dist/cjs/node_modules/@mdi/js/mdi.js +0 -8
- package/dist/cjs/node_modules/prismjs/components/prism-c.js +0 -82
- package/dist/cjs/node_modules/prismjs/components/prism-clike.js +0 -33
- package/dist/cjs/node_modules/prismjs/components/prism-cpp.js +0 -101
- package/dist/cjs/node_modules/prismjs/components/prism-css.js +0 -66
- package/dist/cjs/node_modules/prismjs/components/prism-java.js +0 -126
- package/dist/cjs/node_modules/prismjs/components/prism-javascript.js +0 -174
- package/dist/cjs/node_modules/prismjs/components/prism-markdown.js +0 -417
- package/dist/cjs/node_modules/prismjs/components/prism-markup.js +0 -188
- package/dist/cjs/node_modules/prismjs/components/prism-objectivec.js +0 -14
- package/dist/cjs/node_modules/prismjs/components/prism-powershell.js +0 -60
- package/dist/cjs/node_modules/prismjs/components/prism-python.js +0 -67
- package/dist/cjs/node_modules/prismjs/components/prism-rust.js +0 -130
- package/dist/cjs/node_modules/prismjs/components/prism-sql.js +0 -34
- package/dist/cjs/node_modules/prismjs/components/prism-swift.js +0 -150
- package/dist/cjs/node_modules/prismjs/components/prism-typescript.js +0 -62
- package/dist/cjs/node_modules/prismjs/prism.js +0 -1951
- package/dist/cjs/packages/lexical/src/components/StickyComponent.js +0 -221
- package/dist/cjs/packages/lexical/src/index.js +0 -7
- package/dist/cjs/packages/lexical/src/nodes/StickyNode.js +0 -218
- package/dist/cjs/packages/lexical/src/plugins/StickyPlugin.js +0 -20
- package/dist/esm/_virtual/prism.js +0 -5
- package/dist/esm/editor/Lexical.module.css +0 -1
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -423
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -19
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -18
- package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +0 -1121
- package/dist/esm/node_modules/@lexical/code/LexicalCode.js +0 -21
- package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +0 -32
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +0 -244
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +0 -18
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +0 -17
- package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +0 -463
- package/dist/esm/node_modules/@lexical/link/LexicalLink.js +0 -17
- package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +0 -16
- package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +0 -1262
- package/dist/esm/node_modules/@lexical/list/LexicalList.js +0 -23
- package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +0 -16
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -781
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +0 -23
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -18
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -957
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +0 -26
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -16
- package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +0 -2754
- package/dist/esm/node_modules/@lexical/table/LexicalTable.js +0 -20
- package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +0 -17
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -839
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +0 -26
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -17
- package/dist/esm/node_modules/@mdi/js/mdi.js +0 -7
- package/dist/esm/node_modules/prismjs/components/prism-c.js +0 -82
- package/dist/esm/node_modules/prismjs/components/prism-clike.js +0 -33
- package/dist/esm/node_modules/prismjs/components/prism-cpp.js +0 -101
- package/dist/esm/node_modules/prismjs/components/prism-css.js +0 -66
- package/dist/esm/node_modules/prismjs/components/prism-java.js +0 -126
- package/dist/esm/node_modules/prismjs/components/prism-javascript.js +0 -174
- package/dist/esm/node_modules/prismjs/components/prism-markdown.js +0 -417
- package/dist/esm/node_modules/prismjs/components/prism-markup.js +0 -188
- package/dist/esm/node_modules/prismjs/components/prism-objectivec.js +0 -14
- package/dist/esm/node_modules/prismjs/components/prism-powershell.js +0 -60
- package/dist/esm/node_modules/prismjs/components/prism-python.js +0 -67
- package/dist/esm/node_modules/prismjs/components/prism-rust.js +0 -130
- package/dist/esm/node_modules/prismjs/components/prism-sql.js +0 -34
- package/dist/esm/node_modules/prismjs/components/prism-swift.js +0 -150
- package/dist/esm/node_modules/prismjs/components/prism-typescript.js +0 -62
- package/dist/esm/node_modules/prismjs/prism.js +0 -1951
- package/dist/esm/packages/lexical/src/components/StickyComponent.js +0 -221
- package/dist/esm/packages/lexical/src/index.js +0 -3
- package/dist/esm/packages/lexical/src/nodes/StickyNode.js +0 -216
- package/dist/esm/packages/lexical/src/plugins/StickyPlugin.js +0 -20
- package/dist/types/components/StickyComponent.d.ts +0 -9
- package/dist/types/nodes/StickyNode.d.ts +0 -35
- package/dist/types/plugins/StickyPlugin.d.ts +0 -1
- package/src/components/StickyComponent.tsx +0 -271
- package/src/nodes/StickyNode.tsx +0 -249
- /package/dist/cjs/{packages/lexical/src/editor → editor}/theme.js +0 -0
- /package/dist/cjs/{packages/lexical/src/utils → utils}/index.js +0 -0
- /package/dist/esm/{packages/lexical/src/editor → editor}/theme.js +0 -0
- /package/dist/esm/{packages/lexical/src/utils → utils}/index.js +0 -0
|
@@ -1,423 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { $generateHtmlFromNodes, $generateNodesFromDOM } from '../html/LexicalHtml.js';
|
|
4
|
-
import { $addNodeStyle, $sliceSelectedTextNodeContent } from '../selection/LexicalSelection.js';
|
|
5
|
-
import { objectKlassEquals } from '../utils/LexicalUtils.js';
|
|
6
|
-
import { $isRangeSelection, $getSelection, $createTabNode, SELECTION_INSERT_CLIPBOARD_NODES_COMMAND, $getRoot, $parseSerializedNode, $isTextNode, COPY_COMMAND, COMMAND_PRIORITY_CRITICAL, isSelectionWithinEditor, $getEditor, $isElementNode, $cloneWithProperties } from 'lexical';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
10
|
-
*
|
|
11
|
-
* This source code is licensed under the MIT license found in the
|
|
12
|
-
* LICENSE file in the root directory of this source tree.
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
19
|
-
*
|
|
20
|
-
* This source code is licensed under the MIT license found in the
|
|
21
|
-
* LICENSE file in the root directory of this source tree.
|
|
22
|
-
*
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
29
|
-
*
|
|
30
|
-
* This source code is licensed under the MIT license found in the
|
|
31
|
-
* LICENSE file in the root directory of this source tree.
|
|
32
|
-
*
|
|
33
|
-
*/
|
|
34
|
-
|
|
35
|
-
const getDOMSelection = targetWindow => CAN_USE_DOM ? (targetWindow || window).getSelection() : null;
|
|
36
|
-
/**
|
|
37
|
-
* Returns the *currently selected* Lexical content as an HTML string, relying on the
|
|
38
|
-
* logic defined in the exportDOM methods on the LexicalNode classes. Note that
|
|
39
|
-
* this will not return the HTML content of the entire editor (unless all the content is included
|
|
40
|
-
* in the current selection).
|
|
41
|
-
*
|
|
42
|
-
* @param editor - LexicalEditor instance to get HTML content from
|
|
43
|
-
* @param selection - The selection to use (default is $getSelection())
|
|
44
|
-
* @returns a string of HTML content
|
|
45
|
-
*/
|
|
46
|
-
function $getHtmlContent(editor, selection = $getSelection()) {
|
|
47
|
-
if (selection == null) {
|
|
48
|
-
{
|
|
49
|
-
throw Error(`Expected valid LexicalSelection`);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// If we haven't selected anything
|
|
54
|
-
if ($isRangeSelection(selection) && selection.isCollapsed() || selection.getNodes().length === 0) {
|
|
55
|
-
return '';
|
|
56
|
-
}
|
|
57
|
-
return $generateHtmlFromNodes(editor, selection);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Returns the *currently selected* Lexical content as a JSON string, relying on the
|
|
62
|
-
* logic defined in the exportJSON methods on the LexicalNode classes. Note that
|
|
63
|
-
* this will not return the JSON content of the entire editor (unless all the content is included
|
|
64
|
-
* in the current selection).
|
|
65
|
-
*
|
|
66
|
-
* @param editor - LexicalEditor instance to get the JSON content from
|
|
67
|
-
* @param selection - The selection to use (default is $getSelection())
|
|
68
|
-
* @returns
|
|
69
|
-
*/
|
|
70
|
-
function $getLexicalContent(editor, selection = $getSelection()) {
|
|
71
|
-
if (selection == null) {
|
|
72
|
-
{
|
|
73
|
-
throw Error(`Expected valid LexicalSelection`);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// If we haven't selected anything
|
|
78
|
-
if ($isRangeSelection(selection) && selection.isCollapsed() || selection.getNodes().length === 0) {
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
return JSON.stringify($generateJSONFromSelectedNodes(editor, selection));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Attempts to insert content of the mime-types text/plain or text/uri-list from
|
|
86
|
-
* the provided DataTransfer object into the editor at the provided selection.
|
|
87
|
-
* text/uri-list is only used if text/plain is not also provided.
|
|
88
|
-
*
|
|
89
|
-
* @param dataTransfer an object conforming to the [DataTransfer interface] (https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface)
|
|
90
|
-
* @param selection the selection to use as the insertion point for the content in the DataTransfer object
|
|
91
|
-
*/
|
|
92
|
-
function $insertDataTransferForPlainText(dataTransfer, selection) {
|
|
93
|
-
const text = dataTransfer.getData('text/plain') || dataTransfer.getData('text/uri-list');
|
|
94
|
-
if (text != null) {
|
|
95
|
-
selection.insertRawText(text);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Attempts to insert content of the mime-types application/x-lexical-editor, text/html,
|
|
101
|
-
* text/plain, or text/uri-list (in descending order of priority) from the provided DataTransfer
|
|
102
|
-
* object into the editor at the provided selection.
|
|
103
|
-
*
|
|
104
|
-
* @param dataTransfer an object conforming to the [DataTransfer interface] (https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface)
|
|
105
|
-
* @param selection the selection to use as the insertion point for the content in the DataTransfer object
|
|
106
|
-
* @param editor the LexicalEditor the content is being inserted into.
|
|
107
|
-
*/
|
|
108
|
-
function $insertDataTransferForRichText(dataTransfer, selection, editor) {
|
|
109
|
-
const lexicalString = dataTransfer.getData('application/x-lexical-editor');
|
|
110
|
-
if (lexicalString) {
|
|
111
|
-
try {
|
|
112
|
-
const payload = JSON.parse(lexicalString);
|
|
113
|
-
if (payload.namespace === editor._config.namespace && Array.isArray(payload.nodes)) {
|
|
114
|
-
const nodes = $generateNodesFromSerializedNodes(payload.nodes);
|
|
115
|
-
return $insertGeneratedNodes(editor, nodes, selection);
|
|
116
|
-
}
|
|
117
|
-
} catch (_unused) {
|
|
118
|
-
// Fail silently.
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
const htmlString = dataTransfer.getData('text/html');
|
|
122
|
-
if (htmlString) {
|
|
123
|
-
try {
|
|
124
|
-
const parser = new DOMParser();
|
|
125
|
-
const dom = parser.parseFromString(htmlString, 'text/html');
|
|
126
|
-
const nodes = $generateNodesFromDOM(editor, dom);
|
|
127
|
-
return $insertGeneratedNodes(editor, nodes, selection);
|
|
128
|
-
} catch (_unused2) {
|
|
129
|
-
// Fail silently.
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Multi-line plain text in rich text mode pasted as separate paragraphs
|
|
134
|
-
// instead of single paragraph with linebreaks.
|
|
135
|
-
// Webkit-specific: Supports read 'text/uri-list' in clipboard.
|
|
136
|
-
const text = dataTransfer.getData('text/plain') || dataTransfer.getData('text/uri-list');
|
|
137
|
-
if (text != null) {
|
|
138
|
-
if ($isRangeSelection(selection)) {
|
|
139
|
-
const parts = text.split(/(\r?\n|\t)/);
|
|
140
|
-
if (parts[parts.length - 1] === '') {
|
|
141
|
-
parts.pop();
|
|
142
|
-
}
|
|
143
|
-
for (let i = 0; i < parts.length; i++) {
|
|
144
|
-
const currentSelection = $getSelection();
|
|
145
|
-
if ($isRangeSelection(currentSelection)) {
|
|
146
|
-
const part = parts[i];
|
|
147
|
-
if (part === '\n' || part === '\r\n') {
|
|
148
|
-
currentSelection.insertParagraph();
|
|
149
|
-
} else if (part === '\t') {
|
|
150
|
-
currentSelection.insertNodes([$createTabNode()]);
|
|
151
|
-
} else {
|
|
152
|
-
currentSelection.insertText(part);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
} else {
|
|
157
|
-
selection.insertRawText(text);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Inserts Lexical nodes into the editor using different strategies depending on
|
|
164
|
-
* some simple selection-based heuristics. If you're looking for a generic way to
|
|
165
|
-
* to insert nodes into the editor at a specific selection point, you probably want
|
|
166
|
-
* {@link lexical.$insertNodes}
|
|
167
|
-
*
|
|
168
|
-
* @param editor LexicalEditor instance to insert the nodes into.
|
|
169
|
-
* @param nodes The nodes to insert.
|
|
170
|
-
* @param selection The selection to insert the nodes into.
|
|
171
|
-
*/
|
|
172
|
-
function $insertGeneratedNodes(editor, nodes, selection) {
|
|
173
|
-
if (!editor.dispatchCommand(SELECTION_INSERT_CLIPBOARD_NODES_COMMAND, {
|
|
174
|
-
nodes,
|
|
175
|
-
selection
|
|
176
|
-
})) {
|
|
177
|
-
selection.insertNodes(nodes);
|
|
178
|
-
}
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
function exportNodeToJSON(node) {
|
|
182
|
-
const serializedNode = node.exportJSON();
|
|
183
|
-
const nodeClass = node.constructor;
|
|
184
|
-
if (serializedNode.type !== nodeClass.getType()) {
|
|
185
|
-
{
|
|
186
|
-
throw Error(`LexicalNode: Node ${nodeClass.name} does not implement .exportJSON().`);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
if ($isElementNode(node)) {
|
|
190
|
-
const serializedChildren = serializedNode.children;
|
|
191
|
-
if (!Array.isArray(serializedChildren)) {
|
|
192
|
-
{
|
|
193
|
-
throw Error(`LexicalNode: Node ${nodeClass.name} is an element but .exportJSON() does not have a children array.`);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return serializedNode;
|
|
198
|
-
}
|
|
199
|
-
function $appendNodesToJSON(editor, selection, currentNode, targetArray = []) {
|
|
200
|
-
let shouldInclude = selection !== null ? currentNode.isSelected(selection) : true;
|
|
201
|
-
const shouldExclude = $isElementNode(currentNode) && currentNode.excludeFromCopy('html');
|
|
202
|
-
let target = currentNode;
|
|
203
|
-
if (selection !== null) {
|
|
204
|
-
let clone = $cloneWithProperties(currentNode);
|
|
205
|
-
clone = $isTextNode(clone) && selection !== null ? $sliceSelectedTextNodeContent(selection, clone) : clone;
|
|
206
|
-
target = clone;
|
|
207
|
-
}
|
|
208
|
-
const children = $isElementNode(target) ? target.getChildren() : [];
|
|
209
|
-
const serializedNode = exportNodeToJSON(target);
|
|
210
|
-
|
|
211
|
-
// TODO: TextNode calls getTextContent() (NOT node.__text) within its exportJSON method
|
|
212
|
-
// which uses getLatest() to get the text from the original node with the same key.
|
|
213
|
-
// This is a deeper issue with the word "clone" here, it's still a reference to the
|
|
214
|
-
// same node as far as the LexicalEditor is concerned since it shares a key.
|
|
215
|
-
// We need a way to create a clone of a Node in memory with its own key, but
|
|
216
|
-
// until then this hack will work for the selected text extract use case.
|
|
217
|
-
if ($isTextNode(target)) {
|
|
218
|
-
const text = target.__text;
|
|
219
|
-
// If an uncollapsed selection ends or starts at the end of a line of specialized,
|
|
220
|
-
// TextNodes, such as code tokens, we will get a 'blank' TextNode here, i.e., one
|
|
221
|
-
// with text of length 0. We don't want this, it makes a confusing mess. Reset!
|
|
222
|
-
if (text.length > 0) {
|
|
223
|
-
serializedNode.text = text;
|
|
224
|
-
} else {
|
|
225
|
-
shouldInclude = false;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
for (let i = 0; i < children.length; i++) {
|
|
229
|
-
const childNode = children[i];
|
|
230
|
-
const shouldIncludeChild = $appendNodesToJSON(editor, selection, childNode, serializedNode.children);
|
|
231
|
-
if (!shouldInclude && $isElementNode(currentNode) && shouldIncludeChild && currentNode.extractWithChild(childNode, selection, 'clone')) {
|
|
232
|
-
shouldInclude = true;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
if (shouldInclude && !shouldExclude) {
|
|
236
|
-
targetArray.push(serializedNode);
|
|
237
|
-
} else if (Array.isArray(serializedNode.children)) {
|
|
238
|
-
for (let i = 0; i < serializedNode.children.length; i++) {
|
|
239
|
-
const serializedChildNode = serializedNode.children[i];
|
|
240
|
-
targetArray.push(serializedChildNode);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
return shouldInclude;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// TODO why $ function with Editor instance?
|
|
247
|
-
/**
|
|
248
|
-
* Gets the Lexical JSON of the nodes inside the provided Selection.
|
|
249
|
-
*
|
|
250
|
-
* @param editor LexicalEditor to get the JSON content from.
|
|
251
|
-
* @param selection Selection to get the JSON content from.
|
|
252
|
-
* @returns an object with the editor namespace and a list of serializable nodes as JavaScript objects.
|
|
253
|
-
*/
|
|
254
|
-
function $generateJSONFromSelectedNodes(editor, selection) {
|
|
255
|
-
const nodes = [];
|
|
256
|
-
const root = $getRoot();
|
|
257
|
-
const topLevelChildren = root.getChildren();
|
|
258
|
-
for (let i = 0; i < topLevelChildren.length; i++) {
|
|
259
|
-
const topLevelNode = topLevelChildren[i];
|
|
260
|
-
$appendNodesToJSON(editor, selection, topLevelNode, nodes);
|
|
261
|
-
}
|
|
262
|
-
return {
|
|
263
|
-
namespace: editor._config.namespace,
|
|
264
|
-
nodes
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* This method takes an array of objects conforming to the BaseSeralizedNode interface and returns
|
|
270
|
-
* an Array containing instances of the corresponding LexicalNode classes registered on the editor.
|
|
271
|
-
* Normally, you'd get an Array of BaseSerialized nodes from {@link $generateJSONFromSelectedNodes}
|
|
272
|
-
*
|
|
273
|
-
* @param serializedNodes an Array of objects conforming to the BaseSerializedNode interface.
|
|
274
|
-
* @returns an Array of Lexical Node objects.
|
|
275
|
-
*/
|
|
276
|
-
function $generateNodesFromSerializedNodes(serializedNodes) {
|
|
277
|
-
const nodes = [];
|
|
278
|
-
for (let i = 0; i < serializedNodes.length; i++) {
|
|
279
|
-
const serializedNode = serializedNodes[i];
|
|
280
|
-
const node = $parseSerializedNode(serializedNode);
|
|
281
|
-
if ($isTextNode(node)) {
|
|
282
|
-
$addNodeStyle(node);
|
|
283
|
-
}
|
|
284
|
-
nodes.push(node);
|
|
285
|
-
}
|
|
286
|
-
return nodes;
|
|
287
|
-
}
|
|
288
|
-
const EVENT_LATENCY = 50;
|
|
289
|
-
let clipboardEventTimeout = null;
|
|
290
|
-
|
|
291
|
-
// TODO custom selection
|
|
292
|
-
// TODO potentially have a node customizable version for plain text
|
|
293
|
-
/**
|
|
294
|
-
* Copies the content of the current selection to the clipboard in
|
|
295
|
-
* text/plain, text/html, and application/x-lexical-editor (Lexical JSON)
|
|
296
|
-
* formats.
|
|
297
|
-
*
|
|
298
|
-
* @param editor the LexicalEditor instance to copy content from
|
|
299
|
-
* @param event the native browser ClipboardEvent to add the content to.
|
|
300
|
-
* @returns
|
|
301
|
-
*/
|
|
302
|
-
async function copyToClipboard(editor, event, data) {
|
|
303
|
-
if (clipboardEventTimeout !== null) {
|
|
304
|
-
// Prevent weird race conditions that can happen when this function is run multiple times
|
|
305
|
-
// synchronously. In the future, we can do better, we can cancel/override the previously running job.
|
|
306
|
-
return false;
|
|
307
|
-
}
|
|
308
|
-
if (event !== null) {
|
|
309
|
-
return new Promise((resolve, reject) => {
|
|
310
|
-
editor.update(() => {
|
|
311
|
-
resolve($copyToClipboardEvent(editor, event, data));
|
|
312
|
-
});
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
const rootElement = editor.getRootElement();
|
|
316
|
-
const windowDocument = editor._window == null ? window.document : editor._window.document;
|
|
317
|
-
const domSelection = getDOMSelection(editor._window);
|
|
318
|
-
if (rootElement === null || domSelection === null) {
|
|
319
|
-
return false;
|
|
320
|
-
}
|
|
321
|
-
const element = windowDocument.createElement('span');
|
|
322
|
-
element.style.cssText = 'position: fixed; top: -1000px;';
|
|
323
|
-
element.append(windowDocument.createTextNode('#'));
|
|
324
|
-
rootElement.append(element);
|
|
325
|
-
const range = new Range();
|
|
326
|
-
range.setStart(element, 0);
|
|
327
|
-
range.setEnd(element, 1);
|
|
328
|
-
domSelection.removeAllRanges();
|
|
329
|
-
domSelection.addRange(range);
|
|
330
|
-
return new Promise((resolve, reject) => {
|
|
331
|
-
const removeListener = editor.registerCommand(COPY_COMMAND, secondEvent => {
|
|
332
|
-
if (objectKlassEquals(secondEvent, ClipboardEvent)) {
|
|
333
|
-
removeListener();
|
|
334
|
-
if (clipboardEventTimeout !== null) {
|
|
335
|
-
window.clearTimeout(clipboardEventTimeout);
|
|
336
|
-
clipboardEventTimeout = null;
|
|
337
|
-
}
|
|
338
|
-
resolve($copyToClipboardEvent(editor, secondEvent, data));
|
|
339
|
-
}
|
|
340
|
-
// Block the entire copy flow while we wait for the next ClipboardEvent
|
|
341
|
-
return true;
|
|
342
|
-
}, COMMAND_PRIORITY_CRITICAL);
|
|
343
|
-
// If the above hack execCommand hack works, this timeout code should never fire. Otherwise,
|
|
344
|
-
// the listener will be quickly freed so that the user can reuse it again
|
|
345
|
-
clipboardEventTimeout = window.setTimeout(() => {
|
|
346
|
-
removeListener();
|
|
347
|
-
clipboardEventTimeout = null;
|
|
348
|
-
resolve(false);
|
|
349
|
-
}, EVENT_LATENCY);
|
|
350
|
-
windowDocument.execCommand('copy');
|
|
351
|
-
element.remove();
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
// TODO shouldn't pass editor (pass namespace directly)
|
|
356
|
-
function $copyToClipboardEvent(editor, event, data) {
|
|
357
|
-
if (data === undefined) {
|
|
358
|
-
const domSelection = getDOMSelection(editor._window);
|
|
359
|
-
if (!domSelection) {
|
|
360
|
-
return false;
|
|
361
|
-
}
|
|
362
|
-
const anchorDOM = domSelection.anchorNode;
|
|
363
|
-
const focusDOM = domSelection.focusNode;
|
|
364
|
-
if (anchorDOM !== null && focusDOM !== null && !isSelectionWithinEditor(editor, anchorDOM, focusDOM)) {
|
|
365
|
-
return false;
|
|
366
|
-
}
|
|
367
|
-
const selection = $getSelection();
|
|
368
|
-
if (selection === null) {
|
|
369
|
-
return false;
|
|
370
|
-
}
|
|
371
|
-
data = $getClipboardDataFromSelection(selection);
|
|
372
|
-
}
|
|
373
|
-
event.preventDefault();
|
|
374
|
-
const clipboardData = event.clipboardData;
|
|
375
|
-
if (clipboardData === null) {
|
|
376
|
-
return false;
|
|
377
|
-
}
|
|
378
|
-
setLexicalClipboardDataTransfer(clipboardData, data);
|
|
379
|
-
return true;
|
|
380
|
-
}
|
|
381
|
-
const clipboardDataFunctions = [['text/html', $getHtmlContent], ['application/x-lexical-editor', $getLexicalContent]];
|
|
382
|
-
|
|
383
|
-
/**
|
|
384
|
-
* Serialize the content of the current selection to strings in
|
|
385
|
-
* text/plain, text/html, and application/x-lexical-editor (Lexical JSON)
|
|
386
|
-
* formats (as available).
|
|
387
|
-
*
|
|
388
|
-
* @param selection the selection to serialize (defaults to $getSelection())
|
|
389
|
-
* @returns LexicalClipboardData
|
|
390
|
-
*/
|
|
391
|
-
function $getClipboardDataFromSelection(selection = $getSelection()) {
|
|
392
|
-
const clipboardData = {
|
|
393
|
-
'text/plain': selection ? selection.getTextContent() : ''
|
|
394
|
-
};
|
|
395
|
-
if (selection) {
|
|
396
|
-
const editor = $getEditor();
|
|
397
|
-
for (const [mimeType, $editorFn] of clipboardDataFunctions) {
|
|
398
|
-
const v = $editorFn(editor, selection);
|
|
399
|
-
if (v !== null) {
|
|
400
|
-
clipboardData[mimeType] = v;
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
return clipboardData;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* Call setData on the given clipboardData for each MIME type present
|
|
409
|
-
* in the given data (from {@link $getClipboardDataFromSelection})
|
|
410
|
-
*
|
|
411
|
-
* @param clipboardData the event.clipboardData to populate from data
|
|
412
|
-
* @param data The lexical data
|
|
413
|
-
*/
|
|
414
|
-
function setLexicalClipboardDataTransfer(clipboardData, data) {
|
|
415
|
-
for (const k in data) {
|
|
416
|
-
const v = data[k];
|
|
417
|
-
if (v !== undefined) {
|
|
418
|
-
clipboardData.setData(k, v);
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
export { $generateJSONFromSelectedNodes, $generateNodesFromSerializedNodes, $getClipboardDataFromSelection, $getHtmlContent, $getLexicalContent, $insertDataTransferForPlainText, $insertDataTransferForRichText, $insertGeneratedNodes, copyToClipboard, setLexicalClipboardDataTransfer };
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import * as LexicalClipboard_dev from './LexicalClipboard.dev.js';
|
|
4
|
-
import * as LexicalClipboard_prod from './LexicalClipboard.prod.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
8
|
-
*
|
|
9
|
-
* This source code is licensed under the MIT license found in the
|
|
10
|
-
* LICENSE file in the root directory of this source tree.
|
|
11
|
-
*
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
const mod = process.env.NODE_ENV === 'development' ? LexicalClipboard_dev : LexicalClipboard_prod;
|
|
15
|
-
const $getClipboardDataFromSelection = mod.$getClipboardDataFromSelection;
|
|
16
|
-
const $insertDataTransferForRichText = mod.$insertDataTransferForRichText;
|
|
17
|
-
const copyToClipboard = mod.copyToClipboard;
|
|
18
|
-
|
|
19
|
-
export { $getClipboardDataFromSelection, $insertDataTransferForRichText, copyToClipboard };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { $generateHtmlFromNodes, $generateNodesFromDOM } from '../html/LexicalHtml.js';
|
|
4
|
-
import { $addNodeStyle, $sliceSelectedTextNodeContent } from '../selection/LexicalSelection.js';
|
|
5
|
-
import { objectKlassEquals } from '../utils/LexicalUtils.js';
|
|
6
|
-
import { $isRangeSelection, $getSelection, $createTabNode, SELECTION_INSERT_CLIPBOARD_NODES_COMMAND, $getRoot, $parseSerializedNode, $isTextNode, COPY_COMMAND, COMMAND_PRIORITY_CRITICAL, isSelectionWithinEditor, $getEditor, $isElementNode, $cloneWithProperties } from 'lexical';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
10
|
-
*
|
|
11
|
-
* This source code is licensed under the MIT license found in the
|
|
12
|
-
* LICENSE file in the root directory of this source tree.
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
function w(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var y=w((function(t){const e=new URLSearchParams;e.append("code",t);for(let t=1;t<arguments.length;t++)e.append("v",arguments[t]);throw Error(`Minified Lexical error #${t}; visit https://lexical.dev/docs/error?${e} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));const v="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,C=t=>v?(t||window).getSelection():null;function D(e,n=$getSelection()){return null==n&&y(166),$isRangeSelection(n)&&n.isCollapsed()||0===n.getNodes().length?"":$generateHtmlFromNodes(e,n)}function N(t,e=$getSelection()){return null==e&&y(166),$isRangeSelection(e)&&e.isCollapsed()||0===e.getNodes().length?null:JSON.stringify(R(t,e))}function _(t,e){const n=t.getData("text/plain")||t.getData("text/uri-list");null!=n&&e.insertRawText(n);}function T(t,n,o){const r=t.getData("application/x-lexical-editor");if(r)try{const t=JSON.parse(r);if(t.namespace===o._config.namespace&&Array.isArray(t.nodes)){return S(o,E(t.nodes),n)}}catch(t){}const s=t.getData("text/html");if(s)try{const t=(new DOMParser).parseFromString(s,"text/html");return S(o,$generateNodesFromDOM(o,t),n)}catch(t){}const a=t.getData("text/plain")||t.getData("text/uri-list");if(null!=a)if($isRangeSelection(n)){const t=a.split(/(\r?\n|\t)/);""===t[t.length-1]&&t.pop();for(let e=0;e<t.length;e++){const n=$getSelection();if($isRangeSelection(n)){const o=t[e];"\n"===o||"\r\n"===o?n.insertParagraph():"\t"===o?n.insertNodes([$createTabNode()]):n.insertText(o);}}}else n.insertRawText(a);}function S(t,e,n){t.dispatchCommand(SELECTION_INSERT_CLIPBOARD_NODES_COMMAND,{nodes:e,selection:n})||n.insertNodes(e);}function A(t,e,n,r=[]){let l=null===e||n.isSelected(e);const i=$isElementNode(n)&&n.excludeFromCopy("html");let c=n;if(null!==e){let t=$cloneWithProperties(n);t=$isTextNode(t)&&null!==e?$sliceSelectedTextNodeContent(e,t):t,c=t;}const s=$isElementNode(c)?c.getChildren():[],a=function(t){const e=t.exportJSON(),n=t.constructor;if(e.type!==n.getType()&&y(58,n.name),$isElementNode(t)){const t=e.children;Array.isArray(t)||y(59,n.name);}return e}(c);if($isTextNode(c)){const t=c.__text;t.length>0?a.text=t:l=!1;}for(let o=0;o<s.length;o++){const r=s[o],i=A(t,e,r,a.children);!l&&$isElementNode(n)&&i&&n.extractWithChild(r,e,"clone")&&(l=!0);}if(l&&!i)r.push(a);else if(Array.isArray(a.children))for(let t=0;t<a.children.length;t++){const e=a.children[t];r.push(e);}return l}function R(t,e){const n=[],o=$getRoot().getChildren();for(let r=0;r<o.length;r++){A(t,e,o[r],n);}return {namespace:t._config.namespace,nodes:n}}function E(t){const e=[];for(let o=0;o<t.length;o++){const r=t[o],l=$parseSerializedNode(r);$isTextNode(l)&&$addNodeStyle(l),e.push(l);}return e}let O=null;async function P(t,e,n){if(null!==O)return !1;if(null!==e)return new Promise(((o,r)=>{t.update((()=>{o(b(t,e,n));}));}));const o=t.getRootElement(),l=null==t._window?window.document:t._window.document,i=C(t._window);if(null===o||null===i)return !1;const c=l.createElement("span");c.style.cssText="position: fixed; top: -1000px;",c.append(l.createTextNode("#")),o.append(c);const s=new Range;return s.setStart(c,0),s.setEnd(c,1),i.removeAllRanges(),i.addRange(s),new Promise(((e,o)=>{const i=t.registerCommand(COPY_COMMAND,(o=>(objectKlassEquals(o,ClipboardEvent)&&(i(),null!==O&&(window.clearTimeout(O),O=null),e(b(t,o,n))),!0)),COMMAND_PRIORITY_CRITICAL);O=window.setTimeout((()=>{i(),O=null,e(!1);}),50),l.execCommand("copy"),c.remove();}))}function b(t,e,n){if(void 0===n){const e=C(t._window);if(!e)return !1;const o=e.anchorNode,r=e.focusNode;if(null!==o&&null!==r&&!isSelectionWithinEditor(t,o,r))return !1;const l=$getSelection();if(null===l)return !1;n=M(l);}e.preventDefault();const o=e.clipboardData;return null!==o&&(F(o,n),!0)}const J=[["text/html",D],["application/x-lexical-editor",N]];function M(t=$getSelection()){const e={"text/plain":t?t.getTextContent():""};if(t){const n=$getEditor();for(const[o,r]of J){const l=r(n,t);null!==l&&(e[o]=l);}}return e}function F(t,e){for(const n in e){const o=e[n];void 0!==o&&t.setData(n,o);}}
|
|
17
|
-
|
|
18
|
-
export { R as $generateJSONFromSelectedNodes, E as $generateNodesFromSerializedNodes, M as $getClipboardDataFromSelection, D as $getHtmlContent, N as $getLexicalContent, _ as $insertDataTransferForPlainText, T as $insertDataTransferForRichText, S as $insertGeneratedNodes, P as copyToClipboard, F as setLexicalClipboardDataTransfer };
|