@widgetic/canvas 0.5.38 → 0.5.39
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 +5 -0
- package/dist/canvas/DomCanvas.svelte +26 -5
- package/dist/canvas/DomCanvas.svelte.d.ts +5 -0
- package/dist/canvas/autosaveScheduler.d.ts +10 -2
- package/dist/canvas/autosaveScheduler.js +31 -5
- package/dist/canvas/boardSurface.d.ts +5 -1
- package/dist/canvas/boardSurface.js +5 -1
- package/dist/canvas/chromeLayout.js +3 -1
- package/dist/canvas/host/CanvasHost.d.ts +32 -1
- package/dist/canvas/host/CanvasHost.js +124 -10
- package/dist/canvas/outsideClickDeselect.d.ts +5 -0
- package/dist/canvas/outsideClickDeselect.js +25 -0
- package/dist/canvas/preferredTextFont.d.ts +7 -1
- package/dist/canvas/preferredTextFont.js +26 -2
- package/dist/canvas/ui/CanvasHint.svelte +2 -2
- package/dist/canvas/ui/CanvasToolbar.svelte +1 -0
- package/dist/canvas/ui/ColorField.svelte +1 -0
- package/dist/canvas/ui/SelectionPanel.svelte +58 -23
- package/dist/canvas/ui/SelectorBar.svelte +5 -3
- package/dist/canvas/ui/sizeLabelVisibility.d.ts +13 -0
- package/dist/canvas/ui/sizeLabelVisibility.js +15 -0
- package/dist/canvas/view/SelectionOverlay.svelte +11 -1
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +1 -1
- package/dist/engine/EngineCore.d.ts +19 -2
- package/dist/engine/EngineCore.js +51 -7
- package/dist/engine/core/defaults.d.ts +1 -1
- package/dist/engine/core/defaults.js +1 -1
- package/dist/engine/core/shapeTools.d.ts +16 -6
- package/dist/engine/core/shapeTools.js +8 -6
- package/dist/engine/input/gestures.js +3 -2
- package/dist/engine/text/layoutText.d.ts +1 -2
- package/dist/engine/text/layoutText.js +2 -4
- package/dist/engine/text/textMetrics.d.ts +19 -0
- package/dist/engine/text/textMetrics.js +24 -0
- package/dist/fonts/interFont.d.ts +5 -0
- package/dist/fonts/interFont.js +31 -0
- package/dist/fonts/workspaceFonts.d.ts +12 -0
- package/dist/fonts/workspaceFonts.js +51 -0
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/messages.d.ts +3 -0
- package/dist/i18n/messages.js +3 -0
- package/dist/settings/workspaceToolSettings.d.ts +45 -0
- package/dist/settings/workspaceToolSettings.js +90 -0
- package/package.json +3 -3
|
@@ -5,22 +5,44 @@
|
|
|
5
5
|
import { getCanvasPreferenceStorage } from '../components/preferenceStorage.js';
|
|
6
6
|
/** The font families the selection panel offers for a text, in the order of its list. */
|
|
7
7
|
export const TEXT_FONT_FAMILIES = [
|
|
8
|
+
'Inter',
|
|
9
|
+
'Comic Sans MS',
|
|
8
10
|
'Arial',
|
|
9
11
|
'Helvetica',
|
|
10
12
|
'Times New Roman',
|
|
11
13
|
'Georgia',
|
|
12
14
|
'Verdana',
|
|
13
|
-
'Comic Sans MS',
|
|
14
15
|
'Courier New',
|
|
15
16
|
'Impact'
|
|
16
17
|
];
|
|
18
|
+
/** Families added from the workspace brand kit (not persisted in this list; merged in the panel). */
|
|
19
|
+
let workspaceFontFamilies = [];
|
|
20
|
+
export function setWorkspaceFontFamilies(families) {
|
|
21
|
+
workspaceFontFamilies = [...new Set(families.filter(Boolean))];
|
|
22
|
+
}
|
|
23
|
+
export function fontFamiliesForPicker() {
|
|
24
|
+
const merged = [...TEXT_FONT_FAMILIES];
|
|
25
|
+
for (const family of workspaceFontFamilies) {
|
|
26
|
+
if (!merged.includes(family))
|
|
27
|
+
merged.push(family);
|
|
28
|
+
}
|
|
29
|
+
return merged;
|
|
30
|
+
}
|
|
31
|
+
/** Inter + Comic Sans for the picker’s “Default fonts” group. */
|
|
32
|
+
export const PRIMARY_CANVAS_FONT_FAMILIES = ['Inter', 'Comic Sans MS'];
|
|
33
|
+
export function builtInFontFamiliesForPicker() {
|
|
34
|
+
return TEXT_FONT_FAMILIES.filter((family) => !PRIMARY_CANVAS_FONT_FAMILIES.includes(family));
|
|
35
|
+
}
|
|
36
|
+
export function workspaceFontFamiliesForPicker() {
|
|
37
|
+
return [...workspaceFontFamilies];
|
|
38
|
+
}
|
|
17
39
|
/** The key the choice is kept under. */
|
|
18
40
|
export const PREFERRED_TEXT_FONT_STORAGE_KEY = 'widgetic-canvas:preferred-text-font';
|
|
19
41
|
/** The font family kept from an earlier choice, or null when none was made (or the store refuses, or the name is no longer offered). */
|
|
20
42
|
export function readPreferredTextFontFamily() {
|
|
21
43
|
try {
|
|
22
44
|
const stored = getCanvasPreferenceStorage()?.getItem(PREFERRED_TEXT_FONT_STORAGE_KEY);
|
|
23
|
-
return stored &&
|
|
45
|
+
return stored && fontFamiliesForPicker().includes(stored) ? stored : null;
|
|
24
46
|
}
|
|
25
47
|
catch {
|
|
26
48
|
// A store that blocks reading is the same as a store with nothing in it.
|
|
@@ -29,6 +51,8 @@ export function readPreferredTextFontFamily() {
|
|
|
29
51
|
}
|
|
30
52
|
/** Keeps the choice for the next text. A store that refuses the write is not an error: the choice then lasts until the page closes. */
|
|
31
53
|
export function writePreferredTextFontFamily(fontFamily) {
|
|
54
|
+
if (!fontFamiliesForPicker().includes(fontFamily))
|
|
55
|
+
return;
|
|
32
56
|
try {
|
|
33
57
|
getCanvasPreferenceStorage()?.setItem(PREFERRED_TEXT_FONT_STORAGE_KEY, fontFamily);
|
|
34
58
|
}
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
|
|
13
13
|
<div class="pointer-events-none absolute inset-0 flex items-center justify-center p-6" data-canvas-hint>
|
|
14
14
|
<div class="flex w-80 max-w-full flex-col items-center gap-3 text-center">
|
|
15
|
-
<div class="flex size-
|
|
16
|
-
<IconPencil class="size-
|
|
15
|
+
<div class="flex size-9 items-center justify-center rounded-lg border border-border bg-background text-foreground">
|
|
16
|
+
<IconPencil class="size-4 stroke-[1.333]" aria-hidden="true" />
|
|
17
17
|
</div>
|
|
18
18
|
<p class="text-body-medium text-foreground">{$t('canvas.hint.title')}</p>
|
|
19
19
|
<p class="text-body text-muted-foreground">{$t('canvas.hint.description')}</p>
|
|
@@ -53,6 +53,7 @@
|
|
|
53
53
|
data-canvas-toolbar
|
|
54
54
|
data-anchor={anchor}
|
|
55
55
|
class="canvas-toolbar-el pointer-events-auto flex items-center gap-1 {vertical ? 'max-h-full flex-col overflow-y-auto' : 'max-w-full'} {FLOATING_CARD_CLASSES}"
|
|
56
|
+
data-canvas-ui
|
|
56
57
|
>
|
|
57
58
|
{#if showDrawingTools}
|
|
58
59
|
<ToolButton toggle label={$t('canvas.tool.select')} shortcut={TOOL_KEYS.select} active={state.tool === 'select'} onclick={() => host.onSelectSelectTool()}>
|
|
@@ -120,6 +120,7 @@
|
|
|
120
120
|
onInteractOutside={remember}
|
|
121
121
|
onCloseAutoFocus={returnFocusToField}
|
|
122
122
|
class="{CANVAS_OVERLAY_Z_CLASS} w-64 p-0 shadow-floating data-[state=open]:!animate-none data-[state=closed]:!animate-none data-[state=open]:!fade-in-0 data-[state=closed]:!fade-out-0 data-[state=open]:!duration-0 data-[state=closed]:!duration-0 {OVERLAY_REDUCED_MOTION_CLASSES}"
|
|
123
|
+
data-canvas-ui
|
|
123
124
|
data-canvas-color-picker
|
|
124
125
|
>
|
|
125
126
|
{#if allowNone}
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
<script lang="ts">
|
|
19
19
|
import { onDestroy } from 'svelte';
|
|
20
20
|
import type { Attachment } from 'svelte/attachments';
|
|
21
|
-
import { IconButton, Select, Slider
|
|
21
|
+
import { IconButton, NumberStepper, Select, Slider } from '@widgetic/design-system/components';
|
|
22
22
|
import type { StylePatch } from '../../engine/core/style.js';
|
|
23
23
|
import type { EngineState } from '../../engine/state.js';
|
|
24
24
|
import { useCanvasLanguage } from '../../i18n/language.js';
|
|
@@ -29,7 +29,12 @@
|
|
|
29
29
|
import { CANVAS_OVERLAY_Z_CLASS, FLOATING_CARD_CLASSES, OVERLAY_REDUCED_MOTION_CLASSES } from './floatingCard.js';
|
|
30
30
|
import { canvasOverlayPortalProps, useFloatingLayer } from './floatingLayer.js';
|
|
31
31
|
import { IconAlignCenter, IconAlignLeft, IconAlignRight, IconBold, IconItalic, IconSettings, IconUnderline, IconX } from './icons.js';
|
|
32
|
-
import {
|
|
32
|
+
import {
|
|
33
|
+
builtInFontFamiliesForPicker,
|
|
34
|
+
PRIMARY_CANVAS_FONT_FAMILIES,
|
|
35
|
+
workspaceFontFamiliesForPicker,
|
|
36
|
+
writePreferredTextFontFamily
|
|
37
|
+
} from '../preferredTextFont.js';
|
|
33
38
|
import { describeSelection, formatFontSizeDisplay, showFillColorRow, showStrokeColorRow, traitNumber } from './selectionTraits.js';
|
|
34
39
|
import ToolButton from './ToolButton.svelte';
|
|
35
40
|
import WidgetActions from './WidgetActions.svelte';
|
|
@@ -69,7 +74,8 @@
|
|
|
69
74
|
);
|
|
70
75
|
|
|
71
76
|
// The font of a text, offered as the old panel offered it: eight families that every screen has.
|
|
72
|
-
const
|
|
77
|
+
const WORKSPACE_FONT_FAMILIES = $derived(workspaceFontFamiliesForPicker());
|
|
78
|
+
const BUILTIN_FONT_FAMILIES = $derived(builtInFontFamiliesForPicker());
|
|
73
79
|
|
|
74
80
|
// ── changes
|
|
75
81
|
// A slider or a picker that is still being dragged changes the board without a step of the history; the step is taken when
|
|
@@ -87,7 +93,10 @@
|
|
|
87
93
|
try {
|
|
88
94
|
host.engine.setStyle(patch, { commit });
|
|
89
95
|
stopCommitTimer();
|
|
90
|
-
if (commit)
|
|
96
|
+
if (commit) {
|
|
97
|
+
host.engine.commit();
|
|
98
|
+
host.persistWorkspaceStyle(patch);
|
|
99
|
+
}
|
|
91
100
|
else commitTimer = setTimeout(() => host.engine.commit(), COMMIT_AFTER_MS);
|
|
92
101
|
} catch (error) {
|
|
93
102
|
console.warn('Canvas: the selection panel could not change the selection', error);
|
|
@@ -131,12 +140,6 @@
|
|
|
131
140
|
changeStyle({ fill: color, color }, commit);
|
|
132
141
|
}
|
|
133
142
|
|
|
134
|
-
function changeStep(current: number, delta: number, min: number, max: number, key: 'strokeWidth' | 'fontSize'): void {
|
|
135
|
-
const stepped = key === 'fontSize' ? Math.round(current + delta) : Math.round((current + delta) * 10) / 10;
|
|
136
|
-
const next = Math.min(max, Math.max(min, stepped));
|
|
137
|
-
changeStyle({ [key]: next } as StylePatch);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
143
|
/** Changes the font of the selected text, and keeps it as the font of the next texts (also after the page is opened again). */
|
|
141
144
|
function chooseFontFamily(fontFamily: string): void {
|
|
142
145
|
changeStyle({ fontFamily });
|
|
@@ -173,6 +176,7 @@
|
|
|
173
176
|
{#if showPanel && !ui.selectionPanelDismissed}
|
|
174
177
|
<aside
|
|
175
178
|
aria-label={$t('canvas.panel.label')}
|
|
179
|
+
data-canvas-ui
|
|
176
180
|
data-canvas-selection-panel
|
|
177
181
|
class="pointer-events-auto absolute end-4 z-20 flex w-[336px] flex-col gap-3 overflow-y-auto rounded-card border border-border bg-popover p-4 text-foreground shadow-floating {clearance.belowTopCards ? 'top-16 max-h-[calc(100%-5rem)]' : 'top-4 max-h-[calc(100%-2rem)]'} {PANEL_MAX_WIDTH_CLASSES[clearance.besideSideCard ?? 'none']}"
|
|
178
182
|
style:inset-inline-end={clearance.besideSideCard ? PANEL_END_BESIDE_SIDE_CARD[clearance.besideSideCard] : undefined}
|
|
@@ -203,12 +207,19 @@
|
|
|
203
207
|
{#if traits.strokeWidth.kind !== 'none'}
|
|
204
208
|
<div class="flex h-9 items-center justify-between gap-3">
|
|
205
209
|
<span class="text-body">{$t('canvas.panel.strokeWidth')}</span>
|
|
206
|
-
<!-- The
|
|
207
|
-
<
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
210
|
+
<!-- The design system's number stepper (IC Number): the value can be typed or stepped, and the group names the two buttons after the row. -->
|
|
211
|
+
<NumberStepper
|
|
212
|
+
class="w-[185px]"
|
|
213
|
+
label={$t('canvas.panel.strokeWidth')}
|
|
214
|
+
decreaseLabel={$t('canvas.panel.decrease')}
|
|
215
|
+
increaseLabel={$t('canvas.panel.increase')}
|
|
216
|
+
value={traits.strokeWidth.kind === 'same' ? Number(strokeWidthValue.toFixed(1)) : null}
|
|
217
|
+
fallbackValue={strokeWidthValue}
|
|
218
|
+
min={0}
|
|
219
|
+
max={200}
|
|
220
|
+
step={1}
|
|
221
|
+
onChange={(next) => changeStyle({ strokeWidth: next })}
|
|
222
|
+
/>
|
|
212
223
|
</div>
|
|
213
224
|
{/if}
|
|
214
225
|
|
|
@@ -251,21 +262,45 @@
|
|
|
251
262
|
>
|
|
252
263
|
{traits.text.fontFamily.kind === 'same' ? traits.text.fontFamily.value : $t('canvas.panel.mixed')}
|
|
253
264
|
</Select.Trigger>
|
|
254
|
-
<Select.Content
|
|
255
|
-
{
|
|
265
|
+
<Select.Content
|
|
266
|
+
portalProps={canvasOverlayPortalProps($floatingLayer)}
|
|
267
|
+
class="{CANVAS_OVERLAY_Z_CLASS} {OVERLAY_REDUCED_MOTION_CLASSES}"
|
|
268
|
+
data-canvas-ui
|
|
269
|
+
>
|
|
270
|
+
<p class="text-caption text-muted-foreground px-2 py-1">{$t('canvas.panel.fontGroupDefault')}</p>
|
|
271
|
+
{#each PRIMARY_CANVAS_FONT_FAMILIES as family (family)}
|
|
256
272
|
<Select.Item value={family} label={family}>{family}</Select.Item>
|
|
257
273
|
{/each}
|
|
274
|
+
{#if BUILTIN_FONT_FAMILIES.length > 0}
|
|
275
|
+
<p class="text-caption text-muted-foreground px-2 py-1 pt-2">{$t('canvas.panel.fontGroupMore')}</p>
|
|
276
|
+
{#each BUILTIN_FONT_FAMILIES as family (family)}
|
|
277
|
+
<Select.Item value={family} label={family}>{family}</Select.Item>
|
|
278
|
+
{/each}
|
|
279
|
+
{/if}
|
|
280
|
+
{#if WORKSPACE_FONT_FAMILIES.length > 0}
|
|
281
|
+
<p class="text-caption text-muted-foreground px-2 py-1 pt-2">{$t('canvas.panel.fontGroupWorkspace')}</p>
|
|
282
|
+
{#each WORKSPACE_FONT_FAMILIES as family (family)}
|
|
283
|
+
<Select.Item value={family} label={family}>{family}</Select.Item>
|
|
284
|
+
{/each}
|
|
285
|
+
{/if}
|
|
258
286
|
</Select.Content>
|
|
259
287
|
</Select.Root>
|
|
260
288
|
</div>
|
|
261
289
|
|
|
262
290
|
<div class="flex h-9 items-center justify-between gap-3">
|
|
263
291
|
<span class="text-body">{$t('canvas.panel.fontSize')}</span>
|
|
264
|
-
<
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
292
|
+
<NumberStepper
|
|
293
|
+
class="w-[185px]"
|
|
294
|
+
label={$t('canvas.panel.fontSize')}
|
|
295
|
+
decreaseLabel={$t('canvas.panel.decrease')}
|
|
296
|
+
increaseLabel={$t('canvas.panel.increase')}
|
|
297
|
+
value={typeof fontSizeDisplay === 'number' ? fontSizeDisplay : null}
|
|
298
|
+
fallbackValue={fontSizeValue}
|
|
299
|
+
min={8}
|
|
300
|
+
max={200}
|
|
301
|
+
step={1}
|
|
302
|
+
onChange={(next) => changeStyle({ fontSize: next })}
|
|
303
|
+
/>
|
|
269
304
|
</div>
|
|
270
305
|
|
|
271
306
|
<div class="flex items-center justify-between gap-1" role="group" aria-label={$t('canvas.control.textStyle')}>
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
The element selector bar: what stands where the toolbar stood once the page has hidden the drawing tools (after the first
|
|
3
3
|
generation) and asks for the selector instead.
|
|
4
4
|
|
|
5
|
-
One toggle that puts the page's element selector on or off, and,
|
|
6
|
-
|
|
5
|
+
One toggle that puts the page's element selector on or off, and, whenever the page gave its words, the line that says how to
|
|
6
|
+
use it (click, the arrow keys to move between parent and child, a held key to use the widget, Escape to finish). The bar has
|
|
7
|
+
one form only, the open one: the instructions are there before the toggle is pressed, not after (it used to shrink to the
|
|
8
|
+
toggle while the selector was off and grow when it was on).
|
|
7
9
|
The words come from the page (`selectorGuide`), already in its language; the key caps are the keys themselves.
|
|
8
10
|
|
|
9
11
|
The bar stands where the toolbar stands (`toolbarPosition`). On the left or the right edge it stands upright, and the line
|
|
@@ -54,7 +56,7 @@
|
|
|
54
56
|
<ToolButton toggle label={$t('canvas.tool.selector')} active={active} onclick={onToggle}>
|
|
55
57
|
<IconSelectElement />
|
|
56
58
|
</ToolButton>
|
|
57
|
-
{#if
|
|
59
|
+
{#if guide}
|
|
58
60
|
{#if vertical}
|
|
59
61
|
<div class="flex flex-col items-start gap-1.5 px-1 pb-1.5 text-caption-medium text-muted-foreground">
|
|
60
62
|
<span>{guide.click}</span>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When the size label under a selection is visible (item B21).
|
|
3
|
+
*/
|
|
4
|
+
import type { SelectionDrag } from '../../engine/state.js';
|
|
5
|
+
export interface SizeLabelVisibilityInput {
|
|
6
|
+
hasFrame: boolean;
|
|
7
|
+
/** Any gesture is active (press on body, handle, dot, …). */
|
|
8
|
+
gestureKind: string | null;
|
|
9
|
+
/** Non-null only after the pointer crossed the drag threshold. */
|
|
10
|
+
overlayDragging: SelectionDrag | null;
|
|
11
|
+
}
|
|
12
|
+
/** True while the selection is at rest, or between press and the drag threshold. */
|
|
13
|
+
export declare function shouldShowRestingSizeLabel(input: SizeLabelVisibilityInput): boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When the size label under a selection is visible (item B21).
|
|
3
|
+
*/
|
|
4
|
+
/** True while the selection is at rest, or between press and the drag threshold. */
|
|
5
|
+
export function shouldShowRestingSizeLabel(input) {
|
|
6
|
+
if (!input.hasFrame)
|
|
7
|
+
return false;
|
|
8
|
+
if (input.overlayDragging === 'move' || input.overlayDragging === 'rotate')
|
|
9
|
+
return false;
|
|
10
|
+
if (input.overlayDragging === 'resize')
|
|
11
|
+
return true;
|
|
12
|
+
if (input.gestureKind === 'resize' || input.gestureKind === 'rotate')
|
|
13
|
+
return true;
|
|
14
|
+
return input.gestureKind === null || input.gestureKind === 'move';
|
|
15
|
+
}
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
import type { EngineState } from '../../engine/state.js';
|
|
33
33
|
import { worldToScreen } from '../../engine/core/viewport.js';
|
|
34
34
|
import type { CanvasHost } from '../host/CanvasHost.js';
|
|
35
|
+
import { shouldShowRestingSizeLabel } from '../ui/sizeLabelVisibility.js';
|
|
35
36
|
import {
|
|
36
37
|
arrowOutlinePlacement,
|
|
37
38
|
cssCursor,
|
|
@@ -200,7 +201,16 @@
|
|
|
200
201
|
|
|
201
202
|
// The size sits under the frame, at rest and while the frame is stretched, so it does not cover the widget.
|
|
202
203
|
const sizeLabel = $derived.by(() => {
|
|
203
|
-
if (
|
|
204
|
+
if (
|
|
205
|
+
!shouldShowRestingSizeLabel({
|
|
206
|
+
hasFrame: !!frame,
|
|
207
|
+
gestureKind,
|
|
208
|
+
overlayDragging: state.overlay.dragging
|
|
209
|
+
})
|
|
210
|
+
) {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
if (!frame) return null;
|
|
204
214
|
const text = formatReadout({ kind: 'size', width: Math.round(frame.world.width), height: Math.round(frame.world.height) });
|
|
205
215
|
const at = restingSizeLabelPoint(frame.screen, text);
|
|
206
216
|
if (!at) return null;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Colour of an empty board: the design system's `muted` token, the same the board is painted with. */
|
|
2
|
-
export declare const CANVAS_BOARD_BACKGROUND = "var(--muted, #f5f5f5)";
|
|
2
|
+
export declare const CANVAS_BOARD_BACKGROUND = "var(--canvas-surface, var(--muted, #f5f5f5))";
|
|
3
3
|
/**
|
|
4
4
|
* The empty board, drawn before the canvas itself is there.
|
|
5
5
|
*
|
|
@@ -16,7 +16,7 @@ import type { ArrowHandleId } from './core/arrow.js';
|
|
|
16
16
|
import type { ReorderMode, Scene } from './core/scene.js';
|
|
17
17
|
import type { StylePatch } from './core/style.js';
|
|
18
18
|
import type { SelectionFrame } from './core/transform.js';
|
|
19
|
-
import type { ObjectId, Point, Rect, SceneObject, ShapeObject, Size, Viewport, WidgetObject } from './core/types.js';
|
|
19
|
+
import type { ObjectId, Point, Rect, RectObject, SceneObject, ShapeObject, Size, TextObject, Viewport, WidgetObject } from './core/types.js';
|
|
20
20
|
import type { EngineOverlay, EngineState, PendingWidgetState, ToolId, WidgetRuntime } from './state.js';
|
|
21
21
|
import type { MeasureText } from './text/layoutText.js';
|
|
22
22
|
/** How many undo steps the board keeps (as the canvas it replaces did). */
|
|
@@ -45,6 +45,13 @@ export interface EngineServices {
|
|
|
45
45
|
export interface EngineHooks {
|
|
46
46
|
/** The person changed the board: it needs saving. */
|
|
47
47
|
onDirty?: () => void;
|
|
48
|
+
/**
|
|
49
|
+
* The letters of a text that is still being typed changed. It is not an undo step yet (that comes when the typing ends,
|
|
50
|
+
* and then `onDirty` is called), but it is on the board, so it needs saving too: a person who types a sentence and
|
|
51
|
+
* closes the page must not find the board without it. Called after every change of the letters, and once more when an
|
|
52
|
+
* empty new text is dropped after something was typed in it (the save that held the typed letters is out of date).
|
|
53
|
+
*/
|
|
54
|
+
onDraftChange?: () => void;
|
|
48
55
|
/** The board was emptied by "clear all". */
|
|
49
56
|
onCleared?: () => void;
|
|
50
57
|
/** The selection changed (a selection, a deselection, after a delete or an undo). */
|
|
@@ -102,7 +109,12 @@ export declare class EngineCore {
|
|
|
102
109
|
/** Forgets what can be undone (after the host replaced the board). */
|
|
103
110
|
resetHistory(): void;
|
|
104
111
|
setLoading(loading: boolean): void;
|
|
105
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Tells the engine how big the board element is. With `keepCentre` the view stays on the same point of the world: when
|
|
114
|
+
* the board grows or shrinks, the pan moves by half of the change, so what was in the middle stays in the middle (a board
|
|
115
|
+
* that its page resizes by dragging its edges). The first measure (from 0 by 0) never moves the pan, whatever `keepCentre` says.
|
|
116
|
+
*/
|
|
117
|
+
setViewSize(size: Size, keepCentre?: boolean): void;
|
|
106
118
|
/**
|
|
107
119
|
* Puts a new scene on the board. `transient` is for the frames of a gesture (nothing is recorded), `push`
|
|
108
120
|
* records one undo step and tells the host the board changed, `replace` changes the current step.
|
|
@@ -201,6 +213,11 @@ export declare class EngineCore {
|
|
|
201
213
|
notifyFontsChanged(): void;
|
|
202
214
|
/** Remembers the font family for the next text object the person places. */
|
|
203
215
|
setPreferredTextFontFamily(fontFamily: string): void;
|
|
216
|
+
setWorkspaceToolDefaults(settings: {
|
|
217
|
+
text?: Partial<Pick<TextObject, 'fontFamily' | 'fontSize' | 'fontWeight' | 'fontStyle' | 'color' | 'align' | 'underline'>> | null;
|
|
218
|
+
paint?: Partial<Pick<RectObject, 'fill' | 'stroke' | 'strokeWidth' | 'strokeStyle' | 'opacity' | 'roundness'>> | null;
|
|
219
|
+
}): void;
|
|
220
|
+
workspacePaintForNewShape(wireframe: boolean): Partial<RectObject>;
|
|
204
221
|
/**
|
|
205
222
|
* Makes a text object, puts it on the board and starts editing it. A click makes a label whose width follows its
|
|
206
223
|
* text; a width makes a box whose lines wrap. Nothing is recorded until the editing ends with text in it.
|
|
@@ -66,6 +66,8 @@ export class EngineCore {
|
|
|
66
66
|
#textSession = null;
|
|
67
67
|
/** The last font family chosen in the panel; new text objects start with this instead of the catalog default. */
|
|
68
68
|
#preferredTextFontFamily = null;
|
|
69
|
+
#workspaceTextDefaults = null;
|
|
70
|
+
#workspacePaintDefaults = null;
|
|
69
71
|
// The eraser's trail while it lives; what the views draw of it is `state.scribble`.
|
|
70
72
|
#scribble = null;
|
|
71
73
|
#indexes = new WeakMap();
|
|
@@ -240,11 +242,25 @@ export class EngineCore {
|
|
|
240
242
|
setLoading(loading) {
|
|
241
243
|
this.#set({ loading });
|
|
242
244
|
}
|
|
243
|
-
|
|
245
|
+
/**
|
|
246
|
+
* Tells the engine how big the board element is. With `keepCentre` the view stays on the same point of the world: when
|
|
247
|
+
* the board grows or shrinks, the pan moves by half of the change, so what was in the middle stays in the middle (a board
|
|
248
|
+
* that its page resizes by dragging its edges). The first measure (from 0 by 0) never moves the pan, whatever `keepCentre` says.
|
|
249
|
+
*/
|
|
250
|
+
setViewSize(size, keepCentre = false) {
|
|
244
251
|
const current = this.#state.viewSize;
|
|
245
252
|
if (current.width === size.width && current.height === size.height)
|
|
246
253
|
return;
|
|
247
|
-
|
|
254
|
+
const patch = { viewSize: { width: size.width, height: size.height } };
|
|
255
|
+
if (keepCentre && current.width > 0 && current.height > 0 && size.width > 0 && size.height > 0) {
|
|
256
|
+
const viewport = this.#state.viewport;
|
|
257
|
+
patch.viewport = {
|
|
258
|
+
...viewport,
|
|
259
|
+
x: viewport.x + (size.width - current.width) / 2,
|
|
260
|
+
y: viewport.y + (size.height - current.height) / 2
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
this.#set(patch);
|
|
248
264
|
}
|
|
249
265
|
// ──────────────────────────────────────────────────────── scene changes
|
|
250
266
|
/**
|
|
@@ -672,6 +688,26 @@ export class EngineCore {
|
|
|
672
688
|
setPreferredTextFontFamily(fontFamily) {
|
|
673
689
|
this.#preferredTextFontFamily = fontFamily;
|
|
674
690
|
}
|
|
691
|
+
setWorkspaceToolDefaults(settings) {
|
|
692
|
+
this.#workspaceTextDefaults = settings.text ?? null;
|
|
693
|
+
this.#workspacePaintDefaults = settings.paint ?? null;
|
|
694
|
+
if (settings.text?.fontFamily)
|
|
695
|
+
this.#preferredTextFontFamily = settings.text.fontFamily;
|
|
696
|
+
}
|
|
697
|
+
workspacePaintForNewShape(wireframe) {
|
|
698
|
+
if (!this.#workspacePaintDefaults)
|
|
699
|
+
return {};
|
|
700
|
+
if (wireframe) {
|
|
701
|
+
const { stroke, strokeWidth, strokeStyle, opacity } = this.#workspacePaintDefaults;
|
|
702
|
+
return {
|
|
703
|
+
...(stroke !== undefined ? { stroke } : {}),
|
|
704
|
+
...(strokeWidth !== undefined ? { strokeWidth } : {}),
|
|
705
|
+
...(strokeStyle !== undefined ? { strokeStyle } : {}),
|
|
706
|
+
...(opacity !== undefined ? { opacity } : {})
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
return { ...this.#workspacePaintDefaults };
|
|
710
|
+
}
|
|
675
711
|
/**
|
|
676
712
|
* Makes a text object, puts it on the board and starts editing it. A click makes a label whose width follows its
|
|
677
713
|
* text; a width makes a box whose lines wrap. Nothing is recorded until the editing ends with text in it.
|
|
@@ -685,12 +721,13 @@ export class EngineCore {
|
|
|
685
721
|
y: point.y,
|
|
686
722
|
width: fixedWidth ? options.width : TEXT_DEFAULTS.minimumWidth,
|
|
687
723
|
autoWidth: !fixedWidth,
|
|
688
|
-
fontFamily: this.#preferredTextFontFamily ?? TEXT_DEFAULTS.fontFamily
|
|
724
|
+
fontFamily: this.#preferredTextFontFamily ?? this.#workspaceTextDefaults?.fontFamily ?? TEXT_DEFAULTS.fontFamily,
|
|
725
|
+
...this.#workspaceTextDefaults
|
|
689
726
|
});
|
|
690
727
|
const text = this.#fitText(base);
|
|
691
728
|
const sceneBefore = this.#state.scene;
|
|
692
729
|
this.applyScene([...sceneBefore, text], 'transient');
|
|
693
|
-
this.#textSession = { id: text.id, isNew: true, sceneBefore };
|
|
730
|
+
this.#textSession = { id: text.id, isNew: true, sceneBefore, typed: false };
|
|
694
731
|
this.#set({ editingTextId: text.id, selectedIds: [text.id], enteredGroupId: null });
|
|
695
732
|
return text.id;
|
|
696
733
|
}
|
|
@@ -702,7 +739,7 @@ export class EngineCore {
|
|
|
702
739
|
if (this.#textSession?.id === id)
|
|
703
740
|
return true;
|
|
704
741
|
this.endTextEdit();
|
|
705
|
-
this.#textSession = { id, isNew: false, sceneBefore: this.#state.scene };
|
|
742
|
+
this.#textSession = { id, isNew: false, sceneBefore: this.#state.scene, typed: false };
|
|
706
743
|
this.#set({ editingTextId: id, selectedIds: [id] });
|
|
707
744
|
return true;
|
|
708
745
|
}
|
|
@@ -717,8 +754,12 @@ export class EngineCore {
|
|
|
717
754
|
? fitTextObject({ ...object, text: clipped }, this.services.measureText)
|
|
718
755
|
: object);
|
|
719
756
|
const changed = next.some((object, position) => object !== scene[position]);
|
|
720
|
-
if (changed)
|
|
721
|
-
|
|
757
|
+
if (!changed)
|
|
758
|
+
return;
|
|
759
|
+
this.applyScene(next, 'transient');
|
|
760
|
+
session.typed = true;
|
|
761
|
+
// The board is not recorded until the typing ends, but the letters are on it: the host's next save takes them.
|
|
762
|
+
this.#callHook(() => this.hooks.onDraftChange?.(), 'draft change hook');
|
|
722
763
|
}
|
|
723
764
|
/**
|
|
724
765
|
* Finishes the edit. An empty text is removed (a new one leaves nothing behind, not even an undo step);
|
|
@@ -737,6 +778,9 @@ export class EngineCore {
|
|
|
737
778
|
if (session.isNew) {
|
|
738
779
|
this.applyScene(session.sceneBefore, 'transient');
|
|
739
780
|
this.#set({ selectedIds: [] });
|
|
781
|
+
// A save may have taken the letters that were typed before they were all deleted: it has to be told they are gone.
|
|
782
|
+
if (session.typed)
|
|
783
|
+
this.#callHook(() => this.hooks.onDraftChange?.(), 'draft change hook');
|
|
740
784
|
}
|
|
741
785
|
else {
|
|
742
786
|
this.applyScene(removeFromScene(this.#state.scene, [session.id]), 'push');
|
|
@@ -18,7 +18,7 @@ export declare const SHAPE_STYLE_DEFAULTS: {
|
|
|
18
18
|
};
|
|
19
19
|
export declare const TEXT_DEFAULTS: {
|
|
20
20
|
readonly fontSize: 20;
|
|
21
|
-
readonly fontFamily: "
|
|
21
|
+
readonly fontFamily: "Inter";
|
|
22
22
|
readonly color: "#111827";
|
|
23
23
|
/** The colour of the "Type something" prompt shown in an empty text box. */
|
|
24
24
|
readonly placeholderColor: "#9ca3af";
|
|
@@ -17,7 +17,7 @@ export const SHAPE_STYLE_DEFAULTS = {
|
|
|
17
17
|
};
|
|
18
18
|
export const TEXT_DEFAULTS = {
|
|
19
19
|
fontSize: 20,
|
|
20
|
-
fontFamily: '
|
|
20
|
+
fontFamily: 'Inter',
|
|
21
21
|
color: '#111827',
|
|
22
22
|
/** The colour of the "Type something" prompt shown in an empty text box. */
|
|
23
23
|
placeholderColor: '#9ca3af',
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* once more on release for the shape that is kept.
|
|
5
5
|
*/
|
|
6
6
|
import type { EllipseObject, ObjectId, PathObject, Point, Rect, RectObject } from './types.js';
|
|
7
|
+
type WorkspaceShapePaint = Partial<Pick<RectObject, 'fill' | 'stroke' | 'strokeWidth' | 'strokeStyle' | 'opacity' | 'roundness'>>;
|
|
7
8
|
/** A drag shorter than this (in screen pixels) is a click: the shape is placed at its default size. */
|
|
8
9
|
export declare const MIN_DRAW_DISTANCE = 4;
|
|
9
10
|
/** The size of a library shape placed by a click. */
|
|
@@ -39,16 +40,24 @@ export interface ShapeDraftOptions {
|
|
|
39
40
|
wireframe: boolean;
|
|
40
41
|
createId?: (prefix: string) => ObjectId;
|
|
41
42
|
random?: () => number;
|
|
43
|
+
/** Remembered workspace paint for new shapes (B16). */
|
|
44
|
+
workspacePaint?: WorkspaceShapePaint;
|
|
42
45
|
}
|
|
43
46
|
/** The paint of a new shape of the tools, by the look the board is in. */
|
|
44
|
-
export declare function newShapePaint(wireframe: boolean, random?: () => number): {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
export declare function newShapePaint(wireframe: boolean, random?: () => number, workspacePaint?: WorkspaceShapePaint): {
|
|
48
|
+
roundness?: number | undefined;
|
|
49
|
+
opacity?: number | undefined;
|
|
50
|
+
fill: string;
|
|
51
|
+
stroke: string;
|
|
52
|
+
strokeWidth: number;
|
|
53
|
+
strokeStyle?: import("./types.js").StrokeStyle | undefined;
|
|
48
54
|
} | {
|
|
55
|
+
roundness?: number | undefined;
|
|
56
|
+
opacity?: number | undefined;
|
|
49
57
|
fill: string;
|
|
50
|
-
stroke:
|
|
51
|
-
strokeWidth:
|
|
58
|
+
stroke: string;
|
|
59
|
+
strokeWidth: number;
|
|
60
|
+
strokeStyle?: import("./types.js").StrokeStyle | undefined;
|
|
52
61
|
};
|
|
53
62
|
/** The look of a library shape, and of an arrow. */
|
|
54
63
|
export declare const LIBRARY_SHAPE_PAINT: {
|
|
@@ -78,3 +87,4 @@ export declare function buildLibraryShape(fields: {
|
|
|
78
87
|
height: number;
|
|
79
88
|
key: string | null;
|
|
80
89
|
}): RectObject | PathObject;
|
|
90
|
+
export {};
|
|
@@ -43,14 +43,15 @@ function centeredBox(center, width, height) {
|
|
|
43
43
|
return { x: center.x - width / 2, y: center.y - height / 2, width, height };
|
|
44
44
|
}
|
|
45
45
|
/** The paint of a new shape of the tools, by the look the board is in. */
|
|
46
|
-
export function newShapePaint(wireframe, random = Math.random) {
|
|
47
|
-
|
|
46
|
+
export function newShapePaint(wireframe, random = Math.random, workspacePaint = {}) {
|
|
47
|
+
const base = wireframe
|
|
48
48
|
? {
|
|
49
49
|
fill: SHAPE_STYLE_DEFAULTS.wireframeFill,
|
|
50
50
|
stroke: SHAPE_STYLE_DEFAULTS.wireframeStroke,
|
|
51
51
|
strokeWidth: SHAPE_STYLE_DEFAULTS.wireframeStrokeWidth
|
|
52
52
|
}
|
|
53
53
|
: { fill: randomShapeFill(random), stroke: SHAPE_STYLE_DEFAULTS.stroke, strokeWidth: SHAPE_STYLE_DEFAULTS.strokeWidth };
|
|
54
|
+
return { ...base, ...workspacePaint };
|
|
54
55
|
}
|
|
55
56
|
/** The look of a library shape, and of an arrow. */
|
|
56
57
|
export const LIBRARY_SHAPE_PAINT = { fill: '#ffffff', stroke: '#374151', strokeWidth: 3 };
|
|
@@ -119,19 +120,20 @@ export function buildDraggedShape(options) {
|
|
|
119
120
|
const width = Math.max(box.width, MIN_OBJECT_SIZE);
|
|
120
121
|
const height = Math.max(box.height, MIN_OBJECT_SIZE);
|
|
121
122
|
const frame = { id, x: box.x, y: box.y, width, height };
|
|
123
|
+
const paint = newShapePaint(wireframe, options.random, options.workspacePaint);
|
|
122
124
|
if (kind === 'rect')
|
|
123
|
-
return createRectObject({ ...frame, ...
|
|
125
|
+
return createRectObject({ ...frame, ...paint });
|
|
124
126
|
// The rounded square of the shapes menu: a rectangle that starts with soft corners (its Roundness control still moves them).
|
|
125
127
|
if (kind === 'rounded-rect') {
|
|
126
|
-
return createRectObject({ ...frame, ...
|
|
128
|
+
return createRectObject({ ...frame, ...paint, roundness: ROUNDED_RECT_ROUNDNESS });
|
|
127
129
|
}
|
|
128
130
|
if (kind === 'ellipse' || kind === 'circle') {
|
|
129
|
-
return createEllipseObject({ ...frame, ...
|
|
131
|
+
return createEllipseObject({ ...frame, ...paint, aspectLocked: kind === 'circle' });
|
|
130
132
|
}
|
|
131
133
|
if (kind === 'triangle') {
|
|
132
134
|
return createPathObject({
|
|
133
135
|
...frame,
|
|
134
|
-
...
|
|
136
|
+
...paint,
|
|
135
137
|
d: serializePathData(trianglePathCommands(width, height, 0)),
|
|
136
138
|
shape: TRIANGLE_SHAPE_KEY,
|
|
137
139
|
lineJoin: 'miter'
|
|
@@ -134,7 +134,7 @@ export function createMoveGesture(context, start, unitIds, options = {}) {
|
|
|
134
134
|
if (distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) < DRAG_THRESHOLD)
|
|
135
135
|
return;
|
|
136
136
|
started = true;
|
|
137
|
-
engine.setCursor('
|
|
137
|
+
engine.setCursor('grabbing');
|
|
138
138
|
if (options.duplicateOnDrag && !duplicated) {
|
|
139
139
|
duplicated = true;
|
|
140
140
|
if (engine.state.selectedIds.length === 0 && unitIds.length > 0)
|
|
@@ -573,7 +573,8 @@ export function createShapeGesture(context, start, kind, libraryKey) {
|
|
|
573
573
|
libraryKey,
|
|
574
574
|
wireframe: engine.wireframeMode,
|
|
575
575
|
createId: () => id,
|
|
576
|
-
random: () => colorSeed
|
|
576
|
+
random: () => colorSeed,
|
|
577
|
+
workspacePaint: engine.workspacePaintForNewShape(engine.wireframeMode)
|
|
577
578
|
});
|
|
578
579
|
};
|
|
579
580
|
return {
|
|
@@ -27,8 +27,7 @@ export interface TextLayout {
|
|
|
27
27
|
export declare function fontShorthand(style: Pick<TextStyleFields, 'fontSize' | 'fontFamily' | 'fontWeight' | 'fontStyle'>): string;
|
|
28
28
|
/** The height of one line, in world units. */
|
|
29
29
|
export declare function lineBoxHeight(style: Pick<TextStyleFields, 'fontSize' | 'lineHeight'>): number;
|
|
30
|
-
|
|
31
|
-
export declare function textLineExtent(style: Pick<TextStyleFields, 'fontSize' | 'lineHeight'>): number;
|
|
30
|
+
export { textLineExtent } from './textMetrics.js';
|
|
32
31
|
/** The way a text is written across a line. */
|
|
33
32
|
export type TextDirection = 'ltr' | 'rtl';
|
|
34
33
|
/**
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* browser), and the sketch the AI is shown is drawn from the same lines, so what is on the screen is what is sent.
|
|
8
8
|
*/
|
|
9
9
|
import { TEXT_DEFAULTS } from '../core/defaults.js';
|
|
10
|
+
import { textLineExtent } from './textMetrics.js';
|
|
10
11
|
/** Room so the last letter and the caret stay on the line; more than this reads as padding. */
|
|
11
12
|
export const TEXT_FIT_SLACK = 4;
|
|
12
13
|
/** Extra width for italic slant and serif overhang beyond what `measureText` returns. */
|
|
@@ -30,10 +31,7 @@ export function fontShorthand(style) {
|
|
|
30
31
|
export function lineBoxHeight(style) {
|
|
31
32
|
return style.fontSize * style.lineHeight;
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
-
export function textLineExtent(style) {
|
|
35
|
-
return Math.max(lineBoxHeight(style), style.fontSize * 1.45);
|
|
36
|
-
}
|
|
34
|
+
export { textLineExtent } from './textMetrics.js';
|
|
37
35
|
/** A letter of a script that is written from right to left. Digits, signs and marks have no direction of their own. */
|
|
38
36
|
const RIGHT_TO_LEFT_LETTER = /(?=\p{L})[\p{Script=Hebrew}\p{Script=Arabic}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Nko}\p{Script=Samaritan}\p{Script=Mandaic}\p{Script=Adlam}]/u;
|
|
39
37
|
/** A letter of any script. */
|