Storage

editor.storage.diagrams tells you what the document holds and what the editor can draw. You call its functions directly, not as commands.

This reads the first chart of the editor from A first chart:

const charts = editor.storage.diagrams.listCharts()
const text = editor.storage.diagrams.read({ pos: charts[0].pos, format: 'chart-syntax' })

requestSync is the only function that writes into the document, with the command syncChartData. registerSource, setZoom, setPan and setMaximized change this editor only, and are not saved or shared.

Charts, diagrams and tables in the document

In document order:

FunctionReturns
listCharts()Every chart, with the Chart fields
listDiagrams()Every diagram, see Diagram fields
listTables()Every table a chart or diagram can take its data from, see Table fields. Empty without the linkTables option
getOpenInput()The Quick Input the cursor is in, or null

Chart fields

FieldValue
posThe chart's position in the document, a number
dataThe Chart Data
specThe Chart Spec
rendererThe Renderer id it names, or null
sourceIts Data Source, or null
syncedAtISO time of the last source read, or null
delimiterThe delimiter its rows were typed with, or null
previewUrlIts preview URL, or null
layout'chart', 'with-table' or 'table', see setChartLayout
tableIdThe table it follows, or null
tableReadThe settings of updateLinkedTable that differ from the default, or null

Diagram fields

FieldValue
posThe diagram's position
formatIts format, such as 'mermaid'
sourceIts source text
titleIts title, or null
descriptionIts description, or null
view'diagram' when it shows the picture, 'code' when it shows the source text
optionsRecord<string, unknown> of other settings, such as size
kindThe Mermaid kind, such as 'flowchart'. null for empty source, source that is no known Mermaid diagram kind, and draw.io or BPMN
drawablefalse for empty source. For Mermaid, draw.io and BPMN, whether the source is valid in that format. For another format, whether a registered Renderer for diagrams draws it
linkThe table it is drawn from, { tableId, columns }, or null
lanesstring[], the lane names of a diagram drawn from a table, in order. Otherwise empty

Table fields

FieldValue
posThe table's position
tableIdIts id once a chart or a diagram follows it, or null
headerThe texts of its first row
headerCellstrue when the first row is header cells. With header: 'auto', a first row of plain cells names the columns only if it has more than one cell and every cell after the first is filled and holds no number
chartsHow many charts follow it
diagramsHow many diagrams are drawn from it
followers{ pos, kind: 'chart' | 'diagram' } for each, in document order

The open input

getOpenInput() returns { pos, view: 'chart-syntax' | 'table', type, delimiter, editing }. type is the chart type it becomes when it becomes a chart. delimiter is null until one is set, and in a table. editing is true when the input holds an existing chart.

Reading a chart's data

Without pos, they read the selection:

FunctionReturns
read({ pos?, format? })The chart as text, 'chart-json' by default. Throws without a chart or for an unknown format
readGrid({ pos? })A grid of strings, row 0 the header. Throws without a chart
readTable({ pos? })Table JSON, or null without table nodes. Throws without a chart
readText({ pos? }){ format, text } of a chart or diagram, or null (Text beside the picture)
parseFormat(format, content, type?){ data, spec, delimiter? }. type defaults to chart.defaultType. Throws for content or a format it cannot read
getFormat(id)A registered format, for its serialize, or undefined

What the editor can draw

FunctionReturns
describeCapabilities()Types, formats, Renderers, default Renderer, sources, delimiters, trigger and styles
chartTypesA property: built-in and registered chart types
canRender(spec)Whether a registered Renderer draws the type
rendererFor(spec, preferred?, data?)The Renderer a chart would be drawn with, or null
diagramRendererFor(format, source?)The Diagram Renderer for that format or source, or null
rulesFor(renderer)A Renderer object's rules in effect, see rendererRules
refresh()Nothing. Draws everything again, such as after a CSS-only theme change

What the extension holds

What Diagrams was configured with. The lists include the built-in entries:

MemberValue
optionsThe options Diagrams was configured with
chartOptionsThe chart options with their defaults
renderers, diagramRenderersThe registered Renderers
formats, stylesThe built-in formats and styles, and those of the options
styleChart(spec, theme, scheme?){ spec, theme } with the style the spec names applied. scheme defaults to 'light'

Export

See Export:

FunctionReturns
export(target, options?)A promise of a file. target is { pos }, {} for the selection, or { data, spec, renderer? }
exportAll(options?)Files of every chart and diagram that can be drawn, with pos. Skips charts without rows or with spec.hidden
exportDocx(options?)A promise of { blob, notes }, see Word

Data Sources

See Data Sources:

FunctionWhat it does
readSource(source)A promise of the data now. Rejects for an unknown provider or a failed read
requestSync(source)Writes into each linked chart, resolving true when it wrote
registerSource(provider)Adds or replaces a provider and returns a function that removes it
getSource(id), listSources()One provider or undefined, and all, in registration order

requestSync resolves false when the read fails or no chart is linked. A failed read keeps the snapshot and calls onSourceError.

Zoom, pan and maximize on screen

setZoom, getZoom, setPan, getPan, getShownSize, setMaximized and getMaximized set or read how this editor shows a chart or diagram, as View functions describes. To update your UI when setMaximized runs, listen to the editor's transaction event. That change is not in the undo history.