umaranis / svelte-lexical

Rich text editor for Svelte based on lexical
MIT License
353 stars 33 forks source link

importing a HTML file #96

Open pan2za opened 1 week ago

pan2za commented 1 week ago

I want to import a HTML file and rending it into the rich editor, there are 3 ways, 1 do as the lexical has suggested, getEditor, getRoot insertNode 2 make a HTML plugin like the Image plugin, there's so many difference there. 3 examine the copy/paste method in rich editor, which supports HTML content paste. Any suggestions?

pan2za commented 1 week ago

or make a HTML plugin like the Import plugin.

pan2za commented 4 days ago

I try to create a HTML plugin, the main INSERT command does not work, the code is,

onMount(() => { return editor.registerCommand( INSERT_HTML_COMMAND, (payload) => { console.log("insert html command received.") // In the browser you can use the native DOMParser API to parse the HTML string. const parser = new DOMParser(); const dom = parser.parseFromString(payload, "text/html"); // Once you have the DOM instance it's easy to generate LexicalNodes. const nodes = generateNodesFromDOM(editor, dom); // Select the root // getRoot().select(); // Insert them at a selection. insertNodes(nodes); return true; }, COMMAND_PRIORITY_EDITOR, ); });

pan2za commented 4 days ago

I've add a HTML plugin for this project. I will try to push to this project later...

pan2za commented 1 day ago

I've made a HTML plugin, the core code is pasted in the upper comment. but I'm not sure whether it's really needed by others.