Ask before opening a shared item in a message
The share address takes a plain form POST, which any website can make, and the app opened whatever arrived straight into a composer. It now shows what was shared -- the title, the start of the text and link, and the file names -- and opens a message only when the reader chooses to. Discarding drops it. Confirm dialogs now put a message that is not plain text in a div, since the summary has blocks of its own. Three new strings, translated in all nine catalogs.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { collectShare, shareBody, SHARE_MAX_AGE_MS } from "@/lib/shareTarget";
|
||||
import { shareSummary, collectShare, shareBody, SHARE_MAX_AGE_MS } from "@/lib/shareTarget";
|
||||
import { SW_CACHE_NAME } from "@/lib/sw/swCache";
|
||||
|
||||
/**
|
||||
@@ -112,3 +112,19 @@ describe("the body a share turns into", () => {
|
||||
expect(shareBody({ text: "a thought", url: "" })).toBe("a thought");
|
||||
});
|
||||
});
|
||||
|
||||
describe("shareSummary", () => {
|
||||
const file = (name: string) => new File(["x"], name);
|
||||
it("gives the title, the text and link together, and the file names", () => {
|
||||
expect(shareSummary({ title: " Trip ", text: "See this", url: "https://example.com", files: [file("a.jpg")] })).toEqual({
|
||||
title: "Trip",
|
||||
preview: "See this https://example.com",
|
||||
files: ["a.jpg"],
|
||||
});
|
||||
});
|
||||
it("shortens a long text rather than showing all of it", () => {
|
||||
const { preview } = shareSummary({ title: "", text: "word ".repeat(100), url: "", files: [] });
|
||||
expect(preview.length).toBeLessThanOrEqual(160);
|
||||
expect(preview.endsWith("…")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -117,3 +117,17 @@ export function shareBody(share: Pick<SharedContent, "text" | "url">): string {
|
||||
if (!url || text.includes(url)) return text;
|
||||
return text ? `${text}\n\n${url}` : url;
|
||||
}
|
||||
|
||||
/**
|
||||
* What a share holds, in the few words the confirmation shows.
|
||||
*
|
||||
* Only what the reader needs to recognize it as theirs: the title, the start
|
||||
* of the text or link, and the names of the files. It is shown before any of
|
||||
* it goes near a message, because the page cannot tell a share the reader
|
||||
* made from one a website posted at the same address (#375 review).
|
||||
*/
|
||||
export function shareSummary(share: SharedContent): { title: string; preview: string; files: string[] } {
|
||||
const body = [share.text, share.url].map((s) => s.trim()).filter(Boolean).join(" ");
|
||||
const preview = body.length > 160 ? `${body.slice(0, 157).trimEnd()}…` : body;
|
||||
return { title: share.title.trim(), preview, files: share.files.map((f) => f.name) };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user