e.setEditorFactory is not a function I have been dealing with a issue please anyone can help #1068
Replies: 3 comments
-
import { useLinkClass } from "./EditorComponent/hooks"; const Button: FC<{ icon: string; onClick?: () => void }> = ({ interface MilkdownProps { export interface MilkdownRef { export const PlaygroundMilkdown: FC = ({ useImperativeHandle(milkdownRef, () => ({ function call(command: CmdKey, payload?: T) { return ( <Button icon="undo" onClick={() => call(undoCommand.key)} /> <Button icon="redo" onClick={() => call(redoCommand.key)} /> <Button icon="format_bold" onClick={() => call(toggleStrongCommand.key)} /> <Button icon="format_italic" onClick={() => call(toggleEmphasisCommand.key)} /> <Button icon="format_strikethrough" onClick={() => call(toggleStrikethroughCommand.key)} /> <Button icon="table" onClick={() => call(insertTableCommand.key)} /> <Button icon="format_list_bulleted" onClick={() => call(wrapInBulletListCommand.key)} /> <Button icon="format_list_numbered" onClick={() => call(wrapInOrderedListCommand.key)} /> <Button icon="format_quote" onClick={() => call(wrapInBlockquoteCommand.key)} />
); |
Beta Was this translation helpful? Give feedback.
-
I just worked this out - you have to wrap in |
Beta Was this translation helpful? Give feedback.
-
import { CodeBlock } from "./EditorComponent/CodeBlock";
import { Diagram } from "./EditorComponent/Diagram";
import { FootnoteDef, FootnoteRef } from "./EditorComponent/Footnote";
import {
defaultValueCtx,
Editor,
editorViewOptionsCtx,
rootCtx,
} from "@milkdown/core";
import type { Ctx, MilkdownPlugin } from "@milkdown/ctx";
import { clipboard } from "@milkdown/plugin-clipboard";
import { cursor } from "@milkdown/plugin-cursor";
import { diagram, diagramSchema } from "@milkdown/plugin-diagram";
import { history } from "@milkdown/plugin-history";
import { indent } from "@milkdown/plugin-indent";
import { listener, listenerCtx } from "@milkdown/plugin-listener";
import { prism, prismConfig } from "@milkdown/plugin-prism";
import { trailing } from "@milkdown/plugin-trailing";
import { upload } from "@milkdown/plugin-upload";
import {
codeBlockSchema,
commonmark,
listItemSchema,
} from "@milkdown/preset-commonmark";
import {
footnoteDefinitionSchema,
footnoteReferenceSchema,
gfm,
} from "@milkdown/preset-gfm";
import { useEditor } from "@milkdown/react";
import { nord } from "@milkdown/theme-nord";
import { $view } from "@milkdown/utils";
import {
useNodeViewFactory,
usePluginViewFactory,
useWidgetViewFactory,
} from "@prosemirror-adapter/react";
import debounce from "lodash.debounce";
import { useEffect, useMemo } from "react";
import { refractor } from "refractor/lib/common";
import { ImageTooltip, imageTooltip } from "./EditorComponent/ImageTooltip";
import { ListItem } from "./EditorComponent/ListItem";
import {
tableSelectorPlugin,
TableTooltip,
tableTooltip,
tableTooltipCtx,
} from "./EditorComponent/TableWidget";
export const usePlayground = (
defaultValue: string,
onChange: (markdown: string) => void
) => {
const pluginViewFactory = usePluginViewFactory();
const nodeViewFactory = useNodeViewFactory();
const widgetViewFactory = useWidgetViewFactory();
const gfmPlugins: MilkdownPlugin[] = useMemo(() => {
return [
gfm,
tableTooltip,
tableTooltipCtx,
(ctx: Ctx) => async () => {
ctx.set(tableTooltip.key, {
view: pluginViewFactory({
component: TableTooltip,
}),
});
},
$view(footnoteDefinitionSchema.node, () =>
nodeViewFactory({ component: FootnoteDef })
),
$view(footnoteReferenceSchema.node, () =>
nodeViewFactory({ component: FootnoteRef })
),
tableSelectorPlugin(widgetViewFactory),
].flat();
}, [nodeViewFactory, pluginViewFactory, widgetViewFactory]);
console.log("1", "usePlayground");
const diagramPlugins: MilkdownPlugin[] = useMemo(() => {
return [
diagram,
$view(diagramSchema.node, () =>
nodeViewFactory({
component: Diagram as any,
})
),
].flat();
}, [nodeViewFactory]);
console.log("2", "usePlayground");
const editorInfo = useEditor(
(root) => {
return Editor.make()
.enableInspector()
.config((ctx) => {
ctx.update(editorViewOptionsCtx, (prev) => ({
...prev,
attributes: {
class: "mx-auto px-2 py-4 box-border",
},
}));
ctx.set(rootCtx, root);
ctx.set(defaultValueCtx, defaultValue);
ctx
.get(listenerCtx)
.markdownUpdated((, markdown) => {
debounce(onChange, 100)(markdown);
})
.updated((, doc) => {});
ctx.update(prismConfig.key, (prev) => ({
...prev,
configureRefractor: () => refractor,
}));
ctx.set(imageTooltip.key, {
view: pluginViewFactory({
component: ImageTooltip,
}),
});
})
.config(nord)
.use(commonmark)
.use(listener)
.use(clipboard)
.use(history)
.use(cursor)
.use(prism)
.use(indent)
.use(upload)
.use(trailing)
.use(imageTooltip)
.use(
$view(listItemSchema.node, () =>
nodeViewFactory({ component: ListItem })
)
)
.use(
$view(codeBlockSchema.node, () =>
nodeViewFactory({ component: CodeBlock })
)
);
},
[onChange, defaultValue]
);
const { get, loading } = editorInfo;
useEffect(() => {
console.log("3", "usePlayground");
requestAnimationFrame(() => {
const effect = async () => {
const editor = get();
if (!editor) return;
const enableGFM = true;
if (enableGFM) {
editor.use(gfmPlugins);
} else {
await editor.remove(gfmPlugins);
}
const enableDiagram = true;
}, [diagramPlugins, get, gfmPlugins, loading]);
useEffect(() => {
onChange(defaultValue);
}, [defaultValue, onChange]);
return editorInfo;
};
Beta Was this translation helpful? Give feedback.
All reactions