@widgetic/canvas 0.5.18 → 0.5.20

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 (48) hide show
  1. package/README.md +110 -0
  2. package/dist/canvas/Canvas.svelte +174 -50
  3. package/dist/canvas/Canvas.svelte.d.ts +6 -0
  4. package/dist/canvas/CanvasToolbar.svelte +146 -178
  5. package/dist/canvas/CanvasToolbar.svelte.d.ts +2 -2
  6. package/dist/canvas/ContextMenu.svelte +52 -28
  7. package/dist/canvas/PanZoomPanel.svelte +32 -23
  8. package/dist/canvas/WidgetSelectionLayer.svelte +1 -1
  9. package/dist/canvas/eventScope.d.ts +56 -0
  10. package/dist/canvas/eventScope.js +89 -0
  11. package/dist/canvas/props-panel/PropsPanel.svelte +117 -146
  12. package/dist/canvas/props-panel/TextPropsICSection.svelte +18 -14
  13. package/dist/canvas/shadowRootText.d.ts +5 -0
  14. package/dist/canvas/shadowRootText.js +43 -0
  15. package/dist/canvas/shapes/FrameShape.js +4 -2
  16. package/dist/canvas/shapes/ImageShape.js +9 -11
  17. package/dist/canvas/shapes/ShapeLibrary.d.ts +3 -1
  18. package/dist/canvas/shapes/ShapeLibrary.js +26 -26
  19. package/dist/canvas/shapes/WidgetShape.js +6 -3
  20. package/dist/components/Tooltip.svelte +4 -1
  21. package/dist/components/index.d.ts +3 -0
  22. package/dist/components/index.js +4 -0
  23. package/dist/components/input-controls/ColorIC.svelte +15 -10
  24. package/dist/components/input-controls/ColorIC.svelte.d.ts +1 -1
  25. package/dist/components/input-controls/FontSelectorIC.svelte +12 -3
  26. package/dist/components/input-controls/SliderUnitIC.svelte +21 -7
  27. package/dist/components/input-controls/SliderUnitIC.svelte.d.ts +1 -1
  28. package/dist/components/input-controls/TextAlignIC.svelte +10 -5
  29. package/dist/components/input-controls/TextStyleIC.svelte +11 -7
  30. package/dist/components/preferenceStorage.d.ts +14 -0
  31. package/dist/components/preferenceStorage.js +25 -0
  32. package/dist/i18n/index.d.ts +2 -0
  33. package/dist/i18n/index.js +2 -0
  34. package/dist/i18n/labels.d.ts +23 -0
  35. package/dist/i18n/labels.js +75 -0
  36. package/dist/i18n/language.d.ts +23 -0
  37. package/dist/i18n/language.js +33 -0
  38. package/dist/i18n/messages.d.ts +277 -0
  39. package/dist/i18n/messages.js +292 -0
  40. package/dist/i18n/shapeWords.d.ts +30 -0
  41. package/dist/i18n/shapeWords.js +36 -0
  42. package/dist/i18n/shortcuts.d.ts +52 -0
  43. package/dist/i18n/shortcuts.js +104 -0
  44. package/dist/i18n/slots.d.ts +20 -0
  45. package/dist/i18n/slots.js +40 -0
  46. package/dist/index.d.ts +4 -0
  47. package/dist/index.js +5 -0
  48. package/package.json +11 -2
@@ -3,14 +3,14 @@ declare const __propDef: {
3
3
  props: {
4
4
  class?: string;
5
5
  style?: string;
6
- activeTool?: "select" | "video" | "rect" | "erase" | "draw" | "arrow" | "text" | "circle" | "circle-true" | "triangle" | "frame" | "pan" | "image" | "library-shape";
6
+ activeTool?: "select" | "video" | "rect" | "circle" | "triangle" | "frame" | "arrow" | "image" | "text" | "erase" | "draw" | "circle-true" | "pan" | "library-shape";
7
7
  activeClass?: string;
8
8
  inactiveClass?: string;
9
9
  buttonClass?: string;
10
10
  onSelectSelectTool?: () => void;
11
11
  onSelectEraseTool?: () => void;
12
12
  onSelectDrawTool?: () => void;
13
- onSelectShapeTool?: (shapeType: "rect" | "arrow" | "circle" | "circle-true" | "triangle") => void;
13
+ onSelectShapeTool?: (shapeType: "rect" | "circle" | "triangle" | "arrow" | "circle-true") => void;
14
14
  onSelectFrameTool?: () => void;
15
15
  onSelectTextTool?: () => void;
16
16
  onSelectPanTool?: () => void;
@@ -1,5 +1,27 @@
1
1
  <script lang="ts">
2
2
  import { onMount } from 'svelte';
3
+ import { eventWentThrough } from './eventScope';
4
+ import { useCanvasLanguage } from '../i18n/language.js';
5
+
6
+ // The words of the menu, in the language of the canvas it is on (English when it is used on its own)
7
+ const { t } = useCanvasLanguage();
8
+
9
+ // The key that does the same as each item, as the Mac writes it (⌘ is its Command key, ⇧ its Shift key). These are
10
+ // key caps, not words, so they are the same in every language.
11
+ const menuKeys = {
12
+ cut: '⌘X',
13
+ copy: '⌘C',
14
+ paste: '⌘V',
15
+ duplicate: '⌘D',
16
+ group: '⌘G',
17
+ ungroup: '⌘⇧G',
18
+ bringToFront: '⌘]',
19
+ bringForward: ']',
20
+ sendBackward: '[',
21
+ sendToBack: '⌘[',
22
+ delete: '⌫',
23
+ selectAll: '⌘A'
24
+ } as const;
3
25
 
4
26
  // ── Position & visibility ──────────────────────────────────────────────────
5
27
  export let x: number = 0;
@@ -40,7 +62,9 @@
40
62
  }
41
63
 
42
64
  function handlePointerDown(e: PointerEvent) {
43
- if (visible && menuEl && !menuEl.contains(e.target as Node)) {
65
+ // Did the press go through the menu? (Asked of the event's path: in a shadow root a listener on window is told the
66
+ // host element as the target, which is never inside the menu, and the menu would close before its item is clicked.)
67
+ if (visible && menuEl && !eventWentThrough(e, menuEl)) {
44
68
  onClose();
45
69
  }
46
70
  }
@@ -86,34 +110,34 @@
86
110
  <div
87
111
  bind:this={menuEl}
88
112
  class="context-menu-el"
89
- style="left: {safeX}px; top: {safeY}px;"
113
+ style="left: {safeX}px; top: {safeY}px; /* rtl-ok: put where the pointer is, measured from the left edge of the canvas */"
90
114
  oncontextmenu={(e) => e.preventDefault()}
91
115
  >
92
116
  <!-- Selection actions -->
93
117
  {#if hasSelection}
94
118
  <button class="item" onclick={() => handleAction(onCut)}>
95
119
  <span class="icon">✂️</span>
96
- <span class="label">Cut</span>
97
- <span class="shortcut">⌘X</span>
120
+ <span class="label">{$t('canvas.action.cut')}</span>
121
+ <span class="shortcut">{menuKeys.cut}</span>
98
122
  </button>
99
123
  <button class="item" onclick={() => handleAction(onCopy)}>
100
124
  <span class="icon">📋</span>
101
- <span class="label">Copy</span>
102
- <span class="shortcut">⌘C</span>
125
+ <span class="label">{$t('canvas.action.copy')}</span>
126
+ <span class="shortcut">{menuKeys.copy}</span>
103
127
  </button>
104
128
  {/if}
105
129
 
106
130
  <button class="item" class:disabled={!canPaste} onclick={() => canPaste && handleAction(onPaste)}>
107
131
  <span class="icon">📌</span>
108
- <span class="label">Paste</span>
109
- <span class="shortcut">⌘V</span>
132
+ <span class="label">{$t('canvas.action.paste')}</span>
133
+ <span class="shortcut">{menuKeys.paste}</span>
110
134
  </button>
111
135
 
112
136
  {#if hasSelection}
113
137
  <button class="item" onclick={() => handleAction(onDuplicate)}>
114
138
  <span class="icon">⧉</span>
115
- <span class="label">Duplicate</span>
116
- <span class="shortcut">⌘D</span>
139
+ <span class="label">{$t('canvas.action.duplicate')}</span>
140
+ <span class="shortcut">{menuKeys.duplicate}</span>
117
141
  </button>
118
142
 
119
143
  <div class="divider"></div>
@@ -127,8 +151,8 @@
127
151
  <rect x="2" y="14" width="8" height="8" rx="1"/><rect x="14" y="14" width="8" height="8" rx="1"/>
128
152
  </svg>
129
153
  </span>
130
- <span class="label">Group</span>
131
- <span class="shortcut">⌘G</span>
154
+ <span class="label">{$t('canvas.action.group')}</span>
155
+ <span class="shortcut">{menuKeys.group}</span>
132
156
  </button>
133
157
  {/if}
134
158
  {#if canUngroup}
@@ -139,8 +163,8 @@
139
163
  <rect x="2" y="14" width="8" height="8" rx="1" stroke-dasharray="3 2"/><rect x="14" y="14" width="8" height="8" rx="1" stroke-dasharray="3 2"/>
140
164
  </svg>
141
165
  </span>
142
- <span class="label">Ungroup</span>
143
- <span class="shortcut">⌘⇧G</span>
166
+ <span class="label">{$t('canvas.action.ungroup')}</span>
167
+ <span class="shortcut">{menuKeys.ungroup}</span>
144
168
  </button>
145
169
  {/if}
146
170
 
@@ -154,8 +178,8 @@
154
178
  <path d="M12 4v4M14 6l-2-2-2 2"/>
155
179
  </svg>
156
180
  </span>
157
- <span class="label">Bring to Front</span>
158
- <span class="shortcut">⌘]</span>
181
+ <span class="label">{$t('canvas.layer.front')}</span>
182
+ <span class="shortcut">{menuKeys.bringToFront}</span>
159
183
  </button>
160
184
  <button class="item" onclick={() => handleAction(onBringForward)}>
161
185
  <span class="icon">
@@ -164,8 +188,8 @@
164
188
  <path d="M12 5v3M14 6l-2-2-2 2"/>
165
189
  </svg>
166
190
  </span>
167
- <span class="label">Bring Forward</span>
168
- <span class="shortcut">]</span>
191
+ <span class="label">{$t('canvas.layer.forward')}</span>
192
+ <span class="shortcut">{menuKeys.bringForward}</span>
169
193
  </button>
170
194
  <button class="item" onclick={() => handleAction(onSendBackward)}>
171
195
  <span class="icon">
@@ -174,8 +198,8 @@
174
198
  <path d="M12 16v3M10 18l2 2 2-2"/>
175
199
  </svg>
176
200
  </span>
177
- <span class="label">Send Backward</span>
178
- <span class="shortcut">[</span>
201
+ <span class="label">{$t('canvas.layer.backward')}</span>
202
+ <span class="shortcut">{menuKeys.sendBackward}</span>
179
203
  </button>
180
204
  <button class="item" onclick={() => handleAction(onSendToBack)}>
181
205
  <span class="icon">
@@ -184,24 +208,24 @@
184
208
  <path d="M12 17v3M10 19l2 2 2-2"/>
185
209
  </svg>
186
210
  </span>
187
- <span class="label">Send to Back</span>
188
- <span class="shortcut">⌘[</span>
211
+ <span class="label">{$t('canvas.layer.back')}</span>
212
+ <span class="shortcut">{menuKeys.sendToBack}</span>
189
213
  </button>
190
214
 
191
215
  <div class="divider"></div>
192
216
 
193
217
  <button class="item danger" onclick={() => handleAction(onDelete)}>
194
218
  <span class="icon">🗑</span>
195
- <span class="label">Delete</span>
196
- <span class="shortcut">⌫</span>
219
+ <span class="label">{$t('canvas.action.delete')}</span>
220
+ <span class="shortcut">{menuKeys.delete}</span>
197
221
  </button>
198
222
  {:else}
199
223
  <!-- No selection: only canvas-level actions -->
200
224
  <div class="divider"></div>
201
225
  <button class="item" onclick={() => handleAction(onSelectAll)}>
202
226
  <span class="icon">⊹</span>
203
- <span class="label">Select All</span>
204
- <span class="shortcut">⌘A</span>
227
+ <span class="label">{$t('canvas.action.selectAll')}</span>
228
+ <span class="shortcut">{menuKeys.selectAll}</span>
205
229
  </button>
206
230
  {/if}
207
231
  </div>
@@ -232,7 +256,7 @@
232
256
  border-radius: 5px;
233
257
  cursor: pointer;
234
258
  color: #111827;
235
- text-align: left;
259
+ text-align: start;
236
260
  transition: background 0.1s;
237
261
  }
238
262
 
@@ -270,7 +294,7 @@
270
294
  .shortcut {
271
295
  color: #9ca3af;
272
296
  font-size: 11px;
273
- margin-left: auto;
297
+ margin-inline-start: auto;
274
298
  }
275
299
 
276
300
  .divider {
@@ -1,6 +1,10 @@
1
1
  <script lang="ts">
2
2
  // Pan Zoom Panel - Canvas navigation and view controls
3
3
  import Tooltip from '../components/Tooltip.svelte';
4
+ import { useCanvasLanguage } from '../i18n/language.js';
5
+
6
+ // The words of the panel, in the language of the canvas it is on (English when it is used on its own)
7
+ const { t } = useCanvasLanguage();
4
8
 
5
9
  let className = '';
6
10
  export { className as class };
@@ -70,12 +74,12 @@
70
74
 
71
75
  <div class="pan-zoom-panel-ct {className ? className + ' ' : ''}pan-zoom-panel {isMinimized ? 'pan-zoom-panel-minimized' : ''}" style={style}>
72
76
  {#if isMinimized}
73
- <Tooltip text="Zoom {zoomLevel}% — click to expand" position="above">
77
+ <Tooltip text={$t('canvas.zoom.expandTitle', { percent: String(zoomLevel) })} position="above">
74
78
  <button
75
79
  type="button"
76
80
  class="panel-btn pan-zoom-expand-btn"
77
81
  onclick={toggleMinimized}
78
- aria-label="Expand zoom controls"
82
+ aria-label={$t('canvas.zoom.expandLabel')}
79
83
  >
80
84
  <span class="text-btn">{zoomLevel}%</span>
81
85
  </button>
@@ -83,11 +87,11 @@
83
87
  {:else}
84
88
  {#if advancedFeatures}
85
89
  <!-- Grid Toggle -->
86
- <Tooltip text="Toggle Grid [G]" position="above">
90
+ <Tooltip text={$t('canvas.zoom.gridTitle')} position="above">
87
91
  <button
88
92
  class="panel-btn grid-btn {showGrid ? 'active' : ''}"
89
93
  onclick={onGridToggle}
90
- aria-label="Toggle grid"
94
+ aria-label={$t('canvas.zoom.gridLabel')}
91
95
  >
92
96
  <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
93
97
  <rect x="3" y="3" width="7" height="7"/>
@@ -101,11 +105,11 @@
101
105
  <div class="divider"></div>
102
106
 
103
107
  <!-- Pan Tool -->
104
- <Tooltip text="Hand / Pan [H]" position="above">
108
+ <Tooltip text={$t('canvas.zoom.panTitle')} position="above">
105
109
  <button
106
110
  class="panel-btn pan-btn {isPanActive ? 'active' : ''}"
107
111
  onclick={onPanToggle}
108
- aria-label="Hand / pan tool"
112
+ aria-label={$t('canvas.zoom.panLabel')}
109
113
  >
110
114
  <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
111
115
  <path d="M18 11V6a2 2 0 0 0-2-2v0a2 2 0 0 0-2 2v0"/>
@@ -120,12 +124,12 @@
120
124
  {/if}
121
125
 
122
126
  <!-- Zoom Out -->
123
- <Tooltip text="Zoom Out [Ctrl/Cmd] + [−]" position="above">
127
+ <Tooltip text={$t('canvas.zoom.outTitle')} position="above">
124
128
  <button
125
129
  class="panel-btn"
126
130
  onclick={onZoomOut}
127
131
  disabled={zoomLevel <= 10}
128
- aria-label="Zoom out"
132
+ aria-label={$t('canvas.zoom.outLabel')}
129
133
  >
130
134
  <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
131
135
  <circle cx="11" cy="11" r="8"/>
@@ -137,12 +141,12 @@
137
141
 
138
142
  <!-- Zoom Level Input -->
139
143
  <div class="zoom-level">
140
- <Tooltip text="Reset zoom to 100% [Ctrl/Cmd] + [0]" position="above">
144
+ <Tooltip text={$t('canvas.zoom.resetTitle')} position="above">
141
145
  <button
142
146
  type="button"
143
147
  class="zoom-reset-in-input"
144
148
  onclick={onZoomReset}
145
- aria-label="Reset zoom to 100%"
149
+ aria-label={$t('canvas.zoom.resetLabel')}
146
150
  >
147
151
  <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
148
152
  <polyline points="1 4 1 10 7 10"/>
@@ -158,17 +162,18 @@
158
162
  onblur={handleZoomInputBlur}
159
163
  onkeydown={handleZoomInputKeydown}
160
164
  maxlength="3"
165
+ aria-label={$t('canvas.zoom.levelLabel')}
161
166
  />
162
167
  <span class="zoom-unit">%</span>
163
168
  </div>
164
169
 
165
170
  <!-- Zoom In -->
166
- <Tooltip text="Zoom In [Ctrl/Cmd] + [+]" position="above">
171
+ <Tooltip text={$t('canvas.zoom.inTitle')} position="above">
167
172
  <button
168
173
  class="panel-btn"
169
174
  onclick={onZoomIn}
170
175
  disabled={zoomLevel >= 500}
171
- aria-label="Zoom in"
176
+ aria-label={$t('canvas.zoom.inLabel')}
172
177
  >
173
178
  <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
174
179
  <circle cx="11" cy="11" r="8"/>
@@ -183,21 +188,22 @@
183
188
  <div class="divider"></div>
184
189
 
185
190
  <!-- Reset Zoom (100%) -->
186
- <Tooltip text="Reset Zoom 100% [Ctrl/Cmd] + [0]" position="above">
191
+ <Tooltip text={$t('canvas.zoom.oneToOneTitle')} position="above">
187
192
  <button
188
193
  class="panel-btn"
189
194
  onclick={onZoomReset}
195
+ aria-label={$t('canvas.zoom.resetLabel')}
190
196
  >
191
197
  <span class="text-btn">1:1</span>
192
198
  </button>
193
199
  </Tooltip>
194
200
 
195
201
  <!-- Fit to View -->
196
- <Tooltip text="Fit Content to View [Ctrl/Cmd] + [9]" position="above">
202
+ <Tooltip text={$t('canvas.zoom.fitTitle')} position="above">
197
203
  <button
198
204
  class="panel-btn"
199
205
  onclick={onZoomFit}
200
- aria-label="Fit content to view"
206
+ aria-label={$t('canvas.zoom.fitLabel')}
201
207
  >
202
208
  <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
203
209
  <path d="M8 3H5a2 2 0 0 0-2 2v3"/>
@@ -211,11 +217,11 @@
211
217
  <div class="divider"></div>
212
218
 
213
219
  <!-- Keyboard Shortcuts — moved here from toolbar so it lives with view controls, not drawing tools -->
214
- <Tooltip text="Keyboard Shortcuts [?]" position="above">
220
+ <Tooltip text={$t('canvas.zoom.shortcutsTitle')} position="above">
215
221
  <button
216
222
  class="panel-btn shortcuts-btn {shortcutsOpen ? 'active' : ''}"
217
223
  onclick={onShortcutsToggle}
218
- aria-label="Toggle keyboard shortcuts panel"
224
+ aria-label={$t('canvas.zoom.shortcutsLabel')}
219
225
  >
220
226
  <!-- Keyboard icon — larger so it's clearly readable -->
221
227
  <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
@@ -228,10 +234,11 @@
228
234
 
229
235
  {#if showSaveButton && advancedFeatures}
230
236
  <div class="divider"></div>
231
- <Tooltip text={hasUnsavedChanges ? 'Unsaved changes — click to save [Cmd+S]' : 'All changes saved [Cmd+S]'} position="above">
237
+ <Tooltip text={$t(hasUnsavedChanges ? 'canvas.zoom.unsavedTitle' : 'canvas.zoom.savedTitle')} position="above">
232
238
  <button
233
239
  class="panel-btn save-btn {hasUnsavedChanges ? 'save-btn--dirty' : ''}"
234
240
  onclick={() => onSave?.()}
241
+ aria-label={$t(hasUnsavedChanges ? 'canvas.zoom.unsavedTitle' : 'canvas.zoom.savedTitle')}
235
242
  >
236
243
  {#if hasUnsavedChanges}
237
244
  <span class="save-dirty-dot">●</span>
@@ -247,12 +254,12 @@
247
254
  {/if}
248
255
 
249
256
  <div class="pan-zoom-minimize-anchor">
250
- <Tooltip text="Minimize Zoom Panel" position="above">
257
+ <Tooltip text={$t('canvas.zoom.minimize')} position="above">
251
258
  <button
252
259
  type="button"
253
260
  class="pan-zoom-minimize-btn"
254
261
  onclick={toggleMinimized}
255
- aria-label="Minimize Zoom Panel"
262
+ aria-label={$t('canvas.zoom.minimize')}
256
263
  >
257
264
  <svg width="8" height="8" viewBox="0 0 12 12" fill="none" aria-hidden="true">
258
265
  <rect x="2" y="5.25" width="8" height="1.5" rx="0.25" fill="currentColor"/>
@@ -282,7 +289,7 @@
282
289
  .pan-zoom-minimize-anchor {
283
290
  position: absolute;
284
291
  top: -8px;
285
- right: -8px;
292
+ inset-inline-end: -8px;
286
293
  z-index: 2;
287
294
  }
288
295
 
@@ -414,7 +421,8 @@
414
421
  width: 18px;
415
422
  height: 24px;
416
423
  padding: 0;
417
- margin: 0 0 0 2px;
424
+ margin: 0;
425
+ margin-inline-start: 2px;
418
426
  border: none;
419
427
  background: transparent;
420
428
  color: #6b7280;
@@ -445,7 +453,8 @@
445
453
  .zoom-unit {
446
454
  width: 18px;
447
455
  padding: 4px 0;
448
- margin: 0 2px 0 0;
456
+ margin: 0;
457
+ margin-inline-end: 2px;
449
458
  font-size: 11px;
450
459
  color: #6b7280;
451
460
  text-align: center;
@@ -128,7 +128,7 @@
128
128
  </script>
129
129
 
130
130
  <Portal {target} style="z-index: 70; pointer-events: none;">
131
- <div class="canvas-widget-selection-layer fixed pointer-events-none overflow-hidden {className}" style="left: {viewport.left}px; top: {viewport.top}px; width: {viewport.width}px; height: {viewport.height}px; pointer-events: none; {style}">
131
+ <div class="canvas-widget-selection-layer fixed pointer-events-none overflow-hidden {className}" style="left: {viewport.left}px; /* rtl-ok: laid over the board where the board is on the screen, measured from the left edge of the window */ top: {viewport.top}px; width: {viewport.width}px; height: {viewport.height}px; pointer-events: none; {style}">
132
132
  <canvas class="canvas-widget-selection-paint h-full w-full pointer-events-none" use:captureDrawingCanvas aria-hidden="true"></canvas>
133
133
  <svg class="canvas-widget-control-hit-regions absolute inset-0 h-full w-full pointer-events-none" aria-hidden="true">
134
134
  {#each controls as control (control.key)}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Which events and which focus belong to a canvas that sits in a shadow root.
3
+ *
4
+ * On the Widgetic site the canvas is in the page itself, and every key press, click and paste the page sees is the
5
+ * canvas's to look at. A partner who mounts the creator puts it in a shadow root, and then three things change:
6
+ *
7
+ * - an event that a listener on `window` or `document` receives has its `target` set to the shadow root's host element,
8
+ * so `event.target.closest(…)` and `menu.contains(event.target)` answer for the host and not for what was pressed;
9
+ * - `document.activeElement` is the host too, never the text box inside it that has the focus;
10
+ * - the same listeners also hear what happens on the rest of the partner's page.
11
+ *
12
+ * The helpers below answer from the event's `composedPath()` and from the shadow root's own `activeElement`. On an
13
+ * ordinary page (no shadow root) they answer exactly as the plain DOM calls did, so the Widgetic site behaves as before.
14
+ *
15
+ * Kept to plain DOM calls with no reference to a global that only a browser has, so that it can be tested by Node.
16
+ */
17
+ /** The shadow root a node lives in, or null when it lives in the page itself (or is not in a tree at all). */
18
+ export declare function getShadowRootOf(node: Node | null | undefined): ShadowRoot | null;
19
+ /**
20
+ * Whether an event is one for the canvas whose element is `scopeNode`.
21
+ *
22
+ * On an ordinary page that is every event (as it always was). When the canvas is in a shadow root, only the ones that
23
+ * went through that shadow root: a key pressed while the focus is in the creator, a click on something in it. A key
24
+ * pressed on the partner's own page is not.
25
+ *
26
+ * Has to be called while the event is being dispatched (`composedPath()` is empty afterwards).
27
+ */
28
+ export declare function eventIsForScope(event: Event, scopeNode: Node | null | undefined): boolean;
29
+ /** The element that has the focus in the tree `scopeNode` lives in: the page's, or the shadow root's own. */
30
+ export declare function getFocusedElementIn(scopeNode: Node | null | undefined): Element | null;
31
+ /**
32
+ * The element an event really started on. A listener on `window` or `document` is told the shadow root's host for an
33
+ * event from inside a shadow root; the first element of the path is the one that was pressed.
34
+ */
35
+ export declare function getEventOrigin(event: Event): Element | null;
36
+ /**
37
+ * Whether an event went through `element`, from inside it or on it. The answer for a click on a menu is the same in a
38
+ * shadow root as on the page, where `element.contains(event.target)` gives a wrong "no" inside a shadow root.
39
+ */
40
+ export declare function eventWentThrough(event: Event, element: Element | null | undefined): boolean;
41
+ /**
42
+ * Where a short-lived element that has to sit in the document (a hidden file input that is clicked and then removed,
43
+ * a measuring element) goes for a canvas whose element is `scopeNode`: the shadow root it lives in, so that the partner's
44
+ * page never sees it, and otherwise `document.body`.
45
+ */
46
+ export declare function getTransientHostFor(scopeNode: Node | null | undefined): Node;
47
+ /**
48
+ * Whether the canvas whose element is `scopeNode` should take the keyboard back after the element that held it went away
49
+ * (Fabric's hidden text area, which is taken out of the page when text editing ends, and leaves the focus on nothing).
50
+ *
51
+ * Inside a shadow root the canvas's shortcuts follow the focus, so a focus that fell to nothing (the page's `<body>`, or
52
+ * the shadow root's host alone) has to come back to the canvas. A focus that went to something else is not ours to take:
53
+ * something inside the creator has it, or the visitor clicked on a field of the partner's own page. On an ordinary page
54
+ * the shortcuts do not mind where the focus is, so there is nothing to do.
55
+ */
56
+ export declare function shouldTakeFocusBack(scopeNode: Node | null | undefined): boolean;
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Which events and which focus belong to a canvas that sits in a shadow root.
3
+ *
4
+ * On the Widgetic site the canvas is in the page itself, and every key press, click and paste the page sees is the
5
+ * canvas's to look at. A partner who mounts the creator puts it in a shadow root, and then three things change:
6
+ *
7
+ * - an event that a listener on `window` or `document` receives has its `target` set to the shadow root's host element,
8
+ * so `event.target.closest(…)` and `menu.contains(event.target)` answer for the host and not for what was pressed;
9
+ * - `document.activeElement` is the host too, never the text box inside it that has the focus;
10
+ * - the same listeners also hear what happens on the rest of the partner's page.
11
+ *
12
+ * The helpers below answer from the event's `composedPath()` and from the shadow root's own `activeElement`. On an
13
+ * ordinary page (no shadow root) they answer exactly as the plain DOM calls did, so the Widgetic site behaves as before.
14
+ *
15
+ * Kept to plain DOM calls with no reference to a global that only a browser has, so that it can be tested by Node.
16
+ */
17
+ /** The shadow root a node lives in, or null when it lives in the page itself (or is not in a tree at all). */
18
+ export function getShadowRootOf(node) {
19
+ const root = node?.getRootNode?.();
20
+ // (`instanceof ShadowRoot` would not hold for a root made by another window, an iframe's, say.)
21
+ const isShadowRoot = !!root && root.nodeType === 11 && 'host' in root;
22
+ return isShadowRoot ? root : null;
23
+ }
24
+ /**
25
+ * Whether an event is one for the canvas whose element is `scopeNode`.
26
+ *
27
+ * On an ordinary page that is every event (as it always was). When the canvas is in a shadow root, only the ones that
28
+ * went through that shadow root: a key pressed while the focus is in the creator, a click on something in it. A key
29
+ * pressed on the partner's own page is not.
30
+ *
31
+ * Has to be called while the event is being dispatched (`composedPath()` is empty afterwards).
32
+ */
33
+ export function eventIsForScope(event, scopeNode) {
34
+ const shadowRoot = getShadowRootOf(scopeNode);
35
+ if (!shadowRoot)
36
+ return true;
37
+ return event.composedPath().includes(shadowRoot);
38
+ }
39
+ /** The element that has the focus in the tree `scopeNode` lives in: the page's, or the shadow root's own. */
40
+ export function getFocusedElementIn(scopeNode) {
41
+ const shadowRoot = getShadowRootOf(scopeNode);
42
+ return shadowRoot ? shadowRoot.activeElement : document.activeElement;
43
+ }
44
+ /**
45
+ * The element an event really started on. A listener on `window` or `document` is told the shadow root's host for an
46
+ * event from inside a shadow root; the first element of the path is the one that was pressed.
47
+ */
48
+ export function getEventOrigin(event) {
49
+ const first = event.composedPath()[0] ?? event.target;
50
+ return first && first.nodeType === 1 ? first : null;
51
+ }
52
+ /**
53
+ * Whether an event went through `element`, from inside it or on it. The answer for a click on a menu is the same in a
54
+ * shadow root as on the page, where `element.contains(event.target)` gives a wrong "no" inside a shadow root.
55
+ */
56
+ export function eventWentThrough(event, element) {
57
+ if (!element)
58
+ return false;
59
+ return event.composedPath().includes(element);
60
+ }
61
+ /**
62
+ * Where a short-lived element that has to sit in the document (a hidden file input that is clicked and then removed,
63
+ * a measuring element) goes for a canvas whose element is `scopeNode`: the shadow root it lives in, so that the partner's
64
+ * page never sees it, and otherwise `document.body`.
65
+ */
66
+ export function getTransientHostFor(scopeNode) {
67
+ return getShadowRootOf(scopeNode) ?? document.body;
68
+ }
69
+ /**
70
+ * Whether the canvas whose element is `scopeNode` should take the keyboard back after the element that held it went away
71
+ * (Fabric's hidden text area, which is taken out of the page when text editing ends, and leaves the focus on nothing).
72
+ *
73
+ * Inside a shadow root the canvas's shortcuts follow the focus, so a focus that fell to nothing (the page's `<body>`, or
74
+ * the shadow root's host alone) has to come back to the canvas. A focus that went to something else is not ours to take:
75
+ * something inside the creator has it, or the visitor clicked on a field of the partner's own page. On an ordinary page
76
+ * the shortcuts do not mind where the focus is, so there is nothing to do.
77
+ */
78
+ export function shouldTakeFocusBack(scopeNode) {
79
+ const shadowRoot = getShadowRootOf(scopeNode);
80
+ if (!shadowRoot)
81
+ return false;
82
+ if (shadowRoot.activeElement)
83
+ return false;
84
+ // The focus in the tree the shadow root's host is in (the page's, or another shadow root's).
85
+ const outerTree = shadowRoot.host.getRootNode();
86
+ const focusOutside = outerTree.activeElement;
87
+ const outerBody = outerTree.body ?? null;
88
+ return !focusOutside || focusOutside === shadowRoot.host || focusOutside === outerBody;
89
+ }