RichDocumentSession
class exported by @mwillbanks/tuil-editor.
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
| Member | Type | Required | Description | Related types |
|---|---|---|---|---|
#document | RichDocument | Yes | The #document member uses the RichDocument contract. | RichDocument |
#history | RichDocument[] | Yes | The #history member uses the RichDocument[] contract. | RichDocument |
#redo | RichDocument[] | Yes | The #redo member uses the RichDocument[] contract. | RichDocument |
#providers | ReadonlyMap<string, RichNodeProvider> | Yes | The #providers member uses the ReadonlyMap<string, RichNodeProvider> contract. | RichNodeProvider |
__constructor | any | Yes | The __constructor member uses the any contract. | — |
snapshot | () => RichDocument | Yes | The snapshot member uses the () => RichDocument contract. | RichDocument |
dispatch | (transaction: RichTransaction) => RichDocument | Yes | The dispatch member uses the (transaction: RichTransaction) => RichDocument contract. | RichDocument, RichTransaction |
undo | () => boolean | Yes | The undo member uses the () => boolean contract. | — |
redo | () => boolean | Yes | The redo member uses the () => boolean contract. | — |
serialize | (format?: "json" | "markdown") => string | Yes | The 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.
Related types
Source
View the secondary source reference