Multiple instance of the editor exist? #1665
-
Hello! I'm working with milkdown to create a small page to edit, but I've noticed that when I load up the components for this, there are 3 different boxes that can be clicked into and edited. They seem to exist withing the useEditor so it's not like they are duplicate renders, when I type into one of the empty lines, I log that the changes for that line are completely separate from the others. I'm just a bit confused as to why these extra editors exist inside of my render, is this normal and expected or am I doing something wrong with the code? When examining the elements, I see:
Only the last div contains the information I'm passing into the component. Here is my editor react component:
I did try to remove the key from the WikiEditor but that didn't seem to do anything. Curious if anyone has any ideas or what may be going on. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I'm getting this as well, first time there is only one and then next time there are two. Here is my code: import type {FC} from "react";
import {Milkdown, useEditor} from "@milkdown/react";
import {emoji} from "@milkdown/plugin-emoji";
import {Crepe} from "@milkdown/crepe";
import {fetchWithCSRF} from "@/helpers/common/csrf";
import {useRef, useEffect} from "react";
interface MilkdownEditorProps {
markdown: string;
setMarkdown: (markdown: string) => void;
}
export const MilkdownEditor: FC<MilkdownEditorProps> = ({
markdown,
setMarkdown,
}) => {
// Store the Crepe instance so we can manually destroy it later.
const editorInstance = useRef<Crepe | null>(null);
useEditor((root) => {
const crepe = new Crepe({
root,
defaultValue: markdown,
features: {
[Crepe.Feature.Latex]: false,
},
featureConfigs: {
[Crepe.Feature.Placeholder]: {
text: "Type something here...",
},
[Crepe.Feature.ImageBlock]: {
onUpload: async (file: File) => {
const formData = new FormData();
formData.append("file", file);
// Provide alt_text and caption in a JSON payload field
const payload = {
alt_text: "Uploaded image",
caption: "Uploaded via Milkdown editor",
};
formData.append("payload", JSON.stringify(payload));
const res = await fetchWithCSRF(
`${process.env.NEXT_PUBLIC_API_URL}/api/blog/gallery`,
{
method: "POST",
body: formData,
},
);
if (!res.ok) {
throw new Error("Image upload failed");
}
const data = await res.json();
// Assuming the API returns an object with a URL field
return data.image;
},
},
},
});
crepe.editor.use(emoji);
crepe.on((listener) => {
listener.markdownUpdated((_, markdown, prevMarkdown) => {
if (markdown !== prevMarkdown) {
console.log("Markdown updated", markdown);
setMarkdown(markdown);
}
});
});
// Save the instance for cleanup.
editorInstance.current = crepe;
return crepe;
}, []);
// When the component unmounts, manually destroy the Crepe instance.
useEffect(() => {
return () => {
if (editorInstance.current) {
editorInstance.current.destroy();
editorInstance.current = null;
}
};
}, []);
return <Milkdown />;
};
export default MilkdownEditor; Really not sure what is causing it. |
Beta Was this translation helpful? Give feedback.
-
Should be fixed by #1679 |
Beta Was this translation helpful? Give feedback.
-
Hey @Saul-Mirone, any idea when the fix from #1679 will get pushed to npm? |
Beta Was this translation helpful? Give feedback.
Should be fixed by #1679