first commit
This commit is contained in:
83
src/store/useAppStore.js
Normal file
83
src/store/useAppStore.js
Normal file
@@ -0,0 +1,83 @@
|
||||
import { create } from 'zustand';
|
||||
|
||||
const emptyCropConfig = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
zoom: 1,
|
||||
width: 0,
|
||||
height: 0,
|
||||
top: 0,
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
cropAreaX: 0,
|
||||
cropAreaY: 0,
|
||||
imageWidth: 0,
|
||||
imageHeight: 0,
|
||||
referenceImageId: null,
|
||||
};
|
||||
|
||||
export const useAppStore = create((set) => ({
|
||||
uploadedImages: [],
|
||||
cropConfig: emptyCropConfig,
|
||||
croppedImages: [],
|
||||
ocrText: '',
|
||||
generatedEpub: null,
|
||||
error: null,
|
||||
setError: (message) => set({ error: message }),
|
||||
clearError: () => set({ error: null }),
|
||||
setUploadedImages: (images) => set({ uploadedImages: images }),
|
||||
updateCropConfig: (config) => set({ cropConfig: { ...config } }),
|
||||
setCroppedImages: (images) =>
|
||||
set((state) => {
|
||||
state.croppedImages.forEach((img) => {
|
||||
if (img.url) URL.revokeObjectURL(img.url);
|
||||
});
|
||||
return { croppedImages: images };
|
||||
}),
|
||||
setOcrText: (text) => set({ ocrText: text }),
|
||||
setGeneratedEpub: (epub) =>
|
||||
set((state) => {
|
||||
if (state.generatedEpub?.url) {
|
||||
URL.revokeObjectURL(state.generatedEpub.url);
|
||||
}
|
||||
return { generatedEpub: epub };
|
||||
}),
|
||||
resetFromStep: (step) =>
|
||||
set((state) => {
|
||||
const draft = {};
|
||||
if (step === 'upload') {
|
||||
draft.cropConfig = emptyCropConfig;
|
||||
state.croppedImages.forEach((img) => img.url && URL.revokeObjectURL(img.url));
|
||||
draft.croppedImages = [];
|
||||
draft.ocrText = '';
|
||||
if (state.generatedEpub?.url) {
|
||||
URL.revokeObjectURL(state.generatedEpub.url);
|
||||
}
|
||||
draft.generatedEpub = null;
|
||||
}
|
||||
if (step === 'crop') {
|
||||
state.croppedImages.forEach((img) => img.url && URL.revokeObjectURL(img.url));
|
||||
draft.croppedImages = [];
|
||||
draft.ocrText = '';
|
||||
if (state.generatedEpub?.url) {
|
||||
URL.revokeObjectURL(state.generatedEpub.url);
|
||||
}
|
||||
draft.generatedEpub = null;
|
||||
}
|
||||
if (step === 'ocr') {
|
||||
draft.ocrText = '';
|
||||
if (state.generatedEpub?.url) {
|
||||
URL.revokeObjectURL(state.generatedEpub.url);
|
||||
}
|
||||
draft.generatedEpub = null;
|
||||
}
|
||||
if (step === 'epub' || step === 'download') {
|
||||
if (state.generatedEpub?.url) {
|
||||
URL.revokeObjectURL(state.generatedEpub.url);
|
||||
}
|
||||
draft.generatedEpub = null;
|
||||
}
|
||||
return draft;
|
||||
}),
|
||||
}));
|
||||
Reference in New Issue
Block a user