]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/lexical/headless/index.ts
respective book and chapter structure added.
[bookstack] / resources / js / wysiwyg / lexical / headless / index.ts
1 /**
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  *
7  */
8
9 import type {CreateEditorArgs, LexicalEditor} from 'lexical';
10
11 import {createEditor} from 'lexical';
12
13 /**
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.
18  */
19 export function createHeadlessEditor(
20   editorConfig?: CreateEditorArgs,
21 ): LexicalEditor {
22   const editor = createEditor(editorConfig);
23   editor._headless = true;
24
25   const unsupportedMethods = [
26     'registerDecoratorListener',
27     'registerRootListener',
28     'registerMutationListener',
29     'getRootElement',
30     'setRootElement',
31     'getElementByKey',
32     'focus',
33     'blur',
34   ] as const;
35
36   unsupportedMethods.forEach((method: typeof unsupportedMethods[number]) => {
37     editor[method] = () => {
38       throw new Error(`${method} is not supported in headless mode`);
39     };
40   });
41
42   return editor;
43 }