tuil
ReferencePackages@mwillbanks/tuil-editorAPI

RichDocumentSession

class exported by @mwillbanks/tuil-editor.

View rawEdit

class

Public class exported by @mwillbanks/tuil-editor.

export class RichDocumentSession {
  #document: RichDocument;
  readonly #history: RichDocument[] = [];
  readonly #redo: RichDocument[] = [];
  readonly #providers: ReadonlyMap<string, RichNodeProvider>;

  constructor(
    root: RichNode = { type: "document", children: [] },
    options: RichEditorProviderOptions = {},
  ) {
    this.#providers = richNodeProviderMap(options.nodes);
    const validated = validateRichNode(root, this.#providers);
    if (validated.type !== "document") {
      throw new Error("Rich documents require a document root");
    }
    this.#document = Object.freeze({ version: 0, root: freezeNode(validated) });
  }

  snapshot(): RichDocument {
    return this.#document;
  }

  dispatch(transaction: RichTransaction): RichDocument {
    const node = transaction.node
      ? freezeNode(validateRichNode(transaction.node, this.#providers))
      : undefined;
    const validated = { ...transaction, node };
    if (transaction.path.length === 0 && transaction.node) {
      if (node?.type !== "document") {
        throw new Error("Rich documents require a document root");
      }
      this.#history.push(this.#document);
      this.#redo.length = 0;
      this.#document = Object.freeze({
        version: this.#document.version + 1,
        root: node,
      });
      return this.#document;
    }
    this.#history.push(this.#document);
    this.#redo.length = 0;
    this.#document = Object.freeze({
      version: this.#document.version + 1,
      root: editRichNode(this.#document.root, validated, 0),
    });
    return this.#document;
  }

  undo(): boolean {
    const previous = this.#history.pop();
    if (!previous) return false;
    this.#redo.push(this.#document);
    this.#document = previous;
    return true;
  }

  redo(): boolean {
    const next = this.#redo.pop();
    if (!next) return false;
    this.#history.push(this.#document);
    this.#document = next;
    return true;
  }

  serialize(format: "json" | "markdown" = "json"): string {
    return format === "json"
      ? JSON.stringify(this.#document)
      : richDocumentToMarkdown(this.#document.root, this.#providers);
  }
}

Members

MemberTypeRequiredDescriptionRelated types
#documentRichDocumentYesThe #document member uses the RichDocument contract.RichDocument
#historyRichDocument[]YesThe #history member uses the RichDocument[] contract.RichDocument
#redoRichDocument[]YesThe #redo member uses the RichDocument[] contract.RichDocument
#providersReadonlyMap<string, RichNodeProvider>YesThe #providers member uses the ReadonlyMap<string, RichNodeProvider> contract.RichNodeProvider
__constructoranyYesThe __constructor member uses the any contract.
snapshot() => RichDocumentYesThe snapshot member uses the () => RichDocument contract.RichDocument
dispatch(transaction: RichTransaction) => RichDocumentYesThe dispatch member uses the (transaction: RichTransaction) => RichDocument contract.RichDocument, RichTransaction
undo() => booleanYesThe undo member uses the () => boolean contract.
redo() => booleanYesThe redo member uses the () => boolean contract.
serialize(format?: "json" | "markdown") => stringYesThe serialize member uses the (format?: "json" | "markdown") => string contract.

Parameters

This declaration has no public members.

Returns

This declaration does not return a value.

Throws

No thrown errors are documented for this declaration.

Source

View the secondary source reference

Package

@mwillbanks/tuil-editor

On this page