@widgetic/canvas 0.5.17 → 0.5.19

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 (49) hide show
  1. package/README.md +110 -0
  2. package/dist/canvas/Canvas.svelte +190 -55
  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/canvas/widgetPreviewGeometry.js +5 -8
  21. package/dist/components/Tooltip.svelte +5 -2
  22. package/dist/components/index.d.ts +3 -0
  23. package/dist/components/index.js +4 -0
  24. package/dist/components/input-controls/ColorIC.svelte +15 -10
  25. package/dist/components/input-controls/ColorIC.svelte.d.ts +1 -1
  26. package/dist/components/input-controls/FontSelectorIC.svelte +12 -3
  27. package/dist/components/input-controls/SliderUnitIC.svelte +21 -7
  28. package/dist/components/input-controls/SliderUnitIC.svelte.d.ts +1 -1
  29. package/dist/components/input-controls/TextAlignIC.svelte +10 -5
  30. package/dist/components/input-controls/TextStyleIC.svelte +11 -7
  31. package/dist/components/preferenceStorage.d.ts +14 -0
  32. package/dist/components/preferenceStorage.js +25 -0
  33. package/dist/i18n/index.d.ts +2 -0
  34. package/dist/i18n/index.js +2 -0
  35. package/dist/i18n/labels.d.ts +23 -0
  36. package/dist/i18n/labels.js +75 -0
  37. package/dist/i18n/language.d.ts +23 -0
  38. package/dist/i18n/language.js +33 -0
  39. package/dist/i18n/messages.d.ts +277 -0
  40. package/dist/i18n/messages.js +292 -0
  41. package/dist/i18n/shapeWords.d.ts +30 -0
  42. package/dist/i18n/shapeWords.js +36 -0
  43. package/dist/i18n/shortcuts.d.ts +52 -0
  44. package/dist/i18n/shortcuts.js +104 -0
  45. package/dist/i18n/slots.d.ts +20 -0
  46. package/dist/i18n/slots.js +40 -0
  47. package/dist/index.d.ts +4 -0
  48. package/dist/index.js +5 -0
  49. 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
@@ -2427,6 +2497,15 @@
2427
2497
  });
2428
2498
 
2429
2499
  // Listen for scaling updates during drag
2500
+ const flushWidgetPreviewOverlay = () => {
2501
+ if (!onAfterRender) return;
2502
+ try {
2503
+ onAfterRender();
2504
+ } catch (err) {
2505
+ canvasWarn('Canvas: onAfterRender failed', err);
2506
+ }
2507
+ };
2508
+
2430
2509
  canvas.on('object:scaling', (opt: any) => {
2431
2510
  isCurrentlyScaling = true;
2432
2511
  scalingTarget = opt.target;
@@ -3118,6 +3197,7 @@
3118
3197
  // object:modified fires once at mouseup — ideal for undo snapshots.
3119
3198
  // Also fires after arrow-key nudges.
3120
3199
  canvas.on('object:modified', () => {
3200
+ flushWidgetPreviewOverlay();
3121
3201
  takeHistorySnapshot();
3122
3202
  });
3123
3203
 
@@ -3128,6 +3208,7 @@
3128
3208
  // event, preventing our window-level handlers from seeing it.
3129
3209
  canvas.on('text:editing:exited', () => {
3130
3210
  takeHistorySnapshot();
3211
+ takeFocusBackAfterTextEditing();
3131
3212
 
3132
3213
  if (activeEditingTextbox) {
3133
3214
  detachActiveTextListeners();
@@ -3436,11 +3517,13 @@
3436
3517
 
3437
3518
  // Global keydown handler for Delete/Backspace to delete selected objects
3438
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;
3439
3522
  // Skip if we're in text editing mode
3440
3523
  if (activeEditingTextbox) return;
3441
3524
 
3442
3525
  // Skip if focus is on text input
3443
- const activeElement = document.activeElement as HTMLElement | null;
3526
+ const activeElement = getFocusedElement() as HTMLElement | null;
3444
3527
  if (activeElement) {
3445
3528
  const tagName = activeElement.tagName;
3446
3529
  const isTextInput = tagName === 'INPUT' || tagName === 'TEXTAREA';
@@ -3461,11 +3544,13 @@
3461
3544
 
3462
3545
  // Global keydown handler for Arrow keys to move selected objects
3463
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;
3464
3549
  // Skip if we're in text editing mode
3465
3550
  if (activeEditingTextbox) return;
3466
3551
 
3467
3552
  // Skip if focus is on text input
3468
- const activeElement = document.activeElement as HTMLElement | null;
3553
+ const activeElement = getFocusedElement() as HTMLElement | null;
3469
3554
  if (activeElement) {
3470
3555
  const tagName = activeElement.tagName;
3471
3556
  const isTextInput = tagName === 'INPUT' || tagName === 'TEXTAREA';
@@ -3548,8 +3633,10 @@
3548
3633
 
3549
3634
  const handleSelectAllKeydown = (e: KeyboardEvent) => {
3550
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;
3551
3638
 
3552
- const activeElement = document.activeElement as HTMLElement | null;
3639
+ const activeElement = getFocusedElement() as HTMLElement | null;
3553
3640
 
3554
3641
  // Allow Cmd+A inside form controls and contenteditable elements
3555
3642
  if (activeElement && (
@@ -3662,6 +3749,8 @@
3662
3749
  const handleEscapeKeydown = (e: KeyboardEvent) => {
3663
3750
  if (e.key === 'Escape') {
3664
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;
3665
3754
 
3666
3755
  // ALWAYS close PropsPanel first
3667
3756
  selectedObject = null;
@@ -3831,11 +3920,18 @@
3831
3920
 
3832
3921
  ctx.fillStyle = '#94a3b8';
3833
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
+
3834
3930
  // contentScale shrinks icon+text when placeholder is very small
3835
3931
  const availableW = self.width || 100;
3836
3932
  const availableH = self.height || 100;
3837
3933
  const contentHeight = 76; // icon(24) + gap(8) + 3×line(12) + 2×gap(4)
3838
- const contentScale = Math.min(1, availableH / contentHeight, availableW / 100);
3934
+ const contentScale = Math.min(1, availableH / contentHeight, availableW / contentWidth);
3839
3935
 
3840
3936
  if (contentScale < 0.3) {
3841
3937
  // Too small — show minimal icon only
@@ -3855,9 +3951,7 @@
3855
3951
 
3856
3952
  const fontSize = 12;
3857
3953
  ctx.font = `${fontSize}px Arial`;
3858
- ctx.fillText('Drop image', 0, 0);
3859
- ctx.fillText('or', 0, fontSize + 4);
3860
- ctx.fillText('double click', 0, (fontSize + 4) * 2);
3954
+ placeholderLines.forEach((line, lineIndex) => ctx.fillText(line, 0, (fontSize + 4) * lineIndex));
3861
3955
 
3862
3956
  ctx.restore();
3863
3957
  }
@@ -4354,6 +4448,8 @@
4354
4448
  const handleCopyKeydown = (e: KeyboardEvent) => {
4355
4449
  if ((e.metaKey || e.ctrlKey) && e.key === 'c') {
4356
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;
4357
4453
 
4358
4454
  // Check if we're editing text - if so, don't intercept
4359
4455
  if (activeEditingTextbox && activeEditingTextbox.isEditing) {
@@ -4361,7 +4457,7 @@
4361
4457
  }
4362
4458
 
4363
4459
  // Check if focus is on an input, textarea, or contenteditable — don't intercept
4364
- const activeElement = document.activeElement;
4460
+ const activeElement = getFocusedElement();
4365
4461
  if (activeElement && (
4366
4462
  activeElement.tagName === 'INPUT' ||
4367
4463
  activeElement.tagName === 'TEXTAREA' ||
@@ -4425,11 +4521,13 @@
4425
4521
  const handleCutKeydown = (e: KeyboardEvent) => {
4426
4522
  if ((e.metaKey || e.ctrlKey) && e.key === 'x') {
4427
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;
4428
4526
 
4429
4527
  // Check if we're editing text - let the browser handle text cut
4430
4528
  if (activeEditingTextbox && activeEditingTextbox.isEditing) return;
4431
4529
 
4432
- const activeElement = document.activeElement;
4530
+ const activeElement = getFocusedElement();
4433
4531
  if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) return;
4434
4532
 
4435
4533
  const activeObject = canvas.getActiveObject();
@@ -4457,7 +4555,7 @@
4457
4555
  return;
4458
4556
  }
4459
4557
 
4460
- const activeElement = document.activeElement;
4558
+ const activeElement = getFocusedElement();
4461
4559
  if (activeElement && (
4462
4560
  activeElement.tagName === 'INPUT' ||
4463
4561
  activeElement.tagName === 'TEXTAREA' ||
@@ -4466,7 +4564,8 @@
4466
4564
  return;
4467
4565
  }
4468
4566
 
4469
- 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);
4470
4569
  if (target?.closest?.('.widget-details-floating-panel, .widgets-list-panel, [data-widget-details]')) {
4471
4570
  return;
4472
4571
  }
@@ -4480,6 +4579,8 @@
4480
4579
  const handleDuplicateKeydown = (e: KeyboardEvent) => {
4481
4580
  if ((e.metaKey || e.ctrlKey) && e.key === 'd') {
4482
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;
4483
4584
 
4484
4585
  // Check if we're editing text - if so, don't intercept
4485
4586
  if (activeEditingTextbox && activeEditingTextbox.isEditing) {
@@ -4487,7 +4588,7 @@
4487
4588
  }
4488
4589
 
4489
4590
  // Check if focus is on an input or textarea - don't intercept
4490
- const activeElement = document.activeElement;
4591
+ const activeElement = getFocusedElement();
4491
4592
  if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
4492
4593
  return;
4493
4594
  }
@@ -4520,15 +4621,18 @@
4520
4621
  // Priority: OS clipboard image wins if present, otherwise fall back to internal canvas clipboard.
4521
4622
  const handleOsClipboardPaste = (e: ClipboardEvent) => {
4522
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;
4523
4626
  if (activeEditingTextbox && activeEditingTextbox.isEditing) return;
4524
- const activeElement = document.activeElement;
4627
+ const activeElement = getFocusedElement();
4525
4628
  if (activeElement && (
4526
4629
  activeElement.tagName === 'INPUT' ||
4527
4630
  activeElement.tagName === 'TEXTAREA' ||
4528
4631
  (activeElement as HTMLElement).isContentEditable
4529
4632
  )) return;
4530
4633
  // Skip if paste target is inside a Widget Details panel, Widgets List, or similar non-canvas container
4531
- 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);
4532
4636
  if (target?.closest?.('.widget-details-floating-panel, .widgets-list-panel, [data-widget-details]')) return;
4533
4637
 
4534
4638
  const items = e.clipboardData?.items;
@@ -4591,10 +4695,12 @@
4591
4695
  const handleGroupKeydown = (e: KeyboardEvent) => {
4592
4696
  if (!(e.metaKey || e.ctrlKey) || e.key.toLowerCase() !== 'g') return;
4593
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;
4594
4700
 
4595
4701
  // Don't intercept during text editing
4596
4702
  if (activeEditingTextbox && activeEditingTextbox.isEditing) return;
4597
- const activeElement = document.activeElement;
4703
+ const activeElement = getFocusedElement();
4598
4704
  if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) return;
4599
4705
 
4600
4706
  e.preventDefault();
@@ -4614,8 +4720,10 @@
4614
4720
  // ── History: Ctrl+Z (undo) / Ctrl+Y or Ctrl+Shift+Z (redo) ────────
4615
4721
  const handleUndoRedoKeydown = (e: KeyboardEvent) => {
4616
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;
4617
4725
  // Skip if focus is in a text input field
4618
- const activeElement = document.activeElement;
4726
+ const activeElement = getFocusedElement();
4619
4727
  if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) return;
4620
4728
  // Skip if editing a textbox on the canvas
4621
4729
  if (activeEditingTextbox && activeEditingTextbox.isEditing) return;
@@ -4638,8 +4746,10 @@
4638
4746
  const isBracketLeft = e.code === 'BracketLeft';
4639
4747
  if (!isBracketRight && !isBracketLeft) return;
4640
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;
4641
4751
  if (activeEditingTextbox && activeEditingTextbox.isEditing) return;
4642
- const activeElement = document.activeElement;
4752
+ const activeElement = getFocusedElement();
4643
4753
  if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) return;
4644
4754
 
4645
4755
  e.preventDefault();
@@ -4658,6 +4768,8 @@
4658
4768
  // ── Save: Cmd+S / Ctrl+S ─────────────────────────────────────────────
4659
4769
  const handleSaveKeydown = (e: KeyboardEvent) => {
4660
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;
4661
4773
  e.preventDefault();
4662
4774
  saveNow();
4663
4775
  };
@@ -4904,14 +5016,15 @@
4904
5016
  let afterRenderRaf = 0;
4905
5017
  canvas.on('after:render', () => {
4906
5018
  if (!onAfterRender) return;
5019
+ // While the pointer is resizing, the iframe has to match this paint.
5020
+ if (isCurrentlyScaling) {
5021
+ flushWidgetPreviewOverlay();
5022
+ return;
5023
+ }
4907
5024
  if (afterRenderRaf) return;
4908
5025
  afterRenderRaf = requestAnimationFrame(() => {
4909
5026
  afterRenderRaf = 0;
4910
- try {
4911
- onAfterRender?.();
4912
- } catch (err) {
4913
- canvasWarn('Canvas: onAfterRender failed', err);
4914
- }
5027
+ flushWidgetPreviewOverlay();
4915
5028
  });
4916
5029
  });
4917
5030
 
@@ -5973,6 +6086,10 @@
5973
6086
 
5974
6087
  canvas = null;
5975
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;
5976
6093
  });
5977
6094
 
5978
6095
  function resizeObserver(node: HTMLElement) {
@@ -7855,7 +7972,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7855
7972
  top: finalBounds.top,
7856
7973
  width: finalBounds.width,
7857
7974
  height: finalBounds.height,
7858
- labelText: 'Frame',
7975
+ labelText: $t('canvas.defaults.frameLabel'),
7859
7976
  labelFontSize: 16,
7860
7977
  labelFontFamily: textFontFamily,
7861
7978
  labelColor: '#1f2937',
@@ -8305,6 +8422,13 @@ function checkAndAddNewShapeToFrame(shape: any) {
8305
8422
 
8306
8423
  ctx.fillStyle = '#94a3b8';
8307
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
+
8308
8432
  // Available space in local pixels.
8309
8433
  // contentScale shrinks the icon group when the placeholder is very small
8310
8434
  // so text never overflows the box.
@@ -8313,7 +8437,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8313
8437
 
8314
8438
  // Approx layout height: icon(24) + gap(8) + 3×line(12) + 2×gap(4) = 76px
8315
8439
  const contentHeight = 76;
8316
- const contentScale = Math.min(1, availableH / contentHeight, availableW / 100);
8440
+ const contentScale = Math.min(1, availableH / contentHeight, availableW / contentWidth);
8317
8441
 
8318
8442
  if (contentScale < 0.3) {
8319
8443
  // Too small for text — show a minimal icon only
@@ -8335,9 +8459,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8335
8459
  // Text lines
8336
8460
  const fontSize = 12;
8337
8461
  ctx.font = `${fontSize}px Arial`;
8338
- ctx.fillText('Drop image', 0, 0);
8339
- ctx.fillText('or', 0, fontSize + 4);
8340
- ctx.fillText('double click', 0, (fontSize + 4) * 2);
8462
+ placeholderLines.forEach((line, lineIndex) => ctx.fillText(line, 0, (fontSize + 4) * lineIndex));
8341
8463
 
8342
8464
  ctx.restore();
8343
8465
  }
@@ -8494,6 +8616,16 @@ function checkAndAddNewShapeToFrame(shape: any) {
8494
8616
  });
8495
8617
  }
8496
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
+
8497
8629
  /**
8498
8630
  * Open file picker for an ImagePlaceholder
8499
8631
  * Called when user clicks on placeholder
@@ -8512,15 +8644,15 @@ function checkAndAddNewShapeToFrame(shape: any) {
8512
8644
  const file = target.files?.[0];
8513
8645
 
8514
8646
  if (!file) {
8515
- document.body.removeChild(fileInput);
8647
+ fileInput.remove();
8516
8648
  return;
8517
8649
  }
8518
8650
  if (isVideo && !file.type.startsWith('video/')) {
8519
- document.body.removeChild(fileInput);
8651
+ fileInput.remove();
8520
8652
  return;
8521
8653
  }
8522
8654
  if (!isVideo && !file.type.startsWith('image/')) {
8523
- document.body.removeChild(fileInput);
8655
+ fileInput.remove();
8524
8656
  return;
8525
8657
  }
8526
8658
 
@@ -8530,7 +8662,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8530
8662
  }).catch((err) => {
8531
8663
  console.error('Video poster capture failed', err);
8532
8664
  }).finally(() => {
8533
- if (fileInput.parentNode) document.body.removeChild(fileInput);
8665
+ if (fileInput.parentNode) fileInput.remove();
8534
8666
  });
8535
8667
  return;
8536
8668
  }
@@ -8543,10 +8675,10 @@ function checkAndAddNewShapeToFrame(shape: any) {
8543
8675
  }
8544
8676
  };
8545
8677
  reader.readAsDataURL(file);
8546
- document.body.removeChild(fileInput);
8678
+ fileInput.remove();
8547
8679
  };
8548
8680
 
8549
- document.body.appendChild(fileInput);
8681
+ attachTransientInput(fileInput);
8550
8682
  fileInput.click();
8551
8683
  }
8552
8684
 
@@ -8634,7 +8766,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8634
8766
  const file = target.files?.[0];
8635
8767
 
8636
8768
  if (!file || (isVideo ? !file.type.startsWith('video/') : !file.type.startsWith('image/'))) {
8637
- document.body.removeChild(fileInput);
8769
+ fileInput.remove();
8638
8770
  return;
8639
8771
  }
8640
8772
 
@@ -8644,7 +8776,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8644
8776
  }).catch((err) => {
8645
8777
  console.error('Video poster capture failed', err);
8646
8778
  }).finally(() => {
8647
- if (fileInput.parentNode) document.body.removeChild(fileInput);
8779
+ if (fileInput.parentNode) fileInput.remove();
8648
8780
  });
8649
8781
  return;
8650
8782
  }
@@ -8657,10 +8789,10 @@ function checkAndAddNewShapeToFrame(shape: any) {
8657
8789
  }
8658
8790
  };
8659
8791
  reader.readAsDataURL(file);
8660
- document.body.removeChild(fileInput);
8792
+ fileInput.remove();
8661
8793
  };
8662
8794
 
8663
- document.body.appendChild(fileInput);
8795
+ attachTransientInput(fileInput);
8664
8796
  fileInput.click();
8665
8797
  }
8666
8798
 
@@ -8772,7 +8904,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8772
8904
  img.onerror = (err) => {
8773
8905
  console.error('Failed to load image:', err);
8774
8906
  if (isUrl) {
8775
- alert('Failed to load image from URL. Make sure the URL is correct and allows cross-origin requests.');
8907
+ alert($t('canvas.error.imageFromUrl'));
8776
8908
  }
8777
8909
  };
8778
8910
 
@@ -9642,7 +9774,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
9642
9774
 
9643
9775
  export function addText() {
9644
9776
  let textBoxWidth = 100;
9645
- let text = new Textbox("Hello World!", {
9777
+ let text = new Textbox($t('canvas.defaults.text'), {
9646
9778
  width: textBoxWidth,
9647
9779
  top: Math.min(Math.random() * canvas!.height, canvas!.height - textBoxWidth),
9648
9780
  left: Math.min(Math.random() * canvas!.width, canvas!.width - textBoxWidth),
@@ -9737,7 +9869,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
9737
9869
  top: frameTop,
9738
9870
  width: frameWidth,
9739
9871
  height: frameHeight,
9740
- labelText: 'Frame',
9872
+ labelText: $t('canvas.defaults.frameLabel'),
9741
9873
  labelFontSize: 16,
9742
9874
  labelFontFamily: textFontFamily,
9743
9875
  labelColor: '#1f2937',
@@ -10054,9 +10186,11 @@ function checkAndAddNewShapeToFrame(shape: any) {
10054
10186
  // Handle named colors by creating a temporary element
10055
10187
  const tempEl = document.createElement('div');
10056
10188
  tempEl.style.color = color;
10057
- 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);
10058
10192
  const computed = getComputedStyle(tempEl).color;
10059
- document.body.removeChild(tempEl);
10193
+ tempHost.removeChild(tempEl);
10060
10194
  const match = computed.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/);
10061
10195
  if (match) {
10062
10196
  return `rgba(${match[1]}, ${match[2]}, ${match[3]}, ${alpha})`;
@@ -12054,20 +12188,21 @@ function checkAndAddNewShapeToFrame(shape: any) {
12054
12188
  </div>
12055
12189
 
12056
12190
  <!-- Canvas -->
12057
- <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>
12058
12193
 
12059
12194
  {#if isCanvasLoading}
12060
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);">
12061
12196
  <div style="display:flex;flex-direction:column;align-items:center;gap:12px;">
12062
12197
  <div class="canvas-loader-spinner"></div>
12063
- <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>
12064
12199
  </div>
12065
12200
  </div>
12066
12201
  {/if}
12067
12202
 
12068
12203
  {#if canvasInitializationError}
12069
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">
12070
- <p class="font-semibold">Canvas failed to initialize.</p>
12205
+ <p class="font-semibold">{$t('canvas.state.initFailed')}</p>
12071
12206
  <p class="mt-1 wrap-break-word">{canvasInitializationError.message}</p>
12072
12207
  </div>
12073
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>;