@rasamjs/editor
v0.2.3
Published
A reusable SVG editor composed from rasam packages.
Readme
@rasamjs/editor
Optional extension integrations package lifecycle, tool modes and
input/camera ownership. See the
standalone example,
or run editor's dev server and open /examples/extensions.html locally.
Optional component editing registers class components with
the editor, adds a placement tool per type and builds properties from @prop metadata.
componentEditing({ create }) lets an app supply initial props and positioning inside
the placement undo step while reusing the standard pointer and cancellation behavior.
editor.commands.addPredicate(id => allowed(id)) gates existing and future commands
without replacing their definitions. Remove it with the returned cleanup, and call
commands.invalidate() when application mode changes to refresh command subscribers.
Autosave exposes immutable save state and retryable failures. Optional project metadata persists guides and application preferences separately from ordinary SVG, with compatible legacy restore and protected recovery data.
An SVG editor composed from core, history, selection, transformer, clipboard, viewport and tools. All seven are external peers. Editor 0.2.0 requires clipboard 0.3.x for its paste preparation integration. The source manifest stays private between releases.
Give a mounted HTML host a width and height:
import { Editor } from '@rasamjs/editor';
const editor = new Editor(document.querySelector<HTMLElement>('#canvas')!, {
content(drawing) {
drawing.rect({ x: 40, y: 40, width: 120, height: 80, fill: 'lightblue' });
},
history: { limit: 100 },
autosave: { key: 'my-editor' },
drop: true,
snapper: { shapes: true, spacing: true },
});
editor.on('error', ({ action, error }) => showError(action, error));
editor.on('selection', (nodes) => renderInspector(nodes));
editor.on('scene', () => renderDocumentState());
editor.tool = 'rect';
editor.keepTool = true;
editor.setPaint({ fill: '#a5d8ff', stroke: '#1c4e80', strokeWidth: 2 });
editor.groupSelection();
editor.commands.run('group');
editor.commands.rebind('group', ['Alt+G']);
const stopSave = editor.commands.register({ id: 'save', label: 'Save project', run: saveProject });
const camera = editor.viewport.camera;
editor.load('<svg xmlns="http://www.w3.org/2000/svg"><circle r="40" /></svg>');
editor.viewport.setCamera(camera);
const markup = editor.exportDocument(); // Content frame with 16 units of padding.
stopSave();
editor.destroy();The editor owns a document container and its package overlays inside the host.
Successful loads replace that runtime and retain the camera, paint and tool state.
Failed loads keep the working drawing, selection and history. configure() replaces
one component while the other instances stay connected through structural adapters.
History reconfiguration starts a new undo baseline.
Actions report failures through error; load/configure return a success boolean,
and failed exports return undefined. Constructor validation and subscriptions on
a destroyed editor throw. Scene events and editor overlay refresh coalesce per frame;
drawing and transformer gestures defer refresh and suspend native camera input.
Tools include Select, Hand, Rectangle, Ellipse, Line, Pen, Pencil and Text. A new shape
is named and selected, then the tool returns to Select unless keepTool is enabled.
Escape finishes a pen line, cancels a drag, and otherwise returns to Select. Text input owns Enter/Escape. Canvas
pointer presses focus the owned container; a focusable host can also receive tool
keys after a toolbar action. The command registry owns current shortcut labels, keys
and enabled state for UI controls. Application commands save, open, export and
help start disabled; register their callbacks to enable them. The playground now
uses this package with its properties/autosave entries and optional tool overlays.
Use snapper.create(drawing, options) to supply an owned structural snapper such as
AdvancedSnapper. Its transformer capabilities follow replacement services without
recreating the transformer. See the playground's preference mapping for a complete
integration with ruler guides.
Applications that never draw shapes or edit text can import LeanEditor from
@rasamjs/editor/lean: the same editor without the drawing and text tools and their
bytes. See API.md.
The optional @rasamjs/editor/properties entry provides new Properties(editor):
read(key) returns a shared value or mixed, and write(key, value, { mergeKey })
creates one labelled, rollback-safe edit. It covers geometry, paint and per-shape
attributes. Its bounds and moveTo() APIs move a selection while retaining offsets.
The standalone @rasamjs/editor/autosave entry exports Autosave for integrations
that attach persistence explicitly. Document and camera writes are independent;
camera-only edits do not export the SVG. Storage keys can retain existing app keys.
See API.md, CHANGELOG.md and RELEASING.md.
Run bun run build at the repository root to build peers and this package last.
Package checks use bun run --cwd packages/editor test, typecheck, test:browser,
api:check, check:docs, check:size and check:consumer.
The optional @rasamjs/editor/raster entry installs "Export PNG" and "Copy as PNG"
commands through registerImageCommands(editor, { download(blob) { /* app UI */ } }).
The playground supplies both buttons. PNG export preserves camera/history; fonts
and external styles are not embedded. See API.md.
Optional image insertion embeds dropped
or pasted PNG, JPEG and SVG files at natural size. Register imageElements in
document.elements, then call connectImageInsertion(editor) from
@rasamjs/editor/images. Default commands also include shared-center flips (Shift+H/V)
and select-similar fill, stroke and shape type.
