@mdxeditor/editor 0.0.1 → 0.0.4-development
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 +2 -2
- package/README.md +15 -75
- package/dist/FormatConstants.d.ts +9 -0
- package/dist/FormatConstants.js +10 -0
- package/dist/MDXEditor.d.ts +103 -0
- package/dist/MDXEditor.js +144 -0
- package/dist/directive-editors/AdmonitionDirectiveDescriptor.d.ts +17 -0
- package/dist/directive-editors/AdmonitionDirectiveDescriptor.js +28 -0
- package/dist/directive-editors/GenericDirectiveEditor.d.ts +8 -0
- package/dist/directive-editors/GenericDirectiveEditor.js +32 -0
- package/dist/exportMarkdownFromLexical.d.ts +138 -0
- package/dist/exportMarkdownFromLexical.js +239 -0
- package/dist/gurx/index.d.ts +3 -0
- package/dist/gurx/react.d.ts +152 -0
- package/dist/gurx/react.js +206 -0
- package/dist/gurx/realm.d.ts +148 -0
- package/dist/gurx/realm.js +489 -0
- package/dist/gurx/realmFactory.d.ts +79 -0
- package/dist/gurx/realmFactory.js +32 -0
- package/dist/icons/add_column.svg.js +5 -0
- package/dist/icons/add_photo.svg.js +5 -0
- package/dist/icons/add_row.svg.js +5 -0
- package/dist/icons/admonition.svg.js +5 -0
- package/dist/icons/arrow_drop_down.svg.js +5 -0
- package/dist/icons/check.svg.js +5 -0
- package/dist/icons/close.svg.js +5 -0
- package/dist/icons/code.svg.js +5 -0
- package/dist/icons/content_copy.svg.js +5 -0
- package/dist/icons/delete_big.svg.js +5 -0
- package/dist/icons/delete_small.svg.js +5 -0
- package/dist/icons/difference.svg.js +5 -0
- package/dist/icons/edit.svg.js +5 -0
- package/dist/icons/format_align_center.svg.js +5 -0
- package/dist/icons/format_align_left.svg.js +5 -0
- package/dist/icons/format_align_right.svg.js +5 -0
- package/dist/icons/format_bold.svg.js +5 -0
- package/dist/icons/format_italic.svg.js +5 -0
- package/dist/icons/format_list_bulleted.svg.js +5 -0
- package/dist/icons/format_list_checked.svg.js +5 -0
- package/dist/icons/format_list_numbered.svg.js +5 -0
- package/dist/icons/format_underlined.svg.js +5 -0
- package/dist/icons/frame_source.svg.js +5 -0
- package/dist/icons/frontmatter.svg.js +5 -0
- package/dist/icons/horizontal_rule.svg.js +5 -0
- package/dist/icons/insert_col_left.svg.js +5 -0
- package/dist/icons/insert_col_right.svg.js +5 -0
- package/dist/icons/insert_row_above.svg.js +5 -0
- package/dist/icons/insert_row_below.svg.js +5 -0
- package/dist/icons/link.svg.js +5 -0
- package/dist/icons/link_off.svg.js +5 -0
- package/dist/icons/markdown.svg.js +5 -0
- package/dist/icons/more_horiz.svg.js +5 -0
- package/dist/icons/more_vert.svg.js +5 -0
- package/dist/icons/open_in_new.svg.js +5 -0
- package/dist/icons/redo.svg.js +5 -0
- package/dist/icons/rich_text.svg.js +5 -0
- package/dist/icons/sandpack.svg.js +5 -0
- package/dist/icons/settings.svg.js +5 -0
- package/dist/icons/table.svg.js +5 -0
- package/dist/icons/undo.svg.js +5 -0
- package/dist/importMarkdownToLexical.d.ts +101 -0
- package/dist/importMarkdownToLexical.js +93 -0
- package/dist/index.d.ts +62 -726
- package/dist/index.js +207 -4501
- package/dist/jsx-editors/GenericJsxEditor.d.ts +8 -0
- package/dist/jsx-editors/GenericJsxEditor.js +56 -0
- package/dist/plugins/codeblock/CodeBlockNode.d.ts +100 -0
- package/dist/plugins/codeblock/CodeBlockNode.js +158 -0
- package/dist/plugins/codeblock/CodeBlockVisitor.d.ts +4 -0
- package/dist/plugins/codeblock/CodeBlockVisitor.js +14 -0
- package/dist/plugins/codeblock/MdastCodeVisitor.d.ts +3 -0
- package/dist/plugins/codeblock/MdastCodeVisitor.js +16 -0
- package/dist/plugins/codeblock/index.d.ts +787 -0
- package/dist/plugins/codeblock/index.js +73 -0
- package/dist/plugins/codemirror/CodeMirrorEditor.d.ts +3 -0
- package/dist/plugins/codemirror/CodeMirrorEditor.js +42 -0
- package/dist/plugins/codemirror/index.d.ts +1038 -0
- package/dist/plugins/codemirror/index.js +62 -0
- package/dist/plugins/core/GenericHTMLNode.d.ts +38 -0
- package/dist/plugins/core/GenericHTMLNode.js +117 -0
- package/dist/plugins/core/Icon.d.ts +6 -0
- package/dist/plugins/core/Icon.js +92 -0
- package/dist/plugins/core/LexicalGenericHTMLNodeVisitor.d.ts +4 -0
- package/dist/plugins/core/LexicalGenericHTMLNodeVisitor.js +15 -0
- package/dist/plugins/core/LexicalLinebreakVisitor.d.ts +4 -0
- package/dist/plugins/core/LexicalLinebreakVisitor.js +10 -0
- package/dist/plugins/core/LexicalParagraphVisitor.d.ts +4 -0
- package/dist/plugins/core/LexicalParagraphVisitor.js +10 -0
- package/dist/plugins/core/LexicalRootVisitor.d.ts +4 -0
- package/dist/plugins/core/LexicalRootVisitor.js +10 -0
- package/dist/plugins/core/LexicalTextVisitor.d.ts +5 -0
- package/dist/plugins/core/LexicalTextVisitor.js +102 -0
- package/dist/plugins/core/MdastBreakVisitor.d.ts +3 -0
- package/dist/plugins/core/MdastBreakVisitor.js +10 -0
- package/dist/plugins/core/MdastFormattingVisitor.d.ts +4 -0
- package/dist/plugins/core/MdastFormattingVisitor.js +36 -0
- package/dist/plugins/core/MdastHTMLNode.d.ts +13 -0
- package/dist/plugins/core/MdastHTMLNode.js +122 -0
- package/dist/plugins/core/MdastHTMLVisitor.d.ts +3 -0
- package/dist/plugins/core/MdastHTMLVisitor.js +12 -0
- package/dist/plugins/core/MdastInlineCodeVisitor.d.ts +3 -0
- package/dist/plugins/core/MdastInlineCodeVisitor.js +27 -0
- package/dist/plugins/core/MdastParagraphVisitor.d.ts +3 -0
- package/dist/plugins/core/MdastParagraphVisitor.js +15 -0
- package/dist/plugins/core/MdastRootVisitor.d.ts +3 -0
- package/dist/plugins/core/MdastRootVisitor.js +9 -0
- package/dist/plugins/core/MdastTextVisitor.d.ts +3 -0
- package/dist/plugins/core/MdastTextVisitor.js +10 -0
- package/dist/plugins/core/NestedLexicalEditor.d.ts +92 -0
- package/dist/plugins/core/NestedLexicalEditor.js +185 -0
- package/dist/plugins/core/PropertyPopover.d.ts +23 -0
- package/dist/plugins/core/PropertyPopover.js +39 -0
- package/dist/plugins/core/SharedHistoryPlugin.d.ts +2 -0
- package/dist/plugins/core/SharedHistoryPlugin.js +10 -0
- package/dist/plugins/core/index.d.ts +583 -0
- package/dist/plugins/core/index.js +490 -0
- package/dist/plugins/core/ui/DownshiftAutoComplete.d.ts +15 -0
- package/dist/plugins/core/ui/DownshiftAutoComplete.js +83 -0
- package/dist/plugins/core/ui/PopoverUtils.d.ts +4 -0
- package/dist/plugins/core/ui/PopoverUtils.js +17 -0
- package/dist/plugins/diff-source/DiffSourceWrapper.d.ts +4 -0
- package/dist/plugins/diff-source/DiffSourceWrapper.js +14 -0
- package/dist/plugins/diff-source/DiffViewer.d.ts +2 -0
- package/dist/plugins/diff-source/DiffViewer.js +59 -0
- package/dist/plugins/diff-source/SourceEditor.d.ts +4 -0
- package/dist/plugins/diff-source/SourceEditor.js +53 -0
- package/dist/plugins/diff-source/index.d.ts +725 -0
- package/dist/plugins/diff-source/index.js +56 -0
- package/dist/plugins/directives/DirectiveNode.d.ts +51 -0
- package/dist/plugins/directives/DirectiveNode.js +92 -0
- package/dist/plugins/directives/DirectiveVisitor.d.ts +4 -0
- package/dist/plugins/directives/DirectiveVisitor.js +10 -0
- package/dist/plugins/directives/MdastDirectiveVisitor.d.ts +3 -0
- package/dist/plugins/directives/MdastDirectiveVisitor.js +13 -0
- package/dist/plugins/directives/index.d.ts +773 -0
- package/dist/plugins/directives/index.js +53 -0
- package/dist/plugins/frontmatter/FrontmatterEditor.d.ts +6 -0
- package/dist/plugins/frontmatter/FrontmatterEditor.js +85 -0
- package/dist/plugins/frontmatter/FrontmatterNode.d.ts +35 -0
- package/dist/plugins/frontmatter/FrontmatterNode.js +71 -0
- package/dist/plugins/frontmatter/LexicalFrontmatterVisitor.d.ts +4 -0
- package/dist/plugins/frontmatter/LexicalFrontmatterVisitor.js +10 -0
- package/dist/plugins/frontmatter/MdastFrontmatterVisitor.d.ts +3 -0
- package/dist/plugins/frontmatter/MdastFrontmatterVisitor.js +10 -0
- package/dist/plugins/frontmatter/index.d.ts +717 -0
- package/dist/plugins/frontmatter/index.js +76 -0
- package/dist/plugins/headings/LexicalHeadingVisitor.d.ts +4 -0
- package/dist/plugins/headings/LexicalHeadingVisitor.js +11 -0
- package/dist/plugins/headings/MdastHeadingVisitor.d.ts +3 -0
- package/dist/plugins/headings/MdastHeadingVisitor.js +10 -0
- package/dist/plugins/headings/index.d.ts +707 -0
- package/dist/plugins/headings/index.js +69 -0
- package/dist/plugins/image/ImageDialog.d.ts +2 -0
- package/dist/plugins/image/ImageDialog.js +70 -0
- package/dist/plugins/image/ImageEditor.d.ts +10 -0
- package/dist/plugins/image/ImageEditor.js +227 -0
- package/dist/plugins/image/ImageNode.d.ts +65 -0
- package/dist/plugins/image/ImageNode.js +152 -0
- package/dist/plugins/image/ImageResizer.d.ts +18 -0
- package/dist/plugins/image/ImageResizer.js +214 -0
- package/dist/plugins/image/LexicalImageVisitor.d.ts +4 -0
- package/dist/plugins/image/LexicalImageVisitor.js +35 -0
- package/dist/plugins/image/MdastImageVisitor.d.ts +6 -0
- package/dist/plugins/image/MdastImageVisitor.js +87 -0
- package/dist/plugins/image/index.d.ts +804 -0
- package/dist/plugins/image/index.js +300 -0
- package/dist/plugins/jsx/LexicalJsxNode.d.ts +54 -0
- package/dist/plugins/jsx/LexicalJsxNode.js +102 -0
- package/dist/plugins/jsx/LexicalJsxVisitor.d.ts +4 -0
- package/dist/plugins/jsx/LexicalJsxVisitor.js +24 -0
- package/dist/plugins/jsx/MdastMdxJsEsmVisitor.d.ts +3 -0
- package/dist/plugins/jsx/MdastMdxJsEsmVisitor.js +8 -0
- package/dist/plugins/jsx/MdastMdxJsxElementVisitor.d.ts +3 -0
- package/dist/plugins/jsx/MdastMdxJsxElementVisitor.js +12 -0
- package/dist/plugins/jsx/index.d.ts +779 -0
- package/dist/plugins/jsx/index.js +82 -0
- package/dist/plugins/link/AutoLinkPlugin.d.ts +9 -0
- package/dist/plugins/link/AutoLinkPlugin.js +18 -0
- package/dist/plugins/link/LexicalLinkVisitor.d.ts +4 -0
- package/dist/plugins/link/LexicalLinkVisitor.js +10 -0
- package/dist/plugins/link/MdastLinkVisitor.d.ts +3 -0
- package/dist/plugins/link/MdastLinkVisitor.js +14 -0
- package/dist/plugins/link/index.d.ts +101 -0
- package/dist/plugins/link/index.js +33 -0
- package/dist/plugins/link-dialog/LinkDialog.d.ts +16 -0
- package/dist/plugins/link-dialog/LinkDialog.js +148 -0
- package/dist/plugins/link-dialog/index.d.ts +669 -0
- package/dist/plugins/link-dialog/index.js +254 -0
- package/dist/plugins/lists/LexicalListItemVisitor.d.ts +4 -0
- package/dist/plugins/lists/LexicalListItemVisitor.js +23 -0
- package/dist/plugins/lists/LexicalListVisitor.d.ts +4 -0
- package/dist/plugins/lists/LexicalListVisitor.js +13 -0
- package/dist/plugins/lists/MdastListItemVisitor.d.ts +3 -0
- package/dist/plugins/lists/MdastListItemVisitor.js +10 -0
- package/dist/plugins/lists/MdastListVisitor.d.ts +3 -0
- package/dist/plugins/lists/MdastListVisitor.js +20 -0
- package/dist/plugins/lists/index.d.ts +703 -0
- package/dist/plugins/lists/index.js +122 -0
- package/dist/plugins/markdown-shortcut/index.d.ts +80 -0
- package/dist/plugins/markdown-shortcut/index.js +90 -0
- package/dist/plugins/quote/LexicalQuoteVisitor.d.ts +4 -0
- package/dist/plugins/quote/LexicalQuoteVisitor.js +12 -0
- package/dist/plugins/quote/MdastBlockQuoteVisitor.d.ts +3 -0
- package/dist/plugins/quote/MdastBlockQuoteVisitor.js +10 -0
- package/dist/plugins/quote/index.d.ts +480 -0
- package/dist/plugins/quote/index.js +26 -0
- package/dist/plugins/sandpack/SandpackEditor.d.ts +7 -0
- package/dist/plugins/sandpack/SandpackEditor.js +37 -0
- package/dist/plugins/sandpack/index.d.ts +1077 -0
- package/dist/plugins/sandpack/index.js +93 -0
- package/dist/plugins/sandpack/useCodeMirrorRef.d.ts +4 -0
- package/dist/plugins/sandpack/useCodeMirrorRef.js +96 -0
- package/dist/plugins/table/LexicalTableVisitor.d.ts +4 -0
- package/dist/plugins/table/LexicalTableVisitor.js +10 -0
- package/dist/plugins/table/MdastTableVisitor.d.ts +3 -0
- package/dist/plugins/table/MdastTableVisitor.js +10 -0
- package/dist/plugins/table/TableEditor.d.ts +21 -0
- package/dist/plugins/table/TableEditor.js +388 -0
- package/dist/plugins/table/TableNode.d.ts +52 -0
- package/dist/plugins/table/TableNode.js +152 -0
- package/dist/plugins/table/index.d.ts +704 -0
- package/dist/plugins/table/index.js +69 -0
- package/dist/plugins/thematic-break/LexicalThematicBreakVisitor.d.ts +4 -0
- package/dist/plugins/thematic-break/LexicalThematicBreakVisitor.js +10 -0
- package/dist/plugins/thematic-break/MdastThematicBreakVisitor.d.ts +3 -0
- package/dist/plugins/thematic-break/MdastThematicBreakVisitor.js +10 -0
- package/dist/plugins/thematic-break/index.d.ts +693 -0
- package/dist/plugins/thematic-break/index.js +39 -0
- package/dist/plugins/toolbar/components/BlockTypeSelect.d.ts +6 -0
- package/dist/plugins/toolbar/components/BlockTypeSelect.js +54 -0
- package/dist/plugins/toolbar/components/BoldItalicUnderlineToggles.d.ts +5 -0
- package/dist/plugins/toolbar/components/BoldItalicUnderlineToggles.js +33 -0
- package/dist/plugins/toolbar/components/ChangeAdmonitionType.d.ts +6 -0
- package/dist/plugins/toolbar/components/ChangeAdmonitionType.js +30 -0
- package/dist/plugins/toolbar/components/ChangeCodeMirrorLanguage.d.ts +7 -0
- package/dist/plugins/toolbar/components/ChangeCodeMirrorLanguage.js +32 -0
- package/dist/plugins/toolbar/components/CodeToggle.d.ts +6 -0
- package/dist/plugins/toolbar/components/CodeToggle.js +19 -0
- package/dist/plugins/toolbar/components/CreateLink.d.ts +6 -0
- package/dist/plugins/toolbar/components/CreateLink.js +20 -0
- package/dist/plugins/toolbar/components/DiffSourceToggleWrapper.d.ts +18 -0
- package/dist/plugins/toolbar/components/DiffSourceToggleWrapper.js +25 -0
- package/dist/plugins/toolbar/components/InsertAdmonition.d.ts +6 -0
- package/dist/plugins/toolbar/components/InsertAdmonition.js +29 -0
- package/dist/plugins/toolbar/components/InsertCodeBlock.d.ts +7 -0
- package/dist/plugins/toolbar/components/InsertCodeBlock.js +20 -0
- package/dist/plugins/toolbar/components/InsertFrontmatter.d.ts +6 -0
- package/dist/plugins/toolbar/components/InsertFrontmatter.js +24 -0
- package/dist/plugins/toolbar/components/InsertImage.d.ts +6 -0
- package/dist/plugins/toolbar/components/InsertImage.js +14 -0
- package/dist/plugins/toolbar/components/InsertSandpack.d.ts +6 -0
- package/dist/plugins/toolbar/components/InsertSandpack.js +13 -0
- package/dist/plugins/toolbar/components/InsertTable.d.ts +6 -0
- package/dist/plugins/toolbar/components/InsertTable.js +20 -0
- package/dist/plugins/toolbar/components/InsertThematicBreak.d.ts +6 -0
- package/dist/plugins/toolbar/components/InsertThematicBreak.js +11 -0
- package/dist/plugins/toolbar/components/KitchenSinkToolbar.d.ts +7 -0
- package/dist/plugins/toolbar/components/KitchenSinkToolbar.js +58 -0
- package/dist/plugins/toolbar/components/ListsToggle.d.ts +7 -0
- package/dist/plugins/toolbar/components/ListsToggle.js +23 -0
- package/dist/plugins/toolbar/components/ShowSandpackInfo.d.ts +7 -0
- package/dist/plugins/toolbar/components/ShowSandpackInfo.js +31 -0
- package/dist/plugins/toolbar/components/UndoRedo.d.ts +5 -0
- package/dist/plugins/toolbar/components/UndoRedo.js +58 -0
- package/dist/plugins/toolbar/index.d.ts +564 -0
- package/dist/plugins/toolbar/index.js +40 -0
- package/dist/plugins/toolbar/primitives/DialogButton.d.ts +40 -0
- package/dist/plugins/toolbar/primitives/DialogButton.js +114 -0
- package/dist/plugins/toolbar/primitives/TooltipWrap.d.ts +8 -0
- package/dist/plugins/toolbar/primitives/TooltipWrap.js +12 -0
- package/dist/plugins/toolbar/primitives/select.d.ts +50 -0
- package/dist/plugins/toolbar/primitives/select.js +55 -0
- package/dist/plugins/toolbar/primitives/toolbar.d.ts +163 -0
- package/dist/plugins/toolbar/primitives/toolbar.js +115 -0
- package/dist/style.css +1093 -288
- package/dist/styles/globals.css.js +4 -0
- package/dist/styles/lexical-theme.module.css.js +62 -0
- package/dist/styles/lexicalTheme.d.ts +2 -0
- package/dist/styles/lexicalTheme.js +31 -0
- package/dist/styles/ui.module.css.js +311 -0
- package/dist/utils/detectMac.d.ts +3 -0
- package/dist/utils/detectMac.js +13 -0
- package/dist/utils/fp.d.ts +47 -0
- package/dist/utils/fp.js +44 -0
- package/dist/utils/isPartOftheEditorUI.d.ts +1 -0
- package/dist/utils/isPartOftheEditorUI.js +12 -0
- package/dist/utils/lexicalHelpers.d.ts +11 -0
- package/dist/utils/lexicalHelpers.js +80 -0
- package/dist/utils/makeHslTransparent.d.ts +1 -0
- package/dist/utils/makeHslTransparent.js +6 -0
- package/dist/utils/mergeStyleAttributes.d.ts +1 -0
- package/dist/utils/mergeStyleAttributes.js +22 -0
- package/dist/utils/uuid4.d.ts +1 -0
- package/dist/utils/uuid4.js +10 -0
- package/dist/utils/voidEmitter.d.ts +14 -0
- package/dist/utils/voidEmitter.js +15 -0
- package/dist/utils/whitespaceConversion.d.ts +2 -0
- package/dist/utils/whitespaceConversion.js +9 -0
- package/package.json +326 -39
- package/dist/index.cjs +0 -1
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { $isElementNode } from "lexical";
|
|
2
|
+
import { toMarkdown } from "mdast-util-to-markdown";
|
|
3
|
+
import { isMdastHTMLNode } from "./plugins/core/MdastHTMLNode.js";
|
|
4
|
+
import { mergeStyleAttributes } from "./utils/mergeStyleAttributes.js";
|
|
5
|
+
function isParent(node) {
|
|
6
|
+
return node.children instanceof Array;
|
|
7
|
+
}
|
|
8
|
+
function exportLexicalTreeToMdast({
|
|
9
|
+
root,
|
|
10
|
+
visitors,
|
|
11
|
+
jsxComponentDescriptors,
|
|
12
|
+
jsxIsAvailable,
|
|
13
|
+
addImportStatements = true
|
|
14
|
+
}) {
|
|
15
|
+
let unistRoot = null;
|
|
16
|
+
const referredComponents = /* @__PURE__ */ new Set();
|
|
17
|
+
visitors = visitors.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
18
|
+
visit(root, null);
|
|
19
|
+
function registerReferredComponent(componentName) {
|
|
20
|
+
referredComponents.add(componentName);
|
|
21
|
+
}
|
|
22
|
+
function appendToParent(parentNode, node) {
|
|
23
|
+
if (unistRoot === null) {
|
|
24
|
+
unistRoot = node;
|
|
25
|
+
return unistRoot;
|
|
26
|
+
}
|
|
27
|
+
if (!isParent(parentNode)) {
|
|
28
|
+
throw new Error("Attempting to append children to a non-parent");
|
|
29
|
+
}
|
|
30
|
+
const siblings = parentNode.children;
|
|
31
|
+
const prevSibling = siblings.at(-1);
|
|
32
|
+
if (prevSibling) {
|
|
33
|
+
const joinVisitor = visitors.find((visitor) => {
|
|
34
|
+
var _a;
|
|
35
|
+
return (_a = visitor.shouldJoin) == null ? void 0 : _a.call(visitor, prevSibling, node);
|
|
36
|
+
});
|
|
37
|
+
if (joinVisitor) {
|
|
38
|
+
const joinedNode = joinVisitor.join(prevSibling, node);
|
|
39
|
+
siblings.splice(siblings.length - 1, 1, joinedNode);
|
|
40
|
+
return joinedNode;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
siblings.push(node);
|
|
44
|
+
return node;
|
|
45
|
+
}
|
|
46
|
+
function visitChildren(lexicalNode, parentNode) {
|
|
47
|
+
lexicalNode.getChildren().forEach((lexicalChild) => {
|
|
48
|
+
visit(lexicalChild, parentNode);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function visit(lexicalNode, mdastParent) {
|
|
52
|
+
var _a;
|
|
53
|
+
const visitor = visitors.find((visitor2) => {
|
|
54
|
+
var _a2;
|
|
55
|
+
return (_a2 = visitor2.testLexicalNode) == null ? void 0 : _a2.call(visitor2, lexicalNode);
|
|
56
|
+
});
|
|
57
|
+
if (!visitor) {
|
|
58
|
+
throw new Error(`no lexical visitor found for ${lexicalNode.getType()}`, {
|
|
59
|
+
cause: lexicalNode
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
(_a = visitor.visitLexicalNode) == null ? void 0 : _a.call(visitor, {
|
|
63
|
+
lexicalNode,
|
|
64
|
+
mdastParent,
|
|
65
|
+
actions: {
|
|
66
|
+
addAndStepInto(type, props = {}, hasChildren = true) {
|
|
67
|
+
const newNode = {
|
|
68
|
+
type,
|
|
69
|
+
...props,
|
|
70
|
+
...hasChildren ? { children: [] } : {}
|
|
71
|
+
};
|
|
72
|
+
appendToParent(mdastParent, newNode);
|
|
73
|
+
if ($isElementNode(lexicalNode) && hasChildren) {
|
|
74
|
+
visitChildren(lexicalNode, newNode);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
appendToParent,
|
|
78
|
+
visitChildren,
|
|
79
|
+
registerReferredComponent
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (unistRoot === null) {
|
|
84
|
+
throw new Error("traversal ended with no root element");
|
|
85
|
+
}
|
|
86
|
+
const importsMap = /* @__PURE__ */ new Map();
|
|
87
|
+
const defaultImportsMap = /* @__PURE__ */ new Map();
|
|
88
|
+
for (const componentName of referredComponents) {
|
|
89
|
+
const descriptor = jsxComponentDescriptors.find((descriptor2) => descriptor2.name === componentName);
|
|
90
|
+
if (!descriptor) {
|
|
91
|
+
throw new Error(`Component ${componentName} is used but not imported`);
|
|
92
|
+
}
|
|
93
|
+
if (!descriptor.source) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (descriptor.defaultExport) {
|
|
97
|
+
defaultImportsMap.set(componentName, descriptor.source);
|
|
98
|
+
} else {
|
|
99
|
+
const { source } = descriptor;
|
|
100
|
+
const existing = importsMap.get(source);
|
|
101
|
+
if (existing) {
|
|
102
|
+
existing.push(componentName);
|
|
103
|
+
} else {
|
|
104
|
+
importsMap.set(source, [componentName]);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const imports = Array.from(importsMap).map(([source, componentNames]) => {
|
|
109
|
+
return {
|
|
110
|
+
type: "mdxjsEsm",
|
|
111
|
+
value: `import { ${componentNames.join(", ")} } from '${source}'`
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
imports.push(
|
|
115
|
+
...Array.from(defaultImportsMap).map(([componentName, source]) => {
|
|
116
|
+
return {
|
|
117
|
+
type: "mdxjsEsm",
|
|
118
|
+
value: `import ${componentName} from '${source}'`
|
|
119
|
+
};
|
|
120
|
+
})
|
|
121
|
+
);
|
|
122
|
+
const typedRoot = unistRoot;
|
|
123
|
+
const frontmatter = typedRoot.children.find((child) => child.type === "yaml");
|
|
124
|
+
if (addImportStatements) {
|
|
125
|
+
if (frontmatter) {
|
|
126
|
+
typedRoot.children.splice(typedRoot.children.indexOf(frontmatter) + 1, 0, ...imports);
|
|
127
|
+
} else {
|
|
128
|
+
typedRoot.children.unshift(...imports);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
fixWrappingWhitespace(typedRoot, []);
|
|
132
|
+
collapseNestedHtmlTags(typedRoot);
|
|
133
|
+
if (!jsxIsAvailable) {
|
|
134
|
+
convertUnderlineJsxToHtml(typedRoot);
|
|
135
|
+
}
|
|
136
|
+
return typedRoot;
|
|
137
|
+
}
|
|
138
|
+
function collapseNestedHtmlTags(node) {
|
|
139
|
+
if ("children" in node && node.children) {
|
|
140
|
+
if (isMdastHTMLNode(node) && node.children.length === 1) {
|
|
141
|
+
const onlyChild = node.children[0];
|
|
142
|
+
if (onlyChild.type === "mdxJsxTextElement" && onlyChild.name === "span") {
|
|
143
|
+
(onlyChild.attributes || []).forEach((attribute) => {
|
|
144
|
+
var _a;
|
|
145
|
+
if (attribute.type === "mdxJsxAttribute") {
|
|
146
|
+
const parentAttribute = (_a = node.attributes) == null ? void 0 : _a.find((attr) => attr.type === "mdxJsxAttribute" && attr.name === attribute.name);
|
|
147
|
+
if (parentAttribute) {
|
|
148
|
+
if (attribute.name === "className") {
|
|
149
|
+
const mergedClassesSet = /* @__PURE__ */ new Set([
|
|
150
|
+
...parentAttribute.value.split(" "),
|
|
151
|
+
...attribute.value.split(" ")
|
|
152
|
+
]);
|
|
153
|
+
parentAttribute.value = Array.from(mergedClassesSet).join(" ");
|
|
154
|
+
} else if (attribute.name === "style") {
|
|
155
|
+
parentAttribute.value = mergeStyleAttributes(parentAttribute.value, attribute.value);
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
node.attributes.push(attribute);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
node.children = onlyChild.children;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
node.children.forEach((child) => collapseNestedHtmlTags(child));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function convertUnderlineJsxToHtml(node) {
|
|
169
|
+
if (Object.hasOwn(node, "children")) {
|
|
170
|
+
const nodeAsParent = node;
|
|
171
|
+
const newChildren = [];
|
|
172
|
+
nodeAsParent.children.forEach((child) => {
|
|
173
|
+
if (child.type === "mdxJsxTextElement" && child.name === "u") {
|
|
174
|
+
newChildren.push(...[{ type: "html", value: "<u>" }, ...child.children, { type: "html", value: "</u>" }]);
|
|
175
|
+
} else {
|
|
176
|
+
newChildren.push(child);
|
|
177
|
+
convertUnderlineJsxToHtml(child);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
nodeAsParent.children = newChildren;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const TRAILING_WHITESPACE_REGEXP = /\s+$/;
|
|
184
|
+
const LEADING_WHITESPACE_REGEXP = /^\s+/;
|
|
185
|
+
function fixWrappingWhitespace(node, parentChain) {
|
|
186
|
+
if (node.type === "strong" || node.type === "emphasis") {
|
|
187
|
+
const lastChild = node.children.at(-1);
|
|
188
|
+
if ((lastChild == null ? void 0 : lastChild.type) === "text") {
|
|
189
|
+
const trailingWhitespace = lastChild.value.match(TRAILING_WHITESPACE_REGEXP);
|
|
190
|
+
if (trailingWhitespace) {
|
|
191
|
+
lastChild.value = lastChild.value.replace(TRAILING_WHITESPACE_REGEXP, "");
|
|
192
|
+
const parent = parentChain.at(-1);
|
|
193
|
+
if (parent) {
|
|
194
|
+
parent.children.splice(parent.children.indexOf(node) + 1, 0, {
|
|
195
|
+
type: "text",
|
|
196
|
+
value: trailingWhitespace[0]
|
|
197
|
+
});
|
|
198
|
+
fixWrappingWhitespace(parent, parentChain.slice(0, -1));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const firstChild = node.children.at(0);
|
|
203
|
+
if ((firstChild == null ? void 0 : firstChild.type) === "text") {
|
|
204
|
+
const leadingWhitespace = firstChild.value.match(LEADING_WHITESPACE_REGEXP);
|
|
205
|
+
if (leadingWhitespace) {
|
|
206
|
+
firstChild.value = firstChild.value.replace(LEADING_WHITESPACE_REGEXP, "");
|
|
207
|
+
const parent = parentChain.at(-1);
|
|
208
|
+
if (parent) {
|
|
209
|
+
parent.children.splice(parent.children.indexOf(node), 0, {
|
|
210
|
+
type: "text",
|
|
211
|
+
value: leadingWhitespace[0]
|
|
212
|
+
});
|
|
213
|
+
fixWrappingWhitespace(parent, parentChain.slice(0, -1));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if ("children" in node && node.children) {
|
|
219
|
+
const nodeAsParent = node;
|
|
220
|
+
nodeAsParent.children.forEach((child) => fixWrappingWhitespace(child, [...parentChain, nodeAsParent]));
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function exportMarkdownFromLexical({
|
|
224
|
+
root,
|
|
225
|
+
toMarkdownOptions,
|
|
226
|
+
toMarkdownExtensions,
|
|
227
|
+
visitors,
|
|
228
|
+
jsxComponentDescriptors,
|
|
229
|
+
jsxIsAvailable
|
|
230
|
+
}) {
|
|
231
|
+
return toMarkdown(exportLexicalTreeToMdast({ root, visitors, jsxComponentDescriptors, jsxIsAvailable }), {
|
|
232
|
+
extensions: toMarkdownExtensions,
|
|
233
|
+
...toMarkdownOptions
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
export {
|
|
237
|
+
exportLexicalTreeToMdast,
|
|
238
|
+
exportMarkdownFromLexical
|
|
239
|
+
};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { RealmNode } from './realm';
|
|
3
|
+
import type { ValuesForKeys, TypedRealm, System, RealmFactory, ValueForKey, SystemKeys, SystemDict, AnySystemSpec, SystemOfSpecs } from './realmFactory';
|
|
4
|
+
/**
|
|
5
|
+
* Describes the mapping between the system streams and the component properties.
|
|
6
|
+
* Each property uses the keys as the names of the properties and the values as the corresponding stream names.
|
|
7
|
+
* @typeParam SS - the type of the system.
|
|
8
|
+
*/
|
|
9
|
+
export interface SystemPropsMap<Sys extends System, K = keyof Sys, D = {
|
|
10
|
+
[name: string]: K;
|
|
11
|
+
}> {
|
|
12
|
+
/**
|
|
13
|
+
* Specifies the required component properties.
|
|
14
|
+
*/
|
|
15
|
+
required?: D;
|
|
16
|
+
/**
|
|
17
|
+
* Specifies the optional component properties.
|
|
18
|
+
*/
|
|
19
|
+
optional?: D;
|
|
20
|
+
/**
|
|
21
|
+
* Specifies the component methods, if any. Streams are converted to methods with a single argument.
|
|
22
|
+
* When invoked, the method publishes the value of the argument to the specified stream.
|
|
23
|
+
*/
|
|
24
|
+
methods?: D;
|
|
25
|
+
/**
|
|
26
|
+
* Specifies the component "event" properties, if any.
|
|
27
|
+
* Event properties accept callback functions which get executed when the stream emits a new value.
|
|
28
|
+
*/
|
|
29
|
+
events?: D;
|
|
30
|
+
}
|
|
31
|
+
type StringKeys<T> = Extract<keyof T, string>;
|
|
32
|
+
/** @internal */
|
|
33
|
+
export type PropsFromPropMap<Sys extends System, Map extends SystemPropsMap<Sys>> = {
|
|
34
|
+
[K in StringKeys<Map['required']>]: Map['required'][K] extends string ? Sys[Map['required'][K]] extends RealmNode<infer R> ? R : never : never;
|
|
35
|
+
} & {
|
|
36
|
+
[K in StringKeys<Map['optional']>]?: Map['optional'][K] extends string ? Sys[Map['optional'][K]] extends RealmNode<infer R> ? R : never : never;
|
|
37
|
+
} & {
|
|
38
|
+
[K in StringKeys<Map['events']>]?: Map['events'][K] extends string ? Sys[Map['events'][K]] extends RealmNode<infer R> ? (value: R) => void : never : never;
|
|
39
|
+
};
|
|
40
|
+
/** @internal */
|
|
41
|
+
export type MethodsFromPropMap<Sys extends System, Map extends SystemPropsMap<Sys>> = {
|
|
42
|
+
[K in StringKeys<Map['methods']>]: Map['methods'][K] extends string ? Sys[Map['methods'][K]] extends RealmNode<infer R> ? (value: R) => void : never : never;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* @internal
|
|
46
|
+
* Used to correctly specify type refs for system components.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* const s = system(() => { return { a: statefulStream(0) } })
|
|
51
|
+
* const { Component } = systemToComponent(s)
|
|
52
|
+
*
|
|
53
|
+
* const App = () => {
|
|
54
|
+
* const ref = useRef<RefHandle<typeof Component>>()
|
|
55
|
+
* return <Component ref={ref} />
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @typeParam T - the type of the component
|
|
60
|
+
*/
|
|
61
|
+
export type RefHandle<T> = T extends React.ForwardRefExoticComponent<React.RefAttributes<infer Handle>> ? Handle : never;
|
|
62
|
+
/**
|
|
63
|
+
* Converts a system spec to React component by mapping the system streams to component properties, events and methods. Returns hooks for querying and modifying
|
|
64
|
+
* the system streams from the component's child components.
|
|
65
|
+
* @param realmFactory - The return value from a [[system]] call.
|
|
66
|
+
* @param map - The streams to props / events / methods mapping Check [[SystemPropsMap]] for more details.
|
|
67
|
+
* @param Root - The optional React component to render. By default, the resulting component renders nothing, acting as a logical wrapper for its children.
|
|
68
|
+
* @returns an object containing the following:
|
|
69
|
+
* - `Component`: the React component.
|
|
70
|
+
* - `useEmitterValue`: a hook that lets child components use values emitted from the specified output stream.
|
|
71
|
+
* - `useEmitter`: a hook that calls the provided callback whenever the specified stream emits a value.
|
|
72
|
+
* - `usePublisher`: a hook which lets child components publish values to the specified stream.
|
|
73
|
+
* <hr />
|
|
74
|
+
*/
|
|
75
|
+
export declare function realmFactoryToComponent<RootComp, RF extends RealmFactory<Sys>, Sys extends System = RF extends RealmFactory<infer S> ? S : never, Realm extends TypedRealm<Sys> = TypedRealm<Sys>, M extends SystemPropsMap<Sys> = SystemPropsMap<Sys>>(realmFactory: RF, map: M, Root?: RootComp): {
|
|
76
|
+
useRealmContext: () => TypedRealm<Sys>;
|
|
77
|
+
useEmitter: <K extends Extract<keyof Sys, string>>(key: K, callback: (value: ValueForKey<Sys, K>) => void) => void;
|
|
78
|
+
useEmitterValues: <K_1 extends SystemKeys<Sys>>(...keys: K_1) => ValuesForKeys<Sys, K_1>;
|
|
79
|
+
usePubKeys: () => (values: SystemDict<Sys>) => void;
|
|
80
|
+
usePublisher: <K_2 extends keyof Sys>(key: K_2) => (value: ValueForKey<Sys, K_2>) => void;
|
|
81
|
+
Component: React.ForwardRefExoticComponent<React.PropsWithoutRef<{ [K_3 in Extract<keyof M["required"], string>]: M["required"][K_3] extends string ? Sys[M["required"][K_3]] extends RealmNode<infer R> ? R : never : never; } & { [K_4 in Extract<keyof M["optional"], string>]?: (M["optional"][K_4] extends string ? Sys[M["optional"][K_4]] extends RealmNode<infer R_1> ? R_1 : never : never) | undefined; } & { [K_5 in Extract<keyof M["events"], string>]?: (M["events"][K_5] extends string ? Sys[M["events"][K_5]] extends RealmNode<infer R_2> ? (value: R_2) => void : never : never) | undefined; } & (RootComp extends React.ComponentType<infer RP> ? RP : {
|
|
82
|
+
children?: React.ReactNode;
|
|
83
|
+
})> & React.RefAttributes<MethodsFromPropMap<Sys, M>>>;
|
|
84
|
+
};
|
|
85
|
+
/** @internal */
|
|
86
|
+
export declare function sysHooks<Sys extends System>(): {
|
|
87
|
+
useRealmContext: () => TypedRealm<Sys>;
|
|
88
|
+
useEmitter: <K extends Extract<keyof Sys, string>>(key: K, callback: (value: ValueForKey<Sys, K>) => void) => void;
|
|
89
|
+
useEmitterValues: <K_1 extends SystemKeys<Sys>>(...keys: K_1) => ValuesForKeys<Sys, K_1>;
|
|
90
|
+
usePubKeys: () => (values: SystemDict<Sys>) => void;
|
|
91
|
+
usePublisher: <K_2 extends keyof Sys>(key: K_2) => (value: ValueForKey<Sys, K_2>) => void;
|
|
92
|
+
};
|
|
93
|
+
type SystemAndDependencies<Spec extends AnySystemSpec> = SystemOfSpecs<[Spec]> & SystemOfSpecs<Spec['dependencies']>;
|
|
94
|
+
/**
|
|
95
|
+
* The parameters of a plugin declaration. THe best way to understand what each one does is to examine the source code of the existing plugins.
|
|
96
|
+
*/
|
|
97
|
+
export interface RealmPluginParams<Spec extends AnySystemSpec, Params extends object> {
|
|
98
|
+
/**
|
|
99
|
+
* The id of the plugin. Used to declare conditional features that are activated only if the plugin is present.
|
|
100
|
+
*/
|
|
101
|
+
id: string;
|
|
102
|
+
/**
|
|
103
|
+
* The ids of the plugins that this plugin depends on. The plugin will not be activated if any of the dependencies is not present.
|
|
104
|
+
*/
|
|
105
|
+
dependencies?: string[];
|
|
106
|
+
/**
|
|
107
|
+
* The system spec of the plugin. Construct one using {@link system}.
|
|
108
|
+
*/
|
|
109
|
+
systemSpec: Spec;
|
|
110
|
+
/**
|
|
111
|
+
* The callback is executed every time the react component is re-rendered.
|
|
112
|
+
*/
|
|
113
|
+
applyParamsToSystem?: (realm: TypedRealm<SystemAndDependencies<Spec>>, props: Params) => void;
|
|
114
|
+
/**
|
|
115
|
+
* Executed when the component mounts. Use this to register import/export visitors, add lexical nodes to the editor, etc.
|
|
116
|
+
* @param realm - the realm instance
|
|
117
|
+
* @param params - the parameters passed to the plugin in the component declaration
|
|
118
|
+
* @param pluginIds - the ids of all the plugins that are present in the component declaration
|
|
119
|
+
*/
|
|
120
|
+
init?: (realm: TypedRealm<SystemAndDependencies<Spec>>, params: Params, pluginIds: string[]) => void;
|
|
121
|
+
}
|
|
122
|
+
/** @internal */
|
|
123
|
+
export interface PluginConstructor<Spec extends AnySystemSpec, Params extends object> {
|
|
124
|
+
(params?: Params): {
|
|
125
|
+
pluginParams?: Params;
|
|
126
|
+
} & RealmPluginParams<Spec, Params>;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Declares a new MDXEditor plugin.
|
|
130
|
+
*/
|
|
131
|
+
export declare function realmPlugin<Spec extends AnySystemSpec, Params extends object>(params: RealmPluginParams<Spec, Params>): readonly [PluginConstructor<Spec, Params>, {
|
|
132
|
+
useRealmContext: () => TypedRealm<SystemAndDependencies<Spec>>;
|
|
133
|
+
useEmitter: <K extends Extract<keyof (Spec extends AnySystemSpec ? {} & ReturnType<Spec["constructor"]> : never), string> | Extract<keyof SystemOfSpecs<Spec["dependencies"]>, string>>(key: K, callback: (value: ValueForKey<SystemAndDependencies<Spec>, K>) => void) => void;
|
|
134
|
+
useEmitterValues: <K_1 extends SystemKeys<SystemAndDependencies<Spec>>>(...keys: K_1) => ValuesForKeys<SystemAndDependencies<Spec>, K_1>;
|
|
135
|
+
usePubKeys: () => (values: SystemDict<SystemAndDependencies<Spec>>) => void;
|
|
136
|
+
usePublisher: <K_2 extends keyof (Spec extends AnySystemSpec ? {} & ReturnType<Spec["constructor"]> : never) | keyof SystemOfSpecs<Spec["dependencies"]>>(key: K_2) => (value: ValueForKey<SystemAndDependencies<Spec>, K_2>) => void;
|
|
137
|
+
}];
|
|
138
|
+
/** @internal */
|
|
139
|
+
export declare const RealmPluginInitializer: <P extends ({
|
|
140
|
+
pluginParams?: any;
|
|
141
|
+
} & RealmPluginParams<AnySystemSpec, any>)[]>({ plugins, children }: {
|
|
142
|
+
plugins: P;
|
|
143
|
+
children?: React.ReactNode;
|
|
144
|
+
}) => React.FunctionComponentElement<React.ProviderProps<TypedRealm<any>>>;
|
|
145
|
+
/** @internal */
|
|
146
|
+
export declare function useHasPlugin(id: string): boolean;
|
|
147
|
+
/** @internal */
|
|
148
|
+
export declare const RequirePlugin: React.FC<{
|
|
149
|
+
id: string;
|
|
150
|
+
children: React.ReactNode;
|
|
151
|
+
}>;
|
|
152
|
+
export {};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import React__default, { useEffect } from "react";
|
|
2
|
+
import { tap, always } from "../utils/fp.js";
|
|
3
|
+
import { realmFactory } from "./realmFactory.js";
|
|
4
|
+
function omit(keys, obj) {
|
|
5
|
+
const result = {};
|
|
6
|
+
const index = {};
|
|
7
|
+
let idx = 0;
|
|
8
|
+
const len = keys.length;
|
|
9
|
+
while (idx < len) {
|
|
10
|
+
index[keys[idx]] = 1;
|
|
11
|
+
idx += 1;
|
|
12
|
+
}
|
|
13
|
+
for (const prop in obj) {
|
|
14
|
+
if (!Object.prototype.hasOwnProperty.call(index, prop)) {
|
|
15
|
+
result[prop] = obj[prop];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
const useIsomorphicLayoutEffect = typeof document !== "undefined" ? React__default.useLayoutEffect : React__default.useEffect;
|
|
21
|
+
const GurxContext = React__default.createContext(void 0);
|
|
22
|
+
function realmFactoryToComponent(realmFactory2, map, Root) {
|
|
23
|
+
const requiredPropNames = Object.keys(map.required || {});
|
|
24
|
+
const optionalPropNames = Object.keys(map.optional || {});
|
|
25
|
+
const methodNames = Object.keys(map.methods || {});
|
|
26
|
+
const eventNames = Object.keys(map.events || {});
|
|
27
|
+
const Context = GurxContext;
|
|
28
|
+
function applyPropsToRealm(realm, props) {
|
|
29
|
+
const toBePublished = {};
|
|
30
|
+
for (const requiredPropName of requiredPropNames) {
|
|
31
|
+
const nodeName = map.required[requiredPropName];
|
|
32
|
+
toBePublished[nodeName] = props[requiredPropName];
|
|
33
|
+
}
|
|
34
|
+
for (const optionalPropName of optionalPropNames) {
|
|
35
|
+
const value = props[optionalPropName];
|
|
36
|
+
if (value !== void 0) {
|
|
37
|
+
const nodeName = map.optional[optionalPropName];
|
|
38
|
+
toBePublished[nodeName] = value;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
realm.pubKeys(toBePublished);
|
|
42
|
+
}
|
|
43
|
+
function buildMethods(realm) {
|
|
44
|
+
return methodNames.reduce((acc, methodName) => {
|
|
45
|
+
const nodeName = map.methods[methodName];
|
|
46
|
+
acc[methodName] = (value) => {
|
|
47
|
+
realm.pubKey(nodeName, value);
|
|
48
|
+
};
|
|
49
|
+
return acc;
|
|
50
|
+
}, {});
|
|
51
|
+
}
|
|
52
|
+
const Component = React__default.forwardRef((props, ref) => {
|
|
53
|
+
const realm = React__default.useMemo(() => {
|
|
54
|
+
return tap(realmFactory2(), (realm2) => applyPropsToRealm(realm2, props));
|
|
55
|
+
}, []);
|
|
56
|
+
useIsomorphicLayoutEffect(() => {
|
|
57
|
+
applyPropsToRealm(realm, props);
|
|
58
|
+
realm.resetSingletonSubs();
|
|
59
|
+
for (const eventName of eventNames) {
|
|
60
|
+
if (eventName in props) {
|
|
61
|
+
realm.singletonSubKey(map.events[eventName], props[eventName]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return () => {
|
|
65
|
+
realm.resetSingletonSubs();
|
|
66
|
+
};
|
|
67
|
+
}, [props]);
|
|
68
|
+
React__default.useImperativeHandle(ref, always(buildMethods(realm)));
|
|
69
|
+
const children = props.children;
|
|
70
|
+
return React__default.createElement(
|
|
71
|
+
Context.Provider,
|
|
72
|
+
{ value: realm },
|
|
73
|
+
Root ? React__default.createElement(
|
|
74
|
+
Root,
|
|
75
|
+
omit([...requiredPropNames, ...optionalPropNames, ...eventNames], props),
|
|
76
|
+
children
|
|
77
|
+
) : children
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
Component.displayName = "Gurx Component";
|
|
81
|
+
return {
|
|
82
|
+
Component,
|
|
83
|
+
...sysHooks()
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function sysHooks() {
|
|
87
|
+
const Context = GurxContext;
|
|
88
|
+
const usePublisher = (key) => {
|
|
89
|
+
const realm = React__default.useContext(Context);
|
|
90
|
+
return React__default.useCallback(
|
|
91
|
+
(value) => {
|
|
92
|
+
realm.pubKey(key, value);
|
|
93
|
+
},
|
|
94
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
95
|
+
[key, realm]
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
const useEmitterValues = (...keys) => {
|
|
99
|
+
const realm = React__default.useContext(Context);
|
|
100
|
+
const [values, setValues] = React__default.useState(() => realm.getKeyValues(keys));
|
|
101
|
+
useEffect(
|
|
102
|
+
() => realm == null ? void 0 : realm.subKeys(keys, (newValues) => {
|
|
103
|
+
const setter = () => {
|
|
104
|
+
if (keys.length === 1) {
|
|
105
|
+
newValues = [newValues];
|
|
106
|
+
}
|
|
107
|
+
for (let i = 0; i < keys.length; i++) {
|
|
108
|
+
if (newValues[i] !== values[i]) {
|
|
109
|
+
setValues(newValues);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
setter();
|
|
114
|
+
}),
|
|
115
|
+
[keys, realm, values]
|
|
116
|
+
);
|
|
117
|
+
return values;
|
|
118
|
+
};
|
|
119
|
+
const usePubKeys = () => {
|
|
120
|
+
return React__default.useContext(Context).pubKeys;
|
|
121
|
+
};
|
|
122
|
+
const useEmitter = (key, callback) => {
|
|
123
|
+
const realm = React__default.useContext(Context);
|
|
124
|
+
useIsomorphicLayoutEffect(() => realm.subKey(key, callback), [callback]);
|
|
125
|
+
};
|
|
126
|
+
const useRealmContext = () => {
|
|
127
|
+
return React__default.useContext(Context);
|
|
128
|
+
};
|
|
129
|
+
return {
|
|
130
|
+
useRealmContext,
|
|
131
|
+
useEmitter,
|
|
132
|
+
useEmitterValues,
|
|
133
|
+
usePubKeys,
|
|
134
|
+
usePublisher
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const UsedPluginsContext = React__default.createContext(/* @__PURE__ */ new Set());
|
|
138
|
+
function realmPlugin(params) {
|
|
139
|
+
const plugin = (pluginParams) => {
|
|
140
|
+
return {
|
|
141
|
+
systemSpec: params.systemSpec,
|
|
142
|
+
pluginParams,
|
|
143
|
+
applyParamsToSystem: params.applyParamsToSystem,
|
|
144
|
+
init: params.init,
|
|
145
|
+
id: params.id,
|
|
146
|
+
dependencies: params.dependencies
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
return [plugin, sysHooks()];
|
|
150
|
+
}
|
|
151
|
+
const RealmPluginInitializer = function({
|
|
152
|
+
plugins,
|
|
153
|
+
children
|
|
154
|
+
}) {
|
|
155
|
+
const validPlugins = React__default.useMemo(() => {
|
|
156
|
+
const availablePlugins = plugins.map((plugin) => plugin.id);
|
|
157
|
+
const validPlugins2 = plugins.filter((plugin) => {
|
|
158
|
+
if (plugin.dependencies) {
|
|
159
|
+
if (plugin.dependencies.some((dep) => !availablePlugins.includes(dep))) {
|
|
160
|
+
console.warn("MDXEditor plugin", plugin.id, "has some missing dependencies", plugin.dependencies, ", skipping");
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return true;
|
|
165
|
+
});
|
|
166
|
+
return validPlugins2;
|
|
167
|
+
}, [plugins]);
|
|
168
|
+
const realm = React__default.useMemo(() => {
|
|
169
|
+
const specs = validPlugins.map((plugin) => plugin.systemSpec);
|
|
170
|
+
const pluginIds = validPlugins.map((plugin) => plugin.id);
|
|
171
|
+
const realm2 = realmFactory(...specs);
|
|
172
|
+
validPlugins.forEach((plugin) => {
|
|
173
|
+
var _a, _b;
|
|
174
|
+
(_a = plugin.init) == null ? void 0 : _a.call(plugin, realm2, plugin.pluginParams, pluginIds);
|
|
175
|
+
(_b = plugin.applyParamsToSystem) == null ? void 0 : _b.call(plugin, realm2, plugin.pluginParams);
|
|
176
|
+
});
|
|
177
|
+
return realm2;
|
|
178
|
+
}, []);
|
|
179
|
+
React__default.useEffect(() => {
|
|
180
|
+
validPlugins.forEach((plugin) => {
|
|
181
|
+
var _a;
|
|
182
|
+
(_a = plugin.applyParamsToSystem) == null ? void 0 : _a.call(plugin, realm, plugin.pluginParams);
|
|
183
|
+
});
|
|
184
|
+
}, [realm, validPlugins]);
|
|
185
|
+
const Context = GurxContext;
|
|
186
|
+
return React__default.createElement(
|
|
187
|
+
Context.Provider,
|
|
188
|
+
{ value: realm },
|
|
189
|
+
React__default.createElement(UsedPluginsContext.Provider, { value: new Set(plugins.map((plugin) => plugin.id)) }, children)
|
|
190
|
+
);
|
|
191
|
+
};
|
|
192
|
+
function useHasPlugin(id) {
|
|
193
|
+
const usedPlugins = React__default.useContext(UsedPluginsContext);
|
|
194
|
+
return usedPlugins.has(id);
|
|
195
|
+
}
|
|
196
|
+
const RequirePlugin = ({ id, children }) => {
|
|
197
|
+
return useHasPlugin(id) ? React__default.createElement(React__default.Fragment, {}, children) : null;
|
|
198
|
+
};
|
|
199
|
+
export {
|
|
200
|
+
RealmPluginInitializer,
|
|
201
|
+
RequirePlugin,
|
|
202
|
+
realmFactoryToComponent,
|
|
203
|
+
realmPlugin,
|
|
204
|
+
sysHooks,
|
|
205
|
+
useHasPlugin
|
|
206
|
+
};
|