@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
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daniel Garrido
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,51 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @crux-diagram/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React viewer and editor for drawings in the crux_diagram JSON format (v1), with the same behavior as the Flutter editor [`crux_diagram`](https://pub.dev/packages/crux_diagram).
|
|
4
|
+
|
|
5
|
+
- `DrawingViewer`: read-only view of a drawing.
|
|
6
|
+
- `DrawingEditor`: controlled editor with shapes, joints, groups, zoom and pan, and image export.
|
|
7
|
+
- `Toolbar` and `JointDisplayBar`: ready-made controls for the editor.
|
|
8
|
+
- `VersionHistory`: controlled list of saved versions with preview and restore.
|
|
9
|
+
- Safe to import in server rendering (Next.js): the canvas mounts only in the client.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @crux-diagram/react @crux-diagram/core react react-dom konva react-konva
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`react`, `react-dom` (19), `konva` (10) and `react-konva` (19) are peer dependencies.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { parse } from '@crux-diagram/core';
|
|
23
|
+
import { useState } from 'react';
|
|
24
|
+
import { DrawingEditor, Toolbar } from '@crux-diagram/react';
|
|
25
|
+
|
|
26
|
+
const empty = parse(
|
|
27
|
+
'{"schemaVersion":1,"canvas":{"width":1000,"height":800},"shapes":[],"joints":[],"jointDisplay":{"labelSize":"medium","visible":true},"groups":[]}',
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
export function App() {
|
|
31
|
+
const [doc, setDoc] = useState(empty);
|
|
32
|
+
return (
|
|
33
|
+
<div style={{ width: 800, height: 600 }}>
|
|
34
|
+
<DrawingEditor value={doc} onChange={setDoc}>
|
|
35
|
+
<Toolbar />
|
|
36
|
+
</DrawingEditor>
|
|
37
|
+
</div>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The editor is controlled: it never stores the drawing by itself. Save `serialize(doc)` from `@crux-diagram/core` wherever your app keeps it.
|
|
43
|
+
|
|
44
|
+
## Documentation
|
|
45
|
+
|
|
46
|
+
- Behavior: [`spec/05-spec-react-editor.md`](https://github.com/DanM10/crux_diagram/blob/main/spec/05-spec-react-editor.md)
|
|
47
|
+
- Large drawings: [`docs/large-drawings.md`](https://github.com/DanM10/crux_diagram/blob/main/js/react/docs/large-drawings.md)
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
MIT
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type DrawingEditorHandle, type EditorProviderProps } from './editor-context.js';
|
|
2
|
+
export type DrawingEditorProps = EditorProviderProps & {
|
|
3
|
+
/**
|
|
4
|
+
* Shows the document without selection, dragging or keyboard shortcuts (REA-005).
|
|
5
|
+
* Changes from the parent's `value` and the imperative handle still apply.
|
|
6
|
+
*/
|
|
7
|
+
readOnly?: boolean;
|
|
8
|
+
/** Alternative description for screen readers (REA-052); a summary of the drawing by default. */
|
|
9
|
+
alt?: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Controlled drawing editor (REA-002). Click or drag shapes to select and move them;
|
|
13
|
+
* while the editor has the focus, `Delete`/`Backspace` delete, `Ctrl/Cmd+Z` undoes,
|
|
14
|
+
* `Ctrl/Cmd+Shift+Z` redoes, `Ctrl/Cmd+D` duplicates and the arrow keys move the
|
|
15
|
+
* selection by 1 (`Shift`: 10) (REA-010 to REA-018).
|
|
16
|
+
*/
|
|
17
|
+
export declare const DrawingEditor: import("react").ForwardRefExoticComponent<EditorProviderProps & {
|
|
18
|
+
/**
|
|
19
|
+
* Shows the document without selection, dragging or keyboard shortcuts (REA-005).
|
|
20
|
+
* Changes from the parent's `value` and the imperative handle still apply.
|
|
21
|
+
*/
|
|
22
|
+
readOnly?: boolean;
|
|
23
|
+
/** Alternative description for screen readers (REA-052); a summary of the drawing by default. */
|
|
24
|
+
alt?: string;
|
|
25
|
+
} & import("react").RefAttributes<DrawingEditorHandle>>;
|
|
26
|
+
/**
|
|
27
|
+
* The editor's stage: the gesture preview while there is one, else the document, with
|
|
28
|
+
* the selection drawn in the `editor-ui` layer. Pointer gestures and shortcuts drive the
|
|
29
|
+
* enclosing editor's store.
|
|
30
|
+
*/
|
|
31
|
+
export declare function EditorCanvas({ readOnly, alt }: {
|
|
32
|
+
readOnly?: boolean;
|
|
33
|
+
alt?: string;
|
|
34
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.DrawingEditor = void 0;
|
|
5
|
+
exports.EditorCanvas = EditorCanvas;
|
|
6
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
7
|
+
const core_1 = require("@crux-diagram/core");
|
|
8
|
+
const react_1 = require("react");
|
|
9
|
+
const react_konva_1 = require("react-konva");
|
|
10
|
+
const zustand_1 = require("zustand");
|
|
11
|
+
const editor_context_js_1 = require("./editor-context.js");
|
|
12
|
+
const gestures_js_1 = require("./interaction/gestures.js");
|
|
13
|
+
const NameEditor_js_1 = require("./NameEditor.js");
|
|
14
|
+
const joints_js_1 = require("./interaction/joints.js");
|
|
15
|
+
const keyboard_js_1 = require("./interaction/keyboard.js");
|
|
16
|
+
const viewport_js_1 = require("./interaction/viewport.js");
|
|
17
|
+
const DrawingStage_js_1 = require("./render/DrawingStage.js");
|
|
18
|
+
const fonts_js_1 = require("./render/fonts.js");
|
|
19
|
+
const scene_js_1 = require("./render/scene.js");
|
|
20
|
+
/** Longest time between the two taps of a double tap on a label, in milliseconds (ZM-4). */
|
|
21
|
+
const DOUBLE_TAP_TIMEOUT = 300;
|
|
22
|
+
/**
|
|
23
|
+
* Controlled drawing editor (REA-002). Click or drag shapes to select and move them;
|
|
24
|
+
* while the editor has the focus, `Delete`/`Backspace` delete, `Ctrl/Cmd+Z` undoes,
|
|
25
|
+
* `Ctrl/Cmd+Shift+Z` redoes, `Ctrl/Cmd+D` duplicates and the arrow keys move the
|
|
26
|
+
* selection by 1 (`Shift`: 10) (REA-010 to REA-018).
|
|
27
|
+
*/
|
|
28
|
+
exports.DrawingEditor = (0, react_1.forwardRef)(function DrawingEditor({ readOnly, alt, children, ...props }, ref) {
|
|
29
|
+
return ((0, jsx_runtime_1.jsx)(editor_context_js_1.EditorProvider, { ref: ref, ...props, children: children === undefined ? ((0, jsx_runtime_1.jsx)(EditorCanvas, { readOnly: readOnly, alt: alt })) : ((0, jsx_runtime_1.jsxs)("div", { style: { display: 'flex', flexDirection: 'column', width: '100%', height: '100%' }, children: [children, (0, jsx_runtime_1.jsx)("div", { style: { flex: 1, minHeight: 0, position: 'relative' }, children: (0, jsx_runtime_1.jsx)(EditorCanvas, { readOnly: readOnly, alt: alt }) })] })) }));
|
|
30
|
+
});
|
|
31
|
+
/** Color of the selection outline and the selection rectangle. */
|
|
32
|
+
const SELECTION_COLOR = '#1e88e5';
|
|
33
|
+
/** Color of the ring on the anchor a shape is snapping onto (SN-3). */
|
|
34
|
+
const SNAP_COLOR = '#ff9800';
|
|
35
|
+
/**
|
|
36
|
+
* The editor's stage: the gesture preview while there is one, else the document, with
|
|
37
|
+
* the selection drawn in the `editor-ui` layer. Pointer gestures and shortcuts drive the
|
|
38
|
+
* enclosing editor's store.
|
|
39
|
+
*/
|
|
40
|
+
function EditorCanvas({ readOnly = false, alt }) {
|
|
41
|
+
const editor = (0, editor_context_js_1.useEditor)();
|
|
42
|
+
const config = (0, editor_context_js_1.useEditorConfig)();
|
|
43
|
+
const document = (0, zustand_1.useStore)(editor, (state) => state.preview ?? state.document);
|
|
44
|
+
const zoom = (0, zustand_1.useStore)(editor, (state) => state.view.zoom);
|
|
45
|
+
const tool = (0, zustand_1.useStore)(editor, (state) => state.tool);
|
|
46
|
+
const naming = (0, zustand_1.useStore)(editor, (state) => state.naming);
|
|
47
|
+
const selectedJoint = (0, zustand_1.useStore)(editor, (state) => state.selectedJoint);
|
|
48
|
+
const snapHighlight = (0, zustand_1.useStore)(editor, (state) => state.snapHighlight);
|
|
49
|
+
const selection = (0, zustand_1.useStore)(editor, (state) => state.selection);
|
|
50
|
+
const [marquee, setMarquee] = (0, react_1.useState)(null);
|
|
51
|
+
const focusable = (0, react_1.useRef)(null);
|
|
52
|
+
const session = (0, react_1.useRef)(null);
|
|
53
|
+
const spaceDown = (0, react_1.useRef)(false);
|
|
54
|
+
const lastLabelTap = (0, react_1.useRef)(null);
|
|
55
|
+
const fontsReady = (0, fonts_js_1.useFontsReady)();
|
|
56
|
+
const measurer = (0, fonts_js_1.useLabelMeasurer)();
|
|
57
|
+
// The labels as drawn, to hit-test them; before the fonts load none are drawn.
|
|
58
|
+
const labelRects = (0, react_1.useCallback)(() => {
|
|
59
|
+
if (!fontsReady)
|
|
60
|
+
return [];
|
|
61
|
+
return (0, scene_js_1.placeJoints)(editor.getDocument(), core_1.DEFAULT_JOINT_STYLE, measurer()).map((p) => ({ jointId: p.joint.id, rect: p.labelRect }));
|
|
62
|
+
}, [editor, fontsReady, measurer]);
|
|
63
|
+
// A pointer still down when the editor unmounts, or turns read-only, ends with no change.
|
|
64
|
+
const endSession = (0, react_1.useCallback)(() => {
|
|
65
|
+
const current = session.current;
|
|
66
|
+
if (!current)
|
|
67
|
+
return;
|
|
68
|
+
session.current = null;
|
|
69
|
+
current.dispose();
|
|
70
|
+
if (current.gesture)
|
|
71
|
+
(0, gestures_js_1.cancelDrag)(editor, current.gesture);
|
|
72
|
+
setMarquee(null);
|
|
73
|
+
}, [editor]);
|
|
74
|
+
(0, react_1.useEffect)(() => endSession, [endSession]);
|
|
75
|
+
(0, react_1.useEffect)(() => {
|
|
76
|
+
if (readOnly)
|
|
77
|
+
endSession();
|
|
78
|
+
}, [readOnly, endSession]);
|
|
79
|
+
const onPointerDown = (event) => {
|
|
80
|
+
const evt = event.evt;
|
|
81
|
+
if (readOnly || evt.button > 0)
|
|
82
|
+
return;
|
|
83
|
+
// The same pointer pressed again means its release was lost: start over. Another
|
|
84
|
+
// pointer while one is down is ignored (multi-touch is a zoom gesture).
|
|
85
|
+
if (session.current?.pointerId === evt.pointerId)
|
|
86
|
+
endSession();
|
|
87
|
+
if (session.current)
|
|
88
|
+
return;
|
|
89
|
+
const stage = event.target.getStage();
|
|
90
|
+
const downCanvas = stage?.getRelativePointerPosition();
|
|
91
|
+
if (!stage || !downCanvas)
|
|
92
|
+
return;
|
|
93
|
+
focusable.current?.focus({ preventScroll: true });
|
|
94
|
+
const { gridSize, gridMajorEvery, anchorSnapThreshold } = config;
|
|
95
|
+
const grid = {
|
|
96
|
+
gridSize,
|
|
97
|
+
// The threshold is in screen pixels (REA-026); gestures work in canvas units.
|
|
98
|
+
anchorSnapDistance: anchorSnapThreshold === null ? null : anchorSnapThreshold / stage.scaleX(),
|
|
99
|
+
visibleStep: gridSize !== null && editor.getState().view.showGrid
|
|
100
|
+
? (0, core_1.visibleGridStep)({ gridSize, majorEvery: gridMajorEvery, scale: stage.scaleX() })
|
|
101
|
+
: null,
|
|
102
|
+
};
|
|
103
|
+
const handles = {
|
|
104
|
+
scale: stage.scaleX(),
|
|
105
|
+
rotationSnap: { step: config.rotationSnapStep, tolerance: config.rotationSnapTolerance },
|
|
106
|
+
labels: labelRects(),
|
|
107
|
+
};
|
|
108
|
+
const area = { width: stage.width(), height: stage.height() };
|
|
109
|
+
const shownZoom = () => (0, core_1.clampZoom)(editor.getState().view.zoom, editor.getDocument().canvas, area);
|
|
110
|
+
const canvasPoint = (e) => {
|
|
111
|
+
stage.setPointersPositions(e);
|
|
112
|
+
return stage.getRelativePointerPosition() ?? downCanvas;
|
|
113
|
+
};
|
|
114
|
+
const onMove = (e) => {
|
|
115
|
+
const current = session.current;
|
|
116
|
+
if (!current || e.pointerId !== current.pointerId)
|
|
117
|
+
return;
|
|
118
|
+
if (!current.pan && !current.gesture) {
|
|
119
|
+
if (Math.hypot(e.clientX - current.downScreen.x, e.clientY - current.downScreen.y) < gestures_js_1.TAP_SLOP)
|
|
120
|
+
return;
|
|
121
|
+
// The joint tool never moves, resizes or rotates shapes (JT-1).
|
|
122
|
+
// Labels can be dragged in any tool (REA-022).
|
|
123
|
+
current.gesture =
|
|
124
|
+
editor.getState().tool === 'joint'
|
|
125
|
+
? ((0, gestures_js_1.beginLabelDrag)(editor, current.downCanvas, handles.labels ?? []) ?? { kind: 'none' })
|
|
126
|
+
: (0, gestures_js_1.beginDrag)(editor, current.downCanvas, grid, handles);
|
|
127
|
+
// A drag that grabs nothing pans when zoomed in (REA-016); in selection mode it is a marquee.
|
|
128
|
+
if (current.gesture.kind === 'none' && editor.getState().view.zoom.zoom > 1)
|
|
129
|
+
current.pan = shownZoom();
|
|
130
|
+
}
|
|
131
|
+
if (current.pan) {
|
|
132
|
+
const delta = { dx: e.clientX - current.downScreen.x, dy: e.clientY - current.downScreen.y };
|
|
133
|
+
editor.setZoom((0, viewport_js_1.panBy)(current.pan, delta, editor.getDocument().canvas, area));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (!current.gesture)
|
|
137
|
+
return;
|
|
138
|
+
current.gesture = (0, gestures_js_1.updateDrag)(editor, current.gesture, canvasPoint(e));
|
|
139
|
+
setMarquee(current.gesture.kind === 'marquee' ? current.gesture.rect : null);
|
|
140
|
+
};
|
|
141
|
+
const onUp = (e) => {
|
|
142
|
+
const current = session.current;
|
|
143
|
+
if (!current || e.pointerId !== current.pointerId)
|
|
144
|
+
return;
|
|
145
|
+
session.current = null;
|
|
146
|
+
current.dispose();
|
|
147
|
+
if (current.gesture)
|
|
148
|
+
(0, gestures_js_1.endDrag)(editor, current.gesture);
|
|
149
|
+
else if (!current.pan) {
|
|
150
|
+
if (editor.getState().tool === 'joint') {
|
|
151
|
+
(0, joints_js_1.jointToolTap)(editor, current.downCanvas, {
|
|
152
|
+
scale: handles.scale,
|
|
153
|
+
namePrefix: config.jointNamePrefix,
|
|
154
|
+
nameMaxLength: config.jointNameMaxLength,
|
|
155
|
+
labels: handles.labels,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
else
|
|
159
|
+
(0, gestures_js_1.tap)(editor, current.downCanvas, handles);
|
|
160
|
+
// The first tap of a double tap acts at once; the second, on the same label, renames (NM-2, ZM-4).
|
|
161
|
+
const labeled = (0, gestures_js_1.labelAt)(handles.labels ?? [], current.downCanvas);
|
|
162
|
+
const now = performance.now();
|
|
163
|
+
const last = lastLabelTap.current;
|
|
164
|
+
if (labeled && last?.jointId === labeled && now - last.time <= DOUBLE_TAP_TIMEOUT) {
|
|
165
|
+
lastLabelTap.current = null;
|
|
166
|
+
(0, joints_js_1.beginRename)(editor, labeled);
|
|
167
|
+
}
|
|
168
|
+
else
|
|
169
|
+
lastLabelTap.current = labeled ? { jointId: labeled, time: now } : null;
|
|
170
|
+
}
|
|
171
|
+
setMarquee(null);
|
|
172
|
+
};
|
|
173
|
+
const onCancel = (e) => {
|
|
174
|
+
if (session.current && e.pointerId === session.current.pointerId)
|
|
175
|
+
endSession();
|
|
176
|
+
};
|
|
177
|
+
window.addEventListener('pointermove', onMove);
|
|
178
|
+
window.addEventListener('pointerup', onUp);
|
|
179
|
+
window.addEventListener('pointercancel', onCancel);
|
|
180
|
+
session.current = {
|
|
181
|
+
pointerId: evt.pointerId,
|
|
182
|
+
downScreen: { x: evt.clientX, y: evt.clientY },
|
|
183
|
+
downCanvas,
|
|
184
|
+
gesture: null,
|
|
185
|
+
// With the space bar down the drag pans from the start, over shapes too (REA-016).
|
|
186
|
+
pan: spaceDown.current ? shownZoom() : null,
|
|
187
|
+
dispose: () => {
|
|
188
|
+
window.removeEventListener('pointermove', onMove);
|
|
189
|
+
window.removeEventListener('pointerup', onUp);
|
|
190
|
+
window.removeEventListener('pointercancel', onCancel);
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
const onWheel = (event) => {
|
|
195
|
+
const stage = event.target.getStage();
|
|
196
|
+
const focal = stage?.getPointerPosition();
|
|
197
|
+
if (!stage || !focal)
|
|
198
|
+
return;
|
|
199
|
+
event.evt.preventDefault();
|
|
200
|
+
const { deltaY, deltaMode, ctrlKey } = event.evt;
|
|
201
|
+
const area = { width: stage.width(), height: stage.height() };
|
|
202
|
+
editor.setZoom((0, viewport_js_1.wheelZoom)(editor.getState().view.zoom, { deltaY, deltaMode, ctrlKey, focal }, editor.getDocument().canvas, area));
|
|
203
|
+
};
|
|
204
|
+
// The space bar pans only while the container itself has the focus, so it still activates buttons.
|
|
205
|
+
const onKeyUp = (event) => {
|
|
206
|
+
if (event.key === ' ')
|
|
207
|
+
spaceDown.current = false;
|
|
208
|
+
};
|
|
209
|
+
const onKeyDown = (event) => {
|
|
210
|
+
if (readOnly || (0, keyboard_js_1.isTextEntry)(event.target))
|
|
211
|
+
return;
|
|
212
|
+
if (event.key === ' ' && event.target === event.currentTarget) {
|
|
213
|
+
event.preventDefault();
|
|
214
|
+
spaceDown.current = true;
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const command = (0, keyboard_js_1.commandForKey)(event);
|
|
218
|
+
if (!command)
|
|
219
|
+
return;
|
|
220
|
+
event.preventDefault();
|
|
221
|
+
(0, keyboard_js_1.runCommand)(editor, command);
|
|
222
|
+
};
|
|
223
|
+
const selected = (0, react_1.useMemo)(() => {
|
|
224
|
+
if (readOnly || selection.length === 0)
|
|
225
|
+
return null;
|
|
226
|
+
const ids = new Set(selection);
|
|
227
|
+
const shapes = document.shapes.filter((s) => ids.has(s.id));
|
|
228
|
+
return shapes.length > 0 ? shapes : null;
|
|
229
|
+
}, [readOnly, selection, document]);
|
|
230
|
+
return ((0, jsx_runtime_1.jsxs)("div", { ref: focusable, tabIndex: 0, onKeyDown: onKeyDown, onKeyUp: onKeyUp, onBlur: () => void (spaceDown.current = false), style: { position: 'relative', width: '100%', height: '100%' }, children: [(0, jsx_runtime_1.jsx)(DrawingStage_js_1.DrawingStage, { document: document, listening: true, zoom: zoom, onWheel: onWheel, onPointerDown: onPointerDown, onAreaChange: editor.setStageArea, alt: alt, selection: readOnly ? undefined : selection, overlay: (viewport) => naming &&
|
|
231
|
+
!readOnly && ((0, jsx_runtime_1.jsx)(NameEditor_js_1.NameEditor
|
|
232
|
+
// A new editor per name editing session, so the field starts from the joint's name.
|
|
233
|
+
, { document: document, naming: naming, viewport: viewport, suggestions: config.jointNameSuggestions, onConfirm: (name) => (0, joints_js_1.confirmNaming)(editor, name, config.jointNameMaxLength), onCancel: () => (0, joints_js_1.cancelNaming)(editor) }, naming.jointId)), children: (scale) => ((0, jsx_runtime_1.jsxs)(react_konva_1.Layer, { name: "editor-ui", listening: false, children: [selected && (0, jsx_runtime_1.jsx)(SelectionOverlay, { shapes: selected, scale: scale }), !readOnly && tool === 'joint' && (0, jsx_runtime_1.jsx)(AnchorDots, { document: document, scale: scale }), snapHighlight.map((at) => ((0, jsx_runtime_1.jsx)(react_konva_1.Circle, { name: "snap-highlight", x: at.x, y: at.y, radius: (joints_js_1.ANCHOR_VISUAL_RADIUS + 4) / scale, stroke: SNAP_COLOR, strokeWidth: 2.5, strokeScaleEnabled: false }, `${at.x},${at.y}`))), selectedJoint && (0, jsx_runtime_1.jsx)(JointRing, { document: document, jointId: selectedJoint, scale: scale }), marquee && ((0, jsx_runtime_1.jsx)(react_konva_1.Rect, { name: "selection-marquee", x: marquee.left, y: marquee.top, width: marquee.width, height: marquee.height, stroke: SELECTION_COLOR, strokeWidth: 1, strokeScaleEnabled: false, dash: [4, 4], fill: "rgba(30, 136, 229, 0.08)" }))] })) }), zoom.zoom > FIT_BUTTON_MIN_ZOOM && (0, jsx_runtime_1.jsx)(ZoomControls, { zoom: zoom.zoom, onFit: editor.fitToScreen })] }));
|
|
234
|
+
}
|
|
235
|
+
/** The fit button and the percentage show while the zoom is above 1× (ZM-1). */
|
|
236
|
+
const FIT_BUTTON_MIN_ZOOM = 1.005;
|
|
237
|
+
/** Fit-to-screen button with the zoom percentage, bottom right of the drawing area (ZM-1, ZM-2). */
|
|
238
|
+
function ZoomControls({ zoom, onFit }) {
|
|
239
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: { position: 'absolute', right: 16, bottom: 16, display: 'flex', alignItems: 'center', gap: 8, font: '12px sans-serif' }, children: [(0, jsx_runtime_1.jsx)("span", { children: `${Math.round(zoom * 100)}%` }), (0, jsx_runtime_1.jsx)("button", { type: "button", onClick: onFit, children: "Fit to screen" })] }));
|
|
240
|
+
}
|
|
241
|
+
/** Anchor dots of every shape for the joint tool; filled where a joint exists (JT-1). Sizes are screen pixels. */
|
|
242
|
+
function AnchorDots({ document, scale }) {
|
|
243
|
+
const occupied = new Set(document.joints.map((j) => `${j.shapeId}/${j.anchorId}`));
|
|
244
|
+
return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: document.shapes.flatMap((shape) => (0, core_1.anchorsOf)(shape).map((anchor) => {
|
|
245
|
+
const taken = occupied.has(`${shape.id}/${anchor.id}`);
|
|
246
|
+
return ((0, jsx_runtime_1.jsx)(react_konva_1.Circle, { name: taken ? 'anchor-dot anchor-occupied' : 'anchor-dot', x: anchor.position.x, y: anchor.position.y, radius: joints_js_1.ANCHOR_VISUAL_RADIUS / scale, fill: taken ? SELECTION_COLOR : '#ffffff', stroke: SELECTION_COLOR, strokeWidth: 1.5, strokeScaleEnabled: false }, `${shape.id}/${anchor.id}`));
|
|
247
|
+
})) }));
|
|
248
|
+
}
|
|
249
|
+
/** The ring around the anchor of the selected joint (AW-7). */
|
|
250
|
+
function JointRing({ document, jointId, scale }) {
|
|
251
|
+
const joint = document.joints.find((j) => j.id === jointId);
|
|
252
|
+
const shape = joint && document.shapes.find((s) => s.id === joint.shapeId);
|
|
253
|
+
const anchor = shape && joint ? (0, core_1.anchorPositionOf)(shape, joint.anchorId) : null;
|
|
254
|
+
if (!anchor)
|
|
255
|
+
return null;
|
|
256
|
+
return ((0, jsx_runtime_1.jsx)(react_konva_1.Circle, { name: "joint-selection-ring", x: anchor.x, y: anchor.y, radius: (joints_js_1.ANCHOR_VISUAL_RADIUS + 4) / scale, stroke: SELECTION_COLOR, strokeWidth: 2, strokeScaleEnabled: false }));
|
|
257
|
+
}
|
|
258
|
+
const toDegrees = (radians) => (radians * 180) / Math.PI;
|
|
259
|
+
/**
|
|
260
|
+
* The selection outline and, for a single shape, its resize handles and rotation handle
|
|
261
|
+
* with stem (FR-7). A lone shape's outline follows its rotation; several shapes get the
|
|
262
|
+
* outline of their bounds and no handles (MS-5). Sizes are screen pixels, so they are
|
|
263
|
+
* divided by the view `scale`.
|
|
264
|
+
*/
|
|
265
|
+
function SelectionOverlay({ shapes, scale }) {
|
|
266
|
+
const single = shapes.length === 1 ? shapes[0] : null;
|
|
267
|
+
const bounds = single ? null : (0, core_1.boundsOfShapes)(shapes);
|
|
268
|
+
const center = single
|
|
269
|
+
? single.center
|
|
270
|
+
: {
|
|
271
|
+
x: bounds.left + bounds.width / 2,
|
|
272
|
+
y: bounds.top + bounds.height / 2,
|
|
273
|
+
};
|
|
274
|
+
const width = single ? single.size.width : bounds.width;
|
|
275
|
+
const height = single ? single.size.height : bounds.height;
|
|
276
|
+
const rotation = single ? toDegrees(single.rotation) : 0;
|
|
277
|
+
const stem = gestures_js_1.ROTATION_STEM_LENGTH / scale;
|
|
278
|
+
const handle = {
|
|
279
|
+
radius: gestures_js_1.HANDLE_VISUAL_RADIUS / scale,
|
|
280
|
+
fill: '#ffffff',
|
|
281
|
+
stroke: SELECTION_COLOR,
|
|
282
|
+
strokeWidth: 1.5,
|
|
283
|
+
strokeScaleEnabled: false,
|
|
284
|
+
};
|
|
285
|
+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(react_konva_1.Rect, { name: "selection-outline", x: center.x, y: center.y, offsetX: width / 2, offsetY: height / 2, rotation: rotation, width: width, height: height, stroke: SELECTION_COLOR, strokeWidth: 1.5, strokeScaleEnabled: false }), single && ((0, jsx_runtime_1.jsxs)(react_konva_1.Group, { name: "selection-handles", x: center.x, y: center.y, rotation: rotation, children: [(0, jsx_runtime_1.jsx)(react_konva_1.Line, { name: "rotation-stem", points: [0, -height / 2, 0, -height / 2 - stem], stroke: SELECTION_COLOR, strokeWidth: 1.5, strokeScaleEnabled: false }), (0, jsx_runtime_1.jsx)(react_konva_1.Circle, { name: "rotation-handle", x: 0, y: -height / 2 - stem, ...handle }), (0, core_1.handlesFor)(single.type).map((h) => {
|
|
286
|
+
const at = (0, core_1.handleLocalPosition)(h, single.size);
|
|
287
|
+
return (0, jsx_runtime_1.jsx)(react_konva_1.Circle, { name: `resize-handle handle-${h}`, x: at.x, y: at.y, ...handle }, h);
|
|
288
|
+
})] }))] }));
|
|
289
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DrawingDocument, JointStyle } from '@crux-diagram/core';
|
|
2
|
+
export interface DrawingViewerProps {
|
|
3
|
+
readonly document: DrawingDocument;
|
|
4
|
+
/** Look of leader arrows and labels; the reference style (spec 02, section 12) by default. */
|
|
5
|
+
readonly jointStyle?: JointStyle;
|
|
6
|
+
/** Alternative description for screen readers (REA-052); a summary of the drawing by default. */
|
|
7
|
+
readonly alt?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Read-only view of a drawing (REA-001), like Flutter's `DrawingView`: white canvas,
|
|
11
|
+
* shapes and joints, fitted and centered in its parent, which must have a size. It
|
|
12
|
+
* ignores gestures.
|
|
13
|
+
*/
|
|
14
|
+
export declare function DrawingViewer({ document, jointStyle, alt }: DrawingViewerProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.DrawingViewer = DrawingViewer;
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const DrawingStage_js_1 = require("./render/DrawingStage.js");
|
|
7
|
+
/**
|
|
8
|
+
* Read-only view of a drawing (REA-001), like Flutter's `DrawingView`: white canvas,
|
|
9
|
+
* shapes and joints, fitted and centered in its parent, which must have a size. It
|
|
10
|
+
* ignores gestures.
|
|
11
|
+
*/
|
|
12
|
+
function DrawingViewer({ document, jointStyle, alt }) {
|
|
13
|
+
return (0, jsx_runtime_1.jsx)(DrawingStage_js_1.DrawingStage, { document: document, jointStyle: jointStyle, alt: alt, listening: false });
|
|
14
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CSSProperties } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Joint display bar (REA-024): label size as a single choice, and show/hide. With a joint
|
|
4
|
+
* selected it adds Rename and Delete. Render it inside a `DrawingEditor`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function JointDisplayBar({ style }: {
|
|
7
|
+
style?: CSSProperties;
|
|
8
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.JointDisplayBar = JointDisplayBar;
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const core_1 = require("@crux-diagram/core");
|
|
7
|
+
const zustand_1 = require("zustand");
|
|
8
|
+
const editor_context_js_1 = require("./editor-context.js");
|
|
9
|
+
const joints_js_1 = require("./interaction/joints.js");
|
|
10
|
+
const keyboard_js_1 = require("./interaction/keyboard.js");
|
|
11
|
+
const controls_js_1 = require("./controls.js");
|
|
12
|
+
/**
|
|
13
|
+
* Joint display bar (REA-024): label size as a single choice, and show/hide. With a joint
|
|
14
|
+
* selected it adds Rename and Delete. Render it inside a `DrawingEditor`.
|
|
15
|
+
*/
|
|
16
|
+
function JointDisplayBar({ style }) {
|
|
17
|
+
const editor = (0, editor_context_js_1.useEditor)();
|
|
18
|
+
const labels = (0, editor_context_js_1.useEditorLabels)();
|
|
19
|
+
const { labelSize, visible } = (0, zustand_1.useStore)(editor, (state) => state.document.jointDisplay);
|
|
20
|
+
const selectedJoint = (0, zustand_1.useStore)(editor, (state) => state.selectedJoint);
|
|
21
|
+
return ((0, jsx_runtime_1.jsxs)("div", { role: "toolbar", "aria-label": "Joint display", style: { ...controls_js_1.controlRowStyle, ...style }, children: [core_1.JOINT_LABEL_SIZES.map((size) => ((0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels[size], pressed: labelSize === size, onClick: () => (0, joints_js_1.setJointLabelSize)(editor, size) }, size))), (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: visible ? labels.hide : labels.show, title: visible ? labels.hideJoints : labels.showJoints, pressed: !visible, onClick: () => (0, joints_js_1.setJointsVisible)(editor, !visible) }), selectedJoint && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.rename, onClick: () => (0, joints_js_1.beginRename)(editor, selectedJoint) }), (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.delete, onClick: () => (0, keyboard_js_1.runCommand)(editor, { type: 'delete' }) })] }))] }));
|
|
22
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type CanvasViewport, type DrawingDocument } from '@crux-diagram/core';
|
|
2
|
+
import type { JointNaming } from './store.js';
|
|
3
|
+
export interface NameEditorProps {
|
|
4
|
+
/** The document that shows the joint (a new joint is only in the preview). */
|
|
5
|
+
readonly document: DrawingDocument;
|
|
6
|
+
readonly naming: JointNaming;
|
|
7
|
+
readonly viewport: CanvasViewport;
|
|
8
|
+
/** Names offered as buttons (config `jointNameSuggestions`). */
|
|
9
|
+
readonly suggestions: readonly string[];
|
|
10
|
+
readonly onConfirm: (name: string) => void;
|
|
11
|
+
readonly onCancel: () => void;
|
|
12
|
+
}
|
|
13
|
+
/** Names from `suggestions` that no other joint uses; the joint being named does not count (NS-4). */
|
|
14
|
+
export declare function freeSuggestions(document: DrawingDocument, jointId: string, suggestions: readonly string[]): string[];
|
|
15
|
+
export declare function NameEditor({ document, naming, viewport, suggestions, onConfirm, onCancel }: NameEditorProps): import("react").JSX.Element | null;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.freeSuggestions = freeSuggestions;
|
|
4
|
+
exports.NameEditor = NameEditor;
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
// The joint name editor: an HTML input over the label, in the label font (REA-021, REA-031).
|
|
7
|
+
const core_1 = require("@crux-diagram/core");
|
|
8
|
+
const react_1 = require("react");
|
|
9
|
+
const scene_js_1 = require("./render/scene.js");
|
|
10
|
+
/** Names from `suggestions` that no other joint uses; the joint being named does not count (NS-4). */
|
|
11
|
+
function freeSuggestions(document, jointId, suggestions) {
|
|
12
|
+
const used = new Set(document.joints.filter((j) => j.id !== jointId).map((j) => j.name));
|
|
13
|
+
return suggestions.filter((name) => !used.has(name));
|
|
14
|
+
}
|
|
15
|
+
function NameEditor({ document, naming, viewport, suggestions, onConfirm, onCancel }) {
|
|
16
|
+
const joint = document.joints.find((j) => j.id === naming.jointId);
|
|
17
|
+
const shape = joint && document.shapes.find((s) => s.id === joint.shapeId);
|
|
18
|
+
const anchor = shape && joint ? (0, core_1.anchorPositionOf)(shape, joint.anchorId) : null;
|
|
19
|
+
const [text, setText] = (0, react_1.useState)(joint?.name ?? '');
|
|
20
|
+
if (!joint || !anchor)
|
|
21
|
+
return null;
|
|
22
|
+
const center = (0, core_1.toScreen)(viewport, { x: anchor.x + joint.labelOffset.dx, y: anchor.y + joint.labelOffset.dy });
|
|
23
|
+
const fontSize = (0, core_1.labelFontSize)(core_1.DEFAULT_JOINT_STYLE, document.jointDisplay.labelSize) * viewport.scale;
|
|
24
|
+
const free = freeSuggestions(document, joint.id, suggestions);
|
|
25
|
+
const onKeyDown = (event) => {
|
|
26
|
+
// The editor's own keys: they must not reach the editor's shortcuts.
|
|
27
|
+
event.stopPropagation();
|
|
28
|
+
if (event.key === 'Enter')
|
|
29
|
+
onConfirm(text);
|
|
30
|
+
else if (event.key === 'Escape')
|
|
31
|
+
onCancel();
|
|
32
|
+
};
|
|
33
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: { position: 'absolute', left: center.x, top: center.y, transform: 'translate(-50%, -50%)', textAlign: 'center' }, children: [(0, jsx_runtime_1.jsx)("input", { type: "text", "aria-label": "Joint name", autoFocus: true, value: text, onChange: (event) => setText(event.target.value), onFocus: (event) => event.target.select(), onBlur: () => onConfirm(text), onKeyDown: onKeyDown, style: { width: '8em', textAlign: 'center', fontFamily: `${scene_js_1.LABEL_FONT_FAMILY}, sans-serif`, fontWeight: 'bold', fontSize } }), free.length > 0 && ((0, jsx_runtime_1.jsx)("div", { style: { display: 'flex', flexWrap: 'wrap', justifyContent: 'center', gap: 4, marginTop: 4 }, children: free.map((name) => ((0, jsx_runtime_1.jsx)("button", { type: "button",
|
|
34
|
+
// Keep the focus in the input so that the click, not a blur, confirms.
|
|
35
|
+
onMouseDown: (event) => event.preventDefault(), onClick: () => onConfirm(name), children: name }, name))) }))] }));
|
|
36
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CSSProperties } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Ready-made toolbar (REA-053): the selection bar above the create bar. Render it inside a
|
|
4
|
+
* `DrawingEditor`, as a child.
|
|
5
|
+
*/
|
|
6
|
+
export declare function Toolbar({ style }: {
|
|
7
|
+
style?: CSSProperties;
|
|
8
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Toolbar = Toolbar;
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const zustand_1 = require("zustand");
|
|
7
|
+
const controls_js_1 = require("./controls.js");
|
|
8
|
+
const editor_context_js_1 = require("./editor-context.js");
|
|
9
|
+
const hooks_js_1 = require("./hooks.js");
|
|
10
|
+
const keyboard_js_1 = require("./interaction/keyboard.js");
|
|
11
|
+
const palette_js_1 = require("./interaction/palette.js");
|
|
12
|
+
const JointDisplayBar_js_1 = require("./JointDisplayBar.js");
|
|
13
|
+
/** Height of both rows, so selecting never moves a button or resizes the drawing area (TB-2). */
|
|
14
|
+
const ROW_HEIGHT = 56;
|
|
15
|
+
/**
|
|
16
|
+
* Ready-made toolbar (REA-053): the selection bar above the create bar. Render it inside a
|
|
17
|
+
* `DrawingEditor`, as a child.
|
|
18
|
+
*/
|
|
19
|
+
function Toolbar({ style }) {
|
|
20
|
+
const editor = (0, editor_context_js_1.useEditor)();
|
|
21
|
+
const config = (0, editor_context_js_1.useEditorConfig)();
|
|
22
|
+
const labels = (0, editor_context_js_1.useEditorLabels)();
|
|
23
|
+
const tool = (0, zustand_1.useStore)(editor, (state) => state.tool);
|
|
24
|
+
const showGrid = (0, zustand_1.useStore)(editor, (state) => state.view.showGrid);
|
|
25
|
+
const selectionMode = (0, zustand_1.useStore)(editor, (state) => state.selectionMode);
|
|
26
|
+
const selection = (0, zustand_1.useStore)(editor, (state) => state.selection);
|
|
27
|
+
const { canUndo, canRedo } = (0, hooks_js_1.useHistoryAvailability)(editor);
|
|
28
|
+
const document = (0, zustand_1.useStore)(editor, (state) => state.document);
|
|
29
|
+
const add = (type) => (0, palette_js_1.addPaletteShape)(editor, type, { center: (0, palette_js_1.visibleCenterOf)(editor), gridSize: config.gridSize, style: config.shapeStyle });
|
|
30
|
+
// Rotate 90° only turns a single shape that is not in a group (MS-5).
|
|
31
|
+
const single = selection.length === 1 && !document.groups.some((g) => g.shapeIds.includes(selection[0]));
|
|
32
|
+
const rowStyle = { ...controls_js_1.controlRowStyle, minHeight: ROW_HEIGHT, boxSizing: 'border-box' };
|
|
33
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: { display: 'flex', flexDirection: 'column', ...style }, children: [tool === 'joint' ? ((0, jsx_runtime_1.jsx)(JointDisplayBar_js_1.JointDisplayBar, { style: rowStyle })) : ((0, jsx_runtime_1.jsxs)("div", { role: "toolbar", "aria-label": labels.selectionBar, style: rowStyle, children: [(0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.select, title: labels.selectMode, pressed: selectionMode, onClick: () => editor.setSelectionMode(!selectionMode) }), selection.length > 0 && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, keyboard_js_1.wholeGroup)(document, selection) ? ((0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.ungroup, onClick: () => (0, keyboard_js_1.runCommand)(editor, { type: 'ungroup' }) })) : (selection.length > 1 && (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.group, onClick: () => (0, keyboard_js_1.runCommand)(editor, { type: 'group' }) })), single && (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.rotate, title: labels.rotate90, onClick: () => (0, keyboard_js_1.runCommand)(editor, { type: 'rotate90' }) }), (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.duplicate, onClick: () => (0, keyboard_js_1.runCommand)(editor, { type: 'duplicate' }) }), (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.front, title: labels.bringToFront, onClick: () => (0, keyboard_js_1.runCommand)(editor, { type: 'front' }) }), (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.back, title: labels.sendToBack, onClick: () => (0, keyboard_js_1.runCommand)(editor, { type: 'back' }) }), (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.delete, onClick: () => (0, keyboard_js_1.runCommand)(editor, { type: 'delete' }) })] }))] })), (0, jsx_runtime_1.jsxs)("div", { role: "toolbar", "aria-label": labels.createBar, style: rowStyle, children: [(0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.square, onClick: () => add('square') }), (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.rectangle, onClick: () => add('rectangle') }), (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.circle, onClick: () => add('circle') }), (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.joint, title: labels.jointTool, pressed: tool === 'joint', onClick: () => editor.setTool(tool === 'joint' ? 'select' : 'joint') }), config.gridSize !== null && ((0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.grid, title: showGrid ? labels.hideGrid : labels.showGrid, pressed: showGrid, onClick: () => editor.setShowGrid(!showGrid) })), (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.undo, disabled: !canUndo, onClick: () => editor.undo() }), (0, jsx_runtime_1.jsx)(controls_js_1.ControlButton, { label: labels.redo, disabled: !canRedo, onClick: () => editor.redo() })] })] }));
|
|
34
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { DrawingDocument } from '@crux-diagram/core';
|
|
2
|
+
import { type CSSProperties } from 'react';
|
|
3
|
+
/** One saved version (REA-071). */
|
|
4
|
+
export interface VersionEntry {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly version: number;
|
|
7
|
+
/** ISO 8601. */
|
|
8
|
+
readonly savedAt: string;
|
|
9
|
+
readonly author?: string;
|
|
10
|
+
readonly label?: string;
|
|
11
|
+
readonly document: DrawingDocument;
|
|
12
|
+
}
|
|
13
|
+
export interface VersionHistoryLabels {
|
|
14
|
+
readonly restore: string;
|
|
15
|
+
readonly current: string;
|
|
16
|
+
readonly empty: string;
|
|
17
|
+
readonly selectPrompt: string;
|
|
18
|
+
}
|
|
19
|
+
export declare const DEFAULT_VERSION_HISTORY_LABELS: VersionHistoryLabels;
|
|
20
|
+
export interface VersionHistoryProps {
|
|
21
|
+
readonly versions: readonly VersionEntry[];
|
|
22
|
+
/** Entries whose `version` equals this one are marked as current. */
|
|
23
|
+
readonly currentVersion?: number;
|
|
24
|
+
/** Called with the chosen entry; the component changes nothing else (REA-074). */
|
|
25
|
+
readonly onRestore: (entry: VersionEntry) => void;
|
|
26
|
+
readonly labels?: Partial<VersionHistoryLabels>;
|
|
27
|
+
readonly style?: CSSProperties;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Controlled version history (REA-070): lists, previews and asks to restore; it reads and
|
|
31
|
+
* stores nothing. Works on its own, outside a `DrawingEditor`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function VersionHistory({ versions, currentVersion, onRestore, labels, style }: VersionHistoryProps): import("react").JSX.Element;
|