@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,70 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { DecoratorNode } from "lexical";
|
|
3
|
+
import { FrontmatterEditor } from "./FrontmatterEditor.js";
|
|
4
|
+
class FrontmatterNode extends DecoratorNode {
|
|
5
|
+
__yaml;
|
|
6
|
+
static getType() {
|
|
7
|
+
return "frontmatter";
|
|
8
|
+
}
|
|
9
|
+
static clone(node) {
|
|
10
|
+
return new FrontmatterNode(node.__yaml, node.__key);
|
|
11
|
+
}
|
|
12
|
+
static importJSON(serializedNode) {
|
|
13
|
+
const { yaml } = serializedNode;
|
|
14
|
+
const node = $createFrontmatterNode(yaml);
|
|
15
|
+
return node;
|
|
16
|
+
}
|
|
17
|
+
constructor(code, key) {
|
|
18
|
+
super(key);
|
|
19
|
+
this.__yaml = code;
|
|
20
|
+
}
|
|
21
|
+
exportJSON() {
|
|
22
|
+
return {
|
|
23
|
+
yaml: this.getYaml(),
|
|
24
|
+
type: "frontmatter",
|
|
25
|
+
version: 1
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
// View
|
|
29
|
+
createDOM(_config) {
|
|
30
|
+
return document.createElement("div");
|
|
31
|
+
}
|
|
32
|
+
updateDOM() {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
getYaml() {
|
|
36
|
+
return this.getLatest().__yaml;
|
|
37
|
+
}
|
|
38
|
+
setYaml(yaml) {
|
|
39
|
+
if (yaml !== this.__yaml) {
|
|
40
|
+
this.getWritable().__yaml = yaml;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
decorate(editor) {
|
|
44
|
+
return /* @__PURE__ */ jsx(
|
|
45
|
+
FrontmatterEditor,
|
|
46
|
+
{
|
|
47
|
+
yaml: this.getYaml(),
|
|
48
|
+
onChange: (yaml) => {
|
|
49
|
+
editor.update(() => {
|
|
50
|
+
this.setYaml(yaml);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
isKeyboardSelectable() {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function $createFrontmatterNode(yaml) {
|
|
61
|
+
return new FrontmatterNode(yaml);
|
|
62
|
+
}
|
|
63
|
+
function $isFrontmatterNode(node) {
|
|
64
|
+
return node instanceof FrontmatterNode;
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
$createFrontmatterNode,
|
|
68
|
+
$isFrontmatterNode,
|
|
69
|
+
FrontmatterNode
|
|
70
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { $isFrontmatterNode } from "./FrontmatterNode.js";
|
|
2
|
+
const LexicalFrontmatterVisitor = {
|
|
3
|
+
testLexicalNode: $isFrontmatterNode,
|
|
4
|
+
visitLexicalNode: ({ actions, lexicalNode }) => {
|
|
5
|
+
actions.addAndStepInto("yaml", { value: lexicalNode.getYaml() });
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
LexicalFrontmatterVisitor
|
|
10
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { $createFrontmatterNode } from "./FrontmatterNode.js";
|
|
2
|
+
const MdastFrontmatterVisitor = {
|
|
3
|
+
testNode: "yaml",
|
|
4
|
+
visitNode({ mdastNode, actions }) {
|
|
5
|
+
actions.addAndStepInto($createFrontmatterNode(mdastNode.value));
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
MdastFrontmatterVisitor
|
|
10
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { realmPlugin } from "../../RealmWithPlugins.js";
|
|
2
|
+
import { createRootEditorSubscription$, rootEditor$, addToMarkdownExtension$, addExportVisitor$, addImportVisitor$, addLexicalNode$, addSyntaxExtension$, addMdastExtension$ } from "../core/index.js";
|
|
3
|
+
import { Cell, Action, withLatestFrom } from "@mdxeditor/gurx";
|
|
4
|
+
import { $getRoot, KEY_DOWN_COMMAND, $getSelection, $isRangeSelection, $isTextNode, COMMAND_PRIORITY_CRITICAL } from "lexical";
|
|
5
|
+
import { frontmatterToMarkdown, frontmatterFromMarkdown } from "mdast-util-frontmatter";
|
|
6
|
+
import { frontmatter } from "micromark-extension-frontmatter";
|
|
7
|
+
import { $isFrontmatterNode, $createFrontmatterNode, FrontmatterNode } from "./FrontmatterNode.js";
|
|
8
|
+
import { LexicalFrontmatterVisitor } from "./LexicalFrontmatterVisitor.js";
|
|
9
|
+
import { MdastFrontmatterVisitor } from "./MdastFrontmatterVisitor.js";
|
|
10
|
+
const frontmatterDialogOpen$ = Cell(false);
|
|
11
|
+
const insertFrontmatter$ = Action((r) => {
|
|
12
|
+
r.sub(r.pipe(insertFrontmatter$, withLatestFrom(rootEditor$)), ([, rootEditor]) => {
|
|
13
|
+
rootEditor?.update(() => {
|
|
14
|
+
const firstItem = $getRoot().getFirstChild();
|
|
15
|
+
if (!$isFrontmatterNode(firstItem)) {
|
|
16
|
+
const fmNode = $createFrontmatterNode('"": ""');
|
|
17
|
+
if (firstItem) {
|
|
18
|
+
firstItem.insertBefore(fmNode);
|
|
19
|
+
} else {
|
|
20
|
+
$getRoot().append(fmNode);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
r.pub(frontmatterDialogOpen$, true);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
const removeFrontmatter$ = Action((r) => {
|
|
28
|
+
r.sub(r.pipe(removeFrontmatter$, withLatestFrom(rootEditor$)), ([, rootEditor]) => {
|
|
29
|
+
rootEditor?.update(() => {
|
|
30
|
+
const firstItem = $getRoot().getFirstChild();
|
|
31
|
+
if ($isFrontmatterNode(firstItem)) {
|
|
32
|
+
firstItem.remove();
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
r.pub(frontmatterDialogOpen$, false);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
const hasFrontmatter$ = Cell(false, (r) => {
|
|
39
|
+
r.pub(createRootEditorSubscription$, (rootEditor) => {
|
|
40
|
+
return rootEditor.registerUpdateListener(({ editorState }) => {
|
|
41
|
+
editorState.read(() => {
|
|
42
|
+
r.pub(hasFrontmatter$, $isFrontmatterNode($getRoot().getFirstChild()));
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
const frontmatterPlugin = realmPlugin({
|
|
48
|
+
init: (realm) => {
|
|
49
|
+
realm.pubIn({
|
|
50
|
+
[addMdastExtension$]: frontmatterFromMarkdown("yaml"),
|
|
51
|
+
[addSyntaxExtension$]: frontmatter(),
|
|
52
|
+
[addLexicalNode$]: FrontmatterNode,
|
|
53
|
+
[addImportVisitor$]: MdastFrontmatterVisitor,
|
|
54
|
+
[addExportVisitor$]: LexicalFrontmatterVisitor,
|
|
55
|
+
[addToMarkdownExtension$]: frontmatterToMarkdown("yaml"),
|
|
56
|
+
[createRootEditorSubscription$]: (editor) => {
|
|
57
|
+
return editor.registerCommand(
|
|
58
|
+
KEY_DOWN_COMMAND,
|
|
59
|
+
(event) => {
|
|
60
|
+
let shouldPrevent = false;
|
|
61
|
+
editor.read(() => {
|
|
62
|
+
const selection = $getSelection();
|
|
63
|
+
if ($isRangeSelection(selection)) {
|
|
64
|
+
if (selection.isCollapsed() && selection.anchor.offset === 0 && selection.focus.offset === 0 && event.key === "Backspace") {
|
|
65
|
+
let node = selection.getNodes()[0];
|
|
66
|
+
if ($isTextNode(node)) {
|
|
67
|
+
node = node.getParent();
|
|
68
|
+
}
|
|
69
|
+
const prevSibling = node?.getPreviousSibling();
|
|
70
|
+
if ($isFrontmatterNode(prevSibling)) {
|
|
71
|
+
shouldPrevent = true;
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
const firstNode = selection.getNodes()[0];
|
|
76
|
+
if ($isFrontmatterNode(firstNode)) {
|
|
77
|
+
const yaml = firstNode.getYaml();
|
|
78
|
+
setTimeout(() => {
|
|
79
|
+
editor.update(
|
|
80
|
+
() => {
|
|
81
|
+
const firstItem = $getRoot().getFirstChild();
|
|
82
|
+
if (!$isFrontmatterNode(firstItem)) {
|
|
83
|
+
$getRoot().splice(0, 0, [$createFrontmatterNode(yaml)]);
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
{ discrete: true }
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
if (shouldPrevent) {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
},
|
|
98
|
+
COMMAND_PRIORITY_CRITICAL
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
export {
|
|
105
|
+
$createFrontmatterNode,
|
|
106
|
+
$isFrontmatterNode,
|
|
107
|
+
FrontmatterNode,
|
|
108
|
+
frontmatterDialogOpen$,
|
|
109
|
+
frontmatterPlugin,
|
|
110
|
+
hasFrontmatter$,
|
|
111
|
+
insertFrontmatter$,
|
|
112
|
+
removeFrontmatter$
|
|
113
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { $isHeadingNode } from "@lexical/rich-text";
|
|
2
|
+
const LexicalHeadingVisitor = {
|
|
3
|
+
testLexicalNode: $isHeadingNode,
|
|
4
|
+
visitLexicalNode: ({ lexicalNode, actions }) => {
|
|
5
|
+
const depth = parseInt(lexicalNode.getTag()[1], 10);
|
|
6
|
+
actions.addAndStepInto("heading", { depth });
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
export {
|
|
10
|
+
LexicalHeadingVisitor
|
|
11
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { $createHeadingNode } from "@lexical/rich-text";
|
|
2
|
+
const MdastHeadingVisitor = {
|
|
3
|
+
testNode: "heading",
|
|
4
|
+
visitNode: function({ mdastNode, actions }) {
|
|
5
|
+
actions.addAndStepInto($createHeadingNode(`h${mdastNode.depth}`));
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
MdastHeadingVisitor
|
|
10
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { $createHeadingNode, HeadingNode } from "@lexical/rich-text";
|
|
2
|
+
import { Cell } from "@mdxeditor/gurx";
|
|
3
|
+
import { KEY_DOWN_COMMAND, $createParagraphNode, COMMAND_PRIORITY_LOW } from "lexical";
|
|
4
|
+
import { realmPlugin } from "../../RealmWithPlugins.js";
|
|
5
|
+
import { controlOrMeta } from "../../utils/detectMac.js";
|
|
6
|
+
import { createRootEditorSubscription$, convertSelectionToNode$, addExportVisitor$, addLexicalNode$, addImportVisitor$, addActivePlugin$ } from "../core/index.js";
|
|
7
|
+
import { LexicalHeadingVisitor } from "./LexicalHeadingVisitor.js";
|
|
8
|
+
import { MdastHeadingVisitor } from "./MdastHeadingVisitor.js";
|
|
9
|
+
const FORMATTING_KEYS = ["Digit0", "Digit1", "Digit2", "Digit3", "Digit4", "Digit5", "Digit6"];
|
|
10
|
+
const ALL_HEADING_LEVELS = [1, 2, 3, 4, 5, 6];
|
|
11
|
+
const CODE_TO_HEADING_LEVEL_MAP = {
|
|
12
|
+
Digit1: 1,
|
|
13
|
+
Digit2: 2,
|
|
14
|
+
Digit3: 3,
|
|
15
|
+
Digit4: 4,
|
|
16
|
+
Digit5: 5,
|
|
17
|
+
Digit6: 6
|
|
18
|
+
};
|
|
19
|
+
const allowedHeadingLevels$ = Cell(ALL_HEADING_LEVELS, (r) => {
|
|
20
|
+
r.pub(createRootEditorSubscription$, (theRootEditor) => {
|
|
21
|
+
return theRootEditor.registerCommand(
|
|
22
|
+
KEY_DOWN_COMMAND,
|
|
23
|
+
(event) => {
|
|
24
|
+
const { code, ctrlKey, metaKey, altKey } = event;
|
|
25
|
+
if (FORMATTING_KEYS.includes(code) && controlOrMeta(metaKey, ctrlKey) && altKey) {
|
|
26
|
+
event.preventDefault();
|
|
27
|
+
theRootEditor.update(() => {
|
|
28
|
+
if (code === "Digit0") {
|
|
29
|
+
r.pub(convertSelectionToNode$, () => $createParagraphNode());
|
|
30
|
+
} else {
|
|
31
|
+
const allowedHeadingLevels = r.getValue(allowedHeadingLevels$);
|
|
32
|
+
const requestedHeadingLevel = CODE_TO_HEADING_LEVEL_MAP[code];
|
|
33
|
+
if (allowedHeadingLevels.includes(requestedHeadingLevel)) {
|
|
34
|
+
r.pub(convertSelectionToNode$, () => $createHeadingNode(`h${requestedHeadingLevel}`));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
},
|
|
42
|
+
COMMAND_PRIORITY_LOW
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
const headingsPlugin = realmPlugin({
|
|
47
|
+
init(realm) {
|
|
48
|
+
realm.pubIn({
|
|
49
|
+
[addActivePlugin$]: "headings",
|
|
50
|
+
[addImportVisitor$]: MdastHeadingVisitor,
|
|
51
|
+
[addLexicalNode$]: HeadingNode,
|
|
52
|
+
[addExportVisitor$]: LexicalHeadingVisitor
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
update(realm, params) {
|
|
56
|
+
realm.pub(allowedHeadingLevels$, params?.allowedHeadingLevels ?? ALL_HEADING_LEVELS);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
export {
|
|
60
|
+
ALL_HEADING_LEVELS,
|
|
61
|
+
allowedHeadingLevels$,
|
|
62
|
+
headingsPlugin
|
|
63
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { jsxs, jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
3
|
+
import { useCellValues, usePublisher } from "@mdxeditor/gurx";
|
|
4
|
+
import classNames from "classnames";
|
|
5
|
+
import { $getNodeByKey } from "lexical";
|
|
6
|
+
import { disableImageSettingsButton$, openEditImageDialog$, parseImageDimension } from "./index.js";
|
|
7
|
+
import styles from "../../styles/ui.module.css.js";
|
|
8
|
+
import { iconComponentFor$, readOnly$, useTranslation } from "../core/index.js";
|
|
9
|
+
function EditImageToolbar(props) {
|
|
10
|
+
const { nodeKey, imageSource, initialImagePath, title, alt, width, height } = props;
|
|
11
|
+
const [disableImageSettingsButton, iconComponentFor, readOnly] = useCellValues(disableImageSettingsButton$, iconComponentFor$, readOnly$);
|
|
12
|
+
const [editor] = useLexicalComposerContext();
|
|
13
|
+
const openEditImageDialog = usePublisher(openEditImageDialog$);
|
|
14
|
+
const t = useTranslation();
|
|
15
|
+
return /* @__PURE__ */ jsxs("div", { className: styles.editImageToolbar, children: [
|
|
16
|
+
/* @__PURE__ */ jsx(
|
|
17
|
+
"button",
|
|
18
|
+
{
|
|
19
|
+
className: styles.iconButton,
|
|
20
|
+
type: "button",
|
|
21
|
+
title: t("imageEditor.deleteImage", "Delete image"),
|
|
22
|
+
disabled: readOnly,
|
|
23
|
+
onClick: (e) => {
|
|
24
|
+
e.preventDefault();
|
|
25
|
+
editor.update(() => {
|
|
26
|
+
$getNodeByKey(nodeKey)?.remove();
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
children: iconComponentFor("delete_small")
|
|
30
|
+
}
|
|
31
|
+
),
|
|
32
|
+
!disableImageSettingsButton && /* @__PURE__ */ jsx(
|
|
33
|
+
"button",
|
|
34
|
+
{
|
|
35
|
+
type: "button",
|
|
36
|
+
className: classNames(styles.iconButton, styles.editImageButton),
|
|
37
|
+
title: t("imageEditor.editImage", "Edit image"),
|
|
38
|
+
disabled: readOnly,
|
|
39
|
+
onClick: () => {
|
|
40
|
+
openEditImageDialog({
|
|
41
|
+
nodeKey,
|
|
42
|
+
initialValues: {
|
|
43
|
+
src: initialImagePath ?? imageSource,
|
|
44
|
+
title,
|
|
45
|
+
altText: alt,
|
|
46
|
+
width: parseImageDimension(width),
|
|
47
|
+
height: parseImageDimension(height)
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
children: iconComponentFor("settings")
|
|
52
|
+
}
|
|
53
|
+
)
|
|
54
|
+
] });
|
|
55
|
+
}
|
|
56
|
+
export {
|
|
57
|
+
EditImageToolbar
|
|
58
|
+
};
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import * as Dialog from "@radix-ui/react-dialog";
|
|
3
|
+
import classNames from "classnames";
|
|
4
|
+
import { useForm } from "react-hook-form";
|
|
5
|
+
import styles from "../../styles/ui.module.css.js";
|
|
6
|
+
import { editorRootElementRef$, useTranslation } from "../core/index.js";
|
|
7
|
+
import { imageAutocompleteSuggestions$, imageDialogState$, imageUploadHandler$, allowSetImageDimensions$, saveImage$, closeImageDialog$ } from "./index.js";
|
|
8
|
+
import { DownshiftAutoComplete } from "../core/ui/DownshiftAutoComplete.js";
|
|
9
|
+
import { useCellValues, usePublisher } from "@mdxeditor/gurx";
|
|
10
|
+
const ImageDialog = () => {
|
|
11
|
+
const [imageAutocompleteSuggestions, state, editorRootElementRef, imageUploadHandler, allowSetImageDimensions] = useCellValues(
|
|
12
|
+
imageAutocompleteSuggestions$,
|
|
13
|
+
imageDialogState$,
|
|
14
|
+
editorRootElementRef$,
|
|
15
|
+
imageUploadHandler$,
|
|
16
|
+
allowSetImageDimensions$
|
|
17
|
+
);
|
|
18
|
+
const saveImage = usePublisher(saveImage$);
|
|
19
|
+
const closeImageDialog = usePublisher(closeImageDialog$);
|
|
20
|
+
const t = useTranslation();
|
|
21
|
+
const { register, handleSubmit, control, setValue, reset } = useForm({
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
23
|
+
values: state.type === "editing" ? state.initialValues : {}
|
|
24
|
+
});
|
|
25
|
+
const resetFormState = () => {
|
|
26
|
+
reset({ src: "", title: "", altText: "", width: void 0, height: void 0 });
|
|
27
|
+
};
|
|
28
|
+
if (state.type === "inactive") return null;
|
|
29
|
+
return /* @__PURE__ */ jsx(
|
|
30
|
+
Dialog.Root,
|
|
31
|
+
{
|
|
32
|
+
open: true,
|
|
33
|
+
onOpenChange: (open) => {
|
|
34
|
+
if (!open) {
|
|
35
|
+
closeImageDialog();
|
|
36
|
+
resetFormState();
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
children: /* @__PURE__ */ jsxs(Dialog.Portal, { container: editorRootElementRef?.current, children: [
|
|
40
|
+
/* @__PURE__ */ jsx(Dialog.Overlay, { className: styles.dialogOverlay }),
|
|
41
|
+
/* @__PURE__ */ jsxs(
|
|
42
|
+
Dialog.Content,
|
|
43
|
+
{
|
|
44
|
+
className: styles.dialogContent,
|
|
45
|
+
onOpenAutoFocus: (e) => {
|
|
46
|
+
e.preventDefault();
|
|
47
|
+
},
|
|
48
|
+
children: [
|
|
49
|
+
/* @__PURE__ */ jsx(Dialog.Title, { children: t("uploadImage.dialogTitle", "Upload an image") }),
|
|
50
|
+
/* @__PURE__ */ jsxs(
|
|
51
|
+
"form",
|
|
52
|
+
{
|
|
53
|
+
onSubmit: async (e) => {
|
|
54
|
+
e.preventDefault();
|
|
55
|
+
e.stopPropagation();
|
|
56
|
+
await handleSubmit(saveImage)(e);
|
|
57
|
+
resetFormState();
|
|
58
|
+
},
|
|
59
|
+
className: styles.multiFieldForm,
|
|
60
|
+
children: [
|
|
61
|
+
imageUploadHandler === null ? /* @__PURE__ */ jsx("input", { type: "hidden", accept: "image/*", ...register("file") }) : /* @__PURE__ */ jsxs("div", { className: styles.formField, children: [
|
|
62
|
+
/* @__PURE__ */ jsx("label", { htmlFor: "file", children: t("uploadImage.uploadInstructions", "Upload an image from your device:") }),
|
|
63
|
+
/* @__PURE__ */ jsx("input", { type: "file", accept: "image/*", ...register("file") })
|
|
64
|
+
] }),
|
|
65
|
+
/* @__PURE__ */ jsxs("div", { className: styles.formField, children: [
|
|
66
|
+
/* @__PURE__ */ jsx("label", { htmlFor: "src", children: imageUploadHandler !== null ? t("uploadImage.addViaUrlInstructions", "Or add an image from an URL:") : t("uploadImage.addViaUrlInstructionsNoUpload", "Add an image from an URL:") }),
|
|
67
|
+
/* @__PURE__ */ jsx(
|
|
68
|
+
DownshiftAutoComplete,
|
|
69
|
+
{
|
|
70
|
+
register,
|
|
71
|
+
initialInputValue: state.type === "editing" ? state.initialValues.src ?? "" : "",
|
|
72
|
+
inputName: "src",
|
|
73
|
+
suggestions: imageAutocompleteSuggestions,
|
|
74
|
+
setValue,
|
|
75
|
+
control,
|
|
76
|
+
placeholder: t("uploadImage.autoCompletePlaceholder", "Select or paste an image src")
|
|
77
|
+
}
|
|
78
|
+
)
|
|
79
|
+
] }),
|
|
80
|
+
/* @__PURE__ */ jsxs("div", { className: styles.formField, children: [
|
|
81
|
+
/* @__PURE__ */ jsx("label", { htmlFor: "alt", children: t("uploadImage.alt", "Alt:") }),
|
|
82
|
+
/* @__PURE__ */ jsx("input", { type: "text", ...register("altText"), className: styles.textInput })
|
|
83
|
+
] }),
|
|
84
|
+
/* @__PURE__ */ jsxs("div", { className: styles.formField, children: [
|
|
85
|
+
/* @__PURE__ */ jsx("label", { htmlFor: "title", children: t("uploadImage.title", "Title:") }),
|
|
86
|
+
/* @__PURE__ */ jsx("input", { type: "text", ...register("title"), className: styles.textInput })
|
|
87
|
+
] }),
|
|
88
|
+
allowSetImageDimensions && /* @__PURE__ */ jsxs("div", { className: styles.imageDimensionsContainer, children: [
|
|
89
|
+
/* @__PURE__ */ jsxs("div", { className: styles.formField, children: [
|
|
90
|
+
/* @__PURE__ */ jsx("label", { htmlFor: "width", children: t("uploadImage.width", "Width:") }),
|
|
91
|
+
/* @__PURE__ */ jsx("input", { type: "number", min: 0, ...register("width"), className: styles.textInput })
|
|
92
|
+
] }),
|
|
93
|
+
/* @__PURE__ */ jsxs("div", { className: styles.formField, children: [
|
|
94
|
+
/* @__PURE__ */ jsx("label", { htmlFor: "height", children: t("uploadImage.height", "Height:") }),
|
|
95
|
+
/* @__PURE__ */ jsx("input", { type: "number", min: 0, ...register("height"), className: styles.textInput })
|
|
96
|
+
] })
|
|
97
|
+
] }),
|
|
98
|
+
/* @__PURE__ */ jsxs("div", { style: { display: "flex", justifyContent: "flex-end", gap: "var(--spacing-2)" }, children: [
|
|
99
|
+
/* @__PURE__ */ jsx(
|
|
100
|
+
"button",
|
|
101
|
+
{
|
|
102
|
+
type: "submit",
|
|
103
|
+
title: t("dialogControls.save", "Save"),
|
|
104
|
+
"aria-label": t("dialogControls.save", "Save"),
|
|
105
|
+
className: classNames(styles.primaryButton),
|
|
106
|
+
children: t("dialogControls.save", "Save")
|
|
107
|
+
}
|
|
108
|
+
),
|
|
109
|
+
/* @__PURE__ */ jsx(Dialog.Close, { asChild: true, children: /* @__PURE__ */ jsx(
|
|
110
|
+
"button",
|
|
111
|
+
{
|
|
112
|
+
type: "reset",
|
|
113
|
+
title: t("dialogControls.cancel", "Cancel"),
|
|
114
|
+
"aria-label": t("dialogControls.cancel", "Cancel"),
|
|
115
|
+
className: classNames(styles.secondaryButton),
|
|
116
|
+
children: t("dialogControls.cancel", "Cancel")
|
|
117
|
+
}
|
|
118
|
+
) })
|
|
119
|
+
] })
|
|
120
|
+
]
|
|
121
|
+
}
|
|
122
|
+
)
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
)
|
|
126
|
+
] })
|
|
127
|
+
}
|
|
128
|
+
);
|
|
129
|
+
};
|
|
130
|
+
export {
|
|
131
|
+
ImageDialog
|
|
132
|
+
};
|