]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/lexical/yjs/Bindings.ts
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / resources / js / wysiwyg / lexical / yjs / Bindings.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 {CollabDecoratorNode} from './CollabDecoratorNode';
10 import type {CollabElementNode} from './CollabElementNode';
11 import type {CollabLineBreakNode} from './CollabLineBreakNode';
12 import type {CollabTextNode} from './CollabTextNode';
13 import type {Cursor} from './SyncCursors';
14 import type {LexicalEditor, NodeKey} from 'lexical';
15 import type {Doc} from 'yjs';
16
17 import {Klass, LexicalNode} from 'lexical';
18 import invariant from 'lexical/shared/invariant';
19 import {XmlText} from 'yjs';
20
21 import {Provider} from '.';
22 import {$createCollabElementNode} from './CollabElementNode';
23
24 export type ClientID = number;
25 export type Binding = {
26   clientID: number;
27   collabNodeMap: Map<
28     NodeKey,
29     | CollabElementNode
30     | CollabTextNode
31     | CollabDecoratorNode
32     | CollabLineBreakNode
33   >;
34   cursors: Map<ClientID, Cursor>;
35   cursorsContainer: null | HTMLElement;
36   doc: Doc;
37   docMap: Map<string, Doc>;
38   editor: LexicalEditor;
39   id: string;
40   nodeProperties: Map<string, Array<string>>;
41   root: CollabElementNode;
42   excludedProperties: ExcludedProperties;
43 };
44 export type ExcludedProperties = Map<Klass<LexicalNode>, Set<string>>;
45
46 export function createBinding(
47   editor: LexicalEditor,
48   provider: Provider,
49   id: string,
50   doc: Doc | null | undefined,
51   docMap: Map<string, Doc>,
52   excludedProperties?: ExcludedProperties,
53 ): Binding {
54   invariant(
55     doc !== undefined && doc !== null,
56     'createBinding: doc is null or undefined',
57   );
58   const rootXmlText = doc.get('root', XmlText) as XmlText;
59   const root: CollabElementNode = $createCollabElementNode(
60     rootXmlText,
61     null,
62     'root',
63   );
64   root._key = 'root';
65   return {
66     clientID: doc.clientID,
67     collabNodeMap: new Map(),
68     cursors: new Map(),
69     cursorsContainer: null,
70     doc,
71     docMap,
72     editor,
73     excludedProperties: excludedProperties || new Map(),
74     id,
75     nodeProperties: new Map(),
76     root,
77   };
78 }