@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,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.DEFAULT_VERSION_HISTORY_LABELS = void 0;
|
|
5
|
+
exports.VersionHistory = VersionHistory;
|
|
6
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
7
|
+
const react_1 = require("react");
|
|
8
|
+
const DrawingViewer_js_1 = require("./DrawingViewer.js");
|
|
9
|
+
const controls_js_1 = require("./controls.js");
|
|
10
|
+
exports.DEFAULT_VERSION_HISTORY_LABELS = {
|
|
11
|
+
restore: 'Restore',
|
|
12
|
+
current: 'Current',
|
|
13
|
+
empty: 'No versions yet',
|
|
14
|
+
selectPrompt: 'Select a version to preview',
|
|
15
|
+
};
|
|
16
|
+
function time(savedAt) {
|
|
17
|
+
const ms = Date.parse(savedAt);
|
|
18
|
+
return Number.isNaN(ms) ? Number.NEGATIVE_INFINITY : ms;
|
|
19
|
+
}
|
|
20
|
+
/** Newest version first; ties by `savedAt` (invalid last), then input order (REA-072). */
|
|
21
|
+
function ordered(versions) {
|
|
22
|
+
return versions
|
|
23
|
+
.map((entry, index) => ({ entry, index, at: time(entry.savedAt) }))
|
|
24
|
+
.sort((a, b) => b.entry.version - a.entry.version || (a.at === b.at ? 0 : b.at > a.at ? 1 : -1) || a.index - b.index)
|
|
25
|
+
.map((row) => row.entry);
|
|
26
|
+
}
|
|
27
|
+
/** A fixed UTC format, so server and client render the same text. */
|
|
28
|
+
function formatDate(savedAt) {
|
|
29
|
+
const ms = Date.parse(savedAt);
|
|
30
|
+
return Number.isNaN(ms) ? savedAt : `${new Date(ms).toISOString().slice(0, 16).replace('T', ' ')} UTC`;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Controlled version history (REA-070): lists, previews and asks to restore; it reads and
|
|
34
|
+
* stores nothing. Works on its own, outside a `DrawingEditor`.
|
|
35
|
+
*/
|
|
36
|
+
function VersionHistory({ versions, currentVersion, onRestore, labels, style }) {
|
|
37
|
+
const text = { ...exports.DEFAULT_VERSION_HISTORY_LABELS, ...labels };
|
|
38
|
+
const list = (0, react_1.useMemo)(() => ordered(versions), [versions]);
|
|
39
|
+
const [selectedId, setSelectedId] = (0, react_1.useState)(null);
|
|
40
|
+
const selected = list.find((entry) => entry.id === selectedId) ?? null;
|
|
41
|
+
(0, react_1.useEffect)(() => {
|
|
42
|
+
if (selectedId !== null && selected === null)
|
|
43
|
+
setSelectedId(null);
|
|
44
|
+
}, [selectedId, selected]);
|
|
45
|
+
if (list.length === 0)
|
|
46
|
+
return (0, jsx_runtime_1.jsx)("div", { style: { font: '12px sans-serif', padding: 8, ...style }, children: text.empty });
|
|
47
|
+
const isCurrent = (entry) => entry.version === currentVersion;
|
|
48
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: { display: 'flex', flexDirection: 'column', gap: 8, font: '12px sans-serif', ...style }, children: [(0, jsx_runtime_1.jsx)("ul", { style: { listStyle: 'none', margin: 0, padding: 0, maxHeight: 240, overflowY: 'auto' }, children: list.map((entry) => ((0, jsx_runtime_1.jsx)("li", { children: (0, jsx_runtime_1.jsxs)("button", { type: "button", "aria-pressed": entry.id === selected?.id, onClick: () => setSelectedId(entry.id), style: { display: 'flex', gap: 8, width: '100%', minHeight: 48, alignItems: 'center', font: 'inherit', textAlign: 'left', fontWeight: isCurrent(entry) ? 700 : 400 }, children: [(0, jsx_runtime_1.jsx)("span", { children: `v${entry.version}` }), (0, jsx_runtime_1.jsx)("time", { dateTime: entry.savedAt, children: formatDate(entry.savedAt) }), entry.author && (0, jsx_runtime_1.jsx)("span", { children: entry.author }), entry.label && (0, jsx_runtime_1.jsx)("span", { children: entry.label }), isCurrent(entry) && (0, jsx_runtime_1.jsx)("span", { children: text.current })] }) }, entry.id))) }), (0, jsx_runtime_1.jsx)("div", { style: { position: 'relative', minHeight: 200, flex: 1, border: '1px solid #ccc' }, children: selected ? (0, jsx_runtime_1.jsx)(DrawingViewer_js_1.DrawingViewer, { document: selected.document }) : (0, jsx_runtime_1.jsx)("div", { style: { padding: 8 }, children: text.selectPrompt }) }), (0, jsx_runtime_1.jsx)("div", { style: controls_js_1.controlRowStyle, children: (0, jsx_runtime_1.jsx)("button", { type: "button", disabled: selected === null || isCurrent(selected), onClick: () => selected && !isCurrent(selected) && onRestore(selected), style: { flex: '1 0 48px', minHeight: 48, font: 'inherit' }, children: text.restore }) })] }));
|
|
49
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DrawingDocument } from '@crux-diagram/core';
|
|
2
|
+
/** The default alternative description of a drawing. */
|
|
3
|
+
export declare function describeDrawing(document: DrawingDocument): string;
|
|
4
|
+
/** One phrase per shape, back to front, marking the selected ones. */
|
|
5
|
+
export declare function describeShapes(document: DrawingDocument, selection: readonly string[]): string[];
|
|
6
|
+
/** One phrase per visible joint; none while the joints are hidden (REA-023). */
|
|
7
|
+
export declare function describeJoints(document: DrawingDocument): string[];
|
|
8
|
+
/** The shape and joint list of a drawing, for screen readers. */
|
|
9
|
+
export declare function DrawingList({ document, selection }: {
|
|
10
|
+
document: DrawingDocument;
|
|
11
|
+
selection?: readonly string[];
|
|
12
|
+
}): import("react").JSX.Element;
|
package/dist/cjs/a11y.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.describeDrawing = describeDrawing;
|
|
4
|
+
exports.describeShapes = describeShapes;
|
|
5
|
+
exports.describeJoints = describeJoints;
|
|
6
|
+
exports.DrawingList = DrawingList;
|
|
7
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
+
const plural = (count, noun) => `${count} ${noun}${count === 1 ? '' : 's'}`;
|
|
9
|
+
const typeName = (shape) => shape.type.charAt(0).toUpperCase() + shape.type.slice(1);
|
|
10
|
+
/** The default alternative description of a drawing. */
|
|
11
|
+
function describeDrawing(document) {
|
|
12
|
+
return `Drawing with ${plural(document.shapes.length, 'shape')} and ${plural(document.joints.length, 'joint')}`;
|
|
13
|
+
}
|
|
14
|
+
/** One phrase per shape, back to front, marking the selected ones. */
|
|
15
|
+
function describeShapes(document, selection) {
|
|
16
|
+
const selected = new Set(selection);
|
|
17
|
+
return document.shapes.map((shape, index) => {
|
|
18
|
+
const degrees = Math.round((shape.rotation * 180) / Math.PI);
|
|
19
|
+
const parts = [
|
|
20
|
+
`${typeName(shape)} ${index + 1} of ${document.shapes.length} at ${Math.round(shape.center.x)}, ${Math.round(shape.center.y)}, ${Math.round(shape.size.width)} by ${Math.round(shape.size.height)}`,
|
|
21
|
+
];
|
|
22
|
+
if (degrees !== 0)
|
|
23
|
+
parts.push(`rotated ${degrees}°`);
|
|
24
|
+
if (selected.has(shape.id))
|
|
25
|
+
parts.push('selected');
|
|
26
|
+
return parts.join(', ');
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/** One phrase per visible joint; none while the joints are hidden (REA-023). */
|
|
30
|
+
function describeJoints(document) {
|
|
31
|
+
if (!document.jointDisplay.visible)
|
|
32
|
+
return [];
|
|
33
|
+
return document.joints.flatMap((joint) => {
|
|
34
|
+
const index = document.shapes.findIndex((s) => s.id === joint.shapeId);
|
|
35
|
+
return index < 0 ? [] : [`Joint ${joint.name} on ${typeName(document.shapes[index])} ${index + 1}`];
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/** Hidden by clipping, not `display: none`, so it stays in the accessibility tree. */
|
|
39
|
+
const visuallyHidden = {
|
|
40
|
+
position: 'absolute',
|
|
41
|
+
width: 1,
|
|
42
|
+
height: 1,
|
|
43
|
+
margin: -1,
|
|
44
|
+
padding: 0,
|
|
45
|
+
overflow: 'hidden',
|
|
46
|
+
clip: 'rect(0 0 0 0)',
|
|
47
|
+
whiteSpace: 'nowrap',
|
|
48
|
+
border: 0,
|
|
49
|
+
};
|
|
50
|
+
/** The shape and joint list of a drawing, for screen readers. */
|
|
51
|
+
function DrawingList({ document, selection = [] }) {
|
|
52
|
+
return ((0, jsx_runtime_1.jsx)("ul", { style: { ...visuallyHidden, listStyle: 'none' }, children: [...describeShapes(document, selection), ...describeJoints(document)].map((text, index) => ((0, jsx_runtime_1.jsx)("li", { children: text }, index))) }));
|
|
53
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Size } from '@crux-diagram/core';
|
|
2
|
+
/** Style given to new shapes; colors are ARGB integers (FMT-004). */
|
|
3
|
+
export interface ShapeStyle {
|
|
4
|
+
readonly fill: number;
|
|
5
|
+
readonly stroke: number;
|
|
6
|
+
readonly strokeWidth: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Editor settings, replicating Flutter's `DrawingEditorConfig` (REA-006). Angles are
|
|
10
|
+
* in radians, like the format.
|
|
11
|
+
*/
|
|
12
|
+
export interface DrawingEditorConfig {
|
|
13
|
+
/** Canvas of new documents. */
|
|
14
|
+
readonly canvasSize: Size;
|
|
15
|
+
/** Grid step in canvas units; `null` disables the grid and grid snapping. */
|
|
16
|
+
readonly gridSize: number | null;
|
|
17
|
+
/** Initial grid visibility (a view setting, never in history). */
|
|
18
|
+
readonly showGrid: boolean;
|
|
19
|
+
/** Every how many lines a major grid line is drawn. */
|
|
20
|
+
readonly gridMajorEvery: number;
|
|
21
|
+
/** Rotation snap step; 0 disables it. */
|
|
22
|
+
readonly rotationSnapStep: number;
|
|
23
|
+
/** How close to a step a rotation must be to snap. */
|
|
24
|
+
readonly rotationSnapTolerance: number;
|
|
25
|
+
/** Style of new shapes. */
|
|
26
|
+
readonly shapeStyle: ShapeStyle;
|
|
27
|
+
/** Prefix of automatic joint names. */
|
|
28
|
+
readonly jointNamePrefix: string;
|
|
29
|
+
/** Maximum joint name length, in grapheme clusters. */
|
|
30
|
+
readonly jointNameMaxLength: number;
|
|
31
|
+
/** Names offered as buttons in the joint name editor. */
|
|
32
|
+
readonly jointNameSuggestions: readonly string[];
|
|
33
|
+
/** Anchor snap distance in screen pixels; `null` disables anchor snapping. */
|
|
34
|
+
readonly anchorSnapThreshold: number | null;
|
|
35
|
+
/** Maximum number of undo entries. */
|
|
36
|
+
readonly historyLimit: number;
|
|
37
|
+
/** Most pixels an exported image may have (REA-043). */
|
|
38
|
+
readonly exportMaxPixels: number;
|
|
39
|
+
}
|
|
40
|
+
/** What a caller may pass: any field, with a partial shape style. */
|
|
41
|
+
export type DrawingEditorConfigInput = Partial<Omit<DrawingEditorConfig, 'shapeStyle'>> & {
|
|
42
|
+
readonly shapeStyle?: Partial<ShapeStyle>;
|
|
43
|
+
};
|
|
44
|
+
/** Flutter's defaults (`DrawingEditorConfig`, `ShapeElement` default style). */
|
|
45
|
+
export declare const DEFAULT_EDITOR_CONFIG: DrawingEditorConfig;
|
|
46
|
+
/**
|
|
47
|
+
* Fills the missing fields with the defaults; a field given as `undefined` counts as
|
|
48
|
+
* missing. Throws `RangeError` on an invalid history limit.
|
|
49
|
+
*/
|
|
50
|
+
export declare function resolveEditorConfig(input?: DrawingEditorConfigInput): DrawingEditorConfig;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_EDITOR_CONFIG = void 0;
|
|
4
|
+
exports.resolveEditorConfig = resolveEditorConfig;
|
|
5
|
+
/** Flutter's defaults (`DrawingEditorConfig`, `ShapeElement` default style). */
|
|
6
|
+
exports.DEFAULT_EDITOR_CONFIG = Object.freeze({
|
|
7
|
+
canvasSize: Object.freeze({ width: 1600, height: 1200 }),
|
|
8
|
+
gridSize: 10,
|
|
9
|
+
showGrid: false,
|
|
10
|
+
gridMajorEvery: 5,
|
|
11
|
+
rotationSnapStep: (15 * Math.PI) / 180,
|
|
12
|
+
rotationSnapTolerance: (5 * Math.PI) / 180,
|
|
13
|
+
shapeStyle: Object.freeze({ fill: 0x00ffffff, stroke: 0xff757575, strokeWidth: 2 }),
|
|
14
|
+
jointNamePrefix: 'J',
|
|
15
|
+
jointNameMaxLength: 12,
|
|
16
|
+
jointNameSuggestions: Object.freeze([]),
|
|
17
|
+
anchorSnapThreshold: 12,
|
|
18
|
+
historyLimit: 100,
|
|
19
|
+
exportMaxPixels: 16_000_000,
|
|
20
|
+
});
|
|
21
|
+
/** The entries of `object` whose value is not `undefined`. */
|
|
22
|
+
const defined = (object) => Object.fromEntries(Object.entries(object ?? {}).filter(([, value]) => value !== undefined));
|
|
23
|
+
/**
|
|
24
|
+
* Fills the missing fields with the defaults; a field given as `undefined` counts as
|
|
25
|
+
* missing. Throws `RangeError` on an invalid history limit.
|
|
26
|
+
*/
|
|
27
|
+
function resolveEditorConfig(input = {}) {
|
|
28
|
+
const config = {
|
|
29
|
+
...exports.DEFAULT_EDITOR_CONFIG,
|
|
30
|
+
...defined(input),
|
|
31
|
+
shapeStyle: { ...exports.DEFAULT_EDITOR_CONFIG.shapeStyle, ...defined(input.shapeStyle) },
|
|
32
|
+
};
|
|
33
|
+
if (!Number.isInteger(config.historyLimit) || config.historyLimit < 1) {
|
|
34
|
+
throw new RangeError('historyLimit must be a positive integer');
|
|
35
|
+
}
|
|
36
|
+
if (!(config.exportMaxPixels > 0))
|
|
37
|
+
throw new RangeError('exportMaxPixels must be a positive number');
|
|
38
|
+
return config;
|
|
39
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CSSProperties } from 'react';
|
|
2
|
+
export declare const controlRowStyle: CSSProperties;
|
|
3
|
+
export interface ControlButtonProps {
|
|
4
|
+
readonly label: string;
|
|
5
|
+
/** The longer text, shown as the tooltip; defaults to the label. */
|
|
6
|
+
readonly title?: string;
|
|
7
|
+
/** Marks a toggle or a choice as selected (`aria-pressed`); omit for a plain action. */
|
|
8
|
+
readonly pressed?: boolean;
|
|
9
|
+
readonly disabled?: boolean;
|
|
10
|
+
readonly onClick: () => void;
|
|
11
|
+
}
|
|
12
|
+
export declare function ControlButton({ label, title, pressed, disabled, onClick }: ControlButtonProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.controlRowStyle = void 0;
|
|
4
|
+
exports.ControlButton = ControlButton;
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
exports.controlRowStyle = {
|
|
7
|
+
display: 'flex',
|
|
8
|
+
gap: 4,
|
|
9
|
+
padding: 4,
|
|
10
|
+
overflowX: 'auto',
|
|
11
|
+
font: '12px sans-serif',
|
|
12
|
+
};
|
|
13
|
+
function ControlButton({ label, title, pressed, disabled, onClick }) {
|
|
14
|
+
return ((0, jsx_runtime_1.jsx)("button", { type: "button", title: title ?? label, "aria-pressed": pressed, disabled: disabled, onClick: onClick, style: { flex: '1 0 48px', minHeight: 48, font: 'inherit', fontWeight: pressed ? 700 : 400 }, children: label }));
|
|
15
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { DrawingDocument } from '@crux-diagram/core';
|
|
2
|
+
import { type ExportImageOptions } from './export/export-image.js';
|
|
3
|
+
import { type ReactNode } from 'react';
|
|
4
|
+
import { type DrawingEditorConfig, type DrawingEditorConfigInput } from './config.js';
|
|
5
|
+
import { type EditorLabels, type EditorLabelsInput } from './labels.js';
|
|
6
|
+
import { type EditorStore } from './store.js';
|
|
7
|
+
/** Imperative API of the editor (REA-004). Members are added as their tasks land. */
|
|
8
|
+
export interface DrawingEditorHandle {
|
|
9
|
+
undo(): void;
|
|
10
|
+
redo(): void;
|
|
11
|
+
readonly canUndo: boolean;
|
|
12
|
+
readonly canRedo: boolean;
|
|
13
|
+
/** The committed document (never a gesture preview). */
|
|
14
|
+
getDocument(): DrawingDocument;
|
|
15
|
+
/** Replaces the document and clears the history, without calling `onChange` (opening another drawing). */
|
|
16
|
+
reset(document: DrawingDocument): void;
|
|
17
|
+
/** Returns to the fitted view, 1× with no pan; not a document change (REA-016, REA-017). */
|
|
18
|
+
fitToScreen(): void;
|
|
19
|
+
/** Renders the committed document to a PNG or JPEG `Blob`, optionally saving it (REA-040 to REA-043). */
|
|
20
|
+
exportImage(options: ExportImageOptions): Promise<Blob>;
|
|
21
|
+
}
|
|
22
|
+
export interface EditorProviderProps {
|
|
23
|
+
/**
|
|
24
|
+
* The document to show. A value the editor did not produce is applied as one undoable
|
|
25
|
+
* change; a late echo of an emitted document is ignored (REA-007). To open another
|
|
26
|
+
* drawing with an empty history, change the editor's `key` or call `reset`.
|
|
27
|
+
*/
|
|
28
|
+
value: DrawingDocument;
|
|
29
|
+
/** Receives a new document on every committed change (REA-002). */
|
|
30
|
+
onChange: (document: DrawingDocument) => void;
|
|
31
|
+
/** Receives the document during a continuous gesture, before the release commits it (REA-003). */
|
|
32
|
+
onPreview?: (document: DrawingDocument) => void;
|
|
33
|
+
config?: DrawingEditorConfigInput;
|
|
34
|
+
/** Texts of the ready-made controls (REA-053); English by default. */
|
|
35
|
+
labels?: EditorLabelsInput;
|
|
36
|
+
children?: ReactNode;
|
|
37
|
+
}
|
|
38
|
+
/** Owns the editor store and binds it to the controlled `value`/`onChange` pair. */
|
|
39
|
+
export declare const EditorProvider: import("react").ForwardRefExoticComponent<EditorProviderProps & import("react").RefAttributes<DrawingEditorHandle>>;
|
|
40
|
+
/** The store of the enclosing editor. */
|
|
41
|
+
export declare const useEditor: () => EditorStore;
|
|
42
|
+
/** The resolved config of the enclosing editor. */
|
|
43
|
+
export declare const useEditorConfig: () => DrawingEditorConfig;
|
|
44
|
+
/** The texts of the enclosing editor's controls. */
|
|
45
|
+
export declare const useEditorLabels: () => EditorLabels;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useEditorLabels = exports.useEditorConfig = exports.useEditor = exports.EditorProvider = void 0;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const export_image_js_1 = require("./export/export-image.js");
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
const config_js_1 = require("./config.js");
|
|
8
|
+
const labels_js_1 = require("./labels.js");
|
|
9
|
+
const store_js_1 = require("./store.js");
|
|
10
|
+
const EditorContext = (0, react_1.createContext)(null);
|
|
11
|
+
// useLayoutEffect warns during server rendering on React 18; nothing runs there anyway.
|
|
12
|
+
const useIsomorphicLayoutEffect = typeof window === 'undefined' ? react_1.useEffect : react_1.useLayoutEffect;
|
|
13
|
+
/** Owns the editor store and binds it to the controlled `value`/`onChange` pair. */
|
|
14
|
+
exports.EditorProvider = (0, react_1.forwardRef)(function EditorProvider({ value, onChange, onPreview, config: configInput, labels: labelsInput, children }, ref) {
|
|
15
|
+
const config = (0, react_1.useMemo)(() => (0, config_js_1.resolveEditorConfig)(configInput), [configInput]);
|
|
16
|
+
const labels = (0, react_1.useMemo)(() => (0, labels_js_1.resolveEditorLabels)(labelsInput), [labelsInput]);
|
|
17
|
+
// Created once: the history limit and the initial grid visibility are read at mount.
|
|
18
|
+
const [editor] = (0, react_1.useState)(() => (0, store_js_1.createEditorStore)(value, { historyLimit: config.historyLimit, showGrid: config.showGrid }));
|
|
19
|
+
const latest = (0, react_1.useRef)({ onChange, onPreview });
|
|
20
|
+
useIsomorphicLayoutEffect(() => {
|
|
21
|
+
latest.current = { onChange, onPreview };
|
|
22
|
+
});
|
|
23
|
+
// Documents emitted since the parent last held the editor's document: a parent that
|
|
24
|
+
// adopts late may still render one of them, and that echo must not undo newer commits.
|
|
25
|
+
// Only the most recent ones are kept, so a parent that never adopts cannot grow it forever.
|
|
26
|
+
const unadopted = (0, react_1.useRef)(new Set());
|
|
27
|
+
const maxUnadopted = config.historyLimit + 1;
|
|
28
|
+
// Runs `change` without emitting: for the parent's own value and for `reset`.
|
|
29
|
+
const quiet = (0, react_1.useRef)(false);
|
|
30
|
+
const quietly = (change) => {
|
|
31
|
+
quiet.current = true;
|
|
32
|
+
try {
|
|
33
|
+
change();
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
quiet.current = false;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
// Every committed change made in the editor is emitted, including an undo back to the
|
|
40
|
+
// parent's current value; previews go to onPreview.
|
|
41
|
+
(0, react_1.useEffect)(() => editor.subscribe((state, previous) => {
|
|
42
|
+
if (state.document !== previous.document && !quiet.current) {
|
|
43
|
+
const pending = unadopted.current;
|
|
44
|
+
pending.delete(state.document);
|
|
45
|
+
pending.add(state.document);
|
|
46
|
+
for (const oldest of pending) {
|
|
47
|
+
if (pending.size <= maxUnadopted)
|
|
48
|
+
break;
|
|
49
|
+
pending.delete(oldest);
|
|
50
|
+
}
|
|
51
|
+
latest.current.onChange(state.document);
|
|
52
|
+
}
|
|
53
|
+
if (state.preview && state.preview !== previous.preview)
|
|
54
|
+
latest.current.onPreview?.(state.preview);
|
|
55
|
+
}), [editor, maxUnadopted]);
|
|
56
|
+
// REA-007: a new value never clears the history by itself.
|
|
57
|
+
(0, react_1.useEffect)(() => {
|
|
58
|
+
if (value === editor.getDocument()) {
|
|
59
|
+
unadopted.current.clear();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (unadopted.current.has(value))
|
|
63
|
+
return;
|
|
64
|
+
unadopted.current.clear();
|
|
65
|
+
quietly(() => editor.commit(value));
|
|
66
|
+
}, [editor, value]);
|
|
67
|
+
(0, react_1.useImperativeHandle)(ref, () => ({
|
|
68
|
+
undo: editor.undo,
|
|
69
|
+
redo: editor.redo,
|
|
70
|
+
get canUndo() {
|
|
71
|
+
return editor.canUndo();
|
|
72
|
+
},
|
|
73
|
+
get canRedo() {
|
|
74
|
+
return editor.canRedo();
|
|
75
|
+
},
|
|
76
|
+
getDocument: editor.getDocument,
|
|
77
|
+
fitToScreen: editor.fitToScreen,
|
|
78
|
+
exportImage: (options) => (0, export_image_js_1.exportImage)(editor.getDocument(), options, { maxPixels: config.exportMaxPixels }),
|
|
79
|
+
reset(document) {
|
|
80
|
+
quietly(() => editor.reset(document));
|
|
81
|
+
unadopted.current.clear();
|
|
82
|
+
},
|
|
83
|
+
}), [editor, config]);
|
|
84
|
+
const context = (0, react_1.useMemo)(() => ({ editor, config, labels }), [editor, config, labels]);
|
|
85
|
+
return (0, jsx_runtime_1.jsx)(EditorContext.Provider, { value: context, children: children });
|
|
86
|
+
});
|
|
87
|
+
function useEditorContext() {
|
|
88
|
+
const context = (0, react_1.useContext)(EditorContext);
|
|
89
|
+
if (!context)
|
|
90
|
+
throw new Error('This component must be rendered inside a DrawingEditor');
|
|
91
|
+
return context;
|
|
92
|
+
}
|
|
93
|
+
/** The store of the enclosing editor. */
|
|
94
|
+
const useEditor = () => useEditorContext().editor;
|
|
95
|
+
exports.useEditor = useEditor;
|
|
96
|
+
/** The resolved config of the enclosing editor. */
|
|
97
|
+
const useEditorConfig = () => useEditorContext().config;
|
|
98
|
+
exports.useEditorConfig = useEditorConfig;
|
|
99
|
+
/** The texts of the enclosing editor's controls. */
|
|
100
|
+
const useEditorLabels = () => useEditorContext().labels;
|
|
101
|
+
exports.useEditorLabels = useEditorLabels;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type DrawingDocument } from '@crux-diagram/core';
|
|
2
|
+
import Konva from 'konva';
|
|
3
|
+
/** Most pixels an exported image may have unless `config.exportMaxPixels` says otherwise (REA-043). */
|
|
4
|
+
export declare const DEFAULT_EXPORT_MAX_PIXELS = 16000000;
|
|
5
|
+
export interface ExportImageOptions {
|
|
6
|
+
readonly format: 'png' | 'jpeg';
|
|
7
|
+
/** Image pixels per canvas unit (default 1). */
|
|
8
|
+
readonly pixelRatio?: number;
|
|
9
|
+
/** JPEG quality in [0, 1] (default 0.92); ignored for PNG. */
|
|
10
|
+
readonly quality?: number;
|
|
11
|
+
/** Leaves the background transparent; PNG only (default false). */
|
|
12
|
+
readonly transparentBackground?: boolean;
|
|
13
|
+
/** A file name: the image is also saved under it, with the extension added when missing. */
|
|
14
|
+
readonly download?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The stage of the image: layers `background` (white unless transparent), `shapes` and
|
|
18
|
+
* `joints`, in canvas units. Hidden joints are not drawn (REA-042).
|
|
19
|
+
*/
|
|
20
|
+
export declare function createExportStage(document: DrawingDocument, { transparentBackground }: {
|
|
21
|
+
transparentBackground: boolean;
|
|
22
|
+
}): Konva.Stage;
|
|
23
|
+
/**
|
|
24
|
+
* Renders `document` to an image `Blob` (REA-040). Waits for the fonts first (REA-032).
|
|
25
|
+
* `maxPixels` is `config.exportMaxPixels`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function exportImage(document: DrawingDocument, options: ExportImageOptions, { maxPixels }?: {
|
|
28
|
+
maxPixels?: number;
|
|
29
|
+
}): Promise<Blob>;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Image export (REA-040 to REA-043). The image is drawn on a Konva stage of its own, with
|
|
3
|
+
// the background, shapes and joints layers only, so nothing of the editing UI can leak in.
|
|
4
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.DEFAULT_EXPORT_MAX_PIXELS = void 0;
|
|
9
|
+
exports.createExportStage = createExportStage;
|
|
10
|
+
exports.exportImage = exportImage;
|
|
11
|
+
const core_1 = require("@crux-diagram/core");
|
|
12
|
+
const konva_1 = __importDefault(require("konva"));
|
|
13
|
+
const fonts_js_1 = require("../render/fonts.js");
|
|
14
|
+
const scene_js_1 = require("../render/scene.js");
|
|
15
|
+
/** Most pixels an exported image may have unless `config.exportMaxPixels` says otherwise (REA-043). */
|
|
16
|
+
exports.DEFAULT_EXPORT_MAX_PIXELS = 16_000_000;
|
|
17
|
+
const MIME = { png: 'image/png', jpeg: 'image/jpeg' };
|
|
18
|
+
const EXTENSION = { png: '.png', jpeg: '.jpg' };
|
|
19
|
+
/** Throws `RangeError` for any option the export cannot honor, before anything is rendered (REA-043). */
|
|
20
|
+
function validate(document, options, maxPixels) {
|
|
21
|
+
const { format, pixelRatio = 1, quality = 0.92, transparentBackground = false } = options;
|
|
22
|
+
if (format !== 'png' && format !== 'jpeg')
|
|
23
|
+
throw new RangeError('format must be "png" or "jpeg"');
|
|
24
|
+
if (!Number.isFinite(pixelRatio) || pixelRatio <= 0)
|
|
25
|
+
throw new RangeError('pixelRatio must be a positive number');
|
|
26
|
+
if (format === 'jpeg' && !(quality >= 0 && quality <= 1))
|
|
27
|
+
throw new RangeError('quality must be between 0 and 1');
|
|
28
|
+
if (transparentBackground && format !== 'png')
|
|
29
|
+
throw new RangeError('transparentBackground needs the png format');
|
|
30
|
+
const pixels = document.canvas.width * document.canvas.height * pixelRatio * pixelRatio;
|
|
31
|
+
if (pixels > maxPixels)
|
|
32
|
+
throw new RangeError(`the image would have ${Math.round(pixels)} pixels, above the limit of ${maxPixels}`);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The stage of the image: layers `background` (white unless transparent), `shapes` and
|
|
36
|
+
* `joints`, in canvas units. Hidden joints are not drawn (REA-042).
|
|
37
|
+
*/
|
|
38
|
+
function createExportStage(document, { transparentBackground }) {
|
|
39
|
+
const { width, height } = document.canvas;
|
|
40
|
+
const stage = new konva_1.default.Stage({ container: globalThis.document.createElement('div'), width, height });
|
|
41
|
+
const background = new konva_1.default.Layer({ name: 'background' });
|
|
42
|
+
if (!transparentBackground)
|
|
43
|
+
background.add(new konva_1.default.Rect({ name: 'background-fill', width, height, fill: '#ffffff' }));
|
|
44
|
+
const shapes = new konva_1.default.Layer({ name: 'shapes' });
|
|
45
|
+
for (const shape of document.shapes) {
|
|
46
|
+
const { kind, ...props } = (0, scene_js_1.shapeNodeProps)(shape);
|
|
47
|
+
shapes.add(kind === 'ellipse' ? new konva_1.default.Ellipse({ id: shape.id, ...props }) : new konva_1.default.Rect({ id: shape.id, ...props }));
|
|
48
|
+
}
|
|
49
|
+
const joints = new konva_1.default.Layer({ name: 'joints' });
|
|
50
|
+
const style = core_1.DEFAULT_JOINT_STYLE;
|
|
51
|
+
const color = (0, core_1.argbToCss)(style.color);
|
|
52
|
+
const measurer = new core_1.CanvasTextMeasurer({ fontFamily: scene_js_1.LABEL_FONT_FAMILY });
|
|
53
|
+
for (const placement of (0, scene_js_1.placeJoints)(document, style, measurer)) {
|
|
54
|
+
const { line, head, text } = (0, scene_js_1.jointGraphics)(placement, style, document.jointDisplay.labelSize);
|
|
55
|
+
if (line)
|
|
56
|
+
joints.add(new konva_1.default.Line({ name: 'joint-line', points: line, stroke: color, strokeWidth: style.lineWidth, lineCap: 'butt' }));
|
|
57
|
+
if (head)
|
|
58
|
+
joints.add(new konva_1.default.Line({ name: 'joint-head', points: head, closed: true, fill: color }));
|
|
59
|
+
joints.add(new konva_1.default.Text({ id: placement.joint.id, name: 'joint-label', ...text, align: 'center', verticalAlign: 'middle', wrap: 'none' }));
|
|
60
|
+
}
|
|
61
|
+
stage.add(background, shapes, joints);
|
|
62
|
+
return stage;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Renders `document` to an image `Blob` (REA-040). Waits for the fonts first (REA-032).
|
|
66
|
+
* `maxPixels` is `config.exportMaxPixels`.
|
|
67
|
+
*/
|
|
68
|
+
async function exportImage(document, options, { maxPixels = exports.DEFAULT_EXPORT_MAX_PIXELS } = {}) {
|
|
69
|
+
validate(document, options, maxPixels);
|
|
70
|
+
const { format, pixelRatio = 1, quality = 0.92, transparentBackground = false, download } = options;
|
|
71
|
+
await (0, fonts_js_1.whenFontsReady)();
|
|
72
|
+
const stage = createExportStage(document, { transparentBackground });
|
|
73
|
+
let blob;
|
|
74
|
+
try {
|
|
75
|
+
blob = await stage.toBlob({ pixelRatio, mimeType: MIME[format], ...(format === 'jpeg' ? { quality } : {}) });
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
stage.destroy();
|
|
79
|
+
}
|
|
80
|
+
if (!blob)
|
|
81
|
+
throw new Error('the browser could not encode the image');
|
|
82
|
+
if (download !== undefined)
|
|
83
|
+
save(blob, /\.[a-z0-9]+$/i.test(download) ? download : download + EXTENSION[format]);
|
|
84
|
+
return blob;
|
|
85
|
+
}
|
|
86
|
+
/** Saves `blob` as a file through a temporary link. */
|
|
87
|
+
function save(blob, name) {
|
|
88
|
+
const url = URL.createObjectURL(blob);
|
|
89
|
+
const link = globalThis.document.createElement('a');
|
|
90
|
+
link.href = url;
|
|
91
|
+
link.download = name;
|
|
92
|
+
link.click();
|
|
93
|
+
URL.revokeObjectURL(url);
|
|
94
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { EditorStore } from './store.js';
|
|
2
|
+
/** Whether there is something to undo or redo, kept up to date as the history changes. */
|
|
3
|
+
export declare function useHistoryAvailability(editor: EditorStore): {
|
|
4
|
+
readonly canUndo: boolean;
|
|
5
|
+
readonly canRedo: boolean;
|
|
6
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useHistoryAvailability = useHistoryAvailability;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
/** Whether there is something to undo or redo, kept up to date as the history changes. */
|
|
6
|
+
function useHistoryAvailability(editor) {
|
|
7
|
+
const canUndo = (0, react_1.useSyncExternalStore)(editor.subscribeHistory, editor.canUndo, editor.canUndo);
|
|
8
|
+
const canRedo = (0, react_1.useSyncExternalStore)(editor.subscribeHistory, editor.canRedo, editor.canRedo);
|
|
9
|
+
return { canUndo, canRedo };
|
|
10
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { DrawingEditor, type DrawingEditorProps } from './DrawingEditor.js';
|
|
2
|
+
export { DrawingViewer, type DrawingViewerProps } from './DrawingViewer.js';
|
|
3
|
+
export { Toolbar } from './Toolbar.js';
|
|
4
|
+
export { JointDisplayBar } from './JointDisplayBar.js';
|
|
5
|
+
export { DEFAULT_EDITOR_LABELS, type EditorLabels, type EditorLabelsInput } from './labels.js';
|
|
6
|
+
export type { DrawingEditorHandle } from './editor-context.js';
|
|
7
|
+
export type { ExportImageOptions } from './export/export-image.js';
|
|
8
|
+
export { DEFAULT_EDITOR_CONFIG, resolveEditorConfig, type DrawingEditorConfig, type DrawingEditorConfigInput, type ShapeStyle, } from './config.js';
|
|
9
|
+
export { VersionHistory, DEFAULT_VERSION_HISTORY_LABELS, type VersionEntry, type VersionHistoryLabels, type VersionHistoryProps, } from './VersionHistory.js';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_VERSION_HISTORY_LABELS = exports.VersionHistory = exports.resolveEditorConfig = exports.DEFAULT_EDITOR_CONFIG = exports.DEFAULT_EDITOR_LABELS = exports.JointDisplayBar = exports.Toolbar = exports.DrawingViewer = exports.DrawingEditor = void 0;
|
|
4
|
+
// Public API of @crux-diagram/react (spec 05). Nothing here may touch `window` at
|
|
5
|
+
// import time (REA-054).
|
|
6
|
+
var DrawingEditor_js_1 = require("./DrawingEditor.js");
|
|
7
|
+
Object.defineProperty(exports, "DrawingEditor", { enumerable: true, get: function () { return DrawingEditor_js_1.DrawingEditor; } });
|
|
8
|
+
var DrawingViewer_js_1 = require("./DrawingViewer.js");
|
|
9
|
+
Object.defineProperty(exports, "DrawingViewer", { enumerable: true, get: function () { return DrawingViewer_js_1.DrawingViewer; } });
|
|
10
|
+
var Toolbar_js_1 = require("./Toolbar.js");
|
|
11
|
+
Object.defineProperty(exports, "Toolbar", { enumerable: true, get: function () { return Toolbar_js_1.Toolbar; } });
|
|
12
|
+
var JointDisplayBar_js_1 = require("./JointDisplayBar.js");
|
|
13
|
+
Object.defineProperty(exports, "JointDisplayBar", { enumerable: true, get: function () { return JointDisplayBar_js_1.JointDisplayBar; } });
|
|
14
|
+
var labels_js_1 = require("./labels.js");
|
|
15
|
+
Object.defineProperty(exports, "DEFAULT_EDITOR_LABELS", { enumerable: true, get: function () { return labels_js_1.DEFAULT_EDITOR_LABELS; } });
|
|
16
|
+
var config_js_1 = require("./config.js");
|
|
17
|
+
Object.defineProperty(exports, "DEFAULT_EDITOR_CONFIG", { enumerable: true, get: function () { return config_js_1.DEFAULT_EDITOR_CONFIG; } });
|
|
18
|
+
Object.defineProperty(exports, "resolveEditorConfig", { enumerable: true, get: function () { return config_js_1.resolveEditorConfig; } });
|
|
19
|
+
var VersionHistory_js_1 = require("./VersionHistory.js");
|
|
20
|
+
Object.defineProperty(exports, "VersionHistory", { enumerable: true, get: function () { return VersionHistory_js_1.VersionHistory; } });
|
|
21
|
+
Object.defineProperty(exports, "DEFAULT_VERSION_HISTORY_LABELS", { enumerable: true, get: function () { return VersionHistory_js_1.DEFAULT_VERSION_HISTORY_LABELS; } });
|