Files
image-studio/commands/project.ts

38 lines
1.5 KiB
TypeScript

import type { ImageDocument } from "@core/document";
import { initialEditorState } from "@editor/initial-state";
import type { Command } from "./command";
import { commandIds } from "./ids";
export type ProjectOpenPayload = { document: ImageDocument };
export const projectOpenCommand: Command<ProjectOpenPayload> = {
id: commandIds.projectOpen,
name: "Open project",
history: { mode: "ignore" },
execute({ state }, payload) {
const firstArtboard = payload.document.artboards[0];
const viewport = state.editor.viewport;
const padding = 48;
const availableWidth = Math.max(0, viewport.size.w - padding * 2);
const availableHeight = Math.max(0, viewport.size.h - padding * 2);
const canFit = Boolean(firstArtboard && availableWidth > 0 && availableHeight > 0 && firstArtboard.bounds.w > 0 && firstArtboard.bounds.h > 0);
return {
document: payload.document,
editor: {
...initialEditorState,
viewport: {
...viewport,
center: firstArtboard
? { x: firstArtboard.bounds.x + firstArtboard.bounds.w / 2, y: firstArtboard.bounds.y + firstArtboard.bounds.h / 2 }
: viewport.center,
zoom: canFit && firstArtboard ? Math.max(0.01, Math.min(availableWidth / firstArtboard.bounds.w, availableHeight / firstArtboard.bounds.h)) : viewport.zoom,
},
selection: { artboardId: firstArtboard?.id, layerIds: [] },
},
history: { past: [], future: [] },
};
},
};
export const projectCommands = [projectOpenCommand] satisfies Command<unknown>[];