]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/lexical/core/nodes/LexicalDecoratorNode.ts
99d2669d92f918c616f5fb4b63996c2f3ecb07e8
[bookstack] / resources / js / wysiwyg / lexical / core / nodes / LexicalDecoratorNode.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 {KlassConstructor, LexicalEditor} from '../LexicalEditor';
10 import type {NodeKey} from '../LexicalNode';
11 import type {ElementNode} from './LexicalElementNode';
12
13 import {EditorConfig} from 'lexical';
14 import invariant from 'lexical/shared/invariant';
15
16 import {LexicalNode} from '../LexicalNode';
17
18 // eslint-disable-next-line @typescript-eslint/no-unused-vars
19 export interface DecoratorNode<T> {
20   getTopLevelElement(): ElementNode | this | null;
21   getTopLevelElementOrThrow(): ElementNode | this;
22 }
23
24 /** @noInheritDoc */
25 // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
26 export class DecoratorNode<T> extends LexicalNode {
27   ['constructor']!: KlassConstructor<typeof DecoratorNode<T>>;
28   constructor(key?: NodeKey) {
29     super(key);
30   }
31
32   /**
33    * The returned value is added to the LexicalEditor._decorators
34    */
35   decorate(editor: LexicalEditor, config: EditorConfig): T {
36     invariant(false, 'decorate: base method not extended');
37   }
38
39   isIsolated(): boolean {
40     return false;
41   }
42
43   isInline(): boolean {
44     return true;
45   }
46
47   isKeyboardSelectable(): boolean {
48     return true;
49   }
50 }
51
52 export function $isDecoratorNode<T>(
53   node: LexicalNode | null | undefined,
54 ): node is DecoratorNode<T> {
55   return node instanceof DecoratorNode;
56 }