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
This commit is contained in:
Sped0n
2023-10-30 15:54:36 +08:00
parent f328d727d5
commit ead0a84ac7
2 changed files with 12 additions and 7 deletions

View File

@@ -16,6 +16,7 @@ initState(ijs.length)
initNav() initNav()
if (ijs.length > 0) {
if (!isMobile()) { if (!isMobile()) {
initStage(ijs) initStage(ijs)
initStageNav() initStageNav()
@@ -23,3 +24,4 @@ if (!isMobile()) {
initCollection(ijs) initCollection(ijs)
initGallery(ijs) initGallery(ijs)
} }
}

View File

@@ -9,7 +9,10 @@ export interface ImageJSON {
} }
export function initResources(): 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( return JSON.parse(imagesJson.textContent as string).sort(
(a: ImageJSON, b: ImageJSON) => { (a: ImageJSON, b: ImageJSON) => {
if (a.index < b.index) { if (a.index < b.index) {