From ead0a84ac78e1de26564f84131416a4e5db67854 Mon Sep 17 00:00:00 2001 From: Sped0n Date: Mon, 30 Oct 2023 15:54:36 +0800 Subject: [PATCH] refactor(main.ts): improve code readability by adding conditional check for ijs length before initializing stage or collection fix(resources.ts): handle case when imagesJson element is not found to prevent error and return empty array --- assets/ts/main.ts | 14 ++++++++------ assets/ts/resources.ts | 5 ++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/assets/ts/main.ts b/assets/ts/main.ts index 5e82b59..c2d0a53 100644 --- a/assets/ts/main.ts +++ b/assets/ts/main.ts @@ -16,10 +16,12 @@ initState(ijs.length) initNav() -if (!isMobile()) { - initStage(ijs) - initStageNav() -} else { - initCollection(ijs) - initGallery(ijs) +if (ijs.length > 0) { + if (!isMobile()) { + initStage(ijs) + initStageNav() + } else { + initCollection(ijs) + initGallery(ijs) + } } diff --git a/assets/ts/resources.ts b/assets/ts/resources.ts index 4eb0b06..57c87f4 100644 --- a/assets/ts/resources.ts +++ b/assets/ts/resources.ts @@ -9,7 +9,10 @@ export interface ImageJSON { } export function initResources(): ImageJSON[] { - const imagesJson = document.getElementById('imagesSource') as HTMLScriptElement + const imagesJson = document.getElementById('imagesSource') + if (!imagesJson) { + return [] + } return JSON.parse(imagesJson.textContent as string).sort( (a: ImageJSON, b: ImageJSON) => { if (a.index < b.index) {