import type { Ancestor, Descendant, DescendantIn, Location, Node, NodeEntry, NodeTypeSelector, Path, Point, Text, } from '../interfaces'; import type { AnyEditor as Editor, EditorAboveOptions, EditorCommit, EditorCommitListener, EditorCommitSource, EditorDirectCommandDispatch, PluginReference, PluginDefinitionInput, PluginInput, PluginReconfigureOptions, EditorDocumentValue, EditorLeafOptions, EditorLevelsOptions, EditorNextOptions, EditorParentOptions, EditorPointOptions, EditorPreviousOptions, EditorSnapshot, EditorStateView, EditorStaticApi, EditorUpdateContext, EditorUpdateTransaction, RootKey, NodeKey, Selection, SnapshotListener, Value, } from '../interfaces/editor'; import type { DocumentChange } from './change/document-change'; import type { NodeList } from './change/document-index'; import type { InternalEditorSchemaApi } from './editor-schema'; import type { EditorSchemaContract } from './schema-compiler'; import type { InternalEditorUpdateOptions } from './update-policy'; import type { ViewPresentation } from './view-presentation'; type BindEditorMethod = T extends ( editor: Editor, ...args: infer Args ) => infer Result ? (...args: Args) => Result : never; type RuntimeMethods = { [Key in TKey]: BindEditorMethod; }; export type InternalEditorReadRuntime = RuntimeMethods< | 'after' | 'before' | 'edges' | 'elementReadOnly' | 'first' | 'fragment' | 'hasBlocks' | 'hasInlines' | 'hasPath' | 'hasTexts' | 'isBlock' | 'isEdge' | 'isEmpty' | 'isEnd' | 'isStart' | 'last' | 'path' | 'positions' | 'projectRange' | 'range' | 'shouldMergeNodesRemovePrevNode' | 'string' | 'unhangRange' | 'void' > & { above: ( options?: EditorAboveOptions ) => NodeEntry | undefined; leaf: (at: Location, options?: EditorLeafOptions) => NodeEntry; levels: ( options?: EditorLevelsOptions ) => Generator, void, undefined>; next: ( options?: EditorNextOptions ) => NodeEntry | undefined; parent: { ( at: Location, options: EditorParentOptions & { type: NodeTypeSelector; } ): NodeEntry | undefined; ( at: Location, options?: EditorParentOptions ): NodeEntry; }; point: (at: Location, options?: EditorPointOptions) => Point; previous: ( options?: EditorPreviousOptions ) => NodeEntry | undefined; }; export type InternalEditorSnapshotRuntime = { getChildList: () => NodeList; getChildren: () => V; getFragment: () => Array>; getLastCommit: () => EditorCommit | null; getPathByNodeKey: (nodeKey: NodeKey) => Path | null; getNodeKey: (path: Path) => NodeKey | null; getSelection: () => Selection; getSnapshot: () => EditorSnapshot; }; export type InternalEditorTransactionRuntime = { read: (fn: (state: EditorStateView) => T) => T; runCommand: EditorDirectCommandDispatch>; subscribe: (listener: SnapshotListener) => () => void; subscribeCommit: (listener: EditorCommitListener) => () => void; subscribeSource: ( source: EditorCommitSource, listener: SnapshotListener ) => () => void; update: ( fn: ( transaction: EditorUpdateTransaction, context: EditorUpdateContext> ) => R, options?: InternalEditorUpdateOptions ) => R | undefined; }; export type InternalPluginRuntime = { install: Editor['install']; preparePluginPublication: ( entries: readonly InternalPluginPublicationEntry[], options?: PluginReconfigureOptions ) => Readonly<{ commit: () => void; configurationChanged: boolean; documentChange: DocumentChange; finalize: () => void; afterPublish: () => void; beforePublish: () => void; handUninstalls: (stagedInOrder: readonly StagedPluginInstall[]) => void; rollback: () => void; schemaContract: () => EditorSchemaContract; stage: () => void; validateDocument: (value: EditorDocumentValue) => void; }>; schema: InternalEditorSchemaApi; }; export type InternalCompiledPluginPublicationEntry = Readonly<{ /** Shared nominal author identity installed by this entry. */ descriptor: PluginReference; /** Immutable normalized behavior compiled for this editor. */ definition: PluginDefinitionInput & Readonly<{ name: string }>; editor?: Editor; /** Eligible author ancestors that resolve to the installed descriptor. */ sources: readonly PluginReference[]; /** Optional owner projection for richer package-specific portals. */ createPortal?: (editor: Editor, descriptor: PluginReference) => unknown; }>; export type StagedPluginInstall = Readonly<{ input: PluginInput; onPublished?: (uninstall: ReturnType) => void; }>; export type InternalPluginPublicationEntry = | InternalCompiledPluginPublicationEntry | Readonly<{ /** Raw author descriptor normalized at the publication boundary. */ editor?: Editor; plugin: PluginReference; }>; export type ViewFocus = Readonly<{ composing: boolean; focused: boolean }>; export type ViewPolicySource = Readonly<{ current: () => InternalEditorRuntime | undefined; focusOf?: ( target: InternalEditorRuntime | undefined ) => ViewFocus | undefined; subscribe?: (listener: () => void) => () => void; }>; export type InternalEditorRuntime = InternalPluginRuntime & InternalEditorReadRuntime & InternalEditorSnapshotRuntime & InternalEditorTransactionRuntime & { setViewState?: ( key: 'composing' | 'focused' | 'readOnly', value: boolean ) => boolean; registerSurface?: (readOnly: boolean) => () => void; hasSurface?: () => boolean; mountedIn?: InternalEditorRuntime; policySource?: ViewPolicySource; presentation?: ViewPresentation; }; const EDITOR_RUNTIME = new WeakMap(); const EDITOR_RUNTIME_OWNER = new WeakMap(); const EDITOR_RUNTIME_ROOT = new WeakMap(); export const setEditorRuntime = ( editor: Editor, runtime: InternalEditorRuntime, owner: Editor = editor, root: RootKey = 'main' ) => { EDITOR_RUNTIME.set(editor, runtime as unknown as InternalEditorRuntime); EDITOR_RUNTIME_OWNER.set(editor, owner); EDITOR_RUNTIME_ROOT.set(editor, root); }; export const hasEditorRuntime = (value: unknown): value is Editor => typeof value === 'object' && value !== null && EDITOR_RUNTIME.has(value as Editor); export const getEditorRuntime = ( editor: Editor ): InternalEditorRuntime => { const runtime = EDITOR_RUNTIME.get(editor); if (!runtime) { throw new Error('Editor runtime has not been initialized'); } return runtime as unknown as InternalEditorRuntime; }; export type EditorRuntimeCarrier = Readonly<{ id: string; read: object; update: object; }>; export const getEditorRuntimeOwner = (editor: EditorRuntimeCarrier): Editor => EDITOR_RUNTIME_OWNER.get(editor as Editor) ?? (editor as Editor); export const getEditorRuntimeRoot = (editor: Editor): RootKey => EDITOR_RUNTIME_ROOT.get(editor) ?? 'main'; export const getEditorSchema = ( editor: Editor ): InternalEditorSchemaApi => getEditorRuntime(editor).schema;