@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.
- package/LICENSE +21 -0
- package/README.md +86 -0
- package/UPSTREAM.md +21 -0
- package/dist/EditorIcon.js +75 -0
- package/dist/FormatConstants.js +20 -0
- package/dist/MDXEditor.js +189 -0
- package/dist/MarkdownEditor.js +281 -0
- package/dist/PersistentMarkdownEditor.js +358 -0
- package/dist/RealmWithPlugins.js +35 -0
- package/dist/core.d.ts +3232 -0
- package/dist/core.js +354 -0
- package/dist/defaultSvgIcons.js +371 -0
- package/dist/directive-editors/AdmonitionDirectiveDescriptor.js +28 -0
- package/dist/directive-editors/GenericDirectiveEditor.js +37 -0
- package/dist/exportMarkdownFromLexical.js +262 -0
- package/dist/horizontalRuleShortcut.js +37 -0
- package/dist/importMarkdownToLexical.js +172 -0
- package/dist/index.d.ts +86 -0
- package/dist/index.js +8 -0
- package/dist/jsx-editors/GenericJsxEditor.js +84 -0
- package/dist/mdastUtilHtmlComment.js +125 -0
- package/dist/persistence.d.ts +128 -0
- package/dist/persistence.js +4 -0
- package/dist/plugins/codeblock/CodeBlockNode.js +183 -0
- package/dist/plugins/codeblock/CodeBlockVisitor.js +14 -0
- package/dist/plugins/codeblock/MdastCodeVisitor.js +23 -0
- package/dist/plugins/codeblock/findCodeBlockDescriptor.js +8 -0
- package/dist/plugins/codeblock/index.js +46 -0
- package/dist/plugins/codemirror/CodeMirrorEditor.js +145 -0
- package/dist/plugins/codemirror/index.js +115 -0
- package/dist/plugins/codemirror/useCodeMirrorRef.js +101 -0
- package/dist/plugins/core/GenericHTMLNode.js +118 -0
- package/dist/plugins/core/LexicalGenericHTMLNodeVisitor.js +15 -0
- package/dist/plugins/core/LexicalLinebreakVisitor.js +10 -0
- package/dist/plugins/core/LexicalParagraphVisitor.js +10 -0
- package/dist/plugins/core/LexicalRootVisitor.js +10 -0
- package/dist/plugins/core/LexicalTextVisitor.js +160 -0
- package/dist/plugins/core/MdastBreakVisitor.js +10 -0
- package/dist/plugins/core/MdastFormattingVisitor.js +81 -0
- package/dist/plugins/core/MdastHTMLNode.js +120 -0
- package/dist/plugins/core/MdastHTMLVisitor.js +17 -0
- package/dist/plugins/core/MdastParagraphVisitor.js +23 -0
- package/dist/plugins/core/MdastRootVisitor.js +9 -0
- package/dist/plugins/core/MdastTextVisitor.js +16 -0
- package/dist/plugins/core/NestedLexicalEditor.js +221 -0
- package/dist/plugins/core/PropertyPopover.js +75 -0
- package/dist/plugins/core/SharedHistoryPlugin.js +10 -0
- package/dist/plugins/core/index.js +692 -0
- package/dist/plugins/core/ui/DownshiftAutoComplete.js +89 -0
- package/dist/plugins/core/ui/PopoverUtils.js +22 -0
- package/dist/plugins/diff-source/DiffSourceWrapper.js +24 -0
- package/dist/plugins/diff-source/DiffViewer.js +84 -0
- package/dist/plugins/diff-source/SourceEditor.js +60 -0
- package/dist/plugins/diff-source/index.js +27 -0
- package/dist/plugins/directives/DirectiveNode.js +107 -0
- package/dist/plugins/directives/DirectiveVisitor.js +10 -0
- package/dist/plugins/directives/MdastDirectiveVisitor.js +30 -0
- package/dist/plugins/directives/index.js +45 -0
- package/dist/plugins/frontmatter/FrontmatterEditor.js +137 -0
- package/dist/plugins/frontmatter/FrontmatterNode.js +70 -0
- package/dist/plugins/frontmatter/LexicalFrontmatterVisitor.js +10 -0
- package/dist/plugins/frontmatter/MdastFrontmatterVisitor.js +10 -0
- package/dist/plugins/frontmatter/index.js +113 -0
- package/dist/plugins/headings/LexicalHeadingVisitor.js +11 -0
- package/dist/plugins/headings/MdastHeadingVisitor.js +10 -0
- package/dist/plugins/headings/index.js +63 -0
- package/dist/plugins/image/EditImageToolbar.js +58 -0
- package/dist/plugins/image/ImageDialog.js +132 -0
- package/dist/plugins/image/ImageEditor.js +279 -0
- package/dist/plugins/image/ImageNode.js +187 -0
- package/dist/plugins/image/ImagePlaceholder.js +9 -0
- package/dist/plugins/image/ImageResizer.js +223 -0
- package/dist/plugins/image/LexicalImageVisitor.js +42 -0
- package/dist/plugins/image/MdastImageVisitor.js +91 -0
- package/dist/plugins/image/index.js +364 -0
- package/dist/plugins/jsx/LexicalJsxNode.js +103 -0
- package/dist/plugins/jsx/LexicalJsxVisitor.js +27 -0
- package/dist/plugins/jsx/LexicalMdxExpressionNode.js +130 -0
- package/dist/plugins/jsx/LexicalMdxExpressionVisitor.js +14 -0
- package/dist/plugins/jsx/MdastMdxExpressionVisitor.js +11 -0
- package/dist/plugins/jsx/MdastMdxJsEsmVisitor.js +8 -0
- package/dist/plugins/jsx/MdastMdxJsxElementVisitor.js +28 -0
- package/dist/plugins/jsx/index.js +97 -0
- package/dist/plugins/jsx/jsxTagName.js +7 -0
- package/dist/plugins/link/AutoLinkPlugin.js +18 -0
- package/dist/plugins/link/LexicalLinkVisitor.js +10 -0
- package/dist/plugins/link/MdastLinkVisitor.js +14 -0
- package/dist/plugins/link/index.js +34 -0
- package/dist/plugins/link-dialog/LinkDialog.js +262 -0
- package/dist/plugins/link-dialog/index.js +304 -0
- package/dist/plugins/lists/CheckListPlugin.js +270 -0
- package/dist/plugins/lists/LexicalListItemVisitor.js +41 -0
- package/dist/plugins/lists/LexicalListVisitor.js +13 -0
- package/dist/plugins/lists/MdastListItemVisitor.js +11 -0
- package/dist/plugins/lists/MdastListVisitor.js +19 -0
- package/dist/plugins/lists/NotesListItemNode.js +22 -0
- package/dist/plugins/lists/index.js +111 -0
- package/dist/plugins/markdown-shortcut/index.js +114 -0
- package/dist/plugins/maxlength/index.js +36 -0
- package/dist/plugins/quote/LexicalQuoteVisitor.js +10 -0
- package/dist/plugins/quote/MdastBlockQuoteVisitor.js +10 -0
- package/dist/plugins/quote/index.js +18 -0
- package/dist/plugins/remote/index.js +52 -0
- package/dist/plugins/search/index.js +360 -0
- package/dist/plugins/table/LexicalTableVisitor.js +10 -0
- package/dist/plugins/table/MdastTableVisitor.js +10 -0
- package/dist/plugins/table/TableEditor.js +527 -0
- package/dist/plugins/table/TableNode.js +208 -0
- package/dist/plugins/table/index.js +66 -0
- package/dist/plugins/thematic-break/LexicalThematicBreakVisitor.js +10 -0
- package/dist/plugins/thematic-break/MdastThematicBreakVisitor.js +10 -0
- package/dist/plugins/thematic-break/index.js +27 -0
- package/dist/plugins/toolbar/components/BlockTypeSelect.js +62 -0
- package/dist/plugins/toolbar/components/BoldItalicUnderlineToggles.js +98 -0
- package/dist/plugins/toolbar/components/ChangeAdmonitionType.js +43 -0
- package/dist/plugins/toolbar/components/ChangeCodeMirrorLanguage.js +42 -0
- package/dist/plugins/toolbar/components/CodeToggle.js +21 -0
- package/dist/plugins/toolbar/components/CreateLink.js +24 -0
- package/dist/plugins/toolbar/components/DiffSourceToggleWrapper.js +42 -0
- package/dist/plugins/toolbar/components/HighlightToggle.js +28 -0
- package/dist/plugins/toolbar/components/InsertAdmonition.js +34 -0
- package/dist/plugins/toolbar/components/InsertCodeBlock.js +23 -0
- package/dist/plugins/toolbar/components/InsertFrontmatter.js +28 -0
- package/dist/plugins/toolbar/components/InsertImage.js +29 -0
- package/dist/plugins/toolbar/components/InsertTable.js +25 -0
- package/dist/plugins/toolbar/components/InsertThematicBreak.js +23 -0
- package/dist/plugins/toolbar/components/KitchenSinkToolbar.js +82 -0
- package/dist/plugins/toolbar/components/ListsToggle.js +29 -0
- package/dist/plugins/toolbar/components/UndoRedo.js +60 -0
- package/dist/plugins/toolbar/index.js +32 -0
- package/dist/plugins/toolbar/primitives/DialogButton.js +130 -0
- package/dist/plugins/toolbar/primitives/TooltipWrap.js +17 -0
- package/dist/plugins/toolbar/primitives/select.js +76 -0
- package/dist/plugins/toolbar/primitives/toolbar.js +144 -0
- package/dist/registerCodeBoundaryEscape.js +40 -0
- package/dist/styles/lexical-theme.module.css.js +62 -0
- package/dist/styles/lexicalTheme.js +32 -0
- package/dist/styles/ui.module.css.js +296 -0
- package/dist/styles.css +2838 -0
- package/dist/utils/detectMac.js +16 -0
- package/dist/utils/fp.js +44 -0
- package/dist/utils/isPartOftheEditorUI.js +12 -0
- package/dist/utils/lexicalHelpers.js +185 -0
- package/dist/utils/makeHslTransparent.js +6 -0
- package/dist/utils/mergeStyleAttributes.js +22 -0
- package/dist/utils/uuid4.js +10 -0
- package/dist/utils/voidEmitter.js +15 -0
- 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
|
+
};
|