@widgetic/canvas 0.5.45 → 0.5.46
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/README.md +1 -1
- package/dist/canvas/autosaveScheduler.js +37 -89
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +7 -19
- package/dist/canvas/chromeLayout.js +19 -21
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.d.ts +6 -2
- package/dist/canvas/host/CanvasHost.js +1615 -1847
- package/dist/canvas/host/CanvasHostApi.js +4 -19
- package/dist/canvas/host/boardFiles.js +35 -51
- package/dist/canvas/host/browserServices.js +108 -196
- package/dist/canvas/host/conversion.js +82 -170
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/nearCards.js +18 -21
- package/dist/canvas/host/pageEvents.js +5 -5
- package/dist/canvas/host/pictureSaves.js +9 -11
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/storedPictures.js +149 -243
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/index.d.ts +1 -1
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +6 -7
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +13 -25
- package/dist/canvas/shapes/ShapeLibrary.js +138 -148
- package/dist/canvas/shapes/shapeImageStore.js +4 -4
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/types.d.ts +7 -1
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.js +21 -21
- package/dist/canvas/ui/floatingLayer.js +5 -6
- package/dist/canvas/ui/keyHints.js +9 -11
- package/dist/canvas/ui/popoverSide.js +2 -3
- package/dist/canvas/ui/selectionTraits.js +35 -41
- package/dist/canvas/view/CanvasStage.svelte +1 -1
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/canvas/view/liftedScene.js +24 -36
- package/dist/canvas/view/mountedScene.js +76 -79
- package/dist/components/CanvasBoardPlaceholder.svelte +68 -10
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +10 -2
- package/dist/components/CanvasToolbarPlaceholder.svelte +25 -11
- package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +6 -0
- package/dist/components/boardLoaderDelay.d.ts +39 -0
- package/dist/components/boardLoaderDelay.js +71 -0
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +9 -2
- package/dist/engine/EngineCore.js +798 -907
- package/dist/engine/codec/canvasDocument.js +192 -155
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +26 -38
- package/dist/engine/core/arrow.js +79 -92
- package/dist/engine/core/arrowBinding.js +98 -116
- package/dist/engine/core/arrowBindingRecords.js +23 -38
- package/dist/engine/core/brush.js +84 -96
- package/dist/engine/core/cameraMove.js +39 -51
- package/dist/engine/core/defaults.js +92 -51
- package/dist/engine/core/freehand.js +147 -163
- package/dist/engine/core/geometry.js +47 -54
- package/dist/engine/core/history.js +38 -55
- package/dist/engine/core/hitTest.js +68 -81
- package/dist/engine/core/matrix.js +18 -20
- package/dist/engine/core/pathData.js +183 -210
- package/dist/engine/core/rotationReset.js +5 -6
- package/dist/engine/core/scene.js +160 -224
- package/dist/engine/core/scribble.js +59 -84
- package/dist/engine/core/shapePaths.js +12 -15
- package/dist/engine/core/shapeTools.js +72 -63
- package/dist/engine/core/snapping.js +65 -117
- package/dist/engine/core/style.js +32 -65
- package/dist/engine/core/transform.js +84 -95
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +23 -36
- package/dist/engine/input/PointerController.js +513 -526
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.js +267 -310
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +35 -39
- package/dist/engine/input/selectionHandles.js +45 -50
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +86 -158
- package/dist/engine/state.js +6 -17
- package/dist/engine/text/layoutText.js +43 -62
- package/dist/engine/text/textMetrics.js +8 -10
- package/dist/engine/widgets.js +101 -137
- package/dist/fonts/interFont.js +30 -83
- package/dist/fonts/workspaceFonts.js +33 -92
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.js +1 -1
- package/dist/i18n/shortcuts.js +14 -14
- package/dist/index.d.ts +1 -1
- package/dist/poster/boardPlacement.js +28 -30
- package/dist/poster/boardPoster.js +58 -83
- package/dist/poster/paintBoardPoster.js +16 -39
- package/dist/poster/posterPictures.js +27 -43
- package/dist/settings/rememberedTool.d.ts +50 -0
- package/dist/settings/rememberedTool.js +68 -0
- package/dist/settings/workspaceToolSettings.d.ts +8 -0
- package/dist/settings/workspaceToolSettings.js +35 -48
- package/dist/tools/components/catalog.js +64 -74
- package/dist/tools/components/grouping.js +6 -28
- package/dist/tools/definitions.js +5 -5
- package/dist/tools/executors.js +137 -169
- package/dist/tools/hostAdapter.js +11 -13
- package/dist/tools/sceneSummary.js +43 -57
- package/dist/tools/sketchIntent.js +13 -21
- package/dist/tools/styleArguments.js +19 -21
- package/dist/tools/types.js +1 -1
- package/package.json +2 -2
package/dist/tools/executors.js
CHANGED
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
13
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
14
|
-
if (ar || !(i in from)) {
|
|
15
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
16
|
-
ar[i] = from[i];
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
20
|
-
};
|
|
21
1
|
import { createEllipseObject, createImageObject, createPathObject, createRectObject, createTextObject } from '../engine/core/defaults.js';
|
|
22
2
|
import { readImageAddress, readLibraryFileAddress } from '../engine/codec/canvasDocument.js';
|
|
23
3
|
import { buildArrow } from '../engine/core/arrow.js';
|
|
@@ -28,22 +8,20 @@ import { drawUiComponent, makeArrowBetween } from './components/catalog.js';
|
|
|
28
8
|
import { centerOf, findByLabel, kindOfObject, labelOfObject, summarizeScene } from './sceneSummary.js';
|
|
29
9
|
import { assetSizeFor, readColorArgument, readFontFamilyArgument, readNumberArgument } from './styleArguments.js';
|
|
30
10
|
function groupLabelForName(groupId, scene) {
|
|
31
|
-
for (
|
|
32
|
-
var object = scene_1[_i];
|
|
11
|
+
for (const object of scene) {
|
|
33
12
|
if (object.parentId === groupId && object.type === 'text')
|
|
34
13
|
return object.text.trim();
|
|
35
14
|
}
|
|
36
15
|
return 'group';
|
|
37
16
|
}
|
|
38
17
|
function fail(code, error) {
|
|
39
|
-
return { ok: false, error
|
|
18
|
+
return { ok: false, error, code };
|
|
40
19
|
}
|
|
41
|
-
function ok(message, extra) {
|
|
42
|
-
|
|
43
|
-
return __assign({ ok: true, message: message }, extra);
|
|
20
|
+
function ok(message, extra = {}) {
|
|
21
|
+
return { ok: true, message, ...extra };
|
|
44
22
|
}
|
|
45
23
|
function clampInBounds(host, x, y, width, height) {
|
|
46
|
-
|
|
24
|
+
const pad = 8;
|
|
47
25
|
return (x >= -pad &&
|
|
48
26
|
y >= -pad &&
|
|
49
27
|
x + width <= host.bounds.width + pad &&
|
|
@@ -52,216 +30,215 @@ function clampInBounds(host, x, y, width, height) {
|
|
|
52
30
|
height > 0);
|
|
53
31
|
}
|
|
54
32
|
function addToScene(host, objects) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return { scene
|
|
33
|
+
const withIds = objects.map((object) => ({ ...object, id: object.id ?? host.createId(kindOfObject(object)) }));
|
|
34
|
+
const scene = normalizeScene([...host.getScene(), ...withIds]);
|
|
35
|
+
return { scene, ids: withIds.map((object) => object.id) };
|
|
58
36
|
}
|
|
59
|
-
|
|
60
|
-
get_canvas
|
|
61
|
-
|
|
62
|
-
return ok(
|
|
37
|
+
const executors = {
|
|
38
|
+
get_canvas(host) {
|
|
39
|
+
const summary = summarizeScene(host.getScene(), host.bounds);
|
|
40
|
+
return ok(`Canvas has ${summary.elements.length} elements.`, { summary });
|
|
63
41
|
},
|
|
64
|
-
draw_ui_component
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
var label = typeof args.label === 'string' ? args.label : undefined;
|
|
42
|
+
draw_ui_component(host, args) {
|
|
43
|
+
const component = String(args.component ?? '');
|
|
44
|
+
const x = Number(args.x);
|
|
45
|
+
const y = Number(args.y);
|
|
46
|
+
const width = Number(args.width ?? 360);
|
|
47
|
+
const height = Number(args.height ?? 240);
|
|
48
|
+
const label = typeof args.label === 'string' ? args.label : undefined;
|
|
72
49
|
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
73
50
|
return fail('invalid_args', 'x and y are required numbers.');
|
|
74
|
-
|
|
75
|
-
|
|
51
|
+
const placement = drawUiComponent(component, x, y, width, height, label);
|
|
52
|
+
const { scene, ids } = addToScene(host, placement.objects);
|
|
76
53
|
// The check is on what the component really covers (a button is 48 px high whatever height was asked for, a video
|
|
77
54
|
// player has its title above y and a navigation strip below the thumbnail), not on the size that was passed: a
|
|
78
55
|
// request with no size would otherwise be judged as 360 x 240, and a button near the bottom would be refused.
|
|
79
|
-
|
|
56
|
+
const covered = boundsOfObjects(scene, ids);
|
|
80
57
|
if (!covered || !clampInBounds(host, covered.x, covered.y, covered.width, covered.height)) {
|
|
81
|
-
|
|
82
|
-
?
|
|
58
|
+
const area = covered
|
|
59
|
+
? ` It would cover x ${Math.round(covered.x)} to ${Math.round(covered.x + covered.width)} and y ${Math.round(covered.y)} to ${Math.round(covered.y + covered.height)}, and the canvas is ${Math.round(host.bounds.width)} x ${Math.round(host.bounds.height)}.`
|
|
83
60
|
: '';
|
|
84
|
-
return fail('bounds',
|
|
61
|
+
return fail('bounds', `Component would sit outside the canvas.${area}`);
|
|
85
62
|
}
|
|
86
63
|
host.commitScene(scene);
|
|
87
|
-
return ok(
|
|
64
|
+
return ok(`Drew ${component} (${placement.rootLabel}).`, { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
88
65
|
},
|
|
89
|
-
add_shape
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
var height = Number(args.height);
|
|
66
|
+
add_shape(host, args) {
|
|
67
|
+
const shape = String(args.shape ?? 'rectangle');
|
|
68
|
+
const x = Number(args.x);
|
|
69
|
+
const y = Number(args.y);
|
|
70
|
+
const width = Number(args.width);
|
|
71
|
+
const height = Number(args.height);
|
|
96
72
|
if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(width) || !Number.isFinite(height)) {
|
|
97
73
|
return fail('invalid_args', 'x, y, width and height are required.');
|
|
98
74
|
}
|
|
99
75
|
if (!clampInBounds(host, x, y, width, height))
|
|
100
76
|
return fail('bounds', 'Shape is outside the canvas.');
|
|
101
77
|
// The brand colours the assistant was told about: a bad colour is refused so the model can say it again.
|
|
102
|
-
|
|
78
|
+
const fill = readColorArgument(args.fill, 'fill', true);
|
|
103
79
|
if (!fill.ok)
|
|
104
80
|
return fail('invalid_args', fill.error);
|
|
105
|
-
|
|
81
|
+
const stroke = readColorArgument(args.stroke, 'stroke');
|
|
106
82
|
if (!stroke.ok)
|
|
107
83
|
return fail('invalid_args', stroke.error);
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
84
|
+
const paint = { ...(fill.value !== undefined ? { fill: fill.value } : {}), ...(stroke.value !== undefined ? { stroke: stroke.value } : {}) };
|
|
85
|
+
let object;
|
|
86
|
+
const label = typeof args.label === 'string' ? args.label : undefined;
|
|
111
87
|
if (shape === 'ellipse') {
|
|
112
|
-
object = createEllipseObject(
|
|
88
|
+
object = createEllipseObject({ x, y, width, height, ...paint });
|
|
113
89
|
}
|
|
114
90
|
else if (shape === 'diamond') {
|
|
115
|
-
|
|
116
|
-
object = createPathObject(
|
|
91
|
+
const path = `M ${width / 2} 0 L ${width} ${height / 2} L ${width / 2} ${height} L 0 ${height / 2} Z`;
|
|
92
|
+
object = createPathObject({ x, y, width, height, d: path, shape: 'diamond', ...paint });
|
|
117
93
|
}
|
|
118
94
|
else {
|
|
119
|
-
object = createRectObject(
|
|
95
|
+
object = createRectObject({ x, y, width, height, roundness: shape === 'rounded' ? 16 : 0, ...paint });
|
|
120
96
|
}
|
|
121
|
-
|
|
97
|
+
const objects = [object];
|
|
122
98
|
if (label) {
|
|
123
99
|
objects.push(createTextObject({ x: x + 4, y: y + 4, text: label, fontSize: 12, autoWidth: true }));
|
|
124
100
|
}
|
|
125
|
-
|
|
101
|
+
const { scene, ids } = addToScene(host, objects);
|
|
126
102
|
host.commitScene(scene);
|
|
127
|
-
return ok(
|
|
103
|
+
return ok(`Added ${shape}.`, { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
128
104
|
},
|
|
129
|
-
add_text
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
var y = Number(args.y);
|
|
105
|
+
add_text(host, args) {
|
|
106
|
+
const text = String(args.text ?? '').trim();
|
|
107
|
+
const x = Number(args.x);
|
|
108
|
+
const y = Number(args.y);
|
|
134
109
|
if (!text)
|
|
135
110
|
return fail('invalid_args', 'text is required.');
|
|
136
111
|
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
137
112
|
return fail('invalid_args', 'x and y are required.');
|
|
138
|
-
|
|
113
|
+
const width = Number(args.width ?? 200);
|
|
139
114
|
// The look: the brand fonts and colours the assistant was told about. Anything the canvas does not have is refused.
|
|
140
|
-
|
|
115
|
+
const fontFamily = readFontFamilyArgument(args.fontFamily, host.fontFamilies);
|
|
141
116
|
if (!fontFamily.ok)
|
|
142
117
|
return fail('invalid_args', fontFamily.error);
|
|
143
|
-
|
|
118
|
+
const color = readColorArgument(args.color, 'color');
|
|
144
119
|
if (!color.ok)
|
|
145
120
|
return fail('invalid_args', color.error);
|
|
146
|
-
|
|
121
|
+
const fontSize = readNumberArgument(args.fontSize, 'fontSize', 8, 200);
|
|
147
122
|
if (!fontSize.ok)
|
|
148
123
|
return fail('invalid_args', fontSize.error);
|
|
149
124
|
if (args.fontWeight !== undefined && args.fontWeight !== 'normal' && args.fontWeight !== 'bold') {
|
|
150
125
|
return fail('invalid_args', 'fontWeight must be "normal" or "bold".');
|
|
151
126
|
}
|
|
152
|
-
|
|
153
|
-
|
|
127
|
+
const look = {
|
|
128
|
+
...(fontFamily.value !== undefined ? { fontFamily: fontFamily.value } : {}),
|
|
129
|
+
...(color.value !== undefined ? { color: color.value } : {}),
|
|
130
|
+
...(fontSize.value !== undefined ? { fontSize: fontSize.value, height: Math.ceil(fontSize.value * 1.2) } : {}),
|
|
131
|
+
...(args.fontWeight === 'bold' ? { fontWeight: 'bold' } : {})
|
|
132
|
+
};
|
|
133
|
+
const object = createTextObject({ x, y, width, text, autoWidth: width <= 40, ...look });
|
|
154
134
|
if (!clampInBounds(host, x, y, object.width, object.height))
|
|
155
135
|
return fail('bounds', 'Text is outside the canvas.');
|
|
156
|
-
|
|
136
|
+
const { scene, ids } = addToScene(host, [object]);
|
|
157
137
|
host.commitScene(scene);
|
|
158
138
|
return ok('Added text.', { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
159
139
|
},
|
|
160
|
-
add_asset
|
|
161
|
-
|
|
162
|
-
var assetId = typeof args.assetId === 'string' ? args.assetId.trim() : '';
|
|
140
|
+
add_asset(host, args) {
|
|
141
|
+
const assetId = typeof args.assetId === 'string' ? args.assetId.trim() : '';
|
|
163
142
|
if (!assetId)
|
|
164
143
|
return fail('invalid_args', 'assetId is required.');
|
|
165
|
-
|
|
144
|
+
const assets = host.assets ?? [];
|
|
166
145
|
if (assets.length === 0) {
|
|
167
146
|
return fail('not_found', 'There are no assets to place: this canvas has no brand-kit icons or library files for the assistant. Draw with shapes and text instead.');
|
|
168
147
|
}
|
|
169
|
-
|
|
148
|
+
const asset = assets.find((candidate) => candidate.id === assetId);
|
|
170
149
|
if (!asset) {
|
|
171
|
-
|
|
172
|
-
return fail('not_found',
|
|
150
|
+
const listed = assets.slice(0, 24).map((candidate) => candidate.id).join(', ');
|
|
151
|
+
return fail('not_found', `Unknown assetId "${assetId}". Use one of: ${listed}${assets.length > 24 ? ', …' : ''}.`);
|
|
173
152
|
}
|
|
174
|
-
|
|
175
|
-
|
|
153
|
+
const x = Number(args.x);
|
|
154
|
+
const y = Number(args.y);
|
|
176
155
|
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
177
156
|
return fail('invalid_args', 'x and y are required.');
|
|
178
|
-
|
|
157
|
+
const width = readNumberArgument(args.width, 'width', 8, 2000);
|
|
179
158
|
if (!width.ok)
|
|
180
159
|
return fail('invalid_args', width.error);
|
|
181
|
-
|
|
160
|
+
const height = readNumberArgument(args.height, 'height', 8, 2000);
|
|
182
161
|
if (!height.ok)
|
|
183
162
|
return fail('invalid_args', height.error);
|
|
184
163
|
// The page built the asset list, but its pictures are drawn into the board like any other: only a loadable address goes in.
|
|
185
|
-
|
|
164
|
+
const src = readImageAddress(asset.src);
|
|
186
165
|
if (!src)
|
|
187
|
-
return fail('invalid_args',
|
|
188
|
-
|
|
166
|
+
return fail('invalid_args', `Asset "${asset.id}" has no picture the canvas can draw.`);
|
|
167
|
+
const size = assetSizeFor(asset, width.value, height.value);
|
|
189
168
|
if (!clampInBounds(host, x, y, size.width, size.height))
|
|
190
169
|
return fail('bounds', 'Asset would sit outside the canvas.');
|
|
191
|
-
|
|
192
|
-
x
|
|
193
|
-
y
|
|
170
|
+
const object = createImageObject({
|
|
171
|
+
x,
|
|
172
|
+
y,
|
|
194
173
|
width: size.width,
|
|
195
174
|
height: size.height,
|
|
196
|
-
src
|
|
175
|
+
src,
|
|
197
176
|
fileName: asset.name.slice(0, 300),
|
|
198
177
|
// A library file keeps the library's id, so the canvas never puts it in the library a second time.
|
|
199
|
-
libraryFileId:
|
|
178
|
+
libraryFileId: asset.libraryFileId ?? null,
|
|
200
179
|
libraryFileUrl: readLibraryFileAddress(asset.libraryFileUrl),
|
|
201
180
|
naturalWidth: asset.width && asset.width > 0 ? asset.width : size.width,
|
|
202
181
|
naturalHeight: asset.height && asset.height > 0 ? asset.height : size.height
|
|
203
182
|
});
|
|
204
|
-
|
|
183
|
+
const { scene, ids } = addToScene(host, [object]);
|
|
205
184
|
host.commitScene(scene);
|
|
206
|
-
return ok(
|
|
185
|
+
return ok(`Placed ${asset.name}.`, { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
207
186
|
},
|
|
208
|
-
add_arrow
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
187
|
+
add_arrow(host, args) {
|
|
188
|
+
let fromX = Number(args.fromX);
|
|
189
|
+
let fromY = Number(args.fromY);
|
|
190
|
+
let toX = Number(args.toX);
|
|
191
|
+
let toY = Number(args.toY);
|
|
192
|
+
const scene = host.getScene();
|
|
214
193
|
if (typeof args.fromId === 'string') {
|
|
215
|
-
|
|
194
|
+
const center = centerOf(args.fromId, scene);
|
|
216
195
|
if (!center)
|
|
217
|
-
return fail('not_found',
|
|
196
|
+
return fail('not_found', `fromId ${args.fromId} not found.`);
|
|
218
197
|
fromX = center.x;
|
|
219
198
|
fromY = center.y;
|
|
220
199
|
}
|
|
221
200
|
if (typeof args.toId === 'string') {
|
|
222
|
-
|
|
201
|
+
const center = centerOf(args.toId, scene);
|
|
223
202
|
if (!center)
|
|
224
|
-
return fail('not_found',
|
|
203
|
+
return fail('not_found', `toId ${args.toId} not found.`);
|
|
225
204
|
toX = center.x;
|
|
226
205
|
toY = center.y;
|
|
227
206
|
}
|
|
228
207
|
if (![fromX, fromY, toX, toY].every(Number.isFinite))
|
|
229
208
|
return fail('invalid_args', 'Need from/to points or ids.');
|
|
230
|
-
|
|
209
|
+
const objects = [makeArrowBetween(fromX, fromY, toX, toY)];
|
|
231
210
|
if (typeof args.label === 'string' && args.label.trim()) {
|
|
232
211
|
objects.push(createTextObject({ x: (fromX + toX) / 2, y: (fromY + toY) / 2 - 16, text: args.label.trim(), fontSize: 12, autoWidth: true }));
|
|
233
212
|
}
|
|
234
|
-
|
|
213
|
+
const added = addToScene(host, objects);
|
|
235
214
|
host.commitScene(added.scene);
|
|
236
215
|
return ok('Added arrow.', { createdIds: added.ids, summary: summarizeScene(added.scene, host.bounds) });
|
|
237
216
|
},
|
|
238
|
-
move_elements
|
|
239
|
-
|
|
240
|
-
var ids = Array.isArray(args.ids) ? args.ids.map(String) : [];
|
|
217
|
+
move_elements(host, args) {
|
|
218
|
+
const ids = Array.isArray(args.ids) ? args.ids.map(String) : [];
|
|
241
219
|
if (ids.length === 0)
|
|
242
220
|
return fail('invalid_args', 'ids required.');
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
for (
|
|
246
|
-
var id = ids_1[_i];
|
|
221
|
+
let scene = host.getScene();
|
|
222
|
+
const index = indexScene(scene);
|
|
223
|
+
for (const id of ids) {
|
|
247
224
|
if (!index.has(id))
|
|
248
|
-
return fail('not_found',
|
|
225
|
+
return fail('not_found', `Element ${id} not found.`);
|
|
249
226
|
}
|
|
250
|
-
|
|
251
|
-
|
|
227
|
+
const dx = Number(args.dx ?? 0);
|
|
228
|
+
const dy = Number(args.dy ?? 0);
|
|
252
229
|
if ((Number.isFinite(args.x) || Number.isFinite(args.y)) && ids.length > 0) {
|
|
253
|
-
|
|
230
|
+
const first = findObject(scene, ids[0]);
|
|
254
231
|
if (!first)
|
|
255
232
|
return fail('not_found', 'First id not movable.');
|
|
256
|
-
|
|
233
|
+
const bbox = first.type === 'group'
|
|
257
234
|
? boundsOfObjects(scene, [first.id])
|
|
258
235
|
: isBoxedObject(first)
|
|
259
236
|
? shapeBounds(first)
|
|
260
237
|
: null;
|
|
261
238
|
if (!bbox)
|
|
262
239
|
return fail('not_found', 'First id not movable.');
|
|
263
|
-
|
|
264
|
-
|
|
240
|
+
const targetDx = Number.isFinite(args.x) ? Number(args.x) - bbox.x : 0;
|
|
241
|
+
const targetDy = Number.isFinite(args.y) ? Number(args.y) - bbox.y : 0;
|
|
265
242
|
scene = translateObjects(scene, ids, targetDx, targetDy);
|
|
266
243
|
}
|
|
267
244
|
else if (dx !== 0 || dy !== 0) {
|
|
@@ -271,36 +248,35 @@ var executors = {
|
|
|
271
248
|
return fail('invalid_args', 'Provide dx/dy or x/y.');
|
|
272
249
|
}
|
|
273
250
|
host.commitScene(scene);
|
|
274
|
-
return ok(
|
|
251
|
+
return ok(`Moved ${ids.length} element(s).`, { affectedIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
275
252
|
},
|
|
276
|
-
delete_elements
|
|
277
|
-
|
|
253
|
+
delete_elements(host, args) {
|
|
254
|
+
const ids = Array.isArray(args.ids) ? args.ids.map(String) : [];
|
|
278
255
|
if (ids.length === 0)
|
|
279
256
|
return fail('invalid_args', 'ids required.');
|
|
280
|
-
|
|
257
|
+
let scene = host.getScene();
|
|
281
258
|
if (ids.length === 1 && ids[0] === 'all') {
|
|
282
259
|
scene = [];
|
|
283
260
|
}
|
|
284
261
|
else {
|
|
285
|
-
|
|
262
|
+
const missing = ids.filter((id) => !findObject(scene, id));
|
|
286
263
|
if (missing.length > 0)
|
|
287
|
-
return fail('not_found',
|
|
264
|
+
return fail('not_found', `Unknown ids: ${missing.join(', ')}`);
|
|
288
265
|
scene = removeObjects(scene, ids);
|
|
289
266
|
}
|
|
290
267
|
host.commitScene(scene);
|
|
291
268
|
return ok('Deleted elements.', { affectedIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
292
269
|
},
|
|
293
|
-
clear_canvas
|
|
294
|
-
|
|
270
|
+
clear_canvas(host) {
|
|
271
|
+
const scene = [];
|
|
295
272
|
host.commitScene(scene);
|
|
296
273
|
return ok('Canvas cleared.', { summary: summarizeScene(scene, host.bounds) });
|
|
297
274
|
},
|
|
298
|
-
name_components_with_arrows
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
for (
|
|
303
|
-
var object = sceneBefore_1[_i];
|
|
275
|
+
name_components_with_arrows(host, args) {
|
|
276
|
+
const prefix = typeof args.prefix === 'string' ? args.prefix.trim() : '';
|
|
277
|
+
const sceneBefore = host.getScene();
|
|
278
|
+
const additions = [];
|
|
279
|
+
for (const object of sceneBefore) {
|
|
304
280
|
if (object.parentId !== null)
|
|
305
281
|
continue;
|
|
306
282
|
if (object.type === 'text')
|
|
@@ -309,19 +285,19 @@ var executors = {
|
|
|
309
285
|
continue;
|
|
310
286
|
if (!isBoxedObject(object) && object.type !== 'group')
|
|
311
287
|
continue;
|
|
312
|
-
|
|
313
|
-
|
|
288
|
+
const name = `${prefix ? `${prefix} ` : ''}${object.type === 'group' ? groupLabelForName(object.id, sceneBefore) : labelOfObject(object)}`.trim();
|
|
289
|
+
const bbox = object.type === 'group'
|
|
314
290
|
? boundsOfObjects(sceneBefore, [object.id])
|
|
315
291
|
: isBoxedObject(object)
|
|
316
292
|
? shapeBounds(object)
|
|
317
293
|
: null;
|
|
318
294
|
if (!bbox)
|
|
319
295
|
continue;
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
296
|
+
const labelX = bbox.x - 8;
|
|
297
|
+
const labelY = bbox.y - 28;
|
|
298
|
+
const label = createTextObject({ x: labelX, y: labelY, text: name, fontSize: 12, autoWidth: true });
|
|
323
299
|
additions.push(label);
|
|
324
|
-
|
|
300
|
+
const geometry = buildArrow({ x: labelX + 40, y: labelY + 10 }, { x: bbox.x + bbox.width / 2, y: bbox.y + bbox.height / 2 }, 0, 2);
|
|
325
301
|
additions.push(createPathObject({
|
|
326
302
|
x: geometry.box.x,
|
|
327
303
|
y: geometry.box.y,
|
|
@@ -335,15 +311,15 @@ var executors = {
|
|
|
335
311
|
fill: 'transparent'
|
|
336
312
|
}));
|
|
337
313
|
}
|
|
338
|
-
|
|
314
|
+
const { scene, ids } = addToScene(host, additions);
|
|
339
315
|
host.commitScene(scene);
|
|
340
|
-
return ok(
|
|
316
|
+
return ok(`Named ${additions.length / 2} components.`, { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
341
317
|
}
|
|
342
318
|
};
|
|
343
319
|
export function executeCanvasTool(host, name, args) {
|
|
344
|
-
|
|
320
|
+
const run = executors[name];
|
|
345
321
|
if (!run)
|
|
346
|
-
return fail('invalid_args',
|
|
322
|
+
return fail('invalid_args', `Unknown tool ${name}.`);
|
|
347
323
|
try {
|
|
348
324
|
return run(host, args);
|
|
349
325
|
}
|
|
@@ -353,42 +329,34 @@ export function executeCanvasTool(host, name, args) {
|
|
|
353
329
|
}
|
|
354
330
|
/** Navigation wireframe group (label "Navigation"). */
|
|
355
331
|
export function findNavigationId(scene) {
|
|
356
|
-
for (
|
|
357
|
-
var object = scene_2[_i];
|
|
332
|
+
for (const object of scene) {
|
|
358
333
|
if (object.type === 'text' && object.text.trim().toLowerCase() === 'navigation' && object.parentId) {
|
|
359
334
|
return object.parentId;
|
|
360
335
|
}
|
|
361
336
|
}
|
|
362
|
-
|
|
337
|
+
const direct = findByLabel(scene, 'navigation');
|
|
363
338
|
if (direct) {
|
|
364
|
-
|
|
365
|
-
if (
|
|
339
|
+
const object = findObject(scene, direct);
|
|
340
|
+
if (object?.type === 'group')
|
|
366
341
|
return direct;
|
|
367
|
-
if (object
|
|
342
|
+
if (object?.parentId)
|
|
368
343
|
return object.parentId;
|
|
369
344
|
}
|
|
370
345
|
return findByLabel(scene, 'nav');
|
|
371
346
|
}
|
|
372
347
|
export function findVideoThumbnailBounds(scene) {
|
|
373
|
-
|
|
348
|
+
for (const object of scene) {
|
|
374
349
|
if (object.type === 'text' && object.text.toLowerCase() === 'thumbnail') {
|
|
375
|
-
|
|
350
|
+
const frame = scene.find((candidate) => candidate.parentId === object.parentId || candidate.type === 'rect');
|
|
376
351
|
if (frame && isBoxedObject(frame))
|
|
377
|
-
return
|
|
352
|
+
return shapeBounds(frame);
|
|
378
353
|
}
|
|
379
|
-
};
|
|
380
|
-
for (var _i = 0, scene_3 = scene; _i < scene_3.length; _i++) {
|
|
381
|
-
var object = scene_3[_i];
|
|
382
|
-
var state_1 = _loop_1(object);
|
|
383
|
-
if (typeof state_1 === "object")
|
|
384
|
-
return state_1.value;
|
|
385
354
|
}
|
|
386
|
-
|
|
355
|
+
const screen = scene.find((object) => object.type === 'rect' && object.fill === '#f3f4f6' && object.parentId !== null);
|
|
387
356
|
return screen && isBoxedObject(screen) ? shapeBounds(screen) : null;
|
|
388
357
|
}
|
|
389
358
|
export function findVideoPlayerRoot(scene) {
|
|
390
|
-
for (
|
|
391
|
-
var object = scene_4[_i];
|
|
359
|
+
for (const object of scene) {
|
|
392
360
|
if (object.type === 'text' && object.text.toLowerCase().includes('video player'))
|
|
393
361
|
return object.id;
|
|
394
362
|
}
|
|
@@ -3,36 +3,34 @@ import { createObjectId } from '../engine/core/defaults.js';
|
|
|
3
3
|
* Adapts EngineCore to CanvasToolHost: one `commit()` per assistant turn.
|
|
4
4
|
*/
|
|
5
5
|
export function createCanvasToolHostFromEngine(options) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var bounds = (_a = options.bounds) !== null && _a !== void 0 ? _a : {
|
|
6
|
+
const { engine } = options;
|
|
7
|
+
const bounds = options.bounds ?? {
|
|
9
8
|
width: engine.state.viewSize.width || 1200,
|
|
10
9
|
height: engine.state.viewSize.height || 800
|
|
11
10
|
};
|
|
12
11
|
return {
|
|
13
|
-
bounds
|
|
12
|
+
bounds,
|
|
14
13
|
assets: options.assets,
|
|
15
14
|
fontFamilies: options.fontFamilies,
|
|
16
|
-
getScene
|
|
15
|
+
getScene() {
|
|
17
16
|
return engine.state.scene;
|
|
18
17
|
},
|
|
19
|
-
createId
|
|
20
|
-
return createObjectId(prefix
|
|
18
|
+
createId(prefix) {
|
|
19
|
+
return createObjectId(prefix ?? 'sketch');
|
|
21
20
|
},
|
|
22
|
-
commitScene
|
|
23
|
-
|
|
24
|
-
engine.applyScene(next, (_a = options.commitMode) !== null && _a !== void 0 ? _a : 'push');
|
|
21
|
+
commitScene(next) {
|
|
22
|
+
engine.applyScene(next, options.commitMode ?? 'push');
|
|
25
23
|
}
|
|
26
24
|
};
|
|
27
25
|
}
|
|
28
26
|
/** Run several tool calls as a single undo step on the engine. */
|
|
29
27
|
export function runCanvasToolBatch(host, run, engine) {
|
|
30
28
|
// When backed by engine, defer commit until batch ends.
|
|
31
|
-
|
|
29
|
+
const engineHost = host;
|
|
32
30
|
if (engine) {
|
|
33
|
-
|
|
31
|
+
const snapshot = engine.state.scene;
|
|
34
32
|
run();
|
|
35
|
-
|
|
33
|
+
const finalScene = host.getScene();
|
|
36
34
|
if (finalScene !== snapshot) {
|
|
37
35
|
engine.applyScene(finalScene, 'push');
|
|
38
36
|
}
|