import type { Priority } from '@prosekit/core' import { union, withPriority, type PlainExtension, type Union } from '@prosekit/core' import type { CursorAwareness, CursorEphemeralStore, CursorPluginOptions, LoroDocType, LoroSyncPluginProps, LoroUndoPluginProps, } from 'loro-prosemirror' import { defineLoroCommands, type LoroCommandsExtension } from './loro-commands.ts' import { defineLoroCursorPlugin } from './loro-cursor-plugin.ts' import { defineLoroKeymap } from './loro-keymap.ts' import { defineLoroSyncPlugin } from './loro-sync-plugin.ts' import { defineLoroUndoPlugin } from './loro-undo-plugin.ts' export interface LoroOptions { /** * The Loro instance handles the state of shared data. */ doc: LoroDocType /** * The (legacy) Awareness instance. One of `awareness` or `presence` must be provided. */ awareness?: CursorAwareness /** * The CursorEphemeralStore instance. One of `awareness` or `presence` must be provided. */ presence?: CursorEphemeralStore /** * Extra options for `LoroSyncPlugin`. */ sync?: Omit /** * Extra options for the `LoroUndoPlugin`. */ undo?: Omit /** * Extra options for `LoroCursorPlugin` or `LoroEphemeralCursorPlugin`. */ cursor?: CursorPluginOptions } /** * @internal */ export type LoroExtension = Union<[LoroCommandsExtension, PlainExtension]> export function defineLoro(options: LoroOptions): LoroExtension { const { doc, awareness, presence, sync, undo, cursor } = options return withPriority( union([ defineLoroKeymap(), defineLoroCommands(), defineLoroCursorPlugin({ ...cursor, awareness, presence }), defineLoroUndoPlugin({ ...undo, doc }), defineLoroSyncPlugin({ ...sync, doc }), ]), 3 satisfies typeof Priority.high, ) }