aboutsummaryrefslogtreecommitdiff
path: root/src/lib/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/utils.ts')
-rw-r--r--src/lib/utils.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
new file mode 100644
index 0000000..3678464
--- /dev/null
+++ b/src/lib/utils.ts
@@ -0,0 +1,66 @@
+export function formatDate(unixtimestamp: number) {
+ const months = [
+ 'Jan',
+ 'Feb',
+ 'Mar',
+ 'Apr',
+ 'May',
+ 'Jun',
+ 'Jul',
+ 'Aug',
+ 'Sep',
+ 'Oct',
+ 'Nov',
+ 'Dec'
+ ];
+
+ const date = new Date(unixtimestamp * 1000);
+ const day = date.getDate();
+ const month = months[date.getMonth()];
+ const year = date.getFullYear();
+
+ const formattedDate = `${day} ${month} ${year}`;
+ return formattedDate;
+}
+
+let serial = 0;
+
+export function next(): number {
+ serial++;
+ return serial;
+}
+
+export function scrollTabIntoView(el: string | HTMLElement, wait: boolean) {
+ function scrollTab() {
+ const element =
+ typeof el === 'string' ? document.querySelector(`[id^="wikitab-v0-${el}"]`) : el;
+ if (!element) return;
+
+ element.scrollIntoView({
+ behavior: 'smooth',
+ inline: 'start'
+ });
+ }
+
+ if (wait) {
+ setTimeout(() => {
+ scrollTab();
+ }, 1);
+ } else {
+ scrollTab();
+ }
+}
+
+export function isElementInViewport(el: string | HTMLElement) {
+ const element = typeof el === 'string' ? document.querySelector(`[id^="wikitab-v0-${el}"]`) : el;
+ if (!element) return;
+
+ const rect = element.getBoundingClientRect();
+
+ return (
+ rect.top >= 0 &&
+ rect.left >= 0 &&
+ rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
+ rect.right <= (window.innerWidth || document.documentElement.clientWidth)
+ );
+}