@jant/core 1.0.1 → 1.1.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/dist/{app-Gt7_skt6.js → app-CKQ9PqAr.js} +791 -89
- package/dist/client/.vite/manifest.json +115 -75
- package/dist/client/_assets/chunks/{confirm-D7GrwsCN.js → confirm-CMe01lne.js} +1 -1
- package/dist/client/_assets/chunks/{create-editor-5Y49x7h5.js → create-editor-BQkrw8Ax.js} +4 -4
- package/dist/client/_assets/chunks/footnote-rail-B08scyhW.js +2 -0
- package/dist/client/_assets/chunks/{jant-locale-picker-DL6mIOT5.js → jant-locale-picker-2L4rkDaz.js} +1 -1
- package/dist/client/_assets/chunks/lit-Bw_voPoW.js +2 -0
- package/dist/client/_assets/chunks/preload-helper-Czpn1I53.js +1 -0
- package/dist/client/_assets/chunks/runtime-paths-y0_gHowM.js +1 -0
- package/dist/client/_assets/chunks/{settings-paths-h6RjIe3C.js → settings-paths-B0bNUaih.js} +1 -1
- package/dist/client/_assets/chunks/slug-format-CYmQE5ln.js +1 -0
- package/dist/client/_assets/chunks/{sortable-list-DWNKv782.js → sortable-list-t11Ma1Ek.js} +1 -1
- package/dist/client/_assets/chunks/{unsafe-html-B5ZEXaxg.js → unsafe-html-CWos3v6N.js} +1 -1
- package/dist/client/_assets/chunks/unsafe-svg-DZl-yjas.js +1 -0
- package/dist/client/_assets/client-B0-s9sqx.css +2 -0
- package/dist/client/_assets/client-Dp46QSht.js +354 -0
- package/dist/client/_assets/client-auth-CG55rWb5.js +859 -0
- package/dist/client/_assets/{client-compose-oxzPb3SS.js → client-compose-DkUTuavR.js} +261 -261
- package/dist/client/_assets/{client-manage-C1eDB1Rc.js → client-manage-DnYdQAhf.js} +176 -176
- package/dist/client/_assets/{client-settings-DIM44Zqh.js → client-settings-CHgsIqXJ.js} +166 -166
- package/dist/client/_assets/client-share-0DICJjAc.css +1 -0
- package/dist/client/_assets/client-share-C59mOLJ1.js +149 -0
- package/dist/index.js +1 -1
- package/dist/node.js +2 -2
- package/package.json +2 -1
- package/src/client/components/jant-post-menu.ts +80 -13
- package/src/client/components/jant-share-dialog.ts +422 -0
- package/src/client/components/jant-share-menu.ts +188 -0
- package/src/client/lazy-entries.ts +21 -1
- package/src/client/post-menu-shared.ts +100 -0
- package/src/client/share-capture.ts +443 -0
- package/src/client/share-launch.ts +133 -0
- package/src/client-share.ts +8 -0
- package/src/client.ts +1 -0
- package/src/lib/discover.ts +31 -0
- package/src/lib/share-card.ts +653 -0
- package/src/lib/version.ts +2 -0
- package/src/routes/auth/setup.tsx +37 -1
- package/src/routes/pages/partials.tsx +23 -0
- package/src/style-share.css +16 -0
- package/src/styles/components.css +14 -0
- package/src/styles/share.css +1103 -0
- package/src/styles/ui.css +6 -0
- package/src/ui/layouts/BaseLayout.tsx +7 -0
- package/src/ui/layouts/SiteLayout.tsx +1 -0
- package/src/ui/share/ShareCard.tsx +298 -0
- package/src/ui/shared/Icon.tsx +23 -3
- package/src/ui/shared/MediaGallery.tsx +12 -1
- package/src/ui/shared/PostFooter.tsx +20 -4
- package/src/ui/shared/icon-collector.ts +28 -0
- package/src/ui/shared/post-article-attributes.ts +3 -0
- package/dist/client/_assets/chunks/footnote-rail-BJwswtVQ.js +0 -1
- package/dist/client/_assets/chunks/lit-DW8VmJAT.js +0 -2
- package/dist/client/_assets/chunks/slug-format-Dsi0e9Xn.js +0 -1
- package/dist/client/_assets/chunks/unsafe-svg-DTZZEvLU.js +0 -2
- package/dist/client/_assets/client-BaqmpPQ-.css +0 -2
- package/dist/client/_assets/client-C6melOtG.js +0 -296
- package/dist/client/_assets/client-auth-Ccq8kSqq.js +0 -828
package/dist/index.js
CHANGED
package/dist/node.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A as getTelegramWebhookSecret, B as isAssetPath, C as getConfiguredSingleSitePathPrefix, D as getPort, E as getHostedControlPlaneBaseUrl, F as getFontThemeCssVariables, I as resolveCjkFontProfile, L as buildThemeStyle, M as now, N as BUILTIN_FONT_THEMES, O as getSiteResolutionMode, P as getCjkFontCssVariables, R as BUILTIN_COLOR_THEMES, S as getConfiguredSingleSiteOrigin, T as getEnvString, _ as resolveConfig, a as createNodeCliRuntime, b as setWebhook, c as createSiteService, d as createExportService, f as createStorageDriver, g as schema_exports, h as createNodeDatabase, i as listPendingMigrations, j as shouldTrustProxy, k as getTelegramBotPool, l as getHostBasedStartupConfigurationIssues, m as sqliteSchemaBundle, n as createApp, o as createNodeRequestRuntime, p as pgSchemaBundle, r as PendingMigrationsError, s as setUpNodeInstance, u as resolveDatabaseDialect, v as getWebhookUrl, w as getConfiguredStorageDriver, x as getEnvValueIssues, y as setMyCommands, z as getPublicAssetBasePath } from "./app-
|
|
1
|
+
import { A as getTelegramWebhookSecret, B as isAssetPath, C as getConfiguredSingleSitePathPrefix, D as getPort, E as getHostedControlPlaneBaseUrl, F as getFontThemeCssVariables, I as resolveCjkFontProfile, L as buildThemeStyle, M as now, N as BUILTIN_FONT_THEMES, O as getSiteResolutionMode, P as getCjkFontCssVariables, R as BUILTIN_COLOR_THEMES, S as getConfiguredSingleSiteOrigin, T as getEnvString, _ as resolveConfig, a as createNodeCliRuntime, b as setWebhook, c as createSiteService, d as createExportService, f as createStorageDriver, g as schema_exports, h as createNodeDatabase, i as listPendingMigrations, j as shouldTrustProxy, k as getTelegramBotPool, l as getHostBasedStartupConfigurationIssues, m as sqliteSchemaBundle, n as createApp, o as createNodeRequestRuntime, p as pgSchemaBundle, r as PendingMigrationsError, s as setUpNodeInstance, u as resolveDatabaseDialect, v as getWebhookUrl, w as getConfiguredStorageDriver, x as getEnvValueIssues, y as setMyCommands, z as getPublicAssetBasePath } from "./app-CKQ9PqAr.js";
|
|
2
2
|
import { drizzle } from "drizzle-orm/better-sqlite3";
|
|
3
3
|
import { serve } from "@hono/node-server";
|
|
4
4
|
import Database from "better-sqlite3";
|
|
@@ -626,7 +626,7 @@ async function createNodeRequestHandler(options) {
|
|
|
626
626
|
async function start(env = process.env, app, options = {}) {
|
|
627
627
|
const handler = await createNodeRequestHandler({
|
|
628
628
|
env,
|
|
629
|
-
app: async () => app ?? (await import("./app-
|
|
629
|
+
app: async () => app ?? (await import("./app-CKQ9PqAr.js").then((n) => n.t)).createApp(),
|
|
630
630
|
assetRoot: options.assetRoot
|
|
631
631
|
});
|
|
632
632
|
const hostname = resolveHost(env);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jant/core",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A modern, open-source microblogging platform built on Cloudflare Workers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -89,6 +89,7 @@
|
|
|
89
89
|
"@types/pg": "^8.20.0",
|
|
90
90
|
"@types/yauzl": "^3.4.0",
|
|
91
91
|
"@types/yazl": "^3.3.1",
|
|
92
|
+
"@zumer/snapdom": "^3.4.0",
|
|
92
93
|
"basecoat-css": "^0.3.11",
|
|
93
94
|
"cn-font-split": "^7.4.1",
|
|
94
95
|
"drizzle-kit": "^0.31.10",
|
|
@@ -22,6 +22,13 @@ import { readErrorMessage } from "../json.js";
|
|
|
22
22
|
import { publicPath } from "../runtime-paths.js";
|
|
23
23
|
import { looksLikeAddress } from "../../lib/url.js";
|
|
24
24
|
import { pickPost } from "../post-picker.js";
|
|
25
|
+
import { openShareImage, warmShareImage } from "../share-launch.js";
|
|
26
|
+
import {
|
|
27
|
+
copyPostLink,
|
|
28
|
+
iconImage,
|
|
29
|
+
iconLink,
|
|
30
|
+
menuOpensAbove,
|
|
31
|
+
} from "../post-menu-shared.js";
|
|
25
32
|
import {
|
|
26
33
|
ensureComposeDialog,
|
|
27
34
|
readComposeDialogLabels,
|
|
@@ -45,6 +52,8 @@ interface PostMenuData {
|
|
|
45
52
|
isDraft: boolean;
|
|
46
53
|
/** Content language of the Thread, when the site publishes more than one. */
|
|
47
54
|
language: string | null;
|
|
55
|
+
/** Where the post's date links to, for Copy link. */
|
|
56
|
+
permalink: string | null;
|
|
48
57
|
}
|
|
49
58
|
|
|
50
59
|
/** One language the site publishes. */
|
|
@@ -115,19 +124,7 @@ type PostMenuView =
|
|
|
115
124
|
const LANGUAGE_PANEL_FOCUS =
|
|
116
125
|
"[data-post-menu-translation] a, [data-post-menu-translation-first], [data-post-menu-open-language-switch]";
|
|
117
126
|
|
|
118
|
-
interface MenuTriggerRect {
|
|
119
|
-
top: number;
|
|
120
|
-
bottom: number;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
127
|
const ESTIMATED_MENU_HEIGHT = 360;
|
|
124
|
-
const MENU_VIEWPORT_MARGIN = 12;
|
|
125
|
-
|
|
126
|
-
function getMenuPlacement(rect: MenuTriggerRect) {
|
|
127
|
-
const spaceBelow = window.innerHeight - rect.bottom - MENU_VIEWPORT_MARGIN;
|
|
128
|
-
const spaceAbove = rect.top - MENU_VIEWPORT_MARGIN;
|
|
129
|
-
return spaceBelow < ESTIMATED_MENU_HEIGHT && spaceAbove > spaceBelow;
|
|
130
|
-
}
|
|
131
128
|
|
|
132
129
|
export function removeLeadingFeedDivider(
|
|
133
130
|
feedContainer: HTMLElement | null | undefined,
|
|
@@ -143,6 +140,14 @@ export function removeLeadingFeedDivider(
|
|
|
143
140
|
firstDivider?.remove();
|
|
144
141
|
}
|
|
145
142
|
|
|
143
|
+
/**
|
|
144
|
+
* The image carries a QR code to the post, which a reader can open only once
|
|
145
|
+
* it is published and not Private.
|
|
146
|
+
*/
|
|
147
|
+
function canShareAsImage(data: PostMenuData): boolean {
|
|
148
|
+
return !data.isDraft && data.visibility !== "private";
|
|
149
|
+
}
|
|
150
|
+
|
|
146
151
|
function findThreadRootArticle(threadId: string): HTMLElement | null {
|
|
147
152
|
return (
|
|
148
153
|
Array.from(
|
|
@@ -171,6 +176,7 @@ function readPostMenuData(article: HTMLElement): PostMenuData | null {
|
|
|
171
176
|
isDraft: article.hasAttribute("data-post-draft"),
|
|
172
177
|
// Carried on the Thread root, so a reply reads it from there.
|
|
173
178
|
language: threadRootArticle.dataset.postLanguage ?? null,
|
|
179
|
+
permalink: article.dataset.postPermalink ?? null,
|
|
174
180
|
};
|
|
175
181
|
}
|
|
176
182
|
|
|
@@ -264,7 +270,7 @@ export class JantPostMenu extends LitElement {
|
|
|
264
270
|
}
|
|
265
271
|
|
|
266
272
|
const rect = trigger.getBoundingClientRect();
|
|
267
|
-
this._openAbove =
|
|
273
|
+
this._openAbove = menuOpensAbove(rect, ESTIMATED_MENU_HEIGHT);
|
|
268
274
|
this._x = window.scrollX + rect.right;
|
|
269
275
|
this._y =
|
|
270
276
|
window.scrollY + (this._openAbove ? rect.top - 6 : rect.bottom + 6);
|
|
@@ -379,6 +385,7 @@ export class JantPostMenu extends LitElement {
|
|
|
379
385
|
this._view = "menu";
|
|
380
386
|
this._open = true;
|
|
381
387
|
this.#focusAfterUpdate("[data-post-menu-item-primary]");
|
|
388
|
+
if (canShareAsImage(menuData)) warmShareImage();
|
|
382
389
|
return;
|
|
383
390
|
}
|
|
384
391
|
|
|
@@ -1022,6 +1029,20 @@ export class JantPostMenu extends LitElement {
|
|
|
1022
1029
|
}
|
|
1023
1030
|
}
|
|
1024
1031
|
|
|
1032
|
+
#copyLink() {
|
|
1033
|
+
const permalink = this._data?.permalink;
|
|
1034
|
+
this.#close();
|
|
1035
|
+
if (permalink) void copyPostLink(permalink);
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
#shareImage() {
|
|
1039
|
+
if (!this._data) return;
|
|
1040
|
+
const postId = this._data.id;
|
|
1041
|
+
const trigger = this._triggerEl;
|
|
1042
|
+
this.#close({ restoreFocus: false });
|
|
1043
|
+
void openShareImage(postId, trigger);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1025
1046
|
async #publish() {
|
|
1026
1047
|
if (!this._data) return;
|
|
1027
1048
|
const postId = this._data.id;
|
|
@@ -2283,6 +2304,9 @@ export class JantPostMenu extends LitElement {
|
|
|
2283
2304
|
"[data-collection-id]",
|
|
2284
2305
|
)?.dataset.collectionId;
|
|
2285
2306
|
const isFeatured = this._data.featured;
|
|
2307
|
+
// A draft has no address to pass on yet.
|
|
2308
|
+
const canCopyLink = !this._data.isDraft && !!this._data.permalink;
|
|
2309
|
+
const canShare = canShareAsImage(this._data);
|
|
2286
2310
|
|
|
2287
2311
|
return html`
|
|
2288
2312
|
<div class="post-menu-view post-menu-main">
|
|
@@ -2459,6 +2483,49 @@ export class JantPostMenu extends LitElement {
|
|
|
2459
2483
|
}
|
|
2460
2484
|
</div>
|
|
2461
2485
|
|
|
2486
|
+
${
|
|
2487
|
+
canCopyLink || canShare
|
|
2488
|
+
? html`
|
|
2489
|
+
<div class="post-menu-section">
|
|
2490
|
+
${
|
|
2491
|
+
canCopyLink
|
|
2492
|
+
? html`<button
|
|
2493
|
+
type="button"
|
|
2494
|
+
role="menuitem"
|
|
2495
|
+
class="post-menu-item"
|
|
2496
|
+
data-post-menu-copy-link
|
|
2497
|
+
@click=${() => this.#copyLink()}
|
|
2498
|
+
>
|
|
2499
|
+
<span class="post-menu-item-label">Copy link</span>
|
|
2500
|
+
<span class="post-menu-item-trailing"
|
|
2501
|
+
>${iconLink()}</span
|
|
2502
|
+
>
|
|
2503
|
+
</button>`
|
|
2504
|
+
: nothing
|
|
2505
|
+
}
|
|
2506
|
+
${
|
|
2507
|
+
canShare
|
|
2508
|
+
? html`<button
|
|
2509
|
+
type="button"
|
|
2510
|
+
role="menuitem"
|
|
2511
|
+
class="post-menu-item"
|
|
2512
|
+
data-post-menu-share-image
|
|
2513
|
+
@click=${() => this.#shareImage()}
|
|
2514
|
+
>
|
|
2515
|
+
<span class="post-menu-item-label"
|
|
2516
|
+
>Share as image</span
|
|
2517
|
+
>
|
|
2518
|
+
<span class="post-menu-item-trailing"
|
|
2519
|
+
>${iconImage()}</span
|
|
2520
|
+
>
|
|
2521
|
+
</button>`
|
|
2522
|
+
: nothing
|
|
2523
|
+
}
|
|
2524
|
+
</div>
|
|
2525
|
+
`
|
|
2526
|
+
: nothing
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2462
2529
|
<div class="post-menu-section post-menu-section-danger">
|
|
2463
2530
|
<button
|
|
2464
2531
|
type="button"
|
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Share Dialog
|
|
3
|
+
*
|
|
4
|
+
* "Share as image" from the post menu: lays out the post's share card,
|
|
5
|
+
* paints it as a PNG (`share-capture.ts`), and offers the image to download,
|
|
6
|
+
* hand to the system share sheet, or copy. On a phone the image itself is
|
|
7
|
+
* the save button — press and hold.
|
|
8
|
+
*
|
|
9
|
+
* The card is on screen before the picture is: once it is laid out, the
|
|
10
|
+
* dialog shows it faded, as a print still developing, and the finished PNG
|
|
11
|
+
* takes its place at full strength. The two are the same pixels, so the
|
|
12
|
+
* swap shows only as the card coming up to strength.
|
|
13
|
+
*
|
|
14
|
+
* Light DOM only — BaseCoat and the author stylesheet apply directly.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { LitElement, html, nothing, svg } from "lit";
|
|
18
|
+
import { unsafeHTML } from "lit/directives/unsafe-html.js";
|
|
19
|
+
import {
|
|
20
|
+
prepareShareCard,
|
|
21
|
+
type PreparedShareCard,
|
|
22
|
+
type ShareImage,
|
|
23
|
+
} from "../share-capture.js";
|
|
24
|
+
import { showToast } from "../toast.js";
|
|
25
|
+
|
|
26
|
+
type ShareStatus = "loading" | "developing" | "ready" | "error";
|
|
27
|
+
|
|
28
|
+
function pngFile(blob: Blob, fileName: string): File {
|
|
29
|
+
return new File([blob], fileName, { type: "image/png" });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Whether the system share sheet takes a PNG as a file. */
|
|
33
|
+
function canShareImage(): boolean {
|
|
34
|
+
try {
|
|
35
|
+
return (
|
|
36
|
+
navigator.canShare?.({ files: [pngFile(new Blob(), "post.png")] }) ??
|
|
37
|
+
false
|
|
38
|
+
);
|
|
39
|
+
} catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function canCopyImage(): boolean {
|
|
45
|
+
return (
|
|
46
|
+
globalThis.isSecureContext &&
|
|
47
|
+
typeof globalThis.ClipboardItem === "function" &&
|
|
48
|
+
typeof navigator.clipboard?.write === "function"
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A Lucide icon for a button, in the post menu's stroke. */
|
|
53
|
+
function icon(paths: ReturnType<typeof svg>) {
|
|
54
|
+
return html`<svg
|
|
55
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
56
|
+
viewBox="0 0 24 24"
|
|
57
|
+
fill="none"
|
|
58
|
+
stroke="currentColor"
|
|
59
|
+
stroke-width="1.75"
|
|
60
|
+
stroke-linecap="round"
|
|
61
|
+
stroke-linejoin="round"
|
|
62
|
+
aria-hidden="true"
|
|
63
|
+
>
|
|
64
|
+
${paths}
|
|
65
|
+
</svg>`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const ICON_SHARE = svg`<path d="M12 2v13" /><path d="m16 6-4-4-4 4" /><path d="M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8" />`;
|
|
69
|
+
const ICON_COPY = svg`<rect width="14" height="14" x="8" y="8" rx="2" ry="2" /><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />`;
|
|
70
|
+
const ICON_DOWNLOAD = svg`<path d="M12 15V3" /><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" /><path d="m7 10 5 5 5-5" />`;
|
|
71
|
+
const ICON_CLOSE = svg`<path d="M18 6 6 18" /><path d="m6 6 12 12" />`;
|
|
72
|
+
|
|
73
|
+
/** Resolves once the image at `url` is decoded and can show at once. */
|
|
74
|
+
async function decoded(url: string): Promise<void> {
|
|
75
|
+
const image = new Image();
|
|
76
|
+
image.src = url;
|
|
77
|
+
await image.decode();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export class JantShareDialog extends LitElement {
|
|
81
|
+
static properties = {
|
|
82
|
+
_open: { state: true },
|
|
83
|
+
_status: { state: true },
|
|
84
|
+
_card: { state: true },
|
|
85
|
+
_imageUrl: { state: true },
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
declare _open: boolean;
|
|
89
|
+
declare _status: ShareStatus;
|
|
90
|
+
/** The laid-out card, shown while its picture is painted. */
|
|
91
|
+
declare _card: { html: string; width: number; height: number } | null;
|
|
92
|
+
declare _imageUrl: string;
|
|
93
|
+
|
|
94
|
+
#image: ShareImage | null = null;
|
|
95
|
+
#prepared: PreparedShareCard | null = null;
|
|
96
|
+
#loadCard: () => Promise<string> = () => Promise.reject(new Error());
|
|
97
|
+
#returnFocus: HTMLElement | null = null;
|
|
98
|
+
/** Bumped per generation, so a capture that finishes late is dropped. */
|
|
99
|
+
#generation = 0;
|
|
100
|
+
|
|
101
|
+
constructor() {
|
|
102
|
+
super();
|
|
103
|
+
this._open = false;
|
|
104
|
+
this._status = "loading";
|
|
105
|
+
this._card = null;
|
|
106
|
+
this._imageUrl = "";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
createRenderRoot() {
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
disconnectedCallback() {
|
|
114
|
+
super.disconnectedCallback();
|
|
115
|
+
window.removeEventListener("resize", this.#fitCard);
|
|
116
|
+
this.#reset();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
updated() {
|
|
120
|
+
this.#fitCard();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Open the dialog and start painting the post.
|
|
125
|
+
*
|
|
126
|
+
* @param loadCard - Fetches the card's markup; called again by "Try again"
|
|
127
|
+
* @param returnFocus - Element to focus again on close
|
|
128
|
+
*/
|
|
129
|
+
async open(
|
|
130
|
+
loadCard: () => Promise<string>,
|
|
131
|
+
returnFocus: HTMLElement | null,
|
|
132
|
+
): Promise<void> {
|
|
133
|
+
this.#loadCard = loadCard;
|
|
134
|
+
this.#returnFocus = returnFocus;
|
|
135
|
+
this._open = true;
|
|
136
|
+
document.body.style.overflow = "hidden";
|
|
137
|
+
window.addEventListener("resize", this.#fitCard);
|
|
138
|
+
await this.updateComplete;
|
|
139
|
+
this.querySelector<HTMLDialogElement>(".share-dialog")?.showModal();
|
|
140
|
+
this.querySelector<HTMLElement>(".share-dialog-panel")?.focus();
|
|
141
|
+
await this.#generate();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async #generate(): Promise<void> {
|
|
145
|
+
const generation = ++this.#generation;
|
|
146
|
+
const current = () => generation === this.#generation && this._open;
|
|
147
|
+
this.#reset();
|
|
148
|
+
this._status = "loading";
|
|
149
|
+
try {
|
|
150
|
+
const prepared = await prepareShareCard(await this.#loadCard());
|
|
151
|
+
if (!current()) {
|
|
152
|
+
prepared.dispose();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
this.#prepared = prepared;
|
|
156
|
+
this._card = {
|
|
157
|
+
html: prepared.html,
|
|
158
|
+
width: prepared.width,
|
|
159
|
+
height: prepared.height,
|
|
160
|
+
};
|
|
161
|
+
this._status = "developing";
|
|
162
|
+
|
|
163
|
+
const image = await prepared.capture();
|
|
164
|
+
this.#prepared = null;
|
|
165
|
+
if (!current()) return;
|
|
166
|
+
const url = URL.createObjectURL(image.blob);
|
|
167
|
+
// Swapped in only once it can paint, so the card never blinks out.
|
|
168
|
+
await decoded(url).catch(() => {});
|
|
169
|
+
if (!current()) {
|
|
170
|
+
URL.revokeObjectURL(url);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
this.#image = image;
|
|
174
|
+
this._imageUrl = url;
|
|
175
|
+
this._status = "ready";
|
|
176
|
+
} catch {
|
|
177
|
+
if (!current()) return;
|
|
178
|
+
this.#reset();
|
|
179
|
+
this._status = "error";
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Drop the card and the image, and anything still laid out off screen. */
|
|
184
|
+
#reset(): void {
|
|
185
|
+
this.#prepared?.dispose();
|
|
186
|
+
this.#prepared = null;
|
|
187
|
+
if (this._imageUrl) URL.revokeObjectURL(this._imageUrl);
|
|
188
|
+
this._imageUrl = "";
|
|
189
|
+
this._card = null;
|
|
190
|
+
this.#image = null;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Scale the laid-out card, 540 px wide, to the preview's width. */
|
|
194
|
+
#fitCard = () => {
|
|
195
|
+
const print = this.querySelector<HTMLElement>(".share-dialog-print");
|
|
196
|
+
const card = print?.querySelector<HTMLElement>(".share-dialog-card");
|
|
197
|
+
if (!print || !card || !this._card) return;
|
|
198
|
+
card.style.transform = `scale(${print.clientWidth / this._card.width})`;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
#close(): void {
|
|
202
|
+
this.#generation++;
|
|
203
|
+
this.querySelector<HTMLDialogElement>(".share-dialog")?.close();
|
|
204
|
+
document.body.style.overflow = "";
|
|
205
|
+
window.removeEventListener("resize", this.#fitCard);
|
|
206
|
+
this._open = false;
|
|
207
|
+
this.#reset();
|
|
208
|
+
const target = this.#returnFocus;
|
|
209
|
+
this.#returnFocus = null;
|
|
210
|
+
queueMicrotask(() => {
|
|
211
|
+
if (target?.isConnected) target.focus();
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
#download(): void {
|
|
216
|
+
if (!this.#image || !this._imageUrl) return;
|
|
217
|
+
const link = document.createElement("a");
|
|
218
|
+
link.href = this._imageUrl;
|
|
219
|
+
link.download = this.#image.fileName;
|
|
220
|
+
document.body.appendChild(link);
|
|
221
|
+
link.click();
|
|
222
|
+
link.remove();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async #share(): Promise<void> {
|
|
226
|
+
if (!this.#image) return;
|
|
227
|
+
try {
|
|
228
|
+
await navigator.share({
|
|
229
|
+
files: [pngFile(this.#image.blob, this.#image.fileName)],
|
|
230
|
+
});
|
|
231
|
+
} catch (error) {
|
|
232
|
+
if (
|
|
233
|
+
error instanceof globalThis.DOMException &&
|
|
234
|
+
error.name === "AbortError"
|
|
235
|
+
) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
showToast("Could not share the image. Download it instead.", "error");
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async #copy(): Promise<void> {
|
|
243
|
+
if (!this.#image) return;
|
|
244
|
+
try {
|
|
245
|
+
await navigator.clipboard.write([
|
|
246
|
+
new globalThis.ClipboardItem({ "image/png": this.#image.blob }),
|
|
247
|
+
]);
|
|
248
|
+
showToast("Image copied.");
|
|
249
|
+
} catch {
|
|
250
|
+
showToast("Could not copy the image. Download it instead.", "error");
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#handleKeydown = (event: KeyboardEvent) => {
|
|
255
|
+
if (event.key === "Escape") {
|
|
256
|
+
event.preventDefault();
|
|
257
|
+
event.stopPropagation();
|
|
258
|
+
this.#close();
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
#renderPrint() {
|
|
263
|
+
const card = this._card;
|
|
264
|
+
const content =
|
|
265
|
+
this._status === "ready"
|
|
266
|
+
? html`<img
|
|
267
|
+
class="share-dialog-image"
|
|
268
|
+
src=${this._imageUrl}
|
|
269
|
+
alt="This post as an image"
|
|
270
|
+
/>`
|
|
271
|
+
: card
|
|
272
|
+
? html`<div class="share-dialog-card" inert>
|
|
273
|
+
${unsafeHTML(card.html)}
|
|
274
|
+
</div>`
|
|
275
|
+
: html`<div class="share-dialog-blank"></div>`;
|
|
276
|
+
return html`
|
|
277
|
+
<div
|
|
278
|
+
class="share-dialog-print"
|
|
279
|
+
data-status=${this._status}
|
|
280
|
+
style=${card ? `aspect-ratio: ${card.width} / ${card.height}` : ""}
|
|
281
|
+
>
|
|
282
|
+
${content}
|
|
283
|
+
</div>
|
|
284
|
+
`;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
#renderPreview() {
|
|
288
|
+
if (this._status !== "error") return this.#renderPrint();
|
|
289
|
+
return html`
|
|
290
|
+
<p class="share-dialog-status">
|
|
291
|
+
Could not create the image. Check your connection and try again.
|
|
292
|
+
</p>
|
|
293
|
+
<button
|
|
294
|
+
type="button"
|
|
295
|
+
class="btn-outline"
|
|
296
|
+
@click=${() => void this.#generate()}
|
|
297
|
+
>
|
|
298
|
+
Try again
|
|
299
|
+
</button>
|
|
300
|
+
`;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
#renderNote() {
|
|
304
|
+
switch (this._status) {
|
|
305
|
+
case "loading":
|
|
306
|
+
case "developing":
|
|
307
|
+
return "Creating the image…";
|
|
308
|
+
case "ready":
|
|
309
|
+
return html`<span class="share-dialog-hint"
|
|
310
|
+
>Press and hold the image to save it.</span
|
|
311
|
+
>`;
|
|
312
|
+
case "error":
|
|
313
|
+
return nothing;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* On a phone the actions sit two to a row and the dialog closes from the
|
|
319
|
+
* bottom one, in reach of a thumb; the corner close is for wider screens.
|
|
320
|
+
*/
|
|
321
|
+
#renderActions() {
|
|
322
|
+
if (this._status === "error") return nothing;
|
|
323
|
+
const ready = this._status === "ready";
|
|
324
|
+
return html`
|
|
325
|
+
<footer class="share-dialog-actions">
|
|
326
|
+
${
|
|
327
|
+
canShareImage()
|
|
328
|
+
? html`<button
|
|
329
|
+
type="button"
|
|
330
|
+
class="btn-outline"
|
|
331
|
+
?disabled=${!ready}
|
|
332
|
+
@click=${() => void this.#share()}
|
|
333
|
+
>
|
|
334
|
+
${icon(ICON_SHARE)} Share…
|
|
335
|
+
</button>`
|
|
336
|
+
: nothing
|
|
337
|
+
}
|
|
338
|
+
${
|
|
339
|
+
canCopyImage()
|
|
340
|
+
? html`<button
|
|
341
|
+
type="button"
|
|
342
|
+
class="btn-outline"
|
|
343
|
+
?disabled=${!ready}
|
|
344
|
+
@click=${() => void this.#copy()}
|
|
345
|
+
>
|
|
346
|
+
${icon(ICON_COPY)} Copy image
|
|
347
|
+
</button>`
|
|
348
|
+
: nothing
|
|
349
|
+
}
|
|
350
|
+
<button
|
|
351
|
+
type="button"
|
|
352
|
+
class="btn-outline share-dialog-close"
|
|
353
|
+
@click=${() => this.#close()}
|
|
354
|
+
>
|
|
355
|
+
${icon(ICON_CLOSE)} Close
|
|
356
|
+
</button>
|
|
357
|
+
<button
|
|
358
|
+
type="button"
|
|
359
|
+
class="btn"
|
|
360
|
+
?disabled=${!ready}
|
|
361
|
+
@click=${() => this.#download()}
|
|
362
|
+
>
|
|
363
|
+
${icon(ICON_DOWNLOAD)} Download
|
|
364
|
+
</button>
|
|
365
|
+
</footer>
|
|
366
|
+
`;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
render() {
|
|
370
|
+
if (!this._open) return nothing;
|
|
371
|
+
return html`
|
|
372
|
+
<dialog
|
|
373
|
+
class="dialog share-dialog"
|
|
374
|
+
@cancel=${(event: Event) => {
|
|
375
|
+
event.preventDefault();
|
|
376
|
+
this.#close();
|
|
377
|
+
}}
|
|
378
|
+
@keydown=${this.#handleKeydown}
|
|
379
|
+
@click=${(event: Event) => {
|
|
380
|
+
if ((event.target as HTMLElement).tagName === "DIALOG") this.#close();
|
|
381
|
+
}}
|
|
382
|
+
>
|
|
383
|
+
<div
|
|
384
|
+
class="share-dialog-panel"
|
|
385
|
+
tabindex="-1"
|
|
386
|
+
role="document"
|
|
387
|
+
aria-labelledby="share-dialog-title"
|
|
388
|
+
>
|
|
389
|
+
<header>
|
|
390
|
+
<h2 id="share-dialog-title">Share as image</h2>
|
|
391
|
+
</header>
|
|
392
|
+
<section class="share-dialog-preview" data-status=${this._status}>
|
|
393
|
+
${this.#renderPreview()}
|
|
394
|
+
</section>
|
|
395
|
+
<p class="share-dialog-note" aria-live="polite">
|
|
396
|
+
${this.#renderNote()}
|
|
397
|
+
</p>
|
|
398
|
+
${this.#renderActions()}
|
|
399
|
+
<button
|
|
400
|
+
type="button"
|
|
401
|
+
aria-label="Close"
|
|
402
|
+
@click=${() => this.#close()}
|
|
403
|
+
>
|
|
404
|
+
<svg
|
|
405
|
+
viewBox="0 0 24 24"
|
|
406
|
+
fill="none"
|
|
407
|
+
stroke="currentColor"
|
|
408
|
+
stroke-width="2"
|
|
409
|
+
stroke-linecap="round"
|
|
410
|
+
stroke-linejoin="round"
|
|
411
|
+
aria-hidden="true"
|
|
412
|
+
>
|
|
413
|
+
<path d="M18 6 6 18M6 6l12 12" />
|
|
414
|
+
</svg>
|
|
415
|
+
</button>
|
|
416
|
+
</div>
|
|
417
|
+
</dialog>
|
|
418
|
+
`;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
customElements.define("jant-share-dialog", JantShareDialog);
|