Setting editor content in SvelteKit #1147
Replies: 1 comment
-
I've answered this question for myself. The trick is that you need to store a reference to the return value of Here are the relevant code sections: function editor(dom) {
editorRef = Editor.make()
.config((ctx) => {
ctx.set(rootCtx, dom);
ctx.get(listenerCtx).markdownUpdated((ctx, markdown, prevMarkdown) => {
markdownUpdatedEvent(markdown, prevMarkdown);
});
})
.use(commonmark)
.use(gfm)
.use(history)
.use(indent)
.use(listener)
.create();
} const setContent = async (content) => {
let res = await editorRef;
res.action((ctx) => {
const view = ctx.get(editorViewCtx);
const parser = ctx.get(parserCtx);
const doc = parser(content);
if (!doc) return;
const state = view.state;
view.dispatch(state.tr.replace(0, state.doc.content.size, new Slice(doc.content, 0, 0)));
});
}; |
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
-
I want to be able to erase the editor content, and I can't quite work out how. I found this discussion, but due to my lack of familiarity with Vue I'm struggling to translate it into Svelte code. If anyone could give me some pointers on the correct syntax, that would be hugely appreciated.
Beta Was this translation helpful? Give feedback.
All reactions