feat(commands): add transparent artboard creation

This commit is contained in:
syntaxbullet
2026-07-03 09:52:00 +02:00
parent ca9e265614
commit f4ad35a7f8
5 changed files with 62 additions and 1 deletions

View File

@@ -1,3 +1,4 @@
import { documentCommands } from "@commands/document";
import { createCommandRegistry } from "@commands/registry"; import { createCommandRegistry } from "@commands/registry";
import { selectionCommands } from "@commands/selection"; import { selectionCommands } from "@commands/selection";
import { viewportCommands } from "@commands/viewport"; import { viewportCommands } from "@commands/viewport";
@@ -7,7 +8,7 @@ import { createAppStore } from "@editor/store";
export type ImageStudioApp = ReturnType<typeof createImageStudioApp>; export type ImageStudioApp = ReturnType<typeof createImageStudioApp>;
export function createImageStudioApp(options?: { documentName?: string }) { export function createImageStudioApp(options?: { documentName?: string }) {
const registry = createCommandRegistry([...viewportCommands, ...selectionCommands]); const registry = createCommandRegistry([...viewportCommands, ...selectionCommands, ...documentCommands]);
const store = createAppStore(createInitialAppState(options?.documentName), registry); const store = createAppStore(createInitialAppState(options?.documentName), registry);
return { return {

22
commands/document.test.ts Normal file
View File

@@ -0,0 +1,22 @@
import { describe, expect, test } from "bun:test";
import { createInitialAppState } from "@editor/initial-state";
import { documentAddArtboardCommand } from "./document";
describe("document commands", () => {
test("adds transparent artboard", () => {
const next = documentAddArtboardCommand.execute(
{ state: createInitialAppState("Test") },
{ id: "a1", name: "Artboard 1", bounds: { x: 0, y: 0, w: 320, h: 240 } },
);
expect(next.document.artboards).toEqual([
{
id: "a1",
name: "Artboard 1",
bounds: { x: 0, y: 0, w: 320, h: 240 },
backgroundColor: "transparent",
layers: [],
},
]);
});
});

34
commands/document.ts Normal file
View File

@@ -0,0 +1,34 @@
import type { Rect } from "@core/geometry";
import type { ArtboardId } from "@core/id";
import type { Command } from "./command";
export type DocumentAddArtboardPayload = {
id: ArtboardId;
name: string;
bounds: Rect;
};
export const documentAddArtboardCommand: Command<DocumentAddArtboardPayload> = {
id: "document.addArtboard",
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 documentCommands = [documentAddArtboardCommand] satisfies Command<unknown>[];

View File

@@ -1,4 +1,6 @@
export type { Command, CommandContext } from "./command"; export type { Command, CommandContext } from "./command";
export { documentAddArtboardCommand, documentCommands } from "./document";
export type { DocumentAddArtboardPayload } from "./document";
export type { CommandDispatcher, Dispatch } from "./dispatcher"; export type { CommandDispatcher, Dispatch } from "./dispatcher";
export type { CommandId, CommandPayloads } from "./payloads"; export type { CommandId, CommandPayloads } from "./payloads";
export { createCommandDispatcher } from "./dispatcher"; export { createCommandDispatcher } from "./dispatcher";

View File

@@ -1,3 +1,4 @@
import type { DocumentAddArtboardPayload } from "./document";
import type { SelectionAddLayerPayload, SelectionSetPayload } from "./selection"; import type { SelectionAddLayerPayload, SelectionSetPayload } from "./selection";
import type { import type {
ViewportPanPayload, ViewportPanPayload,
@@ -7,6 +8,7 @@ import type {
} from "./viewport"; } from "./viewport";
export type CommandPayloads = { export type CommandPayloads = {
"document.addArtboard": DocumentAddArtboardPayload;
"selection.set": SelectionSetPayload; "selection.set": SelectionSetPayload;
"selection.clear": void; "selection.clear": void;
"selection.addLayer": SelectionAddLayerPayload; "selection.addLayer": SelectionAddLayerPayload;