@widgetic/canvas 0.5.44 → 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/DomCanvas.svelte +52 -3
- package/dist/canvas/DomCanvas.svelte.d.ts +59 -34
- package/dist/canvas/host/CanvasHost.d.ts +74 -3
- package/dist/canvas/host/CanvasHost.js +278 -16
- package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
- package/dist/canvas/host/CanvasHostApi.js +9 -1
- package/dist/canvas/host/boardFiles.d.ts +90 -0
- package/dist/canvas/host/boardFiles.js +139 -0
- package/dist/canvas/host/browserServices.d.ts +17 -0
- package/dist/canvas/host/browserServices.js +58 -0
- package/dist/canvas/index.d.ts +2 -1
- package/dist/canvas/types.d.ts +7 -1
- package/dist/canvas/ui/CanvasContextMenu.svelte +7 -0
- package/dist/canvas/ui/CanvasToolbar.svelte +4 -1
- package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +2 -0
- package/dist/canvas/ui/SelectionPanel.svelte +78 -17
- package/dist/canvas/ui/ShapesMenu.svelte +65 -31
- package/dist/canvas/ui/ShapesMenu.svelte.d.ts +2 -0
- package/dist/canvas/ui/ShortcutTooltip.svelte +64 -0
- package/dist/canvas/ui/ShortcutTooltip.svelte.d.ts +24 -0
- package/dist/canvas/ui/ToolButton.svelte +34 -55
- package/dist/canvas/ui/ToolButton.svelte.d.ts +4 -1
- package/dist/canvas/ui/floatingCard.d.ts +26 -4
- package/dist/canvas/ui/floatingCard.js +26 -4
- package/dist/canvas/ui/icons.d.ts +1 -1
- package/dist/canvas/ui/icons.js +1 -1
- package/dist/canvas/ui/selectionTraits.d.ts +12 -0
- package/dist/canvas/ui/selectionTraits.js +26 -1
- package/dist/canvas/ui/sizeLabelVisibility.d.ts +4 -2
- package/dist/canvas/ui/sizeLabelVisibility.js +5 -5
- package/dist/canvas/view/CanvasStage.svelte +7 -2
- package/dist/canvas/view/CanvasStage.svelte.d.ts +2 -0
- package/dist/canvas/view/SelectionOverlay.svelte +17 -10
- package/dist/canvas/view/WidgetChromeLayer.svelte +15 -3
- package/dist/canvas/view/geometry.d.ts +1 -1
- package/dist/canvas/view/geometry.js +1 -1
- package/dist/components/CanvasBoardPlaceholder.svelte +68 -10
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +10 -2
- package/dist/components/CanvasBoardPoster.svelte +206 -0
- package/dist/components/CanvasBoardPoster.svelte.d.ts +17 -0
- package/dist/components/CanvasToolbarPlaceholder.svelte +129 -0
- package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +15 -0
- package/dist/components/boardLoaderDelay.d.ts +39 -0
- package/dist/components/boardLoaderDelay.js +71 -0
- package/dist/components/index.d.ts +7 -0
- package/dist/components/index.js +8 -0
- package/dist/engine/EngineCore.d.ts +46 -4
- package/dist/engine/EngineCore.js +85 -16
- package/dist/engine/codec/canvasDocument.d.ts +7 -0
- package/dist/engine/codec/canvasDocument.js +25 -2
- package/dist/engine/core/arrow.d.ts +18 -8
- package/dist/engine/core/arrow.js +24 -12
- package/dist/engine/core/arrowBinding.d.ts +1 -1
- package/dist/engine/core/arrowBinding.js +10 -3
- package/dist/engine/core/arrowBindingRecords.d.ts +9 -3
- package/dist/engine/core/arrowBindingRecords.js +10 -6
- package/dist/engine/core/brush.d.ts +8 -0
- package/dist/engine/core/brush.js +10 -2
- package/dist/engine/core/cameraMove.d.ts +6 -0
- package/dist/engine/core/cameraMove.js +6 -0
- package/dist/engine/core/defaults.js +2 -0
- package/dist/engine/core/scene.d.ts +22 -0
- package/dist/engine/core/scene.js +86 -1
- package/dist/engine/core/style.d.ts +10 -0
- package/dist/engine/core/style.js +19 -3
- package/dist/engine/core/transform.d.ts +6 -0
- package/dist/engine/core/transform.js +9 -0
- package/dist/engine/core/types.d.ts +7 -0
- package/dist/engine/input/PointerController.d.ts +15 -1
- package/dist/engine/input/PointerController.js +70 -12
- package/dist/engine/input/gestures.d.ts +3 -1
- package/dist/engine/input/gestures.js +41 -10
- package/dist/engine/input/keyboard.d.ts +22 -3
- package/dist/engine/input/keyboard.js +100 -5
- package/dist/engine/input/types.d.ts +7 -0
- package/dist/engine/state.d.ts +18 -0
- package/dist/engine/state.js +10 -0
- package/dist/fonts/workspaceFonts.d.ts +6 -0
- package/dist/fonts/workspaceFonts.js +44 -29
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/messages.d.ts +6 -0
- package/dist/i18n/messages.js +6 -0
- package/dist/i18n/shortcuts.d.ts +6 -4
- package/dist/i18n/shortcuts.js +16 -12
- package/dist/index.d.ts +2 -1
- package/dist/poster/boardPlacement.d.ts +80 -0
- package/dist/poster/boardPlacement.js +138 -0
- package/dist/poster/boardPoster.d.ts +80 -0
- package/dist/poster/boardPoster.js +226 -0
- package/dist/poster/paintBoardPoster.d.ts +49 -0
- package/dist/poster/paintBoardPoster.js +89 -0
- package/dist/poster/posterPictures.d.ts +32 -0
- package/dist/poster/posterPictures.js +122 -0
- package/dist/settings/rememberedTool.d.ts +50 -0
- package/dist/settings/rememberedTool.js +68 -0
- package/dist/settings/workspaceToolSettings.d.ts +14 -0
- package/dist/settings/workspaceToolSettings.js +21 -10
- package/dist/tools/definitions.js +26 -4
- package/dist/tools/executors.js +81 -5
- package/dist/tools/hostAdapter.d.ts +5 -1
- package/dist/tools/hostAdapter.js +2 -0
- package/dist/tools/styleArguments.d.ts +35 -0
- package/dist/tools/styleArguments.js +91 -0
- package/dist/tools/types.d.ts +26 -1
- package/dist/tools/types.js +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tool a board opens with.
|
|
3
|
+
*
|
|
4
|
+
* A board used to open with the pencil every time (where the page shows the drawing tools), so a refresh took the person back to the
|
|
5
|
+
* pencil from whatever tool they had chosen. It now opens with the tool they had last, kept for the workspace beside the rest of their
|
|
6
|
+
* tool settings (`workspaceToolSettings.ts`), and with the pencil (Select, where the page hides the drawing tools) when none was kept.
|
|
7
|
+
*
|
|
8
|
+
* One rule decides it for the board (`CanvasHost`) and for the picture of the toolbar that is drawn before the board is there
|
|
9
|
+
* (`CanvasToolbarPlaceholder`), so the tool that is lit does not change at the hand-over.
|
|
10
|
+
*/
|
|
11
|
+
import { loadWorkspaceToolSettings } from './workspaceToolSettings.js';
|
|
12
|
+
/**
|
|
13
|
+
* The tools a board can open with: the ones that have a button on the toolbar, the shapes of the Shapes menu included. `pan` has no
|
|
14
|
+
* button (it is the H key and the space bar) and a library shape needs the key of its shape, so neither is kept.
|
|
15
|
+
*/
|
|
16
|
+
export const REMEMBERED_TOOL_IDS = ['select', 'draw', 'erase', 'arrow', 'text', 'rect', 'rounded-rect', 'ellipse', 'circle', 'triangle'];
|
|
17
|
+
/** The tools of the Shapes menu (its button is the one that is lit while one of them is the tool). */
|
|
18
|
+
export const SHAPE_TOOL_IDS = ['rect', 'rounded-rect', 'ellipse', 'circle', 'triangle'];
|
|
19
|
+
/** The tools of the drawing group: the page takes their buttons off (`showDrawingTools: false`), and a board does not open with a tool it has no button for. */
|
|
20
|
+
export const DRAWING_GROUP_TOOL_IDS = ['draw', 'erase', 'arrow', 'text'];
|
|
21
|
+
/** Whether a value is a tool that is kept (anything else that a storage holds is the same as nothing). */
|
|
22
|
+
export function isRememberedTool(value) {
|
|
23
|
+
return typeof value === 'string' && REMEMBERED_TOOL_IDS.includes(value);
|
|
24
|
+
}
|
|
25
|
+
/** The tool of this change of tool, when it is one that is kept; null for the others (the hand, a library shape). */
|
|
26
|
+
export function toolToRemember(tool) {
|
|
27
|
+
return isRememberedTool(tool) ? tool : null;
|
|
28
|
+
}
|
|
29
|
+
/** Whether the tool is one of the drawing group, which a page may take off its toolbar. */
|
|
30
|
+
export function isDrawingGroupTool(tool) {
|
|
31
|
+
return DRAWING_GROUP_TOOL_IDS.includes(tool);
|
|
32
|
+
}
|
|
33
|
+
/** The tool of a board where none was kept: the pencil where the page shows the drawing tools (a board opens ready for a sketch), else Select. */
|
|
34
|
+
export function defaultFirstTool(showDrawingTools) {
|
|
35
|
+
return showDrawingTools ? 'draw' : 'select';
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The tool a board opens with, given what was kept (anything, as it came from a storage) and whether the page shows the drawing tools:
|
|
39
|
+
* what was kept, when the board has a button for it, else the tool of a board where none was kept.
|
|
40
|
+
*/
|
|
41
|
+
export function toolTheBoardOpensWith(options) {
|
|
42
|
+
const { rememberedToolId, showDrawingTools } = options;
|
|
43
|
+
if (!isRememberedTool(rememberedToolId))
|
|
44
|
+
return defaultFirstTool(showDrawingTools);
|
|
45
|
+
if (!showDrawingTools && isDrawingGroupTool(rememberedToolId))
|
|
46
|
+
return defaultFirstTool(showDrawingTools);
|
|
47
|
+
return rememberedToolId;
|
|
48
|
+
}
|
|
49
|
+
/** The tool kept for this workspace and person, or null (nothing kept, no workspace to keep it for, a store that refuses, or a value that is no tool). */
|
|
50
|
+
export function readRememberedToolId(scope, adapter) {
|
|
51
|
+
const { workspaceId, userId } = scope;
|
|
52
|
+
if (!workspaceId)
|
|
53
|
+
return null;
|
|
54
|
+
try {
|
|
55
|
+
const stored = loadWorkspaceToolSettings({ workspaceId, userId }, adapter)?.tool?.id;
|
|
56
|
+
return isRememberedTool(stored) ? stored : null;
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
// A store that blocks reading is the same as a store with nothing in it.
|
|
60
|
+
console.warn('[canvas] The tool that was kept could not be read, so the board opens with the usual one:', error);
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** The tool a board opens with: read from the storage of the workspace, decided by `toolTheBoardOpensWith`. */
|
|
65
|
+
export function firstToolOfBoard(options) {
|
|
66
|
+
const { showDrawingTools, adapter, ...scope } = options;
|
|
67
|
+
return toolTheBoardOpensWith({ rememberedToolId: readRememberedToolId(scope, adapter), showDrawingTools });
|
|
68
|
+
}
|
|
@@ -32,6 +32,20 @@ export interface WorkspaceToolSettingsV1 {
|
|
|
32
32
|
headStart?: boolean;
|
|
33
33
|
headEnd?: boolean;
|
|
34
34
|
};
|
|
35
|
+
/** How the Draw tool draws, set in the panel before a line is drawn (see `PenStyle`). Left out until the person sets the pen. */
|
|
36
|
+
pen?: {
|
|
37
|
+
color?: string;
|
|
38
|
+
width?: number;
|
|
39
|
+
opacity?: number;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* The tool the person last had chosen on the toolbar, so a board opened again (a refresh included) starts with it and not always with
|
|
43
|
+
* the pencil (see `rememberedTool.ts`, which says which tools are kept). Left out until a tool is chosen. The id is checked when it
|
|
44
|
+
* is read: a value that is not a tool of the toolbar is the same as none.
|
|
45
|
+
*/
|
|
46
|
+
tool?: {
|
|
47
|
+
id?: string;
|
|
48
|
+
};
|
|
35
49
|
}
|
|
36
50
|
export interface WorkspaceToolSettingsScope {
|
|
37
51
|
workspaceId: string;
|
|
@@ -45,16 +45,32 @@ export function parseWorkspaceToolSettings(raw) {
|
|
|
45
45
|
export function loadWorkspaceToolSettings(scope, adapter = adapterFromPreferenceStorage()) {
|
|
46
46
|
return parseWorkspaceToolSettings(adapter.load(storageKey(scope)));
|
|
47
47
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
export function saveWorkspaceToolSettingsDebounced(scope, patch, adapter = adapterFromPreferenceStorage()) {
|
|
51
|
-
const current = loadWorkspaceToolSettings(scope, adapter) ?? { v: WORKSPACE_TOOL_SETTINGS_SCHEMA_VERSION };
|
|
48
|
+
/** The settings with a patch laid over them: each part (text, paint, arrow, pen, tool) keeps what the patch does not say. */
|
|
49
|
+
function mergeSettings(current, patch) {
|
|
52
50
|
const merged = {
|
|
53
51
|
v: WORKSPACE_TOOL_SETTINGS_SCHEMA_VERSION,
|
|
54
52
|
text: { ...current.text, ...patch.text },
|
|
55
53
|
paint: { ...current.paint, ...patch.paint },
|
|
56
54
|
arrow: { ...current.arrow, ...patch.arrow }
|
|
57
55
|
};
|
|
56
|
+
// The pen is written only when there is one: settings of a person who never touched it hold none.
|
|
57
|
+
if (current.pen || patch.pen)
|
|
58
|
+
merged.pen = { ...current.pen, ...patch.pen };
|
|
59
|
+
// The tool is written only when one was chosen, for the same reason.
|
|
60
|
+
if (current.tool || patch.tool)
|
|
61
|
+
merged.tool = { ...current.tool, ...patch.tool };
|
|
62
|
+
return merged;
|
|
63
|
+
}
|
|
64
|
+
let pending = null;
|
|
65
|
+
let debounceTimer;
|
|
66
|
+
export function saveWorkspaceToolSettingsDebounced(scope, patch, adapter = adapterFromPreferenceStorage()) {
|
|
67
|
+
// A change that waits for its write (see the delay below) is part of the settings already: the next change is laid over it, and not
|
|
68
|
+
// over what the storage still holds, so two changes made within the delay are both kept. A change that waits for another workspace
|
|
69
|
+
// is written first, and is not lost to this one.
|
|
70
|
+
if (pending && storageKey(pending.scope) !== storageKey(scope))
|
|
71
|
+
flushWorkspaceToolSettingsPending();
|
|
72
|
+
const current = pending?.value ?? loadWorkspaceToolSettings(scope, adapter) ?? { v: WORKSPACE_TOOL_SETTINGS_SCHEMA_VERSION };
|
|
73
|
+
const merged = mergeSettings(current, patch);
|
|
58
74
|
pending = { scope, value: merged, adapter };
|
|
59
75
|
if (debounceTimer !== undefined)
|
|
60
76
|
clearTimeout(debounceTimer);
|
|
@@ -79,12 +95,7 @@ export function flushWorkspaceToolSettingsPending() {
|
|
|
79
95
|
/** Merges a patch into settings without debounce (tests). */
|
|
80
96
|
export function saveWorkspaceToolSettingsNow(scope, patch, adapter) {
|
|
81
97
|
const current = loadWorkspaceToolSettings(scope, adapter) ?? { v: WORKSPACE_TOOL_SETTINGS_SCHEMA_VERSION };
|
|
82
|
-
const merged =
|
|
83
|
-
v: WORKSPACE_TOOL_SETTINGS_SCHEMA_VERSION,
|
|
84
|
-
text: { ...current.text, ...patch.text },
|
|
85
|
-
paint: { ...current.paint, ...patch.paint },
|
|
86
|
-
arrow: { ...current.arrow, ...patch.arrow }
|
|
87
|
-
};
|
|
98
|
+
const merged = mergeSettings(current, patch);
|
|
88
99
|
adapter.save(storageKey(scope), JSON.stringify(merged));
|
|
89
100
|
return merged;
|
|
90
101
|
}
|
|
@@ -31,7 +31,7 @@ export const CANVAS_TOOL_DEFINITIONS = [
|
|
|
31
31
|
},
|
|
32
32
|
{
|
|
33
33
|
name: 'add_shape',
|
|
34
|
-
description: 'Add a primitive rectangle, rounded rectangle, ellipse, or diamond.',
|
|
34
|
+
description: 'Add a primitive rectangle, rounded rectangle, ellipse, or diamond. Pass fill and stroke (hex colours, for example the brand colours listed in the request context) to colour it.',
|
|
35
35
|
parameters: {
|
|
36
36
|
type: 'object',
|
|
37
37
|
required: ['shape', 'x', 'y', 'width', 'height'],
|
|
@@ -41,14 +41,16 @@ export const CANVAS_TOOL_DEFINITIONS = [
|
|
|
41
41
|
y: { type: 'number' },
|
|
42
42
|
width: { type: 'number', minimum: 4, maximum: 4000 },
|
|
43
43
|
height: { type: 'number', minimum: 4, maximum: 4000 },
|
|
44
|
-
label: { type: 'string', maxLength: 200 }
|
|
44
|
+
label: { type: 'string', maxLength: 200 },
|
|
45
|
+
fill: { type: 'string', maxLength: 9, description: 'Fill colour as a hex colour such as #1a2b3c, or "transparent".' },
|
|
46
|
+
stroke: { type: 'string', maxLength: 9, description: 'Outline colour as a hex colour such as #1a2b3c.' }
|
|
45
47
|
},
|
|
46
48
|
additionalProperties: false
|
|
47
49
|
}
|
|
48
50
|
},
|
|
49
51
|
{
|
|
50
52
|
name: 'add_text',
|
|
51
|
-
description: 'Place a text label on the canvas.',
|
|
53
|
+
description: 'Place a text label on the canvas. Pass fontFamily (one of the brand fonts listed in the request context, or a font the canvas has), color (a hex colour such as a brand colour), fontSize and fontWeight to style it.',
|
|
52
54
|
parameters: {
|
|
53
55
|
type: 'object',
|
|
54
56
|
required: ['text', 'x', 'y'],
|
|
@@ -56,7 +58,27 @@ export const CANVAS_TOOL_DEFINITIONS = [
|
|
|
56
58
|
text: { type: 'string', minLength: 1, maxLength: 500 },
|
|
57
59
|
x: { type: 'number' },
|
|
58
60
|
y: { type: 'number' },
|
|
59
|
-
width: { type: 'number', minimum: 20, maximum: 2000 }
|
|
61
|
+
width: { type: 'number', minimum: 20, maximum: 2000 },
|
|
62
|
+
fontFamily: { type: 'string', maxLength: 80, description: 'A font family name exactly as listed (for example the brand kit fonts).' },
|
|
63
|
+
fontSize: { type: 'number', minimum: 8, maximum: 200 },
|
|
64
|
+
color: { type: 'string', maxLength: 9, description: 'Text colour as a hex colour such as #1a2b3c.' },
|
|
65
|
+
fontWeight: { type: 'string', enum: ['normal', 'bold'] }
|
|
66
|
+
},
|
|
67
|
+
additionalProperties: false
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: 'add_asset',
|
|
72
|
+
description: 'Place one picture from the assets listed in the request context on the canvas, by its assetId: an SVG icon from the user\'s brand kit or a file from the user\'s file browser. Prefer an asset over drawing a shape whenever one fits (a logo, an icon, a photo the user chose). Never invent an assetId. Pass width and height to size it (the proportions are kept when only one is given).',
|
|
73
|
+
parameters: {
|
|
74
|
+
type: 'object',
|
|
75
|
+
required: ['assetId', 'x', 'y'],
|
|
76
|
+
properties: {
|
|
77
|
+
assetId: { type: 'string', minLength: 1, maxLength: 120 },
|
|
78
|
+
x: { type: 'number' },
|
|
79
|
+
y: { type: 'number' },
|
|
80
|
+
width: { type: 'number', minimum: 8, maximum: 2000 },
|
|
81
|
+
height: { type: 'number', minimum: 8, maximum: 2000 }
|
|
60
82
|
},
|
|
61
83
|
additionalProperties: false
|
|
62
84
|
}
|
package/dist/tools/executors.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { createEllipseObject, createPathObject, createRectObject, createTextObject } from '../engine/core/defaults.js';
|
|
1
|
+
import { createEllipseObject, createImageObject, createPathObject, createRectObject, createTextObject } from '../engine/core/defaults.js';
|
|
2
|
+
import { readImageAddress, readLibraryFileAddress } from '../engine/codec/canvasDocument.js';
|
|
2
3
|
import { buildArrow } from '../engine/core/arrow.js';
|
|
3
4
|
import { serializePathData } from '../engine/core/pathData.js';
|
|
4
5
|
import { boundsOfObjects, findObject, indexScene, normalizeScene, removeObjects, shapeBounds, translateObjects } from '../engine/core/scene.js';
|
|
5
6
|
import { isBoxedObject } from '../engine/core/types.js';
|
|
6
7
|
import { drawUiComponent, makeArrowBetween } from './components/catalog.js';
|
|
7
8
|
import { centerOf, findByLabel, kindOfObject, labelOfObject, summarizeScene } from './sceneSummary.js';
|
|
9
|
+
import { assetSizeFor, readColorArgument, readFontFamilyArgument, readNumberArgument } from './styleArguments.js';
|
|
8
10
|
function groupLabelForName(groupId, scene) {
|
|
9
11
|
for (const object of scene) {
|
|
10
12
|
if (object.parentId === groupId && object.type === 'text')
|
|
@@ -72,17 +74,25 @@ const executors = {
|
|
|
72
74
|
}
|
|
73
75
|
if (!clampInBounds(host, x, y, width, height))
|
|
74
76
|
return fail('bounds', 'Shape is outside the canvas.');
|
|
77
|
+
// The brand colours the assistant was told about: a bad colour is refused so the model can say it again.
|
|
78
|
+
const fill = readColorArgument(args.fill, 'fill', true);
|
|
79
|
+
if (!fill.ok)
|
|
80
|
+
return fail('invalid_args', fill.error);
|
|
81
|
+
const stroke = readColorArgument(args.stroke, 'stroke');
|
|
82
|
+
if (!stroke.ok)
|
|
83
|
+
return fail('invalid_args', stroke.error);
|
|
84
|
+
const paint = { ...(fill.value !== undefined ? { fill: fill.value } : {}), ...(stroke.value !== undefined ? { stroke: stroke.value } : {}) };
|
|
75
85
|
let object;
|
|
76
86
|
const label = typeof args.label === 'string' ? args.label : undefined;
|
|
77
87
|
if (shape === 'ellipse') {
|
|
78
|
-
object = createEllipseObject({ x, y, width, height });
|
|
88
|
+
object = createEllipseObject({ x, y, width, height, ...paint });
|
|
79
89
|
}
|
|
80
90
|
else if (shape === 'diamond') {
|
|
81
91
|
const path = `M ${width / 2} 0 L ${width} ${height / 2} L ${width / 2} ${height} L 0 ${height / 2} Z`;
|
|
82
|
-
object = createPathObject({ x, y, width, height, d: path, shape: 'diamond' });
|
|
92
|
+
object = createPathObject({ x, y, width, height, d: path, shape: 'diamond', ...paint });
|
|
83
93
|
}
|
|
84
94
|
else {
|
|
85
|
-
object = createRectObject({ x, y, width, height, roundness: shape === 'rounded' ? 16 : 0 });
|
|
95
|
+
object = createRectObject({ x, y, width, height, roundness: shape === 'rounded' ? 16 : 0, ...paint });
|
|
86
96
|
}
|
|
87
97
|
const objects = [object];
|
|
88
98
|
if (label) {
|
|
@@ -101,13 +111,79 @@ const executors = {
|
|
|
101
111
|
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
102
112
|
return fail('invalid_args', 'x and y are required.');
|
|
103
113
|
const width = Number(args.width ?? 200);
|
|
104
|
-
|
|
114
|
+
// The look: the brand fonts and colours the assistant was told about. Anything the canvas does not have is refused.
|
|
115
|
+
const fontFamily = readFontFamilyArgument(args.fontFamily, host.fontFamilies);
|
|
116
|
+
if (!fontFamily.ok)
|
|
117
|
+
return fail('invalid_args', fontFamily.error);
|
|
118
|
+
const color = readColorArgument(args.color, 'color');
|
|
119
|
+
if (!color.ok)
|
|
120
|
+
return fail('invalid_args', color.error);
|
|
121
|
+
const fontSize = readNumberArgument(args.fontSize, 'fontSize', 8, 200);
|
|
122
|
+
if (!fontSize.ok)
|
|
123
|
+
return fail('invalid_args', fontSize.error);
|
|
124
|
+
if (args.fontWeight !== undefined && args.fontWeight !== 'normal' && args.fontWeight !== 'bold') {
|
|
125
|
+
return fail('invalid_args', 'fontWeight must be "normal" or "bold".');
|
|
126
|
+
}
|
|
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 });
|
|
105
134
|
if (!clampInBounds(host, x, y, object.width, object.height))
|
|
106
135
|
return fail('bounds', 'Text is outside the canvas.');
|
|
107
136
|
const { scene, ids } = addToScene(host, [object]);
|
|
108
137
|
host.commitScene(scene);
|
|
109
138
|
return ok('Added text.', { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
110
139
|
},
|
|
140
|
+
add_asset(host, args) {
|
|
141
|
+
const assetId = typeof args.assetId === 'string' ? args.assetId.trim() : '';
|
|
142
|
+
if (!assetId)
|
|
143
|
+
return fail('invalid_args', 'assetId is required.');
|
|
144
|
+
const assets = host.assets ?? [];
|
|
145
|
+
if (assets.length === 0) {
|
|
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.');
|
|
147
|
+
}
|
|
148
|
+
const asset = assets.find((candidate) => candidate.id === assetId);
|
|
149
|
+
if (!asset) {
|
|
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 ? ', …' : ''}.`);
|
|
152
|
+
}
|
|
153
|
+
const x = Number(args.x);
|
|
154
|
+
const y = Number(args.y);
|
|
155
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
156
|
+
return fail('invalid_args', 'x and y are required.');
|
|
157
|
+
const width = readNumberArgument(args.width, 'width', 8, 2000);
|
|
158
|
+
if (!width.ok)
|
|
159
|
+
return fail('invalid_args', width.error);
|
|
160
|
+
const height = readNumberArgument(args.height, 'height', 8, 2000);
|
|
161
|
+
if (!height.ok)
|
|
162
|
+
return fail('invalid_args', height.error);
|
|
163
|
+
// The page built the asset list, but its pictures are drawn into the board like any other: only a loadable address goes in.
|
|
164
|
+
const src = readImageAddress(asset.src);
|
|
165
|
+
if (!src)
|
|
166
|
+
return fail('invalid_args', `Asset "${asset.id}" has no picture the canvas can draw.`);
|
|
167
|
+
const size = assetSizeFor(asset, width.value, height.value);
|
|
168
|
+
if (!clampInBounds(host, x, y, size.width, size.height))
|
|
169
|
+
return fail('bounds', 'Asset would sit outside the canvas.');
|
|
170
|
+
const object = createImageObject({
|
|
171
|
+
x,
|
|
172
|
+
y,
|
|
173
|
+
width: size.width,
|
|
174
|
+
height: size.height,
|
|
175
|
+
src,
|
|
176
|
+
fileName: asset.name.slice(0, 300),
|
|
177
|
+
// A library file keeps the library's id, so the canvas never puts it in the library a second time.
|
|
178
|
+
libraryFileId: asset.libraryFileId ?? null,
|
|
179
|
+
libraryFileUrl: readLibraryFileAddress(asset.libraryFileUrl),
|
|
180
|
+
naturalWidth: asset.width && asset.width > 0 ? asset.width : size.width,
|
|
181
|
+
naturalHeight: asset.height && asset.height > 0 ? asset.height : size.height
|
|
182
|
+
});
|
|
183
|
+
const { scene, ids } = addToScene(host, [object]);
|
|
184
|
+
host.commitScene(scene);
|
|
185
|
+
return ok(`Placed ${asset.name}.`, { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
186
|
+
},
|
|
111
187
|
add_arrow(host, args) {
|
|
112
188
|
let fromX = Number(args.fromX);
|
|
113
189
|
let fromY = Number(args.fromY);
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import type { EngineCore } from '../engine/EngineCore.js';
|
|
2
|
-
import type { CanvasBounds, CanvasToolHost } from './types.js';
|
|
2
|
+
import type { CanvasBounds, CanvasToolAsset, CanvasToolHost } from './types.js';
|
|
3
3
|
export interface EngineToolHostOptions {
|
|
4
4
|
engine: EngineCore;
|
|
5
5
|
bounds?: CanvasBounds;
|
|
6
6
|
/** When `transient`, scene updates apply without a new undo step until a `push` flush. */
|
|
7
7
|
commitMode?: 'push' | 'transient';
|
|
8
|
+
/** The pictures the assistant may place by id (`add_asset`). */
|
|
9
|
+
assets?: readonly CanvasToolAsset[];
|
|
10
|
+
/** The font families the assistant may use for a text. */
|
|
11
|
+
fontFamilies?: readonly string[];
|
|
8
12
|
}
|
|
9
13
|
/**
|
|
10
14
|
* Adapts EngineCore to CanvasToolHost: one `commit()` per assistant turn.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the sketch assistant may say about the look of what it draws, checked before anything reaches the board: a colour, a font,
|
|
3
|
+
* a size for a picture. The arguments come from a model, so each one is checked like input from outside: a bad value is refused
|
|
4
|
+
* with a sentence the model can act on (it retries), never silently turned into something else.
|
|
5
|
+
*
|
|
6
|
+
* Plain TypeScript: runs in Node.
|
|
7
|
+
*/
|
|
8
|
+
import type { CanvasToolAsset } from './types.js';
|
|
9
|
+
export type ArgumentResult<Value> = {
|
|
10
|
+
ok: true;
|
|
11
|
+
value: Value;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
error: string;
|
|
15
|
+
};
|
|
16
|
+
/** The colour in an argument: a hex colour (and `transparent` when `allowTransparent`). Absent when the model passed nothing. */
|
|
17
|
+
export declare function readColorArgument(value: unknown, name: string, allowTransparent?: boolean): ArgumentResult<string | undefined>;
|
|
18
|
+
/**
|
|
19
|
+
* The font family in an argument, as the canvas knows it. With a list of families (the built-in ones and the brand kit's) the name
|
|
20
|
+
* must be one of them (any spelling of the case); the answer is the family as the list writes it. Without a list any well-formed
|
|
21
|
+
* name is accepted. Absent when the model passed nothing.
|
|
22
|
+
*/
|
|
23
|
+
export declare function readFontFamilyArgument(value: unknown, known: readonly string[] | undefined): ArgumentResult<string | undefined>;
|
|
24
|
+
/** A number inside a range, or absent. Anything else is refused. */
|
|
25
|
+
export declare function readNumberArgument(value: unknown, name: string, minimum: number, maximum: number): ArgumentResult<number | undefined>;
|
|
26
|
+
/** The side a picture is given when the assistant does not say how big (an icon is a small thing). */
|
|
27
|
+
export declare const ASSET_DEFAULT_SIDE = 96;
|
|
28
|
+
/**
|
|
29
|
+
* The size a placed picture gets: both sides as asked; one side as asked and the other from the picture's proportions; or, with no
|
|
30
|
+
* size asked, the longer side is `ASSET_DEFAULT_SIDE`. The proportions are the picture's own when it says its size, a square when not.
|
|
31
|
+
*/
|
|
32
|
+
export declare function assetSizeFor(asset: Pick<CanvasToolAsset, 'width' | 'height'>, width: number | undefined, height: number | undefined): {
|
|
33
|
+
width: number;
|
|
34
|
+
height: number;
|
|
35
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the sketch assistant may say about the look of what it draws, checked before anything reaches the board: a colour, a font,
|
|
3
|
+
* a size for a picture. The arguments come from a model, so each one is checked like input from outside: a bad value is refused
|
|
4
|
+
* with a sentence the model can act on (it retries), never silently turned into something else.
|
|
5
|
+
*
|
|
6
|
+
* Plain TypeScript: runs in Node.
|
|
7
|
+
*/
|
|
8
|
+
import { isFontFamilyName } from '../engine/codec/canvasDocument.js';
|
|
9
|
+
/** A colour the assistant may pass: a hex colour (the brand kit's colours are hex), or `transparent` where a fill may be. */
|
|
10
|
+
const HEX_COLOR_PATTERN = /^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
11
|
+
/** The colour in an argument: a hex colour (and `transparent` when `allowTransparent`). Absent when the model passed nothing. */
|
|
12
|
+
export function readColorArgument(value, name, allowTransparent = false) {
|
|
13
|
+
if (value === undefined || value === null || value === '')
|
|
14
|
+
return { ok: true, value: undefined };
|
|
15
|
+
const text = typeof value === 'string' ? value.trim() : '';
|
|
16
|
+
if (HEX_COLOR_PATTERN.test(text))
|
|
17
|
+
return { ok: true, value: text.toLowerCase() };
|
|
18
|
+
if (allowTransparent && text.toLowerCase() === 'transparent')
|
|
19
|
+
return { ok: true, value: 'transparent' };
|
|
20
|
+
return { ok: false, error: `${name} must be a hex colour such as #1a2b3c${allowTransparent ? ' or "transparent"' : ''}, not ${JSON.stringify(value)}.` };
|
|
21
|
+
}
|
|
22
|
+
/** The first family of a CSS font list, without quotes, lower-cased: how two spellings of one family are compared. */
|
|
23
|
+
function familyKey(family) {
|
|
24
|
+
const first = family.split(',')[0] ?? '';
|
|
25
|
+
return first.trim().replace(/^["']|["']$/g, '').toLowerCase();
|
|
26
|
+
}
|
|
27
|
+
/** How many families a refusal lists (the model needs a few to choose from, not a hundred). */
|
|
28
|
+
const FONT_LIST_IN_REFUSAL = 24;
|
|
29
|
+
/**
|
|
30
|
+
* The font family in an argument, as the canvas knows it. With a list of families (the built-in ones and the brand kit's) the name
|
|
31
|
+
* must be one of them (any spelling of the case); the answer is the family as the list writes it. Without a list any well-formed
|
|
32
|
+
* name is accepted. Absent when the model passed nothing.
|
|
33
|
+
*/
|
|
34
|
+
export function readFontFamilyArgument(value, known) {
|
|
35
|
+
if (value === undefined || value === null || value === '')
|
|
36
|
+
return { ok: true, value: undefined };
|
|
37
|
+
const text = typeof value === 'string' ? value.trim() : '';
|
|
38
|
+
if (text === '' || !isFontFamilyName(text))
|
|
39
|
+
return { ok: false, error: `fontFamily must be a font family name, not ${JSON.stringify(value)}.` };
|
|
40
|
+
if (!known || known.length === 0)
|
|
41
|
+
return { ok: true, value: text };
|
|
42
|
+
const wanted = familyKey(text);
|
|
43
|
+
const match = known.find((family) => familyKey(family) === wanted);
|
|
44
|
+
if (match)
|
|
45
|
+
return { ok: true, value: match };
|
|
46
|
+
const listed = known.slice(0, FONT_LIST_IN_REFUSAL).join(', ');
|
|
47
|
+
return { ok: false, error: `The font "${text}" is not on this canvas. Use one of: ${listed}${known.length > FONT_LIST_IN_REFUSAL ? ', …' : ''}.` };
|
|
48
|
+
}
|
|
49
|
+
/** A number inside a range, or absent. Anything else is refused. */
|
|
50
|
+
export function readNumberArgument(value, name, minimum, maximum) {
|
|
51
|
+
if (value === undefined || value === null || value === '')
|
|
52
|
+
return { ok: true, value: undefined };
|
|
53
|
+
const number = typeof value === 'number' ? value : Number(value);
|
|
54
|
+
if (!Number.isFinite(number) || number < minimum || number > maximum) {
|
|
55
|
+
return { ok: false, error: `${name} must be a number from ${minimum} to ${maximum}, not ${JSON.stringify(value)}.` };
|
|
56
|
+
}
|
|
57
|
+
return { ok: true, value: number };
|
|
58
|
+
}
|
|
59
|
+
/** The side a picture is given when the assistant does not say how big (an icon is a small thing). */
|
|
60
|
+
export const ASSET_DEFAULT_SIDE = 96;
|
|
61
|
+
/**
|
|
62
|
+
* The size a placed picture gets: both sides as asked; one side as asked and the other from the picture's proportions; or, with no
|
|
63
|
+
* size asked, the longer side is `ASSET_DEFAULT_SIDE`. The proportions are the picture's own when it says its size, a square when not.
|
|
64
|
+
*/
|
|
65
|
+
export function assetSizeFor(asset, width, height) {
|
|
66
|
+
const known = typeof asset.width === 'number' && typeof asset.height === 'number' && asset.width > 0 && asset.height > 0;
|
|
67
|
+
const aspect = known ? asset.width / asset.height : 1;
|
|
68
|
+
let finalWidth;
|
|
69
|
+
let finalHeight;
|
|
70
|
+
if (width !== undefined && height !== undefined) {
|
|
71
|
+
finalWidth = width;
|
|
72
|
+
finalHeight = height;
|
|
73
|
+
}
|
|
74
|
+
else if (width !== undefined) {
|
|
75
|
+
finalWidth = width;
|
|
76
|
+
finalHeight = width / aspect;
|
|
77
|
+
}
|
|
78
|
+
else if (height !== undefined) {
|
|
79
|
+
finalHeight = height;
|
|
80
|
+
finalWidth = height * aspect;
|
|
81
|
+
}
|
|
82
|
+
else if (aspect >= 1) {
|
|
83
|
+
finalWidth = ASSET_DEFAULT_SIDE;
|
|
84
|
+
finalHeight = ASSET_DEFAULT_SIDE / aspect;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
finalHeight = ASSET_DEFAULT_SIDE;
|
|
88
|
+
finalWidth = ASSET_DEFAULT_SIDE * aspect;
|
|
89
|
+
}
|
|
90
|
+
return { width: Math.max(8, Math.round(finalWidth)), height: Math.max(8, Math.round(finalHeight)) };
|
|
91
|
+
}
|
package/dist/tools/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ObjectId, Rect } from '../engine/core/types.js';
|
|
2
2
|
import type { Scene } from '../engine/core/scene.js';
|
|
3
|
-
export declare const CANVAS_TOOL_NAMES: readonly ["get_canvas", "draw_ui_component", "add_shape", "add_text", "add_arrow", "move_elements", "delete_elements", "clear_canvas", "name_components_with_arrows"];
|
|
3
|
+
export declare const CANVAS_TOOL_NAMES: readonly ["get_canvas", "draw_ui_component", "add_shape", "add_text", "add_asset", "add_arrow", "move_elements", "delete_elements", "clear_canvas", "name_components_with_arrows"];
|
|
4
4
|
export type CanvasToolName = (typeof CANVAS_TOOL_NAMES)[number];
|
|
5
5
|
export interface CanvasBounds {
|
|
6
6
|
width: number;
|
|
@@ -34,8 +34,33 @@ export interface ToolFailure {
|
|
|
34
34
|
code: 'invalid_args' | 'not_found' | 'bounds' | 'internal';
|
|
35
35
|
}
|
|
36
36
|
export type ToolResult = ToolSuccess | ToolFailure;
|
|
37
|
+
/**
|
|
38
|
+
* A picture the sketch assistant may put on the board by its id: an icon of the brand kit, a file of the person's library. The page
|
|
39
|
+
* builds the list (it knows the brand kit and the library) and the board draws what the assistant names; the assistant is told the
|
|
40
|
+
* ids and names (never the pictures themselves) in its request.
|
|
41
|
+
*/
|
|
42
|
+
export interface CanvasToolAsset {
|
|
43
|
+
/** What the assistant calls it in `add_asset { assetId }`. Short and unique in the list. */
|
|
44
|
+
id: string;
|
|
45
|
+
/** A readable name (`logo.svg`, "Cart icon"). The picture keeps it as its file name, so the scene summary shows it. */
|
|
46
|
+
name: string;
|
|
47
|
+
/** Where it comes from: the brand kit's icons, or the person's file library (the File Browser). */
|
|
48
|
+
source: 'brand-icon' | 'library';
|
|
49
|
+
/** What the board draws: an http(s) address, a path of the page, or a `data:image/…` address (an SVG made from markup). */
|
|
50
|
+
src: string;
|
|
51
|
+
/** For a library file: the library's id and address, which the picture keeps (so it is never put in the library again). */
|
|
52
|
+
libraryFileId?: string | null;
|
|
53
|
+
libraryFileUrl?: string | null;
|
|
54
|
+
/** The size of the picture itself, when known, to keep its proportions. */
|
|
55
|
+
width?: number;
|
|
56
|
+
height?: number;
|
|
57
|
+
}
|
|
37
58
|
export interface CanvasToolHost {
|
|
38
59
|
readonly bounds: CanvasBounds;
|
|
60
|
+
/** The pictures `add_asset` may place. Missing or empty: the tool says there are none. */
|
|
61
|
+
readonly assets?: readonly CanvasToolAsset[];
|
|
62
|
+
/** The font families `add_text` may use (the built-in ones and the brand kit's). Missing or empty: any well-formed family name. */
|
|
63
|
+
readonly fontFamilies?: readonly string[];
|
|
39
64
|
getScene(): Scene;
|
|
40
65
|
/** Apply a new scene as one undo step. */
|
|
41
66
|
commitScene(next: Scene): void;
|
package/dist/tools/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@widgetic/canvas",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.46",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"svelte": "./dist/index.js",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"package": "NODE_ENV=production svelte-package",
|
|
60
60
|
"publish-to-npm": "node ../dev-tools/bin/publish-package.mjs",
|
|
61
61
|
"package:watch": "NODE_ENV=development svelte-package --watch",
|
|
62
|
-
"test": "node --test \"scripts/*.test.ts\" \"tests/*.test.mjs\" \"tests/engine/*.test.mjs\" \"tests/host/*.test.mjs\" \"tests/view/*.test.mjs\" \"tests/ui/*.test.mjs\" \"tests/tools/*.test.mjs\"",
|
|
62
|
+
"test": "node --test \"scripts/*.test.ts\" \"tests/*.test.mjs\" \"tests/engine/*.test.mjs\" \"tests/host/*.test.mjs\" \"tests/settings/*.test.mjs\" \"tests/view/*.test.mjs\" \"tests/ui/*.test.mjs\" \"tests/tools/*.test.mjs\"",
|
|
63
63
|
"preview": "vite preview",
|
|
64
64
|
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
65
65
|
"check:peers": "check-design-system-peers ..",
|