feat(view): add contextual layer controls
This commit is contained in:
43
view/selectionSummary.ts
Normal file
43
view/selectionSummary.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import type { ImageDocument } from "@core/document";
|
||||
import type { Layer } from "@core/layer";
|
||||
import type { SelectionState } from "@editor/state";
|
||||
|
||||
export type SelectionSummary =
|
||||
| { type: "none" }
|
||||
| { type: "artboard"; name: string; layerCount: number }
|
||||
| { type: "layer"; name: string; layer: Layer }
|
||||
| { type: "multi-layer"; count: number; layers: Layer[] };
|
||||
|
||||
export function getSelectionSummary(document: ImageDocument, selection: SelectionState): SelectionSummary {
|
||||
const selectedLayers = selection.layerIds.flatMap((layerId) => {
|
||||
const layer = findLayer(document.artboards.flatMap((artboard) => artboard.layers), layerId);
|
||||
return layer ? [layer] : [];
|
||||
});
|
||||
|
||||
if (selectedLayers.length === 1 && selectedLayers[0]) {
|
||||
return { type: "layer", name: selectedLayers[0].name, layer: selectedLayers[0] };
|
||||
}
|
||||
|
||||
if (selectedLayers.length > 1) {
|
||||
return { type: "multi-layer", count: selectedLayers.length, layers: selectedLayers };
|
||||
}
|
||||
|
||||
if (selection.artboardId) {
|
||||
const artboard = document.artboards.find((candidate) => candidate.id === selection.artboardId);
|
||||
if (artboard) return { type: "artboard", name: artboard.name, layerCount: artboard.layers.length };
|
||||
}
|
||||
|
||||
return { type: "none" };
|
||||
}
|
||||
|
||||
function findLayer(layers: Layer[], layerId: string): Layer | undefined {
|
||||
for (const layer of layers) {
|
||||
if (layer.id === layerId) return layer;
|
||||
if (layer.type === "group") {
|
||||
const child = findLayer(layer.children, layerId);
|
||||
if (child) return child;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user