@rasamjs/clipboard
v0.3.1
Published
SVG clipboard commands for rasam drawings.
Readme
@rasamjs/clipboard
Copy, cut, paste, duplicate and delete SVG shapes in rasam drawings. The only runtime
peer is @rasamjs/core (^0.3.0). Selection connects through a structural adapter;
@rasamjs/selection is optional. @rasamjs/[email protected] is published.
import { SVG } from '@rasamjs/core';
import { Selection } from '@rasamjs/selection';
import { History } from '@rasamjs/history';
import { Clipboard } from '@rasamjs/clipboard';
const drawing = SVG(document.querySelector<HTMLElement>('#drawing')!);
const selection = new Selection(drawing);
const history = new History(drawing);
const clipboard = new Clipboard(drawing, selection);
clipboard.on('pasteerror', ({ error }) => console.warn('Paste rejected', error));
// Select shapes, then Ctrl/Cmd+C, X, V, D, Delete or Backspace.
clipboard.copy();
clipboard.paste();
clipboard.duplicate();Keyboard copy/cut/paste use browser clipboard events and write both image/svg+xml
and text/plain. Paste prefers SVG, then text, then the internal buffer. API calls
use the buffer; copy/cut also try an optional navigator.clipboard.writeText() and
ignore permission failures. Duplicate never touches the system clipboard or buffer.
Focus in editable fields and handled events is ignored. With body focus, the first
eligible clipboard instance handles an event; focus a drawing to address it explicitly.
Each drawing command is one undo step with history. Repeated
pastes of the same content move 10 parent units right/down per paste; copy resets the
step. Duplicate moves 10 units from the current selection. Set offset: 0 to paste in place.
Clipboard contents and selection membership are not undo history.
Pass preparePaste: ({ nodes, source }) => { ... } to customize imported nodes before
paste or duplicate commits. Naming and property changes join the insertion's undo step.
The callback must be synchronous; throwing rolls back the model edit without changing
selection or the next paste offset. The paste event remains a notification after commit.
Paste passes through core's passive SVG import policy, rejects external URLs by default, and remaps resource ids. Unsupported markup changes nothing. SVG document envelopes containing only viewport metadata are unwrapped into the destination scope; roots with presentation attributes or ids stay nested to preserve their meaning. Envelope dimensions are not a camera conversion. Copy keeps each shape's own geometry and transform, not its ancestors'. Defs remain alongside the pasted shapes in that scope. Nested SVG shapes inside a copied document retain their own viewport. Copy can export trusted content that the passive importer subsequently rejects (for example a foreignObject).
See API, release checks, changelog and the interactive example. Align/distribute live in selection's arrange entry.
Copy as PNG
Use copyImage(root, options) from @rasamjs/clipboard/raster for PNG with supported
SVG representations, without changing selection or history. It follows core raster
framing and image-origin rules; web fonts are not embedded. Call from a user gesture
and handle rejected clipboard permission/rendering promises. See API.md.
