Skip to content

Commit

Permalink
Update util function
Browse files Browse the repository at this point in the history
  • Loading branch information
rayc2045 committed Apr 18, 2024
1 parent 8405d9a commit 345f3c4
Showing 1 changed file with 19 additions and 46 deletions.
65 changes: 19 additions & 46 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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`;
Expand All @@ -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() {
Expand All @@ -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}`);
},
};

0 comments on commit 345f3c4

Please sign in to comment.