@haklex/rich-editor 0.0.82 → 0.0.83
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/dist/AlertQuoteEditNode-B2q-1u6S.js +293 -0
- package/dist/KaTeXRenderer-BFryJSIS.js +218 -0
- package/dist/LinkCardRenderer-QmkOlyXb.js +36 -0
- package/dist/MermaidPlugin-BEtIhQHn.js +97 -0
- package/dist/PresentDialogContext-D5dHWtzD.js +58 -0
- package/dist/RubyRenderer-cm5eDc9q.js +14 -0
- package/dist/SubmitShortcutPlugin-o2y6b-8f.js +1650 -0
- package/dist/commands-entry.mjs +23 -14
- package/dist/components/LinkFavicon.d.ts.map +1 -1
- package/dist/components/renderers/FootnoteRenderer.d.ts.map +1 -1
- package/dist/components/renderers/FootnoteSectionEditRenderer.d.ts.map +1 -1
- package/dist/components/renderers/FootnoteSectionRenderer.d.ts.map +1 -1
- package/dist/components/renderers/FootnoteStaticRenderer.d.ts.map +1 -1
- package/dist/components/renderers/RubyRenderer.d.ts.map +1 -1
- package/dist/components/renderers/TagRenderer.d.ts.map +1 -1
- package/dist/config-BP_kyJbW.js +1363 -0
- package/dist/config-edit.d.ts.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/favicon-BQgbXF_a.js +43 -0
- package/dist/index.mjs +283 -259
- package/dist/node-registry-Dzp7X3R4.js +811 -0
- package/dist/nodes/AlertQuoteNode.d.ts.map +1 -1
- package/dist/nodes/CommentNode.d.ts +21 -0
- package/dist/nodes/CommentNode.d.ts.map +1 -0
- package/dist/nodes/FootnoteNode.d.ts.map +1 -1
- package/dist/nodes/FootnoteSectionNode.d.ts.map +1 -1
- package/dist/nodes/RubyNode.d.ts.map +1 -1
- package/dist/nodes/SpoilerNode.d.ts.map +1 -1
- package/dist/nodes/TagNode.d.ts.map +1 -1
- package/dist/nodes-entry.d.ts +2 -0
- package/dist/nodes-entry.d.ts.map +1 -1
- package/dist/nodes-entry.mjs +50 -7
- package/dist/plugins/MarkdownPastePlugin.d.ts.map +1 -1
- package/dist/plugins-entry.mjs +27 -10
- package/dist/renderers-entry.mjs +61 -40
- package/dist/rich-editor.css +1 -2
- package/dist/static-entry.d.ts +1 -1
- package/dist/static-entry.d.ts.map +1 -1
- package/dist/static-entry.mjs +55 -16
- package/dist/styles/shared.css.d.ts +164 -0
- package/dist/styles/shared.css.d.ts.map +1 -1
- package/dist/styles/theme.d.ts.map +1 -1
- package/dist/styles-entry.d.ts +2 -2
- package/dist/styles-entry.d.ts.map +1 -1
- package/dist/styles-entry.mjs +14 -3
- package/dist/theme-Cg8JUW_j.js +1034 -0
- package/dist/transformers/comment.d.ts +4 -0
- package/dist/transformers/comment.d.ts.map +1 -0
- package/dist/transformers/index.d.ts +1 -0
- package/dist/transformers/index.d.ts.map +1 -1
- package/dist/utils-gy2c_HWc.js +24 -0
- package/package.json +4 -4
- package/dist/AlertQuoteEditNode-BDoonB4u.js +0 -267
- package/dist/KaTeXRenderer-BsyRH5_t.js +0 -214
- package/dist/LinkCardRenderer-OEMBDUhD.js +0 -45
- package/dist/MermaidPlugin-Df2Cm2yX.js +0 -67
- package/dist/PresentDialogContext-DQw6MrNh.js +0 -59
- package/dist/RubyRenderer-BpDb_amz.js +0 -14
- package/dist/SubmitShortcutPlugin-6IvsbZD-.js +0 -1463
- package/dist/config-tfg1FWhG.js +0 -1149
- package/dist/favicon-DIWusrrw.js +0 -41
- package/dist/node-registry-CeVi2y9f.js +0 -666
- package/dist/shared.css-wgGgtK_e.js +0 -23
- package/dist/theme-DHOUKKSr.js +0 -1034
|
@@ -0,0 +1,1650 @@
|
|
|
1
|
+
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { CheckListPlugin } from "@lexical/react/LexicalCheckListPlugin";
|
|
3
|
+
import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
|
|
4
|
+
import { ListPlugin } from "@lexical/react/LexicalListPlugin";
|
|
5
|
+
import { TabIndentationPlugin } from "@lexical/react/LexicalTabIndentationPlugin";
|
|
6
|
+
import { TablePlugin } from "@lexical/react/LexicalTablePlugin";
|
|
7
|
+
import { createLinkMatcherWithRegExp, registerAutoLink, LinkNode, AutoLinkNode } from "@lexical/link";
|
|
8
|
+
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
9
|
+
import { useEffect, createContext, use, useRef, useState, useMemo, useCallback } from "react";
|
|
10
|
+
import { $isQuoteNode, QuoteNode, $createQuoteNode, DRAG_DROP_PASTE } from "@lexical/rich-text";
|
|
11
|
+
import { KEY_ARROW_RIGHT_COMMAND, $getSelection, $isRangeSelection, COMMAND_PRIORITY_CRITICAL, KEY_ARROW_DOWN_COMMAND, $isDecoratorNode, $isElementNode, $isRootNode, $isParagraphNode, $createParagraphNode, KEY_ENTER_COMMAND, $isNodeSelection, KEY_BACKSPACE_COMMAND, COMMAND_PRIORITY_HIGH, KEY_DELETE_COMMAND, KEY_ARROW_UP_COMMAND, $isTextNode, IS_CODE, $createNodeSelection, $setSelection, $getRoot, COMMAND_PRIORITY_EDITOR, COMMAND_PRIORITY_LOW, $createTextNode, $createLineBreakNode, PASTE_COMMAND, $insertNodes, createEditor, $parseSerializedNode, createState, $getState, $addUpdateTag, $setState, $nodesOfType } from "lexical";
|
|
12
|
+
import { b as setCodeBlockCursorIntent, g as getResolvedEditNodes, $ as $createBannerEditNode, d as $createCodeBlockEditNode } from "./node-registry-Dzp7X3R4.js";
|
|
13
|
+
import { HorizontalRuleNode, $createHorizontalRuleNode, INSERT_HORIZONTAL_RULE_COMMAND } from "@lexical/extension";
|
|
14
|
+
import { HORIZONTAL_RULE_BLOCK_TRANSFORMER as HORIZONTAL_RULE_BLOCK_TRANSFORMER$1, GIT_ALERT_TRANSFORMER as GIT_ALERT_TRANSFORMER$1, COMMENT_TRANSFORMER as COMMENT_TRANSFORMER$1, CONTAINER_TRANSFORMER as CONTAINER_TRANSFORMER$1, FOOTNOTE_TRANSFORMER as FOOTNOTE_TRANSFORMER$1, FOOTNOTE_SECTION_TRANSFORMER as FOOTNOTE_SECTION_TRANSFORMER$1, KATEX_INLINE_TRANSFORMER as KATEX_INLINE_TRANSFORMER$1, KATEX_BLOCK_TRANSFORMER as KATEX_BLOCK_TRANSFORMER$1, MENTION_TRANSFORMER as MENTION_TRANSFORMER$1, RUBY_TRANSFORMER as RUBY_TRANSFORMER$1, SPOILER_TRANSFORMER as SPOILER_TRANSFORMER$1, INSERT_TRANSFORMER, SUPERSCRIPT_TRANSFORMER, SUBSCRIPT_TRANSFORMER, IMAGE_BLOCK_TRANSFORMER, VIDEO_BLOCK_TRANSFORMER, CODE_BLOCK_NODE_TRANSFORMER, LINK_CARD_BLOCK_TRANSFORMER, MERMAID_BLOCK_TRANSFORMER, TABLE_BLOCK_TRANSFORMER } from "@haklex/rich-headless/transformers";
|
|
15
|
+
import { CHECK_LIST, TRANSFORMERS, QUOTE, CODE, $convertFromMarkdownString } from "@lexical/markdown";
|
|
16
|
+
import { $ as $createAlertQuoteEditNode } from "./AlertQuoteEditNode-B2q-1u6S.js";
|
|
17
|
+
import { s as AlertQuoteNode, F as FootnoteNode, w as $createFootnoteNode, k as KaTeXInlineNode, K as KaTeXBlockNode, t as $createKaTeXInlineNode, v as $createKaTeXBlockNode, M as MentionNode, a as $createMentionNode, o as extractTextContent, S as SpoilerNode, x as $createSpoilerNode, $ as $createImageNode, O as OPEN_IMAGE_UPLOAD_DIALOG_COMMAND } from "./theme-Cg8JUW_j.js";
|
|
18
|
+
import { v as CodeBlockNode, C as CommentNode, w as $createCommentPlaceholderNode, $ as $createCommentNode, q as BannerNode, D as DetailsNode, x as $createDetailsNode, F as FootnoteSectionNode, f as $isFootnoteSectionNode, a as $createFootnoteSectionNode, g as $isGridContainerNode, R as RubyNode, d as $createRubyNode } from "./config-BP_kyJbW.js";
|
|
19
|
+
import { $createTableNode, $createTableRowNode, $createTableCellNode, TableCellHeaderStates } from "@lexical/table";
|
|
20
|
+
import { MarkdownShortcutPlugin } from "@lexical/react/LexicalMarkdownShortcutPlugin";
|
|
21
|
+
import { nanoid } from "nanoid";
|
|
22
|
+
import { Dialog, DialogPopup, DialogTitle, SegmentedControl, ActionButton, ActionBar } from "@haklex/rich-editor-ui";
|
|
23
|
+
import { Check, Info, Upload, Link2 } from "lucide-react";
|
|
24
|
+
import { i as computeImageMeta, F as FootnoteDefinitionsProvider } from "./KaTeXRenderer-BFryJSIS.js";
|
|
25
|
+
import "./utils-gy2c_HWc.js";
|
|
26
|
+
import { g as getHostname, p as probeFavicon } from "./favicon-BQgbXF_a.js";
|
|
27
|
+
const URL_REGEX = /https?:\/\/(?:www\.)?[\w#%+.:=@~-]{1,256}\.[A-Za-z]{2}[\w#%&()+./:=?@~-]*/;
|
|
28
|
+
const EMAIL_REGEX = /(?:[\w%+.-]+@[\d.a-z-]+\.[a-z]{2,})/i;
|
|
29
|
+
const URL_MATCHER = createLinkMatcherWithRegExp(URL_REGEX);
|
|
30
|
+
const EMAIL_MATCHER = createLinkMatcherWithRegExp(EMAIL_REGEX, (text) => `mailto:${text}`);
|
|
31
|
+
const DEFAULT_MATCHERS = [URL_MATCHER, EMAIL_MATCHER];
|
|
32
|
+
function AutoLinkPlugin({ matchers }) {
|
|
33
|
+
const [editor] = useLexicalComposerContext();
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
return registerAutoLink(editor, {
|
|
36
|
+
matchers: matchers ?? DEFAULT_MATCHERS,
|
|
37
|
+
changeHandlers: [],
|
|
38
|
+
excludeParents: []
|
|
39
|
+
});
|
|
40
|
+
}, [editor, matchers]);
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
function selectDecoratorNode(node, cursorPlacement = "start") {
|
|
44
|
+
if (node.getType() === "code-block") {
|
|
45
|
+
setCodeBlockCursorIntent(node.getKey(), cursorPlacement);
|
|
46
|
+
}
|
|
47
|
+
const selection = $createNodeSelection();
|
|
48
|
+
selection.add(node.getKey());
|
|
49
|
+
$setSelection(selection);
|
|
50
|
+
}
|
|
51
|
+
function isAtTopLevelBoundary(selection, direction) {
|
|
52
|
+
const point = selection.anchor;
|
|
53
|
+
const topLevel = point.getNode().getTopLevelElementOrThrow();
|
|
54
|
+
const pointNode = point.getNode();
|
|
55
|
+
if (point.type === "text") {
|
|
56
|
+
if (!$isTextNode(pointNode)) return false;
|
|
57
|
+
const expectedOffset = direction === "start" ? 0 : pointNode.getTextContentSize();
|
|
58
|
+
if (point.offset !== expectedOffset) return false;
|
|
59
|
+
} else {
|
|
60
|
+
if (!$isElementNode(pointNode)) return false;
|
|
61
|
+
const expectedOffset = direction === "start" ? 0 : pointNode.getChildrenSize();
|
|
62
|
+
if (point.offset !== expectedOffset) return false;
|
|
63
|
+
}
|
|
64
|
+
let current = pointNode;
|
|
65
|
+
while (current && current !== topLevel) {
|
|
66
|
+
const sibling = direction === "start" ? current.getPreviousSibling() : current.getNextSibling();
|
|
67
|
+
if (sibling !== null) return false;
|
|
68
|
+
current = current.getParent();
|
|
69
|
+
}
|
|
70
|
+
return current === topLevel;
|
|
71
|
+
}
|
|
72
|
+
function isSingleLineParagraph(node) {
|
|
73
|
+
return $isParagraphNode(node) && !node.getTextContent().includes("\n");
|
|
74
|
+
}
|
|
75
|
+
function getOutermostNodeWithinTopLevel(node, topLevel) {
|
|
76
|
+
let current = node;
|
|
77
|
+
while (current.getParent() !== topLevel) {
|
|
78
|
+
const parent = current.getParent();
|
|
79
|
+
if (!parent) break;
|
|
80
|
+
current = parent;
|
|
81
|
+
}
|
|
82
|
+
return current;
|
|
83
|
+
}
|
|
84
|
+
function exitInlineCodeAtLineEnd(selection) {
|
|
85
|
+
if (!selection.isCollapsed() || selection.anchor.type !== "text") {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
const anchorNode = selection.anchor.getNode();
|
|
89
|
+
if (!$isTextNode(anchorNode) || !anchorNode.hasFormat("code")) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if (selection.anchor.offset !== anchorNode.getTextContentSize()) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (!isAtTopLevelBoundary(selection, "end")) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
const topLevel = anchorNode.getTopLevelElementOrThrow();
|
|
99
|
+
const boundaryNode = getOutermostNodeWithinTopLevel(anchorNode, topLevel);
|
|
100
|
+
const exitOffset = boundaryNode.getIndexWithinParent() + 1;
|
|
101
|
+
selection.anchor.set(topLevel.getKey(), exitOffset, "element");
|
|
102
|
+
selection.focus.set(topLevel.getKey(), exitOffset, "element");
|
|
103
|
+
selection.setFormat(anchorNode.getFormat() & ~IS_CODE);
|
|
104
|
+
selection.setStyle(anchorNode.getStyle());
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
function BlockExitPlugin() {
|
|
108
|
+
const [editor] = useLexicalComposerContext();
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
const unregisterArrowRight = editor.registerCommand(
|
|
111
|
+
KEY_ARROW_RIGHT_COMMAND,
|
|
112
|
+
(event) => {
|
|
113
|
+
const selection = $getSelection();
|
|
114
|
+
if (!$isRangeSelection(selection)) return false;
|
|
115
|
+
if (!exitInlineCodeAtLineEnd(selection)) return false;
|
|
116
|
+
event?.preventDefault();
|
|
117
|
+
return true;
|
|
118
|
+
},
|
|
119
|
+
COMMAND_PRIORITY_CRITICAL
|
|
120
|
+
);
|
|
121
|
+
const unregisterArrowDown = editor.registerCommand(
|
|
122
|
+
KEY_ARROW_DOWN_COMMAND,
|
|
123
|
+
(event) => {
|
|
124
|
+
const selection = $getSelection();
|
|
125
|
+
if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const anchorNode = selection.anchor.getNode();
|
|
129
|
+
const topLevelElement = anchorNode.getTopLevelElementOrThrow();
|
|
130
|
+
const shouldSelectNextDecorator = isAtTopLevelBoundary(selection, "end") || isSingleLineParagraph(topLevelElement);
|
|
131
|
+
if (!$isQuoteNode(topLevelElement) && shouldSelectNextDecorator) {
|
|
132
|
+
const next2 = topLevelElement.getNextSibling();
|
|
133
|
+
if (next2 && $isDecoratorNode(next2) && next2.isKeyboardSelectable()) {
|
|
134
|
+
event?.preventDefault();
|
|
135
|
+
selectDecoratorNode(next2, "start");
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const element = $isElementNode(anchorNode) ? anchorNode : anchorNode.getParentOrThrow();
|
|
140
|
+
let quoteChild = element;
|
|
141
|
+
let quoteNode = null;
|
|
142
|
+
let current = element;
|
|
143
|
+
while (current) {
|
|
144
|
+
const parent = current.getParent();
|
|
145
|
+
if (!parent || $isRootNode(parent)) break;
|
|
146
|
+
if ($isQuoteNode(parent)) {
|
|
147
|
+
quoteNode = parent;
|
|
148
|
+
quoteChild = current;
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
current = parent;
|
|
152
|
+
}
|
|
153
|
+
if (!quoteNode) return false;
|
|
154
|
+
if (quoteChild.getNextSibling() !== null) return false;
|
|
155
|
+
if (!$isParagraphNode(quoteChild) || quoteChild.getTextContent() !== "") return false;
|
|
156
|
+
event?.preventDefault();
|
|
157
|
+
let next = quoteNode.getNextSibling();
|
|
158
|
+
if (!next) {
|
|
159
|
+
next = $createParagraphNode();
|
|
160
|
+
quoteNode.insertAfter(next);
|
|
161
|
+
}
|
|
162
|
+
next.selectStart();
|
|
163
|
+
return true;
|
|
164
|
+
},
|
|
165
|
+
COMMAND_PRIORITY_CRITICAL
|
|
166
|
+
);
|
|
167
|
+
const unregisterCmdEnter = editor.registerCommand(
|
|
168
|
+
KEY_ENTER_COMMAND,
|
|
169
|
+
(event) => {
|
|
170
|
+
if (!event?.metaKey && !event?.ctrlKey) return false;
|
|
171
|
+
const selection = $getSelection();
|
|
172
|
+
if ($isNodeSelection(selection)) {
|
|
173
|
+
const nodes = selection.getNodes();
|
|
174
|
+
if (nodes.length !== 1) return false;
|
|
175
|
+
const node = nodes[0];
|
|
176
|
+
if (!$isDecoratorNode(node)) return false;
|
|
177
|
+
event.preventDefault();
|
|
178
|
+
let next2 = node.getNextSibling();
|
|
179
|
+
if (!next2) {
|
|
180
|
+
next2 = $createParagraphNode();
|
|
181
|
+
node.insertAfter(next2);
|
|
182
|
+
}
|
|
183
|
+
if ($isElementNode(next2)) {
|
|
184
|
+
next2.selectStart();
|
|
185
|
+
} else if ($isDecoratorNode(next2)) {
|
|
186
|
+
selectDecoratorNode(next2, "start");
|
|
187
|
+
}
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
if (!$isRangeSelection(selection)) return false;
|
|
191
|
+
const anchorNode = selection.anchor.getNode();
|
|
192
|
+
const topLevelElement = anchorNode.getTopLevelElementOrThrow();
|
|
193
|
+
if ($isParagraphNode(topLevelElement)) return false;
|
|
194
|
+
event.preventDefault();
|
|
195
|
+
let next = topLevelElement.getNextSibling();
|
|
196
|
+
if (!next || !$isParagraphNode(next)) {
|
|
197
|
+
next = $createParagraphNode();
|
|
198
|
+
topLevelElement.insertAfter(next);
|
|
199
|
+
}
|
|
200
|
+
next.selectStart();
|
|
201
|
+
return true;
|
|
202
|
+
},
|
|
203
|
+
COMMAND_PRIORITY_CRITICAL
|
|
204
|
+
);
|
|
205
|
+
function handleDeleteDecorator(event) {
|
|
206
|
+
const selection = $getSelection();
|
|
207
|
+
if (!$isNodeSelection(selection)) return false;
|
|
208
|
+
const nodes = selection.getNodes();
|
|
209
|
+
if (nodes.length !== 1) return false;
|
|
210
|
+
const node = nodes[0];
|
|
211
|
+
if (!$isDecoratorNode(node)) return false;
|
|
212
|
+
event?.preventDefault();
|
|
213
|
+
const prev = node.getPreviousSibling();
|
|
214
|
+
const next = node.getNextSibling();
|
|
215
|
+
node.remove();
|
|
216
|
+
if (prev && $isElementNode(prev)) {
|
|
217
|
+
prev.selectEnd();
|
|
218
|
+
} else if (prev && $isDecoratorNode(prev)) {
|
|
219
|
+
selectDecoratorNode(prev, "end");
|
|
220
|
+
} else if (next && $isElementNode(next)) {
|
|
221
|
+
next.selectStart();
|
|
222
|
+
} else if (next && $isDecoratorNode(next)) {
|
|
223
|
+
selectDecoratorNode(next, "start");
|
|
224
|
+
} else {
|
|
225
|
+
const root = $getRoot();
|
|
226
|
+
if (root && $isElementNode(root)) {
|
|
227
|
+
const p = $createParagraphNode();
|
|
228
|
+
root.append(p);
|
|
229
|
+
p.selectStart();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
const unregisterBackspace = editor.registerCommand(
|
|
235
|
+
KEY_BACKSPACE_COMMAND,
|
|
236
|
+
handleDeleteDecorator,
|
|
237
|
+
COMMAND_PRIORITY_HIGH
|
|
238
|
+
);
|
|
239
|
+
const unregisterDelete = editor.registerCommand(
|
|
240
|
+
KEY_DELETE_COMMAND,
|
|
241
|
+
handleDeleteDecorator,
|
|
242
|
+
COMMAND_PRIORITY_HIGH
|
|
243
|
+
);
|
|
244
|
+
const unregisterArrowUpDecorator = editor.registerCommand(
|
|
245
|
+
KEY_ARROW_UP_COMMAND,
|
|
246
|
+
(event) => {
|
|
247
|
+
const selection = $getSelection();
|
|
248
|
+
if ($isRangeSelection(selection) && selection.isCollapsed()) {
|
|
249
|
+
const anchorNode = selection.anchor.getNode();
|
|
250
|
+
const topLevelElement = anchorNode.getTopLevelElementOrThrow();
|
|
251
|
+
const shouldSelectPreviousDecorator = isAtTopLevelBoundary(selection, "start") || isSingleLineParagraph(topLevelElement);
|
|
252
|
+
if (!$isQuoteNode(topLevelElement) && shouldSelectPreviousDecorator) {
|
|
253
|
+
const prev2 = topLevelElement.getPreviousSibling();
|
|
254
|
+
if (prev2 && $isDecoratorNode(prev2) && prev2.isKeyboardSelectable()) {
|
|
255
|
+
event?.preventDefault();
|
|
256
|
+
selectDecoratorNode(prev2, "end");
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (!$isNodeSelection(selection)) return false;
|
|
262
|
+
const nodes = selection.getNodes();
|
|
263
|
+
if (nodes.length !== 1) return false;
|
|
264
|
+
const node = nodes[0];
|
|
265
|
+
if (!$isDecoratorNode(node)) return false;
|
|
266
|
+
const prev = node.getPreviousSibling();
|
|
267
|
+
if (prev && $isElementNode(prev)) {
|
|
268
|
+
event?.preventDefault();
|
|
269
|
+
prev.selectEnd();
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
if (prev && $isDecoratorNode(prev)) {
|
|
273
|
+
event?.preventDefault();
|
|
274
|
+
selectDecoratorNode(prev, "end");
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
return false;
|
|
278
|
+
},
|
|
279
|
+
COMMAND_PRIORITY_CRITICAL
|
|
280
|
+
);
|
|
281
|
+
const unregisterArrowDownDecorator = editor.registerCommand(
|
|
282
|
+
KEY_ARROW_DOWN_COMMAND,
|
|
283
|
+
(event) => {
|
|
284
|
+
const selection = $getSelection();
|
|
285
|
+
if (!$isNodeSelection(selection)) return false;
|
|
286
|
+
const nodes = selection.getNodes();
|
|
287
|
+
if (nodes.length !== 1) return false;
|
|
288
|
+
const node = nodes[0];
|
|
289
|
+
if (!$isDecoratorNode(node)) return false;
|
|
290
|
+
const next = node.getNextSibling();
|
|
291
|
+
if (next && $isElementNode(next)) {
|
|
292
|
+
event?.preventDefault();
|
|
293
|
+
next.selectStart();
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
if (next && $isDecoratorNode(next)) {
|
|
297
|
+
event?.preventDefault();
|
|
298
|
+
selectDecoratorNode(next, "start");
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
return false;
|
|
302
|
+
},
|
|
303
|
+
COMMAND_PRIORITY_CRITICAL
|
|
304
|
+
);
|
|
305
|
+
return () => {
|
|
306
|
+
unregisterArrowRight();
|
|
307
|
+
unregisterArrowDown();
|
|
308
|
+
unregisterCmdEnter();
|
|
309
|
+
unregisterBackspace();
|
|
310
|
+
unregisterDelete();
|
|
311
|
+
unregisterArrowUpDecorator();
|
|
312
|
+
unregisterArrowDownDecorator();
|
|
313
|
+
};
|
|
314
|
+
}, [editor]);
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
const HORIZONTAL_RULE_REGEX = /^(---|\*\*\*|___)\s?$/;
|
|
318
|
+
const HORIZONTAL_RULE_BLOCK_TRANSFORMER = {
|
|
319
|
+
...HORIZONTAL_RULE_BLOCK_TRANSFORMER$1,
|
|
320
|
+
dependencies: [HorizontalRuleNode],
|
|
321
|
+
regExp: HORIZONTAL_RULE_REGEX,
|
|
322
|
+
replace: (parentNode) => {
|
|
323
|
+
const hrNode = $createHorizontalRuleNode();
|
|
324
|
+
const paragraph = $createParagraphNode();
|
|
325
|
+
parentNode.replace(hrNode);
|
|
326
|
+
hrNode.insertAfter(paragraph);
|
|
327
|
+
paragraph.selectStart();
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
function HorizontalRulePlugin() {
|
|
331
|
+
const [editor] = useLexicalComposerContext();
|
|
332
|
+
useEffect(() => {
|
|
333
|
+
const unregisterInsert = editor.registerCommand(
|
|
334
|
+
INSERT_HORIZONTAL_RULE_COMMAND,
|
|
335
|
+
() => {
|
|
336
|
+
const selection = $getSelection();
|
|
337
|
+
if (!$isRangeSelection(selection)) return false;
|
|
338
|
+
const focusNode = selection.focus.getNode();
|
|
339
|
+
const topLevel = focusNode.getTopLevelElement();
|
|
340
|
+
if (!topLevel) return false;
|
|
341
|
+
const hrNode = $createHorizontalRuleNode();
|
|
342
|
+
const paragraph = $createParagraphNode();
|
|
343
|
+
topLevel.insertAfter(hrNode);
|
|
344
|
+
hrNode.insertAfter(paragraph);
|
|
345
|
+
paragraph.selectStart();
|
|
346
|
+
return true;
|
|
347
|
+
},
|
|
348
|
+
COMMAND_PRIORITY_EDITOR
|
|
349
|
+
);
|
|
350
|
+
const unregisterEnter = editor.registerCommand(
|
|
351
|
+
KEY_ENTER_COMMAND,
|
|
352
|
+
(event) => {
|
|
353
|
+
const selection = $getSelection();
|
|
354
|
+
if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
const anchorNode = selection.anchor.getNode();
|
|
358
|
+
if (!$isTextNode(anchorNode)) return false;
|
|
359
|
+
const textContent = anchorNode.getTextContent();
|
|
360
|
+
if (!HORIZONTAL_RULE_REGEX.test(textContent)) return false;
|
|
361
|
+
const parentNode = anchorNode.getParent();
|
|
362
|
+
if (!parentNode) return false;
|
|
363
|
+
event?.preventDefault();
|
|
364
|
+
const hrNode = $createHorizontalRuleNode();
|
|
365
|
+
const paragraph = $createParagraphNode();
|
|
366
|
+
parentNode.replace(hrNode);
|
|
367
|
+
hrNode.insertAfter(paragraph);
|
|
368
|
+
paragraph.selectStart();
|
|
369
|
+
return true;
|
|
370
|
+
},
|
|
371
|
+
COMMAND_PRIORITY_LOW
|
|
372
|
+
);
|
|
373
|
+
return () => {
|
|
374
|
+
unregisterInsert();
|
|
375
|
+
unregisterEnter();
|
|
376
|
+
};
|
|
377
|
+
}, [editor]);
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
380
|
+
const ALERT_TYPE_MAP = {
|
|
381
|
+
NOTE: "note",
|
|
382
|
+
TIP: "tip",
|
|
383
|
+
IMPORTANT: "important",
|
|
384
|
+
WARNING: "warning",
|
|
385
|
+
CAUTION: "caution"
|
|
386
|
+
};
|
|
387
|
+
const GIT_ALERT_TRANSFORMER = {
|
|
388
|
+
...GIT_ALERT_TRANSFORMER$1,
|
|
389
|
+
dependencies: [AlertQuoteNode],
|
|
390
|
+
regExp: /^>\s*\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*$/,
|
|
391
|
+
replace: (parentNode, children, match) => {
|
|
392
|
+
const typeKey = match[1];
|
|
393
|
+
const alertType = ALERT_TYPE_MAP[typeKey] || "note";
|
|
394
|
+
const serializedChildren = children.map((child) => child.exportJSON());
|
|
395
|
+
const content = {
|
|
396
|
+
root: {
|
|
397
|
+
children: [
|
|
398
|
+
{
|
|
399
|
+
type: "paragraph",
|
|
400
|
+
children: serializedChildren,
|
|
401
|
+
direction: null,
|
|
402
|
+
format: "",
|
|
403
|
+
indent: 0,
|
|
404
|
+
textFormat: 0,
|
|
405
|
+
textStyle: "",
|
|
406
|
+
version: 1
|
|
407
|
+
}
|
|
408
|
+
],
|
|
409
|
+
direction: null,
|
|
410
|
+
format: "",
|
|
411
|
+
indent: 0,
|
|
412
|
+
type: "root",
|
|
413
|
+
version: 1
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
const alertNode = $createAlertQuoteEditNode(alertType, content);
|
|
417
|
+
parentNode.replace(alertNode);
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
function findCodeBlockKlass(nodes) {
|
|
421
|
+
return nodes.find((n) => n.getType?.() === "code-block") || CodeBlockNode;
|
|
422
|
+
}
|
|
423
|
+
const CODE_BLOCK_MULTILINE_TRANSFORMER = {
|
|
424
|
+
dependencies: [],
|
|
425
|
+
regExpEnd: {
|
|
426
|
+
optional: true,
|
|
427
|
+
regExp: /[\t ]*```$/
|
|
428
|
+
},
|
|
429
|
+
regExpStart: /^[\t ]*```([\w-]+)?/,
|
|
430
|
+
replace: (rootNode, _children, startMatch, _endMatch, linesInBetween, isImport) => {
|
|
431
|
+
const lang = startMatch[1] || "";
|
|
432
|
+
let code = "";
|
|
433
|
+
if (linesInBetween) {
|
|
434
|
+
const lines = [...linesInBetween];
|
|
435
|
+
while (lines.length > 0 && !lines[0].length) lines.shift();
|
|
436
|
+
while (lines.length > 0 && !lines.at(-1)?.length) lines.pop();
|
|
437
|
+
code = lines.join("\n");
|
|
438
|
+
}
|
|
439
|
+
const Klass = findCodeBlockKlass(getResolvedEditNodes());
|
|
440
|
+
const node = new Klass(code, lang);
|
|
441
|
+
if (isImport || $isRootNode(rootNode)) {
|
|
442
|
+
rootNode.append(node);
|
|
443
|
+
} else {
|
|
444
|
+
rootNode.replace(node);
|
|
445
|
+
}
|
|
446
|
+
const selection = $createNodeSelection();
|
|
447
|
+
selection.add(node.getKey());
|
|
448
|
+
$setSelection(selection);
|
|
449
|
+
},
|
|
450
|
+
type: "multiline-element"
|
|
451
|
+
};
|
|
452
|
+
const NEVER = /a^/;
|
|
453
|
+
const COMMENT_OPEN_TRANSFORMER = {
|
|
454
|
+
dependencies: [CommentNode],
|
|
455
|
+
export: () => null,
|
|
456
|
+
importRegExp: NEVER,
|
|
457
|
+
regExp: /<!--$/,
|
|
458
|
+
replace: (textNode) => {
|
|
459
|
+
const node = $createCommentPlaceholderNode();
|
|
460
|
+
textNode.replace(node);
|
|
461
|
+
node.select(0, node.getTextContentSize());
|
|
462
|
+
},
|
|
463
|
+
trigger: "-",
|
|
464
|
+
type: "text-match"
|
|
465
|
+
};
|
|
466
|
+
const COMMENT_TRANSFORMER = {
|
|
467
|
+
...COMMENT_TRANSFORMER$1,
|
|
468
|
+
dependencies: [CommentNode],
|
|
469
|
+
replace: (textNode, match) => {
|
|
470
|
+
textNode.replace($createCommentNode(match[1] ?? ""));
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
const BANNER_TYPE_MAP = {
|
|
474
|
+
note: "note",
|
|
475
|
+
info: "note",
|
|
476
|
+
tip: "tip",
|
|
477
|
+
success: "tip",
|
|
478
|
+
important: "important",
|
|
479
|
+
warning: "warning",
|
|
480
|
+
warn: "warning",
|
|
481
|
+
error: "caution",
|
|
482
|
+
danger: "caution",
|
|
483
|
+
caution: "caution"
|
|
484
|
+
};
|
|
485
|
+
const CONTAINER_TRANSFORMER = {
|
|
486
|
+
...CONTAINER_TRANSFORMER$1,
|
|
487
|
+
dependencies: [BannerNode, DetailsNode],
|
|
488
|
+
regExp: /^:::\s*(\w+)(?:\{([^}]*)\})?\s*$/,
|
|
489
|
+
replace: (parentNode, children, match) => {
|
|
490
|
+
const type = match[1];
|
|
491
|
+
const params = match[2];
|
|
492
|
+
if (type in BANNER_TYPE_MAP) {
|
|
493
|
+
const bannerType = BANNER_TYPE_MAP[type];
|
|
494
|
+
const serializedChildren = children.map(
|
|
495
|
+
(child) => child.exportJSON()
|
|
496
|
+
);
|
|
497
|
+
const content = {
|
|
498
|
+
root: {
|
|
499
|
+
children: [
|
|
500
|
+
{
|
|
501
|
+
type: "paragraph",
|
|
502
|
+
children: serializedChildren,
|
|
503
|
+
direction: null,
|
|
504
|
+
format: "",
|
|
505
|
+
indent: 0,
|
|
506
|
+
textFormat: 0,
|
|
507
|
+
textStyle: "",
|
|
508
|
+
version: 1
|
|
509
|
+
}
|
|
510
|
+
],
|
|
511
|
+
direction: null,
|
|
512
|
+
format: "",
|
|
513
|
+
indent: 0,
|
|
514
|
+
type: "root",
|
|
515
|
+
version: 1
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
const banner = $createBannerEditNode(bannerType, content);
|
|
519
|
+
parentNode.replace(banner);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if (type === "details") {
|
|
523
|
+
const summaryMatch = params?.match(/summary="([^"]*)"/);
|
|
524
|
+
const summary = summaryMatch ? summaryMatch[1] : "Details";
|
|
525
|
+
const details = $createDetailsNode(summary);
|
|
526
|
+
children.forEach((child) => {
|
|
527
|
+
details.append(child);
|
|
528
|
+
});
|
|
529
|
+
parentNode.replace(details);
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
const paragraph = $createParagraphNode();
|
|
533
|
+
paragraph.append($createTextNode(`::: ${type}`));
|
|
534
|
+
children.forEach((child) => {
|
|
535
|
+
paragraph.append(child);
|
|
536
|
+
});
|
|
537
|
+
parentNode.replace(paragraph);
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
const FOOTNOTE_TRANSFORMER = {
|
|
541
|
+
...FOOTNOTE_TRANSFORMER$1,
|
|
542
|
+
dependencies: [FootnoteNode],
|
|
543
|
+
replace: (textNode, match) => {
|
|
544
|
+
const footnoteNode = $createFootnoteNode(match[1]);
|
|
545
|
+
textNode.replace(footnoteNode);
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
const FOOTNOTE_SECTION_TRANSFORMER = {
|
|
549
|
+
...FOOTNOTE_SECTION_TRANSFORMER$1,
|
|
550
|
+
dependencies: [FootnoteSectionNode],
|
|
551
|
+
regExp: /^\[\^(\w+)\]:[\t ]+(\S.*)$/,
|
|
552
|
+
replace: (parentNode, _children, match) => {
|
|
553
|
+
const identifier = match[1];
|
|
554
|
+
const content = match[2];
|
|
555
|
+
const root = parentNode.getParent();
|
|
556
|
+
if (root) {
|
|
557
|
+
const children = root.getChildren();
|
|
558
|
+
for (const child of children) {
|
|
559
|
+
if ($isFootnoteSectionNode(child)) {
|
|
560
|
+
child.setDefinition(identifier, content);
|
|
561
|
+
parentNode.remove();
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
const sectionNode = $createFootnoteSectionNode({ [identifier]: content });
|
|
567
|
+
parentNode.replace(sectionNode);
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
const KATEX_INLINE_TRANSFORMER = {
|
|
571
|
+
...KATEX_INLINE_TRANSFORMER$1,
|
|
572
|
+
dependencies: [KaTeXInlineNode],
|
|
573
|
+
replace: (textNode, match) => {
|
|
574
|
+
const katexNode = $createKaTeXInlineNode(match[1]);
|
|
575
|
+
textNode.replace(katexNode);
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
const KATEX_BLOCK_TRANSFORMER = {
|
|
579
|
+
...KATEX_BLOCK_TRANSFORMER$1,
|
|
580
|
+
dependencies: [KaTeXBlockNode],
|
|
581
|
+
replace: (textNode, match) => {
|
|
582
|
+
const katexNode = $createKaTeXBlockNode(match[1]);
|
|
583
|
+
textNode.replace(katexNode);
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
const MENTION_TRANSFORMER = {
|
|
587
|
+
...MENTION_TRANSFORMER$1,
|
|
588
|
+
dependencies: [MentionNode],
|
|
589
|
+
replace: (textNode, match) => {
|
|
590
|
+
const displayName = match[1] || void 0;
|
|
591
|
+
const mentionNode = $createMentionNode(match[2], match[3], displayName);
|
|
592
|
+
textNode.replace(mentionNode);
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
const QUOTE_TRANSFORMER = {
|
|
596
|
+
dependencies: [QuoteNode],
|
|
597
|
+
export: (node, exportChildren) => {
|
|
598
|
+
if (!$isQuoteNode(node)) {
|
|
599
|
+
return null;
|
|
600
|
+
}
|
|
601
|
+
const lines = exportChildren(node).split("\n");
|
|
602
|
+
return lines.map((line) => `> ${line}`).join("\n");
|
|
603
|
+
},
|
|
604
|
+
regExp: /^>\s/,
|
|
605
|
+
replace: (parentNode, children, _match, isImport) => {
|
|
606
|
+
if (isImport) {
|
|
607
|
+
const previousNode = parentNode.getPreviousSibling();
|
|
608
|
+
if ($isQuoteNode(previousNode)) {
|
|
609
|
+
previousNode.splice(previousNode.getChildrenSize(), 0, [
|
|
610
|
+
$createLineBreakNode(),
|
|
611
|
+
...children
|
|
612
|
+
]);
|
|
613
|
+
parentNode.remove();
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
const node = $createQuoteNode();
|
|
618
|
+
const paragraph = $createParagraphNode();
|
|
619
|
+
paragraph.append(...children);
|
|
620
|
+
node.append(paragraph);
|
|
621
|
+
parentNode.replace(node);
|
|
622
|
+
if (!isImport) {
|
|
623
|
+
paragraph.select(0, 0);
|
|
624
|
+
}
|
|
625
|
+
},
|
|
626
|
+
type: "element"
|
|
627
|
+
};
|
|
628
|
+
function quoteAttr(value) {
|
|
629
|
+
return value.replaceAll('"', '\\"');
|
|
630
|
+
}
|
|
631
|
+
const GRID_CONTAINER_BLOCK_TRANSFORMER = {
|
|
632
|
+
dependencies: [],
|
|
633
|
+
export: (node) => {
|
|
634
|
+
if (!$isGridContainerNode(node)) return null;
|
|
635
|
+
const cells = node.getCellStates().map((state) => extractTextContent(state));
|
|
636
|
+
const body = cells.map((content) => `::: cell
|
|
637
|
+
${content}
|
|
638
|
+
:::`).join("\n");
|
|
639
|
+
return `::: grid{cols=${node.getCols()} gap="${quoteAttr(node.getGap())}"}
|
|
640
|
+
${body}
|
|
641
|
+
:::`;
|
|
642
|
+
},
|
|
643
|
+
regExp: /\b\B/,
|
|
644
|
+
replace: () => {
|
|
645
|
+
},
|
|
646
|
+
type: "element"
|
|
647
|
+
};
|
|
648
|
+
const RUBY_TRANSFORMER = {
|
|
649
|
+
...RUBY_TRANSFORMER$1,
|
|
650
|
+
dependencies: [RubyNode],
|
|
651
|
+
replace: (textNode, match) => {
|
|
652
|
+
const rubyNode = $createRubyNode(match[2] ?? "");
|
|
653
|
+
const baseText = match[1] ?? "";
|
|
654
|
+
if (baseText) {
|
|
655
|
+
rubyNode.append($createTextNode(baseText));
|
|
656
|
+
}
|
|
657
|
+
textNode.replace(rubyNode);
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
const SPOILER_TRANSFORMER = {
|
|
661
|
+
...SPOILER_TRANSFORMER$1,
|
|
662
|
+
dependencies: [SpoilerNode],
|
|
663
|
+
replace: (textNode, match) => {
|
|
664
|
+
const spoilerNode = $createSpoilerNode();
|
|
665
|
+
spoilerNode.append($createTextNode(match[1]));
|
|
666
|
+
textNode.replace(spoilerNode);
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
const TABLE_ROW_REG_EXP = /^\|(.+)\|\s*$/;
|
|
670
|
+
const TABLE_DIVIDER_REG_EXP = /^\|(?:\s*:?-+:?\s*\|)+\s*$/;
|
|
671
|
+
function parseCells(row) {
|
|
672
|
+
const match = row.match(TABLE_ROW_REG_EXP);
|
|
673
|
+
if (!match) return [];
|
|
674
|
+
return match[1].split("|").map((c) => c.trim());
|
|
675
|
+
}
|
|
676
|
+
const TABLE_IMPORT_TRANSFORMER = {
|
|
677
|
+
dependencies: [],
|
|
678
|
+
export: () => null,
|
|
679
|
+
handleImportAfterStartMatch({ lines, rootNode, startLineIndex }) {
|
|
680
|
+
if (startLineIndex + 1 >= lines.length) return null;
|
|
681
|
+
const dividerLine = lines[startLineIndex + 1];
|
|
682
|
+
if (!TABLE_DIVIDER_REG_EXP.test(dividerLine)) return null;
|
|
683
|
+
const headerCells = parseCells(lines[startLineIndex]);
|
|
684
|
+
if (headerCells.length === 0) return null;
|
|
685
|
+
let endLineIndex = startLineIndex + 1;
|
|
686
|
+
const dataRows = [];
|
|
687
|
+
for (let i = startLineIndex + 2; i < lines.length; i++) {
|
|
688
|
+
if (!TABLE_ROW_REG_EXP.test(lines[i])) break;
|
|
689
|
+
dataRows.push(parseCells(lines[i]));
|
|
690
|
+
endLineIndex = i;
|
|
691
|
+
}
|
|
692
|
+
const tableNode = $createTableNode();
|
|
693
|
+
const headerRow = $createTableRowNode();
|
|
694
|
+
for (const cell of headerCells) {
|
|
695
|
+
const cellNode = $createTableCellNode(TableCellHeaderStates.ROW);
|
|
696
|
+
const p = $createParagraphNode();
|
|
697
|
+
p.append($createTextNode(cell));
|
|
698
|
+
cellNode.append(p);
|
|
699
|
+
headerRow.append(cellNode);
|
|
700
|
+
}
|
|
701
|
+
tableNode.append(headerRow);
|
|
702
|
+
for (const row of dataRows) {
|
|
703
|
+
const rowNode = $createTableRowNode();
|
|
704
|
+
for (let i = 0; i < headerCells.length; i++) {
|
|
705
|
+
const cellNode = $createTableCellNode(TableCellHeaderStates.NO_STATUS);
|
|
706
|
+
const p = $createParagraphNode();
|
|
707
|
+
p.append($createTextNode(row[i] ?? ""));
|
|
708
|
+
cellNode.append(p);
|
|
709
|
+
rowNode.append(cellNode);
|
|
710
|
+
}
|
|
711
|
+
tableNode.append(rowNode);
|
|
712
|
+
}
|
|
713
|
+
rootNode.append(tableNode);
|
|
714
|
+
return [true, endLineIndex];
|
|
715
|
+
},
|
|
716
|
+
regExpEnd: {
|
|
717
|
+
optional: true,
|
|
718
|
+
regExp: TABLE_ROW_REG_EXP
|
|
719
|
+
},
|
|
720
|
+
regExpStart: TABLE_ROW_REG_EXP,
|
|
721
|
+
replace: () => {
|
|
722
|
+
},
|
|
723
|
+
type: "multiline-element"
|
|
724
|
+
};
|
|
725
|
+
const ALL_TRANSFORMERS = [
|
|
726
|
+
// Inline transformers
|
|
727
|
+
SPOILER_TRANSFORMER,
|
|
728
|
+
MENTION_TRANSFORMER,
|
|
729
|
+
FOOTNOTE_TRANSFORMER,
|
|
730
|
+
INSERT_TRANSFORMER,
|
|
731
|
+
SUPERSCRIPT_TRANSFORMER,
|
|
732
|
+
SUBSCRIPT_TRANSFORMER,
|
|
733
|
+
RUBY_TRANSFORMER,
|
|
734
|
+
COMMENT_OPEN_TRANSFORMER,
|
|
735
|
+
COMMENT_TRANSFORMER,
|
|
736
|
+
KATEX_INLINE_TRANSFORMER,
|
|
737
|
+
// Block transformers (order matters - more specific first)
|
|
738
|
+
FOOTNOTE_SECTION_TRANSFORMER,
|
|
739
|
+
CONTAINER_TRANSFORMER,
|
|
740
|
+
GIT_ALERT_TRANSFORMER,
|
|
741
|
+
CHECK_LIST,
|
|
742
|
+
KATEX_BLOCK_TRANSFORMER,
|
|
743
|
+
IMAGE_BLOCK_TRANSFORMER,
|
|
744
|
+
VIDEO_BLOCK_TRANSFORMER,
|
|
745
|
+
CODE_BLOCK_NODE_TRANSFORMER,
|
|
746
|
+
CODE_BLOCK_MULTILINE_TRANSFORMER,
|
|
747
|
+
LINK_CARD_BLOCK_TRANSFORMER,
|
|
748
|
+
MERMAID_BLOCK_TRANSFORMER,
|
|
749
|
+
GRID_CONTAINER_BLOCK_TRANSFORMER,
|
|
750
|
+
HORIZONTAL_RULE_BLOCK_TRANSFORMER,
|
|
751
|
+
TABLE_IMPORT_TRANSFORMER,
|
|
752
|
+
TABLE_BLOCK_TRANSFORMER,
|
|
753
|
+
QUOTE_TRANSFORMER,
|
|
754
|
+
...TRANSFORMERS.filter((t) => t !== QUOTE && t !== CODE)
|
|
755
|
+
];
|
|
756
|
+
function getVSCodePasteData(clipboardData) {
|
|
757
|
+
const raw = clipboardData.getData("vscode-editor-data");
|
|
758
|
+
if (!raw) return null;
|
|
759
|
+
try {
|
|
760
|
+
const data = JSON.parse(raw);
|
|
761
|
+
return { language: data.mode || "text" };
|
|
762
|
+
} catch {
|
|
763
|
+
return null;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
function hasRichHTML(clipboardData) {
|
|
767
|
+
const html = clipboardData.getData("text/html");
|
|
768
|
+
if (!html) return false;
|
|
769
|
+
if (/data-vscode|vscode-/i.test(html)) return false;
|
|
770
|
+
const doc = new DOMParser().parseFromString(html, "text/html");
|
|
771
|
+
const richTags = doc.body.querySelectorAll(
|
|
772
|
+
"strong,em,b,i,h1,h2,h3,h4,h5,h6,ul,ol,table,img,blockquote,pre>code,a[href]"
|
|
773
|
+
);
|
|
774
|
+
return richTags.length > 0;
|
|
775
|
+
}
|
|
776
|
+
function detectMarkdown(text) {
|
|
777
|
+
let score = 0;
|
|
778
|
+
if (/^#{1,6}\s+\S/m.test(text)) score += 5;
|
|
779
|
+
if (/^```[\w-]*$/m.test(text)) score += 5;
|
|
780
|
+
if (/\[[^\]]+\]\([^)]+\)/.test(text)) score += 4;
|
|
781
|
+
if (/!\[[^\]]*\]\([^)]+\)/.test(text)) score += 5;
|
|
782
|
+
if (/^\|.+\|$/m.test(text) && /^\|[\s:|-]+\|$/m.test(text)) score += 5;
|
|
783
|
+
if (/^>\s*\[!(?:note|tip|warning|caution|important)\]/im.test(text)) score += 5;
|
|
784
|
+
if (/^[*-]\s+\[[ x]\]/m.test(text)) score += 4;
|
|
785
|
+
if (/\*\*.+?\*\*/.test(text)) score += 2;
|
|
786
|
+
if (/(?<!\*)\*(?!\*)(?!\s).+?(?<!\s)(?<!\*)\*(?!\*)/.test(text)) score += 1;
|
|
787
|
+
if (/^[*+-]\s+\S/m.test(text)) score += 1;
|
|
788
|
+
if (/^\d+\.\s+\S/m.test(text)) score += 1;
|
|
789
|
+
if (/^>\s+\S/m.test(text)) score += 1;
|
|
790
|
+
if (/`.+?`/.test(text)) score += 1;
|
|
791
|
+
if (/^[*_-]{3,}$/m.test(text)) score += 2;
|
|
792
|
+
const paragraphs = text.split(/\n{2,}/).filter(Boolean);
|
|
793
|
+
if (paragraphs.length >= 2) score += 5;
|
|
794
|
+
if (text.length < 20) score -= 3;
|
|
795
|
+
if (!text.includes("\n")) score -= 2;
|
|
796
|
+
return score >= 5;
|
|
797
|
+
}
|
|
798
|
+
function convertAndInsert(markdown) {
|
|
799
|
+
let conversionError = null;
|
|
800
|
+
const tempEditor = createEditor({
|
|
801
|
+
namespace: "markdown-paste-temp",
|
|
802
|
+
nodes: getResolvedEditNodes(),
|
|
803
|
+
onError: (error) => {
|
|
804
|
+
conversionError = error;
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
tempEditor.update(
|
|
808
|
+
() => {
|
|
809
|
+
$convertFromMarkdownString(markdown, ALL_TRANSFORMERS);
|
|
810
|
+
},
|
|
811
|
+
{ discrete: true }
|
|
812
|
+
);
|
|
813
|
+
if (conversionError) {
|
|
814
|
+
console.error("MarkdownPastePlugin: convertAndInsert error", conversionError);
|
|
815
|
+
return false;
|
|
816
|
+
}
|
|
817
|
+
const serializedChildren = tempEditor.getEditorState().toJSON().root.children;
|
|
818
|
+
if (!serializedChildren.length) return false;
|
|
819
|
+
const nodes = serializedChildren.map((s) => $parseSerializedNode(s));
|
|
820
|
+
$insertNodes(nodes);
|
|
821
|
+
return true;
|
|
822
|
+
}
|
|
823
|
+
function MarkdownPastePlugin() {
|
|
824
|
+
const [editor] = useLexicalComposerContext();
|
|
825
|
+
useEffect(() => {
|
|
826
|
+
return editor.registerCommand(
|
|
827
|
+
PASTE_COMMAND,
|
|
828
|
+
(event) => {
|
|
829
|
+
const clipboardData = "clipboardData" in event ? event.clipboardData : null;
|
|
830
|
+
if (!clipboardData) return false;
|
|
831
|
+
if (Array.from(clipboardData.files).some((f) => f.type.startsWith("image/"))) return false;
|
|
832
|
+
const vscodeData = getVSCodePasteData(clipboardData);
|
|
833
|
+
if (vscodeData) {
|
|
834
|
+
const code = clipboardData.getData("text/plain");
|
|
835
|
+
if (code) {
|
|
836
|
+
const segments = code.split(/\r?\n{3,}/).filter(Boolean);
|
|
837
|
+
const nodes = segments.map(
|
|
838
|
+
(s) => $createCodeBlockEditNode(
|
|
839
|
+
s.replace(/^\s*\n/, "").replace(/\n\s*$/, ""),
|
|
840
|
+
vscodeData.language
|
|
841
|
+
)
|
|
842
|
+
);
|
|
843
|
+
$insertNodes(nodes);
|
|
844
|
+
event.preventDefault();
|
|
845
|
+
return true;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
if (hasRichHTML(clipboardData)) return false;
|
|
849
|
+
const text = clipboardData.getData("text/plain");
|
|
850
|
+
if (!text || !detectMarkdown(text)) return false;
|
|
851
|
+
try {
|
|
852
|
+
if (!convertAndInsert(text)) {
|
|
853
|
+
return false;
|
|
854
|
+
}
|
|
855
|
+
event.preventDefault();
|
|
856
|
+
return true;
|
|
857
|
+
} catch (error) {
|
|
858
|
+
console.error("MarkdownPastePlugin: paste error", error);
|
|
859
|
+
return false;
|
|
860
|
+
}
|
|
861
|
+
},
|
|
862
|
+
COMMAND_PRIORITY_HIGH
|
|
863
|
+
);
|
|
864
|
+
}, [editor]);
|
|
865
|
+
return null;
|
|
866
|
+
}
|
|
867
|
+
function MarkdownShortcutsPlugin() {
|
|
868
|
+
return /* @__PURE__ */ jsx(MarkdownShortcutPlugin, { transformers: ALL_TRANSFORMERS });
|
|
869
|
+
}
|
|
870
|
+
function CorePlugins() {
|
|
871
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
872
|
+
/* @__PURE__ */ jsx(ListPlugin, {}),
|
|
873
|
+
/* @__PURE__ */ jsx(LinkPlugin, {}),
|
|
874
|
+
/* @__PURE__ */ jsx(TabIndentationPlugin, {}),
|
|
875
|
+
/* @__PURE__ */ jsx(TablePlugin, {}),
|
|
876
|
+
/* @__PURE__ */ jsx(CheckListPlugin, {}),
|
|
877
|
+
/* @__PURE__ */ jsx(MarkdownShortcutsPlugin, {}),
|
|
878
|
+
/* @__PURE__ */ jsx(MarkdownPastePlugin, {}),
|
|
879
|
+
/* @__PURE__ */ jsx(BlockExitPlugin, {}),
|
|
880
|
+
/* @__PURE__ */ jsx(HorizontalRulePlugin, {}),
|
|
881
|
+
/* @__PURE__ */ jsx(AutoLinkPlugin, {})
|
|
882
|
+
] });
|
|
883
|
+
}
|
|
884
|
+
const ImageUploadContext = createContext(null);
|
|
885
|
+
function ImageUploadProvider({
|
|
886
|
+
upload,
|
|
887
|
+
children
|
|
888
|
+
}) {
|
|
889
|
+
return /* @__PURE__ */ jsx(ImageUploadContext.Provider, { value: upload, children });
|
|
890
|
+
}
|
|
891
|
+
function useImageUpload() {
|
|
892
|
+
return use(ImageUploadContext);
|
|
893
|
+
}
|
|
894
|
+
const blockIdState = createState("blockId", {
|
|
895
|
+
parse: (v) => typeof v === "string" ? v : ""
|
|
896
|
+
});
|
|
897
|
+
const NORMALIZATION_TAG = "block-id-normalization";
|
|
898
|
+
function buildPreviousIdIndex(editorState) {
|
|
899
|
+
return editorState.read(() => {
|
|
900
|
+
const map = /* @__PURE__ */ new Map();
|
|
901
|
+
for (const child of $getRoot().getChildren()) {
|
|
902
|
+
const id = $getState(child, blockIdState);
|
|
903
|
+
if (!id) continue;
|
|
904
|
+
const set = map.get(id) ?? /* @__PURE__ */ new Set();
|
|
905
|
+
set.add(child.getKey());
|
|
906
|
+
map.set(id, set);
|
|
907
|
+
}
|
|
908
|
+
return map;
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
function collectRootChildren(editorState) {
|
|
912
|
+
return editorState.read(
|
|
913
|
+
() => $getRoot().getChildren().map((child) => $getState(child, blockIdState))
|
|
914
|
+
);
|
|
915
|
+
}
|
|
916
|
+
function hasDuplicateOrMissingId(children) {
|
|
917
|
+
const seen = /* @__PURE__ */ new Set();
|
|
918
|
+
for (const id of children) {
|
|
919
|
+
if (!id || seen.has(id)) {
|
|
920
|
+
return true;
|
|
921
|
+
}
|
|
922
|
+
seen.add(id);
|
|
923
|
+
}
|
|
924
|
+
return false;
|
|
925
|
+
}
|
|
926
|
+
function generateBlockId(used) {
|
|
927
|
+
while (true) {
|
|
928
|
+
const id = nanoid(8);
|
|
929
|
+
if (!used.has(id)) {
|
|
930
|
+
return id;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
function pickKeeperKey(nodes, previousKeys) {
|
|
935
|
+
if (!nodes.length) return null;
|
|
936
|
+
if (previousKeys?.size) {
|
|
937
|
+
for (const node of nodes) {
|
|
938
|
+
if (previousKeys.has(node.getKey())) {
|
|
939
|
+
return node.getKey();
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
return nodes[0]?.getKey() ?? null;
|
|
944
|
+
}
|
|
945
|
+
function normalizeRootBlockIds(editor, previousIdIndex) {
|
|
946
|
+
editor.update(
|
|
947
|
+
() => {
|
|
948
|
+
$addUpdateTag("history-merge");
|
|
949
|
+
const children = $getRoot().getChildren();
|
|
950
|
+
const groupedById = /* @__PURE__ */ new Map();
|
|
951
|
+
for (const child of children) {
|
|
952
|
+
const id = $getState(child, blockIdState);
|
|
953
|
+
if (!id) continue;
|
|
954
|
+
const bucket = groupedById.get(id) ?? [];
|
|
955
|
+
bucket.push(child);
|
|
956
|
+
groupedById.set(id, bucket);
|
|
957
|
+
}
|
|
958
|
+
const keeperById = /* @__PURE__ */ new Map();
|
|
959
|
+
for (const [id, nodes] of groupedById) {
|
|
960
|
+
if (nodes.length <= 1) continue;
|
|
961
|
+
const keeperKey = pickKeeperKey(nodes, previousIdIndex.get(id));
|
|
962
|
+
if (keeperKey) {
|
|
963
|
+
keeperById.set(id, keeperKey);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
const used = /* @__PURE__ */ new Set();
|
|
967
|
+
for (const child of children) {
|
|
968
|
+
let id = $getState(child, blockIdState);
|
|
969
|
+
const keeperKey = id ? keeperById.get(id) : null;
|
|
970
|
+
const shouldRegenerate = !id || used.has(id) || keeperKey !== void 0 && child.getKey() !== keeperKey;
|
|
971
|
+
if (shouldRegenerate) {
|
|
972
|
+
id = generateBlockId(used);
|
|
973
|
+
$setState(child, blockIdState, id);
|
|
974
|
+
}
|
|
975
|
+
used.add(id);
|
|
976
|
+
}
|
|
977
|
+
},
|
|
978
|
+
{ tag: NORMALIZATION_TAG }
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
function BlockIdPlugin() {
|
|
982
|
+
const [editor] = useLexicalComposerContext();
|
|
983
|
+
useEffect(() => {
|
|
984
|
+
const initialChildren = collectRootChildren(editor.getEditorState());
|
|
985
|
+
if (hasDuplicateOrMissingId(initialChildren)) {
|
|
986
|
+
normalizeRootBlockIds(editor, /* @__PURE__ */ new Map());
|
|
987
|
+
}
|
|
988
|
+
return editor.registerUpdateListener(({ tags, editorState, prevEditorState }) => {
|
|
989
|
+
if (tags.has(NORMALIZATION_TAG)) return;
|
|
990
|
+
const children = collectRootChildren(editorState);
|
|
991
|
+
if (!hasDuplicateOrMissingId(children)) return;
|
|
992
|
+
const previousIdIndex = buildPreviousIdIndex(prevEditorState);
|
|
993
|
+
normalizeRootBlockIds(editor, previousIdIndex);
|
|
994
|
+
});
|
|
995
|
+
}, [editor]);
|
|
996
|
+
return null;
|
|
997
|
+
}
|
|
998
|
+
var draggingWrapperClass = "rich-image-upload-dragging";
|
|
999
|
+
var toastStack = "_1x58dbf1";
|
|
1000
|
+
var toast = "_1x58dbf2";
|
|
1001
|
+
var toastVariant = { info: "_1x58dbf3", success: "_1x58dbf4", error: "_1x58dbf5" };
|
|
1002
|
+
var spinner = "_1x58dbf7";
|
|
1003
|
+
var dialogPopup = "_1x58dbf8";
|
|
1004
|
+
var dialogHeader = "_1x58dbf9";
|
|
1005
|
+
var dialogTitle = "_1x58dbfa";
|
|
1006
|
+
var dialogBody = "_1x58dbfb";
|
|
1007
|
+
var tabWrap = "_1x58dbfc";
|
|
1008
|
+
var uploadDropzone = "_1x58dbfd";
|
|
1009
|
+
var uploadDropzoneState = { idle: "_1x58dbfe", active: "_1x58dbff", busy: "_1x58dbfg" };
|
|
1010
|
+
var uploadDropIcon = "_1x58dbfh";
|
|
1011
|
+
var uploadDropTitle = "_1x58dbfi";
|
|
1012
|
+
var uploadDropDesc = "_1x58dbfj";
|
|
1013
|
+
var hiddenInput = "_1x58dbfk";
|
|
1014
|
+
var uploadBusyWrap = "_1x58dbfl";
|
|
1015
|
+
var uploadProgress = "_1x58dbfm";
|
|
1016
|
+
var urlInputRow = "_1x58dbfn";
|
|
1017
|
+
var textInput = "_1x58dbfo";
|
|
1018
|
+
var urlPreview = "_1x58dbfp";
|
|
1019
|
+
var urlPreviewImage = "_1x58dbfq";
|
|
1020
|
+
var helperText = "_1x58dbfr";
|
|
1021
|
+
var dialogFooter = "_1x58dbfs";
|
|
1022
|
+
function isImageFile(file) {
|
|
1023
|
+
return file.type.startsWith("image/");
|
|
1024
|
+
}
|
|
1025
|
+
function hasImageData(dataTransfer) {
|
|
1026
|
+
if (!dataTransfer) return false;
|
|
1027
|
+
if ([...dataTransfer.files].some(isImageFile)) return true;
|
|
1028
|
+
return [...dataTransfer.items].some((item) => item.type.startsWith("image/"));
|
|
1029
|
+
}
|
|
1030
|
+
const UNSAFE_URL_RE = /^(?:javascript\s*:|vbscript\s*:|data\s*:(?!image\/))/i;
|
|
1031
|
+
function isSafeImageUrl(url) {
|
|
1032
|
+
return !UNSAFE_URL_RE.test(url);
|
|
1033
|
+
}
|
|
1034
|
+
function loadImageByUrl(src) {
|
|
1035
|
+
return new Promise((resolve, reject) => {
|
|
1036
|
+
const image = new Image();
|
|
1037
|
+
image.onload = () => {
|
|
1038
|
+
resolve({
|
|
1039
|
+
width: image.naturalWidth || image.width,
|
|
1040
|
+
height: image.naturalHeight || image.height
|
|
1041
|
+
});
|
|
1042
|
+
};
|
|
1043
|
+
image.onerror = () => reject(new Error("Failed to load image"));
|
|
1044
|
+
image.src = src;
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
function readAsDataUrl(file) {
|
|
1048
|
+
return new Promise((resolve, reject) => {
|
|
1049
|
+
const reader = new FileReader();
|
|
1050
|
+
reader.onload = () => resolve(String(reader.result));
|
|
1051
|
+
reader.onerror = () => reject(reader.error ?? new Error("File read failed"));
|
|
1052
|
+
reader.readAsDataURL(file);
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
async function defaultImageUpload(file) {
|
|
1056
|
+
return {
|
|
1057
|
+
src: await readAsDataUrl(file),
|
|
1058
|
+
altText: file.name
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
function ImageUploadPlugin({ onUpload }) {
|
|
1062
|
+
const [editor] = useLexicalComposerContext();
|
|
1063
|
+
const uploadRef = useRef(onUpload);
|
|
1064
|
+
uploadRef.current = onUpload;
|
|
1065
|
+
const fileInputRef = useRef(null);
|
|
1066
|
+
const toastTimerRef = useRef(null);
|
|
1067
|
+
const [dialogOpen, setDialogOpen] = useState(false);
|
|
1068
|
+
const [tab, setTab] = useState("upload");
|
|
1069
|
+
const [rootDragActive, setRootDragActive] = useState(false);
|
|
1070
|
+
const [dialogDragActive, setDialogDragActive] = useState(false);
|
|
1071
|
+
const [pendingUploads, setPendingUploads] = useState(0);
|
|
1072
|
+
const [dialogUploading, setDialogUploading] = useState(false);
|
|
1073
|
+
const [toast$1, setToast] = useState(null);
|
|
1074
|
+
const [urlInput, setUrlInput] = useState("");
|
|
1075
|
+
const [urlPreview$1, setUrlPreview] = useState(null);
|
|
1076
|
+
const [urlMeta, setUrlMeta] = useState(null);
|
|
1077
|
+
const [urlLoading, setUrlLoading] = useState(false);
|
|
1078
|
+
const [urlError, setUrlError] = useState(null);
|
|
1079
|
+
const tabItems = useMemo(
|
|
1080
|
+
() => [
|
|
1081
|
+
{ value: "upload", label: "Upload" },
|
|
1082
|
+
{ value: "url", label: "URL" }
|
|
1083
|
+
],
|
|
1084
|
+
[]
|
|
1085
|
+
);
|
|
1086
|
+
const pushToast = useCallback((kind, message) => {
|
|
1087
|
+
setToast({ kind, message });
|
|
1088
|
+
if (toastTimerRef.current) {
|
|
1089
|
+
window.clearTimeout(toastTimerRef.current);
|
|
1090
|
+
}
|
|
1091
|
+
toastTimerRef.current = window.setTimeout(setToast, 2200, null);
|
|
1092
|
+
}, []);
|
|
1093
|
+
const insertByUpload = useCallback(
|
|
1094
|
+
async (file, options) => {
|
|
1095
|
+
if (!isImageFile(file)) return false;
|
|
1096
|
+
const closeDialog = Boolean(options?.closeDialog);
|
|
1097
|
+
setPendingUploads((value) => value + 1);
|
|
1098
|
+
if (closeDialog) setDialogUploading(true);
|
|
1099
|
+
try {
|
|
1100
|
+
const [result, meta] = await Promise.all([uploadRef.current(file), computeImageMeta(file)]);
|
|
1101
|
+
editor.update(() => {
|
|
1102
|
+
const node = $createImageNode({
|
|
1103
|
+
src: result.src,
|
|
1104
|
+
altText: result.altText ?? file.name,
|
|
1105
|
+
width: result.width ?? meta.width,
|
|
1106
|
+
height: result.height ?? meta.height,
|
|
1107
|
+
thumbhash: result.thumbhash ?? meta.thumbhash
|
|
1108
|
+
});
|
|
1109
|
+
$insertNodes([node]);
|
|
1110
|
+
});
|
|
1111
|
+
if (closeDialog) {
|
|
1112
|
+
setDialogOpen(false);
|
|
1113
|
+
setUrlInput("");
|
|
1114
|
+
setUrlPreview(null);
|
|
1115
|
+
setUrlMeta(null);
|
|
1116
|
+
setUrlError(null);
|
|
1117
|
+
}
|
|
1118
|
+
pushToast("success", "Image uploaded");
|
|
1119
|
+
return true;
|
|
1120
|
+
} catch (err) {
|
|
1121
|
+
console.error("[ImageUploadPlugin]", err);
|
|
1122
|
+
pushToast("error", "Image upload failed");
|
|
1123
|
+
return false;
|
|
1124
|
+
} finally {
|
|
1125
|
+
setPendingUploads((value) => Math.max(value - 1, 0));
|
|
1126
|
+
setDialogUploading(false);
|
|
1127
|
+
}
|
|
1128
|
+
},
|
|
1129
|
+
[editor, pushToast]
|
|
1130
|
+
);
|
|
1131
|
+
const handleFiles = useCallback(
|
|
1132
|
+
(files) => {
|
|
1133
|
+
const images = files.filter(isImageFile);
|
|
1134
|
+
if (images.length === 0) return false;
|
|
1135
|
+
for (const file of images) {
|
|
1136
|
+
void insertByUpload(file);
|
|
1137
|
+
}
|
|
1138
|
+
return true;
|
|
1139
|
+
},
|
|
1140
|
+
[insertByUpload]
|
|
1141
|
+
);
|
|
1142
|
+
useEffect(() => {
|
|
1143
|
+
const unregisterDragDrop = editor.registerCommand(
|
|
1144
|
+
DRAG_DROP_PASTE,
|
|
1145
|
+
(files) => handleFiles(files),
|
|
1146
|
+
COMMAND_PRIORITY_HIGH
|
|
1147
|
+
);
|
|
1148
|
+
const unregisterPaste = editor.registerCommand(
|
|
1149
|
+
PASTE_COMMAND,
|
|
1150
|
+
(event) => {
|
|
1151
|
+
const clipboardData = "clipboardData" in event ? event.clipboardData : null;
|
|
1152
|
+
if (!clipboardData) return false;
|
|
1153
|
+
const files = [...clipboardData.files];
|
|
1154
|
+
if (files.some(isImageFile)) {
|
|
1155
|
+
return handleFiles(files);
|
|
1156
|
+
}
|
|
1157
|
+
return false;
|
|
1158
|
+
},
|
|
1159
|
+
COMMAND_PRIORITY_HIGH
|
|
1160
|
+
);
|
|
1161
|
+
const unregisterOpenDialog = editor.registerCommand(
|
|
1162
|
+
OPEN_IMAGE_UPLOAD_DIALOG_COMMAND,
|
|
1163
|
+
() => {
|
|
1164
|
+
setDialogOpen(true);
|
|
1165
|
+
return true;
|
|
1166
|
+
},
|
|
1167
|
+
COMMAND_PRIORITY_EDITOR
|
|
1168
|
+
);
|
|
1169
|
+
const rootElement = editor.getRootElement();
|
|
1170
|
+
const wrapper = rootElement?.parentElement ?? null;
|
|
1171
|
+
if (!wrapper) {
|
|
1172
|
+
return () => {
|
|
1173
|
+
unregisterDragDrop();
|
|
1174
|
+
unregisterPaste();
|
|
1175
|
+
unregisterOpenDialog();
|
|
1176
|
+
};
|
|
1177
|
+
}
|
|
1178
|
+
let dragCounter = 0;
|
|
1179
|
+
const setWrapperDragging = (next) => {
|
|
1180
|
+
setRootDragActive(next);
|
|
1181
|
+
wrapper.classList.toggle(draggingWrapperClass, next);
|
|
1182
|
+
};
|
|
1183
|
+
const onDragEnter = (event) => {
|
|
1184
|
+
if (!hasImageData(event.dataTransfer)) return;
|
|
1185
|
+
dragCounter += 1;
|
|
1186
|
+
setWrapperDragging(true);
|
|
1187
|
+
};
|
|
1188
|
+
const onDragOver = (event) => {
|
|
1189
|
+
if (!hasImageData(event.dataTransfer)) return;
|
|
1190
|
+
event.preventDefault();
|
|
1191
|
+
};
|
|
1192
|
+
const onDragLeave = () => {
|
|
1193
|
+
dragCounter = Math.max(dragCounter - 1, 0);
|
|
1194
|
+
if (dragCounter === 0) {
|
|
1195
|
+
setWrapperDragging(false);
|
|
1196
|
+
}
|
|
1197
|
+
};
|
|
1198
|
+
const onDrop = () => {
|
|
1199
|
+
dragCounter = 0;
|
|
1200
|
+
setWrapperDragging(false);
|
|
1201
|
+
};
|
|
1202
|
+
rootElement?.addEventListener("dragenter", onDragEnter);
|
|
1203
|
+
rootElement?.addEventListener("dragover", onDragOver);
|
|
1204
|
+
rootElement?.addEventListener("dragleave", onDragLeave);
|
|
1205
|
+
rootElement?.addEventListener("drop", onDrop);
|
|
1206
|
+
return () => {
|
|
1207
|
+
if (toastTimerRef.current) {
|
|
1208
|
+
window.clearTimeout(toastTimerRef.current);
|
|
1209
|
+
}
|
|
1210
|
+
unregisterDragDrop();
|
|
1211
|
+
unregisterPaste();
|
|
1212
|
+
unregisterOpenDialog();
|
|
1213
|
+
setWrapperDragging(false);
|
|
1214
|
+
rootElement?.removeEventListener("dragenter", onDragEnter);
|
|
1215
|
+
rootElement?.removeEventListener("dragover", onDragOver);
|
|
1216
|
+
rootElement?.removeEventListener("dragleave", onDragLeave);
|
|
1217
|
+
rootElement?.removeEventListener("drop", onDrop);
|
|
1218
|
+
};
|
|
1219
|
+
}, [editor, handleFiles]);
|
|
1220
|
+
const resetUrlState = useCallback(() => {
|
|
1221
|
+
setUrlInput("");
|
|
1222
|
+
setUrlPreview(null);
|
|
1223
|
+
setUrlMeta(null);
|
|
1224
|
+
setUrlError(null);
|
|
1225
|
+
setUrlLoading(false);
|
|
1226
|
+
}, []);
|
|
1227
|
+
const handleDialogFile = useCallback(
|
|
1228
|
+
async (file) => {
|
|
1229
|
+
if (!file) return;
|
|
1230
|
+
await insertByUpload(file, { closeDialog: true });
|
|
1231
|
+
},
|
|
1232
|
+
[insertByUpload]
|
|
1233
|
+
);
|
|
1234
|
+
const handleUrlPreview = useCallback(async () => {
|
|
1235
|
+
const nextUrl = urlInput.trim();
|
|
1236
|
+
if (!nextUrl) return;
|
|
1237
|
+
if (!isSafeImageUrl(nextUrl)) {
|
|
1238
|
+
setUrlError("Unsupported URL scheme");
|
|
1239
|
+
return;
|
|
1240
|
+
}
|
|
1241
|
+
setUrlLoading(true);
|
|
1242
|
+
setUrlError(null);
|
|
1243
|
+
try {
|
|
1244
|
+
const meta = await loadImageByUrl(nextUrl);
|
|
1245
|
+
setUrlMeta(meta);
|
|
1246
|
+
setUrlPreview(nextUrl);
|
|
1247
|
+
} catch {
|
|
1248
|
+
setUrlPreview(null);
|
|
1249
|
+
setUrlMeta(null);
|
|
1250
|
+
setUrlError("Could not load this image URL");
|
|
1251
|
+
} finally {
|
|
1252
|
+
setUrlLoading(false);
|
|
1253
|
+
}
|
|
1254
|
+
}, [urlInput]);
|
|
1255
|
+
const handleInsertByUrl = useCallback(() => {
|
|
1256
|
+
if (!urlPreview$1 || !isSafeImageUrl(urlPreview$1)) return;
|
|
1257
|
+
editor.update(() => {
|
|
1258
|
+
const node = $createImageNode({
|
|
1259
|
+
src: urlPreview$1,
|
|
1260
|
+
altText: "",
|
|
1261
|
+
width: urlMeta?.width,
|
|
1262
|
+
height: urlMeta?.height
|
|
1263
|
+
});
|
|
1264
|
+
$insertNodes([node]);
|
|
1265
|
+
});
|
|
1266
|
+
pushToast("success", "Image inserted");
|
|
1267
|
+
setDialogOpen(false);
|
|
1268
|
+
resetUrlState();
|
|
1269
|
+
}, [editor, pushToast, resetUrlState, urlMeta, urlPreview$1]);
|
|
1270
|
+
const helperMessage = pendingUploads > 0 ? `Uploading ${pendingUploads} image${pendingUploads > 1 ? "s" : ""}...` : rootDragActive ? "Drop image files to upload" : null;
|
|
1271
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1272
|
+
(helperMessage || toast$1) && /* @__PURE__ */ jsxs("div", { className: toastStack, children: [
|
|
1273
|
+
helperMessage && /* @__PURE__ */ jsxs("div", { className: `${toast} ${toastVariant.info}`, children: [
|
|
1274
|
+
/* @__PURE__ */ jsx("span", { className: spinner }),
|
|
1275
|
+
helperMessage
|
|
1276
|
+
] }),
|
|
1277
|
+
toast$1 && /* @__PURE__ */ jsxs("div", { className: `${toast} ${toastVariant[toast$1.kind]}`, children: [
|
|
1278
|
+
toast$1.kind === "success" ? /* @__PURE__ */ jsx(Check, { size: 12 }) : /* @__PURE__ */ jsx(Info, { size: 12 }),
|
|
1279
|
+
toast$1.message
|
|
1280
|
+
] })
|
|
1281
|
+
] }),
|
|
1282
|
+
/* @__PURE__ */ jsx(
|
|
1283
|
+
Dialog,
|
|
1284
|
+
{
|
|
1285
|
+
open: dialogOpen,
|
|
1286
|
+
onOpenChange: (nextOpen) => {
|
|
1287
|
+
if (!nextOpen && dialogUploading) return;
|
|
1288
|
+
setDialogOpen(nextOpen);
|
|
1289
|
+
if (!nextOpen) {
|
|
1290
|
+
resetUrlState();
|
|
1291
|
+
setDialogUploading(false);
|
|
1292
|
+
setDialogDragActive(false);
|
|
1293
|
+
}
|
|
1294
|
+
},
|
|
1295
|
+
children: /* @__PURE__ */ jsxs(DialogPopup, { className: dialogPopup, showCloseButton: !dialogUploading, children: [
|
|
1296
|
+
/* @__PURE__ */ jsx("div", { className: dialogHeader, children: /* @__PURE__ */ jsx(DialogTitle, { className: dialogTitle, children: "Insert image" }) }),
|
|
1297
|
+
/* @__PURE__ */ jsxs("div", { className: dialogBody, children: [
|
|
1298
|
+
/* @__PURE__ */ jsx("div", { className: tabWrap, children: /* @__PURE__ */ jsx(SegmentedControl, { fullWidth: true, items: tabItems, value: tab, onChange: setTab }) }),
|
|
1299
|
+
tab === "upload" ? /* @__PURE__ */ jsxs(
|
|
1300
|
+
"div",
|
|
1301
|
+
{
|
|
1302
|
+
className: `${uploadDropzone} ${uploadDropzoneState[dialogUploading ? "busy" : dialogDragActive ? "active" : "idle"]}`,
|
|
1303
|
+
role: "button",
|
|
1304
|
+
tabIndex: 0,
|
|
1305
|
+
onDragLeave: () => setDialogDragActive(false),
|
|
1306
|
+
onClick: () => {
|
|
1307
|
+
if (!dialogUploading) fileInputRef.current?.click();
|
|
1308
|
+
},
|
|
1309
|
+
onDragEnter: (event) => {
|
|
1310
|
+
if (hasImageData(event.dataTransfer)) {
|
|
1311
|
+
event.preventDefault();
|
|
1312
|
+
setDialogDragActive(true);
|
|
1313
|
+
}
|
|
1314
|
+
},
|
|
1315
|
+
onDragOver: (event) => {
|
|
1316
|
+
if (hasImageData(event.dataTransfer)) {
|
|
1317
|
+
event.preventDefault();
|
|
1318
|
+
}
|
|
1319
|
+
},
|
|
1320
|
+
onDrop: (event) => {
|
|
1321
|
+
event.preventDefault();
|
|
1322
|
+
setDialogDragActive(false);
|
|
1323
|
+
const file = [...event.dataTransfer.files].find(isImageFile);
|
|
1324
|
+
void handleDialogFile(file ?? null);
|
|
1325
|
+
},
|
|
1326
|
+
onKeyDown: (event) => {
|
|
1327
|
+
if (event.key !== "Enter" && event.key !== " ") return;
|
|
1328
|
+
event.preventDefault();
|
|
1329
|
+
if (!dialogUploading) fileInputRef.current?.click();
|
|
1330
|
+
},
|
|
1331
|
+
children: [
|
|
1332
|
+
/* @__PURE__ */ jsx(
|
|
1333
|
+
"input",
|
|
1334
|
+
{
|
|
1335
|
+
accept: "image/*",
|
|
1336
|
+
className: hiddenInput,
|
|
1337
|
+
ref: fileInputRef,
|
|
1338
|
+
type: "file",
|
|
1339
|
+
onChange: (event) => {
|
|
1340
|
+
const file = event.currentTarget.files?.[0] ?? null;
|
|
1341
|
+
void handleDialogFile(file);
|
|
1342
|
+
event.currentTarget.value = "";
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
),
|
|
1346
|
+
dialogUploading ? /* @__PURE__ */ jsx("div", { className: uploadBusyWrap, children: /* @__PURE__ */ jsxs("div", { className: uploadProgress, children: [
|
|
1347
|
+
/* @__PURE__ */ jsx("span", { className: spinner }),
|
|
1348
|
+
"Uploading image..."
|
|
1349
|
+
] }) }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1350
|
+
/* @__PURE__ */ jsx("span", { className: uploadDropIcon, children: /* @__PURE__ */ jsx(Upload, { size: 18 }) }),
|
|
1351
|
+
/* @__PURE__ */ jsx("span", { className: uploadDropTitle, children: "Click to upload or drag and drop" }),
|
|
1352
|
+
/* @__PURE__ */ jsx("span", { className: uploadDropDesc, children: "PNG, JPG, GIF, WebP" })
|
|
1353
|
+
] })
|
|
1354
|
+
]
|
|
1355
|
+
}
|
|
1356
|
+
) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1357
|
+
/* @__PURE__ */ jsxs("div", { className: urlInputRow, children: [
|
|
1358
|
+
/* @__PURE__ */ jsx(
|
|
1359
|
+
"input",
|
|
1360
|
+
{
|
|
1361
|
+
className: textInput,
|
|
1362
|
+
placeholder: "https://example.com/image.jpg",
|
|
1363
|
+
type: "url",
|
|
1364
|
+
value: urlInput,
|
|
1365
|
+
onChange: (event) => {
|
|
1366
|
+
setUrlInput(event.target.value);
|
|
1367
|
+
setUrlError(null);
|
|
1368
|
+
setUrlPreview(null);
|
|
1369
|
+
setUrlMeta(null);
|
|
1370
|
+
},
|
|
1371
|
+
onKeyDown: (event) => {
|
|
1372
|
+
if (event.key === "Enter") {
|
|
1373
|
+
event.preventDefault();
|
|
1374
|
+
if (urlPreview$1) {
|
|
1375
|
+
handleInsertByUrl();
|
|
1376
|
+
} else {
|
|
1377
|
+
void handleUrlPreview();
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
),
|
|
1383
|
+
/* @__PURE__ */ jsx(
|
|
1384
|
+
ActionButton,
|
|
1385
|
+
{
|
|
1386
|
+
disabled: urlLoading || !urlInput.trim(),
|
|
1387
|
+
size: "md",
|
|
1388
|
+
variant: "outline",
|
|
1389
|
+
onClick: () => void handleUrlPreview(),
|
|
1390
|
+
children: urlLoading ? "Loading" : "Preview"
|
|
1391
|
+
}
|
|
1392
|
+
),
|
|
1393
|
+
/* @__PURE__ */ jsx(
|
|
1394
|
+
ActionButton,
|
|
1395
|
+
{
|
|
1396
|
+
disabled: !urlPreview$1,
|
|
1397
|
+
size: "md",
|
|
1398
|
+
variant: "accent",
|
|
1399
|
+
onClick: handleInsertByUrl,
|
|
1400
|
+
children: "Insert"
|
|
1401
|
+
}
|
|
1402
|
+
)
|
|
1403
|
+
] }),
|
|
1404
|
+
urlError && /* @__PURE__ */ jsxs("span", { className: `${helperText} ${toastVariant.error}`, children: [
|
|
1405
|
+
/* @__PURE__ */ jsx(Info, { size: 12 }),
|
|
1406
|
+
urlError
|
|
1407
|
+
] }),
|
|
1408
|
+
urlPreview$1 && /* @__PURE__ */ jsx("div", { className: urlPreview, children: /* @__PURE__ */ jsx("img", { alt: "Preview", className: urlPreviewImage, src: urlPreview$1 }) })
|
|
1409
|
+
] })
|
|
1410
|
+
] }),
|
|
1411
|
+
/* @__PURE__ */ jsxs("div", { className: dialogFooter, children: [
|
|
1412
|
+
/* @__PURE__ */ jsxs("span", { className: helperText, children: [
|
|
1413
|
+
/* @__PURE__ */ jsx(Link2, { size: 12 }),
|
|
1414
|
+
"You can also paste images or drag files directly into the editor."
|
|
1415
|
+
] }),
|
|
1416
|
+
/* @__PURE__ */ jsx(ActionBar, { children: /* @__PURE__ */ jsx(
|
|
1417
|
+
ActionButton,
|
|
1418
|
+
{
|
|
1419
|
+
disabled: dialogUploading,
|
|
1420
|
+
size: "md",
|
|
1421
|
+
variant: "outline",
|
|
1422
|
+
onClick: () => setDialogOpen(false),
|
|
1423
|
+
children: "Close"
|
|
1424
|
+
}
|
|
1425
|
+
) })
|
|
1426
|
+
] })
|
|
1427
|
+
] })
|
|
1428
|
+
}
|
|
1429
|
+
)
|
|
1430
|
+
] });
|
|
1431
|
+
}
|
|
1432
|
+
function applyFavicon(dom, href) {
|
|
1433
|
+
if (dom.dataset.faviconHref === href) return;
|
|
1434
|
+
dom.dataset.faviconHref = href;
|
|
1435
|
+
const hostname = getHostname(href);
|
|
1436
|
+
if (!hostname) return;
|
|
1437
|
+
probeFavicon(hostname).then((faviconUrl) => {
|
|
1438
|
+
if (faviconUrl) {
|
|
1439
|
+
dom.style.setProperty("--rc-link-favicon", `url(${faviconUrl})`);
|
|
1440
|
+
dom.dataset.favicon = "loaded";
|
|
1441
|
+
}
|
|
1442
|
+
});
|
|
1443
|
+
}
|
|
1444
|
+
function LinkFaviconPlugin() {
|
|
1445
|
+
const [editor] = useLexicalComposerContext();
|
|
1446
|
+
useEffect(() => {
|
|
1447
|
+
const handleMutations = (mutations) => {
|
|
1448
|
+
for (const [nodeKey, mutation] of mutations) {
|
|
1449
|
+
if (mutation === "destroyed") continue;
|
|
1450
|
+
const dom = editor.getElementByKey(nodeKey);
|
|
1451
|
+
if (!dom) continue;
|
|
1452
|
+
const href = dom.getAttribute("href");
|
|
1453
|
+
if (href) applyFavicon(dom, href);
|
|
1454
|
+
}
|
|
1455
|
+
};
|
|
1456
|
+
const unregisterLink = editor.registerMutationListener(
|
|
1457
|
+
LinkNode,
|
|
1458
|
+
handleMutations
|
|
1459
|
+
);
|
|
1460
|
+
const unregisterAutoLink = editor.registerMutationListener(
|
|
1461
|
+
AutoLinkNode,
|
|
1462
|
+
handleMutations
|
|
1463
|
+
);
|
|
1464
|
+
return () => {
|
|
1465
|
+
unregisterLink();
|
|
1466
|
+
unregisterAutoLink();
|
|
1467
|
+
};
|
|
1468
|
+
}, [editor]);
|
|
1469
|
+
return null;
|
|
1470
|
+
}
|
|
1471
|
+
function AutoFocusPlugin() {
|
|
1472
|
+
const [editor] = useLexicalComposerContext();
|
|
1473
|
+
useEffect(() => {
|
|
1474
|
+
const root = editor.getRootElement();
|
|
1475
|
+
if (root) {
|
|
1476
|
+
root.focus({ preventScroll: true });
|
|
1477
|
+
} else {
|
|
1478
|
+
editor.focus();
|
|
1479
|
+
}
|
|
1480
|
+
}, [editor]);
|
|
1481
|
+
return null;
|
|
1482
|
+
}
|
|
1483
|
+
function EditorRefPlugin({ onEditorReady }) {
|
|
1484
|
+
const [editor] = useLexicalComposerContext();
|
|
1485
|
+
const callbackRef = useRef(onEditorReady);
|
|
1486
|
+
callbackRef.current = onEditorReady;
|
|
1487
|
+
useEffect(() => {
|
|
1488
|
+
callbackRef.current?.(editor);
|
|
1489
|
+
return () => callbackRef.current?.(null);
|
|
1490
|
+
}, [editor]);
|
|
1491
|
+
return null;
|
|
1492
|
+
}
|
|
1493
|
+
function FootnotePlugin({ children }) {
|
|
1494
|
+
const [editor] = useLexicalComposerContext();
|
|
1495
|
+
const [definitions, setDefinitions] = useState({});
|
|
1496
|
+
const [displayNumberMap, setDisplayNumberMap] = useState({});
|
|
1497
|
+
const pendingUpdateRef = useRef(false);
|
|
1498
|
+
useEffect(() => {
|
|
1499
|
+
return editor.registerUpdateListener(({ editorState }) => {
|
|
1500
|
+
editorState.read(() => {
|
|
1501
|
+
const footnoteNodes = $nodesOfType(FootnoteNode);
|
|
1502
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1503
|
+
const numberMap = {};
|
|
1504
|
+
let counter = 1;
|
|
1505
|
+
for (const node of footnoteNodes) {
|
|
1506
|
+
const id = node.getIdentifier();
|
|
1507
|
+
if (!seen.has(id)) {
|
|
1508
|
+
seen.add(id);
|
|
1509
|
+
numberMap[id] = counter++;
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
setDisplayNumberMap(numberMap);
|
|
1513
|
+
const sectionNodes = $nodesOfType(FootnoteSectionNode);
|
|
1514
|
+
if (sectionNodes.length > 0) {
|
|
1515
|
+
setDefinitions(sectionNodes[0].getDefinitions());
|
|
1516
|
+
} else {
|
|
1517
|
+
setDefinitions({});
|
|
1518
|
+
}
|
|
1519
|
+
if (!editor.isEditable() || pendingUpdateRef.current) return;
|
|
1520
|
+
if (footnoteNodes.length === 0 && sectionNodes.length > 0) {
|
|
1521
|
+
pendingUpdateRef.current = true;
|
|
1522
|
+
queueMicrotask(() => {
|
|
1523
|
+
editor.update(() => {
|
|
1524
|
+
const sections = $nodesOfType(FootnoteSectionNode);
|
|
1525
|
+
for (const s of sections) s.remove();
|
|
1526
|
+
});
|
|
1527
|
+
pendingUpdateRef.current = false;
|
|
1528
|
+
});
|
|
1529
|
+
return;
|
|
1530
|
+
}
|
|
1531
|
+
if (footnoteNodes.length > 0 && sectionNodes.length === 0) {
|
|
1532
|
+
const seenSnapshot = [...seen];
|
|
1533
|
+
pendingUpdateRef.current = true;
|
|
1534
|
+
queueMicrotask(() => {
|
|
1535
|
+
editor.update(() => {
|
|
1536
|
+
const root = $getRoot();
|
|
1537
|
+
const defs = {};
|
|
1538
|
+
for (const id of seenSnapshot) {
|
|
1539
|
+
defs[id] = "";
|
|
1540
|
+
}
|
|
1541
|
+
const section = $parseSerializedNode({
|
|
1542
|
+
type: "footnote-section",
|
|
1543
|
+
definitions: defs,
|
|
1544
|
+
version: 1
|
|
1545
|
+
});
|
|
1546
|
+
root.append(section);
|
|
1547
|
+
});
|
|
1548
|
+
pendingUpdateRef.current = false;
|
|
1549
|
+
});
|
|
1550
|
+
} else if (sectionNodes.length > 0) {
|
|
1551
|
+
const existingDefs = sectionNodes[0].getDefinitions();
|
|
1552
|
+
const missingIds = [...seen].filter((id) => !(id in existingDefs));
|
|
1553
|
+
const orphanIds = Object.keys(existingDefs).filter(
|
|
1554
|
+
(id) => !seen.has(id)
|
|
1555
|
+
);
|
|
1556
|
+
if (missingIds.length > 0 || orphanIds.length > 0) {
|
|
1557
|
+
pendingUpdateRef.current = true;
|
|
1558
|
+
queueMicrotask(() => {
|
|
1559
|
+
editor.update(() => {
|
|
1560
|
+
const freshSections = $nodesOfType(FootnoteSectionNode);
|
|
1561
|
+
if (freshSections.length > 0) {
|
|
1562
|
+
for (const id of missingIds) {
|
|
1563
|
+
freshSections[0].setDefinition(id, "");
|
|
1564
|
+
}
|
|
1565
|
+
for (const id of orphanIds) {
|
|
1566
|
+
freshSections[0].removeDefinition(id);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
});
|
|
1570
|
+
pendingUpdateRef.current = false;
|
|
1571
|
+
});
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
});
|
|
1575
|
+
});
|
|
1576
|
+
}, [editor]);
|
|
1577
|
+
return /* @__PURE__ */ jsx(
|
|
1578
|
+
FootnoteDefinitionsProvider,
|
|
1579
|
+
{
|
|
1580
|
+
definitions,
|
|
1581
|
+
displayNumberMap,
|
|
1582
|
+
children
|
|
1583
|
+
}
|
|
1584
|
+
);
|
|
1585
|
+
}
|
|
1586
|
+
function OnChangePlugin({ onChange, debounceMs }) {
|
|
1587
|
+
const [editor] = useLexicalComposerContext();
|
|
1588
|
+
const timerRef = useRef(void 0);
|
|
1589
|
+
const onChangeRef = useRef(onChange);
|
|
1590
|
+
onChangeRef.current = onChange;
|
|
1591
|
+
useEffect(() => {
|
|
1592
|
+
const unregister = editor.registerUpdateListener(({ editorState }) => {
|
|
1593
|
+
const fn = onChangeRef.current;
|
|
1594
|
+
if (!fn) return;
|
|
1595
|
+
if (debounceMs && debounceMs > 0) {
|
|
1596
|
+
clearTimeout(timerRef.current);
|
|
1597
|
+
timerRef.current = setTimeout(() => {
|
|
1598
|
+
fn(editorState.toJSON());
|
|
1599
|
+
}, debounceMs);
|
|
1600
|
+
} else {
|
|
1601
|
+
fn(editorState.toJSON());
|
|
1602
|
+
}
|
|
1603
|
+
});
|
|
1604
|
+
return () => {
|
|
1605
|
+
clearTimeout(timerRef.current);
|
|
1606
|
+
unregister();
|
|
1607
|
+
};
|
|
1608
|
+
}, [editor, debounceMs]);
|
|
1609
|
+
return null;
|
|
1610
|
+
}
|
|
1611
|
+
function SubmitShortcutPlugin({ onSubmit }) {
|
|
1612
|
+
const [editor] = useLexicalComposerContext();
|
|
1613
|
+
useEffect(() => {
|
|
1614
|
+
if (!onSubmit) return;
|
|
1615
|
+
return editor.registerCommand(
|
|
1616
|
+
KEY_ENTER_COMMAND,
|
|
1617
|
+
(event) => {
|
|
1618
|
+
if (event && (event.metaKey || event.ctrlKey)) {
|
|
1619
|
+
event.preventDefault();
|
|
1620
|
+
onSubmit();
|
|
1621
|
+
return true;
|
|
1622
|
+
}
|
|
1623
|
+
return false;
|
|
1624
|
+
},
|
|
1625
|
+
COMMAND_PRIORITY_HIGH
|
|
1626
|
+
);
|
|
1627
|
+
}, [editor, onSubmit]);
|
|
1628
|
+
return null;
|
|
1629
|
+
}
|
|
1630
|
+
export {
|
|
1631
|
+
ALL_TRANSFORMERS as A,
|
|
1632
|
+
BlockExitPlugin as B,
|
|
1633
|
+
CorePlugins as C,
|
|
1634
|
+
EditorRefPlugin as E,
|
|
1635
|
+
FootnotePlugin as F,
|
|
1636
|
+
HorizontalRulePlugin as H,
|
|
1637
|
+
ImageUploadPlugin as I,
|
|
1638
|
+
LinkFaviconPlugin as L,
|
|
1639
|
+
MarkdownPastePlugin as M,
|
|
1640
|
+
OnChangePlugin as O,
|
|
1641
|
+
SubmitShortcutPlugin as S,
|
|
1642
|
+
AutoFocusPlugin as a,
|
|
1643
|
+
AutoLinkPlugin as b,
|
|
1644
|
+
BlockIdPlugin as c,
|
|
1645
|
+
ImageUploadProvider as d,
|
|
1646
|
+
MarkdownShortcutsPlugin as e,
|
|
1647
|
+
blockIdState as f,
|
|
1648
|
+
defaultImageUpload as g,
|
|
1649
|
+
useImageUpload as u
|
|
1650
|
+
};
|