Text beside the picture

Show the text of a chart or a diagram in a text field of your own, next to the document. A change in either one updates the other. The extension reads and writes the text, and you build the text field and the layout.
New tab

Quick start

With the editor of A first chart, this keeps a <textarea> in step with the selected chart or diagram, starting with the first chart. It reads the text again after every transaction except those its own typing caused, so the text stays as the user typed it. When another change replaces or deletes the chart or diagram, it lets go until the user selects one:

const textField = document.createElement('textarea')
document.body.append(textField)

let pos = editor.storage.diagrams.listCharts()[0]?.pos ?? null
if (pos !== null) textField.value = editor.storage.diagrams.readText({ pos })?.text ?? ''
let typing = false

editor.on('transaction', ({ transaction }) => {
  // Typing replaces the node. Mapping with -1 keeps pos at its start; any other replacement or a
  // deletion lets go, so the next figure is never changed instead.
  if (pos !== null) {
    const mapped = transaction.mapping.mapResult(pos, -1)
    pos = mapped.deletedAfter && !typing ? null : mapped.pos
  }
  const { node, from } = editor.state.selection
  if (node && ['chart', 'flowDiagram'].includes(node.type.name)) pos = from
  if (!typing && pos !== null) {
    textField.value = editor.storage.diagrams.readText({ pos })?.text ?? ''
  }
})

textField.addEventListener('input', () => {
  if (pos === null) return
  typing = true
  const valid = editor.commands.updateFromText({ pos, text: textField.value })
  typing = false
  textField.toggleAttribute('data-invalid', !valid)
})

Reading the text

editor.storage.diagrams.readText({ pos }) returns the text and its format, { format, text }:

const current = editor.storage.diagrams.readText({ pos })
// { format: 'chart-syntax', text: 'bar\nMonth | Revenue\nJan | 120' }

For a chart, the format is 'chart-syntax'. The text is the chart's type line and its rows in the Chart syntax, split at its delimiter, such as |. For a diagram, the text is its source in the diagram's own format: 'mermaid', 'drawio' or 'bpmn'.

Without pos, readText reads the selected chart or diagram. It returns null when there is none.

Writing it back

updateFromText writes the text back into the chart or diagram.

updateFromText

Changes a chart or a diagram from its text, the same text that readText returns.

editor.commands.updateFromText({ pos, text: 'pie\nDogs, 3\nCats, 2' })
OptionTypeDefaultDescription
textstringRequiredThe text
posnumberThe selected oneThe position of the chart or diagram

A chart reads the text the way the Quick Input does, the field that typing ::: opens. The text is rows below an optional type line, or a Mermaid chart. The data changes, and the type when there is a type line.

A Mermaid chart also sets what its source names, such as the title. The rest of the Chart Spec, such as the colors, stays. A diagram takes the text as its new source, as updateFlowDiagram does.

Returns false without a chart or diagram, and for text the command cannot read, such as an unclosed quote. Keep that text in your field and mark it as invalid, so nothing the user typed is lost.

A chart that follows a read-only Data Source also returns false for new data.

Coloring the text

highlightSource(text, format) from @tiptap-pro/extension-diagrams/core returns the text as escaped HTML, each part in a <span> with a class such as tiptap-diagram-source-keyword. It colors the Chart syntax and Mermaid. Leave draw.io and BPMN XML as plain text.

import { highlightSource } from '@tiptap-pro/extension-diagrams/core'

const { delimiters, chartTypes } = editor.storage.diagrams.describeCapabilities()
code.innerHTML = highlightSource(text, 'chart-syntax', {
  delimiters,
  types: chartTypes.map(type => type.id),
})

code stands for your element around the HTML. Give it the class tiptap-diagram-source, so the Highlighted source colors apply there, in light and dark.

For a field the user can edit, lay a transparent <textarea> over a <pre> that holds the HTML, with the same font, padding and line height. The demo works that way.

The sourceHighlighting option turns the colors off only in the Quick Input and in a diagram's code view, not in your own text field.