@phnx-labs/artifacts-cli 0.4.6 → 0.5.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 +130 -0
- package/README.md +127 -12
- package/dist/cli.d.ts +1 -6
- package/dist/cli.js +93 -85
- package/dist/cli.js.map +1 -1
- package/dist/commands/auth.d.ts +5 -0
- package/dist/commands/auth.js +112 -0
- package/dist/commands/auth.js.map +1 -0
- package/dist/commands/favorite.d.ts +9 -0
- package/dist/commands/favorite.js +59 -0
- package/dist/commands/favorite.js.map +1 -0
- package/dist/commands/share.d.ts +297 -14
- package/dist/commands/share.js +1351 -71
- package/dist/commands/share.js.map +1 -1
- package/dist/lib/browser-detect.d.ts +28 -0
- package/dist/lib/browser-detect.js +126 -0
- package/dist/lib/browser-detect.js.map +1 -0
- package/dist/lib/config.d.ts +14 -0
- package/dist/lib/config.js +8 -0
- package/dist/lib/config.js.map +1 -1
- package/dist/lib/embedded-html.d.ts +1 -1
- package/dist/lib/embedded-html.js +12 -0
- package/dist/lib/embedded-html.js.map +1 -1
- package/dist/lib/excerpt.d.ts +48 -0
- package/dist/lib/excerpt.js +167 -0
- package/dist/lib/excerpt.js.map +1 -0
- package/dist/lib/favorites.d.ts +73 -0
- package/dist/lib/favorites.js +180 -0
- package/dist/lib/favorites.js.map +1 -0
- package/dist/lib/format.d.ts +2 -0
- package/dist/lib/format.js +14 -0
- package/dist/lib/format.js.map +1 -0
- package/dist/lib/freshness.d.ts +1 -1
- package/dist/lib/freshness.js +1 -1
- package/dist/lib/github.d.ts +14 -0
- package/dist/lib/github.js +36 -0
- package/dist/lib/github.js.map +1 -0
- package/dist/lib/header-actions.d.ts +44 -0
- package/dist/lib/header-actions.js +181 -0
- package/dist/lib/header-actions.js.map +1 -0
- package/dist/lib/identity/client.d.ts +49 -0
- package/dist/lib/identity/client.js +97 -0
- package/dist/lib/identity/client.js.map +1 -0
- package/dist/lib/identity/index.d.ts +60 -0
- package/dist/lib/identity/index.js +69 -0
- package/dist/lib/identity/index.js.map +1 -0
- package/dist/lib/link-previews.d.ts +67 -0
- package/dist/lib/link-previews.js +427 -0
- package/dist/lib/link-previews.js.map +1 -0
- package/dist/lib/links.d.ts +2 -0
- package/dist/lib/links.js +21 -2
- package/dist/lib/links.js.map +1 -1
- package/dist/lib/math.d.ts +31 -0
- package/dist/lib/math.js +122 -0
- package/dist/lib/math.js.map +1 -0
- package/dist/lib/metadata-menu.d.ts +16 -1
- package/dist/lib/metadata-menu.js +24 -0
- package/dist/lib/metadata-menu.js.map +1 -1
- package/dist/lib/open.d.ts +6 -0
- package/dist/lib/open.js +54 -0
- package/dist/lib/open.js.map +1 -0
- package/dist/lib/render.d.ts +3 -0
- package/dist/lib/render.js +164 -54
- package/dist/lib/render.js.map +1 -1
- package/dist/lib/revisions.d.ts +17 -0
- package/dist/lib/revisions.js +6 -1
- package/dist/lib/revisions.js.map +1 -1
- package/dist/lib/share/analytics.d.ts +13 -0
- package/dist/lib/share/analytics.js +46 -0
- package/dist/lib/share/analytics.js.map +1 -0
- package/dist/lib/share/backend.d.ts +105 -0
- package/dist/lib/share/backend.js +161 -0
- package/dist/lib/share/backend.js.map +1 -0
- package/dist/lib/share/capture.d.ts +31 -0
- package/dist/lib/share/capture.js +175 -0
- package/dist/lib/share/capture.js.map +1 -0
- package/dist/lib/share/config.d.ts +82 -0
- package/dist/lib/share/config.js +181 -0
- package/dist/lib/share/config.js.map +1 -0
- package/dist/lib/share/delete.d.ts +122 -0
- package/dist/lib/share/delete.js +174 -0
- package/dist/lib/share/delete.js.map +1 -0
- package/dist/lib/share/html.d.ts +22 -0
- package/dist/lib/share/html.js +89 -0
- package/dist/lib/share/html.js.map +1 -0
- package/dist/lib/share/http-error.d.ts +52 -0
- package/dist/lib/share/http-error.js +66 -0
- package/dist/lib/share/http-error.js.map +1 -0
- package/dist/lib/share/og.d.ts +25 -0
- package/dist/lib/share/og.js +85 -0
- package/dist/lib/share/og.js.map +1 -0
- package/dist/lib/share/provision.d.ts +99 -0
- package/dist/lib/share/provision.js +271 -0
- package/dist/lib/share/provision.js.map +1 -0
- package/dist/lib/share/publish.d.ts +393 -0
- package/dist/lib/share/publish.js +829 -0
- package/dist/lib/share/publish.js.map +1 -0
- package/dist/lib/share/qr.d.ts +36 -0
- package/dist/lib/share/qr.js +102 -0
- package/dist/lib/share/qr.js.map +1 -0
- package/dist/lib/share/storage-policy.d.ts +9 -0
- package/dist/lib/share/storage-policy.js +43 -0
- package/dist/lib/share/storage-policy.js.map +1 -0
- package/dist/lib/share/visibility.d.ts +92 -0
- package/dist/lib/share/visibility.js +108 -0
- package/dist/lib/share/visibility.js.map +1 -0
- package/dist/lib/share/worker-template.d.ts +29 -0
- package/dist/lib/share/worker-template.js +2154 -0
- package/dist/lib/share/worker-template.js.map +1 -0
- package/dist/lib/status-list.d.ts +49 -0
- package/dist/lib/status-list.js +153 -0
- package/dist/lib/status-list.js.map +1 -0
- package/dist/lib/validate.js +122 -8
- package/dist/lib/validate.js.map +1 -1
- package/package.json +12 -3
- package/dist/lib/share-provision.d.ts +0 -31
- package/dist/lib/share-provision.js +0 -83
- package/dist/lib/share-provision.js.map +0 -1
- package/dist/lib/share-worker.d.ts +0 -1
- package/dist/lib/share-worker.js +0 -66
- package/dist/lib/share-worker.js.map +0 -1
- package/dist/lib/share.d.ts +0 -39
- package/dist/lib/share.js +0 -159
- package/dist/lib/share.js.map +0 -1
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The document-header action buttons: Share and Favorite.
|
|
3
|
+
*
|
|
4
|
+
* One markup builder, one style block, one script. The renderer emits the
|
|
5
|
+
* slot between {@link HEADER_ACTIONS_START}/{@link HEADER_ACTIONS_END} markers
|
|
6
|
+
* with the buttons in their DEFAULT (unfavorited) state, so the render
|
|
7
|
+
* fingerprint never flips on a favorite toggle; the decorate step (which runs
|
|
8
|
+
* after fingerprinting) rewrites the slot with the favorite state actually
|
|
9
|
+
* recorded in the store.
|
|
10
|
+
*
|
|
11
|
+
* Click behavior is decided at runtime by where the page is being viewed:
|
|
12
|
+
* - served by `artifacts render --open` (loopback http): POST back to the
|
|
13
|
+
* local server, which performs the owner-only share / favorite toggle for
|
|
14
|
+
* real and reports the result;
|
|
15
|
+
* - a plain file:// page: the copy-command idiom — the button copies the
|
|
16
|
+
* exact CLI command (`artifacts share <id> --visibility me`,
|
|
17
|
+
* `artifacts favorite <id>`) to run in a terminal;
|
|
18
|
+
* - a hosted page (the managed share host, or any non-loopback origin): the
|
|
19
|
+
* artifact is already published, so Share degrades to copying the page's
|
|
20
|
+
* own URL rather than offering to re-share; Favorite still copies the
|
|
21
|
+
* command (only meaningful on the author's machine, harmless elsewhere).
|
|
22
|
+
*
|
|
23
|
+
* The copied commands address the artifact by its store ID, never by path —
|
|
24
|
+
* reader-facing HTML must not carry local absolute paths (the revision store
|
|
25
|
+
* has a standing test for that invariant), and `artifacts share` accepts an id
|
|
26
|
+
* exactly like `artifacts history` does.
|
|
27
|
+
*/
|
|
28
|
+
import { escapeHtml } from './html.js';
|
|
29
|
+
export const HEADER_ACTIONS_START = '<!--artifact-actions-start-->';
|
|
30
|
+
export const HEADER_ACTIONS_END = '<!--artifact-actions-end-->';
|
|
31
|
+
export function shareCommand(actions) {
|
|
32
|
+
// The id is a fixed-shape [0-9a-f]{64} digest — safe unquoted.
|
|
33
|
+
return `artifacts share ${actions.artifactId} --visibility me`;
|
|
34
|
+
}
|
|
35
|
+
export function favoriteCommand(actions) {
|
|
36
|
+
return `artifacts favorite ${actions.artifactId}`;
|
|
37
|
+
}
|
|
38
|
+
const STAR_PATH = 'M12 2.6l2.9 6.28 6.9.68-5.19 4.6 1.44 6.8L12 17.77l-6.05 3.19 1.44-6.8-5.19-4.6 6.9-.68z';
|
|
39
|
+
const SHARE_ICON = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"><path d="M12 15V3.5"/><path d="M7.5 7.5L12 3l4.5 4.5"/><path d="M4.5 13.5v5a2 2 0 0 0 2 2h11a2 2 0 0 0 2-2v-5"/></svg>';
|
|
40
|
+
export function headerActions(actions) {
|
|
41
|
+
const share = shareCommand(actions);
|
|
42
|
+
const favorite = favoriteCommand(actions);
|
|
43
|
+
const on = actions.favorited;
|
|
44
|
+
return (`<span class="header-actions" data-header-actions>` +
|
|
45
|
+
`<button type="button" class="header-action share-action" data-share-command="${escapeHtml(share)}" ` +
|
|
46
|
+
`aria-label="Share owner-only (only you can open the link)" title="Share owner-only — only you can open the link. Copies the share command; shares directly when served by artifacts render --open.">` +
|
|
47
|
+
`${SHARE_ICON}<span class="share-action-label">Share</span></button>` +
|
|
48
|
+
`<button type="button" class="header-action favorite-action${on ? ' is-favorite' : ''}" data-favorite-command="${escapeHtml(favorite)}" ` +
|
|
49
|
+
`aria-pressed="${on}" aria-label="${on ? 'Remove from favorites' : 'Add to favorites'}" title="${on ? 'Remove from favorites' : 'Favorite this artifact'}">` +
|
|
50
|
+
`<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linejoin="round" aria-hidden="true" focusable="false"><path d="${STAR_PATH}"/></svg></button>` +
|
|
51
|
+
`<span class="header-action-feedback" role="status" aria-live="polite"></span>` +
|
|
52
|
+
`</span>`);
|
|
53
|
+
}
|
|
54
|
+
/** The slot the renderer emits; `decorateHeaderActions` rewrites its content. */
|
|
55
|
+
export function headerActionsSlot(actions) {
|
|
56
|
+
return `${HEADER_ACTIONS_START}${headerActions(actions)}${HEADER_ACTIONS_END}`;
|
|
57
|
+
}
|
|
58
|
+
/** Replace a rendered document's action slot with fresh state. No markers → unchanged. */
|
|
59
|
+
export function decorateHeaderActions(html, actions) {
|
|
60
|
+
const start = html.indexOf(HEADER_ACTIONS_START);
|
|
61
|
+
const end = html.indexOf(HEADER_ACTIONS_END, start);
|
|
62
|
+
if (start < 0 || end < 0)
|
|
63
|
+
return html;
|
|
64
|
+
return html.slice(0, start) + headerActionsSlot(actions) + html.slice(end + HEADER_ACTIONS_END.length);
|
|
65
|
+
}
|
|
66
|
+
export const HEADER_ACTIONS_CSS = `
|
|
67
|
+
.header-actions{display:inline-flex;align-items:center;gap:6px;flex:0 0 auto}
|
|
68
|
+
.header-action{display:inline-flex;align-items:center;gap:6px;height:30px;padding:0 12px;border:1px solid var(--line);border-radius:999px;background:var(--surface);color:var(--muted);font:500 12px/1 var(--font-mono);cursor:pointer}
|
|
69
|
+
.header-action:hover{color:var(--accent);border-color:var(--accent)}
|
|
70
|
+
.header-action:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
|
|
71
|
+
.header-action:disabled{opacity:.6;cursor:default}
|
|
72
|
+
.header-action svg{width:13px;height:13px;flex:0 0 auto}
|
|
73
|
+
.favorite-action{padding:0 10px;border-color:color-mix(in srgb,var(--accent) 45%,var(--line));color:var(--accent)}
|
|
74
|
+
.favorite-action.is-favorite{background:color-mix(in srgb,var(--accent) 16%,var(--surface));border-color:var(--accent)}
|
|
75
|
+
.favorite-action.is-favorite svg{fill:currentColor}
|
|
76
|
+
.header-action-feedback{color:var(--muted);font:11.5px/1.4 var(--font-mono);max-width:300px;overflow-wrap:anywhere}
|
|
77
|
+
.header-action-feedback:empty{display:none}
|
|
78
|
+
@media print{.header-actions{display:none}}
|
|
79
|
+
`;
|
|
80
|
+
export const HEADER_ACTIONS_SCRIPT = `<script>
|
|
81
|
+
(() => {
|
|
82
|
+
const root = document.querySelector('[data-header-actions]');
|
|
83
|
+
if (!root) return;
|
|
84
|
+
const shareButton = root.querySelector('.share-action');
|
|
85
|
+
const favoriteButton = root.querySelector('.favorite-action');
|
|
86
|
+
const status = root.querySelector('.header-action-feedback');
|
|
87
|
+
let timer;
|
|
88
|
+
const say = (message) => {
|
|
89
|
+
status.textContent = message;
|
|
90
|
+
clearTimeout(timer);
|
|
91
|
+
timer = setTimeout(() => { if (status.textContent === message) status.textContent = ''; }, 8000);
|
|
92
|
+
};
|
|
93
|
+
const copy = async (text) => {
|
|
94
|
+
try {
|
|
95
|
+
if (!navigator.clipboard || !navigator.clipboard.writeText) throw new Error('unavailable');
|
|
96
|
+
await navigator.clipboard.writeText(text);
|
|
97
|
+
return true;
|
|
98
|
+
} catch {
|
|
99
|
+
const area = document.createElement('textarea');
|
|
100
|
+
area.value = text;
|
|
101
|
+
area.style.position = 'fixed'; area.style.opacity = '0';
|
|
102
|
+
document.body.appendChild(area);
|
|
103
|
+
area.focus(); area.select();
|
|
104
|
+
let ok = false;
|
|
105
|
+
try { ok = document.execCommand('copy'); } catch {}
|
|
106
|
+
area.remove();
|
|
107
|
+
return ok;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
// Where is this page being viewed? The loopback dev server (artifacts render
|
|
111
|
+
// --open) answers POSTs for real actions; any other http(s) origin is an
|
|
112
|
+
// already-published page; file:// has no server at all.
|
|
113
|
+
const hostname = location.hostname;
|
|
114
|
+
const web = location.protocol === 'http:' || location.protocol === 'https:';
|
|
115
|
+
const loopback = web && (hostname === '127.0.0.1' || hostname === 'localhost' || hostname === '[::1]' || hostname === '::1');
|
|
116
|
+
const hosted = web && !loopback;
|
|
117
|
+
const copyCommand = async (command, what) => {
|
|
118
|
+
const ok = await copy(command);
|
|
119
|
+
say(ok ? what + ' command copied — run it in your terminal' : 'Could not copy. ' + what + ' command: ' + command);
|
|
120
|
+
};
|
|
121
|
+
const paintFavorite = (on) => {
|
|
122
|
+
favoriteButton.classList.toggle('is-favorite', on);
|
|
123
|
+
favoriteButton.setAttribute('aria-pressed', String(on));
|
|
124
|
+
const label = on ? 'Remove from favorites' : 'Add to favorites';
|
|
125
|
+
favoriteButton.setAttribute('aria-label', label);
|
|
126
|
+
favoriteButton.setAttribute('title', on ? 'Remove from favorites' : 'Favorite this artifact');
|
|
127
|
+
};
|
|
128
|
+
if (hosted) {
|
|
129
|
+
// Already published: re-sharing makes no sense here, so Share becomes a
|
|
130
|
+
// plain copy-link of this page's URL.
|
|
131
|
+
shareButton.querySelector('.share-action-label').textContent = 'Copy link';
|
|
132
|
+
shareButton.setAttribute('aria-label', 'Copy link to this page');
|
|
133
|
+
shareButton.setAttribute('title', 'Copy the link to this page');
|
|
134
|
+
shareButton.addEventListener('click', async () => {
|
|
135
|
+
const ok = await copy(location.href);
|
|
136
|
+
say(ok ? 'Link copied' : 'Could not copy the link — copy it from the address bar');
|
|
137
|
+
});
|
|
138
|
+
} else if (loopback) {
|
|
139
|
+
shareButton.addEventListener('click', async () => {
|
|
140
|
+
shareButton.disabled = true;
|
|
141
|
+
say('Sharing owner-only…');
|
|
142
|
+
try {
|
|
143
|
+
const response = await fetch('/artifacts/share', { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' });
|
|
144
|
+
if (!response.ok) throw new Error(await response.text());
|
|
145
|
+
const result = await response.json();
|
|
146
|
+
const copied = await copy(result.url);
|
|
147
|
+
say(copied ? 'Shared owner-only — link copied' : 'Shared owner-only: ' + result.url);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
await copyCommand(shareButton.dataset.shareCommand, 'Share');
|
|
150
|
+
} finally {
|
|
151
|
+
shareButton.disabled = false;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
} else {
|
|
155
|
+
shareButton.addEventListener('click', () => copyCommand(shareButton.dataset.shareCommand, 'Share'));
|
|
156
|
+
}
|
|
157
|
+
if (loopback) {
|
|
158
|
+
favoriteButton.addEventListener('click', async () => {
|
|
159
|
+
favoriteButton.disabled = true;
|
|
160
|
+
try {
|
|
161
|
+
const response = await fetch('/artifacts/favorite', {
|
|
162
|
+
method: 'POST',
|
|
163
|
+
headers: { 'content-type': 'application/json' },
|
|
164
|
+
body: JSON.stringify({ title: document.title }),
|
|
165
|
+
});
|
|
166
|
+
if (!response.ok) throw new Error(await response.text());
|
|
167
|
+
const result = await response.json();
|
|
168
|
+
paintFavorite(Boolean(result.favorite));
|
|
169
|
+
say(result.favorite ? 'Favorited' : 'Removed from favorites');
|
|
170
|
+
} catch {
|
|
171
|
+
await copyCommand(favoriteButton.dataset.favoriteCommand, 'Favorite');
|
|
172
|
+
} finally {
|
|
173
|
+
favoriteButton.disabled = false;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
} else {
|
|
177
|
+
favoriteButton.addEventListener('click', () => copyCommand(favoriteButton.dataset.favoriteCommand, 'Favorite'));
|
|
178
|
+
}
|
|
179
|
+
})();
|
|
180
|
+
</script>`;
|
|
181
|
+
//# sourceMappingURL=header-actions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"header-actions.js","sourceRoot":"","sources":["../../src/lib/header-actions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEvC,MAAM,CAAC,MAAM,oBAAoB,GAAG,+BAA+B,CAAC;AACpE,MAAM,CAAC,MAAM,kBAAkB,GAAG,6BAA6B,CAAC;AAShE,MAAM,UAAU,YAAY,CAAC,OAAsB;IACjD,+DAA+D;IAC/D,OAAO,mBAAmB,OAAO,CAAC,UAAU,kBAAkB,CAAC;AACjE,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,OAAsB;IACpD,OAAO,sBAAsB,OAAO,CAAC,UAAU,EAAE,CAAC;AACpD,CAAC;AAED,MAAM,SAAS,GAAG,0FAA0F,CAAC;AAC7G,MAAM,UAAU,GAAG,0RAA0R,CAAC;AAE9S,MAAM,UAAU,aAAa,CAAC,OAAsB;IAClD,MAAM,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IACpC,MAAM,QAAQ,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;IAC1C,MAAM,EAAE,GAAG,OAAO,CAAC,SAAS,CAAC;IAC7B,OAAO,CACL,mDAAmD;QACnD,gFAAgF,UAAU,CAAC,KAAK,CAAC,IAAI;QACrG,sMAAsM;QACtM,GAAG,UAAU,wDAAwD;QACrE,6DAA6D,EAAE,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|
2
|
+
* The ONE place artifacts-cli talks to its account backend (Phoenix ID).
|
|
3
|
+
*
|
|
4
|
+
* One base URL, one token reader, one HTTP funnel, one error type. Commands
|
|
5
|
+
* import from `./index.js` and nothing else builds a Phoenix URL or reads the
|
|
6
|
+
* session file itself.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Where the account backend lives. Config, never a literal at a call site.
|
|
10
|
+
*
|
|
11
|
+
* The default is Phoenix ID's branded custom domain. The managed share Worker
|
|
12
|
+
* verifies its bearers against this same base, so the two must not drift.
|
|
13
|
+
*/
|
|
14
|
+
export declare const DEFAULT_PHOENIX_ID_BASE = "https://id.byphoenix.com";
|
|
15
|
+
export declare const PHOENIX_ID_BASE: string;
|
|
16
|
+
/** Our own session file. artifacts-cli never reads another product's credentials. */
|
|
17
|
+
export declare function sessionFilePath(): string;
|
|
18
|
+
export interface PhoenixSession {
|
|
19
|
+
access_token: string;
|
|
20
|
+
email?: string;
|
|
21
|
+
userId?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Hosted OAuth profile image (https URL), when Phoenix ID exposes one. Wins
|
|
24
|
+
* over the email-Gravatar fallback in share attribution. Optional forever —
|
|
25
|
+
* a missing value just means the Gravatar/initials fallback.
|
|
26
|
+
*/
|
|
27
|
+
avatarUrl?: string;
|
|
28
|
+
/** Unix ms; absent means the server did not scope the token's lifetime. */
|
|
29
|
+
expires_at?: number;
|
|
30
|
+
}
|
|
31
|
+
export declare function readSession(): PhoenixSession | null;
|
|
32
|
+
export declare function writeSession(session: PhoenixSession): void;
|
|
33
|
+
export declare function clearSession(): void;
|
|
34
|
+
/** An error carrying the server's status and message, so callers can branch on it. */
|
|
35
|
+
export declare class PhoenixApiError extends Error {
|
|
36
|
+
readonly status: number;
|
|
37
|
+
constructor(message: string, status: number);
|
|
38
|
+
}
|
|
39
|
+
interface RequestOptions {
|
|
40
|
+
body?: unknown;
|
|
41
|
+
/** Send the stored session token. Default true; the device-flow start does not. */
|
|
42
|
+
auth?: boolean;
|
|
43
|
+
/** Use this token instead of the stored one (mid-login, before the write). */
|
|
44
|
+
token?: string;
|
|
45
|
+
timeoutMs?: number;
|
|
46
|
+
}
|
|
47
|
+
/** The single HTTP funnel. Every request to the account backend goes through here. */
|
|
48
|
+
export declare function phoenixRequest<T>(method: 'GET' | 'POST' | 'PATCH' | 'DELETE', route: string, opts?: RequestOptions): Promise<T>;
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ONE place artifacts-cli talks to its account backend (Phoenix ID).
|
|
3
|
+
*
|
|
4
|
+
* One base URL, one token reader, one HTTP funnel, one error type. Commands
|
|
5
|
+
* import from `./index.js` and nothing else builds a Phoenix URL or reads the
|
|
6
|
+
* session file itself.
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
9
|
+
import { dirname, join } from 'node:path';
|
|
10
|
+
import { stateDir } from '../config.js';
|
|
11
|
+
/**
|
|
12
|
+
* Where the account backend lives. Config, never a literal at a call site.
|
|
13
|
+
*
|
|
14
|
+
* The default is Phoenix ID's branded custom domain. The managed share Worker
|
|
15
|
+
* verifies its bearers against this same base, so the two must not drift.
|
|
16
|
+
*/
|
|
17
|
+
export const DEFAULT_PHOENIX_ID_BASE = 'https://id.byphoenix.com';
|
|
18
|
+
export const PHOENIX_ID_BASE = process.env.PHOENIX_ID_BASE ?? DEFAULT_PHOENIX_ID_BASE;
|
|
19
|
+
/** Our own session file. artifacts-cli never reads another product's credentials. */
|
|
20
|
+
export function sessionFilePath() {
|
|
21
|
+
return join(stateDir(), 'phoenix-session.json');
|
|
22
|
+
}
|
|
23
|
+
export function readSession() {
|
|
24
|
+
try {
|
|
25
|
+
const parsed = JSON.parse(readFileSync(sessionFilePath(), 'utf-8'));
|
|
26
|
+
return parsed.access_token ? parsed : null;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function writeSession(session) {
|
|
33
|
+
const file = sessionFilePath();
|
|
34
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
35
|
+
writeFileSync(file, JSON.stringify(session, null, 2), { mode: 0o600 });
|
|
36
|
+
}
|
|
37
|
+
export function clearSession() {
|
|
38
|
+
try {
|
|
39
|
+
if (existsSync(sessionFilePath()))
|
|
40
|
+
rmSync(sessionFilePath(), { force: true });
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// Already gone: logging out twice is not an error.
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** An error carrying the server's status and message, so callers can branch on it. */
|
|
47
|
+
export class PhoenixApiError extends Error {
|
|
48
|
+
status;
|
|
49
|
+
constructor(message, status) {
|
|
50
|
+
super(message);
|
|
51
|
+
this.status = status;
|
|
52
|
+
this.name = 'PhoenixApiError';
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** The single HTTP funnel. Every request to the account backend goes through here. */
|
|
56
|
+
export async function phoenixRequest(method, route, opts = {}) {
|
|
57
|
+
const headers = { 'Content-Type': 'application/json' };
|
|
58
|
+
if (opts.auth !== false) {
|
|
59
|
+
const token = opts.token ?? readSession()?.access_token;
|
|
60
|
+
if (!token)
|
|
61
|
+
throw new PhoenixApiError("Not signed in. Run 'artifacts auth login'.", 401);
|
|
62
|
+
headers.Authorization = `Bearer ${token}`;
|
|
63
|
+
}
|
|
64
|
+
let response;
|
|
65
|
+
try {
|
|
66
|
+
response = await fetch(`${PHOENIX_ID_BASE}${route}`, {
|
|
67
|
+
method,
|
|
68
|
+
headers,
|
|
69
|
+
body: opts.body === undefined ? undefined : JSON.stringify(opts.body),
|
|
70
|
+
signal: AbortSignal.timeout(opts.timeoutMs ?? 15_000),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
75
|
+
throw new PhoenixApiError(`Could not reach the account service (${detail}).`, 0);
|
|
76
|
+
}
|
|
77
|
+
if (response.status === 204)
|
|
78
|
+
return undefined;
|
|
79
|
+
const text = await response.text();
|
|
80
|
+
let payload = null;
|
|
81
|
+
if (text) {
|
|
82
|
+
try {
|
|
83
|
+
payload = JSON.parse(text);
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
payload = null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
const message = payload && typeof payload === 'object' && 'error' in payload
|
|
91
|
+
? String(payload.error)
|
|
92
|
+
: `${response.status} ${response.statusText}`;
|
|
93
|
+
throw new PhoenixApiError(message, response.status);
|
|
94
|
+
}
|
|
95
|
+
return payload;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/lib/identity/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACrF,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,0BAA0B,CAAC;AAClE,MAAM,CAAC,MAAM,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,uBAAuB,CAAC;AAEtF,qFAAqF;AACrF,MAAM,UAAU,eAAe;IAC7B,OAAO,IAAI,CAAC,QAAQ,EAAE,EAAE,sBAAsB,CAAC,CAAC;AAClD,CAAC;AAgBD,MAAM,UAAU,WAAW;IACzB,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,eAAe,EAAE,EAAE,OAAO,CAAC,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K,QAAQ,IAAI,OAAO,IAAI,OAAO;YAC1D,CAAC,CAAC,MAAM,CAAE,OAA8B,CAAC,KAAK,CAAC;YAC/C,CAAC,CAAC,GAAG,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC;QAClD,MAAM,IAAI,eAAe,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IACtD,CAAC;IACD,OAAO,OAAY,CAAC;AACtB,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phoenix ID — the typed surface commands use. Every route the account backend
|
|
3
|
+
* exposes is a function here; no command builds a URL or reads a token itself.
|
|
4
|
+
*/
|
|
5
|
+
export { PHOENIX_ID_BASE, DEFAULT_PHOENIX_ID_BASE, PhoenixApiError, clearSession, readSession, sessionFilePath, writeSession, type PhoenixSession, } from './client.js';
|
|
6
|
+
export interface DeviceAuthorization {
|
|
7
|
+
device_code: string;
|
|
8
|
+
user_code: string;
|
|
9
|
+
verification_uri: string;
|
|
10
|
+
verification_uri_complete: string;
|
|
11
|
+
expires_in: number;
|
|
12
|
+
interval: number;
|
|
13
|
+
}
|
|
14
|
+
export interface WhoAmI {
|
|
15
|
+
userId: string;
|
|
16
|
+
email: string;
|
|
17
|
+
valid: true;
|
|
18
|
+
/** Hosted OAuth profile image, when Phoenix ID stores one for this user. */
|
|
19
|
+
avatar_url?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* RFC 8628 poll outcomes. `pending` and `slow_down` are normal states of a
|
|
23
|
+
* login in progress, not failures — the server signals them through the error
|
|
24
|
+
* body, and this is where that wire detail stops.
|
|
25
|
+
*/
|
|
26
|
+
export type DevicePoll = {
|
|
27
|
+
status: 'authorized';
|
|
28
|
+
access_token: string;
|
|
29
|
+
user: {
|
|
30
|
+
email: string;
|
|
31
|
+
id: string;
|
|
32
|
+
/** Hosted OAuth profile image, when the provider exposed one to Phoenix ID. */
|
|
33
|
+
avatar_url?: string;
|
|
34
|
+
/** Google-style alias some providers use for the same field. */
|
|
35
|
+
picture?: string;
|
|
36
|
+
};
|
|
37
|
+
} | {
|
|
38
|
+
status: 'pending';
|
|
39
|
+
} | {
|
|
40
|
+
status: 'slow_down';
|
|
41
|
+
} | {
|
|
42
|
+
status: 'expired';
|
|
43
|
+
} | {
|
|
44
|
+
status: 'denied';
|
|
45
|
+
};
|
|
46
|
+
export declare function startDeviceAuthorization(): Promise<DeviceAuthorization>;
|
|
47
|
+
export declare function pollDeviceToken(deviceCode: string): Promise<DevicePoll>;
|
|
48
|
+
export declare function fetchWhoAmI(token?: string): Promise<WhoAmI>;
|
|
49
|
+
/**
|
|
50
|
+
* Keep the session's hosted avatar current: merge the `avatar_url` Phoenix ID
|
|
51
|
+
* reports on `/api/v1/auth/me` into the persisted session, so share attribution
|
|
52
|
+
* can stamp the profile image instead of only a Gravatar hash. Pass `known` when
|
|
53
|
+
* the caller already fetched `/auth/me` (whoami) — then a changed picture is
|
|
54
|
+
* written too. Without it, a session that already carries a picture is left
|
|
55
|
+
* alone rather than spending a network round trip to re-check it (the share
|
|
56
|
+
* publish path). A no-op when signed out or when the server exposes none;
|
|
57
|
+
* network/server failures are swallowed (the Gravatar fallback covers
|
|
58
|
+
* attribution either way).
|
|
59
|
+
*/
|
|
60
|
+
export declare function refreshSessionAvatar(known?: WhoAmI): Promise<void>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phoenix ID — the typed surface commands use. Every route the account backend
|
|
3
|
+
* exposes is a function here; no command builds a URL or reads a token itself.
|
|
4
|
+
*/
|
|
5
|
+
import { phoenixRequest, PhoenixApiError, readSession, writeSession } from './client.js';
|
|
6
|
+
export { PHOENIX_ID_BASE, DEFAULT_PHOENIX_ID_BASE, PhoenixApiError, clearSession, readSession, sessionFilePath, writeSession, } from './client.js';
|
|
7
|
+
export function startDeviceAuthorization() {
|
|
8
|
+
return phoenixRequest('POST', '/api/v1/auth/device/authorization', {
|
|
9
|
+
auth: false,
|
|
10
|
+
body: {},
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
export async function pollDeviceToken(deviceCode) {
|
|
14
|
+
try {
|
|
15
|
+
return await phoenixRequest('POST', '/api/v1/auth/device/token', {
|
|
16
|
+
auth: false,
|
|
17
|
+
body: {
|
|
18
|
+
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
|
|
19
|
+
device_code: deviceCode,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
if (!(err instanceof PhoenixApiError))
|
|
25
|
+
throw err;
|
|
26
|
+
// The server encodes poll state in the error body (RFC 8628 §3.5).
|
|
27
|
+
if (err.message.includes('authorization_pending'))
|
|
28
|
+
return { status: 'pending' };
|
|
29
|
+
if (err.message.includes('slow_down'))
|
|
30
|
+
return { status: 'slow_down' };
|
|
31
|
+
if (err.message.includes('expired_token'))
|
|
32
|
+
return { status: 'expired' };
|
|
33
|
+
if (err.message.includes('access_denied'))
|
|
34
|
+
return { status: 'denied' };
|
|
35
|
+
throw err;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function fetchWhoAmI(token) {
|
|
39
|
+
return phoenixRequest('GET', '/api/v1/auth/me', { token });
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Keep the session's hosted avatar current: merge the `avatar_url` Phoenix ID
|
|
43
|
+
* reports on `/api/v1/auth/me` into the persisted session, so share attribution
|
|
44
|
+
* can stamp the profile image instead of only a Gravatar hash. Pass `known` when
|
|
45
|
+
* the caller already fetched `/auth/me` (whoami) — then a changed picture is
|
|
46
|
+
* written too. Without it, a session that already carries a picture is left
|
|
47
|
+
* alone rather than spending a network round trip to re-check it (the share
|
|
48
|
+
* publish path). A no-op when signed out or when the server exposes none;
|
|
49
|
+
* network/server failures are swallowed (the Gravatar fallback covers
|
|
50
|
+
* attribution either way).
|
|
51
|
+
*/
|
|
52
|
+
export async function refreshSessionAvatar(known) {
|
|
53
|
+
const session = readSession();
|
|
54
|
+
if (!session)
|
|
55
|
+
return;
|
|
56
|
+
if (!known && session.avatarUrl)
|
|
57
|
+
return;
|
|
58
|
+
try {
|
|
59
|
+
const me = known ?? (await fetchWhoAmI());
|
|
60
|
+
const hosted = me.avatar_url?.trim();
|
|
61
|
+
if (hosted && /^https:\/\//i.test(hosted) && hosted !== session.avatarUrl) {
|
|
62
|
+
writeSession({ ...session, avatarUrl: hosted });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// Offline or server without the field — the Gravatar fallback covers it.
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/identity/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEzF,OAAO,EACL,eAAe,EACf,uBAAuB,EACvB,eAAe,EACf,YAAY,EACZ,WAAW,EACX,eAAe,EACf,YAAY,GAEb,MAAM,aAAa,CAAC;AA0CrB,MAAM,UAAU,wBAAwB;IACtC,OAAO,cAAc,CAAsB,MAAM,EAAE,mCAAmC,EAAE;QACtF,IAAI,EAAE,KAAK;QACX,IAAI,EAAE,EAAE;KACT,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,UAAkB;IACtD,IAAI,CAAC;QACH,OAAO,MAAM,cAAc,CACzB,MAAM,EACN,2BAA2B,EAC3B;YACE,IAAI,EAAE,KAAK;YACX,IAAI,EAAE;gBACJ,UAAU,EAAE,8CAA8C;gBAC1D,WAAW,EAAE,UAAU;aACxB;SACF,CACF,CAAC;IACJ,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,CAAC,GAAG,YA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AAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,yEAAyE;IAC3E,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Link previews: at render time, every outbound pill's target is fetched once
|
|
3
|
+
* and its title, description, site name, and social image URL are embedded in
|
|
4
|
+
* the page as JSON. In the reader, clicking a pill opens a preview card built
|
|
5
|
+
* from that JSON (an "Open" link on the card, or a modifier-click on the pill,
|
|
6
|
+
* still goes to the page). The text is embedded, so the card works offline and
|
|
7
|
+
* on a shared copy; only the optional image is loaded from the network at view
|
|
8
|
+
* time, and it removes itself when it cannot load — the same fail-open rule
|
|
9
|
+
* the pill favicons already follow.
|
|
10
|
+
*
|
|
11
|
+
* Fetches are bounded (one attempt per URL, a short timeout, a byte cap, a
|
|
12
|
+
* small concurrency) and cached under the artifact history root, so a watch
|
|
13
|
+
* loop re-rendering on every keystroke never re-fetches, and a failed URL is
|
|
14
|
+
* not retried until its short failure TTL passes. Set
|
|
15
|
+
* ARTIFACTS_NO_LINK_PREVIEWS=1 or pass --no-link-previews to skip the
|
|
16
|
+
* network entirely.
|
|
17
|
+
*
|
|
18
|
+
* Only public hosts are fetched. A render can run unattended (a hook, a
|
|
19
|
+
* watch loop) on a machine that can reach loopback and private-network
|
|
20
|
+
* services, and the fetched text lands in a page that may be published, so
|
|
21
|
+
* every hop — the URL itself and each redirect — must resolve to a public
|
|
22
|
+
* address. ARTIFACTS_LINK_PREVIEWS_ALLOW_PRIVATE=1 (or the allowPrivateHosts
|
|
23
|
+
* option) lifts that for intranet documentation and the test suite's local
|
|
24
|
+
* origin.
|
|
25
|
+
*/
|
|
26
|
+
export interface LinkPreview {
|
|
27
|
+
url: string;
|
|
28
|
+
title?: string;
|
|
29
|
+
description?: string;
|
|
30
|
+
site?: string;
|
|
31
|
+
image?: string;
|
|
32
|
+
}
|
|
33
|
+
export declare const LINK_PREVIEWS_ID = "artifacts-link-previews";
|
|
34
|
+
export declare function linkPreviewsEnabled(): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Title, description, site name, and image from a page's <head>: Open Graph
|
|
37
|
+
* first, then Twitter cards, then the plain <title> and meta description.
|
|
38
|
+
* Only the head is scanned; parsing stops at <body>.
|
|
39
|
+
*/
|
|
40
|
+
export declare function parsePreviewMetadata(html: string, pageUrl: string): Omit<LinkPreview, 'url'>;
|
|
41
|
+
/**
|
|
42
|
+
* True only for a globally routable unicast address. Loopback, private
|
|
43
|
+
* (RFC 1918, ULA), link-local (including the cloud metadata range),
|
|
44
|
+
* carrier-grade NAT, unspecified, multicast, and reserved ranges are all
|
|
45
|
+
* refused, in both IPv4 and IPv6 (mapped and NAT64 forms are unwrapped first).
|
|
46
|
+
*/
|
|
47
|
+
export declare function isPublicAddress(address: string): boolean;
|
|
48
|
+
/** A hostname is public when every address it resolves to is; a literal IP is checked directly. */
|
|
49
|
+
export declare function hostIsPublic(hostname: string): Promise<boolean>;
|
|
50
|
+
export type PreviewFetcher = (url: string, init: RequestInit) => Promise<Response>;
|
|
51
|
+
export interface FetchPreviewOptions {
|
|
52
|
+
fetcher?: PreviewFetcher;
|
|
53
|
+
now?: () => number;
|
|
54
|
+
version?: string;
|
|
55
|
+
/** Fetch loopback and private-network hosts too (default: ARTIFACTS_LINK_PREVIEWS_ALLOW_PRIVATE=1). */
|
|
56
|
+
allowPrivateHosts?: boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Previews for the given URLs, keyed by URL. Entries with neither a title nor
|
|
60
|
+
* a description are omitted — a card with nothing on it is worse than a plain
|
|
61
|
+
* link. Never throws: a network problem means fewer previews, not a failed render.
|
|
62
|
+
*/
|
|
63
|
+
export declare function fetchLinkPreviews(urls: string[], options?: FetchPreviewOptions): Promise<Record<string, LinkPreview>>;
|
|
64
|
+
/** The embedded JSON block; `<` is escaped so fetched text can never close the script. */
|
|
65
|
+
export declare function linkPreviewsJson(previews: Record<string, LinkPreview>): string;
|
|
66
|
+
export declare const LINK_PREVIEW_CSS: string;
|
|
67
|
+
export declare const LINK_PREVIEW_SCRIPT = "<script>\n(() => {\n const node = document.getElementById('artifacts-link-previews');\n if (!node) return;\n let previews;\n try { previews = JSON.parse(node.textContent || '{}'); } catch { return; }\n const card = document.createElement('div');\n card.className = 'link-preview';\n card.setAttribute('role', 'dialog');\n card.setAttribute('aria-label', 'Link preview');\n card.hidden = true;\n document.body.appendChild(card);\n let current = null;\n const hide = () => { card.hidden = true; current = null; };\n const text = (className, tag, value) => {\n const el = document.createElement(tag);\n el.className = className;\n el.textContent = value;\n return el;\n };\n const show = (pill, preview) => {\n card.replaceChildren();\n const site = document.createElement('div');\n site.className = 'link-preview-site';\n const icon = pill.querySelector('.link-pill-icon, .link-pill-favicon');\n if (icon) site.appendChild(icon.cloneNode(true));\n let host = '';\n try { host = new URL(preview.url).hostname.replace(/^www\\./, ''); } catch {}\n site.appendChild(document.createTextNode(preview.site && preview.site.toLowerCase() !== host ? preview.site + ' \u00B7 ' + host : host));\n card.appendChild(site);\n if (preview.title) card.appendChild(text('link-preview-title', 'div', preview.title));\n if (preview.description) card.appendChild(text('link-preview-desc', 'p', preview.description));\n if (preview.image) {\n const img = document.createElement('img');\n img.className = 'link-preview-image';\n img.alt = '';\n img.loading = 'lazy';\n img.decoding = 'async';\n img.referrerPolicy = 'no-referrer';\n img.addEventListener('error', () => img.remove());\n img.src = preview.image;\n card.appendChild(img);\n }\n const open = document.createElement('a');\n open.className = 'link-preview-open';\n open.href = preview.url;\n open.target = '_blank';\n open.rel = 'noopener noreferrer';\n open.textContent = 'Open ' + host + ' \\u2197';\n card.appendChild(open);\n card.hidden = false;\n const rect = pill.getBoundingClientRect();\n const width = card.offsetWidth;\n const left = Math.max(12, Math.min(rect.left + window.scrollX, window.scrollX + document.documentElement.clientWidth - width - 12));\n card.style.left = left + 'px';\n card.style.top = (rect.bottom + window.scrollY + 8) + 'px';\n current = pill;\n open.focus({ preventScroll: true });\n };\n document.addEventListener('click', (event) => {\n const pill = event.target instanceof Element ? event.target.closest('a.link-pill') : null;\n if (pill) {\n if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || event.button !== 0) return;\n const preview = previews[pill.getAttribute('href') || ''];\n if (!preview) return;\n event.preventDefault();\n if (current === pill && !card.hidden) { hide(); return; }\n show(pill, preview);\n return;\n }\n if (!card.hidden && !card.contains(event.target)) hide();\n });\n document.addEventListener('keydown', (event) => {\n if (event.key === 'Escape' && !card.hidden) { hide(); if (current) current.focus(); }\n });\n window.addEventListener('resize', hide);\n})();\n</script>";
|