2 * Copyright (c) Meta Platforms, Inc. and affiliates.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
9 import type {CreateEditorArgs, LexicalEditor} from 'lexical';
11 import {createEditor} from 'lexical';
14 * Generates a headless editor that allows lexical to be used without the need for a DOM, eg in Node.js.
15 * Throws an error when unsupported methods are used.
16 * @param editorConfig - The optional lexical editor configuration.
17 * @returns - The configured headless editor.
19 export function createHeadlessEditor(
20 editorConfig?: CreateEditorArgs,
22 const editor = createEditor(editorConfig);
23 editor._headless = true;
25 const unsupportedMethods = [
26 'registerDecoratorListener',
27 'registerRootListener',
28 'registerMutationListener',
36 unsupportedMethods.forEach((method: typeof unsupportedMethods[number]) => {
37 editor[method] = () => {
38 throw new Error(`${method} is not supported in headless mode`);