Replies: 2 comments 4 replies
-
You can use |
Beta Was this translation helpful? Give feedback.
4 replies
-
This is what I used to make controlled editor for https://aimd.app export const MilkdownEditor = ({
contentMarkdown,
onChange,
readOnly,
}: {
readonly contentMarkdown: string;
readonly onChange: (markdown: string) => void;
readonly readOnly: boolean;
}) => {
const editableRef = useRef<boolean>(!readOnly);
const [loading, get] = useInstance();
const action = useCallback(
(fn: (ctx: Ctx) => void) => {
if (loading) return null;
return get().action(fn);
},
[get, loading],
);
useEditor((root) => {
return Editor.make()
.config((ctx) => {
ctx.set(rootCtx, root);
ctx.set(defaultValueCtx, contentMarkdown);
})
.config((ctx) => {
ctx.update(editorViewOptionsCtx, (previous) => ({
...previous,
attributes: {
class: 'milkdown-aimd-theme',
},
editable: () => editableRef.current,
}));
const listener = ctx.get(listenerCtx);
listener.markdownUpdated((context_, markdown, previousMarkdown) => {
if (markdown !== previousMarkdown) {
onChange(markdown);
}
});
})
.use(indent)
.use(listener)
.use(commonmark);
}, []);
useEffect(() => {
if (loading) {
return;
}
const currentMarkdown = action(getMarkdown()) as unknown as string;
if (currentMarkdown === contentMarkdown) {
return;
}
action(replaceAll(contentMarkdown));
}, [action, contentMarkdown, loading]);
useEffect(() => {
editableRef.current = !readOnly;
}, [readOnly]);
return <Milkdown />;
}; I am not confident that this is the best way (as it feels a bit convoluted), but so far it works great. Relevant documentation: https://milkdown.dev/docs/api/utils#macros Relevant thread: #127 (comment) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is it possible to create a controlled Editor component using Milkdown?
I want to lift this state out of Milkdown into a parent component's state such that if the
value
prop changes I can update the value of the editor.value
contains the markdown string. I could be missing something obvious, or maybe its not possible?Beta Was this translation helpful? Give feedback.
All reactions