@dloizides/marketing-astro-kit 1.12.0 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +46 -0
- package/package.json +13 -6
- package/src/ContactCard.astro +107 -0
- package/src/contactCard/CardActions.astro +20 -0
- package/src/contactCard/CardBrand.astro +25 -0
- package/src/contactCard/CardContacts.astro +22 -0
- package/src/contactCard/CardContactsVariant.ts +5 -0
- package/src/contactCard/CardDialog.astro +16 -0
- package/src/contactCard/CardIcon.astro +12 -0
- package/src/contactCard/CardIconName.ts +6 -0
- package/src/contactCard/CardIdentity.astro +14 -0
- package/src/contactCard/CardLinkKind.ts +6 -0
- package/src/contactCard/CardPortrait.astro +14 -0
- package/src/contactCard/CardQr.astro +24 -0
- package/src/contactCard/CardSave.astro +43 -0
- package/src/contactCard/CardValues.astro +12 -0
- package/src/contactCard/ContactCardLayout.ts +7 -0
- package/src/contactCard/ContactPhotoType.ts +5 -0
- package/src/contactCard/InstallRoute.ts +6 -0
- package/src/contactCard/cardIcons.ts +11 -0
- package/src/contactCard/cardLinks.ts +43 -0
- package/src/contactCard/checks.ts +28 -0
- package/src/contactCard/chooseInstall.ts +21 -0
- package/src/contactCard/contactCard.css +69 -0
- package/src/contactCard/index.ts +16 -0
- package/src/contactCard/layouts.css +106 -0
- package/src/contactCard/manifest.ts +37 -0
- package/src/contactCard/motion/OpenMotion.ts +7 -0
- package/src/contactCard/motion/QrMotion.ts +7 -0
- package/src/contactCard/motion/SaveMotion.ts +7 -0
- package/src/contactCard/motion/TiltMotion.ts +7 -0
- package/src/contactCard/motion/beatTable.ts +144 -0
- package/src/contactCard/motion/beats.ts +43 -0
- package/src/contactCard/motion/cardMotion.ts +49 -0
- package/src/contactCard/motion/easings.ts +9 -0
- package/src/contactCard/motion/holdDuringOpen.ts +22 -0
- package/src/contactCard/motion/mountMotion.ts +26 -0
- package/src/contactCard/motion/mountOpenMark.ts +20 -0
- package/src/contactCard/motion/mountQrTurn.ts +86 -0
- package/src/contactCard/motion/mountSave.ts +26 -0
- package/src/contactCard/motion/mountTilt.ts +92 -0
- package/src/contactCard/motion/openCss.ts +18 -0
- package/src/contactCard/motion/parts.ts +25 -0
- package/src/contactCard/motion/playBeats.ts +21 -0
- package/src/contactCard/motion/sides.ts +33 -0
- package/src/contactCard/motion/tiltPose.ts +38 -0
- package/src/contactCard/motion.css +39 -0
- package/src/contactCard/mount.ts +76 -0
- package/src/contactCard/phone.ts +11 -0
- package/src/contactCard/qrSvg.ts +54 -0
- package/src/contactCard/types.ts +47 -0
- package/src/contactCard/vCard.ts +48 -0
- package/src/contactCard/vCardLines.ts +33 -0
- package/src/motionController/createMotionController.ts +11 -9
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { beatsFor, splitBeats, stillBeats, type Beat } from './beats';
|
|
2
|
+
import { cancelAll, playBeats } from './playBeats';
|
|
3
|
+
import { partSelector } from './parts';
|
|
4
|
+
import { QrMotion } from './QrMotion';
|
|
5
|
+
import { cover, setSide, shareOrigin, sidesAround } from './sides';
|
|
6
|
+
|
|
7
|
+
const GAP_PX = 12;
|
|
8
|
+
|
|
9
|
+
interface Turn {
|
|
10
|
+
card: HTMLElement;
|
|
11
|
+
button: HTMLElement;
|
|
12
|
+
qrSide: HTMLElement;
|
|
13
|
+
fronts: HTMLElement[];
|
|
14
|
+
close: HTMLElement | null;
|
|
15
|
+
turnsCard: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function findTurn(card: HTMLElement, qrMotion: QrMotion): Turn | null {
|
|
19
|
+
const button = card.querySelector<HTMLElement>('[data-card-qr]');
|
|
20
|
+
const save = card.querySelector<HTMLElement>('[data-part="save"]');
|
|
21
|
+
const turnsCard = qrMotion === QrMotion.TurnOver || qrMotion === QrMotion.CubeTurn;
|
|
22
|
+
const qrSide = card.querySelector<HTMLElement>(turnsCard ? '[data-part="qr-back"]' : '[data-part="qr-panel"]');
|
|
23
|
+
const list = card.querySelector<HTMLElement>('[data-part="contacts"]');
|
|
24
|
+
if (button === null || qrSide === null || save === null || list === null) return null;
|
|
25
|
+
const fronts = turnsCard ? sidesAround(card, save) : [list];
|
|
26
|
+
return { card, button, qrSide, fronts, close: card.querySelector<HTMLElement>('[data-card-qr-close]'), turnsCard };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function place(turn: Turn): void {
|
|
30
|
+
const save = turn.card.querySelector<HTMLElement>('[data-part="save"]')?.getBoundingClientRect();
|
|
31
|
+
if (!turn.turnsCard) {
|
|
32
|
+
const list = turn.fronts[0]?.getBoundingClientRect();
|
|
33
|
+
const comb = turn.card.querySelector('[data-part="comb"]')?.getBoundingClientRect();
|
|
34
|
+
const box = list !== undefined && list.height < 1 && comb !== undefined ? comb : list;
|
|
35
|
+
if (box !== undefined) cover(turn.card, turn.qrSide, box);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
shareOrigin(turn.card, turn.fronts);
|
|
39
|
+
const frame = turn.card.getBoundingClientRect();
|
|
40
|
+
if (save === undefined) return;
|
|
41
|
+
const top = save.bottom + GAP_PX;
|
|
42
|
+
cover(turn.card, turn.qrSide, new DOMRect(save.left, top, save.width, Math.max(frame.bottom - top - GAP_PX, 0)));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function finder(turn: Turn): (part: string) => Element[] {
|
|
46
|
+
return (part) => (part === 'card' ? turn.fronts : Array.from(turn.card.querySelectorAll(partSelector(part))));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function labelState(turn: Turn, open: boolean): void {
|
|
50
|
+
turn.button.setAttribute('aria-expanded', String(open));
|
|
51
|
+
turn.card.querySelector('[data-part="qr-open"]')?.toggleAttribute('aria-hidden', open);
|
|
52
|
+
turn.card.querySelector('[data-part="qr-close"]')?.toggleAttribute('aria-hidden', !open);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Wires Show my QR to turn the card or its list to the QR, reversing from where it is on a tap mid-turn. */
|
|
56
|
+
export function mountQrTurn(card: HTMLElement, qrMotion: QrMotion, reduced: boolean, signal?: AbortSignal): void {
|
|
57
|
+
const turn = findTurn(card, qrMotion);
|
|
58
|
+
if (turn === null) return;
|
|
59
|
+
const still = reduced || qrMotion === QrMotion.None;
|
|
60
|
+
const source = beatsFor(qrMotion === QrMotion.None ? QrMotion.ListTurns : qrMotion);
|
|
61
|
+
const { out, back } = splitBeats(still ? stillBeats(source) : source);
|
|
62
|
+
let open = false;
|
|
63
|
+
let playing: Animation[] = [];
|
|
64
|
+
const settle = (): void => {
|
|
65
|
+
setSide(turn.qrSide, !open);
|
|
66
|
+
turn.fronts.forEach((front) => setSide(front, open));
|
|
67
|
+
(open && turn.close !== null ? turn.close : turn.button).focus();
|
|
68
|
+
};
|
|
69
|
+
const toggle = (): void => {
|
|
70
|
+
open = !open;
|
|
71
|
+
labelState(turn, open);
|
|
72
|
+
if (playing.some((animation) => animation.playState === 'running')) {
|
|
73
|
+
playing.forEach((animation) => animation.reverse());
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
cancelAll(playing);
|
|
77
|
+
place(turn);
|
|
78
|
+
[turn.qrSide, ...turn.fronts].forEach((side) => setSide(side, false));
|
|
79
|
+
const beats: Beat[] = open ? out : back;
|
|
80
|
+
playing = playBeats(beats, finder(turn));
|
|
81
|
+
Promise.all(playing.map((animation) => animation.finished)).then(settle, () => undefined);
|
|
82
|
+
};
|
|
83
|
+
turn.button.addEventListener('click', toggle, { signal });
|
|
84
|
+
turn.close?.addEventListener('click', () => { if (open) toggle(); }, { signal });
|
|
85
|
+
card.addEventListener('keydown', (event) => { if (event.key === 'Escape' && open) toggle(); }, { signal });
|
|
86
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { RETURN_AT_MS, beatsFor, stillBeats } from './beats';
|
|
2
|
+
import { cancelAll, playBeats } from './playBeats';
|
|
3
|
+
import { partSelector } from './parts';
|
|
4
|
+
import { SaveMotion } from './SaveMotion';
|
|
5
|
+
|
|
6
|
+
/** Plays the Save contact confirmation on each tap and announces the ready words; never holds back the link. */
|
|
7
|
+
export function mountSave(card: HTMLElement, saveMotion: SaveMotion, reduced: boolean, signal?: AbortSignal): void {
|
|
8
|
+
const save = card.querySelector<HTMLElement>('[data-part="save"]');
|
|
9
|
+
const live = card.querySelector<HTMLElement>('[data-part="save-live"]');
|
|
10
|
+
if (save === null || live === null) return;
|
|
11
|
+
const still = reduced || saveMotion === SaveMotion.None;
|
|
12
|
+
const source = beatsFor(saveMotion === SaveMotion.None ? SaveMotion.Tick : saveMotion);
|
|
13
|
+
const beats = still ? stillBeats(source) : source;
|
|
14
|
+
const ready = card.querySelector('[data-part="ready-text"]')?.textContent ?? '';
|
|
15
|
+
const find = (part: string): Element[] => Array.from(card.querySelectorAll(partSelector(part)));
|
|
16
|
+
let playing: Animation[] = [];
|
|
17
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
18
|
+
save.addEventListener('click', () => {
|
|
19
|
+
cancelAll(playing);
|
|
20
|
+
playing = playBeats(beats, find);
|
|
21
|
+
live.textContent = ready;
|
|
22
|
+
clearTimeout(timer);
|
|
23
|
+
timer = setTimeout(() => { live.textContent = ''; }, RETURN_AT_MS);
|
|
24
|
+
}, { signal });
|
|
25
|
+
signal?.addEventListener('abort', () => clearTimeout(timer), { once: true });
|
|
26
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { MotionState, createMotionController } from '../../motionController';
|
|
2
|
+
import { EASINGS } from './easings';
|
|
3
|
+
import { partSelector } from './parts';
|
|
4
|
+
import { TiltMotion } from './TiltMotion';
|
|
5
|
+
import { dragToUnit, gammaToUnit, tiltPose } from './tiltPose';
|
|
6
|
+
|
|
7
|
+
const START_PX = 8;
|
|
8
|
+
const RETURN_MS = 600;
|
|
9
|
+
const SENSOR_SHOWS_LIGHT = 0.1;
|
|
10
|
+
const TILTING = 'data-tilting';
|
|
11
|
+
|
|
12
|
+
interface OrientationGlobal {
|
|
13
|
+
DeviceOrientationEvent?: { requestPermission?: unknown };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function sensorAllowed(): boolean {
|
|
17
|
+
const orientation = (window as OrientationGlobal).DeviceOrientationEvent;
|
|
18
|
+
return orientation !== undefined && orientation.requestPermission === undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function partFinder(card: HTMLElement): (part: string) => HTMLElement[] {
|
|
22
|
+
return (part) => (part === 'card' ? [card] : Array.from(card.querySelectorAll<HTMLElement>(partSelector(part))));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function swallowNextClick(card: HTMLElement, signal?: AbortSignal): void {
|
|
26
|
+
const stopClick = (event: Event): void => {
|
|
27
|
+
event.preventDefault();
|
|
28
|
+
event.stopPropagation();
|
|
29
|
+
};
|
|
30
|
+
card.addEventListener('click', stopClick, { capture: true, once: true, signal });
|
|
31
|
+
setTimeout(() => card.removeEventListener('click', stopClick, { capture: true }), 0);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function easeBack(find: (part: string) => HTMLElement[], tiltMotion: TiltMotion): void {
|
|
35
|
+
const restPose = tiltPose(tiltMotion, 0);
|
|
36
|
+
Object.entries(restPose).forEach(([part, transform]) => find(part).forEach((element) => {
|
|
37
|
+
const from = element.style.transform !== '' ? element.style.transform : transform;
|
|
38
|
+
element.animate?.([{ transform: from }, { transform }], { duration: RETURN_MS, easing: EASINGS['settle'] });
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Leans the card with a sideways drag or a permission-free motion sensor; still under reduced motion. */
|
|
43
|
+
export function mountTilt(card: HTMLElement, tiltMotion: TiltMotion, signal?: AbortSignal): void {
|
|
44
|
+
if (tiltMotion === TiltMotion.None) return;
|
|
45
|
+
const find = partFinder(card);
|
|
46
|
+
const moving = Object.keys(tiltPose(tiltMotion, 1)).flatMap(find);
|
|
47
|
+
const drag = { unit: 0, pointer: null as number | null, startX: 0, active: false, sensing: false, gammaAtLoad: null as number | null };
|
|
48
|
+
const apply = (): void => Object.entries(tiltPose(tiltMotion, drag.unit)).forEach(([part, transform]) => find(part).forEach((element) => { element.style.transform = transform; }));
|
|
49
|
+
const rest = (): void => {
|
|
50
|
+
Object.assign(drag, { unit: 0, active: false, sensing: false });
|
|
51
|
+
moving.forEach((element) => { element.style.transform = ''; });
|
|
52
|
+
card.removeAttribute(TILTING);
|
|
53
|
+
};
|
|
54
|
+
const controller = createMotionController({ target: card, onFrame: apply, onStill: rest, canRun: () => drag.active || drag.sensing, signal });
|
|
55
|
+
const settle = (): void => { rest(); controller.sync(); };
|
|
56
|
+
const running = (): boolean => controller.getState() === MotionState.Running;
|
|
57
|
+
card.addEventListener('pointerdown', (event) => {
|
|
58
|
+
if (drag.pointer !== null) return;
|
|
59
|
+
drag.pointer = event.pointerId;
|
|
60
|
+
drag.startX = event.clientX;
|
|
61
|
+
}, { signal });
|
|
62
|
+
card.addEventListener('pointermove', (event) => {
|
|
63
|
+
if (event.pointerId !== drag.pointer) return;
|
|
64
|
+
const dx = event.clientX - drag.startX;
|
|
65
|
+
if (!drag.active && Math.abs(dx) > START_PX && running()) {
|
|
66
|
+
drag.active = true;
|
|
67
|
+
card.setAttribute(TILTING, '');
|
|
68
|
+
controller.sync();
|
|
69
|
+
}
|
|
70
|
+
if (drag.active) drag.unit = dragToUnit(dx);
|
|
71
|
+
}, { signal });
|
|
72
|
+
const end = (event: PointerEvent): void => {
|
|
73
|
+
if (event.pointerId !== drag.pointer) return;
|
|
74
|
+
drag.pointer = null;
|
|
75
|
+
if (!drag.active) return;
|
|
76
|
+
swallowNextClick(card, signal);
|
|
77
|
+
easeBack(find, tiltMotion);
|
|
78
|
+
settle();
|
|
79
|
+
};
|
|
80
|
+
signal?.addEventListener('abort', settle, { once: true });
|
|
81
|
+
card.addEventListener('pointerup', end, { signal });
|
|
82
|
+
card.addEventListener('pointercancel', end, { signal });
|
|
83
|
+
if (!sensorAllowed()) return;
|
|
84
|
+
window.addEventListener('deviceorientation', (event) => {
|
|
85
|
+
if (event.gamma === null || drag.active || !running()) return;
|
|
86
|
+
drag.gammaAtLoad ??= event.gamma;
|
|
87
|
+
drag.unit = gammaToUnit(event.gamma, drag.gammaAtLoad);
|
|
88
|
+
drag.sensing = true;
|
|
89
|
+
card.toggleAttribute(TILTING, Math.abs(drag.unit) > SENSOR_SHOWS_LIGHT);
|
|
90
|
+
controller.sync();
|
|
91
|
+
}, { signal });
|
|
92
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { beatsFor } from './beats';
|
|
2
|
+
import { partSelector } from './parts';
|
|
3
|
+
|
|
4
|
+
/** The opening as CSS animations scoped to the option, played only when motion is allowed; `''` for `none`. */
|
|
5
|
+
export function buildOpenCss(openMotion: string): string {
|
|
6
|
+
const beats = beatsFor(openMotion);
|
|
7
|
+
if (beats.length === 0) return '';
|
|
8
|
+
const scope = `.tmk-card[data-open-motion="${openMotion}"]`;
|
|
9
|
+
const byPart = new Map<string, string[]>();
|
|
10
|
+
const frames = beats.map((beat, index) => {
|
|
11
|
+
const name = `tmk-card-${openMotion}-${index}`;
|
|
12
|
+
const selector = partSelector(beat.part);
|
|
13
|
+
byPart.set(selector, [...(byPart.get(selector) ?? []), `${name} ${beat.lengthMs}ms ${beat.easing} ${beat.atMs}ms backwards`]);
|
|
14
|
+
return `@keyframes ${name}{from{${beat.property}:${beat.from}}to{${beat.property}:${beat.to}}}`;
|
|
15
|
+
});
|
|
16
|
+
const rules = Array.from(byPart, ([selector, animations]) => `${scope} ${selector}{animation:${animations.join(',')}}`);
|
|
17
|
+
return `@media (prefers-reduced-motion:no-preference){${frames.join('')}${rules.join('')}}`;
|
|
18
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const CONTACTS = '[data-part="contacts"]';
|
|
2
|
+
const SPECIAL: Readonly<Record<string, string>> = {
|
|
3
|
+
photo: '[data-part="portrait"]',
|
|
4
|
+
name1: '[data-part="name"]>span:first-child',
|
|
5
|
+
name2: '[data-part="name"]>span:last-child',
|
|
6
|
+
home: '[data-part="install"]',
|
|
7
|
+
qrBtn: '[data-part="show-qr"]',
|
|
8
|
+
listPanel: CONTACTS,
|
|
9
|
+
qrPanel: '[data-part="qr-panel"]',
|
|
10
|
+
back: '[data-part="qr-back"]',
|
|
11
|
+
rule4: `${CONTACTS}>a:last-child::after`,
|
|
12
|
+
};
|
|
13
|
+
const ROW = /^row(\d)$/;
|
|
14
|
+
const RULE = /^rule(\d)$/;
|
|
15
|
+
|
|
16
|
+
const kebab = (part: string): string => part.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
17
|
+
|
|
18
|
+
/** The CSS selector, inside the card, of a part named in the motion spec. */
|
|
19
|
+
export function partSelector(part: string): string {
|
|
20
|
+
const row = ROW.exec(part)?.[1];
|
|
21
|
+
if (row !== undefined) return `${CONTACTS}>a:nth-child(${row})>*`;
|
|
22
|
+
const rule = RULE.exec(part)?.[1];
|
|
23
|
+
if (SPECIAL[part] === undefined && rule !== undefined) return `${CONTACTS}>a:nth-child(${rule})::before`;
|
|
24
|
+
return SPECIAL[part] ?? `[data-part="${kebab(part)}"]`;
|
|
25
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Beat } from './beats';
|
|
2
|
+
|
|
3
|
+
export type PartFinder = (part: string) => Element[];
|
|
4
|
+
|
|
5
|
+
/** Plays beats as Web Animations; the first beat of each part and property holds its start, later ones hold their end. */
|
|
6
|
+
export function playBeats(beats: readonly Beat[], find: PartFinder): Animation[] {
|
|
7
|
+
const started = new Set<string>();
|
|
8
|
+
return beats.flatMap((beat) => find(beat.part).flatMap((element, index) => {
|
|
9
|
+
if (typeof element.animate !== 'function') return [];
|
|
10
|
+
const key = `${beat.part}|${index}|${beat.property}`;
|
|
11
|
+
const fill: FillMode = started.has(key) ? 'forwards' : 'both';
|
|
12
|
+
started.add(key);
|
|
13
|
+
const frames = [{ [beat.property]: beat.from }, { [beat.property]: beat.to }];
|
|
14
|
+
return [element.animate(frames, { delay: beat.atMs, duration: beat.lengthMs, easing: beat.easing, fill })];
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Cancels every animation still holding a value. */
|
|
19
|
+
export function cancelAll(animations: readonly Animation[]): void {
|
|
20
|
+
animations.forEach((animation) => animation.cancel());
|
|
21
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const NOT_A_SIDE = '[data-part="qr-back"],[data-part="qr-panel"],[data-part="save-live"],[data-part="edge"],dialog,script,style,template';
|
|
2
|
+
const HALF = 2;
|
|
3
|
+
|
|
4
|
+
/** Every element of the card that turns, leaving out Save contact and whatever holds it. */
|
|
5
|
+
export function sidesAround(root: Element, save: Element): HTMLElement[] {
|
|
6
|
+
return Array.from(root.children).flatMap((child) => {
|
|
7
|
+
if (child === save || !(child instanceof HTMLElement) || child.matches(NOT_A_SIDE)) return [];
|
|
8
|
+
return child.contains(save) ? sidesAround(child, save) : [child];
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Turns every side around one shared centre, so they move as one card. */
|
|
13
|
+
export function shareOrigin(card: HTMLElement, sides: readonly HTMLElement[]): void {
|
|
14
|
+
const frame = card.getBoundingClientRect();
|
|
15
|
+
const centreX = frame.left + frame.width / HALF;
|
|
16
|
+
const centreY = frame.top + Math.min(frame.height, window.innerHeight) / HALF;
|
|
17
|
+
sides.forEach((side) => {
|
|
18
|
+
const box = side.getBoundingClientRect();
|
|
19
|
+
side.style.transformOrigin = `${centreX - box.left}px ${centreY - box.top}px`;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Places a hidden side over a box of the card, so showing it moves nothing. */
|
|
24
|
+
export function cover(card: HTMLElement, side: HTMLElement, box: DOMRect): void {
|
|
25
|
+
const frame = card.getBoundingClientRect();
|
|
26
|
+
Object.assign(side.style, { top: `${box.top - frame.top}px`, left: `${box.left - frame.left}px`, width: `${box.width}px`, height: `${box.height}px` });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Marks a side hidden (inert, invisible) or shown. */
|
|
30
|
+
export function setSide(side: HTMLElement, hidden: boolean): void {
|
|
31
|
+
side.toggleAttribute('inert', hidden);
|
|
32
|
+
side.toggleAttribute('data-side-hidden', hidden);
|
|
33
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { TiltMotion } from './TiltMotion';
|
|
2
|
+
|
|
3
|
+
const DRAG_PX = 90;
|
|
4
|
+
const SENSOR_DEG = 20;
|
|
5
|
+
const DIGITS = 3;
|
|
6
|
+
const LEAN = { perspective: 900, tiltX: 3, turnY: 11, shrink: 0.07, light: 190 };
|
|
7
|
+
const FACE = { perspective: 400, turnY: 34, grow: 0.06, light: 46 };
|
|
8
|
+
const DRIFT = { photo: 14, name: -9, role: -5, mark: 5, light: 46 };
|
|
9
|
+
|
|
10
|
+
const round = (value: number): number => Number(value.toFixed(DIGITS)) || 0;
|
|
11
|
+
const shiftX = (px: number): string => `translateX(${round(px)}px)`;
|
|
12
|
+
const clampUnit = (value: number): number => Math.max(-1, Math.min(1, value));
|
|
13
|
+
|
|
14
|
+
/** A sideways drag as -1..1, full at 90 px. */
|
|
15
|
+
export function dragToUnit(dx: number): number {
|
|
16
|
+
return clampUnit(dx / DRAG_PX);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** A sensor gamma as -1..1, full 20 degrees from the angle at load. */
|
|
20
|
+
export function gammaToUnit(gamma: number, gammaAtLoad: number): number {
|
|
21
|
+
return clampUnit((gamma - gammaAtLoad) / SENSOR_DEG);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The transform of every moving part for a lean of -1 (full left) to 1 (full right). */
|
|
25
|
+
export function tiltPose(tiltMotion: TiltMotion, unit: number): Record<string, string> {
|
|
26
|
+
const size = Math.abs(unit);
|
|
27
|
+
if (tiltMotion === TiltMotion.CardLeans) {
|
|
28
|
+
const card = `perspective(${LEAN.perspective}px) rotateX(${round(LEAN.tiltX * size)}deg) rotateY(${round(LEAN.turnY * unit)}deg) scale(${round(1 - LEAN.shrink * size)})`;
|
|
29
|
+
return { card, edgeLight: shiftX(LEAN.light * unit) };
|
|
30
|
+
}
|
|
31
|
+
if (tiltMotion === TiltMotion.PortraitLeans) {
|
|
32
|
+
return { photo: `perspective(${FACE.perspective}px) rotateY(${round(FACE.turnY * unit)}deg) scale(${round(1 + FACE.grow * size)})`, hexBand: shiftX(FACE.light * unit) };
|
|
33
|
+
}
|
|
34
|
+
if (tiltMotion !== TiltMotion.LayersDrift) return {};
|
|
35
|
+
const name = shiftX(DRIFT.name * unit);
|
|
36
|
+
const mark = shiftX(DRIFT.mark * unit);
|
|
37
|
+
return { photo: shiftX(DRIFT.photo * unit), name1: name, name2: name, role: shiftX(DRIFT.role * unit), mark, wordmark: mark, hexBand: shiftX(DRIFT.light * unit) };
|
|
38
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
.tmk-card{touch-action:pan-y}
|
|
2
|
+
.tmk-card [data-side-hidden]{visibility:hidden}
|
|
3
|
+
.tmk-card[data-open-motion]:not([data-open-motion='none']) .tmk-card-name span{display:inline-block}
|
|
4
|
+
.tmk-card[data-layout='name'][data-open-motion] .tmk-card-name span,.tmk-card[data-layout='framed'][data-open-motion] .tmk-card-name span{display:block}
|
|
5
|
+
.tmk-card[data-open-motion='written-in'] .tmk-card-name{overflow:hidden}
|
|
6
|
+
|
|
7
|
+
.tmk-card-mark{display:block;flex:none;width:32px;height:32px}
|
|
8
|
+
.tmk-card-mark svg{display:block;width:100%;height:100%}
|
|
9
|
+
.tmk-card-wordmark{display:block;margin-left:8px}
|
|
10
|
+
|
|
11
|
+
.tmk-card .tmk-card-save{position:relative;z-index:2;overflow:hidden}
|
|
12
|
+
.tmk-card-save-icon{position:relative;display:grid;flex:none;overflow:hidden}
|
|
13
|
+
.tmk-card-save-icon>*{display:grid;grid-area:1/1;place-items:center}
|
|
14
|
+
.tmk-card-mini{flex:none;width:22px;height:22px;fill:none;stroke:currentColor;stroke-width:2;stroke-linecap:square;transform-box:fill-box;transform-origin:center}
|
|
15
|
+
.tmk-card-tick,.tmk-card-cube path,.tmk-card [data-part='ready-text']{opacity:0}
|
|
16
|
+
.tmk-card-cube path{transform-box:fill-box}
|
|
17
|
+
.tmk-card-tick-cover{width:26px;height:22px;background:var(--card-accent);transform:translateX(26px)}
|
|
18
|
+
.tmk-card-save-words{display:grid}
|
|
19
|
+
.tmk-card-save-words>*{grid-area:1/1}
|
|
20
|
+
.tmk-card-wipe{position:absolute;inset:0;overflow:hidden;background:var(--card-on-accent);color:var(--card-accent);transform:translateX(-100%)}
|
|
21
|
+
.tmk-card-wipe-in{display:flex;align-items:center;justify-content:inherit;gap:inherit;height:100%;padding:inherit;transform:translateX(100%)}
|
|
22
|
+
.tmk-card-live{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}
|
|
23
|
+
|
|
24
|
+
.tmk-card-qr-words{display:grid}
|
|
25
|
+
.tmk-card-qr-words>*{grid-area:1/1}
|
|
26
|
+
.tmk-card [data-part='qr-close']{opacity:0}
|
|
27
|
+
|
|
28
|
+
.tmk-card .tmk-card-qr-side{position:absolute;inset:0;z-index:1;display:grid;grid-template-rows:minmax(0,1fr);place-items:center;gap:12px;background:var(--card-bg)}
|
|
29
|
+
.tmk-card .tmk-card-qr-back{grid-template-rows:minmax(0,1fr) auto;padding:16px;border:1px solid var(--card-accent);border-radius:var(--card-radius);background:var(--card-raised)}
|
|
30
|
+
.tmk-card-qr-side .tmk-card-qr{display:grid;place-items:center;width:100%;height:100%;min-height:0}
|
|
31
|
+
.tmk-card .tmk-card-qr-side .tmk-card-qr svg{width:auto;height:100%;min-width:0;max-width:100%;margin:0}
|
|
32
|
+
.tmk-card-qr-back .tmk-card-button{width:100%}
|
|
33
|
+
|
|
34
|
+
.tmk-card-edge{position:absolute;inset:0;z-index:3;padding:2px;overflow:hidden;opacity:0;pointer-events:none;transition:opacity 360ms cubic-bezier(0.2,0,0,1) 200ms;-webkit-mask:linear-gradient(#000 0 0) content-box,linear-gradient(#000 0 0);-webkit-mask-composite:xor;mask-composite:exclude}
|
|
35
|
+
.tmk-card-edge>span,.tmk-card-hex-light>span{position:absolute;top:0;bottom:0;left:50%;width:120px;margin-left:-60px;background:linear-gradient(90deg,transparent,var(--card-accent),transparent)}
|
|
36
|
+
.tmk-card-portrait{position:relative}
|
|
37
|
+
.tmk-card-hex-light{position:absolute;inset:0;overflow:hidden;clip-path:var(--tmk-card-hex);opacity:0;pointer-events:none;mix-blend-mode:soft-light;transition:opacity 360ms cubic-bezier(0.2,0,0,1) 200ms}
|
|
38
|
+
.tmk-card-hex-light>span{width:40px;margin-left:-20px}
|
|
39
|
+
.tmk-card[data-tilting] :is(.tmk-card-edge,.tmk-card-hex-light){opacity:1;transition-duration:200ms;transition-delay:0s}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { InstallRoute } from './InstallRoute';
|
|
2
|
+
import { chooseInstallRoute, isAppleMobile } from './chooseInstall';
|
|
3
|
+
import { mountCardMotion } from './motion/mountMotion';
|
|
4
|
+
|
|
5
|
+
const MOUNTED_ATTRIBUTE = 'data-card-mounted';
|
|
6
|
+
const INSTALLED_QUERY = '(display-mode: standalone)';
|
|
7
|
+
|
|
8
|
+
interface InstallPromptEvent extends Event {
|
|
9
|
+
prompt: () => Promise<unknown>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function isInstallPromptEvent(event: Event): event is InstallPromptEvent {
|
|
13
|
+
return 'prompt' in event && typeof event.prompt === 'function';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function openDialog(card: HTMLElement, name: string, opener: HTMLElement): void {
|
|
17
|
+
const dialog = card.querySelector<HTMLDialogElement>(`[data-card-dialog="${name}"]`);
|
|
18
|
+
if (dialog === null) return;
|
|
19
|
+
dialog.addEventListener('close', () => opener.focus(), { once: true });
|
|
20
|
+
dialog.showModal();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function showInstallHelp(card: HTMLElement, route: InstallRoute, opener: HTMLElement): void {
|
|
24
|
+
card.querySelectorAll<HTMLElement>('[data-install-route]').forEach((help) => {
|
|
25
|
+
help.hidden = help.dataset['installRoute'] !== route;
|
|
26
|
+
});
|
|
27
|
+
openDialog(card, 'install', opener);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function setHidden(buttons: HTMLElement[], hidden: boolean): void {
|
|
31
|
+
buttons.forEach((button) => { button.hidden = hidden; });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function takeUnmountedCards(root: ParentNode, signal?: AbortSignal): HTMLElement[] {
|
|
35
|
+
if (signal?.aborted === true) return [];
|
|
36
|
+
const cards = Array.from(root.querySelectorAll<HTMLElement>(`[data-contact-card]:not([${MOUNTED_ATTRIBUTE}])`));
|
|
37
|
+
cards.forEach((card) => card.setAttribute(MOUNTED_ATTRIBUTE, 'true'));
|
|
38
|
+
signal?.addEventListener('abort', () => cards.forEach((card) => {
|
|
39
|
+
card.removeAttribute(MOUNTED_ATTRIBUTE);
|
|
40
|
+
setHidden(Array.from(card.querySelectorAll<HTMLElement>('[data-card-qr],[data-card-install]')), true);
|
|
41
|
+
}), { once: true });
|
|
42
|
+
return cards;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function mountInstall(cards: HTMLElement[], signal?: AbortSignal): void {
|
|
46
|
+
let offer: InstallPromptEvent | null = null;
|
|
47
|
+
const buttons = cards.flatMap((card) =>
|
|
48
|
+
Array.from(card.querySelectorAll<HTMLElement>('[data-card-install]'), (button) => ({ card, button })),
|
|
49
|
+
);
|
|
50
|
+
const hideButtons = (): void => setHidden(buttons.map(({ button }) => button), true);
|
|
51
|
+
|
|
52
|
+
setHidden(buttons.map(({ button }) => button), window.matchMedia(INSTALLED_QUERY).matches);
|
|
53
|
+
window.addEventListener('appinstalled', hideButtons, { signal });
|
|
54
|
+
window.addEventListener('beforeinstallprompt', (event) => {
|
|
55
|
+
if (!isInstallPromptEvent(event)) return;
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
offer = event;
|
|
58
|
+
}, { signal });
|
|
59
|
+
buttons.forEach(({ card, button }) => button.addEventListener('click', () => {
|
|
60
|
+
const route = chooseInstallRoute({ hasPrompt: offer !== null, isApple: isAppleMobile(navigator) });
|
|
61
|
+
if (route !== InstallRoute.Prompt) showInstallHelp(card, route, button);
|
|
62
|
+
offer?.prompt().catch((error: unknown) => console.warn(error));
|
|
63
|
+
offer = null;
|
|
64
|
+
}, { signal }));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Wires every `[data-contact-card]` once: motion, Show my QR and Add to home screen; `signal` removes the listeners. */
|
|
68
|
+
export function mountContactCards(root: ParentNode = document, signal?: AbortSignal): void {
|
|
69
|
+
const cards = takeUnmountedCards(root, signal);
|
|
70
|
+
if (cards.length === 0) return;
|
|
71
|
+
cards.forEach((card) => {
|
|
72
|
+
mountCardMotion(card, signal);
|
|
73
|
+
setHidden(Array.from(card.querySelectorAll<HTMLElement>('[data-card-qr]')), false);
|
|
74
|
+
});
|
|
75
|
+
mountInstall(cards, signal);
|
|
76
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { optionError } from './checks';
|
|
2
|
+
|
|
3
|
+
const SEPARATORS = /[\s.-]/g;
|
|
4
|
+
const INTERNATIONAL_NUMBER = /^\+[1-9]\d{6,14}$/;
|
|
5
|
+
|
|
6
|
+
/** Returns a phone number as `+` and digits for `tel:` links and contact files; throws unless it is international. */
|
|
7
|
+
export function toDialNumber(phone: string): string {
|
|
8
|
+
const dialNumber = phone.replace(SEPARATORS, '');
|
|
9
|
+
if (!INTERNATIONAL_NUMBER.test(dialNumber)) throw optionError('phone', 'must be an international number starting with +');
|
|
10
|
+
return dialNumber;
|
|
11
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import createQrCode from 'qrcode-generator';
|
|
2
|
+
import { optionError, requireHttpsUrl, requireText } from './checks';
|
|
3
|
+
|
|
4
|
+
const ERROR_CORRECTION_LEVEL = 'M';
|
|
5
|
+
const AUTO_TYPE_NUMBER = 0;
|
|
6
|
+
const QUIET_ZONE_MODULES = 4;
|
|
7
|
+
const DEFAULT_DARK_COLOR = '#000000';
|
|
8
|
+
const DEFAULT_LIGHT_COLOR = '#ffffff';
|
|
9
|
+
const HEX_COLOR_PATTERN = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
10
|
+
|
|
11
|
+
export interface QrSvgOptions {
|
|
12
|
+
url: string;
|
|
13
|
+
title: string;
|
|
14
|
+
darkColor?: string;
|
|
15
|
+
lightColor?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function requireHexColor(value: string, field: string): string {
|
|
19
|
+
if (!HEX_COLOR_PATTERN.test(value)) throw optionError(field, 'must be a 3 or 6 digit hex colour');
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function escapeMarkup(text: string): string {
|
|
24
|
+
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function rowPath(cells: string, row: number): string {
|
|
28
|
+
return Array.from(cells.matchAll(/1+/g), (run) => {
|
|
29
|
+
const width = String(run[0].length);
|
|
30
|
+
return `M${String(run.index + QUIET_ZONE_MODULES)} ${String(row + QUIET_ZONE_MODULES)}h${width}v1h-${width}z`;
|
|
31
|
+
}).join('');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function darkRows(text: string): string[] {
|
|
35
|
+
const qr = createQrCode(AUTO_TYPE_NUMBER, ERROR_CORRECTION_LEVEL);
|
|
36
|
+
qr.addData(text);
|
|
37
|
+
qr.make();
|
|
38
|
+
const indexes = Array.from({ length: qr.getModuleCount() }, (_cell, index) => index);
|
|
39
|
+
return indexes.map((row) => indexes.map((column) => (qr.isDark(row, column) ? '1' : '0')).join(''));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Returns a standalone SVG of the URL's QR code with its quiet zone, for a page or a print file. */
|
|
43
|
+
export function buildQrSvg(options: QrSvgOptions): string {
|
|
44
|
+
const title = escapeMarkup(requireText(options.title, 'title'));
|
|
45
|
+
const dark = requireHexColor(options.darkColor ?? DEFAULT_DARK_COLOR, 'darkColor');
|
|
46
|
+
const light = requireHexColor(options.lightColor ?? DEFAULT_LIGHT_COLOR, 'lightColor');
|
|
47
|
+
const rows = darkRows(requireHttpsUrl(options.url, 'url'));
|
|
48
|
+
const size = String(rows.length + QUIET_ZONE_MODULES + QUIET_ZONE_MODULES);
|
|
49
|
+
return (
|
|
50
|
+
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size} ${size}" role="img" shape-rendering="crispEdges">` +
|
|
51
|
+
`<title>${title}</title><rect width="100%" height="100%" fill="${light}"/>` +
|
|
52
|
+
`<path fill="${dark}" d="${rows.map(rowPath).join('')}"/></svg>`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export interface ContactCardPerson {
|
|
2
|
+
givenName: string;
|
|
3
|
+
familyName: string;
|
|
4
|
+
role: string;
|
|
5
|
+
phone: string;
|
|
6
|
+
email: string;
|
|
7
|
+
website: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ContactCardImage {
|
|
11
|
+
src: string;
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ContactCardBrand extends ContactCardImage {
|
|
17
|
+
href: string;
|
|
18
|
+
label: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ContactCardPortrait extends ContactCardImage {
|
|
22
|
+
alt: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ContactCardLabels {
|
|
26
|
+
save: string;
|
|
27
|
+
call: string;
|
|
28
|
+
email: string;
|
|
29
|
+
website: string;
|
|
30
|
+
contacts: string;
|
|
31
|
+
install: string;
|
|
32
|
+
showQr: string;
|
|
33
|
+
qrTitle: string;
|
|
34
|
+
installTitle: string;
|
|
35
|
+
appleSteps: string[];
|
|
36
|
+
menuHint: string;
|
|
37
|
+
close: string;
|
|
38
|
+
hideQr: string;
|
|
39
|
+
ready: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A brand mark from @dloizides/logo-kit: a mode's intro and still markup, plus its wordmark. */
|
|
43
|
+
export interface ContactCardBrandMarkup {
|
|
44
|
+
intro: string;
|
|
45
|
+
still: string;
|
|
46
|
+
wordmark?: { html: string; css: string } | null;
|
|
47
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { optionError, requireEmail, requireHttpsUrl, requireText } from './checks';
|
|
2
|
+
import type { ContactPhotoType } from './ContactPhotoType';
|
|
3
|
+
import { toDialNumber } from './phone';
|
|
4
|
+
import { escapeText, joinLines } from './vCardLines';
|
|
5
|
+
|
|
6
|
+
const BASE64_PATTERN = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
7
|
+
const BASE64_GROUP_LENGTH = 4;
|
|
8
|
+
|
|
9
|
+
export interface ContactPhoto {
|
|
10
|
+
type: ContactPhotoType;
|
|
11
|
+
base64: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface VCardOptions {
|
|
15
|
+
givenName: string;
|
|
16
|
+
familyName: string;
|
|
17
|
+
organisation: string;
|
|
18
|
+
role: string;
|
|
19
|
+
phone: string;
|
|
20
|
+
email: string;
|
|
21
|
+
website: string;
|
|
22
|
+
photo?: ContactPhoto;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function photoLine(photo: ContactPhoto): string {
|
|
26
|
+
const isBase64 = BASE64_PATTERN.test(photo.base64) && photo.base64.length % BASE64_GROUP_LENGTH === 0;
|
|
27
|
+
if (!isBase64) throw optionError('photo', 'must be plain base64 with no data: prefix or line breaks');
|
|
28
|
+
return `PHOTO;ENCODING=b;TYPE=${photo.type}:${photo.base64}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Returns the text of a vCard 3.0 `.vcf` file for one person; throws naming the first bad option. */
|
|
32
|
+
export function buildVCard(options: VCardOptions): string {
|
|
33
|
+
const givenName = escapeText(requireText(options.givenName, 'givenName'));
|
|
34
|
+
const familyName = escapeText(requireText(options.familyName, 'familyName'));
|
|
35
|
+
return joinLines([
|
|
36
|
+
'BEGIN:VCARD',
|
|
37
|
+
'VERSION:3.0',
|
|
38
|
+
`N:${familyName};${givenName};;;`,
|
|
39
|
+
`FN:${givenName} ${familyName}`,
|
|
40
|
+
`ORG:${escapeText(requireText(options.organisation, 'organisation'))}`,
|
|
41
|
+
`TITLE:${escapeText(requireText(options.role, 'role'))}`,
|
|
42
|
+
`TEL;TYPE=CELL,VOICE:${toDialNumber(options.phone)}`,
|
|
43
|
+
`EMAIL;TYPE=INTERNET:${requireEmail(options.email)}`,
|
|
44
|
+
`URL:${requireHttpsUrl(options.website, 'website')}`,
|
|
45
|
+
...(options.photo === undefined ? [] : [photoLine(options.photo)]),
|
|
46
|
+
'END:VCARD',
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const LINE_BREAK = '\r\n';
|
|
2
|
+
const CONTINUATION = ' ';
|
|
3
|
+
const MAX_LINE_OCTETS = 75;
|
|
4
|
+
const SPECIAL_CHARACTERS = /[\\;,]/g;
|
|
5
|
+
const ANY_LINE_BREAK = /\r\n|\r|\n/g;
|
|
6
|
+
|
|
7
|
+
/** Escapes a vCard text value, so it can neither split a field nor start a new property. */
|
|
8
|
+
export function escapeText(value: string): string {
|
|
9
|
+
return value.replace(SPECIAL_CHARACTERS, '\\$&').replace(ANY_LINE_BREAK, '\\n');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function foldLine(line: string, encoder: TextEncoder): string {
|
|
13
|
+
const parts: string[] = [];
|
|
14
|
+
let part = '';
|
|
15
|
+
let octets = 0;
|
|
16
|
+
for (const character of line) {
|
|
17
|
+
const size = encoder.encode(character).length;
|
|
18
|
+
if (octets + size > MAX_LINE_OCTETS) {
|
|
19
|
+
parts.push(part);
|
|
20
|
+
part = CONTINUATION;
|
|
21
|
+
octets = CONTINUATION.length;
|
|
22
|
+
}
|
|
23
|
+
part += character;
|
|
24
|
+
octets += size;
|
|
25
|
+
}
|
|
26
|
+
return [...parts, part].join(LINE_BREAK);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Joins property lines with CRLF, folding each at 75 octets without splitting a character. */
|
|
30
|
+
export function joinLines(lines: readonly string[]): string {
|
|
31
|
+
const encoder = new TextEncoder();
|
|
32
|
+
return lines.map((line) => `${foldLine(line, encoder)}${LINE_BREAK}`).join('');
|
|
33
|
+
}
|