refactor: simplification du store de lecteur en supprimant les logs de débogage et en optimisant les getters pour une meilleure lisibilité
This commit is contained in:
parent
5e0fc96cd1
commit
d123166dcb
@@ -12,70 +12,39 @@ export const useReaderStore = defineStore('reader', {
|
||||
isLoading: false,
|
||||
error: null,
|
||||
pages: [],
|
||||
totalPages: 0,
|
||||
_debug: {
|
||||
lastUpdate: Date.now(),
|
||||
lastAction: null
|
||||
}
|
||||
totalPages: 0
|
||||
}),
|
||||
|
||||
getters: {
|
||||
isFirstPage: state => state.currentPage === 0,
|
||||
isLastPage: state => state.currentPage === state.totalPages - 1,
|
||||
currentPageData: state => {
|
||||
const data = state.pages[state.currentPage];
|
||||
console.log('Getting currentPageData:', {
|
||||
currentPage: state.currentPage,
|
||||
hasData: !!data,
|
||||
totalPages: state.totalPages,
|
||||
pagesLength: state.pages.length,
|
||||
lastUpdate: state._debug.lastUpdate,
|
||||
lastAction: state._debug.lastAction
|
||||
});
|
||||
return data;
|
||||
}
|
||||
currentPageData: state => state.pages[state.currentPage]
|
||||
},
|
||||
|
||||
actions: {
|
||||
async loadChapter(chapterId) {
|
||||
console.log('Loading chapter:', chapterId);
|
||||
this.isLoading = true;
|
||||
this.error = null;
|
||||
this._debug.lastAction = 'loadChapter';
|
||||
try {
|
||||
const repository = new ApiChapterRepository();
|
||||
|
||||
// Charger les informations du chapitre
|
||||
console.log('Fetching chapter info...');
|
||||
const chapterData = await repository.getChapter(chapterId);
|
||||
console.log('Chapter data received:', chapterData);
|
||||
this.currentChapter = Chapter.create(chapterData);
|
||||
|
||||
// Charger la liste des pages
|
||||
console.log('Fetching pages info...');
|
||||
const pagesData = await repository.getChapterPages(chapterId);
|
||||
console.log('Pages data received:', pagesData);
|
||||
|
||||
// Initialiser le tableau avec des placeholders
|
||||
this.pages = new Array(pagesData.totalItems).fill(null);
|
||||
this.totalPages = pagesData.totalItems;
|
||||
this._debug.lastUpdate = Date.now();
|
||||
|
||||
console.log('Pages array initialized:', {
|
||||
length: this.pages.length,
|
||||
totalPages: this.totalPages
|
||||
});
|
||||
|
||||
// Charger la première page
|
||||
if (this.totalPages > 0) {
|
||||
console.log('Loading first page...');
|
||||
this.currentPage = 0;
|
||||
await this.loadCurrentPageData();
|
||||
} else {
|
||||
console.warn('No pages available for this chapter');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading chapter:', error);
|
||||
this.error = error.message;
|
||||
} finally {
|
||||
this.isLoading = false;
|
||||
@@ -84,38 +53,24 @@ export const useReaderStore = defineStore('reader', {
|
||||
|
||||
async loadCurrentPageData() {
|
||||
if (!this.currentChapter) {
|
||||
console.error('No current chapter loaded');
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.currentPage < 0 || this.currentPage >= this.totalPages) {
|
||||
console.error('Invalid page index:', this.currentPage);
|
||||
return;
|
||||
}
|
||||
|
||||
const pageNumber = this.currentPage + 1; // Convertir en 1-based pour l'API
|
||||
console.log('Loading page data:', {
|
||||
pageNumber,
|
||||
chapterId: this.currentChapter.id
|
||||
});
|
||||
|
||||
if (this.pages[this.currentPage]?.base64Content) {
|
||||
console.log('Page already loaded, skipping fetch');
|
||||
return;
|
||||
}
|
||||
|
||||
this.isLoading = true;
|
||||
this.error = null;
|
||||
this._debug.lastAction = 'loadCurrentPageData';
|
||||
try {
|
||||
const repository = new ApiChapterRepository();
|
||||
console.log('Fetching page from API...');
|
||||
const pageData = await repository.getChapterPage(this.currentChapter.id, pageNumber);
|
||||
console.log('Page data received:', {
|
||||
hasContent: !!pageData?.base64Content,
|
||||
mimeType: pageData?.mimeType,
|
||||
pageNumber: pageData?.pageNumber
|
||||
});
|
||||
|
||||
// Vérifier que les données sont valides
|
||||
if (!pageData || !pageData.base64Content) {
|
||||
@@ -132,14 +87,7 @@ export const useReaderStore = defineStore('reader', {
|
||||
dimensions: pageData.dimensions
|
||||
};
|
||||
this.pages = newPages;
|
||||
this._debug.lastUpdate = Date.now();
|
||||
|
||||
console.log('Page data updated in store:', {
|
||||
pageIndex: this.currentPage,
|
||||
hasContent: !!this.pages[this.currentPage]?.base64Content
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error loading page:', error);
|
||||
this.error = error.message;
|
||||
} finally {
|
||||
this.isLoading = false;
|
||||
@@ -148,40 +96,28 @@ export const useReaderStore = defineStore('reader', {
|
||||
|
||||
async nextPage() {
|
||||
if (!this.isLastPage) {
|
||||
console.log('Moving to next page');
|
||||
this.currentPage++;
|
||||
this._debug.lastAction = 'nextPage';
|
||||
this._debug.lastUpdate = Date.now();
|
||||
await this.loadCurrentPageData();
|
||||
}
|
||||
},
|
||||
|
||||
async previousPage() {
|
||||
if (!this.isFirstPage) {
|
||||
console.log('Moving to previous page');
|
||||
this.currentPage--;
|
||||
this._debug.lastAction = 'previousPage';
|
||||
this._debug.lastUpdate = Date.now();
|
||||
await this.loadCurrentPageData();
|
||||
}
|
||||
},
|
||||
|
||||
setReadingMode(mode) {
|
||||
this.readingMode = mode;
|
||||
this._debug.lastAction = 'setReadingMode';
|
||||
this._debug.lastUpdate = Date.now();
|
||||
},
|
||||
|
||||
setReadingDirection(direction) {
|
||||
this.readingDirection = direction;
|
||||
this._debug.lastAction = 'setReadingDirection';
|
||||
this._debug.lastUpdate = Date.now();
|
||||
},
|
||||
|
||||
setZoom(level) {
|
||||
this.zoom = level;
|
||||
this._debug.lastAction = 'setZoom';
|
||||
this._debug.lastUpdate = Date.now();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -22,13 +22,7 @@
|
||||
<div class="page-container" :style="{ transform: `scale(${store.zoom})` }">
|
||||
<div v-if="!store.currentPageData" class="error"> Aucune donnée d'image disponible </div>
|
||||
<div v-else-if="!store.currentPageData.base64Content" class="error"> Contenu de l'image manquant </div>
|
||||
<img
|
||||
v-else
|
||||
:src="imageSource"
|
||||
:alt="`Page ${store.currentPage + 1}`"
|
||||
class="page-image"
|
||||
@error="handleImageError"
|
||||
@load="handleImageLoad" />
|
||||
<img v-else :src="imageSource" :alt="`Page ${store.currentPage + 1}`" class="page-image" />
|
||||
</div>
|
||||
|
||||
<div class="reader-settings">
|
||||
@@ -44,26 +38,6 @@
|
||||
<button @click="zoomIn">+</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="debug-info"
|
||||
style="
|
||||
position: fixed;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
background: rgba(0, 0, 0, 0.8);
|
||||
color: white;
|
||||
padding: 10px;
|
||||
font-size: 12px;
|
||||
">
|
||||
currentPage: {{ store.currentPage }}<br />
|
||||
totalPages: {{ store.totalPages }}<br />
|
||||
hasCurrentPageData: {{ !!store.currentPageData }}<br />
|
||||
hasBase64Content: {{ !!store.currentPageData?.base64Content }}<br />
|
||||
mimeType: {{ store.currentPageData?.mimeType }}<br />
|
||||
lastAction: {{ store._debug.lastAction }}<br />
|
||||
lastUpdate: {{ new Date(store._debug.lastUpdate).toLocaleTimeString() }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -83,29 +57,12 @@
|
||||
const store = useReaderStore();
|
||||
|
||||
const imageSource = computed(() => {
|
||||
console.log('Computing imageSource:', {
|
||||
hasCurrentPageData: !!store.currentPageData,
|
||||
mimeType: store.currentPageData?.mimeType,
|
||||
hasContent: !!store.currentPageData?.base64Content
|
||||
});
|
||||
|
||||
if (!store.currentPageData?.base64Content || !store.currentPageData?.mimeType) {
|
||||
console.error("Données d'image invalides:", store.currentPageData);
|
||||
return '';
|
||||
}
|
||||
return `data:${store.currentPageData.mimeType};base64,${store.currentPageData.base64Content}`;
|
||||
});
|
||||
|
||||
const handleImageError = e => {
|
||||
console.error("Erreur de chargement de l'image:", e);
|
||||
console.log("Source de l'image:", imageSource.value);
|
||||
console.log('Données de la page:', store.currentPageData);
|
||||
};
|
||||
|
||||
const handleImageLoad = () => {
|
||||
console.log('Image chargée avec succès');
|
||||
};
|
||||
|
||||
const toggleReadingMode = () => {
|
||||
store.setReadingMode(store.readingMode === 'single' ? 'infinite' : 'single');
|
||||
};
|
||||
@@ -134,7 +91,6 @@
|
||||
() => props.chapterId,
|
||||
newId => {
|
||||
if (newId) {
|
||||
console.log('ChapterId changed, loading new chapter:', newId);
|
||||
store.loadChapter(newId);
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user