@r2digisolutions/components 0.21.3 → 0.21.4

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.
@@ -49,7 +49,9 @@
49
49
  scrollBoxOverflow,
50
50
  scrollBarMetrics,
51
51
  selectionAncestorIds,
52
- clipPathForLayer
52
+ clipPathForLayer,
53
+ topLevelSelectedIds,
54
+ translateSelectionAbsolute
53
55
  } from '../../../utils/canvasHierarchy.js';
54
56
  import { flattenLayersWithWidgets } from '../../../utils/canvasWidget.js';
55
57
  import { backgroundAlpha } from '../../../utils/canvasExport.js';
@@ -193,6 +195,11 @@
193
195
  let selectionLive = $state<{ x: number; y: number; w: number; h: number } | null>(null);
194
196
  let selectionBaseLayers = $state<CanvasLayer[] | null>(null);
195
197
  let selectionBaseBox = $state<{ x: number; y: number; w: number; h: number } | null>(null);
198
+ /** Snapshot for rigid multi-select drag (from pointer-down, not last frame). */
199
+ let moveBaseLayers = $state<CanvasLayer[] | null>(null);
200
+ let moveBaseAbs = $state<Map<string, { x: number; y: number; w: number; h: number }> | null>(
201
+ null
202
+ );
196
203
  const selectionFrame = $derived(selectionLive ?? selectionBounds);
197
204
 
198
205
  const fitScale = $derived.by(() => {
@@ -291,7 +298,14 @@
291
298
  return r;
292
299
  },
293
300
  clampRect(rect: WidgetRect, minW: number, minH: number) {
294
- return clampWidgetRect(rect, { width: doc.width, height: doc.height }, minW, minH);
301
+ const sized = {
302
+ ...rect,
303
+ w: Math.max(minW, rect.w),
304
+ h: Math.max(minH, rect.h)
305
+ };
306
+ // Snap on = stay on the artboard. Snap off = free, including off-canvas.
307
+ if (!snap) return sized;
308
+ return clampWidgetRect(sized, { width: doc.width, height: doc.height }, minW, minH);
295
309
  }
296
310
  };
297
311
 
@@ -334,25 +348,53 @@
334
348
  });
335
349
  }
336
350
 
337
- function commitGroupGeometry(next: CanvasLayer[]) {
338
- draftLayers = fitGroupsToChildren(next, { width: doc.width, height: doc.height });
339
- if (interactCount === 0) commitDraft();
340
- }
341
-
342
351
  function commitDraft() {
343
352
  if (!draftLayers) return;
344
- const layers = draftLayers;
353
+ const layers = fitGroupsToChildren(draftLayers, { width: doc.width, height: doc.height });
345
354
  draftLayers = null;
346
355
  ondocumentchange?.({ ...doc, layers });
347
356
  }
348
357
 
358
+ function commitGroupGeometry(next: CanvasLayer[]) {
359
+ if (interactCount > 0) {
360
+ draftLayers = next;
361
+ return;
362
+ }
363
+ draftLayers = fitGroupsToChildren(next, { width: doc.width, height: doc.height });
364
+ commitDraft();
365
+ }
366
+
367
+ function clearMoveSnapshot() {
368
+ moveBaseLayers = null;
369
+ moveBaseAbs = null;
370
+ }
371
+
372
+ function clampDeltaToKeepUnion(
373
+ union: { x: number; y: number; w: number; h: number },
374
+ dx: number,
375
+ dy: number
376
+ ) {
377
+ let x = union.x + dx;
378
+ let y = union.y + dy;
379
+ if (union.w < doc.width) x = Math.min(Math.max(0, x), doc.width - union.w);
380
+ if (union.h < doc.height) y = Math.min(Math.max(0, y), doc.height - union.h);
381
+ return { dx: x - union.x, dy: y - union.y };
382
+ }
383
+
349
384
  function handleLayerInteract(active: boolean) {
350
385
  if (active) {
351
386
  interactCount += 1;
387
+ if (selectedIds.length > 1 && !moveBaseLayers) {
388
+ moveBaseLayers = workingLayers;
389
+ moveBaseAbs = new Map(absMap);
390
+ }
352
391
  return;
353
392
  }
354
393
  interactCount = Math.max(0, interactCount - 1);
355
- if (interactCount === 0) commitDraft();
394
+ if (interactCount === 0) {
395
+ clearMoveSnapshot();
396
+ commitDraft();
397
+ }
356
398
  }
357
399
 
358
400
  function cancelLayerDraft() {
@@ -361,6 +403,7 @@
361
403
  selectionBaseLayers = null;
362
404
  selectionBaseBox = null;
363
405
  selectionLive = null;
406
+ clearMoveSnapshot();
364
407
  }
365
408
 
366
409
  function isLayoutLocked(layer: CanvasLayer): boolean {
@@ -395,7 +438,9 @@
395
438
  { width: doc.width, height: doc.height },
396
439
  minW,
397
440
  minH,
398
- guideSnapList
441
+ guideSnapList,
442
+ 8,
443
+ false
399
444
  );
400
445
 
401
446
  const prevAbs = absMap.get(source.id) ?? source.rect;
@@ -417,21 +462,34 @@
417
462
  (Math.abs(dx) > 0.01 || Math.abs(dy) > 0.01);
418
463
 
419
464
  if (groupMove) {
465
+ const baseLayers = moveBaseLayers ?? layers;
466
+ const baseAbs = moveBaseAbs ?? absMap;
467
+ const start = baseAbs.get(source.id) ?? source.rect;
468
+ let moveDx = nextAbs.x - start.x;
469
+ let moveDy = nextAbs.y - start.y;
470
+ const movableIds = selectedIds.filter(
471
+ (id) =>
472
+ !isEffectivelyLocked(workingLayers, id, workingById) &&
473
+ !isLayoutPositionLocked(workingLayers, id, workingById)
474
+ );
475
+ if (snap) {
476
+ const top = topLevelSelectedIds(baseLayers, movableIds);
477
+ const union = unionAbsRect(baseAbs, top);
478
+ if (union) {
479
+ const clamped = clampDeltaToKeepUnion(union, moveDx, moveDy);
480
+ moveDx = clamped.dx;
481
+ moveDy = clamped.dy;
482
+ }
483
+ }
420
484
  commitGroupGeometry(
421
- layers.map((l) => {
422
- if (!selectedSet.has(l.id) || isEffectivelyLocked(workingLayers, l.id, workingById)) {
423
- return l;
424
- }
425
- if (isLayoutPositionLocked(workingLayers, l.id, workingById)) return l;
426
- const a = absMap.get(l.id) ?? l.rect;
427
- const moved = {
428
- x: a.x + dx,
429
- y: a.y + dy,
430
- w: a.w,
431
- h: a.h
432
- };
433
- return absToLocalUpdate(l, moved);
434
- })
485
+ translateSelectionAbsolute(
486
+ baseLayers,
487
+ movableIds,
488
+ moveDx,
489
+ moveDy,
490
+ { width: doc.width, height: doc.height },
491
+ baseAbs
492
+ )
435
493
  );
436
494
  return;
437
495
  }
package/dist/index.d.ts CHANGED
@@ -865,7 +865,7 @@ export type { MediaAsset, MediaAssetKind, MediaClip, MediaClipAlign, MediaClipRe
865
865
  export { uid, createMediaClip, createMediaTrack, createMediaAsset, clipDurationMs, moveClip, trimClipStart, trimClipEnd, isClipActiveAt, findClipAt, updateClipInTracks, removeClipFromTracks, removeClipsFromTracks, splitClipAt, splitClipsAt, duplicateClipInTracks, mergeClipsInTracks, canMergeMediaClips, moveClipsByDelta, findClipById, nextTrackName, reorderTracks, moveTrack, removeTrack, removeTracks, defaultOverlayRect, defaultMediaClipRect, resolveClipRect, alignClipRect, alignMediaRect, clipHasSpatialRect, isMediaClipFieldModified, resetMediaClipField, findTransitionAt, transitionLayerStyle, transitionDipOverlay, resolvePreviewOpacity, resolveClipOpacity, MEDIA_CLIP_DEFAULTS, kindFromFile, probeMediaSrc } from './utils/mediaTracks.js';
866
866
  export { emptyCanvasDocument, createCanvasLayer, createCanvasGuide, createPathFromDocPoints, rebakePathLayer, pathPointsToDoc, reorderCanvasLayers, updateCanvasLayer, updateCanvasLayers, alignLayerRect, snapLayerRect, snapRectToGuides, migrateCanvasDocument, defaultSlotFromRect, rectFromSlot, ensureLayerSlot, canvasLayerFieldDefault, isCanvasFieldModified, resetCanvasField, applyTextAutoSize, CANVAS_PRESETS, presetIdForSize } from './utils/canvasDocument.js';
867
867
  export type { CanvasDocument, CanvasLayer, CanvasLayerKind, CanvasLayerRect, CanvasPoint, CanvasObjectFit, CanvasTextAlign, CanvasResettableField, CanvasAlign, CanvasFontStyle, CanvasTextDecoration, CanvasGuide, CanvasGuideOrientation, CanvasAnchors, CanvasSlot, CanvasExposedProp, CanvasWidgetDefinition } from './utils/canvasDocument.js';
868
- export { isContainerKind, canHaveChildren, getChildren, getDescendantIds, getSubtreeIds, getAncestors, enclosingGroupId, isEffectivelyVisible, isEffectivelyLocked, isLayoutPositionLocked, isLayoutSizeLocked, selectionAncestorIds, LAYOUT_POSITION_KINDS, LAYOUT_SIZE_KINDS, paintTransformForLayer, isDescendant, canReparent, ANCHOR_PRESETS, resolveSlotRect, slotFromLocalRect, translateSlot, stepAxis, computeAbsoluteRects, absoluteRectFor, reparentLayer, wrapSelection, groupLayers, ungroupLayers, layerFromAbsoluteRect, fitGroupsToChildren, scaleSubtreeAbsolute, scaleSelectionAbsolute, shouldAutoSizeChildren, topLevelSelectedIds, unionAbsRect, deleteSubtree, duplicateSubtree, reorderSiblings, clipPathForLayer, scrollBoxOverflow, scrollBarMetrics, applyScrollBoxOffsets } from './utils/canvasHierarchy.js';
868
+ export { isContainerKind, canHaveChildren, getChildren, getDescendantIds, getSubtreeIds, getAncestors, enclosingGroupId, isEffectivelyVisible, isEffectivelyLocked, isLayoutPositionLocked, isLayoutSizeLocked, selectionAncestorIds, LAYOUT_POSITION_KINDS, LAYOUT_SIZE_KINDS, paintTransformForLayer, isDescendant, canReparent, ANCHOR_PRESETS, resolveSlotRect, slotFromLocalRect, translateSlot, stepAxis, computeAbsoluteRects, absoluteRectFor, reparentLayer, wrapSelection, groupLayers, ungroupLayers, layerFromAbsoluteRect, fitGroupsToChildren, scaleSubtreeAbsolute, scaleSelectionAbsolute, shouldAutoSizeChildren, topLevelSelectedIds, translateSelectionAbsolute, unionAbsRect, deleteSubtree, duplicateSubtree, reorderSiblings, clipPathForLayer, scrollBoxOverflow, scrollBarMetrics, applyScrollBoxOffsets } from './utils/canvasHierarchy.js';
869
869
  export { createWidgetDefinition, createWidgetFromSelection, placeWidgetInstance, resolveWidgetInstance, flattenLayersWithWidgets, breakWidgetInstance, updateWidgetDefinition, deleteWidgetDefinition, renameWidgetDefinition, duplicateWidgetDefinition, syncInstanceSizesToDefinition, setExposedProp, isPropExposed, getExposedProp, getExposedFieldDefault, resolveExposedPropValue, groupExposedPropsByLayer, setInstanceOverride, getNamedSlotsFromDefinition, listFillableSlotsForInstance } from './utils/canvasWidget.js';
870
870
  export { createCanvasHistory, pushCanvasHistory, undoCanvasHistory, redoCanvasHistory, serializeCanvasDoc, parseCanvasDoc } from './utils/canvasHistory.js';
871
871
  export type { CanvasHistoryState } from './utils/canvasHistory.js';
package/dist/index.js CHANGED
@@ -587,7 +587,7 @@ export { emptyBlogDocument, createBlock, createComponentBlock, serializeBlogDocu
587
587
  export { msToPx, pxToMs, pxPerMsFromZoom, snapMs, clampMs, formatTimecode, ticksForDuration, snapStepForZoom } from './utils/mediaTime.js';
588
588
  export { uid, createMediaClip, createMediaTrack, createMediaAsset, clipDurationMs, moveClip, trimClipStart, trimClipEnd, isClipActiveAt, findClipAt, updateClipInTracks, removeClipFromTracks, removeClipsFromTracks, splitClipAt, splitClipsAt, duplicateClipInTracks, mergeClipsInTracks, canMergeMediaClips, moveClipsByDelta, findClipById, nextTrackName, reorderTracks, moveTrack, removeTrack, removeTracks, defaultOverlayRect, defaultMediaClipRect, resolveClipRect, alignClipRect, alignMediaRect, clipHasSpatialRect, isMediaClipFieldModified, resetMediaClipField, findTransitionAt, transitionLayerStyle, transitionDipOverlay, resolvePreviewOpacity, resolveClipOpacity, MEDIA_CLIP_DEFAULTS, kindFromFile, probeMediaSrc } from './utils/mediaTracks.js';
589
589
  export { emptyCanvasDocument, createCanvasLayer, createCanvasGuide, createPathFromDocPoints, rebakePathLayer, pathPointsToDoc, reorderCanvasLayers, updateCanvasLayer, updateCanvasLayers, alignLayerRect, snapLayerRect, snapRectToGuides, migrateCanvasDocument, defaultSlotFromRect, rectFromSlot, ensureLayerSlot, canvasLayerFieldDefault, isCanvasFieldModified, resetCanvasField, applyTextAutoSize, CANVAS_PRESETS, presetIdForSize } from './utils/canvasDocument.js';
590
- export { isContainerKind, canHaveChildren, getChildren, getDescendantIds, getSubtreeIds, getAncestors, enclosingGroupId, isEffectivelyVisible, isEffectivelyLocked, isLayoutPositionLocked, isLayoutSizeLocked, selectionAncestorIds, LAYOUT_POSITION_KINDS, LAYOUT_SIZE_KINDS, paintTransformForLayer, isDescendant, canReparent, ANCHOR_PRESETS, resolveSlotRect, slotFromLocalRect, translateSlot, stepAxis, computeAbsoluteRects, absoluteRectFor, reparentLayer, wrapSelection, groupLayers, ungroupLayers, layerFromAbsoluteRect, fitGroupsToChildren, scaleSubtreeAbsolute, scaleSelectionAbsolute, shouldAutoSizeChildren, topLevelSelectedIds, unionAbsRect, deleteSubtree, duplicateSubtree, reorderSiblings, clipPathForLayer, scrollBoxOverflow, scrollBarMetrics, applyScrollBoxOffsets } from './utils/canvasHierarchy.js';
590
+ export { isContainerKind, canHaveChildren, getChildren, getDescendantIds, getSubtreeIds, getAncestors, enclosingGroupId, isEffectivelyVisible, isEffectivelyLocked, isLayoutPositionLocked, isLayoutSizeLocked, selectionAncestorIds, LAYOUT_POSITION_KINDS, LAYOUT_SIZE_KINDS, paintTransformForLayer, isDescendant, canReparent, ANCHOR_PRESETS, resolveSlotRect, slotFromLocalRect, translateSlot, stepAxis, computeAbsoluteRects, absoluteRectFor, reparentLayer, wrapSelection, groupLayers, ungroupLayers, layerFromAbsoluteRect, fitGroupsToChildren, scaleSubtreeAbsolute, scaleSelectionAbsolute, shouldAutoSizeChildren, topLevelSelectedIds, translateSelectionAbsolute, unionAbsRect, deleteSubtree, duplicateSubtree, reorderSiblings, clipPathForLayer, scrollBoxOverflow, scrollBarMetrics, applyScrollBoxOffsets } from './utils/canvasHierarchy.js';
591
591
  export { createWidgetDefinition, createWidgetFromSelection, placeWidgetInstance, resolveWidgetInstance, flattenLayersWithWidgets, breakWidgetInstance, updateWidgetDefinition, deleteWidgetDefinition, renameWidgetDefinition, duplicateWidgetDefinition, syncInstanceSizesToDefinition, setExposedProp, isPropExposed, getExposedProp, getExposedFieldDefault, resolveExposedPropValue, groupExposedPropsByLayer, setInstanceOverride, getNamedSlotsFromDefinition, listFillableSlotsForInstance } from './utils/canvasWidget.js';
592
592
  export { createCanvasHistory, pushCanvasHistory, undoCanvasHistory, redoCanvasHistory, serializeCanvasDoc, parseCanvasDoc } from './utils/canvasHistory.js';
593
593
  export { renderCanvasDocument, exportCanvasBlob, downloadBlob, backgroundAlpha } from './utils/canvasExport.js';
@@ -182,7 +182,7 @@ export declare function updateCanvasLayers(doc: CanvasDocument, ids: string[], u
182
182
  export declare function snapLayerRect(rect: CanvasLayerRect, cellSize: number, enabled: boolean, bounds: {
183
183
  width: number;
184
184
  height: number;
185
- }, minW?: number, minH?: number, guides?: Pick<CanvasGuide, 'orientation' | 'position'>[], guideThreshold?: number): CanvasLayerRect;
185
+ }, minW?: number, minH?: number, guides?: Pick<CanvasGuide, 'orientation' | 'position'>[], guideThreshold?: number, clampToBounds?: boolean): CanvasLayerRect;
186
186
  /** Snap left / center / right (or top / middle / bottom) edges to guides. */
187
187
  export declare function snapRectToGuides(rect: CanvasLayerRect, guides: Pick<CanvasGuide, 'orientation' | 'position'>[], threshold?: number): CanvasLayerRect;
188
188
  export declare function alignLayerRect(rect: CanvasLayerRect, bounds: {
@@ -149,7 +149,8 @@ export function createCanvasLayer(kind, partial) {
149
149
  text: partial?.text ?? d.text,
150
150
  fill: partial?.fill ?? d.fill,
151
151
  stroke: partial?.stroke,
152
- strokeWidth: partial?.strokeWidth ?? (kind === 'line' || kind === 'arrow' || kind === 'path' ? 4 : undefined),
152
+ strokeWidth: partial?.strokeWidth ??
153
+ (kind === 'line' || kind === 'arrow' || kind === 'path' ? 4 : undefined),
153
154
  fontSize: partial?.fontSize ?? d.fontSize,
154
155
  fontWeight: partial?.fontWeight,
155
156
  fontFamily: partial?.fontFamily,
@@ -176,7 +177,7 @@ export function createCanvasLayer(kind, partial) {
176
177
  clipChildren: partial?.clipChildren ?? d.clipChildren ?? false,
177
178
  autoSizeChildren: partial?.autoSizeChildren ?? d.autoSizeChildren ?? (kind === 'group' ? true : undefined),
178
179
  gap: partial?.gap ?? d.gap,
179
- slotName: partial?.slotName ?? (kind === 'namedSlot' ? partial?.name ?? d.name : undefined),
180
+ slotName: partial?.slotName ?? (kind === 'namedSlot' ? (partial?.name ?? d.name) : undefined),
180
181
  fillSlot: partial?.fillSlot,
181
182
  columns: partial?.columns ?? (kind === 'uniformGrid' ? 2 : undefined),
182
183
  definitionId: partial?.definitionId,
@@ -324,7 +325,7 @@ export function updateCanvasLayers(doc, ids, updater) {
324
325
  layers: doc.layers.map((l) => (set.has(l.id) ? updater(l) : l))
325
326
  };
326
327
  }
327
- export function snapLayerRect(rect, cellSize, enabled, bounds, minW = 24, minH = 24, guides = [], guideThreshold = 8) {
328
+ export function snapLayerRect(rect, cellSize, enabled, bounds, minW = 24, minH = 24, guides = [], guideThreshold = 8, clampToBounds = true) {
328
329
  const snap = (v) => (enabled ? Math.round(v / cellSize) * cellSize : v);
329
330
  let w = Math.max(minW, enabled ? snap(rect.w) : rect.w);
330
331
  let h = Math.max(minH, enabled ? snap(rect.h) : rect.h);
@@ -335,10 +336,12 @@ export function snapLayerRect(rect, cellSize, enabled, bounds, minW = 24, minH =
335
336
  x = snapped.x;
336
337
  y = snapped.y;
337
338
  }
338
- x = Math.min(Math.max(0, x), Math.max(0, bounds.width - w));
339
- y = Math.min(Math.max(0, y), Math.max(0, bounds.height - h));
340
- w = Math.min(w, bounds.width - x);
341
- h = Math.min(h, bounds.height - y);
339
+ if (clampToBounds) {
340
+ x = Math.min(Math.max(0, x), Math.max(0, bounds.width - w));
341
+ y = Math.min(Math.max(0, y), Math.max(0, bounds.height - h));
342
+ w = Math.min(w, bounds.width - x);
343
+ h = Math.min(h, bounds.height - y);
344
+ }
342
345
  return { x, y, w, h };
343
346
  }
344
347
  /** Snap left / center / right (or top / middle / bottom) edges to guides. */
@@ -261,3 +261,12 @@ export declare function scaleSelectionAbsolute(layers: CanvasLayer[], selectedId
261
261
  width: number;
262
262
  height: number;
263
263
  }): CanvasLayer[];
264
+ /**
265
+ * Translate a multi-selection as one rigid body. Only top-level selected layers
266
+ * move; descendants keep parent-local coords so a group+child selection does not
267
+ * double-shift nested items.
268
+ */
269
+ export declare function translateSelectionAbsolute(layers: CanvasLayer[], selectedIds: string[], dx: number, dy: number, rootSize: {
270
+ width: number;
271
+ height: number;
272
+ }, absMap?: Map<string, CanvasLayerRect>): CanvasLayer[];
@@ -985,3 +985,22 @@ export function scaleSelectionAbsolute(layers, selectedIds, prevBox, nextBox, ro
985
985
  }
986
986
  return applyAbsoluteRects(layers, nextAbsMap, absMap, rootSize);
987
987
  }
988
+ /**
989
+ * Translate a multi-selection as one rigid body. Only top-level selected layers
990
+ * move; descendants keep parent-local coords so a group+child selection does not
991
+ * double-shift nested items.
992
+ */
993
+ export function translateSelectionAbsolute(layers, selectedIds, dx, dy, rootSize, absMap = computeAbsoluteRects(layers, rootSize)) {
994
+ if (Math.abs(dx) < 0.01 && Math.abs(dy) < 0.01)
995
+ return layers;
996
+ const byId = new Map(layers.map((l) => [l.id, l]));
997
+ const top = new Set(topLevelSelectedIds(layers, selectedIds));
998
+ return layers.map((l) => {
999
+ if (!top.has(l.id))
1000
+ return l;
1001
+ const a = absMap.get(l.id) ?? l.rect;
1002
+ const parent = l.parentId ? (byId.get(l.parentId) ?? null) : null;
1003
+ const parentAbs = l.parentId ? (absMap.get(l.parentId) ?? null) : rootFrame(rootSize);
1004
+ return layerFromAbsoluteRect(l, { x: a.x + dx, y: a.y + dy, w: a.w, h: a.h }, parentAbs, parent, rootSize);
1005
+ });
1006
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@r2digisolutions/components",
3
- "version": "0.21.3",
3
+ "version": "0.21.4",
4
4
  "private": false,
5
5
  "description": "R2DigiSolutions Svelte 5 component library — Atomic Design, Tailwind 4, Light/Dark mode",
6
6
  "license": "MIT",