BookStack/resources/js/wysiwyg/lexical/yjs/Bindings.ts
Dan Brown 22d078b47f
Lexical: Imported core lexical libs
Imported at 0.17.1, Modified to work in-app.
Added & configured test dependancies.
Tests need to be altered to avoid using non-included deps including
react dependancies.
2024-09-18 13:43:39 +01:00

79 lines
2.0 KiB
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type {CollabDecoratorNode} from './CollabDecoratorNode';
import type {CollabElementNode} from './CollabElementNode';
import type {CollabLineBreakNode} from './CollabLineBreakNode';
import type {CollabTextNode} from './CollabTextNode';
import type {Cursor} from './SyncCursors';
import type {LexicalEditor, NodeKey} from 'lexical';
import type {Doc} from 'yjs';
import {Klass, LexicalNode} from 'lexical';
import invariant from 'lexical/shared/invariant';
import {XmlText} from 'yjs';
import {Provider} from '.';
import {$createCollabElementNode} from './CollabElementNode';
export type ClientID = number;
export type Binding = {
clientID: number;
collabNodeMap: Map<
NodeKey,
| CollabElementNode
| CollabTextNode
| CollabDecoratorNode
| CollabLineBreakNode
>;
cursors: Map<ClientID, Cursor>;
cursorsContainer: null | HTMLElement;
doc: Doc;
docMap: Map<string, Doc>;
editor: LexicalEditor;
id: string;
nodeProperties: Map<string, Array<string>>;
root: CollabElementNode;
excludedProperties: ExcludedProperties;
};
export type ExcludedProperties = Map<Klass<LexicalNode>, Set<string>>;
export function createBinding(
editor: LexicalEditor,
provider: Provider,
id: string,
doc: Doc | null | undefined,
docMap: Map<string, Doc>,
excludedProperties?: ExcludedProperties,
): Binding {
invariant(
doc !== undefined && doc !== null,
'createBinding: doc is null or undefined',
);
const rootXmlText = doc.get('root', XmlText) as XmlText;
const root: CollabElementNode = $createCollabElementNode(
rootXmlText,
null,
'root',
);
root._key = 'root';
return {
clientID: doc.clientID,
collabNodeMap: new Map(),
cursors: new Map(),
cursorsContainer: null,
doc,
docMap,
editor,
excludedProperties: excludedProperties || new Map(),
id,
nodeProperties: new Map(),
root,
};
}