@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.
Files changed (107) hide show
  1. package/README.md +1 -1
  2. package/dist/canvas/DomCanvas.svelte +52 -3
  3. package/dist/canvas/DomCanvas.svelte.d.ts +59 -34
  4. package/dist/canvas/host/CanvasHost.d.ts +74 -3
  5. package/dist/canvas/host/CanvasHost.js +278 -16
  6. package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
  7. package/dist/canvas/host/CanvasHostApi.js +9 -1
  8. package/dist/canvas/host/boardFiles.d.ts +90 -0
  9. package/dist/canvas/host/boardFiles.js +139 -0
  10. package/dist/canvas/host/browserServices.d.ts +17 -0
  11. package/dist/canvas/host/browserServices.js +58 -0
  12. package/dist/canvas/index.d.ts +2 -1
  13. package/dist/canvas/types.d.ts +7 -1
  14. package/dist/canvas/ui/CanvasContextMenu.svelte +7 -0
  15. package/dist/canvas/ui/CanvasToolbar.svelte +4 -1
  16. package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +2 -0
  17. package/dist/canvas/ui/SelectionPanel.svelte +78 -17
  18. package/dist/canvas/ui/ShapesMenu.svelte +65 -31
  19. package/dist/canvas/ui/ShapesMenu.svelte.d.ts +2 -0
  20. package/dist/canvas/ui/ShortcutTooltip.svelte +64 -0
  21. package/dist/canvas/ui/ShortcutTooltip.svelte.d.ts +24 -0
  22. package/dist/canvas/ui/ToolButton.svelte +34 -55
  23. package/dist/canvas/ui/ToolButton.svelte.d.ts +4 -1
  24. package/dist/canvas/ui/floatingCard.d.ts +26 -4
  25. package/dist/canvas/ui/floatingCard.js +26 -4
  26. package/dist/canvas/ui/icons.d.ts +1 -1
  27. package/dist/canvas/ui/icons.js +1 -1
  28. package/dist/canvas/ui/selectionTraits.d.ts +12 -0
  29. package/dist/canvas/ui/selectionTraits.js +26 -1
  30. package/dist/canvas/ui/sizeLabelVisibility.d.ts +4 -2
  31. package/dist/canvas/ui/sizeLabelVisibility.js +5 -5
  32. package/dist/canvas/view/CanvasStage.svelte +7 -2
  33. package/dist/canvas/view/CanvasStage.svelte.d.ts +2 -0
  34. package/dist/canvas/view/SelectionOverlay.svelte +17 -10
  35. package/dist/canvas/view/WidgetChromeLayer.svelte +15 -3
  36. package/dist/canvas/view/geometry.d.ts +1 -1
  37. package/dist/canvas/view/geometry.js +1 -1
  38. package/dist/components/CanvasBoardPlaceholder.svelte +68 -10
  39. package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +10 -2
  40. package/dist/components/CanvasBoardPoster.svelte +206 -0
  41. package/dist/components/CanvasBoardPoster.svelte.d.ts +17 -0
  42. package/dist/components/CanvasToolbarPlaceholder.svelte +129 -0
  43. package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +15 -0
  44. package/dist/components/boardLoaderDelay.d.ts +39 -0
  45. package/dist/components/boardLoaderDelay.js +71 -0
  46. package/dist/components/index.d.ts +7 -0
  47. package/dist/components/index.js +8 -0
  48. package/dist/engine/EngineCore.d.ts +46 -4
  49. package/dist/engine/EngineCore.js +85 -16
  50. package/dist/engine/codec/canvasDocument.d.ts +7 -0
  51. package/dist/engine/codec/canvasDocument.js +25 -2
  52. package/dist/engine/core/arrow.d.ts +18 -8
  53. package/dist/engine/core/arrow.js +24 -12
  54. package/dist/engine/core/arrowBinding.d.ts +1 -1
  55. package/dist/engine/core/arrowBinding.js +10 -3
  56. package/dist/engine/core/arrowBindingRecords.d.ts +9 -3
  57. package/dist/engine/core/arrowBindingRecords.js +10 -6
  58. package/dist/engine/core/brush.d.ts +8 -0
  59. package/dist/engine/core/brush.js +10 -2
  60. package/dist/engine/core/cameraMove.d.ts +6 -0
  61. package/dist/engine/core/cameraMove.js +6 -0
  62. package/dist/engine/core/defaults.js +2 -0
  63. package/dist/engine/core/scene.d.ts +22 -0
  64. package/dist/engine/core/scene.js +86 -1
  65. package/dist/engine/core/style.d.ts +10 -0
  66. package/dist/engine/core/style.js +19 -3
  67. package/dist/engine/core/transform.d.ts +6 -0
  68. package/dist/engine/core/transform.js +9 -0
  69. package/dist/engine/core/types.d.ts +7 -0
  70. package/dist/engine/input/PointerController.d.ts +15 -1
  71. package/dist/engine/input/PointerController.js +70 -12
  72. package/dist/engine/input/gestures.d.ts +3 -1
  73. package/dist/engine/input/gestures.js +41 -10
  74. package/dist/engine/input/keyboard.d.ts +22 -3
  75. package/dist/engine/input/keyboard.js +100 -5
  76. package/dist/engine/input/types.d.ts +7 -0
  77. package/dist/engine/state.d.ts +18 -0
  78. package/dist/engine/state.js +10 -0
  79. package/dist/fonts/workspaceFonts.d.ts +6 -0
  80. package/dist/fonts/workspaceFonts.js +44 -29
  81. package/dist/i18n/language.d.ts +3 -3
  82. package/dist/i18n/messages.d.ts +6 -0
  83. package/dist/i18n/messages.js +6 -0
  84. package/dist/i18n/shortcuts.d.ts +6 -4
  85. package/dist/i18n/shortcuts.js +16 -12
  86. package/dist/index.d.ts +2 -1
  87. package/dist/poster/boardPlacement.d.ts +80 -0
  88. package/dist/poster/boardPlacement.js +138 -0
  89. package/dist/poster/boardPoster.d.ts +80 -0
  90. package/dist/poster/boardPoster.js +226 -0
  91. package/dist/poster/paintBoardPoster.d.ts +49 -0
  92. package/dist/poster/paintBoardPoster.js +89 -0
  93. package/dist/poster/posterPictures.d.ts +32 -0
  94. package/dist/poster/posterPictures.js +122 -0
  95. package/dist/settings/rememberedTool.d.ts +50 -0
  96. package/dist/settings/rememberedTool.js +68 -0
  97. package/dist/settings/workspaceToolSettings.d.ts +14 -0
  98. package/dist/settings/workspaceToolSettings.js +21 -10
  99. package/dist/tools/definitions.js +26 -4
  100. package/dist/tools/executors.js +81 -5
  101. package/dist/tools/hostAdapter.d.ts +5 -1
  102. package/dist/tools/hostAdapter.js +2 -0
  103. package/dist/tools/styleArguments.d.ts +35 -0
  104. package/dist/tools/styleArguments.js +91 -0
  105. package/dist/tools/types.d.ts +26 -1
  106. package/dist/tools/types.js +1 -0
  107. package/package.json +2 -2
@@ -4,13 +4,13 @@
4
4
  /**
5
5
  * True only while the selection is being resized (Figma "Selection Box" keeps the size chip hidden at rest;
6
6
  * see 515:623 `hidden` on the component board).
7
+ *
8
+ * The size is shown once the pointer has started to drag the handle, and not at the press: the engine only reports a drag
9
+ * (`overlayDragging`) after the pointer went the drag threshold, so a plain click on a handle changes nothing on the board.
10
+ * The kind of gesture is not read here: a resize gesture exists from the press on, before anything is being stretched.
7
11
  */
8
12
  export function shouldShowRestingSizeLabel(input) {
9
13
  if (!input.hasFrame)
10
14
  return false;
11
- if (input.overlayDragging === 'resize')
12
- return true;
13
- if (input.gestureKind === 'resize')
14
- return true;
15
- return false;
15
+ return input.overlayDragging === 'resize';
16
16
  }
@@ -42,11 +42,13 @@
42
42
  showEmptyBoardHint: boolean;
43
43
  /** True when the frame of a selected live widget card is drawn in the page's own layer above it. */
44
44
  frameElsewhere: boolean;
45
+ /** The poster of the board is on the screen: the objects are there to look at, so the loader draws no spinner over them (its layer stays). */
46
+ posterVisible?: boolean;
45
47
  /** The element the pointer events come from, handed to the shell (it is what the host is attached to). */
46
48
  board?: HTMLElement | null;
47
49
  }
48
50
 
49
- let { host, state, ui, advancedFeatures, loading, showEmptyBoardHint, frameElsewhere, board = $bindable(null) }: Props = $props();
51
+ let { host, state, ui, advancedFeatures, loading, showEmptyBoardHint, frameElsewhere, posterVisible = false, board = $bindable(null) }: Props = $props();
50
52
 
51
53
  const { t } = useCanvasLanguage();
52
54
  // The shapes the eraser has marked, as a set: each object asks whether it is one of them. A store of its own, told only
@@ -159,7 +161,10 @@
159
161
  {#if loading}
160
162
  <!-- The board stays visible while its content loads. Input waits, so a late load cannot replace what was just drawn. -->
161
163
  <div class="absolute inset-0 z-30 cursor-progress" data-canvas-loader>
162
- <CanvasBoardPlaceholder showSurface={false} />
164
+ <!-- No spinner over the poster: the placeholders before the canvas drew none, and a spinner made now, when the opening is older than its delay, would show at once and blink for the moment the board reads its saved text. The layer stays, so input still waits. -->
165
+ {#if !posterVisible}
166
+ <CanvasBoardPlaceholder showSurface={false} />
167
+ {/if}
163
168
  </div>
164
169
  {/if}
165
170
  </div>
@@ -13,6 +13,8 @@ interface Props {
13
13
  showEmptyBoardHint: boolean;
14
14
  /** True when the frame of a selected live widget card is drawn in the page's own layer above it. */
15
15
  frameElsewhere: boolean;
16
+ /** The poster of the board is on the screen: the objects are there to look at, so the loader draws no spinner over them (its layer stays). */
17
+ posterVisible?: boolean;
16
18
  /** The element the pointer events come from, handed to the shell (it is what the host is attached to). */
17
19
  board?: HTMLElement | null;
18
20
  }
@@ -10,8 +10,10 @@
10
10
  nothing here takes the pointer.
11
11
 
12
12
  The frame follows what is done with the selection, the way the Memoreco canvas does: while it is carried the handles step
13
- aside; while it is stretched the outline is dashed, the turning dot is gone and its size is the figure in the middle;
14
- while it is turned the dot stays, grown, and the angle is the figure. The dot is 8 px at rest, 24 px under the pointer.
13
+ aside and so does the line of the frame (the line along a selected arrow too), so that only the objects are seen, and no
14
+ outline of anything is drawn while it is carried; while it is stretched the outline is dashed, the turning dot is gone and its
15
+ size is the figure in the middle; while it is turned the dot stays, grown, and the angle is the figure. The dot is 8 px at
16
+ rest, 24 px under the pointer.
15
17
 
16
18
  One selected arrow has no frame at all (the way tldraw shows one): its own thin outline, a round handle on each end and one in
17
19
  the middle that bends it. No corners, no turning dot, no size: an arrow is a line from here to there, and it points where its
@@ -127,8 +129,11 @@
127
129
 
128
130
  // What is being done with the selection since the press became a drag, and what the frame shows for it.
129
131
  const dragging = $derived(state.overlay.dragging);
130
- // The handles step aside while the selection is carried.
131
- const shownHandles = $derived(dragging !== 'move' && availability ? availability.handles : []);
132
+ // The handles step aside while the selection is carried, and so does every line the board draws round it (the frame, the line
133
+ // along a selected arrow): what is carried is all that is to be seen, and a thin outline over the shapes it passes would be one
134
+ // more stroke on them.
135
+ const carried = $derived(dragging === 'move');
136
+ const shownHandles = $derived(!carried && availability ? availability.handles : []);
132
137
  const showDot = $derived(!!availability?.canRotate && (dragging === null || dragging === 'rotate'));
133
138
  const dotActive = $derived(state.rotationDotHovered || dragging === 'rotate');
134
139
  const dotCenter = $derived(frame && showDot ? pointBelowFrame(frame.screen, ROTATION_DOT_OFFSET) : null);
@@ -141,11 +146,12 @@
141
146
  return only.length === 1 && only[0].type === 'widget' && only[0].isPrimary;
142
147
  });
143
148
 
144
- // The unit under the pointer, unless it is already selected.
149
+ // The unit under the pointer, unless it is already selected, and never while the selection is carried, stretched or turned (the
150
+ // engine drops its hover when a drag begins, and the outline is kept from coming back for one that is still on its way).
145
151
  const hoveredId = $derived.by(() => {
146
152
  if (!onBoard) return null;
147
153
  const id = state.hoverId;
148
- if (!id || state.selectedIds.includes(id) || state.tool !== 'select' || gestureKind !== null) return null;
154
+ if (!id || state.selectedIds.includes(id) || state.tool !== 'select' || gestureKind !== null || dragging !== null) return null;
149
155
  return id;
150
156
  });
151
157
 
@@ -212,7 +218,6 @@
212
218
  if (
213
219
  !shouldShowRestingSizeLabel({
214
220
  hasFrame: !!frame,
215
- gestureKind,
216
221
  overlayDragging: state.overlay.dragging
217
222
  })
218
223
  ) {
@@ -324,8 +329,10 @@
324
329
  {/each}
325
330
 
326
331
  {#if arrowSelection}
327
- {@render arrowOutline(arrowSelection.arrow, false)}
328
- {#each dragging === 'move' ? [] : arrowSelection.handles as handle (handle.id)}
332
+ {#if !carried}
333
+ {@render arrowOutline(arrowSelection.arrow, false)}
334
+ {/if}
335
+ {#each carried ? [] : arrowSelection.handles as handle (handle.id)}
329
336
  {@const active = state.arrowHandleHovered === handle.id}
330
337
  <!-- A round handle: 10 px on an end, 8 px on the middle, 14 px under the pointer and while it is dragged. -->
331
338
  <span
@@ -353,7 +360,7 @@
353
360
  <div class="absolute box-border border-2" style="{screenFrameStyle(frame.screen)}border-color:{PRIMARY_RING_COLOR};"></div>
354
361
  {/if}
355
362
  <div
356
- class="absolute box-border border-[1.5px] {dragging === 'resize' ? 'border-transparent' : 'border-ring'}"
363
+ class="absolute box-border border-[1.5px] {dragging === 'resize' || carried ? 'border-transparent' : 'border-ring'}"
357
364
  style={screenFrameStyle(frame.screen)}
358
365
  data-canvas-frame
359
366
  data-canvas-selection-frame
@@ -27,8 +27,10 @@
27
27
  // `state` is a prop name of the canvas; here it is renamed, because a local `state` would turn `$state` into a store.
28
28
  let { host, state: engineState, gestureKind, advancedFeatures, target }: Props = $props();
29
29
 
30
- // Where the board is on the screen, in pixels from the corner of the window: the layer is laid over it there.
30
+ // Where the board is on the screen, in pixels from its `position: fixed` containing block (the window on a normal page,
31
+ // or `:host` inside a shadow root with `contain: layout`).
31
32
  let boardRect = $state({ left: 0, top: 0, width: 0, height: 0 });
33
+ let fixedOriginEl = $state<HTMLSpanElement | null>(null);
32
34
  let measureFrame = 0;
33
35
 
34
36
  function measure(): void {
@@ -36,8 +38,16 @@
36
38
  const board = host.boardElement;
37
39
  if (!board) return;
38
40
  const bounds = board.getBoundingClientRect();
39
- if (bounds.left !== boardRect.left || bounds.top !== boardRect.top || bounds.width !== boardRect.width || bounds.height !== boardRect.height) {
40
- boardRect = { left: bounds.left, top: bounds.top, width: bounds.width, height: bounds.height };
41
+ const root = board.getRootNode();
42
+ const origin = fixedOriginEl
43
+ ? fixedOriginEl.getBoundingClientRect()
44
+ : root instanceof ShadowRoot
45
+ ? root.host.getBoundingClientRect()
46
+ : { left: 0, top: 0 };
47
+ const left = bounds.left - origin.left;
48
+ const top = bounds.top - origin.top;
49
+ if (left !== boardRect.left || top !== boardRect.top || bounds.width !== boardRect.width || bounds.height !== boardRect.height) {
50
+ boardRect = { left, top, width: bounds.width, height: bounds.height };
41
51
  }
42
52
  }
43
53
 
@@ -77,6 +87,8 @@
77
87
  </script>
78
88
 
79
89
  <Portal {target} style="z-index: 70; pointer-events: none;">
90
+ <!-- rtl-ok: physical left: 0 origin probe for getBoundingClientRect() -->
91
+ <span bind:this={fixedOriginEl} class="pointer-events-none fixed left-0 top-0 size-0" aria-hidden="true"></span>
80
92
  <div class="canvas-widget-selection-layer pointer-events-none fixed overflow-hidden" dir="ltr" data-canvas-widget-chrome style={layerStyle}>
81
93
  <SelectionOverlay state={engineState} {host} {gestureKind} {advancedFeatures} layer="above-widgets" />
82
94
  </div>
@@ -171,7 +171,7 @@ export declare const DOT_GRID_STEP = 24;
171
171
  * One dot in the middle of a cell, as a gradient: it takes its colour from the page's `foreground` token (a data URL
172
172
  * cannot), stays two pixels across at every zoom, and costs one background layer however large the board is.
173
173
  */
174
- export declare const DOT_GRID_IMAGE = "radial-gradient(circle at 50% 50%, color-mix(in srgb, var(--foreground, #0f172a) 30%, transparent) 0, color-mix(in srgb, var(--foreground, #0f172a) 30%, transparent) 1px, transparent 1.5px)";
174
+ export declare const DOT_GRID_IMAGE = "radial-gradient(circle at 50% 50%, color-mix(in srgb, var(--canvas-ink, var(--foreground, #0f172a)) 30%, transparent) 0, color-mix(in srgb, var(--canvas-ink, var(--foreground, #0f172a)) 30%, transparent) 1px, transparent 1.5px)";
175
175
  export declare function dotGridStyle(viewport: Viewport): string;
176
176
  /** The CSS `cursor` for what the engine asked the board to show. */
177
177
  export declare function cssCursor(cursor: string): string;
@@ -253,7 +253,7 @@ const DOT_GRID_MIN_SCREEN_STEP = 12;
253
253
  * One dot in the middle of a cell, as a gradient: it takes its colour from the page's `foreground` token (a data URL
254
254
  * cannot), stays two pixels across at every zoom, and costs one background layer however large the board is.
255
255
  */
256
- export const DOT_GRID_IMAGE = 'radial-gradient(circle at 50% 50%, color-mix(in srgb, var(--foreground, #0f172a) 30%, transparent) 0, color-mix(in srgb, var(--foreground, #0f172a) 30%, transparent) 1px, transparent 1.5px)';
256
+ export const DOT_GRID_IMAGE = 'radial-gradient(circle at 50% 50%, color-mix(in srgb, var(--canvas-ink, var(--foreground, #0f172a)) 30%, transparent) 0, color-mix(in srgb, var(--canvas-ink, var(--foreground, #0f172a)) 30%, transparent) 1px, transparent 1.5px)';
257
257
  export function dotGridStyle(viewport) {
258
258
  let step = DOT_GRID_STEP;
259
259
  while (step * viewport.zoom < DOT_GRID_MIN_SCREEN_STEP)
@@ -6,7 +6,10 @@
6
6
  </script>
7
7
 
8
8
  <script lang="ts">
9
+ import { onDestroy } from 'svelte';
9
10
  import { provideCanvasLanguage, useCanvasLanguage } from '../i18n/language.js';
11
+ import { IconLoader2 } from '../canvas/ui/icons.js';
12
+ import { BOARD_LOADER_DELAY_MS, claimBoardLoader } from './boardLoaderDelay.js';
10
13
 
11
14
  /**
12
15
  * The empty board, drawn before the canvas itself is there.
@@ -14,12 +17,20 @@
14
17
  * A host shows it while the canvas code is still downloading, so the page
15
18
  * has its board at first paint. The real canvas replaces it with the same
16
19
  * surface, so the swap is not visible.
20
+ *
21
+ * Its loader is the design system's spinner, in the design system's muted colour, with no words beside it. It is not drawn at
22
+ * once: a board that opens within `BOARD_LOADER_DELAY_MS` (everything already in the browser) never shows one, and a slower
23
+ * board fades it in after that time, counted from the first placeholder of the opening, not from this one (see
24
+ * `boardLoaderDelay.ts`). A screen reader still hears the name of what is loading.
17
25
  */
18
26
  interface Props {
27
+ /** False draws only the board, with no loader. */
28
+ showLoader?: boolean;
29
+ /** @deprecated The placeholder shows no label any more: this is `showLoader` under its old name. */
19
30
  showLoadingLabel?: boolean;
20
- /** False draws only the loading label, over a board that is already painted. */
31
+ /** False draws only the loader, over a board that is already painted. */
21
32
  showSurface?: boolean;
22
- /** Same as Canvas: give these outside a canvas so the label speaks the page's language. */
33
+ /** Same as Canvas: give these outside a canvas so the name of the loader is in the page's language. */
23
34
  locale?: string | null;
24
35
  messages?: Readonly<Record<string, unknown>> | null;
25
36
  class?: string;
@@ -27,7 +38,8 @@
27
38
  }
28
39
 
29
40
  let {
30
- showLoadingLabel = true,
41
+ showLoader = undefined,
42
+ showLoadingLabel = undefined,
31
43
  showSurface = true,
32
44
  locale = undefined,
33
45
  messages = undefined,
@@ -35,6 +47,9 @@
35
47
  style = ''
36
48
  }: Props = $props();
37
49
 
50
+ /** Whether this placeholder has a loader at all. */
51
+ const hasLoader = $derived(showLoader ?? showLoadingLabel ?? true);
52
+
38
53
  // Inside a canvas the root already set the language; on its own, the page passes it.
39
54
  // svelte-ignore state_referenced_locally
40
55
  const ownsLanguage = locale !== undefined || messages !== undefined;
@@ -47,19 +62,62 @@
47
62
  $effect(() => {
48
63
  languageHandle?.update({ locale: locale ?? null, messages: messages ?? null });
49
64
  });
65
+
66
+ // This placeholder takes its place in the opening of the board when it is made, and gives it back when it goes away. On the page
67
+ // only: a server has no opening to count, and it draws the loader with the whole delay.
68
+ const claim = typeof window === 'undefined' ? null : claimBoardLoader();
69
+ onDestroy(() => claim?.release());
70
+
71
+ /** How long the loader waits before it is first seen: what is left of the delay of the opening this placeholder belongs to. */
72
+ const loaderDelayMs = claim?.delayMs ?? BOARD_LOADER_DELAY_MS;
50
73
  </script>
51
74
 
52
75
  <div
53
76
  class="canvas-board-placeholder relative h-full w-full overflow-hidden {className}"
54
77
  style="{showSurface ? `background-color: ${CANVAS_BOARD_BACKGROUND};` : ''} {style}"
55
- aria-busy={showLoadingLabel}
78
+ aria-busy={hasLoader}
56
79
  >
57
- {#if showLoadingLabel}
58
- <div class="canvas-board-placeholder-status absolute inset-0 flex items-center justify-center pointer-events-none">
59
- <div class="canvas-board-placeholder-status-inner flex items-center gap-2 text-sm font-medium text-gray-500">
60
- <span class="canvas-board-placeholder-spinner h-4 w-4 rounded-full border-2 border-gray-300 border-t-gray-600 animate-spin"></span>
61
- <span>{$t('canvas.state.loading')}</span>
62
- </div>
80
+ {#if hasLoader}
81
+ <div class="canvas-board-placeholder-status pointer-events-none absolute inset-0 flex items-center justify-center" role="status">
82
+ <!-- Fades in once the opening is older than the delay; at once (no fade) when the placeholder before this one was already seen -->
83
+ <span
84
+ class={['canvas-board-placeholder-spinner flex', { 'canvas-board-placeholder-spinner-seen': loaderDelayMs === 0 }]}
85
+ style="--canvas-board-loader-delay: {loaderDelayMs}ms;"
86
+ aria-hidden="true"
87
+ >
88
+ <IconLoader2 class="text-muted-foreground size-4 animate-spin stroke-2 motion-reduce:animate-none" />
89
+ </span>
90
+ <span class="sr-only">{$t('canvas.state.loading')}</span>
63
91
  </div>
64
92
  {/if}
65
93
  </div>
94
+
95
+ <style>
96
+ /* Hidden for the delay, then faded in: no timer to keep, and a placeholder that goes away first never shows it. */
97
+ .canvas-board-placeholder-spinner {
98
+ opacity: 0;
99
+ animation: canvas-board-placeholder-spinner-in var(--duration-base, 160ms) var(--ease-out, ease-out)
100
+ var(--canvas-board-loader-delay, 400ms) both;
101
+ }
102
+
103
+ /* The loader of the placeholder before this one was already on screen: carry on with no new fade, so it does not blink at the hand-over. */
104
+ .canvas-board-placeholder-spinner-seen {
105
+ opacity: 1;
106
+ animation: none;
107
+ }
108
+
109
+ @keyframes canvas-board-placeholder-spinner-in {
110
+ from {
111
+ opacity: 0;
112
+ }
113
+ to {
114
+ opacity: 1;
115
+ }
116
+ }
117
+
118
+ @media (prefers-reduced-motion: reduce) {
119
+ .canvas-board-placeholder-spinner {
120
+ animation-duration: 1ms;
121
+ }
122
+ }
123
+ </style>
@@ -6,12 +6,20 @@ export declare const CANVAS_BOARD_BACKGROUND = "var(--canvas-surface, var(--mute
6
6
  * A host shows it while the canvas code is still downloading, so the page
7
7
  * has its board at first paint. The real canvas replaces it with the same
8
8
  * surface, so the swap is not visible.
9
+ *
10
+ * Its loader is the design system's spinner, in the design system's muted colour, with no words beside it. It is not drawn at
11
+ * once: a board that opens within `BOARD_LOADER_DELAY_MS` (everything already in the browser) never shows one, and a slower
12
+ * board fades it in after that time, counted from the first placeholder of the opening, not from this one (see
13
+ * `boardLoaderDelay.ts`). A screen reader still hears the name of what is loading.
9
14
  */
10
15
  interface Props {
16
+ /** False draws only the board, with no loader. */
17
+ showLoader?: boolean;
18
+ /** @deprecated The placeholder shows no label any more: this is `showLoader` under its old name. */
11
19
  showLoadingLabel?: boolean;
12
- /** False draws only the loading label, over a board that is already painted. */
20
+ /** False draws only the loader, over a board that is already painted. */
13
21
  showSurface?: boolean;
14
- /** Same as Canvas: give these outside a canvas so the label speaks the page's language. */
22
+ /** Same as Canvas: give these outside a canvas so the name of the loader is in the page's language. */
15
23
  locale?: string | null;
16
24
  messages?: Readonly<Record<string, unknown>> | null;
17
25
  class?: string;
@@ -0,0 +1,206 @@
1
+ <!--
2
+ The poster of the board: the objects that were in the saved view, drawn from the first frame, before the canvas engine has them.
3
+
4
+ A refresh used to show the empty board first and the objects a moment later: the saved board is kept next to the page, but
5
+ nothing can draw it until the canvas code has been downloaded, mounted and has read it. The page keeps a small copy of the part
6
+ of the board that was on the screen (`createBoardPoster`) and this draws it, with the engine's own painter (`paintShape`), at the
7
+ view the board was saved with. It is a picture, no more: it takes no pointer, it is hidden from the accessibility tree and the
8
+ tab order, and it draws no selection and no live widget (a card is drawn with its screenshot).
9
+
10
+ A picture whose bytes are in the host's image store (a photo dropped on the board, the screenshot of a widget card) has only a
11
+ reference in the saved text. The host gives `resolvePicture`, which answers a reference with the bytes (or an address) from its
12
+ store; the poster asks for each once, draws the card without the picture until it arrives, and draws it again when it does.
13
+
14
+ It is shown while `hold` is true, which is while the board is loading its own objects (the canvas passes its loading flag), and
15
+ it lets go two frames after that ends, with a short fade, so the hand-over is not seen: the board has painted the same objects
16
+ by then, in the same place. A poster that is given after the board has painted is never shown.
17
+
18
+ It fills the positioned element it is in. Inside the canvas it sits between the board and the chrome (the toolbar stays above it);
19
+ in a host's placeholder it fills the board that stands in for the canvas. The host decides where it is and how high; this file
20
+ only draws.
21
+
22
+ It says when it is on the screen (`bind:visible`). The canvas uses that to draw no spinner over the objects: they are there to look
23
+ at, the placeholders before the canvas drew none, and a spinner that came back for the moment the board reads its saved text would
24
+ blink at the hand-over.
25
+ -->
26
+ <script lang="ts">
27
+ import { onMount, untrack } from 'svelte';
28
+ import { createFixedAdvanceMeasure } from '../engine/text/layoutText.js';
29
+ import type { MeasureText } from '../engine/text/layoutText.js';
30
+ import { parseBoardPoster } from '../poster/boardPoster.js';
31
+ import type { BoardPoster } from '../poster/boardPoster.js';
32
+ import {
33
+ paintBoardPoster,
34
+ posterPaintDependencies,
35
+ posterPictureReferences,
36
+ posterPictureSources,
37
+ withReferencedPictures
38
+ } from '../poster/paintBoardPoster.js';
39
+ import {
40
+ posterPictureFor,
41
+ requestPosterPictures,
42
+ requestReferencedPictures,
43
+ subscribeToPosterPictures
44
+ } from '../poster/posterPictures.js';
45
+ import type { PosterPictureResolver } from '../poster/posterPictures.js';
46
+
47
+ interface Props {
48
+ /** What the page kept of the board (`createBoardPoster`). Nothing is drawn without it. */
49
+ poster?: BoardPoster | null;
50
+ /** True while the real board has not painted its objects yet. When it turns false the poster waits two frames and fades away. */
51
+ hold?: boolean;
52
+ /** The host's image store: answers the reference of a picture the board keeps there with its bytes (a data URL) or an address. */
53
+ resolvePicture?: PosterPictureResolver | null;
54
+ /** Said by the poster, for the host to bind: true while the poster is on the screen (drawn, and not yet gone), false before and after. */
55
+ visible?: boolean;
56
+ /** Where the poster is and how high (the host's own classes). */
57
+ class?: string;
58
+ }
59
+
60
+ let { poster = null, hold = true, resolvePicture = null, visible = $bindable(false), class: className = '' }: Props = $props();
61
+
62
+ /** How long the poster takes to fade out, in milliseconds (the class below says the same, in CSS). */
63
+ const FADE_OUT_MS = 150;
64
+ /** The densest screen the poster draws for: a canvas of more pixels costs memory and shows nothing a person can tell. */
65
+ const MAX_PIXEL_RATIO = 2;
66
+
67
+ // The poster as it is drawn: read through the canvas's own reader, so a damaged or foreign one draws nothing.
68
+ const parsedPoster = $derived(poster ? parseBoardPoster(poster) : null);
69
+ // The same poster with each picture kept in the host's store named by its key, which the painter asks the loaded pictures for.
70
+ const drawablePoster = $derived(parsedPoster ? withReferencedPictures(parsedPoster) : null);
71
+
72
+ // showing → fading → gone. A poster given after the board painted starts gone: the board is already there.
73
+ let phase = $state<'showing' | 'fading' | 'gone'>(untrack(() => (hold ? 'showing' : 'gone')));
74
+
75
+ let canvasElement = $state<HTMLCanvasElement | null>(null);
76
+ // The size of the area the poster stands in (bound to the wrapper below: measured at mount, so the first draw is in the first
77
+ // frame, and again at every resize).
78
+ let areaWidth = $state(0);
79
+ let areaHeight = $state(0);
80
+ // Bumped when a picture has arrived or a font has loaded, so the poster is drawn again with it.
81
+ let picturesRevision = $state(0);
82
+ let fontsRevision = $state(0);
83
+ let paintFailureLogged = false;
84
+
85
+ /** A text measurer made of the browser's canvas; a fixed advance where there is none. */
86
+ function createMeasureText(): MeasureText {
87
+ let context: CanvasRenderingContext2D | null | undefined;
88
+ const fallback = createFixedAdvanceMeasure();
89
+ return (text, font) => {
90
+ if (context === undefined) {
91
+ try {
92
+ context = document.createElement('canvas').getContext('2d');
93
+ } catch {
94
+ context = null;
95
+ }
96
+ }
97
+ if (!context) return fallback(text, font);
98
+ if (context.font !== font) context.font = font;
99
+ return context.measureText(text).width;
100
+ };
101
+ }
102
+ const measureText = createMeasureText();
103
+
104
+ // A picture that arrives, or a font that finishes loading, draws the poster again: neither is there in the first frame.
105
+ onMount(() => {
106
+ const stopListeningToPictures = subscribeToPosterPictures(() => {
107
+ picturesRevision += 1;
108
+ });
109
+ const fonts = typeof document === 'undefined' ? undefined : document.fonts;
110
+ const onFontsLoaded = () => {
111
+ fontsRevision += 1;
112
+ };
113
+ fonts?.addEventListener?.('loadingdone', onFontsLoaded);
114
+ return () => {
115
+ stopListeningToPictures();
116
+ fonts?.removeEventListener?.('loadingdone', onFontsLoaded);
117
+ // A poster that is taken away while it is up leaves nothing on the screen: the host is told
118
+ visible = false;
119
+ };
120
+ });
121
+
122
+ // Whether the poster is on the screen, told to the host: while it is showing, and while it fades.
123
+ $effect(() => {
124
+ visible = parsedPoster !== null && phase !== 'gone';
125
+ });
126
+
127
+ // Drawing. Setting a canvas's width clears it, so the size is only set when it changed; the picture is painted every time.
128
+ $effect(() => {
129
+ const element = canvasElement;
130
+ const shapes = drawablePoster;
131
+ const asRead = parsedPoster;
132
+ const resolve = resolvePicture;
133
+ const width = areaWidth;
134
+ const height = areaHeight;
135
+ void picturesRevision;
136
+ void fontsRevision;
137
+ if (!element || !shapes || !asRead || width <= 0 || height <= 0) return;
138
+ if (untrack(() => phase) === 'gone') return;
139
+ try {
140
+ const pixelRatio = Math.min(MAX_PIXEL_RATIO, window.devicePixelRatio || 1);
141
+ const pixelWidth = Math.round(width * pixelRatio);
142
+ const pixelHeight = Math.round(height * pixelRatio);
143
+ if (element.width !== pixelWidth) element.width = pixelWidth;
144
+ if (element.height !== pixelHeight) element.height = pixelHeight;
145
+ const context = element.getContext('2d');
146
+ if (!context) return;
147
+ paintBoardPoster(
148
+ context,
149
+ shapes,
150
+ { width, height, pixelRatio },
151
+ posterPaintDependencies({ pictureFor: posterPictureFor, createPath: (data) => new Path2D(data), measureText })
152
+ );
153
+ // Pictures with an address of their own: asked for after the shapes are drawn, and drawn again when they arrive.
154
+ // (Asked from the poster as it was read: the one the painter has names the stored pictures by keys, which are no addresses.)
155
+ requestPosterPictures(posterPictureSources(asRead));
156
+ // Pictures the board keeps in the host's store: the host is asked for each reference once.
157
+ if (resolve) requestReferencedPictures(posterPictureReferences(asRead), resolve);
158
+ } catch (error) {
159
+ // Once per poster: the board draws its own objects whatever happens here.
160
+ if (!paintFailureLogged) {
161
+ paintFailureLogged = true;
162
+ console.warn('[CanvasBoardPoster] The poster could not be drawn; the board will draw its own objects', error);
163
+ }
164
+ }
165
+ });
166
+
167
+ // The hand-over. While the board is loading the poster stays; two frames after it stops, the browser has shown the board's own
168
+ // objects, and the poster fades and goes. A board that starts loading again takes the poster back while it fades.
169
+ $effect(() => {
170
+ if (hold) {
171
+ untrack(() => {
172
+ if (phase === 'fading') phase = 'showing';
173
+ });
174
+ return;
175
+ }
176
+ if (untrack(() => phase) !== 'showing') return;
177
+ let secondFrame = 0;
178
+ let removal: ReturnType<typeof setTimeout> | undefined;
179
+ const firstFrame = requestAnimationFrame(() => {
180
+ secondFrame = requestAnimationFrame(() => {
181
+ phase = 'fading';
182
+ removal = setTimeout(() => {
183
+ phase = 'gone';
184
+ }, FADE_OUT_MS + 60);
185
+ });
186
+ });
187
+ return () => {
188
+ cancelAnimationFrame(firstFrame);
189
+ cancelAnimationFrame(secondFrame);
190
+ if (removal !== undefined) clearTimeout(removal);
191
+ };
192
+ });
193
+ </script>
194
+
195
+ {#if parsedPoster && phase !== 'gone'}
196
+ <div
197
+ bind:clientWidth={areaWidth}
198
+ bind:clientHeight={areaHeight}
199
+ class="canvas-board-poster pointer-events-none absolute inset-0 overflow-hidden transition-opacity duration-150 motion-reduce:transition-none {phase === 'fading' ? 'opacity-0' : 'opacity-100'} {className}"
200
+ data-canvas-board-poster
201
+ aria-hidden="true"
202
+ inert
203
+ >
204
+ <canvas bind:this={canvasElement} class="block h-full w-full"></canvas>
205
+ </div>
206
+ {/if}
@@ -0,0 +1,17 @@
1
+ import type { BoardPoster } from '../poster/boardPoster.js';
2
+ import type { PosterPictureResolver } from '../poster/posterPictures.js';
3
+ interface Props {
4
+ /** What the page kept of the board (`createBoardPoster`). Nothing is drawn without it. */
5
+ poster?: BoardPoster | null;
6
+ /** True while the real board has not painted its objects yet. When it turns false the poster waits two frames and fades away. */
7
+ hold?: boolean;
8
+ /** The host's image store: answers the reference of a picture the board keeps there with its bytes (a data URL) or an address. */
9
+ resolvePicture?: PosterPictureResolver | null;
10
+ /** Said by the poster, for the host to bind: true while the poster is on the screen (drawn, and not yet gone), false before and after. */
11
+ visible?: boolean;
12
+ /** Where the poster is and how high (the host's own classes). */
13
+ class?: string;
14
+ }
15
+ declare const CanvasBoardPoster: import("svelte").Component<Props, {}, "visible">;
16
+ type CanvasBoardPoster = ReturnType<typeof CanvasBoardPoster>;
17
+ export default CanvasBoardPoster;