-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
712 lines (571 loc) · 21.7 KB
/
index.html
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Chris Shank</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="description" content="Chris Shank's notes." />
<meta name="author" content="Chris Shank" />
<meta property="og:site_name" content="Chris Shank's Notes" />
<!-- Light and dark mode icon -->
<link
rel="icon"
type="image/svg+xml"
sizes="any"
href="data:image/svg+xml,%3Csvg height='128' width='128' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cstyle%3E text %7B fill: %23000000; %7D @media (prefers-color-scheme: dark) %7B text %7B fill: %23ffffff; %7D %7D %3C/style%3E%3Ctext y='.9em' font-size='90'%3E𝕮%3C/text%3E%3C/svg%3E"
/>
<style>
@font-face {
font-family: 'DepatureMono';
font-display: swap;
src: url('./DepartureMono-Regular.woff2') format('woff2');
}
/* https://blog.glyphdrawing.club/font-with-built-in-syntax-highlighting/ */
@font-face {
font-family: 'Monaspace-Krypton';
font-display: swap;
src: url('./Monaspace-Krypton.woff2') format('woff2');
}
code {
font-family: 'Monaspace-Krypton', monospace;
font-feature-settings: 'colr', 'calt';
}
pre {
min-width: 0;
overflow-x: auto;
padding: 2ch;
}
* {
box-sizing: border-box;
}
html {
font-family: 'DepatureMono', serif;
font-optical-sizing: auto;
font-style: normal;
font-size: 22px;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
height: 100%;
}
body {
min-height: 100%;
margin: 0;
position: relative;
}
main {
display: flex;
gap: 10rem;
padding: 1rem;
}
</style>
</head>
<body>
<main>
<article id="home">
<h1>Programming in the ℓittle. ✨</h1>
</article>
</main>
<!-- Script for hash routing. -->
<script type="module">
// function enhanceLinks() {
// document.querySelectorAll('xanadu-link').forEach((link) => link.remove());
// const article = document.querySelector('article[active]');
// if (article === undefined) return;
// article.querySelectorAll('a[href^="#"]').forEach((anchor) => {
// const href = anchor.href;
// const xanaduLink = document.createElement('xanadu-link');
// xanaduLink.source = `#${article.id} a[href="${href}"]`;
// xanaduLink.target = anchor.href;
// });
// }
// function onHashChange() {
// const hash = location.hash.slice(1) || 'home';
// const articles = document.querySelectorAll(`article`);
// articles.forEach((article) => {
// if (article.id === hash) {
// article.setAttribute('active', '');
// } else {
// article.removeAttribute('active');
// }
// });
// }
// window.addEventListener('hashchange', onHashChange);
// onHashChange();
</script>
<!-- Script for persist changes made to HTML file -->
<script type="module">
// Based on https://github.com/jakearchibald/idb-keyval
// class KeyValueStore {
// #db;
// #storeName;
// constructor(name = 'keyval-store') {
// this.#storeName = name;
// const request = indexedDB.open(name);
// request.onupgradeneeded = () => request.result.createObjectStore(name);
// this.#db = new Promise((resolve, reject) => {
// request.onsuccess = () => resolve(request.result);
// request.onerror = () => reject(request.error);
// });
// }
// #promisifyTransaction(transaction) {
// return new Promise((resolve, reject) => {
// transaction.oncomplete = transaction.onsuccess = () => resolve(transaction.result);
// transaction.onabort = transaction.onerror = () => reject(transaction.error);
// });
// }
// #getStore(mode) {
// return this.#db.then((db) =>
// db.transaction(this.#storeName, mode).objectStore(this.#storeName)
// );
// }
// get(key) {
// return this.#getStore('readonly').then((store) =>
// this.#promisifyTransaction(store.get(key))
// );
// }
// set(key, value) {
// return this.#getStore('readwrite').then((store) => {
// store.put(value, key);
// return this.#promisifyTransaction(store.transaction);
// });
// }
// clear() {
// return this.#getStore('readwrite').then((store) => {
// store.clear();
// return this.#promisifyTransaction(store.transaction);
// });
// }
// }
// TODO: progressively-enhance File System API
// Feature detection. The API needs to be supported and the app not run in an iframe.
// const supportsFileSystemAccess =
// 'showSaveFilePicker' in window &&
// (() => {
// try {
// return window.self === window.top;
// } catch {
// return false;
// }
// })();
// class HTMLFileSaver {
// #store = new KeyValueStore('html-file-saver');
// #fileHandler;
// get #file() {
// if (this.#fileHandler === undefined) {
// this.#fileHandler = this.#getFile();
// }
// return this.#fileHandler;
// }
// set #file(file) {
// this.#fileHandler = file;
// }
// async #getFile() {
// const file = await this.#store.get('file');
// // We need to request permission since the file handler was persisted.
// // Calling `queryPermission` seems unnecessary atm since the browser prompts permission for each session
// if (
// (await file?.queryPermission({ mode: 'readwrite' })) !== 'granted' ||
// (await file?.requestPermission({ mode: 'readwrite' })) !== 'granted'
// ) {
// throw new Error('File write permission not granted.');
// }
// return file;
// }
// async save(content, promptNewFile = false) {
// let file = await this.#file;
// if (file === undefined || promptNewFile) {
// let suggestedName = 'index.html';
// // If the web page is a local file, use that name. Otherwise use the name of the
// if (location.protocol === 'file:' && location.pathname.endsWith('.html')) {
// const path = location.pathname;
// suggestedName = path.substring(path.lastIndexOf('/') + 1);
// } else if (file.name.endsWith('.html')) {
// suggestedName = file.name;
// }
// this.#file = showSaveFilePicker({
// id: 'self-modifying_html_file',
// suggestedName,
// types: [{ description: 'HTML document', accept: { 'text/html': ['.html'] } }],
// });
// file = await this.#file;
// await this.#store.set('file', file);
// }
// const writer = await file.createWritable();
// await writer.write(content);
// await writer.close();
// }
// }
// const saver = new HTMLFileSaver();
// window.save = () => saver.save(document.documentElement.innerHTML);
// window.saveAs = () => saver.save(document.documentElement.innerHTML, true);
</script>
<!-- VisualObserver -->
<script type="module">
class VisualObserver {
#root = document.documentElement;
#rootRect = this.#root.getBoundingClientRect();
#entries = [];
#rafId = 0;
#callback;
constructor(callback) {
this.#callback = callback;
}
#elements = new Map();
#resizeObserver = new ResizeObserver((entries) => {
const rootEntry = entries.find((entry) => entry.target === this.#root);
// Any time the root element resizes we need to refresh all the observed elements.
if (rootEntry !== undefined) {
this.#rootRect = rootEntry.contentRect;
this.#elements.forEach((_, target) => {
// Why force a refresh? we really just need to reset the IntersectionObserver?
this.#appendEntry(this.#refreshElement(target));
});
} else {
for (const entry of entries) {
this.#appendEntry(this.#refreshElement(entry.target));
}
}
});
#appendEntry(entry) {
// deduplicate the same target
this.#entries.push(entry);
if (this.#rafId === 0) {
this.#rafId = requestAnimationFrame(this.#flush);
}
}
#flush = () => {
const entries = this.#entries;
this.#entries = [];
this.#rafId = 0;
this.#callback(entries, this);
};
// We should be guaranteed that each `IntersectionObserver` only observes one element.
#onIntersection = ([{ target, intersectionRatio, boundingClientRect }]) => {
const el = this.#elements.get(target);
if (el === undefined) return;
if (intersectionRatio !== el.threshold) {
// It's possible for the watched element to not be at perfect 1.0 visibility when we create
// the IntersectionObserver. This has a couple of causes:
// - elements being on partial pixels
// - elements being hidden offscreen (e.g., <html> has `overflow: hidden`)
// - delays: if your DOM change occurs due to e.g., page resize, you can see elements
// behind their actual position
//
// In all of these cases, refresh but with this lower ratio of threshold. When the element
// moves beneath _that_ new value, the user will get notified.
if (el.isFirstUpdate) {
el.threshold =
intersectionRatio === 0.0
? 0.0000001 // just needs to be non-zero
: intersectionRatio;
}
this.#appendEntry(this.#refreshElement(target, boundingClientRect));
}
el.isFirstUpdate = false;
};
#refreshElement(target, contentRect = target.getBoundingClientRect()) {
// Assume el exists
const el = this.#elements.get(target);
el.io?.disconnect();
el.io = null;
const { left, top, height, width } = contentRect;
// Don't create a IntersectionObserver until the target has a size.
if (width === 0 && height === 0) {
return {
target,
contentRect,
isAppearing: false,
};
}
const root = this.#root;
const floor = Math.floor;
const x = left + root.scrollLeft;
const y = top + root.scrollTop;
// `${insetTop}px ${insetRight}px ${insetBottom}px ${insetLeft}px`;
const rootMargin = `${-floor(y)}px ${-floor(
this.#rootRect.width - (x + width)
)}px ${-floor(this.#rootRect.height - (y + height))}px ${-floor(x)}px`;
// Reset the threshold and isFirstUpdate before creating a new Intersection Observer.
const { threshold } = el;
el.threshold = 1;
el.isFirstUpdate = true;
el.io = new IntersectionObserver(this.#onIntersection, {
root,
rootMargin,
threshold,
});
el.io.observe(target);
return {
target,
contentRect: DOMRectReadOnly.fromRect({
x,
y,
width,
height,
}),
isAppearing: true,
};
}
disconnect() {
this.#elements.forEach((el) => el.io?.disconnect());
this.#elements.clear();
this.#resizeObserver.disconnect();
}
observe(target) {
if (this.#elements.has(target)) return;
if (this.#elements.size === 0) {
this.#resizeObserver.observe(this.#root);
}
this.#elements.set(target, {
io: null,
threshold: 1,
isFirstUpdate: true,
});
// The resize observer will be called immediately, so we don't have to manually refresh.
this.#resizeObserver.observe(target);
}
takeRecords() {
if (this.#rafId === 0) return [];
const entries = this.#entries;
this.#entries = [];
cancelAnimationFrame(this.#rafId);
this.#rafId = 0;
return entries;
}
unobserve(target) {
const el = this.#elements.get(target);
if (el === undefined) return;
this.#resizeObserver.unobserve(target);
el.io?.disconnect();
this.#elements.delete(target);
if (this.#elements.size === 0) {
this.#resizeObserver.disconnect();
}
}
}
class VisualObserverManager {
#elementMap = new WeakMap();
#vo = new VisualObserver((entries) => {
for (const entry of entries) {
const callbacks = this.#elementMap.get(entry.target);
if (callbacks) {
callbacks.forEach((callback) => callback(entry));
}
}
});
observe(target, callback) {
let callbacks = this.#elementMap.get(target);
if (callbacks === undefined) {
this.#vo.observe(target);
this.#elementMap.set(target, (callbacks = new Set()));
} else {
callback({ target, contentRect: target.getBoundingClientRect(), isAppearing: true });
}
callbacks.add(callback);
}
unobserve(target, callback) {
let callbacks = this.#elementMap.get(target);
if (callbacks === undefined) return;
callbacks.delete(callback);
if (callbacks.size === 0) {
this.#vo.unobserve(target);
this.#elementMap.delete(target);
}
}
}
const visualObserver = new VisualObserverManager();
class AbstractArrow extends HTMLElement {
static tagName = 'abstract-arrow';
static register() {
customElements.define(this.tagName, this);
}
static observedAttributes = ['source', 'target'];
#source = '';
/** A CSS selector for the source of the arrow. */
get source() {
return this.#source;
}
set source(source) {
this.setAttribute('source', source);
}
#sourceRect;
#sourceElement = null;
#sourceCallback = (entry) => {
this.#sourceRect = entry.contentRect;
this.update();
};
#target = '';
/** A CSS selector for the target of the arrow. */
get target() {
return this.#target;
}
set target(target) {
this.setAttribute('target', target);
}
#targetRect;
#targetElement = null;
#targetCallback = (entry) => {
this.#targetRect = entry.contentRect;
this.update();
};
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'source') {
this.#source = newValue;
this.observeSource();
} else if (name === 'target') {
this.#target = newValue;
this.observeTarget();
}
}
disconnectedCallback() {
this.unobserveSource();
this.unobserveTarget();
}
observeSource() {
this.unobserveSource();
this.#sourceElement = document.querySelector(this.source);
if (!this.#sourceElement) {
throw new Error('source is not a valid element');
}
visualObserver.observe(this.#sourceElement, this.#sourceCallback);
}
unobserveSource() {
if (this.#sourceElement === null) return;
visualObserver.unobserve(this.#sourceElement, this.#sourceCallback);
}
observeTarget() {
this.unobserveTarget();
this.#targetElement = document.querySelector(this.target);
if (!this.#targetElement) {
throw new Error('target is not a valid element');
}
visualObserver.observe(this.#targetElement, this.#targetCallback);
}
unobserveTarget() {
if (this.#targetElement === null) return;
visualObserver.unobserve(this.#targetElement, this.#targetCallback);
}
update() {
if (
this.#sourceRect === undefined ||
this.#targetRect === undefined ||
this.#sourceElement === null ||
this.#targetElement === null
)
return;
this.render(this.#sourceRect, this.#targetRect, this.#sourceElement, this.#targetElement);
}
render(sourceRect, targetRect, sourceElement, targetElement) {}
}
class XanaduLink extends AbstractArrow {
static tagName = 'xanadu-link';
render(sourceRect, targetRect, sourceElement, targetElement) {
// If the right side of the target is to the left of the right side of the source then swap them
if (sourceRect.x + sourceRect.width > targetRect.x + targetRect.width) {
const temp = sourceElement;
sourceElement = targetElement;
targetElement = temp;
}
let sourceRects = Array.from(sourceElement.getClientRects());
const targetRects = Array.from(targetElement.getClientRects());
if (sourceRects.length === 0 || targetRects.length === 0) {
this.style.clipPath = '';
return;
}
const vertices = [];
if (sourceRects.length === 1) {
const rect = sourceRects[0];
vertices.push(
{ x: rect.right, y: rect.bottom },
{ x: rect.left, y: rect.bottom },
{ x: rect.left, y: rect.top },
{ x: rect.right, y: rect.top }
);
} else {
sourceRects.reverse();
const [lastRect, secondToLastRect] = sourceRects;
if (Math.round(lastRect.right) > Math.round(secondToLastRect.right)) {
vertices.push({ x: lastRect.right, y: lastRect.bottom });
} else {
vertices.push(
{ x: secondToLastRect.right, y: secondToLastRect.bottom },
{ x: lastRect.right, y: lastRect.top },
{ x: lastRect.right, y: lastRect.bottom }
);
}
for (const rect of sourceRects) {
vertices.push({ x: rect.left, y: rect.bottom }, { x: rect.left, y: rect.top });
}
const firstRect = sourceRects.at(-1);
vertices.push({ x: firstRect.right, y: firstRect.top });
}
if (targetRects.length === 1) {
const rect = targetRects[0];
vertices.push(
{ x: rect.left, y: rect.top },
{ x: rect.right, y: rect.top },
{ x: rect.right, y: rect.bottom },
{ x: rect.left, y: rect.bottom }
);
} else {
const [firstRect, secondRect] = targetRects;
if (Math.round(firstRect.left) < Math.round(secondRect.left)) {
vertices.push({ x: firstRect.left, y: firstRect.top });
} else {
vertices.push(
{ x: secondRect.left, y: secondRect.top },
{ x: firstRect.left, y: firstRect.bottom },
{ x: firstRect.left, y: firstRect.top }
);
}
for (const rect of targetRects) {
vertices.push({ x: rect.right, y: rect.top }, { x: rect.right, y: rect.bottom });
}
const lastRect = targetRects.at(-1);
vertices.push({ x: lastRect.left, y: lastRect.bottom });
}
this.style.clipPath = this.verticesToPolygon(vertices);
}
verticesToPolygon(vertices) {
if (vertices.length === 0) return '';
return `polygon(${vertices.map((vertex) => `${vertex.x}px ${vertex.y}px`).join(', ')})`;
}
}
XanaduLink.register();
</script>
<!-- Script for adding content -->
<!-- <script type="module">
const noop = (e) => {
e.preventDefault();
e.stopPropagation();
};
const toBase64 = (blob) =>
new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
const content = document.getElementById('content');
const textArea = document.getElementById('text');
const dropArea = document.getElementById('drop');
dropArea.addEventListener('dragenter', (e) => {
noop(e);
dropArea.style.border = '1px dotted black';
});
dropArea.addEventListener('drop', async (e) => {
noop(e);
dropArea.style.border = '1px dashed black';
const img = document.createElement('img');
const f = e.dataTransfer.files[0];
const b64 = await toBase64(f);
img.src = b64;
content.appendChild(img);
});
dropArea.addEventListener('dragover', noop);
dropArea.addEventListener('dragexit', noop);
window.append = () => {
content.innerHTML += textArea.value;
};
</script> -->
</body>
</html>