@pixerate/editor 0.150.0 → 0.151.0

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 (87) hide show
  1. package/dist/agent/index.cjs +34 -34
  2. package/dist/agent/index.js +8 -8
  3. package/dist/animation-editor/editorDocument.d.cts +9 -0
  4. package/dist/animation-editor/editorDocument.d.ts +9 -0
  5. package/dist/animation-editor/index.cjs +33 -1
  6. package/dist/animation-editor/index.d.cts +1 -0
  7. package/dist/animation-editor/index.d.ts +1 -0
  8. package/dist/animation-editor/index.js +32 -0
  9. package/dist/bridge/cli.cjs +4 -4
  10. package/dist/bridge/cli.js +3 -3
  11. package/dist/bridge/index.cjs +4 -4
  12. package/dist/bridge/index.js +3 -3
  13. package/dist/chart/controller.d.cts +5 -0
  14. package/dist/chart/controller.d.ts +5 -0
  15. package/dist/chart/editorDocument.d.cts +10 -0
  16. package/dist/chart/editorDocument.d.ts +10 -0
  17. package/dist/chart/index.cjs +54 -18
  18. package/dist/chart/index.d.cts +1 -0
  19. package/dist/chart/index.d.ts +1 -0
  20. package/dist/chart/index.js +37 -1
  21. package/dist/{chunk-ENIZYNEF.cjs → chunk-24M7VD6M.cjs} +7 -7
  22. package/dist/{chunk-OET45HIR.js → chunk-4RF2H3N3.js} +10 -0
  23. package/dist/{chunk-KC7HB5GF.cjs → chunk-5H2C3QSF.cjs} +4 -3
  24. package/dist/{chunk-HRLT5BYO.js → chunk-GNV54DLO.js} +2 -1
  25. package/dist/{chunk-EAQ4BGNS.js → chunk-MKTDELCD.js} +1 -1
  26. package/dist/{chunk-X35BV5YT.cjs → chunk-ML2II3Y3.cjs} +10 -0
  27. package/dist/{chunk-QCEQIRJQ.cjs → chunk-PI45JMP7.cjs} +163 -136
  28. package/dist/{chunk-DVLMW3GB.cjs → chunk-R3PECGTQ.cjs} +44 -44
  29. package/dist/{chunk-2AQL4JHM.js → chunk-R6HNWN7B.js} +1 -1
  30. package/dist/{chunk-6QTON3VW.cjs → chunk-RZJCYJWO.cjs} +8 -8
  31. package/dist/{chunk-O5ML2I6O.js → chunk-SVINJIGK.js} +1 -1
  32. package/dist/{chunk-ZQUANEK4.js → chunk-Y64YPIFT.js} +28 -1
  33. package/dist/diagram/index.cjs +6 -6
  34. package/dist/diagram/index.js +5 -5
  35. package/dist/export/index.cjs +1 -1
  36. package/dist/export/index.js +1 -1
  37. package/dist/extensions/index.cjs +5 -5
  38. package/dist/extensions/index.js +4 -4
  39. package/dist/image-editor/canvas-viewport.d.cts +52 -0
  40. package/dist/image-editor/canvas-viewport.d.ts +52 -0
  41. package/dist/image-editor/index.cjs +666 -599
  42. package/dist/image-editor/index.d.cts +1 -0
  43. package/dist/image-editor/index.d.ts +1 -0
  44. package/dist/image-editor/index.js +100 -33
  45. package/dist/image-editor/layers/ops.d.cts +21 -3
  46. package/dist/image-editor/layers/ops.d.ts +21 -3
  47. package/dist/image-editor/renderer.d.cts +7 -0
  48. package/dist/image-editor/renderer.d.ts +7 -0
  49. package/dist/index.cjs +14 -14
  50. package/dist/index.js +8 -8
  51. package/dist/paint-editor/index.cjs +6 -6
  52. package/dist/paint-editor/index.js +1 -1
  53. package/dist/parameters/controller.d.cts +6 -0
  54. package/dist/parameters/controller.d.ts +6 -0
  55. package/dist/parameters/editorDocument.d.cts +9 -0
  56. package/dist/parameters/editorDocument.d.ts +9 -0
  57. package/dist/parameters/index.cjs +71 -1
  58. package/dist/parameters/index.d.cts +1 -0
  59. package/dist/parameters/index.d.ts +1 -0
  60. package/dist/parameters/index.js +70 -0
  61. package/dist/persistence/editorDocument.d.cts +6 -3
  62. package/dist/persistence/editorDocument.d.ts +6 -3
  63. package/dist/persistence/index.cjs +7 -7
  64. package/dist/persistence/index.js +1 -1
  65. package/dist/tools/index.cjs +5 -5
  66. package/dist/tools/index.js +6 -6
  67. package/dist/vector/editorDocument.d.cts +9 -0
  68. package/dist/vector/editorDocument.d.ts +9 -0
  69. package/dist/vector/index.cjs +45 -1
  70. package/dist/vector/index.d.cts +1 -0
  71. package/dist/vector/index.d.ts +1 -0
  72. package/dist/vector/index.js +44 -0
  73. package/dist/video-editor/editorDocument.d.cts +9 -0
  74. package/dist/video-editor/editorDocument.d.ts +9 -0
  75. package/dist/video-editor/index.cjs +33 -1
  76. package/dist/video-editor/index.d.cts +1 -0
  77. package/dist/video-editor/index.d.ts +1 -0
  78. package/dist/video-editor/index.js +32 -0
  79. package/package.json +2 -2
  80. package/skills/pixerate-animation-editor/SKILL.md +12 -0
  81. package/skills/pixerate-chart/SKILL.md +12 -0
  82. package/skills/pixerate-image-editor/SKILL.md +6 -6
  83. package/skills/pixerate-parameters/SKILL.md +12 -0
  84. package/skills/pixerate-vector/SKILL.md +14 -0
  85. package/skills/pixerate-video-editor/SKILL.md +12 -0
  86. package/dist/{chunk-Y4BWZB6J.cjs → chunk-462WGGXX.cjs} +2 -2
  87. package/dist/{chunk-RFBFTLVN.js → chunk-EVXQNQND.js} +3 -3
@@ -4,6 +4,7 @@ export * from './color.cjs';
4
4
  export * from './renderer.cjs';
5
5
  export * from './controller.cjs';
6
6
  export * from './geometry.cjs';
7
+ export * from './canvas-viewport.cjs';
7
8
  export * from './document.cjs';
8
9
  export * from './blend.cjs';
9
10
  export * from './adjustment-layers.cjs';
@@ -4,6 +4,7 @@ export * from './color.js';
4
4
  export * from './renderer.js';
5
5
  export * from './controller.js';
6
6
  export * from './geometry.js';
7
+ export * from './canvas-viewport.js';
7
8
  export * from './document.js';
8
9
  export * from './blend.js';
9
10
  export * from './adjustment-layers.js';
@@ -52,6 +52,7 @@ import {
52
52
  XYZ_D50_TO_SRGB,
53
53
  activeAdjustmentLayers,
54
54
  activeEffectSmartFilters,
55
+ activeLayerAfterRemoval,
55
56
  activeSmartFilters,
56
57
  addLayerEffect,
57
58
  addLayerFilter,
@@ -117,6 +118,7 @@ import {
117
118
  decodePlanes,
118
119
  decodePngDataUrl,
119
120
  decodeSelectionRle,
121
+ defaultActiveLayerId,
120
122
  defaultAdjustmentParams,
121
123
  defaultShapeName,
122
124
  depthMaskAlpha,
@@ -178,6 +180,7 @@ import {
178
180
  isDefaultDepthMask,
179
181
  isIdentityTransform,
180
182
  isLegacyShape,
183
+ isPaintableLayer,
181
184
  isQuarterTurnSwapped,
182
185
  isRenderedLayer,
183
186
  isSelectionEmpty,
@@ -348,6 +351,7 @@ import {
348
351
  updateShapeLayer,
349
352
  updateSmartFilter,
350
353
  updateTypeLayer,
354
+ userErrorMessage,
351
355
  uvToXy,
352
356
  versionedDescriptorBytes,
353
357
  vibranceLinear,
@@ -363,13 +367,10 @@ import {
363
367
  writePsd,
364
368
  xyToTempTint,
365
369
  xyToUv
366
- } from "../chunk-ZQUANEK4.js";
370
+ } from "../chunk-Y64YPIFT.js";
367
371
  import {
368
372
  parseCssColor
369
373
  } from "../chunk-3CI7FRMK.js";
370
- import {
371
- loadDocument
372
- } from "../chunk-WHKNYFM2.js";
373
374
  import {
374
375
  EffectRendererPool
375
376
  } from "../chunk-EHLOCVWB.js";
@@ -583,7 +584,6 @@ import {
583
584
  writeJpegIcc,
584
585
  writePngIcc
585
586
  } from "../chunk-HVQVHUTV.js";
586
- import "../chunk-HF5QZS7Z.js";
587
587
  import {
588
588
  DEFAULT_AUTO_LEADING,
589
589
  DEFAULT_TEXT_STYLE,
@@ -625,6 +625,9 @@ import {
625
625
  } from "../chunk-FLOGD3QB.js";
626
626
  import "../chunk-ORDXMBD4.js";
627
627
  import "../chunk-M6DNZNVY.js";
628
+ import {
629
+ loadDocument
630
+ } from "../chunk-WHKNYFM2.js";
628
631
  import "../chunk-WEIK3UYB.js";
629
632
  import "../chunk-PWBZH3FK.js";
630
633
  import {
@@ -832,6 +835,7 @@ import {
832
835
  } from "../chunk-QGHEDC7B.js";
833
836
  import "../chunk-SIFAIGB3.js";
834
837
  import "../chunk-VDGBHYC7.js";
838
+ import "../chunk-HF5QZS7Z.js";
835
839
  import "../chunk-KY3FYKP3.js";
836
840
  import "../chunk-2WPNAGHI.js";
837
841
  import "../chunk-AA34BA7G.js";
@@ -1245,8 +1249,10 @@ var ImageEditorRenderer = class {
1245
1249
  const { crop, transform, adjustments, depthMask, annotations, selectedAnnotationId, zoom, pan, isOriginalCompared } = state;
1246
1250
  const vpWidth = options.viewportWidth || targetCanvas.width;
1247
1251
  const vpHeight = options.viewportHeight || targetCanvas.height;
1248
- targetCanvas.width = vpWidth;
1249
- targetCanvas.height = vpHeight;
1252
+ const ratio = options.pixelRatio && Number.isFinite(options.pixelRatio) && options.pixelRatio > 0 ? options.pixelRatio : 1;
1253
+ targetCanvas.width = Math.max(1, Math.round(vpWidth * ratio));
1254
+ targetCanvas.height = Math.max(1, Math.round(vpHeight * ratio));
1255
+ if (ratio !== 1 && typeof ctx.setTransform === "function") ctx.setTransform(targetCanvas.width / vpWidth, 0, 0, targetCanvas.height / vpHeight, 0, 0);
1250
1256
  ctx.clearRect(0, 0, vpWidth, vpHeight);
1251
1257
  if (isOriginalCompared) {
1252
1258
  this.drawFittedImage(ctx, img, naturalWidth, naturalHeight, vpWidth, vpHeight, zoom, pan, fit);
@@ -4901,8 +4907,8 @@ var ImageEditorController = class {
4901
4907
  this.state.layers = next;
4902
4908
  extra?.();
4903
4909
  if (depth !== 8 && this.bitDepth === depth) this.state.layers = keepDeepLayers(prev, this.state.layers);
4904
- if (this.state.activeLayerId && !findLayer(next, this.state.activeLayerId)) {
4905
- this.state.activeLayerId = null;
4910
+ if (this.state.activeLayerId && !findLayer(this.state.layers, this.state.activeLayerId)) {
4911
+ this.state.activeLayerId = activeLayerAfterRemoval(prev, this.state.layers, this.state.activeLayerId);
4906
4912
  this.state.activeTarget = "pixels";
4907
4913
  }
4908
4914
  this.notify();
@@ -6126,27 +6132,28 @@ var ImageEditorController = class {
6126
6132
  /** Resolves and validates what a stroke, fill or clear paints into. */
6127
6133
  paintTarget(layerId, channel, action) {
6128
6134
  const id = layerId ?? this.state.activeLayerId;
6129
- if (!id) throw new LayerOperationError(`No layer given and none is active; pass layerId (see list_layers) to ${action}.`);
6135
+ if (!id) throw new LayerOperationError(`No layer given and none is active; pass layerId (see list_layers) to ${action}.`, `Pick a layer to ${action} on in the Layers panel.`);
6130
6136
  const layer = requireLayer(this.state.layers, id).layer;
6137
+ const name = layer.name || id;
6131
6138
  const ch = channel ?? (id === this.state.activeLayerId ? this.state.activeTarget : "pixels");
6132
6139
  if (ch === "mask") {
6133
- if (!layer.mask) throw new LayerOperationError(`Layer "${layer.name || id}" has no mask to ${action}; add one with add_layer_mask, or target pixels.`);
6134
- if (layer.mask.source) throw new LayerOperationError(`Layer "${layer.name || id}" has a depth mask, which is generated, not painted.`);
6140
+ if (!layer.mask) throw new LayerOperationError(`Layer "${name}" has no mask to ${action}; add one with add_layer_mask, or target pixels.`, `Layer "${name}" has no mask; add a mask or ${action} on its pixels.`);
6141
+ if (layer.mask.source) throw new LayerOperationError(`Layer "${name}" has a depth mask, which is generated, not painted.`);
6135
6142
  requireUnlocked(layer, "all", action);
6136
6143
  } else if (ch === "filter-mask") {
6137
6144
  if (layer.type !== "smart" || !layer.smartFilters?.mask) {
6138
- throw new LayerOperationError(`Layer "${layer.name || id}" has no smart filter mask to ${action}; add one with set_smart_filter_mask, or target pixels or mask.`);
6145
+ throw new LayerOperationError(`Layer "${name}" has no smart filter mask to ${action}; add one with set_smart_filter_mask, or target pixels or mask.`, `Layer "${name}" has no smart filter mask to ${action}.`);
6139
6146
  }
6140
- if (!isContentsDecoded(layer.contents)) throw new LayerOperationError(`The contents of "${layer.name || id}" are not decoded here, so its filter mask cannot be painted; run prepareSmartObjects() first.`);
6147
+ if (!isContentsDecoded(layer.contents)) throw new LayerOperationError(`The contents of "${name}" are not decoded here, so its filter mask cannot be painted; run prepareSmartObjects() first.`, `The smart object "${name}" is still loading; try again in a moment.`);
6141
6148
  requireUnlocked(layer, "all", action);
6142
6149
  } else {
6143
- if (layer.type === "smart") throw new LayerOperationError(`Layer "${layer.name || id}" is a smart object; its pixels come from its contents. Rasterize it (rasterize_smart_object), edit its contents (edit_smart_object_contents), or ${action} on its mask or filter mask.`);
6144
- if (layer.type === "shape") throw new LayerOperationError(`Layer "${layer.name || id}" is a shape layer; its pixels are drawn from its paths. Rasterize it first (rasterize_layer), or ${action} on its mask (channel mask).`);
6145
- if (layer.type === "type") throw new LayerOperationError(`Layer "${layer.name || id}" is a type layer; its pixels are drawn from its text. Rasterize it first (rasterize_layer), or ${action} on its mask (channel mask).`);
6146
- if (layer.type !== "pixel") throw new LayerOperationError(`Layer "${layer.name || id}" is a ${layer.type} layer; ${action} on a pixel layer, or on this layer's mask (channel mask).`);
6150
+ if (layer.type === "smart") throw new LayerOperationError(`Layer "${name}" is a smart object; its pixels come from its contents. Rasterize it (rasterize_smart_object), edit its contents (edit_smart_object_contents), or ${action} on its mask or filter mask.`, `"${name}" is a smart object; rasterize it or edit its contents to ${action} on it.`);
6151
+ if (layer.type === "shape") throw new LayerOperationError(`Layer "${name}" is a shape layer; its pixels are drawn from its paths. Rasterize it first (rasterize_layer), or ${action} on its mask (channel mask).`, `"${name}" is a shape layer; rasterize it to ${action} on it.`);
6152
+ if (layer.type === "type") throw new LayerOperationError(`Layer "${name}" is a type layer; its pixels are drawn from its text. Rasterize it first (rasterize_layer), or ${action} on its mask (channel mask).`, `"${name}" is a type layer; rasterize it to ${action} on it.`);
6153
+ if (layer.type !== "pixel") throw new LayerOperationError(`Layer "${name}" is a ${layer.type} layer; ${action} on a pixel layer, or on this layer's mask (channel mask).`, `"${name}" is not a pixel layer; pick a pixel layer to ${action} on.`);
6147
6154
  requireUnlocked(layer, "pixels", action);
6148
6155
  }
6149
- if (!layer.visible) throw new LayerOperationError(`Layer "${layer.name || id}" is hidden; show it before you ${action}.`);
6156
+ if (!layer.visible) throw new LayerOperationError(`Layer "${name}" is hidden; show it before you ${action}.`);
6150
6157
  const offset = layer.type === "pixel" ? layer.offset : { x: 0, y: 0 };
6151
6158
  const surface = ch === "mask" ? layer.mask.surface : ch === "filter-mask" ? layer.smartFilters.mask.surface : this.surfaceOf(layer);
6152
6159
  const lockTransparency = ch === "pixels" && Boolean(layer.locks?.transparency);
@@ -6171,7 +6178,7 @@ var ImageEditorController = class {
6171
6178
  if (!STROKE_TOOLS.includes(tool)) throw new LayerOperationError(`Unknown paint tool "${String(tool)}"; use one of ${STROKE_TOOLS.join(", ")}.`);
6172
6179
  const target = this.paintTarget(init.layerId, init.channel, tool === "eraser" ? "erase" : isRetouchTool(tool) ? "retouch" : "paint");
6173
6180
  if (tool === "mixer" && target.channel !== "pixels") {
6174
- throw new LayerOperationError("The Mixer Brush paints colour on pixels, not on masks; target pixels (channel pixels), or paint the mask with the brush.");
6181
+ throw new LayerOperationError("The Mixer Brush paints colour on pixels, not on masks; target pixels (channel pixels), or paint the mask with the brush.", "The Mixer Brush paints on pixels, not masks; target the layer's pixels, or paint the mask with the Brush.");
6175
6182
  }
6176
6183
  const brush = normalizeBrush({ ...this.state.paint.brush, ...init.brush ?? {} });
6177
6184
  let strokePaint = null;
@@ -6650,8 +6657,12 @@ var ImageEditorController = class {
6650
6657
  const loc = findLayer(this.state.layers, id);
6651
6658
  if (!loc || loc.layer.type !== "adjustment") return false;
6652
6659
  this.recordHistory();
6653
- this.state.layers = mapLayer(this.state.layers, id, () => null);
6654
- if (this.state.activeLayerId === id) this.state.activeLayerId = null;
6660
+ const prev = this.state.layers;
6661
+ this.state.layers = mapLayer(prev, id, () => null);
6662
+ if (this.state.activeLayerId === id) {
6663
+ this.state.activeLayerId = activeLayerAfterRemoval(prev, this.state.layers, id);
6664
+ this.state.activeTarget = "pixels";
6665
+ }
6655
6666
  this.notify();
6656
6667
  return true;
6657
6668
  }
@@ -6689,10 +6700,15 @@ var ImageEditorController = class {
6689
6700
  const nodes = this.adjustmentNodes();
6690
6701
  if (!nodes.length) return;
6691
6702
  this.recordHistory();
6692
- let layers = this.state.layers;
6703
+ const prev = this.state.layers;
6704
+ let layers = prev;
6693
6705
  for (const n of nodes) layers = mapLayer(layers, n.id, () => null);
6694
6706
  this.state.layers = layers;
6695
- if (nodes.some((n) => n.id === this.state.activeLayerId)) this.state.activeLayerId = null;
6707
+ const active = this.state.activeLayerId;
6708
+ if (active && nodes.some((n) => n.id === active)) {
6709
+ this.state.activeLayerId = activeLayerAfterRemoval(prev, layers, active);
6710
+ this.state.activeTarget = "pixels";
6711
+ }
6696
6712
  this.notify();
6697
6713
  }
6698
6714
  // --- Selection ---
@@ -8009,8 +8025,7 @@ var ImageEditorController = class {
8009
8025
  delete s.legacyOutputBlur;
8010
8026
  s.selection = null;
8011
8027
  s.selectedAnnotationId = null;
8012
- const pixels = flattenLayers(result.layers).filter((l) => l.type === "pixel");
8013
- s.activeLayerId = pixels.length ? pixels[pixels.length - 1].id : null;
8028
+ s.activeLayerId = defaultActiveLayerId(s.layers);
8014
8029
  s.activeTarget = "pixels";
8015
8030
  this.psdResolution = result.resolution;
8016
8031
  if (result.patterns.length) s.patterns = result.patterns;
@@ -8338,7 +8353,7 @@ var ImageEditorController = class {
8338
8353
  s.outputOpacity = data.opacity;
8339
8354
  if (typeof data.legacyOutputBlur === "number" && data.legacyOutputBlur >= 0) s.legacyOutputBlur = data.legacyOutputBlur;
8340
8355
  else delete s.legacyOutputBlur;
8341
- s.activeLayerId = data.activeLayerId && findLayer(s.layers, data.activeLayerId) ? data.activeLayerId : null;
8356
+ s.activeLayerId = data.activeLayerId && findLayer(s.layers, data.activeLayerId) ? data.activeLayerId : defaultActiveLayerId(s.layers);
8342
8357
  const light = data.globalLight;
8343
8358
  if (light && Number.isFinite(light.angle) && Number.isFinite(light.altitude)) s.globalLight = { angle: light.angle, altitude: Math.min(90, Math.max(0, light.altitude)) };
8344
8359
  else delete s.globalLight;
@@ -8365,7 +8380,7 @@ var ImageEditorController = class {
8365
8380
  else delete s.legacyOutputBlur;
8366
8381
  s.crop = data.crop ? { ...data.crop } : null;
8367
8382
  s.transform = { ...data.transform };
8368
- s.activeLayerId = null;
8383
+ s.activeLayerId = defaultActiveLayerId(s.layers);
8369
8384
  delete s.globalLight;
8370
8385
  delete s.patterns;
8371
8386
  delete s.bitDepth;
@@ -8677,6 +8692,49 @@ function scaleAnnotation(ann, sx, sy) {
8677
8692
  }
8678
8693
  }
8679
8694
 
8695
+ // src/image-editor/canvas-viewport.ts
8696
+ var DEFAULT_CANVAS_VIEWPORT = { width: 800, height: 500 };
8697
+ var MAX_CANVAS_BACKING_SIDE = 8192;
8698
+ var currentDevicePixelRatio = () => typeof window !== "undefined" && window.devicePixelRatio > 0 ? window.devicePixelRatio : 1;
8699
+ function measureCanvasViewport(el, devicePixelRatio = currentDevicePixelRatio(), fallback = DEFAULT_CANVAS_VIEWPORT) {
8700
+ const width = el.clientWidth || fallback.width;
8701
+ const height = el.clientHeight || fallback.height;
8702
+ const dpr = Number.isFinite(devicePixelRatio) && devicePixelRatio > 0 ? devicePixelRatio : 1;
8703
+ const box = el.getBoundingClientRect();
8704
+ const layoutW = el.offsetWidth || el.clientWidth;
8705
+ const layoutH = el.offsetHeight || el.clientHeight;
8706
+ const sx = layoutW > 0 && box.width > 0 ? box.width / layoutW : 1;
8707
+ const sy = layoutH > 0 && box.height > 0 ? box.height / layoutH : 1;
8708
+ let pixelRatio = dpr * Math.max(sx, sy);
8709
+ pixelRatio = Math.min(pixelRatio, MAX_CANVAS_BACKING_SIDE / Math.max(width, height));
8710
+ return { width, height, pixelRatio: Number.isFinite(pixelRatio) && pixelRatio > 0 ? pixelRatio : 1 };
8711
+ }
8712
+ function sameCanvasViewport(a, b) {
8713
+ return a.width === b.width && a.height === b.height && Math.abs(a.pixelRatio - b.pixelRatio) < 1e-3;
8714
+ }
8715
+ function watchCanvasViewport(el, onChange) {
8716
+ let last = measureCanvasViewport(el);
8717
+ const check = () => {
8718
+ const next = measureCanvasViewport(el);
8719
+ if (sameCanvasViewport(next, last)) return;
8720
+ last = next;
8721
+ onChange(next);
8722
+ };
8723
+ const stops = [];
8724
+ if (typeof ResizeObserver !== "undefined" && typeof Element !== "undefined" && el instanceof Element) {
8725
+ const observer = new ResizeObserver(check);
8726
+ observer.observe(el);
8727
+ stops.push(() => observer.disconnect());
8728
+ }
8729
+ if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
8730
+ for (const type of ["resize", "transitionend", "animationend"]) {
8731
+ window.addEventListener(type, check, true);
8732
+ stops.push(() => window.removeEventListener(type, check, true));
8733
+ }
8734
+ }
8735
+ return () => stops.forEach((stop) => stop());
8736
+ }
8737
+
8680
8738
  // src/image-editor/document.ts
8681
8739
  var IMAGE_EDITOR_STATE_VERSION = 3;
8682
8740
  var imageEditorStateMigrations = {};
@@ -10140,13 +10198,13 @@ function createImageLayerTools(controller) {
10140
10198
  const remove = {
10141
10199
  name: "remove_layer",
10142
10200
  title: "Delete a layer",
10143
- description: "Deletes a layer (a group with its children). Refused for a layer locked with all. Prefer hiding it (set_layer_properties visible false) when it may be wanted back. One undo step. Returns the removed id and the remaining top-level ids.",
10201
+ description: "Deletes a layer (a group with its children). Refused for a layer locked with all. Prefer hiding it (set_layer_properties visible false) when it may be wanted back. Deleting the active layer makes the nearest paintable pixel layer below it active (else the topmost one). One undo step. Returns the removed id, the remaining top-level ids and the active layer id afterwards.",
10144
10202
  inputSchema: { type: "object", properties: { layerId: LAYER_ID2 }, required: ["layerId"], additionalProperties: false },
10145
10203
  annotations: { title: "Delete a layer", ...DESTRUCTIVE },
10146
10204
  handler: ({ layerId }) => {
10147
10205
  const id = exists(layerId);
10148
10206
  controller.removeLayer(id);
10149
- return { removed: id, layers: controller.getLayers().map((l) => l.id) };
10207
+ return { removed: id, layers: controller.getLayers().map((l) => l.id), activeLayerId: controller.getState().activeLayerId };
10150
10208
  }
10151
10209
  };
10152
10210
  const move = {
@@ -11047,7 +11105,7 @@ var PaintGesture = class {
11047
11105
  return null;
11048
11106
  }
11049
11107
  } catch (err) {
11050
- this.error = err instanceof Error ? err.message : String(err);
11108
+ this.error = userErrorMessage(err);
11051
11109
  this.stroke = null;
11052
11110
  this.move = null;
11053
11111
  }
@@ -11069,7 +11127,7 @@ var PaintGesture = class {
11069
11127
  this.controller.offsetLayer(m.id, { x, y }, !m.moved);
11070
11128
  m.moved = true;
11071
11129
  } catch (err) {
11072
- this.error = err instanceof Error ? err.message : String(err);
11130
+ this.error = userErrorMessage(err);
11073
11131
  this.move = null;
11074
11132
  }
11075
11133
  }
@@ -11079,7 +11137,7 @@ var PaintGesture = class {
11079
11137
  try {
11080
11138
  this.stroke.end();
11081
11139
  } catch (err) {
11082
- this.error = err instanceof Error ? err.message : String(err);
11140
+ this.error = userErrorMessage(err);
11083
11141
  }
11084
11142
  }
11085
11143
  this.stroke = null;
@@ -15225,6 +15283,7 @@ export {
15225
15283
  DEFAULT_AUTO_LEADING,
15226
15284
  DEFAULT_BLENDER,
15227
15285
  DEFAULT_BRUSH,
15286
+ DEFAULT_CANVAS_VIEWPORT,
15228
15287
  DEFAULT_DEPTH_MASK,
15229
15288
  DEFAULT_FLOW,
15230
15289
  DEFAULT_GLOBAL_LIGHT,
@@ -15299,6 +15358,7 @@ export {
15299
15358
  LayerOperationError,
15300
15359
  MAX_BATCH,
15301
15360
  MAX_BRUSH_SIZE,
15361
+ MAX_CANVAS_BACKING_SIDE,
15302
15362
  MAX_EFFECT_REACH,
15303
15363
  MAX_KELVIN,
15304
15364
  MAX_TILT_ACCEL,
@@ -15435,6 +15495,7 @@ export {
15435
15495
  ZlibError,
15436
15496
  activeAdjustmentLayers,
15437
15497
  activeEffectSmartFilters,
15498
+ activeLayerAfterRemoval,
15438
15499
  activeSmartFilters,
15439
15500
  addLayerEffect,
15440
15501
  addLayerFilter,
@@ -15597,6 +15658,7 @@ export {
15597
15658
  decodeSelectionRle,
15598
15659
  deepBytes,
15599
15660
  deepTile,
15661
+ defaultActiveLayerId,
15600
15662
  defaultAdjustmentParams,
15601
15663
  defaultEffectGradient,
15602
15664
  defaultLayerEffect,
@@ -15751,6 +15813,7 @@ export {
15751
15813
  isLayerLocked,
15752
15814
  isLegacyShape,
15753
15815
  isPaintTool,
15816
+ isPaintableLayer,
15754
15817
  isPainterlyBlendMode,
15755
15818
  isPng,
15756
15819
  isQuarterTurn,
@@ -15815,6 +15878,7 @@ export {
15815
15878
  maskSummary,
15816
15879
  matrixFromCorners,
15817
15880
  matrixFromParams,
15881
+ measureCanvasViewport,
15818
15882
  membraneFill,
15819
15883
  mergeLayerEffect,
15820
15884
  mergeShapeComponents,
@@ -15993,6 +16057,7 @@ export {
15993
16057
  runLayerFilters,
15994
16058
  runLayerFiltersFloat,
15995
16059
  runLayerMenuAction,
16060
+ sameCanvasViewport,
15996
16061
  sampleLut,
15997
16062
  sampleToneLut,
15998
16063
  saturationOf,
@@ -16119,6 +16184,7 @@ export {
16119
16184
  updateShapeLayer,
16120
16185
  updateSmartFilter,
16121
16186
  updateTypeLayer,
16187
+ userErrorMessage,
16122
16188
  uvToXy,
16123
16189
  validateEffectGradient,
16124
16190
  validateLayerEffect,
@@ -16132,6 +16198,7 @@ export {
16132
16198
  warpMeshParamAt,
16133
16199
  warpPresetMesh,
16134
16200
  warpSurface,
16201
+ watchCanvasViewport,
16135
16202
  wbMatrix,
16136
16203
  wetGpuSupport,
16137
16204
  wetSimdSupported,
@@ -6,11 +6,29 @@
6
6
  import type { BlendMode } from '../blend.cjs';
7
7
  import type { CropBox } from '../types.cjs';
8
8
  import { type LayerEffect, type LayerEffectKind, type LayerEffects } from '../../raster/effects/model.cjs';
9
- import { type ImageLayer, type LayerBlendMode, type LayerFilter, type LayerLocks, type RasterMask } from './model.cjs';
10
- /** A refused layer operation; the message says why and how to fix it. */
9
+ import { type ImageLayer, type LayerBlendMode, type LayerFilter, type LayerLocks, type PixelLayer, type RasterMask } from './model.cjs';
10
+ /**
11
+ * A refused layer operation; the message says why and how to fix it (written for agents: it names
12
+ * tools and arguments). `userMessage`, when given, says the same for a person at the canvas.
13
+ */
11
14
  export declare class LayerOperationError extends Error {
12
- constructor(message: string);
15
+ readonly userMessage?: string;
16
+ constructor(message: string, userMessage?: string);
13
17
  }
18
+ /** The message of a refusal to show a person: a `LayerOperationError`'s `userMessage`, else the message. */
19
+ export declare function userErrorMessage(err: unknown): string;
20
+ /** Whether brush strokes can go on a layer's pixels: a visible pixel layer without its pixels locked. */
21
+ export declare function isPaintableLayer(layer: ImageLayer): layer is PixelLayer;
22
+ /**
23
+ * The layer a document with no active layer works on: the topmost paintable pixel layer (the
24
+ * Background of a newly loaded image), else the topmost pixel layer, else `null`.
25
+ */
26
+ export declare function defaultActiveLayerId(layers: readonly ImageLayer[]): string | null;
27
+ /**
28
+ * The layer to make active when the active layer `removedId` left the stack (`prev` → `next`):
29
+ * the nearest paintable layer that was below it, else `defaultActiveLayerId(next)`.
30
+ */
31
+ export declare function activeLayerAfterRemoval(prev: readonly ImageLayer[], next: readonly ImageLayer[], removedId: string): string | null;
14
32
  /** Where a layer goes: above or below a layer (in its group), or at a position in a group. */
15
33
  export interface LayerPlacement {
16
34
  /** Directly above this layer, in the same group. */
@@ -6,11 +6,29 @@
6
6
  import type { BlendMode } from '../blend.js';
7
7
  import type { CropBox } from '../types.js';
8
8
  import { type LayerEffect, type LayerEffectKind, type LayerEffects } from '../../raster/effects/model.js';
9
- import { type ImageLayer, type LayerBlendMode, type LayerFilter, type LayerLocks, type RasterMask } from './model.js';
10
- /** A refused layer operation; the message says why and how to fix it. */
9
+ import { type ImageLayer, type LayerBlendMode, type LayerFilter, type LayerLocks, type PixelLayer, type RasterMask } from './model.js';
10
+ /**
11
+ * A refused layer operation; the message says why and how to fix it (written for agents: it names
12
+ * tools and arguments). `userMessage`, when given, says the same for a person at the canvas.
13
+ */
11
14
  export declare class LayerOperationError extends Error {
12
- constructor(message: string);
15
+ readonly userMessage?: string;
16
+ constructor(message: string, userMessage?: string);
13
17
  }
18
+ /** The message of a refusal to show a person: a `LayerOperationError`'s `userMessage`, else the message. */
19
+ export declare function userErrorMessage(err: unknown): string;
20
+ /** Whether brush strokes can go on a layer's pixels: a visible pixel layer without its pixels locked. */
21
+ export declare function isPaintableLayer(layer: ImageLayer): layer is PixelLayer;
22
+ /**
23
+ * The layer a document with no active layer works on: the topmost paintable pixel layer (the
24
+ * Background of a newly loaded image), else the topmost pixel layer, else `null`.
25
+ */
26
+ export declare function defaultActiveLayerId(layers: readonly ImageLayer[]): string | null;
27
+ /**
28
+ * The layer to make active when the active layer `removedId` left the stack (`prev` → `next`):
29
+ * the nearest paintable layer that was below it, else `defaultActiveLayerId(next)`.
30
+ */
31
+ export declare function activeLayerAfterRemoval(prev: readonly ImageLayer[], next: readonly ImageLayer[], removedId: string): string | null;
14
32
  /** Where a layer goes: above or below a layer (in its group), or at a position in a group. */
15
33
  export interface LayerPlacement {
16
34
  /** Directly above this layer, in the same group. */
@@ -7,8 +7,15 @@ import { type PixelSurface } from '../raster/surface.cjs';
7
7
  import type { GlobalLight } from '../raster/effects/model.cjs';
8
8
  import type { EffectPattern } from '../raster/effects/patterns.cjs';
9
9
  export interface RenderOptions {
10
+ /** The viewport's size in CSS pixels: layout, the projection and overlays use these units. */
10
11
  viewportWidth?: number;
11
12
  viewportHeight?: number;
13
+ /**
14
+ * Backing-store pixels per CSS pixel (default 1): the canvas is sized `viewport × pixelRatio` and
15
+ * drawn through that scale, so it stays sharp on high-DPI screens and under CSS transforms. See
16
+ * `measureCanvasViewport`.
17
+ */
18
+ pixelRatio?: number;
12
19
  showCropOverlay?: boolean;
13
20
  showSelectionOverlay?: boolean;
14
21
  interactive?: boolean;
@@ -7,8 +7,15 @@ import { type PixelSurface } from '../raster/surface.js';
7
7
  import type { GlobalLight } from '../raster/effects/model.js';
8
8
  import type { EffectPattern } from '../raster/effects/patterns.js';
9
9
  export interface RenderOptions {
10
+ /** The viewport's size in CSS pixels: layout, the projection and overlays use these units. */
10
11
  viewportWidth?: number;
11
12
  viewportHeight?: number;
13
+ /**
14
+ * Backing-store pixels per CSS pixel (default 1): the canvas is sized `viewport × pixelRatio` and
15
+ * drawn through that scale, so it stays sharp on high-DPI screens and under CSS transforms. See
16
+ * `measureCanvasViewport`.
17
+ */
18
+ pixelRatio?: number;
12
19
  showCropOverlay?: boolean;
13
20
  showSelectionOverlay?: boolean;
14
21
  interactive?: boolean;