Replies: 7 comments 6 replies
-
You can instantiate a new Crepe instance within the useEditor(
(root) =>
new Crepe({root}).editor); Something like the above should work in this case. |
Beta Was this translation helpful? Give feedback.
-
You can use crepe according to the way of milkdown website using it. Link In short, initiate the import { Crepe } from "@milkdown/crepe";
import { FC, useLayoutEffect, useRef } from "react";
const HomeEditor: FC<{ value: string }> = ({ value }) => {
const divRef = useRef<HTMLDivElement>(null);
const loading = useRef(false);
useLayoutEffect(() => {
if (!divRef.current) return;
loading.current = true;
const crepe = new Crepe({
root: divRef.current,
defaultValue: value,
features: {
[Crepe.Feature.CodeMirror]: false,
[Crepe.Feature.BlockEdit]: false,
},
});
crepe.create().then(() => {
loading.current = false;
});
return () => {
crepe.destroy();
};
}, [ value]);
return (
<div
ref={divRef}
className="crepe !h-80 overflow-auto rounded-2xl border border-nord-outline shadow dark:border-nord-outline-dark md:!h-[480px]"
/>
);
};
export default HomeEditor; |
Beta Was this translation helpful? Give feedback.
-
There is PR for this but it's not ready for merge yet: #1561 (comment) |
Beta Was this translation helpful? Give feedback.
-
Please follow https://milkdown.dev/docs/recipes/react#using-crepe |
Beta Was this translation helpful? Give feedback.
-
How can I add a event listener so that i can capture the state change in react??
import { type FC } from "react";
import { Crepe } from "@milkdown/crepe";
import { Milkdown, useEditor } from "@milkdown/react";
import "@milkdown/crepe/theme/common/style.css";
import "@milkdown/crepe/theme/frame.css";
interface MilkdownEditorProps {
markdown: string;
setMarkdown: (markdown: string) => void;
}
export const MilkdownEditor: FC<MilkdownEditorProps> = ({
markdown,
setMarkdown,
}) => {
useEditor(
(root) => {
const crepe = new Crepe({
root,
defaultValue: markdown,
});
return crepe;
},
[markdown]
);
return (
<>
<Milkdown />
</>
);
}; |
Beta Was this translation helpful? Give feedback.
-
Yes, it would be good to know, how to access the markdown or to be notified of a state change as I want to use this editor in a form and the react tuorial doesn't expakin how the markdown is exposed. |
Beta Was this translation helpful? Give feedback.
-
Hey, I have found a solution: we could use the import { type FC } from "react";
import { Crepe } from "@milkdown/crepe";
import { Milkdown, useEditor } from "@milkdown/react";
import "@milkdown/crepe/theme/common/style.css";
import "@milkdown/crepe/theme/frame.css";
import { listener, listenerCtx } from "@milkdown/plugin-listener";
interface MilkdownEditorProps {
markdown: string;
setMarkdown: (markdown: string) => void;
}
export const MilkdownEditor: FC<MilkdownEditorProps> = ({
markdown,
setMarkdown,
}) => {
useEditor((root) => {
const crepe = new Crepe({
root,
defaultValue: markdown,
});
crepe.editor.use(listener);
crepe.editor.config((ctx) => {
const listener = ctx.get(listenerCtx);
listener.markdownUpdated((_, markdown, prevMarkdown) => {
if (markdown !== prevMarkdown) {
console.log("Markdown updated", markdown);
setMarkdown(markdown);
}
});
});
return crepe;
}, []);
return (
<>
<Milkdown />
</>
);
}; |
Beta Was this translation helpful? Give feedback.
-
maybe useCrepe() hook is missing? https://milkdown.dev/docs/recipes/react
Beta Was this translation helpful? Give feedback.
All reactions