@widgetic/canvas 0.5.40 → 0.5.41

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.
Files changed (120) hide show
  1. package/README.md +7 -3
  2. package/dist/canvas/DomCanvas.svelte +4 -0
  3. package/dist/canvas/DomCanvas.svelte.d.ts +8 -0
  4. package/dist/canvas/autosaveScheduler.js +89 -37
  5. package/dist/canvas/boardFrame.js +1 -1
  6. package/dist/canvas/boardSurface.js +1 -1
  7. package/dist/canvas/canvasLogger.js +19 -7
  8. package/dist/canvas/chromeLayout.js +18 -17
  9. package/dist/canvas/eventScope.js +14 -10
  10. package/dist/canvas/host/CanvasHost.d.ts +6 -0
  11. package/dist/canvas/host/CanvasHost.js +1556 -1267
  12. package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
  13. package/dist/canvas/host/CanvasHostApi.js +25 -4
  14. package/dist/canvas/host/browserServices.js +116 -65
  15. package/dist/canvas/host/conversion.js +170 -82
  16. package/dist/canvas/host/hostObject.js +7 -7
  17. package/dist/canvas/host/pageEvents.js +4 -4
  18. package/dist/canvas/host/screenRects.js +31 -28
  19. package/dist/canvas/host/strokeSnap.js +143 -88
  20. package/dist/canvas/openMarks.js +2 -2
  21. package/dist/canvas/outsideClickDeselect.js +7 -6
  22. package/dist/canvas/panZoomPosition.js +2 -2
  23. package/dist/canvas/preferredTextFont.js +25 -13
  24. package/dist/canvas/shapes/ShapeLibrary.js +148 -138
  25. package/dist/canvas/shapes/shapeImageStore.js +3 -3
  26. package/dist/canvas/shapes/strokeShapeSnap.js +233 -188
  27. package/dist/canvas/toolbarPosition.js +2 -2
  28. package/dist/canvas/ui/CanvasHint.svelte +16 -3
  29. package/dist/canvas/ui/CanvasHint.svelte.d.ts +2 -17
  30. package/dist/canvas/ui/CanvasToolbar.svelte +7 -7
  31. package/dist/canvas/ui/ToolButton.svelte +11 -3
  32. package/dist/canvas/ui/ToolButton.svelte.d.ts +2 -0
  33. package/dist/canvas/ui/colorModel.js +32 -21
  34. package/dist/canvas/ui/colorPickerMessages.js +5 -5
  35. package/dist/canvas/ui/floatingCard.js +18 -18
  36. package/dist/canvas/ui/floatingLayer.js +6 -5
  37. package/dist/canvas/ui/keyHints.js +11 -9
  38. package/dist/canvas/ui/popoverSide.js +3 -2
  39. package/dist/canvas/ui/selectionTraits.js +40 -34
  40. package/dist/canvas/ui/sizeLabelVisibility.d.ts +1 -1
  41. package/dist/canvas/ui/sizeLabelVisibility.js +1 -1
  42. package/dist/canvas/view/SelectionOverlay.svelte +33 -14
  43. package/dist/canvas/view/erasingMarks.js +7 -7
  44. package/dist/canvas/view/geometry.d.ts +14 -4
  45. package/dist/canvas/view/geometry.js +103 -77
  46. package/dist/components/preferenceStorage.js +2 -2
  47. package/dist/engine/EngineCore.d.ts +4 -1
  48. package/dist/engine/EngineCore.js +801 -695
  49. package/dist/engine/codec/canvasDocument.js +173 -173
  50. package/dist/engine/codec/hostReadableKeys.js +1 -1
  51. package/dist/engine/core/arrange.js +38 -26
  52. package/dist/engine/core/arrow.d.ts +10 -3
  53. package/dist/engine/core/arrow.js +96 -75
  54. package/dist/engine/core/arrowBinding.d.ts +69 -0
  55. package/dist/engine/core/arrowBinding.js +344 -0
  56. package/dist/engine/core/arrowBindingRecords.d.ts +43 -0
  57. package/dist/engine/core/arrowBindingRecords.js +141 -0
  58. package/dist/engine/core/brush.js +94 -67
  59. package/dist/engine/core/defaults.js +51 -90
  60. package/dist/engine/core/freehand.js +163 -147
  61. package/dist/engine/core/geometry.js +54 -47
  62. package/dist/engine/core/history.js +50 -33
  63. package/dist/engine/core/hitTest.d.ts +3 -0
  64. package/dist/engine/core/hitTest.js +83 -69
  65. package/dist/engine/core/matrix.js +20 -18
  66. package/dist/engine/core/pathData.js +210 -183
  67. package/dist/engine/core/rotationReset.js +6 -5
  68. package/dist/engine/core/scene.d.ts +3 -0
  69. package/dist/engine/core/scene.js +192 -129
  70. package/dist/engine/core/scribble.js +84 -59
  71. package/dist/engine/core/shapePaths.js +15 -12
  72. package/dist/engine/core/shapeTools.js +63 -72
  73. package/dist/engine/core/snapping.js +117 -65
  74. package/dist/engine/core/style.js +64 -31
  75. package/dist/engine/core/transform.js +94 -83
  76. package/dist/engine/core/types.d.ts +29 -0
  77. package/dist/engine/core/types.js +4 -4
  78. package/dist/engine/core/viewport.js +36 -23
  79. package/dist/engine/input/PointerController.js +476 -447
  80. package/dist/engine/input/arrowHandles.js +12 -12
  81. package/dist/engine/input/gestures.d.ts +12 -1
  82. package/dist/engine/input/gestures.js +342 -251
  83. package/dist/engine/input/hoverCursor.js +1 -1
  84. package/dist/engine/input/keyboard.d.ts +9 -1
  85. package/dist/engine/input/keyboard.js +46 -27
  86. package/dist/engine/input/selectionHandles.js +50 -45
  87. package/dist/engine/input/types.js +1 -1
  88. package/dist/engine/render/rasterize.js +158 -86
  89. package/dist/engine/state.d.ts +6 -1
  90. package/dist/engine/state.js +17 -5
  91. package/dist/engine/text/layoutText.js +62 -43
  92. package/dist/engine/text/textMetrics.js +10 -8
  93. package/dist/engine/widgets.js +137 -101
  94. package/dist/fonts/interFont.js +68 -17
  95. package/dist/fonts/workspaceFonts.js +90 -32
  96. package/dist/i18n/labels.js +4 -4
  97. package/dist/i18n/language.js +4 -4
  98. package/dist/i18n/messages.js +1 -1
  99. package/dist/i18n/shortcuts.d.ts +12 -0
  100. package/dist/i18n/shortcuts.js +32 -18
  101. package/dist/settings/workspaceToolSettings.js +48 -32
  102. package/dist/tools/components/catalog.d.ts +11 -0
  103. package/dist/tools/components/catalog.js +270 -0
  104. package/dist/tools/components/grouping.d.ts +9 -0
  105. package/dist/tools/components/grouping.js +33 -0
  106. package/dist/tools/definitions.d.ts +19 -0
  107. package/dist/tools/definitions.js +147 -0
  108. package/dist/tools/executors.d.ts +7 -0
  109. package/dist/tools/executors.js +324 -0
  110. package/dist/tools/hostAdapter.d.ts +14 -0
  111. package/dist/tools/hostAdapter.js +40 -0
  112. package/dist/tools/index.d.ts +6 -0
  113. package/dist/tools/index.js +6 -0
  114. package/dist/tools/sceneSummary.d.ts +24 -0
  115. package/dist/tools/sceneSummary.js +170 -0
  116. package/dist/tools/sketchIntent.d.ts +17 -0
  117. package/dist/tools/sketchIntent.js +166 -0
  118. package/dist/tools/types.d.ts +54 -0
  119. package/dist/tools/types.js +11 -0
  120. package/package.json +7 -2
@@ -0,0 +1,324 @@
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
+ import { createEllipseObject, createPathObject, createRectObject, createTextObject } from '../engine/core/defaults.js';
22
+ import { buildArrow } from '../engine/core/arrow.js';
23
+ import { serializePathData } from '../engine/core/pathData.js';
24
+ import { boundsOfObjects, findObject, indexScene, normalizeScene, removeObjects, shapeBounds, translateObjects } from '../engine/core/scene.js';
25
+ import { isBoxedObject } from '../engine/core/types.js';
26
+ import { drawUiComponent, makeArrowBetween } from './components/catalog.js';
27
+ import { centerOf, findByLabel, kindOfObject, labelOfObject, summarizeScene } from './sceneSummary.js';
28
+ function groupLabelForName(groupId, scene) {
29
+ for (var _i = 0, scene_1 = scene; _i < scene_1.length; _i++) {
30
+ var object = scene_1[_i];
31
+ if (object.parentId === groupId && object.type === 'text')
32
+ return object.text.trim();
33
+ }
34
+ return 'group';
35
+ }
36
+ function fail(code, error) {
37
+ return { ok: false, error: error, code: code };
38
+ }
39
+ function ok(message, extra) {
40
+ if (extra === void 0) { extra = {}; }
41
+ return __assign({ ok: true, message: message }, extra);
42
+ }
43
+ function clampInBounds(host, x, y, width, height) {
44
+ var pad = 8;
45
+ return (x >= -pad &&
46
+ y >= -pad &&
47
+ x + width <= host.bounds.width + pad &&
48
+ y + height <= host.bounds.height + pad &&
49
+ width > 0 &&
50
+ height > 0);
51
+ }
52
+ function addToScene(host, objects) {
53
+ var withIds = objects.map(function (object) { var _a; return (__assign(__assign({}, object), { id: (_a = object.id) !== null && _a !== void 0 ? _a : host.createId(kindOfObject(object)) })); });
54
+ var scene = normalizeScene(__spreadArray(__spreadArray([], host.getScene(), true), withIds, true));
55
+ return { scene: scene, ids: withIds.map(function (object) { return object.id; }) };
56
+ }
57
+ var executors = {
58
+ get_canvas: function (host) {
59
+ var summary = summarizeScene(host.getScene(), host.bounds);
60
+ return ok("Canvas has ".concat(summary.elements.length, " elements."), { summary: summary });
61
+ },
62
+ draw_ui_component: function (host, args) {
63
+ var _a, _b, _c;
64
+ var component = String((_a = args.component) !== null && _a !== void 0 ? _a : '');
65
+ var x = Number(args.x);
66
+ var y = Number(args.y);
67
+ var width = Number((_b = args.width) !== null && _b !== void 0 ? _b : 360);
68
+ var height = Number((_c = args.height) !== null && _c !== void 0 ? _c : 240);
69
+ var label = typeof args.label === 'string' ? args.label : undefined;
70
+ if (!Number.isFinite(x) || !Number.isFinite(y))
71
+ return fail('invalid_args', 'x and y are required numbers.');
72
+ var placement = drawUiComponent(component, x, y, width, height, label);
73
+ var _d = addToScene(host, placement.objects), scene = _d.scene, ids = _d.ids;
74
+ // The check is on what the component really covers (a button is 48 px high whatever height was asked for, a video
75
+ // player has its title above y and a navigation strip below the thumbnail), not on the size that was passed: a
76
+ // request with no size would otherwise be judged as 360 x 240, and a button near the bottom would be refused.
77
+ var covered = boundsOfObjects(scene, ids);
78
+ if (!covered || !clampInBounds(host, covered.x, covered.y, covered.width, covered.height)) {
79
+ var area = covered
80
+ ? " It would cover x ".concat(Math.round(covered.x), " to ").concat(Math.round(covered.x + covered.width), " and y ").concat(Math.round(covered.y), " to ").concat(Math.round(covered.y + covered.height), ", and the canvas is ").concat(Math.round(host.bounds.width), " x ").concat(Math.round(host.bounds.height), ".")
81
+ : '';
82
+ return fail('bounds', "Component would sit outside the canvas.".concat(area));
83
+ }
84
+ host.commitScene(scene);
85
+ return ok("Drew ".concat(component, " (").concat(placement.rootLabel, ")."), { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
86
+ },
87
+ add_shape: function (host, args) {
88
+ var _a;
89
+ var shape = String((_a = args.shape) !== null && _a !== void 0 ? _a : 'rectangle');
90
+ var x = Number(args.x);
91
+ var y = Number(args.y);
92
+ var width = Number(args.width);
93
+ var height = Number(args.height);
94
+ if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(width) || !Number.isFinite(height)) {
95
+ return fail('invalid_args', 'x, y, width and height are required.');
96
+ }
97
+ if (!clampInBounds(host, x, y, width, height))
98
+ return fail('bounds', 'Shape is outside the canvas.');
99
+ var object;
100
+ var label = typeof args.label === 'string' ? args.label : undefined;
101
+ if (shape === 'ellipse') {
102
+ object = createEllipseObject({ x: x, y: y, width: width, height: height });
103
+ }
104
+ else if (shape === 'diamond') {
105
+ var path = "M ".concat(width / 2, " 0 L ").concat(width, " ").concat(height / 2, " L ").concat(width / 2, " ").concat(height, " L 0 ").concat(height / 2, " Z");
106
+ object = createPathObject({ x: x, y: y, width: width, height: height, d: path, shape: 'diamond' });
107
+ }
108
+ else {
109
+ object = createRectObject({ x: x, y: y, width: width, height: height, roundness: shape === 'rounded' ? 16 : 0 });
110
+ }
111
+ var objects = [object];
112
+ if (label) {
113
+ objects.push(createTextObject({ x: x + 4, y: y + 4, text: label, fontSize: 12, autoWidth: true }));
114
+ }
115
+ var _b = addToScene(host, objects), scene = _b.scene, ids = _b.ids;
116
+ host.commitScene(scene);
117
+ return ok("Added ".concat(shape, "."), { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
118
+ },
119
+ add_text: function (host, args) {
120
+ var _a, _b;
121
+ var text = String((_a = args.text) !== null && _a !== void 0 ? _a : '').trim();
122
+ var x = Number(args.x);
123
+ var y = Number(args.y);
124
+ if (!text)
125
+ return fail('invalid_args', 'text is required.');
126
+ if (!Number.isFinite(x) || !Number.isFinite(y))
127
+ return fail('invalid_args', 'x and y are required.');
128
+ var width = Number((_b = args.width) !== null && _b !== void 0 ? _b : 200);
129
+ var object = createTextObject({ x: x, y: y, width: width, text: text, autoWidth: width <= 40 });
130
+ if (!clampInBounds(host, x, y, object.width, object.height))
131
+ return fail('bounds', 'Text is outside the canvas.');
132
+ var _c = addToScene(host, [object]), scene = _c.scene, ids = _c.ids;
133
+ host.commitScene(scene);
134
+ return ok('Added text.', { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
135
+ },
136
+ add_arrow: function (host, args) {
137
+ var fromX = Number(args.fromX);
138
+ var fromY = Number(args.fromY);
139
+ var toX = Number(args.toX);
140
+ var toY = Number(args.toY);
141
+ var scene = host.getScene();
142
+ if (typeof args.fromId === 'string') {
143
+ var center = centerOf(args.fromId, scene);
144
+ if (!center)
145
+ return fail('not_found', "fromId ".concat(args.fromId, " not found."));
146
+ fromX = center.x;
147
+ fromY = center.y;
148
+ }
149
+ if (typeof args.toId === 'string') {
150
+ var center = centerOf(args.toId, scene);
151
+ if (!center)
152
+ return fail('not_found', "toId ".concat(args.toId, " not found."));
153
+ toX = center.x;
154
+ toY = center.y;
155
+ }
156
+ if (![fromX, fromY, toX, toY].every(Number.isFinite))
157
+ return fail('invalid_args', 'Need from/to points or ids.');
158
+ var objects = [makeArrowBetween(fromX, fromY, toX, toY)];
159
+ if (typeof args.label === 'string' && args.label.trim()) {
160
+ objects.push(createTextObject({ x: (fromX + toX) / 2, y: (fromY + toY) / 2 - 16, text: args.label.trim(), fontSize: 12, autoWidth: true }));
161
+ }
162
+ var added = addToScene(host, objects);
163
+ host.commitScene(added.scene);
164
+ return ok('Added arrow.', { createdIds: added.ids, summary: summarizeScene(added.scene, host.bounds) });
165
+ },
166
+ move_elements: function (host, args) {
167
+ var _a, _b;
168
+ var ids = Array.isArray(args.ids) ? args.ids.map(String) : [];
169
+ if (ids.length === 0)
170
+ return fail('invalid_args', 'ids required.');
171
+ var scene = host.getScene();
172
+ var index = indexScene(scene);
173
+ for (var _i = 0, ids_1 = ids; _i < ids_1.length; _i++) {
174
+ var id = ids_1[_i];
175
+ if (!index.has(id))
176
+ return fail('not_found', "Element ".concat(id, " not found."));
177
+ }
178
+ var dx = Number((_a = args.dx) !== null && _a !== void 0 ? _a : 0);
179
+ var dy = Number((_b = args.dy) !== null && _b !== void 0 ? _b : 0);
180
+ if ((Number.isFinite(args.x) || Number.isFinite(args.y)) && ids.length > 0) {
181
+ var first = findObject(scene, ids[0]);
182
+ if (!first)
183
+ return fail('not_found', 'First id not movable.');
184
+ var bbox = first.type === 'group'
185
+ ? boundsOfObjects(scene, [first.id])
186
+ : isBoxedObject(first)
187
+ ? shapeBounds(first)
188
+ : null;
189
+ if (!bbox)
190
+ return fail('not_found', 'First id not movable.');
191
+ var targetDx = Number.isFinite(args.x) ? Number(args.x) - bbox.x : 0;
192
+ var targetDy = Number.isFinite(args.y) ? Number(args.y) - bbox.y : 0;
193
+ scene = translateObjects(scene, ids, targetDx, targetDy);
194
+ }
195
+ else if (dx !== 0 || dy !== 0) {
196
+ scene = translateObjects(scene, ids, dx, dy);
197
+ }
198
+ else {
199
+ return fail('invalid_args', 'Provide dx/dy or x/y.');
200
+ }
201
+ host.commitScene(scene);
202
+ return ok("Moved ".concat(ids.length, " element(s)."), { affectedIds: ids, summary: summarizeScene(scene, host.bounds) });
203
+ },
204
+ delete_elements: function (host, args) {
205
+ var ids = Array.isArray(args.ids) ? args.ids.map(String) : [];
206
+ if (ids.length === 0)
207
+ return fail('invalid_args', 'ids required.');
208
+ var scene = host.getScene();
209
+ if (ids.length === 1 && ids[0] === 'all') {
210
+ scene = [];
211
+ }
212
+ else {
213
+ var missing = ids.filter(function (id) { return !findObject(scene, id); });
214
+ if (missing.length > 0)
215
+ return fail('not_found', "Unknown ids: ".concat(missing.join(', ')));
216
+ scene = removeObjects(scene, ids);
217
+ }
218
+ host.commitScene(scene);
219
+ return ok('Deleted elements.', { affectedIds: ids, summary: summarizeScene(scene, host.bounds) });
220
+ },
221
+ clear_canvas: function (host) {
222
+ var scene = [];
223
+ host.commitScene(scene);
224
+ return ok('Canvas cleared.', { summary: summarizeScene(scene, host.bounds) });
225
+ },
226
+ name_components_with_arrows: function (host, args) {
227
+ var prefix = typeof args.prefix === 'string' ? args.prefix.trim() : '';
228
+ var sceneBefore = host.getScene();
229
+ var additions = [];
230
+ for (var _i = 0, sceneBefore_1 = sceneBefore; _i < sceneBefore_1.length; _i++) {
231
+ var object = sceneBefore_1[_i];
232
+ if (object.parentId !== null)
233
+ continue;
234
+ if (object.type === 'text')
235
+ continue;
236
+ if (object.type === 'path' && object.arrow)
237
+ continue;
238
+ if (!isBoxedObject(object) && object.type !== 'group')
239
+ continue;
240
+ var name = "".concat(prefix ? "".concat(prefix, " ") : '').concat(object.type === 'group' ? groupLabelForName(object.id, sceneBefore) : labelOfObject(object)).trim();
241
+ var bbox = object.type === 'group'
242
+ ? boundsOfObjects(sceneBefore, [object.id])
243
+ : isBoxedObject(object)
244
+ ? shapeBounds(object)
245
+ : null;
246
+ if (!bbox)
247
+ continue;
248
+ var labelX = bbox.x - 8;
249
+ var labelY = bbox.y - 28;
250
+ var label = createTextObject({ x: labelX, y: labelY, text: name, fontSize: 12, autoWidth: true });
251
+ additions.push(label);
252
+ var geometry = buildArrow({ x: labelX + 40, y: labelY + 10 }, { x: bbox.x + bbox.width / 2, y: bbox.y + bbox.height / 2 }, 0, 2);
253
+ additions.push(createPathObject({
254
+ x: geometry.box.x,
255
+ y: geometry.box.y,
256
+ width: geometry.box.width,
257
+ height: geometry.box.height,
258
+ d: serializePathData(geometry.commands),
259
+ shape: 'arrow',
260
+ arrow: geometry.spec,
261
+ stroke: '#111827',
262
+ strokeWidth: 2,
263
+ fill: 'transparent'
264
+ }));
265
+ }
266
+ var _a = addToScene(host, additions), scene = _a.scene, ids = _a.ids;
267
+ host.commitScene(scene);
268
+ return ok("Named ".concat(additions.length / 2, " components."), { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
269
+ }
270
+ };
271
+ export function executeCanvasTool(host, name, args) {
272
+ var run = executors[name];
273
+ if (!run)
274
+ return fail('invalid_args', "Unknown tool ".concat(name, "."));
275
+ try {
276
+ return run(host, args);
277
+ }
278
+ catch (error) {
279
+ return { ok: false, error: error instanceof Error ? error.message : 'Tool failed.', code: 'internal' };
280
+ }
281
+ }
282
+ /** Navigation wireframe group (label "Navigation"). */
283
+ export function findNavigationId(scene) {
284
+ for (var _i = 0, scene_2 = scene; _i < scene_2.length; _i++) {
285
+ var object = scene_2[_i];
286
+ if (object.type === 'text' && object.text.trim().toLowerCase() === 'navigation' && object.parentId) {
287
+ return object.parentId;
288
+ }
289
+ }
290
+ var direct = findByLabel(scene, 'navigation');
291
+ if (direct) {
292
+ var object = findObject(scene, direct);
293
+ if ((object === null || object === void 0 ? void 0 : object.type) === 'group')
294
+ return direct;
295
+ if (object === null || object === void 0 ? void 0 : object.parentId)
296
+ return object.parentId;
297
+ }
298
+ return findByLabel(scene, 'nav');
299
+ }
300
+ export function findVideoThumbnailBounds(scene) {
301
+ var _loop_1 = function (object) {
302
+ if (object.type === 'text' && object.text.toLowerCase() === 'thumbnail') {
303
+ var frame = scene.find(function (candidate) { return candidate.parentId === object.parentId || candidate.type === 'rect'; });
304
+ if (frame && isBoxedObject(frame))
305
+ return { value: shapeBounds(frame) };
306
+ }
307
+ };
308
+ for (var _i = 0, scene_3 = scene; _i < scene_3.length; _i++) {
309
+ var object = scene_3[_i];
310
+ var state_1 = _loop_1(object);
311
+ if (typeof state_1 === "object")
312
+ return state_1.value;
313
+ }
314
+ var screen = scene.find(function (object) { return object.type === 'rect' && object.fill === '#f3f4f6' && object.parentId !== null; });
315
+ return screen && isBoxedObject(screen) ? shapeBounds(screen) : null;
316
+ }
317
+ export function findVideoPlayerRoot(scene) {
318
+ for (var _i = 0, scene_4 = scene; _i < scene_4.length; _i++) {
319
+ var object = scene_4[_i];
320
+ if (object.type === 'text' && object.text.toLowerCase().includes('video player'))
321
+ return object.id;
322
+ }
323
+ return findByLabel(scene, 'video player');
324
+ }
@@ -0,0 +1,14 @@
1
+ import type { EngineCore } from '../engine/EngineCore.js';
2
+ import type { CanvasBounds, CanvasToolHost } from './types.js';
3
+ export interface EngineToolHostOptions {
4
+ engine: EngineCore;
5
+ bounds?: CanvasBounds;
6
+ /** When `transient`, scene updates apply without a new undo step until a `push` flush. */
7
+ commitMode?: 'push' | 'transient';
8
+ }
9
+ /**
10
+ * Adapts EngineCore to CanvasToolHost: one `commit()` per assistant turn.
11
+ */
12
+ export declare function createCanvasToolHostFromEngine(options: EngineToolHostOptions): CanvasToolHost;
13
+ /** Run several tool calls as a single undo step on the engine. */
14
+ export declare function runCanvasToolBatch(host: CanvasToolHost, run: () => void, engine?: EngineCore): void;
@@ -0,0 +1,40 @@
1
+ import { createObjectId } from '../engine/core/defaults.js';
2
+ /**
3
+ * Adapts EngineCore to CanvasToolHost: one `commit()` per assistant turn.
4
+ */
5
+ export function createCanvasToolHostFromEngine(options) {
6
+ var _a;
7
+ var engine = options.engine;
8
+ var bounds = (_a = options.bounds) !== null && _a !== void 0 ? _a : {
9
+ width: engine.state.viewSize.width || 1200,
10
+ height: engine.state.viewSize.height || 800
11
+ };
12
+ return {
13
+ bounds: bounds,
14
+ getScene: function () {
15
+ return engine.state.scene;
16
+ },
17
+ createId: function (prefix) {
18
+ return createObjectId(prefix !== null && prefix !== void 0 ? prefix : 'sketch');
19
+ },
20
+ commitScene: function (next) {
21
+ var _a;
22
+ engine.applyScene(next, (_a = options.commitMode) !== null && _a !== void 0 ? _a : 'push');
23
+ }
24
+ };
25
+ }
26
+ /** Run several tool calls as a single undo step on the engine. */
27
+ export function runCanvasToolBatch(host, run, engine) {
28
+ // When backed by engine, defer commit until batch ends.
29
+ var engineHost = host;
30
+ if (engine) {
31
+ var snapshot = engine.state.scene;
32
+ run();
33
+ var finalScene = host.getScene();
34
+ if (finalScene !== snapshot) {
35
+ engine.applyScene(finalScene, 'push');
36
+ }
37
+ return;
38
+ }
39
+ run();
40
+ }
@@ -0,0 +1,6 @@
1
+ export * from './types.js';
2
+ export * from './definitions.js';
3
+ export * from './sceneSummary.js';
4
+ export * from './executors.js';
5
+ export * from './hostAdapter.js';
6
+ export * from './sketchIntent.js';
@@ -0,0 +1,6 @@
1
+ export * from './types.js';
2
+ export * from './definitions.js';
3
+ export * from './sceneSummary.js';
4
+ export * from './executors.js';
5
+ export * from './hostAdapter.js';
6
+ export * from './sketchIntent.js';
@@ -0,0 +1,24 @@
1
+ import type { Scene } from '../engine/core/scene.js';
2
+ import type { ObjectId, SceneObject } from '../engine/core/types.js';
3
+ import type { CanvasBounds, SceneSummary } from './types.js';
4
+ export declare const SCENE_SUMMARY_MAX_ELEMENTS = 48;
5
+ export declare function labelOfObject(object: SceneObject): string;
6
+ export declare function kindOfObject(object: SceneObject): string;
7
+ export declare function summarizeScene(scene: Scene, bounds?: CanvasBounds): SceneSummary;
8
+ export declare function findByLabel(scene: Scene, needle: string): ObjectId | null;
9
+ export declare function centerOf(id: ObjectId, scene: Scene): {
10
+ x: number;
11
+ y: number;
12
+ } | null;
13
+ export declare function sceneIndexMap(scene: Scene): Map<ObjectId, SceneObject>;
14
+ export declare function bboxIntersects(a: {
15
+ x: number;
16
+ y: number;
17
+ width: number;
18
+ height: number;
19
+ }, b: {
20
+ x: number;
21
+ y: number;
22
+ width: number;
23
+ height: number;
24
+ }): boolean;
@@ -0,0 +1,170 @@
1
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
2
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3
+ if (ar || !(i in from)) {
4
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
5
+ ar[i] = from[i];
6
+ }
7
+ }
8
+ return to.concat(ar || Array.prototype.slice.call(from));
9
+ };
10
+ import { boundsOfObjects, findObject, indexScene, shapeBounds } from '../engine/core/scene.js';
11
+ import { isBoxedObject } from '../engine/core/types.js';
12
+ var DEFAULT_BOUNDS = { width: 1200, height: 800 };
13
+ export var SCENE_SUMMARY_MAX_ELEMENTS = 48;
14
+ /** The longest label a summary carries (the assistant request schema accepts 256, and the model needs far fewer). */
15
+ var SCENE_LABEL_MAX_CHARS = 80;
16
+ /** A label cut to the length a summary carries, so a long text never makes a request the gateway refuses. */
17
+ function shortenLabel(text) {
18
+ return text.length > SCENE_LABEL_MAX_CHARS ? "".concat(text.slice(0, SCENE_LABEL_MAX_CHARS - 3), "...") : text;
19
+ }
20
+ export function labelOfObject(object) {
21
+ var _a, _b;
22
+ if (object.type === 'text') {
23
+ return shortenLabel(object.text.trim());
24
+ }
25
+ if (object.type === 'widget')
26
+ return (_a = object.widgetName) !== null && _a !== void 0 ? _a : 'widget';
27
+ if (object.type === 'group')
28
+ return 'group';
29
+ if (object.type === 'path' && object.arrow)
30
+ return 'arrow';
31
+ if (object.type === 'path' && object.shape)
32
+ return String(object.shape);
33
+ if (object.type === 'rect')
34
+ return 'rectangle';
35
+ if (object.type === 'ellipse')
36
+ return 'ellipse';
37
+ if (object.type === 'image')
38
+ return (_b = object.fileName) !== null && _b !== void 0 ? _b : 'image';
39
+ return object.type;
40
+ }
41
+ function groupLabelFromMembers(groupId, scene) {
42
+ for (var _i = 0, scene_1 = scene; _i < scene_1.length; _i++) {
43
+ var object = scene_1[_i];
44
+ if (object.parentId === groupId && object.type === 'text') {
45
+ // Cut like the label of a loose text: a component's first text can be a whole paragraph.
46
+ return shortenLabel(object.text.trim()) || 'group';
47
+ }
48
+ }
49
+ return 'group';
50
+ }
51
+ export function kindOfObject(object) {
52
+ if (object.type === 'group')
53
+ return 'group';
54
+ if (object.type === 'path' && object.arrow)
55
+ return 'arrow';
56
+ if (object.type === 'text')
57
+ return 'text';
58
+ return object.type;
59
+ }
60
+ function elementPriority(element) {
61
+ if (element.kind === 'group')
62
+ return 0;
63
+ if (/navigation|video|thumbnail|card|button/i.test(element.label))
64
+ return 1;
65
+ if (element.kind === 'rect')
66
+ return 2;
67
+ return 3;
68
+ }
69
+ function compareElements(a, b) {
70
+ var pa = elementPriority(a);
71
+ var pb = elementPriority(b);
72
+ if (pa !== pb)
73
+ return pa - pb;
74
+ var areaA = a.bbox.width * a.bbox.height;
75
+ var areaB = b.bbox.width * b.bbox.height;
76
+ if (areaA !== areaB)
77
+ return areaB - areaA;
78
+ return a.zIndex - b.zIndex;
79
+ }
80
+ export function summarizeScene(scene, bounds) {
81
+ if (bounds === void 0) { bounds = DEFAULT_BOUNDS; }
82
+ var elements = [];
83
+ scene.forEach(function (object, zIndex) {
84
+ if (object.type === 'group') {
85
+ var memberIds = scene.filter(function (member) { return member.parentId === object.id; }).map(function (member) { return member.id; });
86
+ var bbox_1 = boundsOfObjects(scene, memberIds);
87
+ if (!bbox_1)
88
+ return;
89
+ elements.push({
90
+ id: object.id,
91
+ kind: 'group',
92
+ label: groupLabelFromMembers(object.id, scene),
93
+ bbox: bbox_1,
94
+ zIndex: zIndex,
95
+ parentId: object.parentId
96
+ });
97
+ return;
98
+ }
99
+ if (object.parentId !== null)
100
+ return;
101
+ if (!isBoxedObject(object))
102
+ return;
103
+ var bbox = shapeBounds(object);
104
+ elements.push({
105
+ id: object.id,
106
+ kind: kindOfObject(object),
107
+ label: labelOfObject(object),
108
+ bbox: bbox,
109
+ zIndex: zIndex,
110
+ parentId: object.parentId
111
+ });
112
+ });
113
+ var sorted = __spreadArray([], elements, true).sort(compareElements);
114
+ var totalCount = sorted.length;
115
+ var truncated = totalCount > SCENE_SUMMARY_MAX_ELEMENTS;
116
+ var limited = truncated ? sorted.slice(0, SCENE_SUMMARY_MAX_ELEMENTS) : sorted;
117
+ return {
118
+ bounds: bounds,
119
+ elements: limited,
120
+ totalCount: totalCount,
121
+ truncated: truncated,
122
+ truncationNote: truncated ? "Showing ".concat(SCENE_SUMMARY_MAX_ELEMENTS, " of ").concat(totalCount, " top-level elements.") : undefined
123
+ };
124
+ }
125
+ export function findByLabel(scene, needle) {
126
+ var lower = needle.toLowerCase();
127
+ for (var _i = 0, scene_2 = scene; _i < scene_2.length; _i++) {
128
+ var object = scene_2[_i];
129
+ if (object.type === 'group') {
130
+ var label = groupLabelFromMembers(object.id, scene).toLowerCase();
131
+ if (label.includes(lower))
132
+ return object.id;
133
+ }
134
+ if (object.type === 'text') {
135
+ var label = object.text.toLowerCase();
136
+ if (label.includes(lower))
137
+ return object.id;
138
+ }
139
+ }
140
+ for (var _a = 0, scene_3 = scene; _a < scene_3.length; _a++) {
141
+ var object = scene_3[_a];
142
+ var label = labelOfObject(object).toLowerCase();
143
+ if (label.includes(lower))
144
+ return object.id;
145
+ }
146
+ return null;
147
+ }
148
+ export function centerOf(id, scene) {
149
+ var object = findObject(scene, id);
150
+ if ((object === null || object === void 0 ? void 0 : object.type) === 'group') {
151
+ var bbox_2 = boundsOfObjects(scene, [id]);
152
+ if (!bbox_2)
153
+ return null;
154
+ return { x: bbox_2.x + bbox_2.width / 2, y: bbox_2.y + bbox_2.height / 2 };
155
+ }
156
+ if (!object || !isBoxedObject(object))
157
+ return null;
158
+ var bbox = shapeBounds(object);
159
+ return { x: bbox.x + bbox.width / 2, y: bbox.y + bbox.height / 2 };
160
+ }
161
+ export function sceneIndexMap(scene) {
162
+ return indexScene(scene);
163
+ }
164
+ export function bboxIntersects(a, b) {
165
+ var pad = 2;
166
+ return (a.x + a.width > b.x + pad &&
167
+ b.x + b.width > a.x + pad &&
168
+ a.y + a.height > b.y + pad &&
169
+ b.y + b.height > a.y + pad);
170
+ }
@@ -0,0 +1,17 @@
1
+ export type SketchIntentVerdict = 'sketch' | 'generate' | 'unknown';
2
+ export declare const SKETCH_INTENT_COUNTER_EXAMPLES: readonly ["build me a countdown widget", "make the button blue in the widget", "generate the react code for this", "implement the checkout form", "ship this widget to production", "publish the widget now"];
3
+ /**
4
+ * Free-text requests that draw, in the languages the router knows. Every one must route to the sketch assistant
5
+ * (the creator's tests and the gateway's mirror hold the same list).
6
+ */
7
+ export declare const SKETCH_FREE_TEXT_EXAMPLES: readonly ["Sketch a pricing page with three plans and a call to action under each", "wireframe a dashboard with a sidebar and four stat cards", "can you draw three circles in a row and connect them with arrows", "doodle a house with a red roof", "put the navigation outside the thumbnail", "dibuja una página de precios con tres planes", "dibújame un formulario de inicio de sesión", "haz un boceto de una tienda en línea", "dessine une page de tarification avec trois offres", "fais un croquis de la page d’accueil", "zeichne eine Preisseite mit drei Tarifen", "skizziere ein Dashboard mit vier Kacheln", "disegna una pagina di login", "fai uno schizzo della home page", "desenha uma página de preços com três planos", "faz um rascunho do painel", "desenează o pagină de prețuri cu trei planuri", "desenează-mi un formular de autentificare", "fă o schiță a paginii de start", "narysuj stronę z cennikiem", "zrób szkic panelu", "teken een prijzenpagina met drie abonnementen", "maak een schets van het dashboard", "ログイン画面のスケッチを描いて", "画一个登录表单", "로그인 폼을 그려줘"];
8
+ /**
9
+ * Requests that are not for a drawing, with the verdict each gets. Creating something ("make", "create", "I need") is a
10
+ * widget, so most of them are `unknown` and go to the code generator by default; the ones that name code are `generate`.
11
+ */
12
+ export declare const SKETCH_FREE_TEXT_COUNTER_EXAMPLES: readonly [readonly ["a video player with a playlist", "unknown"], readonly ["create a countdown timer with a start button", "unknown"], readonly ["I want a quiz about world capitals", "unknown"], readonly ["make a picture slider with arrows", "unknown"], readonly ["dibuja y publica el widget", "generate"], readonly ["publie le widget maintenant", "generate"], readonly ["erstelle einen Countdown-Timer", "unknown"], readonly ["costruisci un widget con il conto alla rovescia", "generate"], readonly ["crea un widget di sondaggio", "unknown"], readonly ["faz um widget de contagem decrescente", "unknown"], readonly ["construiește un widget cu numărătoare inversă", "generate"], readonly ["opublikuj widget teraz", "generate"], readonly ["画面を作って", "unknown"], readonly ["kennzeichnen Sie die Felder", "unknown"]];
13
+ /**
14
+ * Lightweight sketch-vs-codegen classifier for the creator chat (gateway mirrors this in tests).
15
+ */
16
+ export declare function classifySketchIntent(message: string): SketchIntentVerdict;
17
+ export declare const SKETCH_ACCEPTANCE_PROMPTS: readonly ["draw a video player for me", "move the navigation outside the video thumbnail", "delete all sketches", "name every component and draw an arrow to it"];