@nkzw/mdx-editor 0.1.0

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.
Files changed (148) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +86 -0
  3. package/UPSTREAM.md +21 -0
  4. package/dist/EditorIcon.js +75 -0
  5. package/dist/FormatConstants.js +20 -0
  6. package/dist/MDXEditor.js +189 -0
  7. package/dist/MarkdownEditor.js +281 -0
  8. package/dist/PersistentMarkdownEditor.js +358 -0
  9. package/dist/RealmWithPlugins.js +35 -0
  10. package/dist/core.d.ts +3232 -0
  11. package/dist/core.js +354 -0
  12. package/dist/defaultSvgIcons.js +371 -0
  13. package/dist/directive-editors/AdmonitionDirectiveDescriptor.js +28 -0
  14. package/dist/directive-editors/GenericDirectiveEditor.js +37 -0
  15. package/dist/exportMarkdownFromLexical.js +262 -0
  16. package/dist/horizontalRuleShortcut.js +37 -0
  17. package/dist/importMarkdownToLexical.js +172 -0
  18. package/dist/index.d.ts +86 -0
  19. package/dist/index.js +8 -0
  20. package/dist/jsx-editors/GenericJsxEditor.js +84 -0
  21. package/dist/mdastUtilHtmlComment.js +125 -0
  22. package/dist/persistence.d.ts +128 -0
  23. package/dist/persistence.js +4 -0
  24. package/dist/plugins/codeblock/CodeBlockNode.js +183 -0
  25. package/dist/plugins/codeblock/CodeBlockVisitor.js +14 -0
  26. package/dist/plugins/codeblock/MdastCodeVisitor.js +23 -0
  27. package/dist/plugins/codeblock/findCodeBlockDescriptor.js +8 -0
  28. package/dist/plugins/codeblock/index.js +46 -0
  29. package/dist/plugins/codemirror/CodeMirrorEditor.js +145 -0
  30. package/dist/plugins/codemirror/index.js +115 -0
  31. package/dist/plugins/codemirror/useCodeMirrorRef.js +101 -0
  32. package/dist/plugins/core/GenericHTMLNode.js +118 -0
  33. package/dist/plugins/core/LexicalGenericHTMLNodeVisitor.js +15 -0
  34. package/dist/plugins/core/LexicalLinebreakVisitor.js +10 -0
  35. package/dist/plugins/core/LexicalParagraphVisitor.js +10 -0
  36. package/dist/plugins/core/LexicalRootVisitor.js +10 -0
  37. package/dist/plugins/core/LexicalTextVisitor.js +160 -0
  38. package/dist/plugins/core/MdastBreakVisitor.js +10 -0
  39. package/dist/plugins/core/MdastFormattingVisitor.js +81 -0
  40. package/dist/plugins/core/MdastHTMLNode.js +120 -0
  41. package/dist/plugins/core/MdastHTMLVisitor.js +17 -0
  42. package/dist/plugins/core/MdastParagraphVisitor.js +23 -0
  43. package/dist/plugins/core/MdastRootVisitor.js +9 -0
  44. package/dist/plugins/core/MdastTextVisitor.js +16 -0
  45. package/dist/plugins/core/NestedLexicalEditor.js +221 -0
  46. package/dist/plugins/core/PropertyPopover.js +75 -0
  47. package/dist/plugins/core/SharedHistoryPlugin.js +10 -0
  48. package/dist/plugins/core/index.js +692 -0
  49. package/dist/plugins/core/ui/DownshiftAutoComplete.js +89 -0
  50. package/dist/plugins/core/ui/PopoverUtils.js +22 -0
  51. package/dist/plugins/diff-source/DiffSourceWrapper.js +24 -0
  52. package/dist/plugins/diff-source/DiffViewer.js +84 -0
  53. package/dist/plugins/diff-source/SourceEditor.js +60 -0
  54. package/dist/plugins/diff-source/index.js +27 -0
  55. package/dist/plugins/directives/DirectiveNode.js +107 -0
  56. package/dist/plugins/directives/DirectiveVisitor.js +10 -0
  57. package/dist/plugins/directives/MdastDirectiveVisitor.js +30 -0
  58. package/dist/plugins/directives/index.js +45 -0
  59. package/dist/plugins/frontmatter/FrontmatterEditor.js +137 -0
  60. package/dist/plugins/frontmatter/FrontmatterNode.js +70 -0
  61. package/dist/plugins/frontmatter/LexicalFrontmatterVisitor.js +10 -0
  62. package/dist/plugins/frontmatter/MdastFrontmatterVisitor.js +10 -0
  63. package/dist/plugins/frontmatter/index.js +113 -0
  64. package/dist/plugins/headings/LexicalHeadingVisitor.js +11 -0
  65. package/dist/plugins/headings/MdastHeadingVisitor.js +10 -0
  66. package/dist/plugins/headings/index.js +63 -0
  67. package/dist/plugins/image/EditImageToolbar.js +58 -0
  68. package/dist/plugins/image/ImageDialog.js +132 -0
  69. package/dist/plugins/image/ImageEditor.js +279 -0
  70. package/dist/plugins/image/ImageNode.js +187 -0
  71. package/dist/plugins/image/ImagePlaceholder.js +9 -0
  72. package/dist/plugins/image/ImageResizer.js +223 -0
  73. package/dist/plugins/image/LexicalImageVisitor.js +42 -0
  74. package/dist/plugins/image/MdastImageVisitor.js +91 -0
  75. package/dist/plugins/image/index.js +364 -0
  76. package/dist/plugins/jsx/LexicalJsxNode.js +103 -0
  77. package/dist/plugins/jsx/LexicalJsxVisitor.js +27 -0
  78. package/dist/plugins/jsx/LexicalMdxExpressionNode.js +130 -0
  79. package/dist/plugins/jsx/LexicalMdxExpressionVisitor.js +14 -0
  80. package/dist/plugins/jsx/MdastMdxExpressionVisitor.js +11 -0
  81. package/dist/plugins/jsx/MdastMdxJsEsmVisitor.js +8 -0
  82. package/dist/plugins/jsx/MdastMdxJsxElementVisitor.js +28 -0
  83. package/dist/plugins/jsx/index.js +97 -0
  84. package/dist/plugins/jsx/jsxTagName.js +7 -0
  85. package/dist/plugins/link/AutoLinkPlugin.js +18 -0
  86. package/dist/plugins/link/LexicalLinkVisitor.js +10 -0
  87. package/dist/plugins/link/MdastLinkVisitor.js +14 -0
  88. package/dist/plugins/link/index.js +34 -0
  89. package/dist/plugins/link-dialog/LinkDialog.js +262 -0
  90. package/dist/plugins/link-dialog/index.js +304 -0
  91. package/dist/plugins/lists/CheckListPlugin.js +270 -0
  92. package/dist/plugins/lists/LexicalListItemVisitor.js +41 -0
  93. package/dist/plugins/lists/LexicalListVisitor.js +13 -0
  94. package/dist/plugins/lists/MdastListItemVisitor.js +11 -0
  95. package/dist/plugins/lists/MdastListVisitor.js +19 -0
  96. package/dist/plugins/lists/NotesListItemNode.js +22 -0
  97. package/dist/plugins/lists/index.js +111 -0
  98. package/dist/plugins/markdown-shortcut/index.js +114 -0
  99. package/dist/plugins/maxlength/index.js +36 -0
  100. package/dist/plugins/quote/LexicalQuoteVisitor.js +10 -0
  101. package/dist/plugins/quote/MdastBlockQuoteVisitor.js +10 -0
  102. package/dist/plugins/quote/index.js +18 -0
  103. package/dist/plugins/remote/index.js +52 -0
  104. package/dist/plugins/search/index.js +360 -0
  105. package/dist/plugins/table/LexicalTableVisitor.js +10 -0
  106. package/dist/plugins/table/MdastTableVisitor.js +10 -0
  107. package/dist/plugins/table/TableEditor.js +527 -0
  108. package/dist/plugins/table/TableNode.js +208 -0
  109. package/dist/plugins/table/index.js +66 -0
  110. package/dist/plugins/thematic-break/LexicalThematicBreakVisitor.js +10 -0
  111. package/dist/plugins/thematic-break/MdastThematicBreakVisitor.js +10 -0
  112. package/dist/plugins/thematic-break/index.js +27 -0
  113. package/dist/plugins/toolbar/components/BlockTypeSelect.js +62 -0
  114. package/dist/plugins/toolbar/components/BoldItalicUnderlineToggles.js +98 -0
  115. package/dist/plugins/toolbar/components/ChangeAdmonitionType.js +43 -0
  116. package/dist/plugins/toolbar/components/ChangeCodeMirrorLanguage.js +42 -0
  117. package/dist/plugins/toolbar/components/CodeToggle.js +21 -0
  118. package/dist/plugins/toolbar/components/CreateLink.js +24 -0
  119. package/dist/plugins/toolbar/components/DiffSourceToggleWrapper.js +42 -0
  120. package/dist/plugins/toolbar/components/HighlightToggle.js +28 -0
  121. package/dist/plugins/toolbar/components/InsertAdmonition.js +34 -0
  122. package/dist/plugins/toolbar/components/InsertCodeBlock.js +23 -0
  123. package/dist/plugins/toolbar/components/InsertFrontmatter.js +28 -0
  124. package/dist/plugins/toolbar/components/InsertImage.js +29 -0
  125. package/dist/plugins/toolbar/components/InsertTable.js +25 -0
  126. package/dist/plugins/toolbar/components/InsertThematicBreak.js +23 -0
  127. package/dist/plugins/toolbar/components/KitchenSinkToolbar.js +82 -0
  128. package/dist/plugins/toolbar/components/ListsToggle.js +29 -0
  129. package/dist/plugins/toolbar/components/UndoRedo.js +60 -0
  130. package/dist/plugins/toolbar/index.js +32 -0
  131. package/dist/plugins/toolbar/primitives/DialogButton.js +130 -0
  132. package/dist/plugins/toolbar/primitives/TooltipWrap.js +17 -0
  133. package/dist/plugins/toolbar/primitives/select.js +76 -0
  134. package/dist/plugins/toolbar/primitives/toolbar.js +144 -0
  135. package/dist/registerCodeBoundaryEscape.js +40 -0
  136. package/dist/styles/lexical-theme.module.css.js +62 -0
  137. package/dist/styles/lexicalTheme.js +32 -0
  138. package/dist/styles/ui.module.css.js +296 -0
  139. package/dist/styles.css +2838 -0
  140. package/dist/utils/detectMac.js +16 -0
  141. package/dist/utils/fp.js +44 -0
  142. package/dist/utils/isPartOftheEditorUI.js +12 -0
  143. package/dist/utils/lexicalHelpers.js +185 -0
  144. package/dist/utils/makeHslTransparent.js +6 -0
  145. package/dist/utils/mergeStyleAttributes.js +22 -0
  146. package/dist/utils/uuid4.js +10 -0
  147. package/dist/utils/voidEmitter.js +15 -0
  148. package/package.json +133 -0
@@ -0,0 +1,527 @@
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import { ContentEditable } from "@lexical/react/LexicalContentEditable";
3
+ import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary";
4
+ import { LexicalNestedComposer } from "@lexical/react/LexicalNestedComposer";
5
+ import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
6
+ import * as RadixPopover from "@radix-ui/react-popover";
7
+ import { $createParagraphNode, createEditor, $getRoot, KEY_TAB_COMMAND, COMMAND_PRIORITY_CRITICAL, FOCUS_COMMAND, COMMAND_PRIORITY_LOW, KEY_ENTER_COMMAND, BLUR_COMMAND, COMMAND_PRIORITY_EDITOR } from "lexical";
8
+ import React from "react";
9
+ import { exportLexicalTreeToMdast } from "../../exportMarkdownFromLexical.js";
10
+ import { importMdastTreeToLexical } from "../../importMarkdownToLexical.js";
11
+ import { lexicalTheme } from "../../styles/lexicalTheme.js";
12
+ import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
13
+ import { mergeRegister } from "@lexical/utils";
14
+ import * as RadixToolbar from "@radix-ui/react-toolbar";
15
+ import classNames from "classnames";
16
+ import { EditorIcon } from "../../EditorIcon.js";
17
+ import styles from "../../styles/ui.module.css.js";
18
+ import { isPartOftheEditorUI } from "../../utils/isPartOftheEditorUI.js";
19
+ import { uuidv4 } from "../../utils/uuid4.js";
20
+ import { readOnly$, useTranslation, editorRootElementRef$, importVisitors$, exportVisitors$, usedLexicalNodes$, jsxComponentDescriptors$, directiveDescriptors$, codeBlockEditorDescriptors$, defaultCodeBlockLanguage$, jsxIsAvailable$, rootEditor$, tableCellEditorChildren$, NESTED_EDITOR_UPDATED_COMMAND } from "../core/index.js";
21
+ import { useCellValues } from "@mdxeditor/gurx";
22
+ const getCellType = (rowIndex) => {
23
+ if (rowIndex === 0) {
24
+ return "th";
25
+ }
26
+ return "td";
27
+ };
28
+ const AlignToTailwindClassMap = {
29
+ center: styles.centeredCell,
30
+ left: styles.leftAlignedCell,
31
+ right: styles.rightAlignedCell
32
+ };
33
+ const TableEditor = ({ mdastNode, parentEditor, lexicalTable }) => {
34
+ const [activeCell, setActiveCell] = React.useState(null);
35
+ const [readOnly] = useCellValues(readOnly$);
36
+ const getCellKey = React.useMemo(() => {
37
+ return (cell) => {
38
+ cell.__cacheKey ??= uuidv4();
39
+ return cell.__cacheKey;
40
+ };
41
+ }, []);
42
+ const setActiveCellWithBoundaries = React.useCallback(
43
+ (cell) => {
44
+ const colCount = lexicalTable.getColCount();
45
+ if (cell === null) {
46
+ setActiveCell(null);
47
+ return;
48
+ }
49
+ let [colIndex, rowIndex] = cell;
50
+ if (colIndex > colCount - 1) {
51
+ colIndex = 0;
52
+ rowIndex++;
53
+ }
54
+ if (colIndex < 0) {
55
+ colIndex = colCount - 1;
56
+ rowIndex -= 1;
57
+ }
58
+ if (rowIndex > lexicalTable.getRowCount() - 1) {
59
+ setActiveCell(null);
60
+ parentEditor.update(() => {
61
+ const nextSibling = lexicalTable.getLatest().getNextSibling();
62
+ if (nextSibling) {
63
+ lexicalTable.getLatest().selectNext();
64
+ } else {
65
+ const newParagraph = $createParagraphNode();
66
+ lexicalTable.insertAfter(newParagraph);
67
+ newParagraph.select();
68
+ }
69
+ });
70
+ return;
71
+ }
72
+ if (rowIndex < 0) {
73
+ setActiveCell(null);
74
+ parentEditor.update(() => {
75
+ lexicalTable.getLatest().selectPrevious();
76
+ });
77
+ return;
78
+ }
79
+ setActiveCell([colIndex, rowIndex]);
80
+ },
81
+ [lexicalTable, parentEditor]
82
+ );
83
+ React.useEffect(() => {
84
+ lexicalTable.focusEmitter.subscribe(setActiveCellWithBoundaries);
85
+ }, [lexicalTable, setActiveCellWithBoundaries]);
86
+ const addRowToBottom = React.useCallback(
87
+ (e) => {
88
+ e.preventDefault();
89
+ parentEditor.update(() => {
90
+ lexicalTable.addRowToBottom();
91
+ setActiveCell([0, lexicalTable.getRowCount()]);
92
+ });
93
+ },
94
+ [parentEditor, lexicalTable]
95
+ );
96
+ const addColumnToRight = React.useCallback(
97
+ (e) => {
98
+ e.preventDefault();
99
+ parentEditor.update(() => {
100
+ lexicalTable.addColumnToRight();
101
+ setActiveCell([lexicalTable.getColCount(), 0]);
102
+ });
103
+ },
104
+ [parentEditor, lexicalTable]
105
+ );
106
+ const [highlightedCoordinates, setHighlightedCoordinates] = React.useState([-1, -1]);
107
+ const onTableMouseOver = React.useCallback((e) => {
108
+ let tableCell = e.target;
109
+ while (tableCell && !["TH", "TD"].includes(tableCell.tagName)) {
110
+ if (tableCell === e.currentTarget) {
111
+ return;
112
+ }
113
+ tableCell = tableCell.parentElement;
114
+ }
115
+ if (tableCell === null) {
116
+ return;
117
+ }
118
+ const tableRow = tableCell.parentElement;
119
+ const tableContainer = tableRow.parentElement;
120
+ const colIndex = tableContainer.tagName === "TFOOT" ? -1 : Array.from(tableRow.children).indexOf(tableCell);
121
+ const rowIndex = tableCell.tagName === "TH" ? -1 : Array.from(tableRow.parentElement.children).indexOf(tableRow);
122
+ setHighlightedCoordinates([colIndex, rowIndex]);
123
+ }, []);
124
+ const t = useTranslation();
125
+ return /* @__PURE__ */ jsxs(
126
+ "table",
127
+ {
128
+ className: styles.tableEditor,
129
+ onMouseOver: onTableMouseOver,
130
+ onMouseLeave: () => {
131
+ setHighlightedCoordinates([-1, -1]);
132
+ },
133
+ children: [
134
+ /* @__PURE__ */ jsxs("colgroup", { children: [
135
+ readOnly ? null : /* @__PURE__ */ jsx("col", {}),
136
+ Array.from({ length: mdastNode.children[0].children.length }, (_, colIndex) => {
137
+ const align = mdastNode.align ?? [];
138
+ const currentColumnAlign = align[colIndex] ?? "left";
139
+ const className = AlignToTailwindClassMap[currentColumnAlign];
140
+ return /* @__PURE__ */ jsx("col", { className }, colIndex);
141
+ }),
142
+ readOnly ? null : /* @__PURE__ */ jsx("col", {})
143
+ ] }),
144
+ readOnly || /* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [
145
+ /* @__PURE__ */ jsx("th", { className: styles.tableToolsColumn }),
146
+ Array.from({ length: mdastNode.children[0].children.length }, (_, colIndex) => {
147
+ return /* @__PURE__ */ jsx("th", { "data-tool-cell": true, children: /* @__PURE__ */ jsx(
148
+ ColumnEditor,
149
+ {
150
+ ...{
151
+ setActiveCellWithBoundaries,
152
+ parentEditor,
153
+ colIndex,
154
+ highlightedCoordinates,
155
+ lexicalTable,
156
+ align: (mdastNode.align ?? [])[colIndex]
157
+ }
158
+ }
159
+ ) }, colIndex);
160
+ }),
161
+ /* @__PURE__ */ jsx("th", { className: styles.tableToolsColumn, "data-tool-cell": true, children: /* @__PURE__ */ jsx(
162
+ "button",
163
+ {
164
+ className: styles.iconButton,
165
+ type: "button",
166
+ title: t("table.deleteTable", "Delete table"),
167
+ onClick: (e) => {
168
+ e.preventDefault();
169
+ parentEditor.update(() => {
170
+ lexicalTable.selectNext();
171
+ lexicalTable.remove();
172
+ });
173
+ },
174
+ children: /* @__PURE__ */ jsx(EditorIcon, { name: "delete" })
175
+ }
176
+ ) })
177
+ ] }) }),
178
+ /* @__PURE__ */ jsx("tbody", { children: mdastNode.children.map((row, rowIndex) => {
179
+ const CellElement = getCellType(rowIndex);
180
+ return /* @__PURE__ */ jsxs("tr", { children: [
181
+ readOnly || /* @__PURE__ */ jsx(CellElement, { className: styles.toolCell, "data-tool-cell": true, children: /* @__PURE__ */ jsx(RowEditor, { ...{ setActiveCellWithBoundaries, parentEditor, rowIndex, highlightedCoordinates, lexicalTable } }) }),
182
+ row.children.map((mdastCell, colIndex) => {
183
+ return /* @__PURE__ */ jsx(
184
+ Cell,
185
+ {
186
+ align: mdastNode.align?.[colIndex],
187
+ contents: mdastCell.children,
188
+ setActiveCell: setActiveCellWithBoundaries,
189
+ ...{
190
+ rowIndex,
191
+ colIndex,
192
+ lexicalTable,
193
+ parentEditor,
194
+ activeCell: readOnly ? [-1, -1] : activeCell
195
+ }
196
+ },
197
+ getCellKey(mdastCell)
198
+ );
199
+ }),
200
+ readOnly || rowIndex === 0 && /* @__PURE__ */ jsx("th", { rowSpan: lexicalTable.getRowCount(), "data-tool-cell": true, children: /* @__PURE__ */ jsx("button", { type: "button", className: styles.addColumnButton, onClick: addColumnToRight, children: /* @__PURE__ */ jsx("span", { className: styles.tableAddButtonIcon, children: /* @__PURE__ */ jsx(EditorIcon, { name: "add-column" }) }) }) })
201
+ ] }, rowIndex);
202
+ }) }),
203
+ readOnly || /* @__PURE__ */ jsx("tfoot", { children: /* @__PURE__ */ jsxs("tr", { children: [
204
+ /* @__PURE__ */ jsx("th", {}),
205
+ /* @__PURE__ */ jsx("th", { colSpan: lexicalTable.getColCount(), "data-tool-cell": true, children: /* @__PURE__ */ jsx("button", { type: "button", className: styles.addRowButton, onClick: addRowToBottom, children: /* @__PURE__ */ jsx("span", { className: styles.tableAddButtonIcon, children: /* @__PURE__ */ jsx(EditorIcon, { name: "add-row" }) }) }) }),
206
+ /* @__PURE__ */ jsx("th", {})
207
+ ] }) })
208
+ ]
209
+ }
210
+ );
211
+ };
212
+ const Cell = ({ align, ...props }) => {
213
+ const { activeCell, setActiveCell } = props;
214
+ const isActive = Boolean(activeCell && activeCell[0] === props.colIndex && activeCell[1] === props.rowIndex);
215
+ const className = AlignToTailwindClassMap[align ?? "left"];
216
+ const CellElement = getCellType(props.rowIndex);
217
+ return /* @__PURE__ */ jsx(
218
+ CellElement,
219
+ {
220
+ className,
221
+ "data-active": isActive,
222
+ onClick: () => {
223
+ setActiveCell([props.colIndex, props.rowIndex]);
224
+ },
225
+ children: /* @__PURE__ */ jsx(CellEditor, { ...props, focus: isActive })
226
+ }
227
+ );
228
+ };
229
+ const CellEditor = ({ focus, setActiveCell, parentEditor, lexicalTable, contents, colIndex, rowIndex }) => {
230
+ const [
231
+ importVisitors,
232
+ exportVisitors,
233
+ usedLexicalNodes,
234
+ jsxComponentDescriptors,
235
+ directiveDescriptors,
236
+ codeBlockEditorDescriptors,
237
+ defaultCodeBlockLanguage,
238
+ jsxIsAvailable,
239
+ rootEditor,
240
+ tableCellEditorChildren
241
+ ] = useCellValues(
242
+ importVisitors$,
243
+ exportVisitors$,
244
+ usedLexicalNodes$,
245
+ jsxComponentDescriptors$,
246
+ directiveDescriptors$,
247
+ codeBlockEditorDescriptors$,
248
+ defaultCodeBlockLanguage$,
249
+ jsxIsAvailable$,
250
+ rootEditor$,
251
+ tableCellEditorChildren$
252
+ );
253
+ const [editor] = React.useState(() => {
254
+ const editor2 = createEditor({
255
+ nodes: usedLexicalNodes,
256
+ theme: lexicalTheme,
257
+ namespace: "TableCellEditor"
258
+ });
259
+ editor2.update(() => {
260
+ importMdastTreeToLexical({
261
+ root: $getRoot(),
262
+ mdastRoot: { type: "root", children: [{ type: "paragraph", children: contents }] },
263
+ visitors: importVisitors,
264
+ jsxComponentDescriptors,
265
+ directiveDescriptors,
266
+ codeBlockEditorDescriptors,
267
+ defaultCodeBlockLanguage
268
+ });
269
+ });
270
+ return editor2;
271
+ });
272
+ const saveAndFocus = React.useCallback(
273
+ (nextCell) => {
274
+ editor.getEditorState().read(() => {
275
+ const mdast = exportLexicalTreeToMdast({
276
+ root: $getRoot(),
277
+ jsxComponentDescriptors,
278
+ visitors: exportVisitors,
279
+ jsxIsAvailable
280
+ });
281
+ parentEditor.update(
282
+ () => {
283
+ lexicalTable.updateCellContents(colIndex, rowIndex, mdast.children[0].children);
284
+ },
285
+ { discrete: true }
286
+ );
287
+ parentEditor.dispatchCommand(NESTED_EDITOR_UPDATED_COMMAND, void 0);
288
+ });
289
+ setActiveCell(nextCell);
290
+ },
291
+ [colIndex, editor, exportVisitors, jsxComponentDescriptors, jsxIsAvailable, lexicalTable, parentEditor, rowIndex, setActiveCell]
292
+ );
293
+ React.useEffect(() => {
294
+ return mergeRegister(
295
+ editor.registerCommand(
296
+ KEY_TAB_COMMAND,
297
+ (payload) => {
298
+ payload.preventDefault();
299
+ const nextCell = payload.shiftKey ? [colIndex - 1, rowIndex] : [colIndex + 1, rowIndex];
300
+ saveAndFocus(nextCell);
301
+ return true;
302
+ },
303
+ COMMAND_PRIORITY_CRITICAL
304
+ ),
305
+ editor.registerCommand(
306
+ FOCUS_COMMAND,
307
+ () => {
308
+ setActiveCell([colIndex, rowIndex]);
309
+ return false;
310
+ },
311
+ COMMAND_PRIORITY_LOW
312
+ ),
313
+ editor.registerCommand(
314
+ KEY_ENTER_COMMAND,
315
+ (payload) => {
316
+ payload?.preventDefault();
317
+ const nextCell = payload?.shiftKey ? [colIndex, rowIndex - 1] : [colIndex, rowIndex + 1];
318
+ saveAndFocus(nextCell);
319
+ return true;
320
+ },
321
+ COMMAND_PRIORITY_CRITICAL
322
+ ),
323
+ editor.registerCommand(
324
+ BLUR_COMMAND,
325
+ (payload) => {
326
+ const relatedTarget = payload.relatedTarget;
327
+ if (isPartOftheEditorUI(relatedTarget, rootEditor.getRootElement())) {
328
+ return false;
329
+ }
330
+ saveAndFocus(null);
331
+ return true;
332
+ },
333
+ COMMAND_PRIORITY_EDITOR
334
+ ),
335
+ editor.registerCommand(
336
+ NESTED_EDITOR_UPDATED_COMMAND,
337
+ () => {
338
+ saveAndFocus(null);
339
+ return true;
340
+ },
341
+ COMMAND_PRIORITY_EDITOR
342
+ )
343
+ );
344
+ }, [colIndex, editor, rootEditor, rowIndex, saveAndFocus, setActiveCell]);
345
+ React.useEffect(() => {
346
+ if (focus) {
347
+ editor.focus();
348
+ }
349
+ }, [focus, editor]);
350
+ return /* @__PURE__ */ jsxs(LexicalNestedComposer, { initialEditor: editor, children: [
351
+ /* @__PURE__ */ jsx(RichTextPlugin, { contentEditable: /* @__PURE__ */ jsx(ContentEditable, {}), placeholder: /* @__PURE__ */ jsx("div", {}), ErrorBoundary: LexicalErrorBoundary }),
352
+ tableCellEditorChildren.map((Child, index) => /* @__PURE__ */ jsx(Child, {}, index)),
353
+ /* @__PURE__ */ jsx(HistoryPlugin, {})
354
+ ] });
355
+ };
356
+ const ColumnEditor = ({
357
+ parentEditor,
358
+ highlightedCoordinates,
359
+ align,
360
+ lexicalTable,
361
+ colIndex,
362
+ setActiveCellWithBoundaries
363
+ }) => {
364
+ const [editorRootElementRef] = useCellValues(editorRootElementRef$);
365
+ const insertColumnAt = React.useCallback(
366
+ (colIndex2) => {
367
+ parentEditor.update(() => {
368
+ lexicalTable.insertColumnAt(colIndex2);
369
+ });
370
+ setActiveCellWithBoundaries([colIndex2, 0]);
371
+ },
372
+ [parentEditor, lexicalTable, setActiveCellWithBoundaries]
373
+ );
374
+ const deleteColumnAt = React.useCallback(
375
+ (colIndex2) => {
376
+ parentEditor.update(() => {
377
+ lexicalTable.deleteColumnAt(colIndex2);
378
+ });
379
+ },
380
+ [parentEditor, lexicalTable]
381
+ );
382
+ const setColumnAlign = React.useCallback(
383
+ (colIndex2, align2) => {
384
+ parentEditor.update(() => {
385
+ lexicalTable.setColumnAlign(colIndex2, align2);
386
+ });
387
+ },
388
+ [parentEditor, lexicalTable]
389
+ );
390
+ const t = useTranslation();
391
+ return /* @__PURE__ */ jsxs(RadixPopover.Root, { children: [
392
+ /* @__PURE__ */ jsx(
393
+ RadixPopover.PopoverTrigger,
394
+ {
395
+ className: styles.tableColumnEditorTrigger,
396
+ "data-active": highlightedCoordinates[0] === colIndex + 1,
397
+ title: t("table.columnMenu", "Column menu"),
398
+ children: /* @__PURE__ */ jsx(EditorIcon, { name: "more" })
399
+ }
400
+ ),
401
+ /* @__PURE__ */ jsx(RadixPopover.Portal, { container: editorRootElementRef?.current, children: /* @__PURE__ */ jsxs(
402
+ RadixPopover.PopoverContent,
403
+ {
404
+ className: classNames(styles.tableColumnEditorPopoverContent),
405
+ onOpenAutoFocus: (e) => {
406
+ e.preventDefault();
407
+ },
408
+ sideOffset: 5,
409
+ side: "top",
410
+ children: [
411
+ /* @__PURE__ */ jsxs(RadixToolbar.Root, { className: styles.tableColumnEditorToolbar, children: [
412
+ /* @__PURE__ */ jsxs(
413
+ RadixToolbar.ToggleGroup,
414
+ {
415
+ className: styles.toggleGroupRoot,
416
+ onValueChange: (value) => {
417
+ setColumnAlign(colIndex, value);
418
+ },
419
+ value: align ?? "left",
420
+ type: "single",
421
+ "aria-label": t("table.textAlignment", "Text alignment"),
422
+ children: [
423
+ /* @__PURE__ */ jsx(RadixToolbar.ToggleItem, { value: "left", title: t("table.alignLeft", "Align left"), children: /* @__PURE__ */ jsx(EditorIcon, { name: "align-left" }) }),
424
+ /* @__PURE__ */ jsx(RadixToolbar.ToggleItem, { value: "center", title: t("table.alignCenter", "Align center"), children: /* @__PURE__ */ jsx(EditorIcon, { name: "align-center" }) }),
425
+ /* @__PURE__ */ jsx(RadixToolbar.ToggleItem, { value: "right", title: t("table.alignRight", "Align right"), children: /* @__PURE__ */ jsx(EditorIcon, { name: "align-right" }) })
426
+ ]
427
+ }
428
+ ),
429
+ /* @__PURE__ */ jsx(RadixToolbar.Separator, {}),
430
+ /* @__PURE__ */ jsx(
431
+ RadixToolbar.Button,
432
+ {
433
+ onClick: insertColumnAt.bind(null, colIndex),
434
+ title: t("table.insertColumnLeft", "Insert a column to the left of this one"),
435
+ children: /* @__PURE__ */ jsx(EditorIcon, { name: "insert-column-left" })
436
+ }
437
+ ),
438
+ /* @__PURE__ */ jsx(
439
+ RadixToolbar.Button,
440
+ {
441
+ onClick: insertColumnAt.bind(null, colIndex + 1),
442
+ title: t("table.insertColumnRight", "Insert a column to the right of this one"),
443
+ children: /* @__PURE__ */ jsx(EditorIcon, { name: "insert-column-right" })
444
+ }
445
+ ),
446
+ /* @__PURE__ */ jsx(RadixToolbar.Button, { onClick: deleteColumnAt.bind(null, colIndex), title: t("table.deleteColumn", "Delete this column"), children: /* @__PURE__ */ jsx(EditorIcon, { name: "delete" }) })
447
+ ] }),
448
+ /* @__PURE__ */ jsx(RadixPopover.Arrow, { className: styles.popoverArrow })
449
+ ]
450
+ }
451
+ ) })
452
+ ] });
453
+ };
454
+ const RowEditor = ({
455
+ parentEditor,
456
+ highlightedCoordinates,
457
+ lexicalTable,
458
+ rowIndex,
459
+ setActiveCellWithBoundaries
460
+ }) => {
461
+ const [editorRootElementRef] = useCellValues(editorRootElementRef$);
462
+ const insertRowAt = React.useCallback(
463
+ (rowIndex2) => {
464
+ parentEditor.update(() => {
465
+ lexicalTable.insertRowAt(rowIndex2);
466
+ });
467
+ setActiveCellWithBoundaries([0, rowIndex2]);
468
+ },
469
+ [parentEditor, lexicalTable, setActiveCellWithBoundaries]
470
+ );
471
+ const deleteRowAt = React.useCallback(
472
+ (rowIndex2) => {
473
+ parentEditor.update(() => {
474
+ lexicalTable.deleteRowAt(rowIndex2);
475
+ });
476
+ },
477
+ [parentEditor, lexicalTable]
478
+ );
479
+ const t = useTranslation();
480
+ return /* @__PURE__ */ jsxs(RadixPopover.Root, { children: [
481
+ /* @__PURE__ */ jsx(
482
+ RadixPopover.PopoverTrigger,
483
+ {
484
+ className: styles.tableColumnEditorTrigger,
485
+ "data-active": highlightedCoordinates[1] === rowIndex,
486
+ title: t("table.rowMenu", "Row menu"),
487
+ children: /* @__PURE__ */ jsx(EditorIcon, { name: "more" })
488
+ }
489
+ ),
490
+ /* @__PURE__ */ jsx(RadixPopover.Portal, { container: editorRootElementRef?.current, children: /* @__PURE__ */ jsxs(
491
+ RadixPopover.PopoverContent,
492
+ {
493
+ className: classNames(styles.tableColumnEditorPopoverContent),
494
+ onOpenAutoFocus: (e) => {
495
+ e.preventDefault();
496
+ },
497
+ sideOffset: 5,
498
+ side: "bottom",
499
+ children: [
500
+ /* @__PURE__ */ jsxs(RadixToolbar.Root, { className: styles.tableColumnEditorToolbar, children: [
501
+ /* @__PURE__ */ jsx(
502
+ RadixToolbar.Button,
503
+ {
504
+ onClick: insertRowAt.bind(null, rowIndex),
505
+ title: t("table.insertRowAbove", "Insert a row above this one"),
506
+ children: /* @__PURE__ */ jsx(EditorIcon, { name: "insert-row-above" })
507
+ }
508
+ ),
509
+ /* @__PURE__ */ jsx(
510
+ RadixToolbar.Button,
511
+ {
512
+ onClick: insertRowAt.bind(null, rowIndex + 1),
513
+ title: t("table.insertRowBelow", "Insert a row below this one"),
514
+ children: /* @__PURE__ */ jsx(EditorIcon, { name: "insert-row-below" })
515
+ }
516
+ ),
517
+ /* @__PURE__ */ jsx(RadixToolbar.Button, { onClick: deleteRowAt.bind(null, rowIndex), title: t("table.deleteRow", "Delete this row"), children: /* @__PURE__ */ jsx(EditorIcon, { name: "delete" }) })
518
+ ] }),
519
+ /* @__PURE__ */ jsx(RadixPopover.Arrow, { className: styles.popoverArrow })
520
+ ]
521
+ }
522
+ ) })
523
+ ] });
524
+ };
525
+ export {
526
+ TableEditor
527
+ };