Files
image-studio/commands/document.ts
2026-07-03 16:18:40 +02:00

105 lines
2.6 KiB
TypeScript

import type { Asset } from "@core/asset";
import type { ImageLayer } from "@core/image-layer";
import type { Rect } from "@core/geometry";
import type { ArtboardId } from "@core/id";
import type { Command } from "./command";
import { commandIds } from "./ids";
export type DocumentAddArtboardPayload = {
id: ArtboardId;
name: string;
bounds: Rect;
};
export type DocumentSetArtboardBoundsPayload = {
id: ArtboardId;
bounds: Rect;
};
export type DocumentAddAssetPayload = {
asset: Asset;
};
export type DocumentAddImageLayerPayload = {
artboardId: ArtboardId;
layer: ImageLayer;
};
export const documentAddArtboardCommand: Command<DocumentAddArtboardPayload> = {
id: commandIds.documentAddArtboard,
name: "Add artboard",
execute({ state }, payload) {
return {
...state,
document: {
...state.document,
artboards: [
...state.document.artboards,
{
id: payload.id,
name: payload.name,
bounds: payload.bounds,
backgroundColor: "transparent",
layers: [],
},
],
},
};
},
};
export const documentSetArtboardBoundsCommand: Command<DocumentSetArtboardBoundsPayload> = {
id: commandIds.documentSetArtboardBounds,
name: "Set artboard bounds",
execute({ state }, payload) {
return {
...state,
document: {
...state.document,
artboards: state.document.artboards.map((artboard) =>
artboard.id === payload.id ? { ...artboard, bounds: { ...payload.bounds } } : artboard,
),
},
};
},
};
export const documentAddAssetCommand: Command<DocumentAddAssetPayload> = {
id: commandIds.documentAddAsset,
name: "Add asset",
execute({ state }, payload) {
if (state.document.assets.some((asset) => asset.id === payload.asset.id)) return state;
return {
...state,
document: {
...state.document,
assets: [...state.document.assets, payload.asset],
},
};
},
};
export const documentAddImageLayerCommand: Command<DocumentAddImageLayerPayload> = {
id: commandIds.documentAddImageLayer,
name: "Add image layer",
execute({ state }, payload) {
return {
...state,
document: {
...state.document,
artboards: state.document.artboards.map((artboard) =>
artboard.id === payload.artboardId ? { ...artboard, layers: [...artboard.layers, payload.layer] } : artboard,
),
},
};
},
};
export const documentCommands = [
documentAddArtboardCommand,
documentSetArtboardBoundsCommand,
documentAddAssetCommand,
documentAddImageLayerCommand,
] satisfies Command<unknown>[];