import { commandIds } from "@commands/ids"; import type { AppStore } from "@editor/store"; import type { BrowserImageFile } from "@platform/browser/imageFiles"; export function importImageAsLayer(store: AppStore, image: BrowserImageFile): boolean { const state = store.getState(); const artboard = state.editor.selection.artboardId ? state.document.artboards.find((candidate) => candidate.id === state.editor.selection.artboardId) : state.document.artboards[0]; if (!artboard) { image.release(); return false; } const assetId = crypto.randomUUID(); const layerId = crypto.randomUUID(); const center = state.editor.viewport.center; store.dispatch(commandIds.documentAddAsset, { asset: { id: assetId, name: image.name, mimeType: image.mimeType, source: image.source, intrinsicSize: image.intrinsicSize } }); store.dispatch(commandIds.documentAddImageLayer, { artboardId: artboard.id, layer: { id: layerId, type: "image", name: image.name, visible: true, locked: false, opacity: 1, assetId, transform: { position: { x: center.x - image.intrinsicSize.w / 2, y: center.y - image.intrinsicSize.h / 2 }, scale: { x: 1, y: 1 }, rotation: 0 }, }, }); store.dispatch(commandIds.selectionSet, { artboardId: artboard.id, layerIds: [layerId] }); return true; }