@crux-diagram/react 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +50 -2
- package/dist/cjs/DrawingEditor.d.ts +34 -0
- package/dist/cjs/DrawingEditor.js +289 -0
- package/dist/cjs/DrawingViewer.d.ts +14 -0
- package/dist/cjs/DrawingViewer.js +14 -0
- package/dist/cjs/JointDisplayBar.d.ts +8 -0
- package/dist/cjs/JointDisplayBar.js +22 -0
- package/dist/cjs/NameEditor.d.ts +15 -0
- package/dist/cjs/NameEditor.js +36 -0
- package/dist/cjs/Toolbar.d.ts +8 -0
- package/dist/cjs/Toolbar.js +34 -0
- package/dist/cjs/VersionHistory.d.ts +33 -0
- package/dist/cjs/VersionHistory.js +49 -0
- package/dist/cjs/a11y.d.ts +12 -0
- package/dist/cjs/a11y.js +53 -0
- package/dist/cjs/config.d.ts +50 -0
- package/dist/cjs/config.js +39 -0
- package/dist/cjs/controls.d.ts +12 -0
- package/dist/cjs/controls.js +15 -0
- package/dist/cjs/editor-context.d.ts +45 -0
- package/dist/cjs/editor-context.js +101 -0
- package/dist/cjs/export/export-image.d.ts +29 -0
- package/dist/cjs/export/export-image.js +94 -0
- package/dist/cjs/hooks.d.ts +6 -0
- package/dist/cjs/hooks.js +10 -0
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/interaction/gestures.d.ts +133 -0
- package/dist/cjs/interaction/gestures.js +353 -0
- package/dist/cjs/interaction/joints.d.ts +41 -0
- package/dist/cjs/interaction/joints.js +104 -0
- package/dist/cjs/interaction/keyboard.d.ts +51 -0
- package/dist/cjs/interaction/keyboard.js +116 -0
- package/dist/cjs/interaction/palette.d.ts +19 -0
- package/dist/cjs/interaction/palette.js +64 -0
- package/dist/cjs/interaction/viewport.d.ts +14 -0
- package/dist/cjs/interaction/viewport.js +23 -0
- package/dist/cjs/labels.d.ts +39 -0
- package/dist/cjs/labels.js +49 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/render/DrawingStage.d.ts +27 -0
- package/dist/cjs/render/DrawingStage.js +77 -0
- package/dist/cjs/render/fonts.d.ts +7 -0
- package/dist/cjs/render/fonts.js +50 -0
- package/dist/cjs/render/scene.d.ts +75 -0
- package/dist/cjs/render/scene.js +92 -0
- package/dist/cjs/store.d.ts +90 -0
- package/dist/cjs/store.js +159 -0
- package/dist/esm/DrawingEditor.d.ts +34 -0
- package/dist/esm/DrawingEditor.js +285 -0
- package/dist/esm/DrawingViewer.d.ts +14 -0
- package/dist/esm/DrawingViewer.js +11 -0
- package/dist/esm/JointDisplayBar.d.ts +8 -0
- package/dist/esm/JointDisplayBar.js +19 -0
- package/dist/esm/NameEditor.d.ts +15 -0
- package/dist/esm/NameEditor.js +32 -0
- package/dist/esm/Toolbar.d.ts +8 -0
- package/dist/esm/Toolbar.js +31 -0
- package/dist/esm/VersionHistory.d.ts +33 -0
- package/dist/esm/VersionHistory.js +45 -0
- package/dist/esm/a11y.d.ts +12 -0
- package/dist/esm/a11y.js +47 -0
- package/dist/esm/config.d.ts +50 -0
- package/dist/esm/config.js +35 -0
- package/dist/esm/controls.d.ts +12 -0
- package/dist/esm/controls.js +11 -0
- package/dist/esm/editor-context.d.ts +45 -0
- package/dist/esm/editor-context.js +95 -0
- package/dist/esm/export/export-image.d.ts +29 -0
- package/dist/esm/export/export-image.js +86 -0
- package/dist/esm/hooks.d.ts +6 -0
- package/dist/esm/hooks.js +7 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/interaction/gestures.d.ts +133 -0
- package/dist/esm/interaction/gestures.js +338 -0
- package/dist/esm/interaction/joints.d.ts +41 -0
- package/dist/esm/interaction/joints.js +95 -0
- package/dist/esm/interaction/keyboard.d.ts +51 -0
- package/dist/esm/interaction/keyboard.js +109 -0
- package/dist/esm/interaction/palette.d.ts +19 -0
- package/dist/esm/interaction/palette.js +59 -0
- package/dist/esm/interaction/viewport.d.ts +14 -0
- package/dist/esm/interaction/viewport.js +19 -0
- package/dist/esm/labels.d.ts +39 -0
- package/dist/esm/labels.js +45 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/render/DrawingStage.d.ts +27 -0
- package/dist/esm/render/DrawingStage.js +74 -0
- package/dist/esm/render/fonts.d.ts +7 -0
- package/dist/esm/render/fonts.js +45 -0
- package/dist/esm/render/scene.d.ts +75 -0
- package/dist/esm/render/scene.js +86 -0
- package/dist/esm/store.d.ts +90 -0
- package/dist/esm/store.js +156 -0
- package/package.json +80 -4
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Editor keyboard shortcuts (REA-014, REA-015, REA-018). Flutter has none; these follow
|
|
3
|
+
// the REA-018 assumption: they act only while the editor has the focus and never while
|
|
4
|
+
// a text field has it.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.NUDGE_STEP_LARGE = exports.NUDGE_STEP = void 0;
|
|
7
|
+
exports.commandForKey = commandForKey;
|
|
8
|
+
exports.isTextEntry = isTextEntry;
|
|
9
|
+
exports.wholeGroup = wholeGroup;
|
|
10
|
+
exports.runCommand = runCommand;
|
|
11
|
+
const core_1 = require("@crux-diagram/core");
|
|
12
|
+
/** Arrow-key move, in canvas units; `Shift` uses the large step (REA-015). */
|
|
13
|
+
exports.NUDGE_STEP = 1;
|
|
14
|
+
exports.NUDGE_STEP_LARGE = 10;
|
|
15
|
+
const ARROWS = {
|
|
16
|
+
ArrowLeft: [-1, 0],
|
|
17
|
+
ArrowRight: [1, 0],
|
|
18
|
+
ArrowUp: [0, -1],
|
|
19
|
+
ArrowDown: [0, 1],
|
|
20
|
+
};
|
|
21
|
+
/** The command bound to a key press, or `null` when the key is not a shortcut. */
|
|
22
|
+
function commandForKey(event) {
|
|
23
|
+
if (event.altKey)
|
|
24
|
+
return null;
|
|
25
|
+
const command = event.ctrlKey || event.metaKey;
|
|
26
|
+
const key = event.key.toLowerCase();
|
|
27
|
+
if (command) {
|
|
28
|
+
if (key === 'z')
|
|
29
|
+
return { type: event.shiftKey ? 'redo' : 'undo' };
|
|
30
|
+
if (key === 'd' && !event.shiftKey)
|
|
31
|
+
return { type: 'duplicate' };
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
if (event.key === 'Delete' || event.key === 'Backspace')
|
|
35
|
+
return { type: 'delete' };
|
|
36
|
+
const arrow = ARROWS[event.key];
|
|
37
|
+
if (!arrow)
|
|
38
|
+
return null;
|
|
39
|
+
const step = event.shiftKey ? exports.NUDGE_STEP_LARGE : exports.NUDGE_STEP;
|
|
40
|
+
return { type: 'nudge', dx: arrow[0] * step, dy: arrow[1] * step };
|
|
41
|
+
}
|
|
42
|
+
/** Whether key presses on `target` belong to a text field rather than to the editor. */
|
|
43
|
+
function isTextEntry(target) {
|
|
44
|
+
if (!target || typeof target.tagName !== 'string')
|
|
45
|
+
return false;
|
|
46
|
+
const element = target;
|
|
47
|
+
const tag = element.tagName.toLowerCase();
|
|
48
|
+
return tag === 'input' || tag === 'textarea' || tag === 'select' || element.isContentEditable === true || element.getAttribute('contenteditable') === 'true';
|
|
49
|
+
}
|
|
50
|
+
/** The group whose shapes are exactly `selection`, if any (a selected group, MS-6). */
|
|
51
|
+
function wholeGroup(document, selection) {
|
|
52
|
+
return document.groups.find((g) => g.shapeIds.length === selection.length && g.shapeIds.every((id) => selection.includes(id)));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Runs `command` on the committed document. Each change is one history entry: delete
|
|
56
|
+
* removes the selected shapes with their joints (REC-071) and clears the selection;
|
|
57
|
+
* duplicate copies them as FR-20 does and selects the copies; nudge moves them without
|
|
58
|
+
* grid snapping, every center kept inside the canvas (FR-10); rotate90 turns a single
|
|
59
|
+
* shape a quarter turn clockwise (FR-17, REA-013; it has no key and serves the toolbar).
|
|
60
|
+
* Without a selection, or when nothing would change, it does nothing.
|
|
61
|
+
*/
|
|
62
|
+
function runCommand(editor, command) {
|
|
63
|
+
if (command.type === 'undo')
|
|
64
|
+
return editor.undo();
|
|
65
|
+
if (command.type === 'redo')
|
|
66
|
+
return editor.redo();
|
|
67
|
+
const { document, selection, selectedJoint } = editor.getState();
|
|
68
|
+
// A selected joint is deleted on its own, as one entry (DL-1); it never coexists with shapes.
|
|
69
|
+
if (command.type === 'delete' && selectedJoint)
|
|
70
|
+
return editor.commit((0, core_1.removeJoint)(document, selectedJoint));
|
|
71
|
+
if (selection.length === 0)
|
|
72
|
+
return;
|
|
73
|
+
switch (command.type) {
|
|
74
|
+
case 'delete':
|
|
75
|
+
editor.commit((0, core_1.removeShape)(document, selection), []);
|
|
76
|
+
return;
|
|
77
|
+
case 'duplicate': {
|
|
78
|
+
const next = (0, core_1.duplicateShapes)(document, selection);
|
|
79
|
+
editor.commit(next, next.shapes.slice(-selection.length).map((s) => s.id));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
case 'rotate90':
|
|
83
|
+
// Only a single shape rotates; a multi-selection or a group does nothing (MS-5).
|
|
84
|
+
if (selection.length === 1)
|
|
85
|
+
editor.commit((0, core_1.rotateShape)(document, selection[0], { by: Math.PI / 2 }));
|
|
86
|
+
return;
|
|
87
|
+
case 'front':
|
|
88
|
+
case 'back': {
|
|
89
|
+
// The order is a toolbar command (TB-3); it has no key. An unchanged order adds no entry.
|
|
90
|
+
const next = command.type === 'front' ? (0, core_1.bringToFront)(document, selection) : (0, core_1.sendToBack)(document, selection);
|
|
91
|
+
if (next !== document)
|
|
92
|
+
editor.commit(next);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
case 'group': {
|
|
96
|
+
// A toolbar command (MS-6); a selection that is already one whole group adds no entry.
|
|
97
|
+
if (selection.length < 2)
|
|
98
|
+
return;
|
|
99
|
+
const next = (0, core_1.groupShapes)(document, selection);
|
|
100
|
+
if (next !== document)
|
|
101
|
+
editor.commit(next);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
case 'ungroup': {
|
|
105
|
+
const group = wholeGroup(document, selection);
|
|
106
|
+
if (group)
|
|
107
|
+
editor.commit((0, core_1.ungroup)(document, group.id));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
case 'nudge': {
|
|
111
|
+
const next = (0, core_1.moveShape)(document, selection, { dx: command.dx, dy: command.dy });
|
|
112
|
+
if (next !== document)
|
|
113
|
+
editor.commit(next);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Point, type ShapeType, type Size } from '@crux-diagram/core';
|
|
2
|
+
import type { ShapeStyle } from '../config.js';
|
|
3
|
+
import type { EditorStore } from '../store.js';
|
|
4
|
+
/** Size of a new shape, in canvas units (Flutter spec 01, section 4). */
|
|
5
|
+
export declare const DEFAULT_SHAPE_SIZES: Readonly<Record<ShapeType, Size>>;
|
|
6
|
+
export interface PaletteOptions {
|
|
7
|
+
/** The unsnapped visible center (PL-1). */
|
|
8
|
+
readonly center: Point;
|
|
9
|
+
/** Grid step, or `null` for none (FR-9). */
|
|
10
|
+
readonly gridSize: number | null;
|
|
11
|
+
readonly style: ShapeStyle;
|
|
12
|
+
}
|
|
13
|
+
/** Adds a default shape of `type` on top as one history entry and selects it (FR-4). */
|
|
14
|
+
export declare function addPaletteShape(editor: EditorStore, type: ShapeType, { center, gridSize, style }: PaletteOptions): void;
|
|
15
|
+
/**
|
|
16
|
+
* The canvas point under the center of the drawing area, with the current zoom and pan
|
|
17
|
+
* (PL-1); the canvas center while the area is not measured yet.
|
|
18
|
+
*/
|
|
19
|
+
export declare function visibleCenterOf(editor: EditorStore): Point;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Adding shapes from the toolbar palette (REA-053; Flutter FR-4, FR-5, FR-9, PL-1, PL-2).
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.DEFAULT_SHAPE_SIZES = void 0;
|
|
5
|
+
exports.addPaletteShape = addPaletteShape;
|
|
6
|
+
exports.visibleCenterOf = visibleCenterOf;
|
|
7
|
+
const core_1 = require("@crux-diagram/core");
|
|
8
|
+
/** Size of a new shape, in canvas units (Flutter spec 01, section 4). */
|
|
9
|
+
exports.DEFAULT_SHAPE_SIZES = {
|
|
10
|
+
square: { width: 160, height: 160 },
|
|
11
|
+
rectangle: { width: 240, height: 140 },
|
|
12
|
+
circle: { width: 160, height: 160 },
|
|
13
|
+
};
|
|
14
|
+
/** Distance of one cascade step, in canvas units (FR-5). */
|
|
15
|
+
const CASCADE_STEP = 20;
|
|
16
|
+
const inCanvas = (point, canvas) => point.x >= 0 && point.x <= canvas.width && point.y >= 0 && point.y <= canvas.height;
|
|
17
|
+
/**
|
|
18
|
+
* Where a new shape of `type` goes: `start` snapped to the grid, moved +20/+20 while a shape
|
|
19
|
+
* of the same type sits exactly there; when that would leave the canvas, −20/−20 from the
|
|
20
|
+
* start instead; when both are exhausted, the start (FR-5, PL-2).
|
|
21
|
+
*/
|
|
22
|
+
function freeCenter(document, type, start) {
|
|
23
|
+
const taken = (p) => document.shapes.some((s) => s.type === type && s.center.x === p.x && s.center.y === p.y);
|
|
24
|
+
if (!taken(start))
|
|
25
|
+
return start;
|
|
26
|
+
for (const direction of [1, -1]) {
|
|
27
|
+
let candidate = start;
|
|
28
|
+
for (;;) {
|
|
29
|
+
candidate = { x: candidate.x + direction * CASCADE_STEP, y: candidate.y + direction * CASCADE_STEP };
|
|
30
|
+
if (!inCanvas(candidate, document.canvas))
|
|
31
|
+
break;
|
|
32
|
+
if (!taken(candidate))
|
|
33
|
+
return candidate;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return start;
|
|
37
|
+
}
|
|
38
|
+
/** Adds a default shape of `type` on top as one history entry and selects it (FR-4). */
|
|
39
|
+
function addPaletteShape(editor, type, { center, gridSize, style }) {
|
|
40
|
+
const document = editor.getDocument();
|
|
41
|
+
const start = gridSize === null ? center : (0, core_1.snapPointToGrid)(center, gridSize);
|
|
42
|
+
const id = (0, core_1.generateId)('s');
|
|
43
|
+
const next = (0, core_1.addShape)(document, {
|
|
44
|
+
id,
|
|
45
|
+
type,
|
|
46
|
+
center: freeCenter(document, type, start),
|
|
47
|
+
size: exports.DEFAULT_SHAPE_SIZES[type],
|
|
48
|
+
rotation: 0,
|
|
49
|
+
...style,
|
|
50
|
+
});
|
|
51
|
+
editor.commit(next, [id]);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The canvas point under the center of the drawing area, with the current zoom and pan
|
|
55
|
+
* (PL-1); the canvas center while the area is not measured yet.
|
|
56
|
+
*/
|
|
57
|
+
function visibleCenterOf(editor) {
|
|
58
|
+
const { stageArea, view } = editor.getState();
|
|
59
|
+
const { canvas } = editor.getDocument();
|
|
60
|
+
if (!stageArea)
|
|
61
|
+
return { x: canvas.width / 2, y: canvas.height / 2 };
|
|
62
|
+
const viewport = (0, core_1.applyZoom)((0, core_1.clampZoom)(view.zoom, canvas, stageArea), (0, core_1.fitViewport)(canvas, stageArea));
|
|
63
|
+
return (0, core_1.visibleCenter)(viewport, stageArea, canvas);
|
|
64
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Point, type Size, type Vector, type ViewportZoom } from '@crux-diagram/core';
|
|
2
|
+
export interface WheelInput {
|
|
3
|
+
readonly deltaY: number;
|
|
4
|
+
/** `WheelEvent.deltaMode`: 0 pixels, 1 lines. */
|
|
5
|
+
readonly deltaMode?: number;
|
|
6
|
+
/** True for a trackpad pinch, which browsers report as a wheel with `ctrlKey`. */
|
|
7
|
+
readonly ctrlKey: boolean;
|
|
8
|
+
/** The pointer, in screen coordinates of the drawing area. */
|
|
9
|
+
readonly focal: Point;
|
|
10
|
+
}
|
|
11
|
+
/** The view after `wheel`: zoomed around the pointer, between 1× and 4×, with the pan kept inside the canvas. */
|
|
12
|
+
export declare function wheelZoom(current: ViewportZoom, wheel: WheelInput, canvas: Size, area: Size): ViewportZoom;
|
|
13
|
+
/** `current` moved by the screen `delta`, with the pan kept inside the canvas. */
|
|
14
|
+
export declare function panBy(current: ViewportZoom, delta: Vector, canvas: Size, area: Size): ViewportZoom;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Pure zoom and pan rules for the editor view (REA-016). The geometry (pinch, clamping)
|
|
3
|
+
// is core's `ViewportZoom`; this adds how a wheel event or a drag becomes a new view.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.wheelZoom = wheelZoom;
|
|
6
|
+
exports.panBy = panBy;
|
|
7
|
+
const core_1 = require("@crux-diagram/core");
|
|
8
|
+
/** Zoom per wheel pixel for a mouse wheel: one 100 px notch is about 22 %. */
|
|
9
|
+
const WHEEL_SENSITIVITY = 0.002;
|
|
10
|
+
/** Zoom per wheel pixel for a trackpad pinch (`ctrlKey`), whose deltas are small. */
|
|
11
|
+
const PINCH_SENSITIVITY = 0.01;
|
|
12
|
+
/** Pixels per line when the browser reports the wheel delta in lines (`deltaMode` 1). */
|
|
13
|
+
const LINE_PIXELS = 33;
|
|
14
|
+
/** The view after `wheel`: zoomed around the pointer, between 1× and 4×, with the pan kept inside the canvas. */
|
|
15
|
+
function wheelZoom(current, wheel, canvas, area) {
|
|
16
|
+
const pixels = wheel.deltaY * (wheel.deltaMode === 1 ? LINE_PIXELS : 1);
|
|
17
|
+
const scale = Math.exp(-pixels * (wheel.ctrlKey ? PINCH_SENSITIVITY : WHEEL_SENSITIVITY));
|
|
18
|
+
return (0, core_1.clampZoom)((0, core_1.pinchZoom)(current, { startFocal: wheel.focal, focal: wheel.focal, scale }), canvas, area);
|
|
19
|
+
}
|
|
20
|
+
/** `current` moved by the screen `delta`, with the pan kept inside the canvas. */
|
|
21
|
+
function panBy(current, delta, canvas, area) {
|
|
22
|
+
return (0, core_1.clampZoom)({ zoom: current.zoom, pan: { dx: current.pan.dx + delta.dx, dy: current.pan.dy + delta.dy } }, canvas, area);
|
|
23
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface EditorLabels {
|
|
2
|
+
readonly fitToScreen: string;
|
|
3
|
+
readonly rename: string;
|
|
4
|
+
readonly delete: string;
|
|
5
|
+
readonly small: string;
|
|
6
|
+
readonly medium: string;
|
|
7
|
+
readonly large: string;
|
|
8
|
+
readonly hide: string;
|
|
9
|
+
readonly show: string;
|
|
10
|
+
readonly hideJoints: string;
|
|
11
|
+
readonly showJoints: string;
|
|
12
|
+
readonly square: string;
|
|
13
|
+
readonly rectangle: string;
|
|
14
|
+
readonly circle: string;
|
|
15
|
+
readonly joint: string;
|
|
16
|
+
readonly grid: string;
|
|
17
|
+
readonly undo: string;
|
|
18
|
+
readonly redo: string;
|
|
19
|
+
readonly select: string;
|
|
20
|
+
readonly rotate: string;
|
|
21
|
+
readonly duplicate: string;
|
|
22
|
+
readonly front: string;
|
|
23
|
+
readonly back: string;
|
|
24
|
+
readonly group: string;
|
|
25
|
+
readonly ungroup: string;
|
|
26
|
+
readonly showGrid: string;
|
|
27
|
+
readonly hideGrid: string;
|
|
28
|
+
readonly rotate90: string;
|
|
29
|
+
readonly bringToFront: string;
|
|
30
|
+
readonly sendToBack: string;
|
|
31
|
+
readonly selectMode: string;
|
|
32
|
+
readonly jointTool: string;
|
|
33
|
+
readonly createBar: string;
|
|
34
|
+
readonly selectionBar: string;
|
|
35
|
+
}
|
|
36
|
+
export type EditorLabelsInput = Partial<EditorLabels>;
|
|
37
|
+
export declare const DEFAULT_EDITOR_LABELS: EditorLabels;
|
|
38
|
+
/** The defaults with the given texts on top; a field given as `undefined` keeps its default. */
|
|
39
|
+
export declare function resolveEditorLabels(input?: EditorLabelsInput): EditorLabels;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Texts of the ready-made controls (REA-053), like Flutter's `DrawingEditorLabels`: short
|
|
3
|
+
// visible texts with the longer ones as titles. English by default.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.DEFAULT_EDITOR_LABELS = void 0;
|
|
6
|
+
exports.resolveEditorLabels = resolveEditorLabels;
|
|
7
|
+
exports.DEFAULT_EDITOR_LABELS = {
|
|
8
|
+
fitToScreen: 'Fit to screen',
|
|
9
|
+
rename: 'Rename',
|
|
10
|
+
delete: 'Delete',
|
|
11
|
+
small: 'Small',
|
|
12
|
+
medium: 'Medium',
|
|
13
|
+
large: 'Large',
|
|
14
|
+
hide: 'Hide',
|
|
15
|
+
show: 'Show',
|
|
16
|
+
hideJoints: 'Hide joints',
|
|
17
|
+
showJoints: 'Show joints',
|
|
18
|
+
square: 'Square',
|
|
19
|
+
rectangle: 'Rect',
|
|
20
|
+
circle: 'Circle',
|
|
21
|
+
joint: 'Joint',
|
|
22
|
+
grid: 'Grid',
|
|
23
|
+
undo: 'Undo',
|
|
24
|
+
redo: 'Redo',
|
|
25
|
+
select: 'Select',
|
|
26
|
+
rotate: 'Rotate',
|
|
27
|
+
duplicate: 'Copy',
|
|
28
|
+
front: 'Front',
|
|
29
|
+
back: 'Back',
|
|
30
|
+
group: 'Group',
|
|
31
|
+
ungroup: 'Ungroup',
|
|
32
|
+
showGrid: 'Show grid',
|
|
33
|
+
hideGrid: 'Hide grid',
|
|
34
|
+
rotate90: 'Rotate 90°',
|
|
35
|
+
bringToFront: 'Bring to front',
|
|
36
|
+
sendToBack: 'Send to back',
|
|
37
|
+
selectMode: 'Select several shapes',
|
|
38
|
+
jointTool: 'Add joints',
|
|
39
|
+
createBar: 'Create',
|
|
40
|
+
selectionBar: 'Selection',
|
|
41
|
+
};
|
|
42
|
+
/** The defaults with the given texts on top; a field given as `undefined` keeps its default. */
|
|
43
|
+
function resolveEditorLabels(input) {
|
|
44
|
+
const labels = { ...exports.DEFAULT_EDITOR_LABELS };
|
|
45
|
+
for (const [key, value] of Object.entries(input ?? {}))
|
|
46
|
+
if (typeof value === 'string')
|
|
47
|
+
labels[key] = value;
|
|
48
|
+
return labels;
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs","sideEffects":false}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type CanvasViewport, type DrawingDocument, type JointStyle, type Size, type ViewportZoom } from '@crux-diagram/core';
|
|
2
|
+
import type { KonvaEventObject } from 'konva/lib/Node';
|
|
3
|
+
import { type ReactNode } from 'react';
|
|
4
|
+
export interface DrawingStageProps {
|
|
5
|
+
readonly document: DrawingDocument;
|
|
6
|
+
readonly jointStyle?: JointStyle;
|
|
7
|
+
/** Whether the stage handles pointer events. */
|
|
8
|
+
readonly listening: boolean;
|
|
9
|
+
/** Zoom and pan on top of the fitted view (default: fitted); clamped so the canvas stays in view. */
|
|
10
|
+
readonly zoom?: ViewportZoom;
|
|
11
|
+
/** Called on a mouse wheel or trackpad pinch over the stage (REA-016). */
|
|
12
|
+
readonly onWheel?: (event: KonvaEventObject<WheelEvent>) => void;
|
|
13
|
+
/** Alternative description of the drawing for screen readers (REA-052); a summary by default. */
|
|
14
|
+
readonly alt?: string;
|
|
15
|
+
/** Ids of the selected shapes, announced in the shape list (REA-052). */
|
|
16
|
+
readonly selection?: readonly string[];
|
|
17
|
+
/** Called with the measured size of the drawing area whenever it changes. */
|
|
18
|
+
readonly onAreaChange?: (area: Size) => void;
|
|
19
|
+
/** Called when a pointer goes down anywhere on the stage (the editor's gestures). */
|
|
20
|
+
readonly onPointerDown?: (event: KonvaEventObject<PointerEvent>) => void;
|
|
21
|
+
/** HTML drawn over the stage, positioned with the viewport (the joint name editor). */
|
|
22
|
+
readonly overlay?: (viewport: CanvasViewport) => ReactNode;
|
|
23
|
+
/** Layer 4, drawn above the joints; a function receives the view scale (screen pixels per canvas unit). */
|
|
24
|
+
readonly children?: ReactNode | ((scale: number) => ReactNode);
|
|
25
|
+
}
|
|
26
|
+
/** Fills its parent and fits the canvas into it, centered, keeping the aspect ratio. */
|
|
27
|
+
export declare function DrawingStage({ document, jointStyle, listening, zoom, onWheel, onPointerDown, onAreaChange, alt, selection, overlay, children }: DrawingStageProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DrawingStage = DrawingStage;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
// The Konva stage shared by the viewer and the editor. Layers, bottom to top (spec 05
|
|
6
|
+
// section 4): (1) background, (2) shapes, (3) joints, (4) editing UI, given as children.
|
|
7
|
+
const core_1 = require("@crux-diagram/core");
|
|
8
|
+
const react_1 = require("react");
|
|
9
|
+
const react_konva_1 = require("react-konva");
|
|
10
|
+
const fonts_js_1 = require("./fonts.js");
|
|
11
|
+
const a11y_js_1 = require("../a11y.js");
|
|
12
|
+
const scene_js_1 = require("./scene.js");
|
|
13
|
+
/** Fills its parent and fits the canvas into it, centered, keeping the aspect ratio. */
|
|
14
|
+
function DrawingStage({ document, jointStyle = core_1.DEFAULT_JOINT_STYLE, listening, zoom = core_1.FITTED_ZOOM, onWheel, onPointerDown, onAreaChange, alt, selection, overlay, children }) {
|
|
15
|
+
const container = (0, react_1.useRef)(null);
|
|
16
|
+
const area = useContainerSize(container);
|
|
17
|
+
const width = area?.width;
|
|
18
|
+
const height = area?.height;
|
|
19
|
+
(0, react_1.useEffect)(() => {
|
|
20
|
+
if (width !== undefined && height !== undefined)
|
|
21
|
+
onAreaChange?.({ width, height });
|
|
22
|
+
}, [width, height, onAreaChange]);
|
|
23
|
+
return ((0, jsx_runtime_1.jsxs)("div", { ref: container, role: "group", "aria-label": alt ?? (0, a11y_js_1.describeDrawing)(document), style: { position: 'relative', width: '100%', height: '100%', overflow: 'hidden' }, children: [(0, jsx_runtime_1.jsx)(a11y_js_1.DrawingList, { document: document, selection: selection }), area && area.width > 0 && area.height > 0 && ((0, jsx_runtime_1.jsx)(FittedStage, { document: document, jointStyle: jointStyle, listening: listening, zoom: zoom, onWheel: onWheel, onPointerDown: onPointerDown, overlay: overlay, area: area, children: children }))] }));
|
|
24
|
+
}
|
|
25
|
+
function FittedStage({ document, jointStyle, listening, zoom, onWheel, onPointerDown, overlay, area, children, }) {
|
|
26
|
+
const viewport = (0, core_1.applyZoom)((0, core_1.clampZoom)(zoom, document.canvas, area), (0, core_1.fitViewport)(document.canvas, area));
|
|
27
|
+
const clip = { x: 0, y: 0, width: document.canvas.width, height: document.canvas.height };
|
|
28
|
+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { "aria-hidden": "true", children: (0, jsx_runtime_1.jsxs)(react_konva_1.Stage, { width: area.width, height: area.height, x: viewport.origin.x, y: viewport.origin.y, scaleX: viewport.scale, scaleY: viewport.scale, listening: listening, onPointerDown: onPointerDown, onWheel: onWheel, children: [(0, jsx_runtime_1.jsx)(react_konva_1.Layer, { name: "background", clip: clip, listening: false, children: (0, jsx_runtime_1.jsx)(react_konva_1.Rect, { name: "background-fill", width: clip.width, height: clip.height, fill: "#ffffff" }) }), (0, jsx_runtime_1.jsx)(react_konva_1.Layer, { name: "shapes", clip: clip, children: document.shapes.map((shape) => ((0, jsx_runtime_1.jsx)(ShapeNode, { shape: shape }, shape.id))) }), (0, jsx_runtime_1.jsx)(JointsLayer, { document: document, style: jointStyle, clip: clip }), typeof children === 'function' ? children(viewport.scale) : children] }) }), overlay?.(viewport)] }));
|
|
29
|
+
}
|
|
30
|
+
/** One shape; re-renders only when its data changes (REA-050). */
|
|
31
|
+
const ShapeNode = (0, react_1.memo)(function ShapeNode({ shape }) {
|
|
32
|
+
const { kind, ...props } = (0, scene_js_1.shapeNodeProps)(shape);
|
|
33
|
+
return kind === 'ellipse' ? (0, jsx_runtime_1.jsx)(react_konva_1.Ellipse, { id: shape.id, ...props }) : (0, jsx_runtime_1.jsx)(react_konva_1.Rect, { id: shape.id, ...props });
|
|
34
|
+
});
|
|
35
|
+
function JointsLayer({ document, style, clip }) {
|
|
36
|
+
const fontsReady = (0, fonts_js_1.useFontsReady)();
|
|
37
|
+
const measurer = (0, fonts_js_1.useLabelMeasurer)();
|
|
38
|
+
const graphics = (0, react_1.useMemo)(() => {
|
|
39
|
+
if (!fontsReady)
|
|
40
|
+
return [];
|
|
41
|
+
const labelSize = document.jointDisplay.labelSize;
|
|
42
|
+
return (0, scene_js_1.placeJoints)(document, style, measurer()).map((p) => ({
|
|
43
|
+
id: p.joint.id,
|
|
44
|
+
...(0, scene_js_1.jointGraphics)(p, style, labelSize),
|
|
45
|
+
}));
|
|
46
|
+
}, [document, style, fontsReady, measurer]);
|
|
47
|
+
const color = (0, core_1.argbToCss)(style.color);
|
|
48
|
+
return ((0, jsx_runtime_1.jsx)(react_konva_1.Layer, { name: "joints", clip: clip, children: graphics.map(({ id, line, head, text }) => [
|
|
49
|
+
line && ((0, jsx_runtime_1.jsx)(react_konva_1.Line, { name: "joint-line", points: line, stroke: color, strokeWidth: style.lineWidth, lineCap: "butt" }, `${id}-line`)),
|
|
50
|
+
head && (0, jsx_runtime_1.jsx)(react_konva_1.Line, { name: "joint-head", points: head, closed: true, fill: color }, `${id}-head`),
|
|
51
|
+
(0, jsx_runtime_1.jsx)(react_konva_1.Text, { id: id, name: "joint-label", ...text, align: "center", verticalAlign: "middle", wrap: "none" }, `${id}-label`),
|
|
52
|
+
]) }));
|
|
53
|
+
}
|
|
54
|
+
/** The content size of `ref`, tracked with `ResizeObserver` (window resizes without it); `null` before mount. */
|
|
55
|
+
function useContainerSize(ref) {
|
|
56
|
+
const [size, setSize] = (0, react_1.useState)(null);
|
|
57
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
58
|
+
const element = ref.current;
|
|
59
|
+
if (!element)
|
|
60
|
+
return;
|
|
61
|
+
const update = (width, height) => setSize((old) => (old && old.width === width && old.height === height ? old : { width, height }));
|
|
62
|
+
if (typeof ResizeObserver === 'undefined') {
|
|
63
|
+
// Without ResizeObserver, follow window resizes only.
|
|
64
|
+
const read = () => update(element.clientWidth, element.clientHeight);
|
|
65
|
+
read();
|
|
66
|
+
window.addEventListener('resize', read);
|
|
67
|
+
return () => window.removeEventListener('resize', read);
|
|
68
|
+
}
|
|
69
|
+
const observer = new ResizeObserver(([entry]) => {
|
|
70
|
+
if (entry)
|
|
71
|
+
update(entry.contentRect.width, entry.contentRect.height);
|
|
72
|
+
});
|
|
73
|
+
observer.observe(element);
|
|
74
|
+
return () => observer.disconnect();
|
|
75
|
+
}, [ref]);
|
|
76
|
+
return size;
|
|
77
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type TextMeasurer } from '@crux-diagram/core';
|
|
2
|
+
/** Resolves when `document.fonts` has settled, also when loading fails; at once without a font set (REA-032). */
|
|
3
|
+
export declare function whenFontsReady(): Promise<void>;
|
|
4
|
+
/** Whether `document.fonts` has settled; labels are measured only after it has (REA-032). */
|
|
5
|
+
export declare function useFontsReady(): boolean;
|
|
6
|
+
/** One measurer per component, created on first use in the browser with the label font. */
|
|
7
|
+
export declare function useLabelMeasurer(): () => TextMeasurer;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Font readiness and the label measurer (REA-030, REA-032). Client only: nothing here
|
|
3
|
+
// runs at import time.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.whenFontsReady = whenFontsReady;
|
|
6
|
+
exports.useFontsReady = useFontsReady;
|
|
7
|
+
exports.useLabelMeasurer = useLabelMeasurer;
|
|
8
|
+
const core_1 = require("@crux-diagram/core");
|
|
9
|
+
const react_1 = require("react");
|
|
10
|
+
const scene_js_1 = require("./scene.js");
|
|
11
|
+
function fontSet() {
|
|
12
|
+
return globalThis.document?.fonts;
|
|
13
|
+
}
|
|
14
|
+
/** Resolves when `document.fonts` has settled, also when loading fails; at once without a font set (REA-032). */
|
|
15
|
+
async function whenFontsReady() {
|
|
16
|
+
const fonts = fontSet();
|
|
17
|
+
if (!fonts || fonts.status === 'loaded')
|
|
18
|
+
return;
|
|
19
|
+
await fonts.ready.catch(() => undefined);
|
|
20
|
+
}
|
|
21
|
+
/** Whether `document.fonts` has settled; labels are measured only after it has (REA-032). */
|
|
22
|
+
function useFontsReady() {
|
|
23
|
+
const [ready, setReady] = (0, react_1.useState)(() => {
|
|
24
|
+
const fonts = fontSet();
|
|
25
|
+
return !fonts || fonts.status === 'loaded';
|
|
26
|
+
});
|
|
27
|
+
(0, react_1.useEffect)(() => {
|
|
28
|
+
if (ready)
|
|
29
|
+
return;
|
|
30
|
+
let live = true;
|
|
31
|
+
// A failed load still measures, with whatever font the browser falls back to.
|
|
32
|
+
const settle = () => {
|
|
33
|
+
if (live)
|
|
34
|
+
setReady(true);
|
|
35
|
+
};
|
|
36
|
+
void fontSet()?.ready.then(settle, settle);
|
|
37
|
+
return () => {
|
|
38
|
+
live = false;
|
|
39
|
+
};
|
|
40
|
+
}, [ready]);
|
|
41
|
+
return ready;
|
|
42
|
+
}
|
|
43
|
+
/** One measurer per component, created on first use in the browser with the label font. */
|
|
44
|
+
function useLabelMeasurer() {
|
|
45
|
+
const [get] = (0, react_1.useState)(() => {
|
|
46
|
+
let measurer;
|
|
47
|
+
return () => (measurer ??= new core_1.CanvasTextMeasurer({ fontFamily: scene_js_1.LABEL_FONT_FAMILY }));
|
|
48
|
+
});
|
|
49
|
+
return get;
|
|
50
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { type DrawingDocument, type Joint, type JointLabelSize, type JointStyle, type Point, type Rect, type Shape, type TextMeasurer } from '@crux-diagram/core';
|
|
2
|
+
/** Font family of joint labels, fixed in both implementations (REA-030, spec 02 section 12). */
|
|
3
|
+
export declare const LABEL_FONT_FAMILY = "Arial";
|
|
4
|
+
interface ShapeNodeBase {
|
|
5
|
+
readonly x: number;
|
|
6
|
+
readonly y: number;
|
|
7
|
+
readonly width: number;
|
|
8
|
+
readonly height: number;
|
|
9
|
+
/** Degrees, clockwise, as Konva expects. */
|
|
10
|
+
readonly rotation: number;
|
|
11
|
+
readonly fill: string;
|
|
12
|
+
readonly stroke: string;
|
|
13
|
+
readonly strokeWidth: number;
|
|
14
|
+
readonly fillEnabled: boolean;
|
|
15
|
+
readonly strokeEnabled: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** A square or rectangle, pivoting on its center through the offset (spec 05 section 4). */
|
|
18
|
+
export interface RectNodeProps extends ShapeNodeBase {
|
|
19
|
+
readonly kind: 'rect';
|
|
20
|
+
readonly offsetX: number;
|
|
21
|
+
readonly offsetY: number;
|
|
22
|
+
}
|
|
23
|
+
/** A circle: the ellipse inscribed in its size, which Konva already centers on `x, y`. */
|
|
24
|
+
export interface EllipseNodeProps extends ShapeNodeBase {
|
|
25
|
+
readonly kind: 'ellipse';
|
|
26
|
+
readonly radiusX: number;
|
|
27
|
+
readonly radiusY: number;
|
|
28
|
+
}
|
|
29
|
+
export type ShapeNodeProps = RectNodeProps | EllipseNodeProps;
|
|
30
|
+
/** Konva props for `shape`. Paint with no alpha or no width is disabled, like Flutter. */
|
|
31
|
+
export declare function shapeNodeProps(shape: Shape): ShapeNodeProps;
|
|
32
|
+
/** A joint resolved against its document, in canvas units. */
|
|
33
|
+
export interface JointPlacement {
|
|
34
|
+
readonly joint: Joint;
|
|
35
|
+
/** Canvas position of the joint's anchor. */
|
|
36
|
+
readonly anchor: Point;
|
|
37
|
+
/** Measured size of the name. */
|
|
38
|
+
readonly textSize: {
|
|
39
|
+
readonly width: number;
|
|
40
|
+
readonly height: number;
|
|
41
|
+
};
|
|
42
|
+
/** Padded label rectangle centered at `anchor + labelOffset`. */
|
|
43
|
+
readonly labelRect: Rect;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Places every joint whose shape and anchor exist, measuring names at the document's
|
|
47
|
+
* label size. Hidden joints are not placed, so they are neither drawn nor exported.
|
|
48
|
+
*/
|
|
49
|
+
export declare function placeJoints(document: DrawingDocument, style: JointStyle, measurer: TextMeasurer): JointPlacement[];
|
|
50
|
+
/** Konva props of a joint label. */
|
|
51
|
+
export interface LabelTextProps {
|
|
52
|
+
readonly text: string;
|
|
53
|
+
readonly x: number;
|
|
54
|
+
readonly y: number;
|
|
55
|
+
readonly width: number;
|
|
56
|
+
readonly height: number;
|
|
57
|
+
readonly fontSize: number;
|
|
58
|
+
readonly fontFamily: string;
|
|
59
|
+
readonly fontStyle: 'bold' | 'normal';
|
|
60
|
+
readonly fill: string;
|
|
61
|
+
}
|
|
62
|
+
/** What to draw for one joint: flat point lists as Konva expects them. */
|
|
63
|
+
export interface JointGraphics {
|
|
64
|
+
/** Leader line from the label edge to the arrowhead base, or `null`. */
|
|
65
|
+
readonly line: number[] | null;
|
|
66
|
+
/** Arrowhead triangle `[tip, base + normal, base − normal]`, or `null`. */
|
|
67
|
+
readonly head: number[] | null;
|
|
68
|
+
readonly text: LabelTextProps;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Leader arrow and label of `placement` (FMT-043, FMT-044). No arrow when the anchor is
|
|
72
|
+
* inside the label; no line when it would not reach past the arrowhead.
|
|
73
|
+
*/
|
|
74
|
+
export declare function jointGraphics(placement: JointPlacement, style: JointStyle, labelSize: JointLabelSize): JointGraphics;
|
|
75
|
+
export {};
|