You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to convert html format to lexical as per documentation.
I am able to convert all other nodes but heading nodes don't work i get this error:
Error code #16
Unable to find an active editor. This method can only be used synchronously during the callback of editor.update().
This is my conversion code:
`import { JSDOM } from "jsdom";
import { $generateNodesFromDOM } from "@lexical/html";
import {
$getRoot,
$getSelection,
Klass,
LexicalNode,
LexicalNodeReplacement,
} from "lexical";
import {
getEnabledNodes,
sanitizeEditorConfig,
defaultEditorConfig,
defaultEditorFeatures,
} from "@payloadcms/richtext-lexical";
import { createHeadlessEditor } from "@lexical/headless"; // <= make sure this package is installed
export function HtmlToLexicalSerializer(htmlString: string) {
const myEditorConfig = defaultEditorConfig;
myEditorConfig.features = [...defaultEditorFeatures];
const headlessEditor = createHeadlessEditor({
nodes: getEnabledNodes({
editorConfig: sanitizeEditorConfig(myEditorConfig),
}) as unknown as readonly (Klass | LexicalNodeReplacement)[],
});
console.log(htmlString);
headlessEditor.update(
() => {
// In a headless environment you can use a package such as JSDom to parse the HTML string.
const dom = new JSDOM(htmlString);
// Once you have the DOM instance it's easy to generate LexicalNodes.
const nodes = $generateNodesFromDOM(headlessEditor, dom.window.document);
// Select the root
$getRoot().select();
// Insert them at a selection.
const selection = $getSelection();
selection.insertNodes(nodes);
},
{ discrete: true }
);
// Do this if you then want to get the editor JSON
const editorJSON = headlessEditor.getEditorState().toJSON();
console.dir(editorJSON);
console.dir(editorJSON.root.children);
return editorJSON;
}
`
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to convert html format to lexical as per documentation.
I am able to convert all other nodes but heading nodes don't work i get this error:
Error code #16
Unable to find an active editor. This method can only be used synchronously during the callback of editor.update().
This is my conversion code:
`import { JSDOM } from "jsdom";
import { $generateNodesFromDOM } from "@lexical/html";
import {
$getRoot,
$getSelection,
Klass,
LexicalNode,
LexicalNodeReplacement,
} from "lexical";
import {
getEnabledNodes,
sanitizeEditorConfig,
defaultEditorConfig,
defaultEditorFeatures,
} from "@payloadcms/richtext-lexical";
import { createHeadlessEditor } from "@lexical/headless"; // <= make sure this package is installed
export function HtmlToLexicalSerializer(htmlString: string) {
const myEditorConfig = defaultEditorConfig;
myEditorConfig.features = [...defaultEditorFeatures];
const headlessEditor = createHeadlessEditor({
nodes: getEnabledNodes({
editorConfig: sanitizeEditorConfig(myEditorConfig),
}) as unknown as readonly (Klass | LexicalNodeReplacement)[],
});
console.log(htmlString);
headlessEditor.update(
() => {
// In a headless environment you can use a package such as JSDom to parse the HTML string.
const dom = new JSDOM(htmlString);
);
// Do this if you then want to get the editor JSON
const editorJSON = headlessEditor.getEditorState().toJSON();
console.dir(editorJSON);
console.dir(editorJSON.root.children);
return editorJSON;
}
`
Beta Was this translation helpful? Give feedback.
All reactions