@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,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 {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Pure scene geometry for the Konva layers, shared by the viewer and the editor.
|
|
2
|
+
// Port of Flutter's shape_painting.dart and joint_painting.dart (REA-001, REA-023).
|
|
3
|
+
import { anchorPositionOf, argbToCss, arrowheadPoints, arrowheadSize, labelFontSize, leaderLineStart, toDegrees, } from '@crux-diagram/core';
|
|
4
|
+
/** Font family of joint labels, fixed in both implementations (REA-030, spec 02 section 12). */
|
|
5
|
+
export const LABEL_FONT_FAMILY = 'Arial';
|
|
6
|
+
/** Konva props for `shape`. Paint with no alpha or no width is disabled, like Flutter. */
|
|
7
|
+
export function shapeNodeProps(shape) {
|
|
8
|
+
const { width, height } = shape.size;
|
|
9
|
+
const base = {
|
|
10
|
+
x: shape.center.x,
|
|
11
|
+
y: shape.center.y,
|
|
12
|
+
width,
|
|
13
|
+
height,
|
|
14
|
+
rotation: toDegrees(shape.rotation),
|
|
15
|
+
fill: argbToCss(shape.fill),
|
|
16
|
+
stroke: argbToCss(shape.stroke),
|
|
17
|
+
strokeWidth: shape.strokeWidth,
|
|
18
|
+
fillEnabled: shape.fill >>> 24 > 0,
|
|
19
|
+
strokeEnabled: shape.strokeWidth > 0,
|
|
20
|
+
};
|
|
21
|
+
if (shape.type === 'circle')
|
|
22
|
+
return { ...base, kind: 'ellipse', radiusX: width / 2, radiusY: height / 2 };
|
|
23
|
+
return { ...base, kind: 'rect', offsetX: width / 2, offsetY: height / 2 };
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Places every joint whose shape and anchor exist, measuring names at the document's
|
|
27
|
+
* label size. Hidden joints are not placed, so they are neither drawn nor exported.
|
|
28
|
+
*/
|
|
29
|
+
export function placeJoints(document, style, measurer) {
|
|
30
|
+
if (!document.jointDisplay.visible)
|
|
31
|
+
return [];
|
|
32
|
+
const fontSize = labelFontSize(style, document.jointDisplay.labelSize);
|
|
33
|
+
const shapes = new Map(document.shapes.map((shape) => [shape.id, shape]));
|
|
34
|
+
const placements = [];
|
|
35
|
+
for (const joint of document.joints) {
|
|
36
|
+
const shape = shapes.get(joint.shapeId);
|
|
37
|
+
const anchor = shape ? anchorPositionOf(shape, joint.anchorId) : null;
|
|
38
|
+
if (!anchor)
|
|
39
|
+
continue;
|
|
40
|
+
const textSize = measurer.measure(joint.name, fontSize, style.labelBold);
|
|
41
|
+
const width = textSize.width + 2 * style.labelPadding;
|
|
42
|
+
const height = textSize.height + 2 * style.labelPadding;
|
|
43
|
+
placements.push({
|
|
44
|
+
joint,
|
|
45
|
+
anchor,
|
|
46
|
+
textSize,
|
|
47
|
+
labelRect: {
|
|
48
|
+
left: anchor.x + joint.labelOffset.dx - width / 2,
|
|
49
|
+
top: anchor.y + joint.labelOffset.dy - height / 2,
|
|
50
|
+
width,
|
|
51
|
+
height,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return placements;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Leader arrow and label of `placement` (FMT-043, FMT-044). No arrow when the anchor is
|
|
59
|
+
* inside the label; no line when it would not reach past the arrowhead.
|
|
60
|
+
*/
|
|
61
|
+
export function jointGraphics(placement, style, labelSize) {
|
|
62
|
+
const { anchor, labelRect, textSize } = placement;
|
|
63
|
+
const text = {
|
|
64
|
+
text: placement.joint.name,
|
|
65
|
+
x: labelRect.left + style.labelPadding,
|
|
66
|
+
y: labelRect.top + style.labelPadding,
|
|
67
|
+
width: textSize.width,
|
|
68
|
+
height: textSize.height,
|
|
69
|
+
fontSize: labelFontSize(style, labelSize),
|
|
70
|
+
fontFamily: LABEL_FONT_FAMILY,
|
|
71
|
+
fontStyle: style.labelBold ? 'bold' : 'normal',
|
|
72
|
+
fill: argbToCss(style.color),
|
|
73
|
+
};
|
|
74
|
+
const start = leaderLineStart(labelRect, anchor);
|
|
75
|
+
if (!start || (start.x === anchor.x && start.y === anchor.y))
|
|
76
|
+
return { line: null, head: null, text };
|
|
77
|
+
const size = arrowheadSize(style, labelSize);
|
|
78
|
+
const [tip, left, right] = arrowheadPoints({ from: start, tip: anchor, length: size.length, width: size.width });
|
|
79
|
+
const base = { x: (left.x + right.x) / 2, y: (left.y + right.y) / 2 };
|
|
80
|
+
const length = Math.hypot(anchor.x - start.x, anchor.y - start.y);
|
|
81
|
+
return {
|
|
82
|
+
line: length > size.length ? [start.x, start.y, base.x, base.y] : null,
|
|
83
|
+
head: [tip.x, tip.y, left.x, left.y, right.x, right.y],
|
|
84
|
+
text,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type DrawingDocument, type Point, type Size, type ViewportZoom } from '@crux-diagram/core';
|
|
2
|
+
/** View settings: never in history and never a document change (REA-017). */
|
|
3
|
+
export interface EditorView {
|
|
4
|
+
readonly showGrid: boolean;
|
|
5
|
+
/** Zoom and pan on top of the fitted view; the stage clamps it to the canvas (REA-016). */
|
|
6
|
+
readonly zoom: ViewportZoom;
|
|
7
|
+
}
|
|
8
|
+
/** The active tool: `select` moves and edits shapes, `joint` places and selects joints (REA-020). */
|
|
9
|
+
export type EditorTool = 'select' | 'joint';
|
|
10
|
+
/** The open joint name editor: for a joint being created (still a preview) or an existing one (REA-021). */
|
|
11
|
+
export interface JointNaming {
|
|
12
|
+
readonly jointId: string;
|
|
13
|
+
readonly isNew: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface EditorState {
|
|
16
|
+
/** The committed document, the one `onChange` receives. */
|
|
17
|
+
readonly document: DrawingDocument;
|
|
18
|
+
/** The document shown during a continuous gesture, or `null` (REA-003). */
|
|
19
|
+
readonly preview: DrawingDocument | null;
|
|
20
|
+
readonly view: EditorView;
|
|
21
|
+
/**
|
|
22
|
+
* Ids of the selected shapes in document z-order, always with whole groups (MS-6).
|
|
23
|
+
* Never in history: selecting adds no entry (REA-018).
|
|
24
|
+
*/
|
|
25
|
+
readonly selection: readonly string[];
|
|
26
|
+
/** Whether taps toggle shapes and empty-canvas drags draw a selection rectangle (MS-1, MS-3). */
|
|
27
|
+
readonly selectionMode: boolean;
|
|
28
|
+
readonly tool: EditorTool;
|
|
29
|
+
/** The selected joint, never together with selected shapes. Not in history. */
|
|
30
|
+
readonly selectedJoint: string | null;
|
|
31
|
+
readonly naming: JointNaming | null;
|
|
32
|
+
/** Anchors highlighted while an anchor snap is active during a gesture (SN-3). Not in history. */
|
|
33
|
+
readonly snapHighlight: readonly Point[];
|
|
34
|
+
/** The measured size of the drawing area in screen pixels, or `null` before the first render; not in history. */
|
|
35
|
+
readonly stageArea: Size | null;
|
|
36
|
+
}
|
|
37
|
+
export interface EditorStoreOptions {
|
|
38
|
+
/** Maximum number of undo entries (default 100). */
|
|
39
|
+
readonly historyLimit?: number;
|
|
40
|
+
/** Initial grid visibility (default false). */
|
|
41
|
+
readonly showGrid?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The editor's state with a snapshot history (spec 05 section 4). Only the committed
|
|
45
|
+
* document is recorded: a commit is one entry, previews and view changes are none.
|
|
46
|
+
*/
|
|
47
|
+
export declare function createEditorStore(initial: DrawingDocument, options?: EditorStoreOptions): {
|
|
48
|
+
getState: () => EditorState;
|
|
49
|
+
getInitialState: () => EditorState;
|
|
50
|
+
subscribe: (listener: (state: EditorState, prevState: EditorState) => void) => () => void;
|
|
51
|
+
/** The committed document. */
|
|
52
|
+
getDocument: () => DrawingDocument;
|
|
53
|
+
/** Calls `listener` whenever the undo or redo stacks change (for `canUndo`/`canRedo` in the UI). */
|
|
54
|
+
subscribeHistory: (listener: () => void) => () => void;
|
|
55
|
+
canUndo: () => boolean;
|
|
56
|
+
canRedo: () => boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Commits `document` as one entry and ends any gesture; the same instance adds none.
|
|
59
|
+
* With `selection`, the selection becomes those shapes (and their groups) in the same update.
|
|
60
|
+
*/
|
|
61
|
+
commit(document: DrawingDocument, selection?: readonly string[]): void;
|
|
62
|
+
/** Shows `document` during a gesture without committing it; `null` cancels the gesture. */
|
|
63
|
+
preview(document: DrawingDocument | null, snapHighlight?: readonly Point[]): void;
|
|
64
|
+
undo(): void;
|
|
65
|
+
redo(): void;
|
|
66
|
+
/** Replaces the document and clears the history, adding no entry. */
|
|
67
|
+
reset(document: DrawingDocument): void;
|
|
68
|
+
/** Selects the shape with `id` and the rest of its group, or clears the selection with `null` (Flutter `select`). */
|
|
69
|
+
select(id: string | null): void;
|
|
70
|
+
/** Adds the shape with `id` and its group to the selection, or removes them when it is selected (MS-1, MS-6). */
|
|
71
|
+
toggleSelected(id: string): void;
|
|
72
|
+
/** Adds the shapes with `ids`, and their groups, to the selection (MS-3). */
|
|
73
|
+
selectAll(ids: readonly string[]): void;
|
|
74
|
+
/** Switches the tool: a view change, never a history entry. An open name editor closes. */
|
|
75
|
+
setTool(tool: EditorTool): void;
|
|
76
|
+
/** Selects the joint with `id` (and no shapes), or clears the joint selection with `null`. */
|
|
77
|
+
selectJoint(id: string | null): void;
|
|
78
|
+
/** Opens (or with `null` closes) the joint name editor. */
|
|
79
|
+
setNaming(naming: JointNaming | null): void;
|
|
80
|
+
/** Enters or leaves selection mode; leaving it keeps the selection (MS-8). */
|
|
81
|
+
setSelectionMode(selectionMode: boolean): void;
|
|
82
|
+
/** Sets zoom and pan: a view change, never a history entry (REA-017). */
|
|
83
|
+
setZoom(zoom: ViewportZoom): void;
|
|
84
|
+
/** Returns to the fitted view: 1× with no pan (REA-004). */
|
|
85
|
+
fitToScreen(): void;
|
|
86
|
+
/** Records the size of the drawing area, to place new shapes at the visible center (PL-1). */
|
|
87
|
+
setStageArea(stageArea: Size): void;
|
|
88
|
+
setShowGrid(showGrid: boolean): void;
|
|
89
|
+
};
|
|
90
|
+
export type EditorStore = ReturnType<typeof createEditorStore>;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { FITTED_ZOOM, withGroupMates } from '@crux-diagram/core';
|
|
2
|
+
import { temporal } from 'zundo';
|
|
3
|
+
import { createStore } from 'zustand/vanilla';
|
|
4
|
+
import { DEFAULT_EDITOR_CONFIG } from './config.js';
|
|
5
|
+
/**
|
|
6
|
+
* The editor's state with a snapshot history (spec 05 section 4). Only the committed
|
|
7
|
+
* document is recorded: a commit is one entry, previews and view changes are none.
|
|
8
|
+
*/
|
|
9
|
+
export function createEditorStore(initial, options = {}) {
|
|
10
|
+
const store = createStore()(temporal(() => ({
|
|
11
|
+
document: initial,
|
|
12
|
+
preview: null,
|
|
13
|
+
view: { showGrid: options.showGrid ?? DEFAULT_EDITOR_CONFIG.showGrid, zoom: FITTED_ZOOM },
|
|
14
|
+
selection: [],
|
|
15
|
+
selectionMode: false,
|
|
16
|
+
tool: 'select',
|
|
17
|
+
selectedJoint: null,
|
|
18
|
+
naming: null,
|
|
19
|
+
snapHighlight: [],
|
|
20
|
+
stageArea: null,
|
|
21
|
+
}), {
|
|
22
|
+
limit: options.historyLimit ?? DEFAULT_EDITOR_CONFIG.historyLimit,
|
|
23
|
+
partialize: (state) => ({ document: state.document }),
|
|
24
|
+
// A set that keeps the same document instance (a preview or a view change) is not an entry.
|
|
25
|
+
equality: (past, current) => past.document === current.document,
|
|
26
|
+
}));
|
|
27
|
+
const history = () => store.temporal.getState();
|
|
28
|
+
const dropPreview = () => {
|
|
29
|
+
if (store.getState().preview)
|
|
30
|
+
store.setState({ preview: null, snapHighlight: [] });
|
|
31
|
+
};
|
|
32
|
+
const sameIds = (a, b) => a.length === b.length && a.every((id, i) => id === b[i]);
|
|
33
|
+
const setSelection = (selection) => {
|
|
34
|
+
if (!sameIds(selection, store.getState().selection)) {
|
|
35
|
+
store.setState({ selection, ...(selection.length > 0 ? { selectedJoint: null } : {}) });
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
// Flutter `_dropStaleSelection`: after the document changes, keep only the shapes that
|
|
39
|
+
// still exist, with their current groups.
|
|
40
|
+
const dropStaleSelection = () => {
|
|
41
|
+
const { document, selection } = store.getState();
|
|
42
|
+
const existing = new Set(document.shapes.map((s) => s.id));
|
|
43
|
+
setSelection(withGroupMates(document, selection.filter((id) => existing.has(id))));
|
|
44
|
+
// A joint, or an open name editor, whose joint is gone (a new joint lives only in the preview).
|
|
45
|
+
const { preview, selectedJoint, naming } = store.getState();
|
|
46
|
+
const joints = new Set((preview ?? document).joints.map((j) => j.id));
|
|
47
|
+
if (selectedJoint && !joints.has(selectedJoint))
|
|
48
|
+
store.setState({ selectedJoint: null });
|
|
49
|
+
if (naming && !joints.has(naming.jointId))
|
|
50
|
+
store.setState({ naming: null });
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
getState: store.getState,
|
|
54
|
+
getInitialState: store.getInitialState,
|
|
55
|
+
subscribe: store.subscribe,
|
|
56
|
+
/** The committed document. */
|
|
57
|
+
getDocument: () => store.getState().document,
|
|
58
|
+
/** Calls `listener` whenever the undo or redo stacks change (for `canUndo`/`canRedo` in the UI). */
|
|
59
|
+
subscribeHistory: (listener) => store.temporal.subscribe(listener),
|
|
60
|
+
canUndo: () => history().pastStates.length > 0,
|
|
61
|
+
canRedo: () => history().futureStates.length > 0,
|
|
62
|
+
/**
|
|
63
|
+
* Commits `document` as one entry and ends any gesture; the same instance adds none.
|
|
64
|
+
* With `selection`, the selection becomes those shapes (and their groups) in the same update.
|
|
65
|
+
*/
|
|
66
|
+
commit(document, selection) {
|
|
67
|
+
// Resolve the selection first: an unknown id throws before anything is recorded.
|
|
68
|
+
const next = selection ? withGroupMates(document, selection) : null;
|
|
69
|
+
store.setState({ document, preview: null, snapHighlight: [] });
|
|
70
|
+
if (next)
|
|
71
|
+
setSelection(next);
|
|
72
|
+
else
|
|
73
|
+
dropStaleSelection();
|
|
74
|
+
},
|
|
75
|
+
/** Shows `document` during a gesture without committing it; `null` cancels the gesture. */
|
|
76
|
+
preview(document, snapHighlight = []) {
|
|
77
|
+
store.setState({ preview: document, snapHighlight: document ? snapHighlight : [] });
|
|
78
|
+
},
|
|
79
|
+
undo() {
|
|
80
|
+
dropPreview();
|
|
81
|
+
history().undo();
|
|
82
|
+
dropStaleSelection();
|
|
83
|
+
},
|
|
84
|
+
redo() {
|
|
85
|
+
dropPreview();
|
|
86
|
+
history().redo();
|
|
87
|
+
dropStaleSelection();
|
|
88
|
+
},
|
|
89
|
+
/** Replaces the document and clears the history, adding no entry. */
|
|
90
|
+
reset(document) {
|
|
91
|
+
store.setState({ document, preview: null });
|
|
92
|
+
history().clear();
|
|
93
|
+
dropStaleSelection();
|
|
94
|
+
},
|
|
95
|
+
/** Selects the shape with `id` and the rest of its group, or clears the selection with `null` (Flutter `select`). */
|
|
96
|
+
select(id) {
|
|
97
|
+
setSelection(id === null ? [] : withGroupMates(store.getState().document, [id]));
|
|
98
|
+
},
|
|
99
|
+
/** Adds the shape with `id` and its group to the selection, or removes them when it is selected (MS-1, MS-6). */
|
|
100
|
+
toggleSelected(id) {
|
|
101
|
+
const { document, selection } = store.getState();
|
|
102
|
+
const members = new Set(withGroupMates(document, [id]));
|
|
103
|
+
const next = selection.includes(id)
|
|
104
|
+
? selection.filter((s) => !members.has(s))
|
|
105
|
+
: withGroupMates(document, [...selection, ...members]);
|
|
106
|
+
setSelection(next);
|
|
107
|
+
},
|
|
108
|
+
/** Adds the shapes with `ids`, and their groups, to the selection (MS-3). */
|
|
109
|
+
selectAll(ids) {
|
|
110
|
+
const { document, selection } = store.getState();
|
|
111
|
+
setSelection(withGroupMates(document, [...selection, ...ids]));
|
|
112
|
+
},
|
|
113
|
+
/** Switches the tool: a view change, never a history entry. An open name editor closes. */
|
|
114
|
+
setTool(tool) {
|
|
115
|
+
if (store.getState().tool !== tool)
|
|
116
|
+
store.setState({ tool, naming: null });
|
|
117
|
+
},
|
|
118
|
+
/** Selects the joint with `id` (and no shapes), or clears the joint selection with `null`. */
|
|
119
|
+
selectJoint(id) {
|
|
120
|
+
const state = store.getState();
|
|
121
|
+
if (id === null) {
|
|
122
|
+
if (state.selectedJoint !== null)
|
|
123
|
+
store.setState({ selectedJoint: null });
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
store.setState({ selectedJoint: id, selection: [] });
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
/** Opens (or with `null` closes) the joint name editor. */
|
|
130
|
+
setNaming(naming) {
|
|
131
|
+
store.setState({ naming });
|
|
132
|
+
},
|
|
133
|
+
/** Enters or leaves selection mode; leaving it keeps the selection (MS-8). */
|
|
134
|
+
setSelectionMode(selectionMode) {
|
|
135
|
+
if (store.getState().selectionMode !== selectionMode)
|
|
136
|
+
store.setState({ selectionMode });
|
|
137
|
+
},
|
|
138
|
+
/** Sets zoom and pan: a view change, never a history entry (REA-017). */
|
|
139
|
+
setZoom(zoom) {
|
|
140
|
+
store.setState({ view: { ...store.getState().view, zoom } });
|
|
141
|
+
},
|
|
142
|
+
/** Returns to the fitted view: 1× with no pan (REA-004). */
|
|
143
|
+
fitToScreen() {
|
|
144
|
+
store.setState({ view: { ...store.getState().view, zoom: FITTED_ZOOM } });
|
|
145
|
+
},
|
|
146
|
+
/** Records the size of the drawing area, to place new shapes at the visible center (PL-1). */
|
|
147
|
+
setStageArea(stageArea) {
|
|
148
|
+
const current = store.getState().stageArea;
|
|
149
|
+
if (!current || current.width !== stageArea.width || current.height !== stageArea.height)
|
|
150
|
+
store.setState({ stageArea });
|
|
151
|
+
},
|
|
152
|
+
setShowGrid(showGrid) {
|
|
153
|
+
store.setState({ view: { ...store.getState().view, showGrid } });
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,82 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crux-diagram/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React viewer and editor for crux_diagram drawings, with joints, version history and image export.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"diagram",
|
|
7
|
+
"drawing",
|
|
8
|
+
"editor",
|
|
9
|
+
"react",
|
|
10
|
+
"konva"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/DanM10/crux_diagram.git",
|
|
16
|
+
"directory": "js/react"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/DanM10/crux_diagram/tree/main/js/react#readme",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/DanM10/crux_diagram/issues"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"main": "./dist/cjs/index.js",
|
|
25
|
+
"module": "./dist/esm/index.js",
|
|
26
|
+
"types": "./dist/esm/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": {
|
|
30
|
+
"types": "./dist/esm/index.d.ts",
|
|
31
|
+
"default": "./dist/esm/index.js"
|
|
32
|
+
},
|
|
33
|
+
"require": {
|
|
34
|
+
"types": "./dist/cjs/index.d.ts",
|
|
35
|
+
"default": "./dist/cjs/index.js"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"CHANGELOG.md"
|
|
42
|
+
],
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=22"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "node scripts/build.mjs",
|
|
48
|
+
"typecheck": "tsc --noEmit",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"e2e": "playwright test",
|
|
51
|
+
"storybook": "storybook dev -p 6006",
|
|
52
|
+
"build-storybook": "storybook build -o storybook-static",
|
|
53
|
+
"prepack": "npm run build"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@crux-diagram/core": "^0.1.0",
|
|
57
|
+
"zundo": "^2.3.0",
|
|
58
|
+
"zustand": "^5.1.0"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"konva": "^10.0.0",
|
|
62
|
+
"react": "^19.3.0",
|
|
63
|
+
"react-dom": "^19.3.0",
|
|
64
|
+
"react-konva": "^19.3.0"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@playwright/test": "^1.65.1",
|
|
68
|
+
"@storybook/react-vite": "^10.6.7",
|
|
69
|
+
"@testing-library/react": "^16.3.3",
|
|
70
|
+
"@types/react-dom": "^19.3.0",
|
|
71
|
+
"jsdom": "^31.0.1",
|
|
72
|
+
"konva": "^10.9.1",
|
|
73
|
+
"react": "^19.3.1",
|
|
74
|
+
"react-dom": "^19.3.1",
|
|
75
|
+
"react-konva": "^19.3.2",
|
|
76
|
+
"storybook": "^10.6.7",
|
|
77
|
+
"vite": "^8.3.8"
|
|
78
|
+
},
|
|
79
|
+
"publishConfig": {
|
|
80
|
+
"access": "public"
|
|
81
|
+
}
|
|
82
|
+
}
|