Text Editing Implementation in Vanilla-Diagram

A node's text and a connector's label are edited in place, in the <foreignObject> that draws them, so CSS wraps and edits the text the same way.

Opening

Each fires vn-diagram-text-editing first, cancelable, with { target: 'node' | 'connector', id, source }. Read-only opens nothing, from the user or the API: the editor is a user edit.

The editor

A <textarea class="vn-diagram-text-editor"> takes the text <div>'s place in the same <foreignObject>, at the same box, font, colour and alignment, inside the item's group, so it turns with a rotated node and scales with the zoom. Its top padding is half the room the lines leave (measured with the height at 0, where scrollHeight is the lines' own height) and follows each input, so the text doesn't move when editing starts. The box takes the pointer while the editor is open (vn-diagram-text-box--editing). Its aria-label is the textEditorLabel message, and the text is selected on opening. The item's group, aria-hidden otherwise, isn't while the editor is open, and the item is drawn wherever it is and at full detail, below zoom 0.4 too (15-screen-readers, 14-large-diagrams).

Key in the editor
Enter Commits, focus back on the canvas
Shift + Enter Adds a line
Escape Cancels, focus back on the canvas
Tab Commits, focus back on the canvas

Every key event, the clipboard events and a pointer press stop at the editor, and the canvas's key handler ignores events whose target is the editor: Delete, the arrows and Ctrl+Z act on the text, never on the diagram (Ctrl+Z is the textarea's own undo).

Commit and close

A commit is one update of text (or label), labelled command.editText, with the source the editor was opened with: one undo step, one vn-diagram-changed, one change set. Unchanged text commits nothing; empty text removes the field.

What happens The editor
Enter, Tab, blur (a press elsewhere, focus leaving) Commits
Escape Closes without committing
read-only turned on, a layout() beginning Commits, as it commits a nudge burst
A load, destroy(), setDataManager() Closes without committing
The item removed Closes without committing
A failed save that reverts the item Closes without committing

Newlines

The text <div> has white-space: pre-wrap, so a \n in a node's text, from the editor or the data, starts a line. Phase 1 collapsed it to a space.

Connector labels

A connector's label is drawn in its own <foreignObject> (vn-diagram-connector-label-box, 200 x 80 world px) centred on the route's midpoint by length (a bezier sampled at 25 points, VanillaDiagramRouting.midpoint()), inside the connector's group, so it stays under the nodes. The text sits in a span on --vn-diagram-bg, which takes the pointer: a press on it selects its connector. The box exists only while the connector has a label or its editor is open, so 1,000 unlabelled connectors cost nothing. updateConnector(id, { label }) sets it (a string, or null to remove it).

Touch

A double-tap opens the editor; the on-screen keyboard follows the textarea's focus. Real devices are checked by hand.