2 * Copyright (c) Meta Platforms, Inc. and affiliates.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
9 import type {Binding} from './Bindings';
10 import type {LexicalCommand} from 'lexical';
11 import type {Doc, RelativePosition, UndoManager, XmlText} from 'yjs';
13 import {createCommand} from 'lexical';
14 import {UndoManager as YjsUndoManager} from 'yjs';
16 export type UserState = {
17 anchorPos: null | RelativePosition;
20 focusPos: null | RelativePosition;
22 awarenessData: object;
24 export const CONNECTED_COMMAND: LexicalCommand<boolean> =
25 createCommand('CONNECTED_COMMAND');
26 export const TOGGLE_CONNECT_COMMAND: LexicalCommand<boolean> = createCommand(
27 'TOGGLE_CONNECT_COMMAND',
29 export type ProviderAwareness = {
30 getLocalState: () => UserState | null;
31 getStates: () => Map<number, UserState>;
32 off: (type: 'update', cb: () => void) => void;
33 on: (type: 'update', cb: () => void) => void;
34 setLocalState: (arg0: UserState) => void;
36 declare interface Provider {
37 awareness: ProviderAwareness;
38 connect(): void | Promise<void>;
40 off(type: 'sync', cb: (isSynced: boolean) => void): void;
41 off(type: 'update', cb: (arg0: unknown) => void): void;
42 off(type: 'status', cb: (arg0: {status: string}) => void): void;
43 off(type: 'reload', cb: (doc: Doc) => void): void;
44 on(type: 'sync', cb: (isSynced: boolean) => void): void;
45 on(type: 'status', cb: (arg0: {status: string}) => void): void;
46 on(type: 'update', cb: (arg0: unknown) => void): void;
47 on(type: 'reload', cb: (doc: Doc) => void): void;
49 export type Operation = {
53 insert: string | Record<string, unknown>;
55 export type Delta = Array<Operation>;
56 export type YjsNode = Record<string, unknown>;
57 export type YjsEvent = Record<string, unknown>;
58 export type {Provider};
59 export type {Binding, ClientID, ExcludedProperties} from './Bindings';
60 export {createBinding} from './Bindings';
62 export function createUndoManager(
66 return new YjsUndoManager(root, {
67 trackedOrigins: new Set([binding, null]),
71 export function initLocalState(
76 awarenessData: object,
78 provider.awareness.setLocalState({
88 export function setLocalStateFocus(
93 awarenessData: object,
95 const {awareness} = provider;
96 let localState = awareness.getLocalState();
98 if (localState === null) {
109 localState.focusing = focusing;
110 awareness.setLocalState(localState);
112 export {syncCursorPositions} from './SyncCursors';
114 syncLexicalUpdateToYjs,
115 syncYjsChangesToLexical,
116 } from './SyncEditorStates';