Skip to content

Commit 2b8c8d2

Browse files
author
Attila Cseh
committed
canvas auto naming
1 parent d70c006 commit 2b8c8d2

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

invokeai/frontend/web/src/features/controlLayers/store/canvasSlice.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
CanvasesStateWithoutHistory,
2424
CanvasInpaintMaskState,
2525
CanvasMetadata,
26+
CanvasStateWithHistory,
2627
ChannelName,
2728
ChannelPoints,
2829
ControlLoRAConfig,
@@ -136,7 +137,7 @@ const getInitialCanvasHistoryState = (id: string, name: string): StateWithHistor
136137

137138
const getInitialCanvasesState = (): CanvasesStateWithoutHistory => {
138139
const canvasId = getPrefixedId('canvas');
139-
const canvasName = 'default';
140+
const canvasName = getNextCanvasName([]);
140141
const canvas = getInitialCanvasState(canvasId, canvasName);
141142

142143
return {
@@ -155,6 +156,15 @@ const getInitialCanvasesHistoryState = (): CanvasesStateWithHistory => {
155156
};
156157
};
157158

159+
const getNextCanvasName = (canvases: CanvasStateWithHistory[]): string => {
160+
for (let i = 1; ; i++) {
161+
const name = `Canvas-${i}`;
162+
if (!canvases.some((c) => c.present.name === name)) {
163+
return name;
164+
}
165+
}
166+
};
167+
158168
const canvasesSlice = createSlice({
159169
name: 'canvas',
160170
initialState: getInitialCanvasesHistoryState(),
@@ -163,7 +173,7 @@ const canvasesSlice = createSlice({
163173
reducer: (state, action: PayloadAction<{ id: string; isSelected?: boolean }>) => {
164174
const { id, isSelected } = action.payload;
165175

166-
const name = 'default';
176+
const name = getNextCanvasName(state.canvases);
167177
const canvas = getInitialCanvasHistoryState(id, name);
168178
state.canvases.push(canvas);
169179

invokeai/frontend/web/src/features/controlLayers/store/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ const zCanvasState = z.object({
824824
});
825825
export type CanvasState = z.infer<typeof zCanvasState>;
826826
const zCanvasStateWithHistory = zStateWithHistory(zCanvasState);
827+
export type CanvasStateWithHistory = z.infer<typeof zCanvasStateWithHistory>;
827828
const zCanvasesStateMigration = z.object({
828829
isMultiCanvasMigrationPending: z.boolean().optional(),
829830
});

0 commit comments

Comments
 (0)