Clipboard and Export Implementation in Vanilla-Diagram

What is copied

The selected nodes, a selected container's descendants, and the connectors whose ends are all among them; a free end counts as among them, so a selected connector with two free ends is copied alone. A selected connector whose node isn't selected is left out. Nothing to copy: copy(), cut() and duplicate() return false.

The format is text/plain holding JSON:

{ "vanilla-diagram-clipboard": 1, "nodes": [ ... ], "connectors": [ ... ] }

Nodes in z order, as copies of the model's items.

Keys and API

Keys (canvas focused) API
Copy Ctrl/Cmd + C: the copy event copy()
Cut Ctrl/Cmd + X: the cut event cut()
Paste Ctrl/Cmd + V: the paste event paste(), a promise
Duplicate Ctrl/Cmd + D duplicate()

Paste

  1. The items are validated as a load validates them (02-model-and-commands): an unknown symbol is kept and drawn as a rectangle, a connector whose node isn't pasted, or whose known symbol lacks the port, is left out.
  2. Every node and connector gets a new id, and the connector ends are remapped, and so is a parent pasted with its node; a parent that isn't pasted is dropped. The nodes go on top, from maxZ + 1 in their order.
  3. Each paste of the same content in the same diagram lands 16 world px right and below the previous one (the first 16px from the original); when the result's centre would be out of view, the items are centred in the view instead. Free ends and an elbow's stored points move with them; a bezier's points are offsets and don't. A node without a parent then takes the container under its pasted centre, if any (13-containers).
  4. vn-diagram-pasting fires, cancelable, with { nodes, connectors, source } (copies, new ids assigned); the host may change each node's text, data and swatch.
  5. One add command, labelled command.paste, adds them, and the nodes are selected: one undo step, one change set.

The pure part (VanillaDiagramClipboard: collect(), serialize(), parse(), prepare(), pasteOffset(), translate()) loads without the core, for tests/node/diagram-clipboard.test.js.

toSVG()

toSVG({ ids, padding, background }) resolves with a standalone SVG of the items: default all, a listed container with its descendants, a connector included when both its ends are (a free end counts). It is a promise: the export is built in slices (During an export).

Before initializeDiagram(), after destroy(), or with nothing to draw, it resolves with an empty <svg xmlns="http://www.w3.org/2000/svg"></svg>.

In slices. The items are listed first, in the canvas's order, then built in slices of VanillaDiagram.EXPORT_SLICE_MS (SLICE_MS, 8ms; tests lower it), yielding with VanillaDiagram.yieldToPage between slices; the first slice runs before toSVG() returns. Each slice:

  1. draws its items the canvas leaves out, and the far view's text of the drawn ones, at full detail, places them, and grows the box with their bounds and routes;
  2. clones and paints each group, and measures its texts for the <text> lines;
  3. releases the items it drew and gives the far view back, so between slices the drawing is as it was, unless the export holds it (below);
  4. serializes its clones: they are appended to one detached <svg>, which is serialized and its own start and end tags cut off, so the groups inherit the SVG namespace, declared once on the root.

At the end the root's start tag (the box's viewBox, width and height, capped as above), the <defs> and the background <rect> are written, and the slices' markup joined inside them. A slice sizes itself from the previous one's rate, about 4 items per ms at first.

The drawing's hold

Steps 1 and 3 change the live drawing for each drawn item a slice exports (in the far view its text box is written and hidden again), and a slice that draws an undrawn item places it among the drawn ones. Either way the page renders every drawn item again in the frame after the slice, and the slice's own paint and text reads restyle and lay them out. With the whole 10,000-shape sample drawn (fitted in view, 21,400 items) that was about 40ms a frame in Chromium and 99ms in Firefox, for about one frame every second slice: toSVG() took 42.8s there against 5.6s with nothing drawn, and 243.6s against 14.2s in Firefox.

So an export with VanillaDiagram.EXPORT_HOLD_ITEMS (1,000; tests lower it) or more items left after its first slice holds the drawing (_holdDrawing(), in the rendering), from then until its last slice:

Each slice then draws its items into nearly empty layers, as with nothing drawn. The first slice runs with the drawing in place, so an export of one slice, or one with fewer items left, never changes it. Under the scrim the drawing empties item by item over a few passes.

When the last slice is done the hold is released: what is still drawn is queued for a rewrite at the zoom's detail (a zoom across 0.4 meanwhile queued none), and the export draws the drawn rect again, a pass per frame as a load does, before it returns: toSVG() settles, and toPNG() rasters, with the drawing back (about 1 to 1.4s in Chromium and 5.3 to 6.5s in Firefox at the 10,000-shape fit, as a zoom-out there). Holds count: with two exports at once the drawing comes back after the last. A load's swap drops every hold, and a rejected export doesn't wait: destroy() draws nothing again, and off the page the drawing comes back when the element does. The checks between slices hold for that wait too: a load's swap, destroy() or the element leaving the page during it rejects the export.

One Range per export. The lines are measured with one Range for the whole export, never one per text: a live range stays registered with its document until it is garbage collected, and every insertion and removal in the document updates every registered range, so a range per text made each later append, removal and release cost more, and the 10,000-shape export grew from about 5s to 25s.

toPNG()

toPNG({ ids, padding, background, scale }) builds that SVG (its text as HTML only), loads it from a data: URL into an image, draws it on a canvas at the box times scale (default devicePixelRatio), or smaller where the browser refuses that canvas, and resolves with toBlob('image/png').

During an export

Fonts. A picture drawn from an SVG can't load the page's web fonts, so text in a web font (IBM Plex Sans in the Carbon themes, when the page loads it) falls back to an installed one. Fonts are not embedded.

Cost. Both are O(n + m) per call: one clone and one computed style per element, and one draw of each item the canvas leaves out. toSVG() adds, per text, one off-screen layout and one measure per character, for its <text> lines, with the export's one Range. They are not on a hot path. In the process-flowchart-js sample, headless Chromium, with the drawing held: toSVG() about 7s at 10,000 shapes at zoom 0.1, about 8s fitted in view (the redraw included), and 62s at 100,000 at zoom 0.1, with no frame gap over about 115ms (the final join included). toPNG({ scale: 1 }) about 8s at 10,000, the raster included. Without the hold they took 13 to 15s, 43s and 144s: every drawn item rendered again in each frame between slices (with the canvas visibility: hidden, 28s at the fit: paint was about a third of it) (14-large-diagrams).