Skip to content
Text Diagram
Developer docs
Preview · unreleased

Save and restore

Keep source authoritative, commit its matching preview atomically and reject stale or released requests.

A structured host saves TextDiagramEmbed: protocol version, stable id, title, source language/text/revision and the matching PNG. A new draft may have no preview. A saved object should carry a preview generated from that exact source.

Save source and image together

createTextDiagram creates revision 0. renderTextDiagram creates a source-bound snapshot, rasterizeTextDiagram generates its PNG, and prepareTextDiagramSave proposes a complete object with revision + 1. The workbench manages this pipeline already.

readTextDiagram validates unknown input and returns an immutable copy. It rejects extra fields, accessors and noncanonical values. It validates structure and size; it does not re-render to prove that the PNG matches the source. Structural validation alone does not make an untrusted image a verified diagram projection.

serializeTextDiagram provides a stable comparison string. After parsing JSON, validate it again with readTextDiagram before passing it to a host.

A complete local host

When a user opens a diagram, the workbench eventually returns an edited result. The application must save it and check that another action has not changed the original while the dialog was open.

createDiagramSession(initial, persist) maintains the accepted version of one diagram. read() returns the current diagram. capture() records the opening version and supplies commit() to accept a result and release() to end that edit. The application supplies a synchronous persist callback, such as a local-storage write. If it throws, the accepted revision remains unchanged and success is not reported.

FilePurpose
recipes/session.tsCompare opening and current versions, call storage, then update the accepted diagram
shared/types.tsShared types for the edit request and session
recipes/session.tsts

The workbench supplies an object with a matching preview. A production host must also check that the target exists, remains editable and still maps to a valid position at commit time. Classic, Block and MSFE own those editor semantics. This example does not implement collaboration or cross-tab synchronization.

Four acceptance results

ResultMeaningUI behavior
savedHost atomically accepted source and previewReport success
conflictContent changed after the captured baselineKeep draft; explain conflict
readonlyWrites are no longer allowedKeep draft; explain read-only state
missingTarget was removed or releasedKeep draft; explain missing target

Storage failures can throw. The workbench displays failure and allows retry. This contract does not provide asynchronous server confirmation, automatic merging or backend retry; those belong to the host's persistence strategy.

Markdown hosts

Use textDiagramMarkdown(language, source) to generate a standard fence. It chooses a delimiter longer than any backtick run in the source, preventing an accidental closing fence. Markdown retains language and source, without a structured id, title, PNG or revision. Its source host owns editing and conflict detection.

Text Diagram · QuireKitQuireKit Text Diagram · Local rendering and source authoring · MIT