From 345f3c41118384b133372c17085b70db37fe94ba Mon Sep 17 00:00:00 2001 From: Ray C Date: Thu, 18 Apr 2024 12:18:11 +0800 Subject: [PATCH] Update util function --- src/app.js | 65 ++++++++++++++++-------------------------------------- 1 file changed, 19 insertions(+), 46 deletions(-) diff --git a/src/app.js b/src/app.js index ea1ac07..f7c682c 100644 --- a/src/app.js +++ b/src/app.js @@ -6,38 +6,14 @@ const { log } = console; const utils = { log, async svg(name) { - const res = await fetch(`./src/components/svg/${name}.html`); - return await res.text(); - }, - scrollToTop() { - scrollTo({ top: 0, behavior: 'smooth' }); - }, - getWindowWidth() { - return window.innerWidth; - }, - isVisible(el) { - return el.getBoundingClientRect().bottom > 0; - }, - toggleClasses(el, cls) { - cls.split(' ').map(cl => el.classList.toggle(cl)); - }, - getScrollProgress() { - const winScroll = - document.body.scrollTop || document.documentElement.scrollTop; - const height = - document.documentElement.scrollHeight - - document.documentElement.clientHeight; - const scrolled = (winScroll / height) * 100; - return Math.round(scrolled) + '%'; - }, - playAudio(audio, volume = 1) { - audio.currentTime = 0; - audio.volume = volume; - audio.play(); + return this.fetchText(`./src/components/svg/${name}.html`); }, async fetchData(api) { return await fetch(api).then(res => res.json()); }, + async fetchText(path) { + return await fetch(path).then(res => res.text()); + }, async delay(delay = 0) { await new Promise(resolve => { setTimeout(resolve, delay * 1000); @@ -54,25 +30,26 @@ const utils = { }); return entries; }, + scrollToTop() { + scrollTo({ top: 0, behavior: 'smooth' }); + }, + getScrollProgress() { + const winScroll = + document.body.scrollTop || document.documentElement.scrollTop; + const height = + document.documentElement.scrollHeight - + document.documentElement.clientHeight; + const scrolled = (winScroll / height) * 100; + return Math.round(scrolled) + '%'; + }, copyText(text) { navigator.clipboard.writeText(text.trim()); }, - getRandomNum(min, max) { - return Math.floor(Math.random() * max) + min; - }, thousandFormat(num) { const parts = num.toString().split('.'); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ','); return parts.join('.'); }, - getRepeatedItem(arr) { - const set = new Set(); - return arr.filter(item => - set.has(JSON.stringify(item)) - ? true - : (set.add(JSON.stringify(item)), false) - ); - }, }; const STORAGE_KEY = `${TITLE.replaceAll(' ', '-')}-hyper-app`; @@ -91,12 +68,8 @@ const localStore = { const router = { routes: [ { path: '/', component: '/home.html' }, - { path: '/docs', component: '/docs.html' }, - { path: '/about/faq', component: '/about/faq.html' }, - { - path: '/about/acknowledgements', - component: '/about/acknowledgements.html', - }, + { path: '/faq', component: '/about/faq.html' }, + { path: '/acknowledgements', component: '/about/acknowledgements.html' }, ], currentPath: '', updatePath() { @@ -106,6 +79,6 @@ const router = { async getPageHTML(path = this.currentPath) { const page = this.routes.find(route => route.path === path)?.component || '/404.html'; - return await fetch(`./src/pages${page}`).then(res => res.text()); + return await utils.fetchText(`./src/pages${page}`); }, };