import { Cursor } from "./cursor"; import type { StrandPath } from "./path"; import { Strand } from "./strand"; import { h, VNode } from "./vdom"; import { SelectionRange } from "./selectionRange"; export class EditorState { readonly content: Strand; readonly selection: SelectionRange; constructor( content = new Strand([]), selection: SelectionRange | Cursor = new SelectionRange() ) { this.content = content; this.selection = selection instanceof Cursor ? new SelectionRange(selection) : selection; content.parent = this; } /** * @deprecated This function is only for debugging purposes. */ renderToDebugText(): string { return this.content.renderToDebugText(); } /** * @deprecated This function is only for debugging purposes. */ renderToDebugHTML(): VNode { return h("div", { class: "math" }, this.content.renderToDebugHTML()); } /** * @deprecated This function is only for debugging purposes. */ renderToDebugMathML(): VNode { return h("math", {}, this.content.renderToDebugMathML()); } /** * Get the cursor position within a given token's child strand. * @param strandPath The StrandPath of the strand to check against. * @param cursor The cursor whose position is being queried. * @returns The position of the cursor within the strand, or null if the cursor is not within that strand. */ getPositionOfCursorInStrand( strandPath: StrandPath, cursor: Cursor ): number | null { if (cursor.strandPath.length !== strandPath.length) { return null; } for (let i = 0; i < strandPath.length; i++) { const cursorLevel = cursor.strandPath[i]; const targetLevel = strandPath[i]; if ( cursorLevel.tokenIndex !== targetLevel.tokenIndex || cursorLevel.childIndex !== targetLevel.childIndex ) { return null; } } return cursor.pos; } }