From f3d3efb4ffe17af02164d4334fcfd5b0c6376116 Mon Sep 17 00:00:00 2001 From: Spedon Date: Tue, 14 Mar 2023 01:19:01 +0800 Subject: [PATCH] move json data fetching to a standalone module --- assets/ts/dataFetch.ts | 19 +++++++++++++++++++ assets/ts/main.ts | 3 ++- assets/ts/trackMouse.ts | 21 +-------------------- 3 files changed, 22 insertions(+), 21 deletions(-) create mode 100644 assets/ts/dataFetch.ts diff --git a/assets/ts/dataFetch.ts b/assets/ts/dataFetch.ts new file mode 100644 index 0000000..81adbc7 --- /dev/null +++ b/assets/ts/dataFetch.ts @@ -0,0 +1,19 @@ +interface ImageData { + index: string + url: string + imgH: string + imgW: string +} + +// fetch images info from JSON +const imageArrayElement = document.getElementById('images_array') as HTMLScriptElement +const rawImageArray = imageArrayElement.textContent as string +export const imagesArray: ImageData[] = JSON.parse(rawImageArray).sort( + (a: ImageData, b: ImageData) => { + if (a.index < b.index) { + return -1 + } + return 1 + } +) +export const imagesArrayLen: number = imagesArray.length diff --git a/assets/ts/main.ts b/assets/ts/main.ts index 1d5297d..dff1f1d 100644 --- a/assets/ts/main.ts +++ b/assets/ts/main.ts @@ -1,7 +1,8 @@ import { footerHeightUpdateInit } from './utils' import { imgIndexSpanUpdate } from './indexDisp' -import { imagesArrayLen, trackMouseInit } from './trackMouse' +import { trackMouseInit } from './trackMouse' import { thresholdCtlInit } from './thresholdCtl' +import { imagesArrayLen } from './dataFetch' function init(): void { footerHeightUpdateInit() diff --git a/assets/ts/trackMouse.ts b/assets/ts/trackMouse.ts index ca29c53..d37bbf4 100644 --- a/assets/ts/trackMouse.ts +++ b/assets/ts/trackMouse.ts @@ -2,32 +2,13 @@ import { overlayEnable } from './overlay' import { posCache, FIFO, layersPosSet, center } from './utils' import { thresholdSensitivityArray, thresholdIndex } from './thresholdCtl' import { imgIndexSpanUpdate } from './indexDisp' - -interface ImageData { - index: string - url: string - imgH: string - imgW: string -} +import { imagesArrayLen, imagesArray } from './dataFetch' export interface position { x: number y: number } -// get images info from JSON -const imageArrayElement = document.getElementById('images_array') as HTMLElement -const rawImageArray = imageArrayElement.textContent as string -export const imagesArray: ImageData[] = JSON.parse(rawImageArray).sort( - (a: ImageData, b: ImageData) => { - if (a.index < b.index) { - return -1 - } - return 1 - } -) -export const imagesArrayLen: number = imagesArray.length - // get layer divs const layer5 = document.getElementById('layer5') as HTMLDivElement const layer4 = document.getElementById('layer4') as HTMLDivElement