@r2digisolutions/components 0.19.22 → 0.20.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.
- package/dist/components/molecules/MediaStage/MediaStage.svelte +54 -45
- package/dist/components/organisms/CanvasEditor/CanvasEditor.svelte +18 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/utils/canvasHierarchy.d.ts +22 -0
- package/dist/utils/canvasHierarchy.js +137 -0
- package/package.json +1 -1
|
@@ -30,18 +30,19 @@
|
|
|
30
30
|
import {
|
|
31
31
|
applyScrollBoxOffsets,
|
|
32
32
|
computeAbsoluteRects,
|
|
33
|
-
contentPadding,
|
|
34
33
|
enclosingGroupId,
|
|
34
|
+
fitGroupsToChildren,
|
|
35
35
|
isEffectivelyLocked,
|
|
36
36
|
isEffectivelyVisible,
|
|
37
37
|
isLayoutPositionLocked,
|
|
38
38
|
isLayoutSizeLocked,
|
|
39
39
|
isMarqueePassThroughKind,
|
|
40
|
+
layerFromAbsoluteRect,
|
|
40
41
|
paintTransformForLayer,
|
|
42
|
+
scaleSubtreeAbsolute,
|
|
41
43
|
scrollBoxOverflow,
|
|
42
44
|
scrollBarMetrics,
|
|
43
45
|
selectionAncestorIds,
|
|
44
|
-
slotFromLocalRect,
|
|
45
46
|
clipPathForLayer
|
|
46
47
|
} from '../../../utils/canvasHierarchy.js';
|
|
47
48
|
import { flattenLayersWithWidgets } from '../../../utils/canvasWidget.js';
|
|
@@ -312,28 +313,15 @@
|
|
|
312
313
|
const parentId = layer.parentId ?? null;
|
|
313
314
|
const parentAbs = parentId ? (absMap.get(parentId) ?? null) : null;
|
|
314
315
|
const parentLayer = parentId ? (workingLayers.find((l) => l.id === parentId) ?? null) : null;
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
:
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
y: absRect.y - originY,
|
|
325
|
-
w: absRect.w,
|
|
326
|
-
h: absRect.h
|
|
327
|
-
};
|
|
328
|
-
const anchors = layer.slot?.anchors ?? { minX: 0, minY: 0, maxX: 0, maxY: 0 };
|
|
329
|
-
const slot = {
|
|
330
|
-
...slotFromLocalRect({ width: contentW, height: contentH }, local, anchors),
|
|
331
|
-
padding: layer.slot?.padding,
|
|
332
|
-
sizeRule: layer.slot?.sizeRule,
|
|
333
|
-
alignment: layer.slot?.alignment,
|
|
334
|
-
order: layer.slot?.order
|
|
335
|
-
};
|
|
336
|
-
return { ...layer, rect: local, slot };
|
|
316
|
+
return layerFromAbsoluteRect(layer, absRect, parentAbs, parentLayer, {
|
|
317
|
+
width: doc.width,
|
|
318
|
+
height: doc.height
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function commitGroupGeometry(next: CanvasLayer[]) {
|
|
323
|
+
draftLayers = fitGroupsToChildren(next, { width: doc.width, height: doc.height });
|
|
324
|
+
if (interactCount === 0) commitDraft();
|
|
337
325
|
}
|
|
338
326
|
|
|
339
327
|
function commitDraft() {
|
|
@@ -411,27 +399,40 @@
|
|
|
411
399
|
(Math.abs(dx) > 0.01 || Math.abs(dy) > 0.01);
|
|
412
400
|
|
|
413
401
|
if (groupMove) {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
402
|
+
commitGroupGeometry(
|
|
403
|
+
layers.map((l) => {
|
|
404
|
+
if (!selectedSet.has(l.id) || isEffectivelyLocked(workingLayers, l.id, workingById)) {
|
|
405
|
+
return l;
|
|
406
|
+
}
|
|
407
|
+
if (isLayoutPositionLocked(workingLayers, l.id, workingById)) return l;
|
|
408
|
+
const a = absMap.get(l.id) ?? l.rect;
|
|
409
|
+
const moved = {
|
|
410
|
+
x: a.x + dx,
|
|
411
|
+
y: a.y + dy,
|
|
412
|
+
w: a.w,
|
|
413
|
+
h: a.h
|
|
414
|
+
};
|
|
415
|
+
return absToLocalUpdate(l, moved);
|
|
416
|
+
})
|
|
417
|
+
);
|
|
428
418
|
return;
|
|
429
419
|
}
|
|
430
420
|
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
if (
|
|
421
|
+
const sizeChanged = !moving;
|
|
422
|
+
const rootSize = { width: doc.width, height: doc.height };
|
|
423
|
+
let nextLayers = layers;
|
|
424
|
+
if (source.kind === 'group' && sizeChanged) {
|
|
425
|
+
nextLayers = scaleSubtreeAbsolute(layers, source.id, prevAbs, nextAbs, rootSize);
|
|
426
|
+
} else {
|
|
427
|
+
const updated = applyTextAutoSize(
|
|
428
|
+
source,
|
|
429
|
+
absToLocalUpdate(source, nextAbs),
|
|
430
|
+
prevAbs,
|
|
431
|
+
nextAbs
|
|
432
|
+
);
|
|
433
|
+
nextLayers = layers.map((l) => (l.id === updated.id ? updated : l));
|
|
434
|
+
}
|
|
435
|
+
commitGroupGeometry(nextLayers);
|
|
435
436
|
}
|
|
436
437
|
|
|
437
438
|
function groupedChildPassthrough(layer: CanvasLayer): boolean {
|
|
@@ -516,7 +517,10 @@
|
|
|
516
517
|
const s = scale;
|
|
517
518
|
return sorted
|
|
518
519
|
.filter((l) => {
|
|
519
|
-
if (
|
|
520
|
+
if (
|
|
521
|
+
!effectivelyVisible.has(l.id) ||
|
|
522
|
+
isEffectivelyLocked(workingLayers, l.id, workingById)
|
|
523
|
+
) {
|
|
520
524
|
return false;
|
|
521
525
|
}
|
|
522
526
|
const realId = resolveSelectableId(l.id);
|
|
@@ -798,7 +802,12 @@
|
|
|
798
802
|
}
|
|
799
803
|
|
|
800
804
|
function removeActivePathPoint() {
|
|
801
|
-
if (
|
|
805
|
+
if (
|
|
806
|
+
!selectedPath ||
|
|
807
|
+
isEffectivelyLocked(workingLayers, selectedPath.id, workingById) ||
|
|
808
|
+
activePathPoint == null
|
|
809
|
+
)
|
|
810
|
+
return;
|
|
802
811
|
const docs = pathAbsPoints(selectedPath);
|
|
803
812
|
if (docs.length <= 2) return;
|
|
804
813
|
docs.splice(activePathPoint, 1);
|
|
@@ -1302,7 +1311,7 @@
|
|
|
1302
1311
|
{@const outline = paintAbsMap.get(pid)}
|
|
1303
1312
|
{#if outline}
|
|
1304
1313
|
<div
|
|
1305
|
-
class="pointer-events-none absolute z-[999980] outline outline-1 outline-
|
|
1314
|
+
class="pointer-events-none absolute z-[999980] outline outline-1 outline-[#3b82f6]/80 outline-dashed"
|
|
1306
1315
|
style:left="{outline.x}px"
|
|
1307
1316
|
style:top="{outline.y}px"
|
|
1308
1317
|
style:width="{outline.w}px"
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
import {
|
|
37
37
|
deleteSubtree,
|
|
38
38
|
duplicateSubtree,
|
|
39
|
+
fitGroupsToChildren,
|
|
39
40
|
getAncestors,
|
|
40
41
|
isContainerKind,
|
|
41
42
|
isEffectivelyLocked,
|
|
@@ -45,6 +46,7 @@
|
|
|
45
46
|
reparentLayer,
|
|
46
47
|
reorderSiblings,
|
|
47
48
|
resolveSlotRect,
|
|
49
|
+
scaleSubtreeAbsolute,
|
|
48
50
|
stepAxis,
|
|
49
51
|
syncSlotFromRect,
|
|
50
52
|
ungroupLayers,
|
|
@@ -421,10 +423,13 @@
|
|
|
421
423
|
}
|
|
422
424
|
|
|
423
425
|
function emitStageLayers(layers: CanvasLayer[], extra?: Partial<CanvasDocument>) {
|
|
426
|
+
const width = extra?.width ?? stageDoc.width;
|
|
427
|
+
const height = extra?.height ?? stageDoc.height;
|
|
428
|
+
const fitted = fitGroupsToChildren(layers, { width, height });
|
|
424
429
|
if (editingWidgetId) {
|
|
425
430
|
let next = updateWidgetDefinition(value, editingWidgetId, (d) => ({
|
|
426
431
|
...d,
|
|
427
|
-
layers,
|
|
432
|
+
layers: fitted,
|
|
428
433
|
width: extra?.width ?? d.width,
|
|
429
434
|
height: extra?.height ?? d.height,
|
|
430
435
|
background: extra?.background ?? d.background
|
|
@@ -436,11 +441,21 @@
|
|
|
436
441
|
emit(next);
|
|
437
442
|
return;
|
|
438
443
|
}
|
|
439
|
-
emit({ ...value, ...extra, layers });
|
|
444
|
+
emit({ ...value, ...extra, layers: fitted });
|
|
440
445
|
}
|
|
441
446
|
|
|
442
447
|
function patchLayer(layer: CanvasLayer) {
|
|
443
|
-
|
|
448
|
+
const rootSize = { width: stageDoc.width, height: stageDoc.height };
|
|
449
|
+
const prev = stageDoc.layers.find((l) => l.id === layer.id);
|
|
450
|
+
let layers = stageDoc.layers.map((l) => (l.id === layer.id ? layer : l));
|
|
451
|
+
if (prev && layer.kind === 'group') {
|
|
452
|
+
const prevAbs = computeAbsoluteRects(stageDoc.layers, rootSize).get(prev.id) ?? prev.rect;
|
|
453
|
+
const nextAbs = computeAbsoluteRects(layers, rootSize).get(layer.id) ?? layer.rect;
|
|
454
|
+
if (Math.abs(nextAbs.w - prevAbs.w) > 0.5 || Math.abs(nextAbs.h - prevAbs.h) > 0.5) {
|
|
455
|
+
layers = scaleSubtreeAbsolute(stageDoc.layers, layer.id, prevAbs, nextAbs, rootSize);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
emitStageLayers(layers);
|
|
444
459
|
}
|
|
445
460
|
|
|
446
461
|
function addLayer(layer: CanvasLayer, opts?: { parentId?: string | null }) {
|
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, 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, 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, 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, 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';
|
|
@@ -220,3 +220,25 @@ export declare function syncSlotFromRect(layer: CanvasLayer, parentSize: {
|
|
|
220
220
|
width: number;
|
|
221
221
|
height: number;
|
|
222
222
|
}): CanvasLayer;
|
|
223
|
+
/** Write an absolute rect back to layer.rect / slot in parent-local space. */
|
|
224
|
+
export declare function layerFromAbsoluteRect(layer: CanvasLayer, absRect: CanvasLayerRect, parentAbs: CanvasLayerRect | null, parentLayer: CanvasLayer | null, rootSize: {
|
|
225
|
+
width: number;
|
|
226
|
+
height: number;
|
|
227
|
+
}): CanvasLayer;
|
|
228
|
+
/**
|
|
229
|
+
* Figma-style groups: origin + size is the AABB of direct children.
|
|
230
|
+
* Children keep their absolute positions (local coords are rebased).
|
|
231
|
+
* Nested groups are fitted innermost-first.
|
|
232
|
+
*/
|
|
233
|
+
export declare function fitGroupsToChildren(layers: CanvasLayer[], rootSize: {
|
|
234
|
+
width: number;
|
|
235
|
+
height: number;
|
|
236
|
+
}): CanvasLayer[];
|
|
237
|
+
/**
|
|
238
|
+
* Scale a group (or any subtree) from `prevAbs` to `nextAbs`.
|
|
239
|
+
* Descendants keep their relative placement inside the box.
|
|
240
|
+
*/
|
|
241
|
+
export declare function scaleSubtreeAbsolute(layers: CanvasLayer[], rootId: string, prevAbs: CanvasLayerRect, nextAbs: CanvasLayerRect, rootSize: {
|
|
242
|
+
width: number;
|
|
243
|
+
height: number;
|
|
244
|
+
}): CanvasLayer[];
|
|
@@ -767,3 +767,140 @@ export function syncSlotFromRect(layer, parentSize) {
|
|
|
767
767
|
};
|
|
768
768
|
return { ...layer, slot, rect: rectFromSlot(slot, parentSize) };
|
|
769
769
|
}
|
|
770
|
+
const GROUP_FIT_EPS = 0.5;
|
|
771
|
+
function rectsApproxEqual(a, b, eps = GROUP_FIT_EPS) {
|
|
772
|
+
return (Math.abs(a.x - b.x) < eps &&
|
|
773
|
+
Math.abs(a.y - b.y) < eps &&
|
|
774
|
+
Math.abs(a.w - b.w) < eps &&
|
|
775
|
+
Math.abs(a.h - b.h) < eps);
|
|
776
|
+
}
|
|
777
|
+
function rootFrame(rootSize) {
|
|
778
|
+
return { x: 0, y: 0, w: rootSize.width, h: rootSize.height };
|
|
779
|
+
}
|
|
780
|
+
function ancestorDepth(layers, layerId, map) {
|
|
781
|
+
let d = 0;
|
|
782
|
+
let cur = map.get(layerId);
|
|
783
|
+
while (cur?.parentId) {
|
|
784
|
+
d += 1;
|
|
785
|
+
cur = map.get(cur.parentId);
|
|
786
|
+
if (d > layers.length)
|
|
787
|
+
break;
|
|
788
|
+
}
|
|
789
|
+
return d;
|
|
790
|
+
}
|
|
791
|
+
/** Write an absolute rect back to layer.rect / slot in parent-local space. */
|
|
792
|
+
export function layerFromAbsoluteRect(layer, absRect, parentAbs, parentLayer, rootSize) {
|
|
793
|
+
const pad = parentLayer ? contentPadding(parentLayer) : { left: 0, top: 0, right: 0, bottom: 0 };
|
|
794
|
+
const frame = parentAbs ?? rootFrame(rootSize);
|
|
795
|
+
const originX = frame.x + pad.left;
|
|
796
|
+
const originY = frame.y + pad.top;
|
|
797
|
+
const contentW = Math.max(0, frame.w - pad.left - pad.right);
|
|
798
|
+
const contentH = Math.max(0, frame.h - pad.top - pad.bottom);
|
|
799
|
+
const local = {
|
|
800
|
+
x: absRect.x - originX,
|
|
801
|
+
y: absRect.y - originY,
|
|
802
|
+
w: absRect.w,
|
|
803
|
+
h: absRect.h
|
|
804
|
+
};
|
|
805
|
+
return syncSlotFromRect({ ...layer, rect: local }, { width: contentW, height: contentH });
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Figma-style groups: origin + size is the AABB of direct children.
|
|
809
|
+
* Children keep their absolute positions (local coords are rebased).
|
|
810
|
+
* Nested groups are fitted innermost-first.
|
|
811
|
+
*/
|
|
812
|
+
export function fitGroupsToChildren(layers, rootSize) {
|
|
813
|
+
const byId = new Map(layers.map((l) => [l.id, l]));
|
|
814
|
+
const groups = layers
|
|
815
|
+
.filter((l) => l.kind === 'group')
|
|
816
|
+
.sort((a, b) => ancestorDepth(layers, b.id, byId) - ancestorDepth(layers, a.id, byId));
|
|
817
|
+
if (!groups.length)
|
|
818
|
+
return layers;
|
|
819
|
+
let next = layers;
|
|
820
|
+
for (const group of groups) {
|
|
821
|
+
next = fitOneGroup(next, group.id, rootSize);
|
|
822
|
+
}
|
|
823
|
+
return next;
|
|
824
|
+
}
|
|
825
|
+
function fitOneGroup(layers, groupId, rootSize) {
|
|
826
|
+
const children = getChildren(layers, groupId);
|
|
827
|
+
if (!children.length)
|
|
828
|
+
return layers;
|
|
829
|
+
const absMap = computeAbsoluteRects(layers, rootSize);
|
|
830
|
+
const group = layers.find((l) => l.id === groupId);
|
|
831
|
+
if (!group)
|
|
832
|
+
return layers;
|
|
833
|
+
let minX = Infinity;
|
|
834
|
+
let minY = Infinity;
|
|
835
|
+
let maxX = -Infinity;
|
|
836
|
+
let maxY = -Infinity;
|
|
837
|
+
for (const c of children) {
|
|
838
|
+
const r = absMap.get(c.id);
|
|
839
|
+
if (!r)
|
|
840
|
+
continue;
|
|
841
|
+
minX = Math.min(minX, r.x);
|
|
842
|
+
minY = Math.min(minY, r.y);
|
|
843
|
+
maxX = Math.max(maxX, r.x + r.w);
|
|
844
|
+
maxY = Math.max(maxY, r.y + r.h);
|
|
845
|
+
}
|
|
846
|
+
if (!Number.isFinite(minX))
|
|
847
|
+
return layers;
|
|
848
|
+
const newAbs = {
|
|
849
|
+
x: minX,
|
|
850
|
+
y: minY,
|
|
851
|
+
w: Math.max(1, maxX - minX),
|
|
852
|
+
h: Math.max(1, maxY - minY)
|
|
853
|
+
};
|
|
854
|
+
const oldAbs = absMap.get(groupId) ?? group.rect;
|
|
855
|
+
if (rectsApproxEqual(oldAbs, newAbs))
|
|
856
|
+
return layers;
|
|
857
|
+
const byId = new Map(layers.map((l) => [l.id, l]));
|
|
858
|
+
const parent = group.parentId ? (byId.get(group.parentId) ?? null) : null;
|
|
859
|
+
const parentAbs = group.parentId ? (absMap.get(group.parentId) ?? null) : rootFrame(rootSize);
|
|
860
|
+
const updated = new Map();
|
|
861
|
+
updated.set(groupId, layerFromAbsoluteRect(group, newAbs, parentAbs, parent, rootSize));
|
|
862
|
+
for (const c of children) {
|
|
863
|
+
const r = absMap.get(c.id);
|
|
864
|
+
if (!r)
|
|
865
|
+
continue;
|
|
866
|
+
updated.set(c.id, layerFromAbsoluteRect(c, r, newAbs, group, rootSize));
|
|
867
|
+
}
|
|
868
|
+
return layers.map((l) => updated.get(l.id) ?? l);
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Scale a group (or any subtree) from `prevAbs` to `nextAbs`.
|
|
872
|
+
* Descendants keep their relative placement inside the box.
|
|
873
|
+
*/
|
|
874
|
+
export function scaleSubtreeAbsolute(layers, rootId, prevAbs, nextAbs, rootSize) {
|
|
875
|
+
const sx = prevAbs.w > 0 ? nextAbs.w / prevAbs.w : 1;
|
|
876
|
+
const sy = prevAbs.h > 0 ? nextAbs.h / prevAbs.h : 1;
|
|
877
|
+
const absMap = computeAbsoluteRects(layers, rootSize);
|
|
878
|
+
const ids = new Set(getSubtreeIds(layers, rootId));
|
|
879
|
+
const byId = new Map(layers.map((l) => [l.id, l]));
|
|
880
|
+
const nextAbsMap = new Map();
|
|
881
|
+
for (const id of ids) {
|
|
882
|
+
const r = absMap.get(id);
|
|
883
|
+
if (!r)
|
|
884
|
+
continue;
|
|
885
|
+
if (id === rootId) {
|
|
886
|
+
nextAbsMap.set(id, { ...nextAbs });
|
|
887
|
+
continue;
|
|
888
|
+
}
|
|
889
|
+
nextAbsMap.set(id, {
|
|
890
|
+
x: nextAbs.x + (r.x - prevAbs.x) * sx,
|
|
891
|
+
y: nextAbs.y + (r.y - prevAbs.y) * sy,
|
|
892
|
+
w: Math.max(1, r.w * sx),
|
|
893
|
+
h: Math.max(1, r.h * sy)
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
return layers.map((l) => {
|
|
897
|
+
const abs = nextAbsMap.get(l.id);
|
|
898
|
+
if (!abs)
|
|
899
|
+
return l;
|
|
900
|
+
const parent = l.parentId ? (byId.get(l.parentId) ?? null) : null;
|
|
901
|
+
const parentAbs = l.parentId
|
|
902
|
+
? (nextAbsMap.get(l.parentId) ?? absMap.get(l.parentId) ?? null)
|
|
903
|
+
: rootFrame(rootSize);
|
|
904
|
+
return layerFromAbsoluteRect(l, abs, parentAbs, parent, rootSize);
|
|
905
|
+
});
|
|
906
|
+
}
|