@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
@@ -42,7 +42,18 @@
42
42
  type PathCommand
43
43
  } from './shapes/strokeShapeSnap';
44
44
  import { canvasLog, canvasWarn, setCanvasLogsEnabled } from './canvasLogger';
45
+ import {
46
+ eventIsForScope,
47
+ getEventOrigin,
48
+ getFocusedElementIn,
49
+ getShadowRootOf,
50
+ getTransientHostFor,
51
+ shouldTakeFocusBack
52
+ } from './eventScope';
53
+ import { installTextareaInShadowRoot } from './shadowRootText';
45
54
  import type { WidgetConversionPayload, StrokeRecognizer } from './types';
55
+ import { provideCanvasLanguage } from '../i18n/language.js';
56
+ import { makeShapeWords, setShapeWords, shapeWordsOf } from '../i18n/shapeWords.js';
46
57
 
47
58
  // ── Library shape path fingerprints (migration for old saves) ──────────────
48
59
  // Maps a normalized path string → shape key. Used to identify library shapes
@@ -268,6 +279,14 @@
268
279
  // Set to false in production or when logs clutter the console.
269
280
  export let showLogs: boolean = true;
270
281
 
282
+ /**
283
+ * The language of the canvas: `locale` ("de", "pt-BR") picks the plural rules, the number format and the language
284
+ * the page's own words are in; `messages` are the page's own words, keyed like CANVAS_MESSAGES (a key left out
285
+ * keeps its English text). Without them the canvas speaks English. Both can change while the canvas is open.
286
+ */
287
+ export let locale: string | null = null;
288
+ export let messages: Readonly<Record<string, unknown>> | null = null;
289
+
271
290
  /**
272
291
  * COMPONENT STATE
273
292
  */
@@ -288,6 +307,48 @@
288
307
  */
289
308
  let canvas: Canvas | null = null;
290
309
 
310
+ // The language of everything on the canvas: the toolbar, the panels and the menu read their words from it, and the
311
+ // shapes read the words they draw onto the canvas itself (see i18n/shapeWords.ts).
312
+ const language = provideCanvasLanguage({ locale, messages });
313
+ const { t } = language;
314
+ $: language.update({ locale, messages });
315
+ // The shapes read their words from the canvas they are on: set when the canvas is made, and again (with a redraw)
316
+ // whenever the page sets another language
317
+ $: if (canvas) {
318
+ setShapeWords(canvas, makeShapeWords($t));
319
+ canvas.requestRenderAll();
320
+ }
321
+
322
+ /**
323
+ * Whether a key press or a paste is for this canvas. On an ordinary page every one is. When the canvas is in a
324
+ * shadow root (a partner's page that mounts the creator) only the ones that come from inside it are, so that Delete,
325
+ * the arrows, Ctrl+A and the rest never act on keys pressed on the partner's own page. See eventScope.ts.
326
+ */
327
+ const isForThisCanvas = (event: Event): boolean => eventIsForScope(event, canvasEl);
328
+
329
+ /** The element that has the focus in this canvas's tree: the page's, or the shadow root's own (never just its host). */
330
+ const getFocusedElement = (): Element | null => getFocusedElementIn(canvasEl);
331
+
332
+ /**
333
+ * Fabric's hidden text area has the keyboard while text is edited, and is taken out of the page when the editing ends,
334
+ * which leaves the focus on nothing. In a shadow root the shortcuts follow the focus, so the canvas takes it back (not
335
+ * when the visitor went to something else, which then has it). Does nothing on an ordinary page.
336
+ */
337
+ function takeFocusBackAfterTextEditing(): void {
338
+ // (After Fabric has taken its text area out.)
339
+ setTimeout(() => {
340
+ try {
341
+ if (!canvas || !shouldTakeFocusBack(canvasEl)) return;
342
+ canvas.upperCanvasEl?.focus({ preventScroll: true });
343
+ } catch (focusError) {
344
+ canvasWarn('[Canvas] Could not give the focus back to the canvas after text editing:', focusError);
345
+ }
346
+ }, 0);
347
+ }
348
+
349
+ /** The cursor rules this canvas put into its shadow root (null when it is on an ordinary page, where they go to the <head>). */
350
+ let rotateCursorStyleInShadowRoot: HTMLStyleElement | null = null;
351
+
291
352
  // Canvas state & configuration
292
353
  /**
293
354
  * Lifecycle phase of the canvas as an explicit union. Replaces the
@@ -648,7 +709,8 @@
648
709
  }
649
710
 
650
711
  // Text settings
651
- let textBoxPlaceholder = 'Type here';
712
+ // What a new text box starts with, in the language of the canvas
713
+ $: textBoxPlaceholder = $t('canvas.defaults.textBox');
652
714
  let textFontSize = 20;
653
715
  let textFontFamily = 'Arial';
654
716
  let textFillColor = '#111827';
@@ -896,8 +958,8 @@
896
958
  const screenshot = await captureObjectScreenshot(obj);
897
959
 
898
960
  const objectName = isFrame
899
- ? (obj._labelText || 'Untitled Frame')
900
- : (obj._customType || obj.type || 'Untitled Shape');
961
+ ? (obj._labelText || $t('canvas.defaults.untitledFrame'))
962
+ : (obj._customType || obj.type || $t('canvas.defaults.untitledShape'));
901
963
 
902
964
  const shapeTypes = isFrame
903
965
  ? [...new Set(children.map((c: any) => c._customType || c.type || 'unknown'))]
@@ -1518,7 +1580,7 @@
1518
1580
  if (anyObj._widgetOrphaned !== orphaned) {
1519
1581
  anyObj._widgetOrphaned = orphaned;
1520
1582
  if (orphaned) {
1521
- anyObj._widgetName = 'Unlinked widget';
1583
+ anyObj._widgetName = $t('canvas.widget.unlinkedName');
1522
1584
  }
1523
1585
  marked++;
1524
1586
  }
@@ -1618,12 +1680,12 @@
1618
1680
  function getPanZoomPositionClass(position: string): string {
1619
1681
  canvasLog('getPanZoomPositionClass: ', position);
1620
1682
  const positions: Record<string, string> = {
1621
- 'TL': 'absolute z-10 top-4 left-4',
1683
+ 'TL': 'absolute z-10 top-4 left-4', // rtl-ok: the page names a corner of the screen, as it is drawn
1622
1684
  'TC': 'absolute z-10 top-4 left-1/2 -translate-x-1/2',
1623
- 'TR': 'absolute z-10 top-4 right-4',
1624
- 'BL': 'absolute z-10 bottom-20 left-4',
1685
+ 'TR': 'absolute z-10 top-4 right-4', // rtl-ok: the page names a corner of the screen, as it is drawn
1686
+ 'BL': 'absolute z-10 bottom-20 left-4', // rtl-ok: the page names a corner of the screen, as it is drawn
1625
1687
  'BC': 'absolute z-10 bottom-20 left-1/2 -translate-x-1/2',
1626
- 'BR': 'absolute z-10 bottom-20 right-4'
1688
+ 'BR': 'absolute z-10 bottom-20 right-4' // rtl-ok: the page names a corner of the screen, as it is drawn
1627
1689
  };
1628
1690
  return positions[position] || positions['TL'];
1629
1691
  }
@@ -1644,6 +1706,9 @@
1644
1706
  }
1645
1707
 
1646
1708
  try {
1709
+ // A text being edited types into a hidden text area: for a canvas in a shadow root it stays in there (see shadowRootText.ts).
1710
+ installTextareaInShadowRoot();
1711
+
1647
1712
  canvas = new Canvas(canvasEl, {
1648
1713
  width: canvasEl.clientWidth,
1649
1714
  height: canvasEl.clientHeight,
@@ -1709,7 +1774,10 @@
1709
1774
  // Inject a style element at runtime for retina-aware cursor using image-set.
1710
1775
  // Done via JS (not Svelte style block) to bypass PostCSS/Tailwind preprocessing.
1711
1776
  const ROTATE_CURSOR_STYLE_ID = 'fabric-rotate-cursor-style';
1712
- if (!document.getElementById(ROTATE_CURSOR_STYLE_ID)) {
1777
+ // The rules go where the canvas is: the page's <head>, or — for a canvas in a shadow root, where the page's
1778
+ // rules do not reach and a partner's page is not ours to change — the shadow root itself.
1779
+ const cursorStyleShadowRoot = getShadowRootOf(canvasEl);
1780
+ if (!(cursorStyleShadowRoot ?? document).getElementById(ROTATE_CURSOR_STYLE_ID)) {
1713
1781
  var cursorStyleEl = document.createElement('style');
1714
1782
  cursorStyleEl.id = ROTATE_CURSOR_STYLE_ID;
1715
1783
  var cssUrl1x = "url('data:image/svg+xml," + rotSvgBase1x + "') 10 10, crosshair !important";
@@ -1720,7 +1788,9 @@
1720
1788
  + 'cursor: ' + cssImageSet + '; '
1721
1789
  + 'cursor: ' + cssImageSetStd + '; '
1722
1790
  + '}';
1723
- document.head.appendChild(cursorStyleEl);
1791
+ (cursorStyleShadowRoot ?? document.head).appendChild(cursorStyleEl);
1792
+ // Only the shadow root's copy is taken away again when this canvas goes: the page's is shared and always was left.
1793
+ rotateCursorStyleInShadowRoot = cursorStyleShadowRoot ? cursorStyleEl : null;
1724
1794
  }
1725
1795
 
1726
1796
  // Track rotation state for cursor class management
@@ -3138,6 +3208,7 @@
3138
3208
  // event, preventing our window-level handlers from seeing it.
3139
3209
  canvas.on('text:editing:exited', () => {
3140
3210
  takeHistorySnapshot();
3211
+ takeFocusBackAfterTextEditing();
3141
3212
 
3142
3213
  if (activeEditingTextbox) {
3143
3214
  detachActiveTextListeners();
@@ -3446,11 +3517,13 @@
3446
3517
 
3447
3518
  // Global keydown handler for Delete/Backspace to delete selected objects
3448
3519
  const handleDeleteKeydown = (e: KeyboardEvent) => {
3520
+ // Not for a key pressed elsewhere on a page that has this canvas in a shadow root
3521
+ if (!isForThisCanvas(e)) return;
3449
3522
  // Skip if we're in text editing mode
3450
3523
  if (activeEditingTextbox) return;
3451
3524
 
3452
3525
  // Skip if focus is on text input
3453
- const activeElement = document.activeElement as HTMLElement | null;
3526
+ const activeElement = getFocusedElement() as HTMLElement | null;
3454
3527
  if (activeElement) {
3455
3528
  const tagName = activeElement.tagName;
3456
3529
  const isTextInput = tagName === 'INPUT' || tagName === 'TEXTAREA';
@@ -3471,11 +3544,13 @@
3471
3544
 
3472
3545
  // Global keydown handler for Arrow keys to move selected objects
3473
3546
  const handleArrowKeydown = (e: KeyboardEvent) => {
3547
+ // Not for a key pressed elsewhere on a page that has this canvas in a shadow root
3548
+ if (!isForThisCanvas(e)) return;
3474
3549
  // Skip if we're in text editing mode
3475
3550
  if (activeEditingTextbox) return;
3476
3551
 
3477
3552
  // Skip if focus is on text input
3478
- const activeElement = document.activeElement as HTMLElement | null;
3553
+ const activeElement = getFocusedElement() as HTMLElement | null;
3479
3554
  if (activeElement) {
3480
3555
  const tagName = activeElement.tagName;
3481
3556
  const isTextInput = tagName === 'INPUT' || tagName === 'TEXTAREA';
@@ -3558,8 +3633,10 @@
3558
3633
 
3559
3634
  const handleSelectAllKeydown = (e: KeyboardEvent) => {
3560
3635
  if (!((e.metaKey || e.ctrlKey) && e.key === 'a')) return;
3636
+ // Not for a key pressed elsewhere on a page that has this canvas in a shadow root
3637
+ if (!isForThisCanvas(e)) return;
3561
3638
 
3562
- const activeElement = document.activeElement as HTMLElement | null;
3639
+ const activeElement = getFocusedElement() as HTMLElement | null;
3563
3640
 
3564
3641
  // Allow Cmd+A inside form controls and contenteditable elements
3565
3642
  if (activeElement && (
@@ -3672,6 +3749,8 @@
3672
3749
  const handleEscapeKeydown = (e: KeyboardEvent) => {
3673
3750
  if (e.key === 'Escape') {
3674
3751
  if (!canvas) return;
3752
+ // Not for a key pressed elsewhere on a page that has this canvas in a shadow root
3753
+ if (!isForThisCanvas(e)) return;
3675
3754
 
3676
3755
  // ALWAYS close PropsPanel first
3677
3756
  selectedObject = null;
@@ -3841,11 +3920,18 @@
3841
3920
 
3842
3921
  ctx.fillStyle = '#94a3b8';
3843
3922
 
3923
+ // The three lines are in the language of the canvas (i18n/shapeWords.ts). The block is made as wide
3924
+ // as the longest, so a language with longer words shrinks the block instead of overflowing the box.
3925
+ const words = shapeWordsOf(self.canvas);
3926
+ const placeholderLines = [words.dropImage, words.or, words.doubleClick];
3927
+ ctx.font = '12px Arial';
3928
+ const contentWidth = Math.max(100, ...placeholderLines.map((line) => ctx.measureText(line).width));
3929
+
3844
3930
  // contentScale shrinks icon+text when placeholder is very small
3845
3931
  const availableW = self.width || 100;
3846
3932
  const availableH = self.height || 100;
3847
3933
  const contentHeight = 76; // icon(24) + gap(8) + 3×line(12) + 2×gap(4)
3848
- const contentScale = Math.min(1, availableH / contentHeight, availableW / 100);
3934
+ const contentScale = Math.min(1, availableH / contentHeight, availableW / contentWidth);
3849
3935
 
3850
3936
  if (contentScale < 0.3) {
3851
3937
  // Too small — show minimal icon only
@@ -3865,9 +3951,7 @@
3865
3951
 
3866
3952
  const fontSize = 12;
3867
3953
  ctx.font = `${fontSize}px Arial`;
3868
- ctx.fillText('Drop image', 0, 0);
3869
- ctx.fillText('or', 0, fontSize + 4);
3870
- ctx.fillText('double click', 0, (fontSize + 4) * 2);
3954
+ placeholderLines.forEach((line, lineIndex) => ctx.fillText(line, 0, (fontSize + 4) * lineIndex));
3871
3955
 
3872
3956
  ctx.restore();
3873
3957
  }
@@ -4364,6 +4448,8 @@
4364
4448
  const handleCopyKeydown = (e: KeyboardEvent) => {
4365
4449
  if ((e.metaKey || e.ctrlKey) && e.key === 'c') {
4366
4450
  if (!canvas) return;
4451
+ // Not for a key pressed elsewhere on a page that has this canvas in a shadow root
4452
+ if (!isForThisCanvas(e)) return;
4367
4453
 
4368
4454
  // Check if we're editing text - if so, don't intercept
4369
4455
  if (activeEditingTextbox && activeEditingTextbox.isEditing) {
@@ -4371,7 +4457,7 @@
4371
4457
  }
4372
4458
 
4373
4459
  // Check if focus is on an input, textarea, or contenteditable — don't intercept
4374
- const activeElement = document.activeElement;
4460
+ const activeElement = getFocusedElement();
4375
4461
  if (activeElement && (
4376
4462
  activeElement.tagName === 'INPUT' ||
4377
4463
  activeElement.tagName === 'TEXTAREA' ||
@@ -4435,11 +4521,13 @@
4435
4521
  const handleCutKeydown = (e: KeyboardEvent) => {
4436
4522
  if ((e.metaKey || e.ctrlKey) && e.key === 'x') {
4437
4523
  if (!canvas) return;
4524
+ // Not for a key pressed elsewhere on a page that has this canvas in a shadow root
4525
+ if (!isForThisCanvas(e)) return;
4438
4526
 
4439
4527
  // Check if we're editing text - let the browser handle text cut
4440
4528
  if (activeEditingTextbox && activeEditingTextbox.isEditing) return;
4441
4529
 
4442
- const activeElement = document.activeElement;
4530
+ const activeElement = getFocusedElement();
4443
4531
  if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) return;
4444
4532
 
4445
4533
  const activeObject = canvas.getActiveObject();
@@ -4467,7 +4555,7 @@
4467
4555
  return;
4468
4556
  }
4469
4557
 
4470
- const activeElement = document.activeElement;
4558
+ const activeElement = getFocusedElement();
4471
4559
  if (activeElement && (
4472
4560
  activeElement.tagName === 'INPUT' ||
4473
4561
  activeElement.tagName === 'TEXTAREA' ||
@@ -4476,7 +4564,8 @@
4476
4564
  return;
4477
4565
  }
4478
4566
 
4479
- const target = e.target as HTMLElement;
4567
+ // (The element the key went to, not the host element that a shadow root makes the target of a window listener.)
4568
+ const target = getEventOrigin(e);
4480
4569
  if (target?.closest?.('.widget-details-floating-panel, .widgets-list-panel, [data-widget-details]')) {
4481
4570
  return;
4482
4571
  }
@@ -4490,6 +4579,8 @@
4490
4579
  const handleDuplicateKeydown = (e: KeyboardEvent) => {
4491
4580
  if ((e.metaKey || e.ctrlKey) && e.key === 'd') {
4492
4581
  if (!canvas) return;
4582
+ // Not for a key pressed elsewhere on a page that has this canvas in a shadow root
4583
+ if (!isForThisCanvas(e)) return;
4493
4584
 
4494
4585
  // Check if we're editing text - if so, don't intercept
4495
4586
  if (activeEditingTextbox && activeEditingTextbox.isEditing) {
@@ -4497,7 +4588,7 @@
4497
4588
  }
4498
4589
 
4499
4590
  // Check if focus is on an input or textarea - don't intercept
4500
- const activeElement = document.activeElement;
4591
+ const activeElement = getFocusedElement();
4501
4592
  if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
4502
4593
  return;
4503
4594
  }
@@ -4530,15 +4621,18 @@
4530
4621
  // Priority: OS clipboard image wins if present, otherwise fall back to internal canvas clipboard.
4531
4622
  const handleOsClipboardPaste = (e: ClipboardEvent) => {
4532
4623
  if (!canvas) return;
4624
+ // Not for a paste elsewhere on a page that has this canvas in a shadow root
4625
+ if (!isForThisCanvas(e)) return;
4533
4626
  if (activeEditingTextbox && activeEditingTextbox.isEditing) return;
4534
- const activeElement = document.activeElement;
4627
+ const activeElement = getFocusedElement();
4535
4628
  if (activeElement && (
4536
4629
  activeElement.tagName === 'INPUT' ||
4537
4630
  activeElement.tagName === 'TEXTAREA' ||
4538
4631
  (activeElement as HTMLElement).isContentEditable
4539
4632
  )) return;
4540
4633
  // Skip if paste target is inside a Widget Details panel, Widgets List, or similar non-canvas container
4541
- const target = e.target as HTMLElement;
4634
+ // (the element the paste went to, not the host element that a shadow root makes the target of a window listener)
4635
+ const target = getEventOrigin(e);
4542
4636
  if (target?.closest?.('.widget-details-floating-panel, .widgets-list-panel, [data-widget-details]')) return;
4543
4637
 
4544
4638
  const items = e.clipboardData?.items;
@@ -4601,10 +4695,12 @@
4601
4695
  const handleGroupKeydown = (e: KeyboardEvent) => {
4602
4696
  if (!(e.metaKey || e.ctrlKey) || e.key.toLowerCase() !== 'g') return;
4603
4697
  if (!canvas) return;
4698
+ // Not for a key pressed elsewhere on a page that has this canvas in a shadow root
4699
+ if (!isForThisCanvas(e)) return;
4604
4700
 
4605
4701
  // Don't intercept during text editing
4606
4702
  if (activeEditingTextbox && activeEditingTextbox.isEditing) return;
4607
- const activeElement = document.activeElement;
4703
+ const activeElement = getFocusedElement();
4608
4704
  if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) return;
4609
4705
 
4610
4706
  e.preventDefault();
@@ -4624,8 +4720,10 @@
4624
4720
  // ── History: Ctrl+Z (undo) / Ctrl+Y or Ctrl+Shift+Z (redo) ────────
4625
4721
  const handleUndoRedoKeydown = (e: KeyboardEvent) => {
4626
4722
  if (!(e.ctrlKey || e.metaKey)) return;
4723
+ // Not for a key pressed elsewhere on a page that has this canvas in a shadow root
4724
+ if (!isForThisCanvas(e)) return;
4627
4725
  // Skip if focus is in a text input field
4628
- const activeElement = document.activeElement;
4726
+ const activeElement = getFocusedElement();
4629
4727
  if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) return;
4630
4728
  // Skip if editing a textbox on the canvas
4631
4729
  if (activeEditingTextbox && activeEditingTextbox.isEditing) return;
@@ -4648,8 +4746,10 @@
4648
4746
  const isBracketLeft = e.code === 'BracketLeft';
4649
4747
  if (!isBracketRight && !isBracketLeft) return;
4650
4748
  if (!canvas || !selectedObject) return;
4749
+ // Not for a key pressed elsewhere on a page that has this canvas in a shadow root
4750
+ if (!isForThisCanvas(e)) return;
4651
4751
  if (activeEditingTextbox && activeEditingTextbox.isEditing) return;
4652
- const activeElement = document.activeElement;
4752
+ const activeElement = getFocusedElement();
4653
4753
  if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) return;
4654
4754
 
4655
4755
  e.preventDefault();
@@ -4668,6 +4768,8 @@
4668
4768
  // ── Save: Cmd+S / Ctrl+S ─────────────────────────────────────────────
4669
4769
  const handleSaveKeydown = (e: KeyboardEvent) => {
4670
4770
  if (!(e.metaKey || e.ctrlKey) || e.key !== 's') return;
4771
+ // Not for a key pressed elsewhere on a page that has this canvas in a shadow root
4772
+ if (!isForThisCanvas(e)) return;
4671
4773
  e.preventDefault();
4672
4774
  saveNow();
4673
4775
  };
@@ -5984,6 +6086,10 @@
5984
6086
 
5985
6087
  canvas = null;
5986
6088
  }
6089
+
6090
+ // The cursor rules in a shadow root go with the canvas (the page's <head> copy is shared and stays, as it always did).
6091
+ rotateCursorStyleInShadowRoot?.remove();
6092
+ rotateCursorStyleInShadowRoot = null;
5987
6093
  });
5988
6094
 
5989
6095
  function resizeObserver(node: HTMLElement) {
@@ -7866,7 +7972,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7866
7972
  top: finalBounds.top,
7867
7973
  width: finalBounds.width,
7868
7974
  height: finalBounds.height,
7869
- labelText: 'Frame',
7975
+ labelText: $t('canvas.defaults.frameLabel'),
7870
7976
  labelFontSize: 16,
7871
7977
  labelFontFamily: textFontFamily,
7872
7978
  labelColor: '#1f2937',
@@ -8316,6 +8422,13 @@ function checkAndAddNewShapeToFrame(shape: any) {
8316
8422
 
8317
8423
  ctx.fillStyle = '#94a3b8';
8318
8424
 
8425
+ // The three lines are in the language of the canvas (i18n/shapeWords.ts). The block is made as wide
8426
+ // as the longest, so a language with longer words shrinks the block instead of overflowing the box.
8427
+ const words = shapeWordsOf((this as any).canvas);
8428
+ const placeholderLines = [words.dropImage, words.or, words.doubleClick];
8429
+ ctx.font = '12px Arial';
8430
+ const contentWidth = Math.max(100, ...placeholderLines.map((line) => ctx.measureText(line).width));
8431
+
8319
8432
  // Available space in local pixels.
8320
8433
  // contentScale shrinks the icon group when the placeholder is very small
8321
8434
  // so text never overflows the box.
@@ -8324,7 +8437,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8324
8437
 
8325
8438
  // Approx layout height: icon(24) + gap(8) + 3×line(12) + 2×gap(4) = 76px
8326
8439
  const contentHeight = 76;
8327
- const contentScale = Math.min(1, availableH / contentHeight, availableW / 100);
8440
+ const contentScale = Math.min(1, availableH / contentHeight, availableW / contentWidth);
8328
8441
 
8329
8442
  if (contentScale < 0.3) {
8330
8443
  // Too small for text — show a minimal icon only
@@ -8346,9 +8459,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8346
8459
  // Text lines
8347
8460
  const fontSize = 12;
8348
8461
  ctx.font = `${fontSize}px Arial`;
8349
- ctx.fillText('Drop image', 0, 0);
8350
- ctx.fillText('or', 0, fontSize + 4);
8351
- ctx.fillText('double click', 0, (fontSize + 4) * 2);
8462
+ placeholderLines.forEach((line, lineIndex) => ctx.fillText(line, 0, (fontSize + 4) * lineIndex));
8352
8463
 
8353
8464
  ctx.restore();
8354
8465
  }
@@ -8505,6 +8616,16 @@ function checkAndAddNewShapeToFrame(shape: any) {
8505
8616
  });
8506
8617
  }
8507
8618
 
8619
+ /**
8620
+ * Puts a hidden file input where it can be clicked: into the shadow root of a canvas that is in one (the partner's
8621
+ * page never sees it), otherwise into the page's body. It also goes away when the visitor closes the picker without
8622
+ * choosing a file, which fires no `change` event.
8623
+ */
8624
+ function attachTransientInput(fileInput: HTMLInputElement) {
8625
+ getTransientHostFor(canvasEl).appendChild(fileInput);
8626
+ fileInput.addEventListener('cancel', () => fileInput.remove(), { once: true });
8627
+ }
8628
+
8508
8629
  /**
8509
8630
  * Open file picker for an ImagePlaceholder
8510
8631
  * Called when user clicks on placeholder
@@ -8523,15 +8644,15 @@ function checkAndAddNewShapeToFrame(shape: any) {
8523
8644
  const file = target.files?.[0];
8524
8645
 
8525
8646
  if (!file) {
8526
- document.body.removeChild(fileInput);
8647
+ fileInput.remove();
8527
8648
  return;
8528
8649
  }
8529
8650
  if (isVideo && !file.type.startsWith('video/')) {
8530
- document.body.removeChild(fileInput);
8651
+ fileInput.remove();
8531
8652
  return;
8532
8653
  }
8533
8654
  if (!isVideo && !file.type.startsWith('image/')) {
8534
- document.body.removeChild(fileInput);
8655
+ fileInput.remove();
8535
8656
  return;
8536
8657
  }
8537
8658
 
@@ -8541,7 +8662,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8541
8662
  }).catch((err) => {
8542
8663
  console.error('Video poster capture failed', err);
8543
8664
  }).finally(() => {
8544
- if (fileInput.parentNode) document.body.removeChild(fileInput);
8665
+ if (fileInput.parentNode) fileInput.remove();
8545
8666
  });
8546
8667
  return;
8547
8668
  }
@@ -8554,10 +8675,10 @@ function checkAndAddNewShapeToFrame(shape: any) {
8554
8675
  }
8555
8676
  };
8556
8677
  reader.readAsDataURL(file);
8557
- document.body.removeChild(fileInput);
8678
+ fileInput.remove();
8558
8679
  };
8559
8680
 
8560
- document.body.appendChild(fileInput);
8681
+ attachTransientInput(fileInput);
8561
8682
  fileInput.click();
8562
8683
  }
8563
8684
 
@@ -8645,7 +8766,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8645
8766
  const file = target.files?.[0];
8646
8767
 
8647
8768
  if (!file || (isVideo ? !file.type.startsWith('video/') : !file.type.startsWith('image/'))) {
8648
- document.body.removeChild(fileInput);
8769
+ fileInput.remove();
8649
8770
  return;
8650
8771
  }
8651
8772
 
@@ -8655,7 +8776,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8655
8776
  }).catch((err) => {
8656
8777
  console.error('Video poster capture failed', err);
8657
8778
  }).finally(() => {
8658
- if (fileInput.parentNode) document.body.removeChild(fileInput);
8779
+ if (fileInput.parentNode) fileInput.remove();
8659
8780
  });
8660
8781
  return;
8661
8782
  }
@@ -8668,10 +8789,10 @@ function checkAndAddNewShapeToFrame(shape: any) {
8668
8789
  }
8669
8790
  };
8670
8791
  reader.readAsDataURL(file);
8671
- document.body.removeChild(fileInput);
8792
+ fileInput.remove();
8672
8793
  };
8673
8794
 
8674
- document.body.appendChild(fileInput);
8795
+ attachTransientInput(fileInput);
8675
8796
  fileInput.click();
8676
8797
  }
8677
8798
 
@@ -8783,7 +8904,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8783
8904
  img.onerror = (err) => {
8784
8905
  console.error('Failed to load image:', err);
8785
8906
  if (isUrl) {
8786
- alert('Failed to load image from URL. Make sure the URL is correct and allows cross-origin requests.');
8907
+ alert($t('canvas.error.imageFromUrl'));
8787
8908
  }
8788
8909
  };
8789
8910
 
@@ -9653,7 +9774,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
9653
9774
 
9654
9775
  export function addText() {
9655
9776
  let textBoxWidth = 100;
9656
- let text = new Textbox("Hello World!", {
9777
+ let text = new Textbox($t('canvas.defaults.text'), {
9657
9778
  width: textBoxWidth,
9658
9779
  top: Math.min(Math.random() * canvas!.height, canvas!.height - textBoxWidth),
9659
9780
  left: Math.min(Math.random() * canvas!.width, canvas!.width - textBoxWidth),
@@ -9748,7 +9869,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
9748
9869
  top: frameTop,
9749
9870
  width: frameWidth,
9750
9871
  height: frameHeight,
9751
- labelText: 'Frame',
9872
+ labelText: $t('canvas.defaults.frameLabel'),
9752
9873
  labelFontSize: 16,
9753
9874
  labelFontFamily: textFontFamily,
9754
9875
  labelColor: '#1f2937',
@@ -10065,9 +10186,11 @@ function checkAndAddNewShapeToFrame(shape: any) {
10065
10186
  // Handle named colors by creating a temporary element
10066
10187
  const tempEl = document.createElement('div');
10067
10188
  tempEl.style.color = color;
10068
- document.body.appendChild(tempEl);
10189
+ // (Measured where the canvas is: in the shadow root of a canvas that has one, away from the partner's page.)
10190
+ const tempHost = getTransientHostFor(canvasEl);
10191
+ tempHost.appendChild(tempEl);
10069
10192
  const computed = getComputedStyle(tempEl).color;
10070
- document.body.removeChild(tempEl);
10193
+ tempHost.removeChild(tempEl);
10071
10194
  const match = computed.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/);
10072
10195
  if (match) {
10073
10196
  return `rgba(${match[1]}, ${match[2]}, ${match[3]}, ${alpha})`;
@@ -12065,20 +12188,21 @@ function checkAndAddNewShapeToFrame(shape: any) {
12065
12188
  </div>
12066
12189
 
12067
12190
  <!-- Canvas -->
12068
- <canvas class="canvas-el" bind:this={canvasEl}></canvas>
12191
+ <!-- The board is a drawing: what is at its left stays at its left, and the text drawn on it is read left to right, on every screen. -->
12192
+ <canvas class="canvas-el" dir="ltr" bind:this={canvasEl}></canvas>
12069
12193
 
12070
12194
  {#if isCanvasLoading}
12071
12195
  <div class="canvas-loader" style="position:absolute;inset:0;z-index:30;display:flex;align-items:center;justify-content:center;background:rgba(255,255,255,0.85);backdrop-filter:blur(4px);">
12072
12196
  <div style="display:flex;flex-direction:column;align-items:center;gap:12px;">
12073
12197
  <div class="canvas-loader-spinner"></div>
12074
- <span style="font-size:14px;font-weight:500;color:#6b7280;letter-spacing:0.02em;">Loading canvas…</span>
12198
+ <span style="font-size:14px;font-weight:500;color:#6b7280;letter-spacing:0.02em;">{$t('canvas.state.loading')}</span>
12075
12199
  </div>
12076
12200
  </div>
12077
12201
  {/if}
12078
12202
 
12079
12203
  {#if canvasInitializationError}
12080
12204
  <div class="canvas-error absolute inset-x-0 bottom-4 z-20 mx-auto w-11/12 max-w-md rounded-md border border-red-300 bg-red-50 p-4 text-sm text-red-700 shadow-md">
12081
- <p class="font-semibold">Canvas failed to initialize.</p>
12205
+ <p class="font-semibold">{$t('canvas.state.initFailed')}</p>
12082
12206
  <p class="mt-1 wrap-break-word">{canvasInitializationError.message}</p>
12083
12207
  </div>
12084
12208
  {/if}
@@ -48,6 +48,12 @@ declare const __propDef: {
48
48
  /** Bumped when names change so PropsPanel re-resolves getWidgetName. */ widgetNameRevision?: number;
49
49
  /** Returns true when widgetId still exists in the host widget list. */ isWidgetKnown?: ((widgetId: string) => boolean) | null;
50
50
  showLogs?: boolean;
51
+ /**
52
+ * The language of the canvas: `locale` ("de", "pt-BR") picks the plural rules, the number format and the language
53
+ * the page's own words are in; `messages` are the page's own words, keyed like CANVAS_MESSAGES (a key left out
54
+ * keeps its English text). Without them the canvas speaks English. Both can change while the canvas is open.
55
+ */ locale?: string | null;
56
+ messages?: Readonly<Record<string, unknown>> | null;
51
57
  shapeFillColor?: () => string;
52
58
  deselectAll?: () => void;
53
59
  duplicateSelectedObjects?: () => Promise<void>;