#113

Zoom: Let a drawing be dragged before it is enlarged

Openronicayuopened 1mo ago

Use case

A drawing can be dragged only once it has been enlarged. Before that, pressing on it and moving the mouse does nothing.

In a container with a fixed height, such as an editor's preview pane, a playground, or a docs page that gives each Mermaid block a set height, readers expect to grab the drawing and move it within the empty space around it, as on a canvas. always: true gives them the zoom controls, but they still have to zoom in before they can move anything.

Proposal

Add an opt-in option to ZoomOptions:

// Whether a drawing at rest can be dragged about the room its element has to spare, and not
// only once enlarged. It cannot by default, which leaves its text to be selected.
drag?: boolean;
mount(el, source, { zoom: { always: true, drag: true } });

When it is set, and the element has room to spare around the drawing:

  • Mouse drag: past the same 4px threshold that panning already uses, the drawing becomes a view at the scale that fits, and follows the pointer. It is held inside the element by frame(), just as an enlarged one is.
  • Arrow keys: they move it as they pan an enlarged drawing. +/- still zoom.
  • Ending it: reset, 0/Escape, a double-click, or zooming out back to fit all return it to rest.
  • Cursor: at rest the drawing shows cursor: grab and user-select: none, so pressing on it drags rather than selects text.

What stays the same:

  • It is off by default, so existing pages keep text selection and behave as now.
  • Pointer capture starts only once a drag begins, so a press on a link is still a click. The click that ends a drag is cancelled, as it is now.
  • One finger at rest still scrolls the page (touch-action: pan-y is kept). Touch readers pinch, then drag.
  • A drawing whose element has no room to spare isn't affected.

Implementation

It's about 20 lines in src/zoom.ts:

  • show() takes a held flag, so it can show a view at the fit scale instead of settling.
  • A lift() helper is called from pointermove, once the threshold is passed and the pointer isn't touch, and from the arrow keys.

I have a working branch with unit tests and docs, tested in Chromium on the playground: https://github.com/ronicayu/pele/tree/claude/draggable-diagram-rendering-myzdhr

It relies on the padding fix in #112. Without that fix, lifting a drawing that overflows its padded element makes it jump by the top padding.

Happy to open a PR if this is wanted, or to change the API, for example to one value of a wider gesture setting instead of a separate boolean.

Activity

0

No activity yet.