-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.ts
More file actions
429 lines (372 loc) · 13.2 KB
/
Copy pathutils.ts
File metadata and controls
429 lines (372 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import Sortable from "sortablejs";
import { SourcePrefix } from "./data";
export function dateToText(utcTimestamp: number): string {
const now = Date.now();
const timeDifference = now - utcTimestamp;
const seconds = Math.floor(timeDifference / 1000);
if (seconds < 60) {
return seconds + "s";
}
const minutes = Math.floor(timeDifference / (1000 * 60));
if (minutes < 60) {
return minutes + "m";
}
const hours = Math.floor(timeDifference / (1000 * 60 * 60));
if (hours < 24) {
return hours + "h";
}
const days = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
if (days < 30) {
return days + "d";
}
const months = Math.floor(timeDifference / (1000 * 60 * 60 * 24 * 30));
if (months < 12) {
return months + "mo";
}
const years = Math.floor(timeDifference / (1000 * 60 * 60 * 24 * 365));
return years + "y";
}
export function onVisibleOnce(target: Element, callback: () => void) {
let callbackTriggered = false;
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
callbackTriggered = true;
callback();
observer.unobserve(entry.target);
}
});
},
{
root: null,
rootMargin: "200px",
threshold: 0.01,
}
);
observer.observe(target);
}
export function enableYoutubeJSApi(originalString: string) {
const srcIndex = originalString.indexOf('src="');
if (srcIndex !== -1) {
const closingQuoteIndex = originalString.indexOf('"', srcIndex + 5);
if (closingQuoteIndex !== -1) {
const srcValue = originalString.substring(srcIndex + 5, closingQuoteIndex);
const updatedSrcValue = `${srcValue}&enablejsapi=1`;
const updatedString = originalString.replace(srcValue, updatedSrcValue);
return updatedString;
}
}
return originalString;
}
export function enableYoutubePause(videoElement: HTMLIFrameElement) {
// Pause when out of view
document.addEventListener("scroll", () => {
if (videoElement && !intersectsViewport(videoElement)) {
videoElement.contentWindow?.postMessage('{"event":"command","func":"' + "pauseVideo" + '","args":""}', "*");
}
});
window.addEventListener("overlay-opened", () => {
if (videoElement) {
videoElement.contentWindow?.postMessage('{"event":"command","func":"' + "pauseVideo" + '","args":""}', "*");
}
});
}
export function htmlDecode(input: string) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
export function intersectsViewport(element: Element | null) {
if (element == null) return false;
var rect = element.getBoundingClientRect();
var windowHeight = window.innerHeight || document.documentElement.clientHeight;
var windowWidth = window.innerWidth || document.documentElement.clientWidth;
var verticalVisible = rect.top <= windowHeight && rect.bottom >= 0;
var horizontalVisible = rect.left <= windowWidth && rect.right >= 0;
return verticalVisible && horizontalVisible;
}
export function navigate(hash: string) {
console.log("navigating to " + hash);
window.location.hash = hash;
window.location.reload();
}
export function replaceLastHashFragment(newFragment: string) {
const fragmentIndex = window.location.hash.lastIndexOf("/");
if (fragmentIndex == -1) return "#" + newFragment;
return window.location.hash.substring(fragmentIndex + 1) + newFragment;
}
export function addCommasToNumber(number: number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
export function onTapped(element: HTMLElement, callback: () => void) {
let touchStartY = 0;
element.addEventListener("touchstart", (event) => {
touchStartY = event.touches[0].clientY;
});
element.addEventListener("touchend", (event) => {
if (Math.abs(event.changedTouches[0].clientY - touchStartY) < 16) {
callback();
}
});
}
export function assertNever(x: never) {
throw new Error("Unexpected object: " + x);
}
export function proxyFetch(url: string) {
const baseUrl = window.location.host.includes("localhost") ? "http://localhost:3000/proxy/?url=" : "https://ledit.lol/proxy/?url=";
return fetch(baseUrl + encodeURI(url));
}
export function scrollToAndCenter(element: Element) {
requestAnimationFrame(() => {
element.scrollIntoView({
behavior: "smooth",
block: "center",
});
});
}
export function makeChildrenDraggable(container: HTMLElement, complete: () => void) {
const preventContextMenu = (event: any) => {
event.preventDefault();
event.stopImmediatePropagation();
event.stopPropagation();
};
new Sortable(container, {
chosenClass: "overlay-row-dragged",
ghostClass: "overlay-row-dragged",
delay: 300,
delayOnTouchOnly: true,
onStart: () => {
Array.from(container.children).forEach((el) => {
el.classList.add("no-hover");
el.addEventListener("contextmenu", preventContextMenu);
Array.from(el.querySelectorAll("a")).forEach((el) => el.addEventListener("contextmenu", preventContextMenu));
});
},
onEnd: () => {
Array.from(container.children).forEach((el) => {
el.classList.remove("no-hover");
el.removeEventListener("contextmenu", preventContextMenu);
el.addEventListener("contextmenu", preventContextMenu);
Array.from(el.querySelectorAll("a")).forEach((el) => el.removeEventListener("contextmenu", preventContextMenu));
});
complete();
},
});
}
export function setLinkTargetsToBlank(element: HTMLElement | ShadowRoot) {
if (element instanceof HTMLAnchorElement) {
element.setAttribute("target", "_blank");
}
let links = element.querySelectorAll("a")!;
for (let i = 0; i < links.length; i++) {
let link = links[i];
if (link.hash.trim().length > 0 && new URL(link.href).host == location.host) continue;
link.setAttribute("target", "_blank");
}
}
export function elements<T>(view: HTMLElement): T {
let elements: HTMLElement[] = [];
view.querySelectorAll("[x-id]").forEach((el) => elements.push(el as HTMLElement));
const result = {};
elements.forEach((element) => {
// @ts-ignore
if (result[element.getAttribute("x-id")]) {
console.log(`View - Duplicate element x-id ${element.getAttribute("x-id")} in ${view.localName}`);
}
// @ts-ignore
result[element.getAttribute("x-id")] = element;
});
return result as T;
}
// https://nucleoapp.com/tool/icon-transition
export function animateSvgIcon(i: HTMLElement) {
if (i.classList.contains("js-nc-int-icon-loaded")) return i;
i.classList.add("js-nc-int-icon-loaded");
i.querySelector("svg")?.addEventListener("click", function (n) {
(i.querySelector("svg")?.children[0] as any).classList.toggle("nc-int-icon-state-b");
});
return i;
}
export function firstTextChild(divElement: HTMLElement): HTMLElement | null {
const firstNonWhitespaceSequence = divElement.innerText.trim().match(/\S+/);
if (firstNonWhitespaceSequence) {
const sequence = firstNonWhitespaceSequence[0];
for (const child of Array.from(divElement.children) as HTMLElement[]) {
if (child.innerText.includes(sequence)) {
const childChild = firstTextChild(child);
return childChild ? childChild : child;
}
const foundInChildren = firstTextChild(child);
if (foundInChildren) {
return foundInChildren;
}
}
}
return null;
}
export function waitForMediaLoaded(element: HTMLElement | ShadowRoot, callback: () => void): void {
const mediaElements: HTMLMediaElement[] = Array.from(element.querySelectorAll("img, video"));
let mediaLoaded = 0;
let callbackCalled = false;
const checkAllLoaded = () => {
if (mediaLoaded == mediaElements.length) {
if (!callbackCalled) {
callbackCalled = true;
callback();
}
}
};
mediaElements.forEach((el) => {
const handleImageLoad = () => {
mediaLoaded++;
checkAllLoaded();
};
if ((el as any).complete || el.readyState === 4) {
// Already loaded or errored
handleImageLoad();
} else {
el.addEventListener("load", handleImageLoad);
el.addEventListener("error", handleImageLoad);
}
});
checkAllLoaded();
}
export function isLink(element: HTMLElement | Element | null) {
if (element == null) return false;
element = element as HTMLElement;
if (element.tagName == "A") return true;
let parent = element.parentElement;
while (parent) {
if (parent.tagName == "A") return true;
parent = parent.parentElement;
}
return false;
}
export type Route = { test: (hash: string) => Record<string, string> | null; render: (params: Record<string, string>) => void };
export function route(pattern: string, render: (params: Record<string, string>) => void): Route {
return {
test: (hash: string) => matchHashPattern(hash, pattern),
render,
};
}
function matchHashPattern(urlHash: string, pattern: string): Record<string, string> | null {
const patternParts = pattern.split("/");
const urlHashParts = urlHash.split("/");
if (patternParts.length !== urlHashParts.length) {
return null; // Number of path elements doesn't match
}
const params: Record<string, string> = {};
for (let i = 0; i < patternParts.length; i++) {
const patternPart = patternParts[i];
const hashPart = urlHashParts[i];
if (patternPart.startsWith(":")) {
const paramName = patternPart.slice(1);
params[paramName] = decodeURIComponent(hashPart);
} else if (patternPart !== hashPart) {
return null; // Path element doesn't match
}
}
return params;
}
export function getSourcePrefixFromHash(): string | null {
let hash = location.hash;
if (hash.length == 0) {
return null;
}
let slashIndex = hash.indexOf("/");
if (slashIndex == -1) return null;
return decodeURIComponent(hash.substring(1, slashIndex + 1));
}
export function getFeedFromHash(hash?: string): string {
if (!hash) hash = location.hash;
if (hash.length == 0) {
return "";
}
const prefix = getSourcePrefixFromHash();
if (!prefix) return "";
const afterPrefix = hash.substring(prefix.length + 1);
if (afterPrefix.includes("+")) return afterPrefix;
// if (prefix == "m/") return afterPrefix;
const tokens = afterPrefix.split("/");
if (tokens.length == 0) return "";
return decodeURIComponent(tokens[0]);
}
export function sourcePrefixToLabel(source: SourcePrefix | string) {
if (typeof source == "string" && !source.endsWith("/")) source = source + "/";
const src: SourcePrefix = source as SourcePrefix;
switch (src) {
case "r/":
return "Reddit";
case "hn/":
return "Hackernews";
case "rss/":
return "RSS";
case "yt/":
return "YouTube";
case "m/":
return "Mastodon";
default:
assertNever(src);
}
}
export function sourcePrefixToFeedLabel(source: SourcePrefix | string) {
if (typeof source == "string" && !source.endsWith("/")) source = source + "/";
const src: SourcePrefix = source as SourcePrefix;
switch (src) {
case "r/":
return "Subreddits";
case "hn/":
return "Hackernews";
case "rss/":
return "RSS feeds";
case "yt/":
return "YouTube channels";
case "m/":
return "Mastodon accounts";
default:
assertNever(src);
}
}
export function removeTrailingEmptyParagraphs(htmlString: string | null): string | null {
if (htmlString == null) return null;
const parser = new DOMParser();
const parsedDoc = parser.parseFromString(htmlString, "text/html");
const paragraphs = parsedDoc.querySelectorAll("p");
let lastNonEmptyParagraphIndex = -1;
// Find the index of the last non-empty paragraph
for (let i = paragraphs.length - 1; i >= 0; i--) {
const paragraphText = paragraphs[i].textContent?.trim() || "";
if (paragraphText !== "") {
lastNonEmptyParagraphIndex = i;
break;
}
}
if (lastNonEmptyParagraphIndex >= 0) {
// Remove the empty paragraphs after the last non-empty paragraph
for (let i = paragraphs.length - 1; i > lastNonEmptyParagraphIndex; i--) {
paragraphs[i].parentNode?.removeChild(paragraphs[i]);
}
}
return parsedDoc.body.innerHTML;
}
export function onAddedToDom(element: Element, callback: () => void) {
const checkForInsertion = () => {
if (element.isConnected) {
callback();
} else {
requestAnimationFrame(checkForInsertion);
}
};
checkForInsertion();
}
export function addWindowEventListener<T>(element: Element | undefined, eventName: string, callback: (event: T) => void) {
if (!element) return;
const listener = (e: any) => {
if (!element.isConnected) {
window.removeEventListener(eventName, listener);
} else {
callback(e as T);
}
};
window.addEventListener(eventName, listener);
}