]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/lexical/yjs/CollabLineBreakNode.ts
Images: Added testing to cover animated avif handling
[bookstack] / resources / js / wysiwyg / lexical / yjs / CollabLineBreakNode.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 {Binding} from '.';
10 import type {CollabElementNode} from './CollabElementNode';
11 import type {LineBreakNode, NodeKey} from 'lexical';
12 import type {Map as YMap} from 'yjs';
13
14 import {$getNodeByKey, $isLineBreakNode} from 'lexical';
15
16 export class CollabLineBreakNode {
17   _map: YMap<unknown>;
18   _key: NodeKey;
19   _parent: CollabElementNode;
20   _type: 'linebreak';
21
22   constructor(map: YMap<unknown>, parent: CollabElementNode) {
23     this._key = '';
24     this._map = map;
25     this._parent = parent;
26     this._type = 'linebreak';
27   }
28
29   getNode(): null | LineBreakNode {
30     const node = $getNodeByKey(this._key);
31     return $isLineBreakNode(node) ? node : null;
32   }
33
34   getKey(): NodeKey {
35     return this._key;
36   }
37
38   getSharedType(): YMap<unknown> {
39     return this._map;
40   }
41
42   getType(): string {
43     return this._type;
44   }
45
46   getSize(): number {
47     return 1;
48   }
49
50   getOffset(): number {
51     const collabElementNode = this._parent;
52     return collabElementNode.getChildOffset(this);
53   }
54
55   destroy(binding: Binding): void {
56     const collabNodeMap = binding.collabNodeMap;
57     collabNodeMap.delete(this._key);
58   }
59 }
60
61 export function $createCollabLineBreakNode(
62   map: YMap<unknown>,
63   parent: CollabElementNode,
64 ): CollabLineBreakNode {
65   const collabNode = new CollabLineBreakNode(map, parent);
66   map._collabNode = collabNode;
67   return collabNode;
68 }