44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
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;
|
|
}
|