@xsynaptic/share-kit 0.2.0 → 0.3.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/README.md +56 -6
- package/dist/index.d.mts +332 -170
- package/dist/index.mjs +656 -399
- package/dist/index.mjs.map +1 -1
- package/dist/styles.css +11 -0
- package/package.json +3 -1
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,95 @@
|
|
|
1
|
+
// src/count.ts
|
|
2
|
+
const urlPattern = /https?:\/\/[\w!#$%&'()*+,./:;=?@~-]+/giu;
|
|
3
|
+
const urlTrailingPunctuation = /[!'),.:;?]+$/u;
|
|
4
|
+
const graphemeSegmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
|
|
5
|
+
const utf8Encoder = new TextEncoder();
|
|
6
|
+
const latinCombiningMarkRanges = [
|
|
7
|
+
[768, 879],
|
|
8
|
+
[6832, 6911],
|
|
9
|
+
[7616, 7679],
|
|
10
|
+
[8400, 8447],
|
|
11
|
+
[65056, 65071]
|
|
12
|
+
];
|
|
13
|
+
function getEmojiPattern() {
|
|
14
|
+
try {
|
|
15
|
+
return new RegExp(String.raw`^\p{RGI_Emoji}$`, "v");
|
|
16
|
+
} catch {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const emojiPattern = getEmojiPattern();
|
|
21
|
+
function getGraphemes(text) {
|
|
22
|
+
return Array.from(graphemeSegmenter.segment(text), ({ segment }) => segment);
|
|
23
|
+
}
|
|
24
|
+
function isLightCodePoint(codePoint) {
|
|
25
|
+
return codePoint <= 4351 || codePoint >= 8192 && codePoint <= 8205 || codePoint >= 8208 && codePoint <= 8223 || codePoint >= 8242 && codePoint <= 8247;
|
|
26
|
+
}
|
|
27
|
+
function shareKitCountWeighted(text) {
|
|
28
|
+
const graphemes = getGraphemes(text.normalize("NFC"));
|
|
29
|
+
let total = 0;
|
|
30
|
+
for (const grapheme of graphemes) {
|
|
31
|
+
if (emojiPattern?.test(grapheme)) {
|
|
32
|
+
total += 2;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
for (const character of grapheme) total += isLightCodePoint(character.codePointAt(0) ?? 0) ? 1 : 2;
|
|
36
|
+
}
|
|
37
|
+
return total;
|
|
38
|
+
}
|
|
39
|
+
function isLatinCombiningMark(codePoint) {
|
|
40
|
+
return latinCombiningMarkRanges.some(([start, end]) => codePoint >= start && codePoint <= end);
|
|
41
|
+
}
|
|
42
|
+
function shareKitCountStringz(text) {
|
|
43
|
+
let total = 0;
|
|
44
|
+
for (const grapheme of getGraphemes(text)) {
|
|
45
|
+
if (emojiPattern?.test(grapheme)) {
|
|
46
|
+
total += 1;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
for (const character of grapheme) if (!isLatinCombiningMark(character.codePointAt(0) ?? 0)) total += 1;
|
|
50
|
+
}
|
|
51
|
+
return total;
|
|
52
|
+
}
|
|
53
|
+
function shareKitShortenedLinkCost(url) {
|
|
54
|
+
if (!URL.canParse(url)) return getGraphemes(url).length;
|
|
55
|
+
const { hash, host, pathname, search } = new URL(url);
|
|
56
|
+
const path = `${pathname === "/" ? "" : pathname}${search}${hash}`;
|
|
57
|
+
return host.length + Math.min(path.length, 16);
|
|
58
|
+
}
|
|
59
|
+
function shareKitCountLength(text) {
|
|
60
|
+
return text.length;
|
|
61
|
+
}
|
|
62
|
+
function shareKitCountGraphemes(text) {
|
|
63
|
+
return getGraphemes(text).length;
|
|
64
|
+
}
|
|
65
|
+
function shareKitCountUtf8(text) {
|
|
66
|
+
return utf8Encoder.encode(text).length;
|
|
67
|
+
}
|
|
68
|
+
function splitLinks(text) {
|
|
69
|
+
const links = [];
|
|
70
|
+
return {
|
|
71
|
+
links,
|
|
72
|
+
rest: text.replaceAll(urlPattern, (match) => {
|
|
73
|
+
const trailing = urlTrailingPunctuation.exec(match)?.[0] ?? "";
|
|
74
|
+
links.push(match.slice(0, match.length - trailing.length));
|
|
75
|
+
return trailing;
|
|
76
|
+
})
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function shareKitWithLinkCost(count, cost) {
|
|
80
|
+
return (text) => {
|
|
81
|
+
const { links, rest } = splitLinks(text);
|
|
82
|
+
let total = count(rest);
|
|
83
|
+
for (const link of links) total += typeof cost === "number" ? cost : cost(link);
|
|
84
|
+
return total;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
1
87
|
// src/actions.ts
|
|
2
88
|
const shareCapabilities = [
|
|
3
89
|
"files",
|
|
4
90
|
"link",
|
|
5
91
|
"copy-image",
|
|
6
|
-
"
|
|
92
|
+
"mobile"
|
|
7
93
|
];
|
|
8
94
|
const pngType = "image/png";
|
|
9
95
|
function canShareFiles() {
|
|
@@ -17,14 +103,19 @@ function canCopyImage() {
|
|
|
17
103
|
const capabilityTests = {
|
|
18
104
|
"copy-image": canCopyImage,
|
|
19
105
|
files: canShareFiles,
|
|
20
|
-
|
|
21
|
-
|
|
106
|
+
link: () => "share" in navigator,
|
|
107
|
+
mobile: () => matchMedia("(pointer: coarse)").matches
|
|
22
108
|
};
|
|
23
109
|
function isShareCapability(value) {
|
|
24
110
|
return shareCapabilities.some((capability) => capability === value);
|
|
25
111
|
}
|
|
26
112
|
function getShareCapabilities(preset) {
|
|
27
|
-
if (preset !== void 0)
|
|
113
|
+
if (preset !== void 0) {
|
|
114
|
+
const tokens = preset.split(/\s+/u).filter(Boolean);
|
|
115
|
+
const unknown = tokens.filter((token) => !isShareCapability(token));
|
|
116
|
+
if (unknown.length > 0) console.warn(`share-kit: unknown data-can tokens ignored: ${unknown.join(", ")}`);
|
|
117
|
+
return tokens.filter(isShareCapability);
|
|
118
|
+
}
|
|
28
119
|
return shareCapabilities.filter((capability) => capabilityTests[capability]());
|
|
29
120
|
}
|
|
30
121
|
function isDomError(error, name) {
|
|
@@ -70,6 +161,14 @@ function clearOpenHash(openHash) {
|
|
|
70
161
|
if (!isOpenHash(openHash)) return;
|
|
71
162
|
history.replaceState(history.state, "", `${location.pathname}${location.search}`);
|
|
72
163
|
}
|
|
164
|
+
function openComposer(href) {
|
|
165
|
+
const { protocol } = new URL(href, location.href);
|
|
166
|
+
if (protocol !== "http:" && protocol !== "https:") {
|
|
167
|
+
location.assign(href);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
window.open(href, "_blank", "noopener");
|
|
171
|
+
}
|
|
73
172
|
// src/assets.ts
|
|
74
173
|
function isFile(file) {
|
|
75
174
|
return file !== void 0;
|
|
@@ -138,228 +237,6 @@ var ShareAssetFiles = class {
|
|
|
138
237
|
}
|
|
139
238
|
}
|
|
140
239
|
};
|
|
141
|
-
// src/count.ts
|
|
142
|
-
const urlFixedCost = 23;
|
|
143
|
-
const urlPattern = /https?:\/\/[\w!#$%&'()*+,./:;=?@~-]+/giu;
|
|
144
|
-
const urlTrailingPunctuation = /[!'),.:;?]+$/u;
|
|
145
|
-
const graphemeSegmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
|
|
146
|
-
const utf8Encoder = new TextEncoder();
|
|
147
|
-
const latinCombiningMarkRanges = [
|
|
148
|
-
[768, 879],
|
|
149
|
-
[6832, 6911],
|
|
150
|
-
[7616, 7679],
|
|
151
|
-
[8400, 8447],
|
|
152
|
-
[65056, 65071]
|
|
153
|
-
];
|
|
154
|
-
function getEmojiPattern() {
|
|
155
|
-
try {
|
|
156
|
-
return new RegExp(String.raw`^\p{RGI_Emoji}$`, "v");
|
|
157
|
-
} catch {
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
const emojiPattern = getEmojiPattern();
|
|
162
|
-
function getGraphemes(text) {
|
|
163
|
-
return Array.from(graphemeSegmenter.segment(text), ({ segment }) => segment);
|
|
164
|
-
}
|
|
165
|
-
function isLightCodePoint(codePoint) {
|
|
166
|
-
return codePoint <= 4351 || codePoint >= 8192 && codePoint <= 8205 || codePoint >= 8208 && codePoint <= 8223 || codePoint >= 8242 && codePoint <= 8247;
|
|
167
|
-
}
|
|
168
|
-
function getWeightedLength(text) {
|
|
169
|
-
const graphemes = getGraphemes(text.normalize("NFC"));
|
|
170
|
-
let total = 0;
|
|
171
|
-
for (const grapheme of graphemes) {
|
|
172
|
-
if (emojiPattern?.test(grapheme)) {
|
|
173
|
-
total += 2;
|
|
174
|
-
continue;
|
|
175
|
-
}
|
|
176
|
-
for (const character of grapheme) total += isLightCodePoint(character.codePointAt(0) ?? 0) ? 1 : 2;
|
|
177
|
-
}
|
|
178
|
-
return total;
|
|
179
|
-
}
|
|
180
|
-
function isLatinCombiningMark(codePoint) {
|
|
181
|
-
return latinCombiningMarkRanges.some(([start, end]) => codePoint >= start && codePoint <= end);
|
|
182
|
-
}
|
|
183
|
-
function getStringzLength(text) {
|
|
184
|
-
let total = 0;
|
|
185
|
-
for (const character of text) if (!isLatinCombiningMark(character.codePointAt(0) ?? 0)) total += 1;
|
|
186
|
-
return total;
|
|
187
|
-
}
|
|
188
|
-
function getShortenedUrlLength(url) {
|
|
189
|
-
if (!URL.canParse(url)) return getGraphemes(url).length;
|
|
190
|
-
const { hash, host, pathname, search } = new URL(url);
|
|
191
|
-
const path = `${pathname === "/" ? "" : pathname}${search}${hash}`;
|
|
192
|
-
return host.length + Math.min(path.length, 16);
|
|
193
|
-
}
|
|
194
|
-
const textCounters = {
|
|
195
|
-
graphemes: (text) => getGraphemes(text).length,
|
|
196
|
-
length: (text) => text.length,
|
|
197
|
-
stringz: getStringzLength,
|
|
198
|
-
utf8: (text) => utf8Encoder.encode(text).length,
|
|
199
|
-
weighted: getWeightedLength
|
|
200
|
-
};
|
|
201
|
-
function splitLinks(text) {
|
|
202
|
-
const links = [];
|
|
203
|
-
return {
|
|
204
|
-
links,
|
|
205
|
-
rest: text.replaceAll(urlPattern, (match) => {
|
|
206
|
-
const trailing = urlTrailingPunctuation.exec(match)?.[0] ?? "";
|
|
207
|
-
links.push(match.slice(0, match.length - trailing.length));
|
|
208
|
-
return trailing;
|
|
209
|
-
})
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
function countText(text, { counter, urlCost }) {
|
|
213
|
-
const count = textCounters[counter];
|
|
214
|
-
if (urlCost === "actual") return count(text);
|
|
215
|
-
const { links, rest } = splitLinks(text);
|
|
216
|
-
let total = count(rest);
|
|
217
|
-
for (const link of links) total += urlCost === "fixed" ? urlFixedCost : getShortenedUrlLength(link);
|
|
218
|
-
return total;
|
|
219
|
-
}
|
|
220
|
-
// src/destinations.ts
|
|
221
|
-
function withQuery(base, parameters) {
|
|
222
|
-
const url = new URL(base);
|
|
223
|
-
for (const [name, value] of Object.entries(parameters)) if (value) url.searchParams.set(name, value);
|
|
224
|
-
return url.href;
|
|
225
|
-
}
|
|
226
|
-
const shareDestinations = {
|
|
227
|
-
bluesky: {
|
|
228
|
-
assetRole: "portrait",
|
|
229
|
-
counter: "graphemes",
|
|
230
|
-
getComposerUrl: ({ text }) => withQuery("https://bsky.app/intent/compose", { text }),
|
|
231
|
-
isImagePost: true,
|
|
232
|
-
limit: 300,
|
|
233
|
-
shape: "full",
|
|
234
|
-
urlCost: "shortened"
|
|
235
|
-
},
|
|
236
|
-
facebook: {
|
|
237
|
-
assetRole: "landscape",
|
|
238
|
-
counter: "length",
|
|
239
|
-
getComposerUrl: ({ url }) => withQuery("https://www.facebook.com/sharer/sharer.php", { u: url }),
|
|
240
|
-
isImagePost: false,
|
|
241
|
-
isLinkOnly: true,
|
|
242
|
-
limit: 63206,
|
|
243
|
-
shape: "full",
|
|
244
|
-
urlCost: "actual"
|
|
245
|
-
},
|
|
246
|
-
instagram: {
|
|
247
|
-
assetRole: "portrait",
|
|
248
|
-
counter: "length",
|
|
249
|
-
isImagePost: true,
|
|
250
|
-
limit: 2200,
|
|
251
|
-
shape: "text",
|
|
252
|
-
urlCost: "actual"
|
|
253
|
-
},
|
|
254
|
-
"instagram-story": {
|
|
255
|
-
assetRole: "story",
|
|
256
|
-
counter: "length",
|
|
257
|
-
isImagePost: true,
|
|
258
|
-
shape: "url",
|
|
259
|
-
urlCost: "actual"
|
|
260
|
-
},
|
|
261
|
-
linkedin: {
|
|
262
|
-
assetRole: "landscape",
|
|
263
|
-
counter: "length",
|
|
264
|
-
getComposerUrl: ({ url }) => withQuery("https://www.linkedin.com/sharing/share-offsite/", { url }),
|
|
265
|
-
isImagePost: false,
|
|
266
|
-
isLinkOnly: true,
|
|
267
|
-
limit: 3e3,
|
|
268
|
-
shape: "full",
|
|
269
|
-
urlCost: "actual"
|
|
270
|
-
},
|
|
271
|
-
mastodon: {
|
|
272
|
-
assetRole: "portrait",
|
|
273
|
-
counter: "stringz",
|
|
274
|
-
isImagePost: true,
|
|
275
|
-
limit: 500,
|
|
276
|
-
shape: "full",
|
|
277
|
-
urlCost: "fixed"
|
|
278
|
-
},
|
|
279
|
-
messages: {
|
|
280
|
-
assetRole: "landscape",
|
|
281
|
-
counter: "length",
|
|
282
|
-
isImagePost: false,
|
|
283
|
-
shape: "full",
|
|
284
|
-
urlCost: "actual"
|
|
285
|
-
},
|
|
286
|
-
pinterest: {
|
|
287
|
-
assetRole: "portrait",
|
|
288
|
-
counter: "length",
|
|
289
|
-
getComposerUrl: ({ media, text, url }) => withQuery("https://www.pinterest.com/pin/create/button/", {
|
|
290
|
-
description: text,
|
|
291
|
-
media,
|
|
292
|
-
url
|
|
293
|
-
}),
|
|
294
|
-
isImagePost: false,
|
|
295
|
-
limit: 800,
|
|
296
|
-
shape: "text",
|
|
297
|
-
urlCost: "actual"
|
|
298
|
-
},
|
|
299
|
-
reddit: {
|
|
300
|
-
assetRole: "landscape",
|
|
301
|
-
counter: "length",
|
|
302
|
-
getComposerUrl: ({ text, url }) => withQuery("https://www.reddit.com/submit", {
|
|
303
|
-
title: text,
|
|
304
|
-
url
|
|
305
|
-
}),
|
|
306
|
-
isImagePost: false,
|
|
307
|
-
limit: 300,
|
|
308
|
-
shape: "title",
|
|
309
|
-
urlCost: "actual"
|
|
310
|
-
},
|
|
311
|
-
["any"]: {
|
|
312
|
-
assetRole: "landscape",
|
|
313
|
-
counter: "length",
|
|
314
|
-
isImagePost: false,
|
|
315
|
-
shape: "full",
|
|
316
|
-
urlCost: "actual"
|
|
317
|
-
},
|
|
318
|
-
threads: {
|
|
319
|
-
assetRole: "portrait",
|
|
320
|
-
counter: "utf8",
|
|
321
|
-
getComposerUrl: ({ text }) => withQuery("https://www.threads.com/intent/post", { text }),
|
|
322
|
-
isImagePost: true,
|
|
323
|
-
limit: 500,
|
|
324
|
-
shape: "full",
|
|
325
|
-
urlCost: "actual"
|
|
326
|
-
},
|
|
327
|
-
x: {
|
|
328
|
-
assetRole: "landscape",
|
|
329
|
-
counter: "weighted",
|
|
330
|
-
getComposerUrl: ({ text }) => withQuery("https://x.com/intent/tweet", { text }),
|
|
331
|
-
isImagePost: false,
|
|
332
|
-
limit: 280,
|
|
333
|
-
shape: "full",
|
|
334
|
-
urlCost: "fixed"
|
|
335
|
-
}
|
|
336
|
-
};
|
|
337
|
-
function isShareDestinationId(id) {
|
|
338
|
-
return Object.hasOwn(shareDestinations, id);
|
|
339
|
-
}
|
|
340
|
-
function getShareDestination(id) {
|
|
341
|
-
return shareDestinations[id];
|
|
342
|
-
}
|
|
343
|
-
function getDestinationUrl(url, id, utmMedium) {
|
|
344
|
-
if (!utmMedium || id === "any" || !URL.canParse(url)) return url;
|
|
345
|
-
return withQuery(url, {
|
|
346
|
-
utm_medium: utmMedium,
|
|
347
|
-
utm_source: id
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
function getHandheldAction({ canShareFiles, canShareText, id }) {
|
|
351
|
-
const { isImagePost, isLinkOnly } = getShareDestination(id);
|
|
352
|
-
if (isImagePost && canShareFiles) return "share-image";
|
|
353
|
-
if (isLinkOnly === true && canShareText) return "share-link";
|
|
354
|
-
if (id === "any" && canShareText) return "share-text";
|
|
355
|
-
}
|
|
356
|
-
function getPrimaryAction(input) {
|
|
357
|
-
const handheldAction = input.isHandheld ? getHandheldAction(input) : void 0;
|
|
358
|
-
if (handheldAction) return handheldAction;
|
|
359
|
-
if (input.hasComposerUrl) return "open-composer";
|
|
360
|
-
if (input.id === "messages" && input.canShareText) return "share-text";
|
|
361
|
-
return "copy-caption";
|
|
362
|
-
}
|
|
363
240
|
// src/caption.ts
|
|
364
241
|
const separator = "\n\n";
|
|
365
242
|
const ellipsis = "…";
|
|
@@ -436,11 +313,7 @@ function fitParts(input) {
|
|
|
436
313
|
const bare = join([title, link]);
|
|
437
314
|
return canFit(bare) ? bare : join([fitTitle(input), link]);
|
|
438
315
|
}
|
|
439
|
-
function
|
|
440
|
-
return countText(text, getShareDestination(id));
|
|
441
|
-
}
|
|
442
|
-
function fitCaption({ description, title, url }, id, locale = "en") {
|
|
443
|
-
const { limit, shape } = getShareDestination(id);
|
|
316
|
+
function fitCaption({ description, title, url }, { count, limit, shape }, locale = "en") {
|
|
444
317
|
const link = shape === "full" ? url : void 0;
|
|
445
318
|
const body = shape === "title" ? void 0 : description?.trim() || void 0;
|
|
446
319
|
const whole = shape === "url" ? url : join([
|
|
@@ -448,7 +321,7 @@ function fitCaption({ description, title, url }, id, locale = "en") {
|
|
|
448
321
|
body,
|
|
449
322
|
link
|
|
450
323
|
]);
|
|
451
|
-
const canFit = (text) => limit === void 0 ||
|
|
324
|
+
const canFit = (text) => limit === void 0 || count(text) <= limit;
|
|
452
325
|
const text = canFit(whole) ? whole : fitParts({
|
|
453
326
|
body,
|
|
454
327
|
canFit,
|
|
@@ -457,12 +330,251 @@ function fitCaption({ description, title, url }, id, locale = "en") {
|
|
|
457
330
|
title
|
|
458
331
|
});
|
|
459
332
|
return {
|
|
460
|
-
count:
|
|
333
|
+
count: count(text),
|
|
461
334
|
isTruncated: text !== whole,
|
|
462
335
|
limit,
|
|
463
336
|
text
|
|
464
337
|
};
|
|
465
338
|
}
|
|
339
|
+
// src/formats.ts
|
|
340
|
+
const shareKitLandscapeFormat = {
|
|
341
|
+
height: 630,
|
|
342
|
+
id: "landscape",
|
|
343
|
+
width: 1200
|
|
344
|
+
};
|
|
345
|
+
const shareKitPortraitFormat = {
|
|
346
|
+
height: 1350,
|
|
347
|
+
id: "portrait",
|
|
348
|
+
width: 1080
|
|
349
|
+
};
|
|
350
|
+
const shareKitStoryFormat = {
|
|
351
|
+
height: 1920,
|
|
352
|
+
id: "story",
|
|
353
|
+
width: 1080
|
|
354
|
+
};
|
|
355
|
+
const shareKitPinFormat = {
|
|
356
|
+
height: 1500,
|
|
357
|
+
id: "pin",
|
|
358
|
+
width: 1e3
|
|
359
|
+
};
|
|
360
|
+
const fitTolerance = .02;
|
|
361
|
+
function getRatioDistance(image, format) {
|
|
362
|
+
return Math.abs(Math.log(image.width / image.height / (format.width / format.height)));
|
|
363
|
+
}
|
|
364
|
+
function isFormatFit(image, format) {
|
|
365
|
+
return getRatioDistance(image, format) <= fitTolerance;
|
|
366
|
+
}
|
|
367
|
+
function getSuggestedImage(images, formats) {
|
|
368
|
+
const candidates = images.filter((image) => image.isSuggestable !== false);
|
|
369
|
+
for (const format of formats) {
|
|
370
|
+
const match = candidates.find((image) => isFormatFit(image, format));
|
|
371
|
+
if (match) return match;
|
|
372
|
+
}
|
|
373
|
+
let nearest;
|
|
374
|
+
let nearestDistance = 1e400;
|
|
375
|
+
for (const image of candidates) {
|
|
376
|
+
const distance = getRatioDistance(image, formats[0]);
|
|
377
|
+
if (!(distance < nearestDistance)) continue;
|
|
378
|
+
nearest = image;
|
|
379
|
+
nearestDistance = distance;
|
|
380
|
+
}
|
|
381
|
+
return nearest;
|
|
382
|
+
}
|
|
383
|
+
function getShareKitImageFormats(destinations) {
|
|
384
|
+
const formats = /* @__PURE__ */ new Map();
|
|
385
|
+
for (const { images } of destinations) if (!formats.has(images[0].id)) formats.set(images[0].id, images[0]);
|
|
386
|
+
return [...formats.values()];
|
|
387
|
+
}
|
|
388
|
+
// src/destinations.ts
|
|
389
|
+
function defineShareKitDestination(destination) {
|
|
390
|
+
return destination;
|
|
391
|
+
}
|
|
392
|
+
function withQuery(base, parameters) {
|
|
393
|
+
const url = new URL(base);
|
|
394
|
+
for (const [name, value] of Object.entries(parameters)) if (value) url.searchParams.set(name, value);
|
|
395
|
+
return url.href;
|
|
396
|
+
}
|
|
397
|
+
const fixedLinkCost = 23;
|
|
398
|
+
const shareKitAnywhere = {
|
|
399
|
+
actions: {
|
|
400
|
+
desktop: [],
|
|
401
|
+
mobile: ["share-text"]
|
|
402
|
+
},
|
|
403
|
+
count: shareKitCountLength,
|
|
404
|
+
id: "any",
|
|
405
|
+
images: [shareKitLandscapeFormat],
|
|
406
|
+
label: "Anywhere",
|
|
407
|
+
shape: "full",
|
|
408
|
+
utmSource: false
|
|
409
|
+
};
|
|
410
|
+
const shareKitBluesky = {
|
|
411
|
+
actions: {
|
|
412
|
+
desktop: ["open-composer"],
|
|
413
|
+
mobile: ["share-image", "open-composer"]
|
|
414
|
+
},
|
|
415
|
+
composer: ({ text }) => withQuery("https://bsky.app/intent/compose", { text }),
|
|
416
|
+
count: shareKitWithLinkCost(shareKitCountGraphemes, shareKitShortenedLinkCost),
|
|
417
|
+
id: "bluesky",
|
|
418
|
+
images: [shareKitPortraitFormat],
|
|
419
|
+
label: "Bluesky",
|
|
420
|
+
limit: 300,
|
|
421
|
+
shape: "full"
|
|
422
|
+
};
|
|
423
|
+
const shareKitFacebook = {
|
|
424
|
+
actions: {
|
|
425
|
+
desktop: ["open-composer"],
|
|
426
|
+
mobile: ["share-link", "open-composer"]
|
|
427
|
+
},
|
|
428
|
+
composer: ({ url }) => withQuery("https://www.facebook.com/sharer/sharer.php", { u: url }),
|
|
429
|
+
count: shareKitCountLength,
|
|
430
|
+
hint: "Facebook only takes the link. Your caption is copied, so paste it into your post.",
|
|
431
|
+
id: "facebook",
|
|
432
|
+
images: [shareKitLandscapeFormat, shareKitPortraitFormat],
|
|
433
|
+
label: "Facebook",
|
|
434
|
+
limit: 63206,
|
|
435
|
+
shape: "full"
|
|
436
|
+
};
|
|
437
|
+
const shareKitInstagram = {
|
|
438
|
+
actions: {
|
|
439
|
+
desktop: [],
|
|
440
|
+
mobile: ["share-image"]
|
|
441
|
+
},
|
|
442
|
+
count: shareKitCountLength,
|
|
443
|
+
hint: "Links in Instagram captions aren't clickable. Point people to the link in your bio.",
|
|
444
|
+
id: "instagram",
|
|
445
|
+
images: [shareKitPortraitFormat],
|
|
446
|
+
label: "Instagram",
|
|
447
|
+
limit: 2200,
|
|
448
|
+
shape: "text"
|
|
449
|
+
};
|
|
450
|
+
const shareKitInstagramStory = {
|
|
451
|
+
actions: {
|
|
452
|
+
desktop: [],
|
|
453
|
+
mobile: ["share-image"]
|
|
454
|
+
},
|
|
455
|
+
count: shareKitCountLength,
|
|
456
|
+
hint: "Add the link to your Story with a link sticker.",
|
|
457
|
+
id: "instagram-story",
|
|
458
|
+
images: [shareKitStoryFormat, shareKitPortraitFormat],
|
|
459
|
+
label: "Story",
|
|
460
|
+
shape: "url"
|
|
461
|
+
};
|
|
462
|
+
const shareKitLinkedin = {
|
|
463
|
+
actions: {
|
|
464
|
+
desktop: ["open-composer"],
|
|
465
|
+
mobile: ["share-link", "open-composer"]
|
|
466
|
+
},
|
|
467
|
+
composer: ({ url }) => withQuery("https://www.linkedin.com/sharing/share-offsite/", { url }),
|
|
468
|
+
count: shareKitCountLength,
|
|
469
|
+
hint: "LinkedIn only takes the link. Your caption is copied, so paste it into your post.",
|
|
470
|
+
id: "linkedin",
|
|
471
|
+
images: [shareKitLandscapeFormat],
|
|
472
|
+
label: "LinkedIn",
|
|
473
|
+
limit: 3e3,
|
|
474
|
+
shape: "full"
|
|
475
|
+
};
|
|
476
|
+
const shareKitMastodon = {
|
|
477
|
+
actions: {
|
|
478
|
+
desktop: [],
|
|
479
|
+
mobile: ["share-image"]
|
|
480
|
+
},
|
|
481
|
+
count: shareKitWithLinkCost(shareKitCountStringz, fixedLinkCost),
|
|
482
|
+
id: "mastodon",
|
|
483
|
+
images: [shareKitPortraitFormat],
|
|
484
|
+
label: "Mastodon",
|
|
485
|
+
limit: 500,
|
|
486
|
+
shape: "full"
|
|
487
|
+
};
|
|
488
|
+
const shareKitMessages = {
|
|
489
|
+
actions: {
|
|
490
|
+
desktop: ["share-text"],
|
|
491
|
+
mobile: ["share-text"]
|
|
492
|
+
},
|
|
493
|
+
count: shareKitCountLength,
|
|
494
|
+
id: "messages",
|
|
495
|
+
images: [shareKitLandscapeFormat],
|
|
496
|
+
label: "Messages",
|
|
497
|
+
shape: "full"
|
|
498
|
+
};
|
|
499
|
+
const shareKitPinterest = {
|
|
500
|
+
actions: {
|
|
501
|
+
desktop: ["open-composer"],
|
|
502
|
+
mobile: ["open-composer"]
|
|
503
|
+
},
|
|
504
|
+
composer: ({ media, text, url }) => withQuery("https://www.pinterest.com/pin/create/button/", {
|
|
505
|
+
description: text,
|
|
506
|
+
media,
|
|
507
|
+
url
|
|
508
|
+
}),
|
|
509
|
+
count: shareKitCountLength,
|
|
510
|
+
id: "pinterest",
|
|
511
|
+
images: [shareKitPinFormat, shareKitPortraitFormat],
|
|
512
|
+
label: "Pinterest",
|
|
513
|
+
limit: 800,
|
|
514
|
+
shape: "text"
|
|
515
|
+
};
|
|
516
|
+
const shareKitReddit = {
|
|
517
|
+
actions: {
|
|
518
|
+
desktop: ["open-composer"],
|
|
519
|
+
mobile: ["open-composer"]
|
|
520
|
+
},
|
|
521
|
+
composer: ({ text, url }) => withQuery("https://www.reddit.com/submit", {
|
|
522
|
+
title: text,
|
|
523
|
+
url
|
|
524
|
+
}),
|
|
525
|
+
count: shareKitCountLength,
|
|
526
|
+
hint: "Reddit takes a title and the link.",
|
|
527
|
+
id: "reddit",
|
|
528
|
+
images: [shareKitLandscapeFormat],
|
|
529
|
+
label: "Reddit",
|
|
530
|
+
limit: 300,
|
|
531
|
+
shape: "title"
|
|
532
|
+
};
|
|
533
|
+
const shareKitThreads = {
|
|
534
|
+
actions: {
|
|
535
|
+
desktop: ["open-composer"],
|
|
536
|
+
mobile: ["share-image", "open-composer"]
|
|
537
|
+
},
|
|
538
|
+
composer: ({ text }) => withQuery("https://www.threads.com/intent/post", { text }),
|
|
539
|
+
count: shareKitCountUtf8,
|
|
540
|
+
id: "threads",
|
|
541
|
+
images: [shareKitPortraitFormat],
|
|
542
|
+
label: "Threads",
|
|
543
|
+
limit: 500,
|
|
544
|
+
shape: "full"
|
|
545
|
+
};
|
|
546
|
+
const shareKitX = {
|
|
547
|
+
actions: {
|
|
548
|
+
desktop: ["open-composer"],
|
|
549
|
+
mobile: ["open-composer"]
|
|
550
|
+
},
|
|
551
|
+
composer: ({ text }) => withQuery("https://x.com/intent/tweet", { text }),
|
|
552
|
+
count: shareKitWithLinkCost(shareKitCountWeighted, fixedLinkCost),
|
|
553
|
+
id: "x",
|
|
554
|
+
images: [shareKitLandscapeFormat, shareKitPortraitFormat],
|
|
555
|
+
label: "X",
|
|
556
|
+
limit: 280,
|
|
557
|
+
shape: "full"
|
|
558
|
+
};
|
|
559
|
+
function getDestinationUrl(url, { id, utmSource = id }, utmMedium) {
|
|
560
|
+
if (!utmMedium || utmSource === false || !URL.canParse(url)) return url;
|
|
561
|
+
return withQuery(url, {
|
|
562
|
+
utm_medium: utmMedium,
|
|
563
|
+
utm_source: utmSource
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
function canRun(action, input) {
|
|
567
|
+
switch (action) {
|
|
568
|
+
case "copy-caption": return true;
|
|
569
|
+
case "open-composer": return Boolean(input.composerUrl);
|
|
570
|
+
case "share-image": return input.canShareFiles && input.hasSuggestedImage;
|
|
571
|
+
case "share-link":
|
|
572
|
+
case "share-text": return input.canShareText;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
function getPrimaryAction(actions, input) {
|
|
576
|
+
return actions.find((action) => canRun(action, input)) ?? "copy-caption";
|
|
577
|
+
}
|
|
466
578
|
// src/dom.ts
|
|
467
579
|
function createElement(tag, attributes = {}, children = []) {
|
|
468
580
|
const element = document.createElement(tag);
|
|
@@ -474,18 +586,31 @@ function joinClasses(...classes) {
|
|
|
474
586
|
return classes.filter(Boolean).join(" ");
|
|
475
587
|
}
|
|
476
588
|
const svgNamespace = "http://www.w3.org/2000/svg";
|
|
477
|
-
|
|
589
|
+
const iconSize = 24;
|
|
590
|
+
function createIcon(path) {
|
|
478
591
|
const svg = document.createElementNS(svgNamespace, "svg");
|
|
479
592
|
const shape = document.createElementNS(svgNamespace, "path");
|
|
480
593
|
svg.setAttribute("aria-hidden", "true");
|
|
481
|
-
svg.setAttribute("height", String(
|
|
594
|
+
svg.setAttribute("height", String(iconSize));
|
|
482
595
|
svg.setAttribute("viewBox", "0 0 24 24");
|
|
483
|
-
svg.setAttribute("width", String(
|
|
596
|
+
svg.setAttribute("width", String(iconSize));
|
|
484
597
|
shape.setAttribute("d", path);
|
|
485
598
|
shape.setAttribute("fill", "currentColor");
|
|
486
599
|
svg.append(shape);
|
|
487
600
|
return svg;
|
|
488
601
|
}
|
|
602
|
+
function createIconSlot(name, path, fallback) {
|
|
603
|
+
return createElement("span", {
|
|
604
|
+
class: "share-kit-icon",
|
|
605
|
+
"data-share-kit-icon": name
|
|
606
|
+
}, [path ? createIcon(path) : fallback]);
|
|
607
|
+
}
|
|
608
|
+
function patchIcons(root, icons) {
|
|
609
|
+
for (const slot of root.querySelectorAll("[data-share-kit-icon]")) {
|
|
610
|
+
const path = slot.dataset.shareKitIcon ? icons[slot.dataset.shareKitIcon] : void 0;
|
|
611
|
+
if (path && !slot.querySelector("svg")) slot.replaceChildren(createIcon(path));
|
|
612
|
+
}
|
|
613
|
+
}
|
|
489
614
|
function isOnBackdrop(dialog, event) {
|
|
490
615
|
if (event.target !== dialog) return false;
|
|
491
616
|
const rect = dialog.getBoundingClientRect();
|
|
@@ -563,61 +688,53 @@ var ShareFeedback = class {
|
|
|
563
688
|
}, flashDurationMs);
|
|
564
689
|
}
|
|
565
690
|
};
|
|
566
|
-
// src/
|
|
567
|
-
const
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
suggested: "Suggested",
|
|
613
|
-
title: "Share this page"
|
|
691
|
+
// src/icons.ts
|
|
692
|
+
const shareKitInterfaceIconNames = [
|
|
693
|
+
"close",
|
|
694
|
+
"more",
|
|
695
|
+
"qr"
|
|
696
|
+
];
|
|
697
|
+
const shareKitInterfaceIcons = {
|
|
698
|
+
any: "M3.16 14.47L5.99 11.65A4.5 4.5 0 0 1 12.35 18.01L9.53 20.84A4.5 4.5 0 0 1 3.16 14.47ZM4.58 15.89A2.5 2.5 0 0 0 8.11 19.42L10.94 16.6A2.5 2.5 0 0 0 7.4 13.06ZM11.65 5.99L14.47 3.16A4.5 4.5 0 0 1 20.84 9.53L18.01 12.35A4.5 4.5 0 0 1 11.65 5.99ZM13.06 7.4A2.5 2.5 0 0 0 16.6 10.94L19.42 8.11A2.5 2.5 0 0 0 15.89 4.58ZM8.46 14.12L14.12 8.46L15.54 9.88L9.88 15.54Z",
|
|
699
|
+
close: "M6 7.41 7.41 6 12 10.59 16.59 6 18 7.41 13.41 12 18 16.59 16.59 18 12 13.41 7.41 18 6 16.59 10.59 12Z",
|
|
700
|
+
messages: "M5 4h14a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2ZM5 6v9h14V6ZM7 17v4l4-4Z",
|
|
701
|
+
more: "M4 12a2 2 0 1 0 4 0a2 2 0 1 0-4 0ZM10 12a2 2 0 1 0 4 0a2 2 0 1 0-4 0ZM16 12a2 2 0 1 0 4 0a2 2 0 1 0-4 0Z",
|
|
702
|
+
qr: "M3 3h7v7H3ZM4.5 4.5v4h4v-4ZM5.5 5.5h2v2h-2ZM14 3h7v7h-7ZM15.5 4.5v4h4v-4ZM16.5 5.5h2v2h-2ZM3 14h7v7H3ZM4.5 15.5v4h4v-4ZM5.5 16.5h2v2h-2ZM14 14h2v2h-2ZM18 14h3v2h-3ZM16 16h2v2h-2ZM14 18h2v3h-2ZM18 18h3v3h-3Z"
|
|
703
|
+
};
|
|
704
|
+
var ShareIconSource = class {
|
|
705
|
+
get icons() {
|
|
706
|
+
return this.#icons;
|
|
707
|
+
}
|
|
708
|
+
get isLoading() {
|
|
709
|
+
return !this.#isSettled;
|
|
710
|
+
}
|
|
711
|
+
#icons = {};
|
|
712
|
+
#isSettled = false;
|
|
713
|
+
#loader;
|
|
714
|
+
#pending;
|
|
715
|
+
constructor(option) {
|
|
716
|
+
if (typeof option === "function") {
|
|
717
|
+
this.#loader = option;
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
this.#icons = option;
|
|
721
|
+
this.#isSettled = true;
|
|
722
|
+
}
|
|
723
|
+
load() {
|
|
724
|
+
if (!this.#pending) this.#pending = this.#load();
|
|
725
|
+
return this.#pending;
|
|
726
|
+
}
|
|
727
|
+
async #load() {
|
|
728
|
+
if (!this.#loader) return this.#icons;
|
|
729
|
+
try {
|
|
730
|
+
this.#icons = await this.#loader();
|
|
731
|
+
} catch (error) {
|
|
732
|
+
console.warn("share-kit: the icons loader failed, so the dialog keeps its text labels", error);
|
|
733
|
+
}
|
|
734
|
+
this.#isSettled = true;
|
|
735
|
+
return this.#icons;
|
|
736
|
+
}
|
|
614
737
|
};
|
|
615
|
-
function isShareStringKey(key) {
|
|
616
|
-
return Object.hasOwn(shareKitStringDefaults, key);
|
|
617
|
-
}
|
|
618
|
-
function formatShareString(template, values) {
|
|
619
|
-
return template.replaceAll(/\{(\w+)\}/gu, (match, name) => values[name] ?? match);
|
|
620
|
-
}
|
|
621
738
|
// src/payload.ts
|
|
622
739
|
function isPayload(value) {
|
|
623
740
|
if (typeof value !== "object" || value === null) return false;
|
|
@@ -637,25 +754,42 @@ function readSharePayload(host) {
|
|
|
637
754
|
return;
|
|
638
755
|
}
|
|
639
756
|
}
|
|
640
|
-
function getShareStrings(overrides = {}) {
|
|
641
|
-
const strings = { ...shareKitStringDefaults };
|
|
642
|
-
for (const [key, value] of Object.entries(overrides)) if (value !== void 0 && isShareStringKey(key)) strings[key] = value;
|
|
643
|
-
return strings;
|
|
644
|
-
}
|
|
645
757
|
function getIds(value) {
|
|
646
758
|
if (!Array.isArray(value)) return [];
|
|
647
|
-
return [...new Set(value)]
|
|
648
|
-
}
|
|
649
|
-
function getDestinationLists({ destinations, destinationsMore }) {
|
|
650
|
-
const
|
|
651
|
-
const
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
759
|
+
return [...new Set(value)];
|
|
760
|
+
}
|
|
761
|
+
function getDestinationLists({ destinations, destinationsMore }, catalogue) {
|
|
762
|
+
const unknown = [];
|
|
763
|
+
const resolve = (ids) => ids.flatMap((id) => {
|
|
764
|
+
const destination = typeof id === "string" ? catalogue.get(id) : void 0;
|
|
765
|
+
if (!destination) unknown.push(String(id));
|
|
766
|
+
return destination ? [destination] : [];
|
|
767
|
+
});
|
|
768
|
+
const shown = resolve(getIds(destinations));
|
|
769
|
+
const rest = resolve(getIds(destinationsMore)).filter((destination) => !shown.includes(destination));
|
|
770
|
+
if (unknown.length > 0) console.warn(`share-kit: unregistered destinations dropped: ${unknown.join(", ")}`);
|
|
771
|
+
if (shown.length > 0) return {
|
|
772
|
+
main: shown,
|
|
773
|
+
more: rest
|
|
774
|
+
};
|
|
775
|
+
if (rest.length > 0) return {
|
|
776
|
+
main: rest,
|
|
777
|
+
more: []
|
|
655
778
|
};
|
|
779
|
+
console.warn("share-kit: no registered destinations to show");
|
|
780
|
+
}
|
|
781
|
+
function getShareAssets(assets) {
|
|
782
|
+
const seen = /* @__PURE__ */ new Set();
|
|
783
|
+
const kept = assets.filter((asset) => {
|
|
784
|
+
const id = asset.id;
|
|
785
|
+
if (typeof id !== "string" || id === "" || seen.has(id)) return false;
|
|
786
|
+
seen.add(id);
|
|
787
|
+
return true;
|
|
788
|
+
});
|
|
789
|
+
if (kept.length < assets.length) console.warn("share-kit: images with an empty or repeated id dropped");
|
|
790
|
+
return kept;
|
|
656
791
|
}
|
|
657
792
|
// src/qr.ts
|
|
658
|
-
const iconSize = 24;
|
|
659
793
|
const qrSize = 200;
|
|
660
794
|
async function createQrSource(text) {
|
|
661
795
|
const [{ generate }, { toSvgDataURL }] = await Promise.all([import("lean-qr"), import("lean-qr/extras/svg")]);
|
|
@@ -695,7 +829,7 @@ var ShareQrDialog = class {
|
|
|
695
829
|
class: "share-kit-icon-button",
|
|
696
830
|
title: strings.close,
|
|
697
831
|
type: "submit"
|
|
698
|
-
}, [
|
|
832
|
+
}, [createIconSlot("close", closeIcon, "×")])]), this.#image]);
|
|
699
833
|
polyfillLightDismiss(this.dialog, signal);
|
|
700
834
|
}
|
|
701
835
|
async open() {
|
|
@@ -710,14 +844,74 @@ function getQrUrl(url, openHash) {
|
|
|
710
844
|
return target.href;
|
|
711
845
|
}
|
|
712
846
|
function createShareQr({ capabilities, payload, ...input }) {
|
|
713
|
-
if (capabilities.has("
|
|
847
|
+
if (capabilities.has("mobile")) return;
|
|
714
848
|
return new ShareQrDialog({
|
|
715
849
|
...input,
|
|
716
850
|
url: getQrUrl(payload.url, payload.settings.openHash)
|
|
717
851
|
});
|
|
718
852
|
}
|
|
853
|
+
// src/strings.ts
|
|
854
|
+
const shareKitStringDefaults = {
|
|
855
|
+
assetCopy: "Copy",
|
|
856
|
+
assetSave: "Save",
|
|
857
|
+
assetShare: "Share",
|
|
858
|
+
captionCopy: "Copy caption",
|
|
859
|
+
captionOver: "{count} over the limit for {destination}",
|
|
860
|
+
captionReset: "Reset",
|
|
861
|
+
captionTitle: "Caption",
|
|
862
|
+
close: "Close",
|
|
863
|
+
copied: "Copied",
|
|
864
|
+
copyManual: "Couldn't copy. The caption is selected, so you can copy it yourself.",
|
|
865
|
+
destinationsMore: "More",
|
|
866
|
+
destinationsTitle: "Post to",
|
|
867
|
+
error: "Something went wrong. Try saving instead.",
|
|
868
|
+
imagesTitle: "Images",
|
|
869
|
+
linkCopy: "Copy link",
|
|
870
|
+
linkTitle: "Link",
|
|
871
|
+
primaryCopy: "Copy caption",
|
|
872
|
+
primaryOpen: "Open {destination}",
|
|
873
|
+
primaryShareImage: "Share image",
|
|
874
|
+
primaryShareLink: "Share link",
|
|
875
|
+
primaryShareText: "Share",
|
|
876
|
+
qrAlt: "QR code for this page",
|
|
877
|
+
qrTitle: "Scan to continue on your phone",
|
|
878
|
+
qrToggle: "Continue on your phone",
|
|
879
|
+
retry: "Ready. Tap again to share.",
|
|
880
|
+
saved: "Saved",
|
|
881
|
+
saveError: "Couldn't save. Try again.",
|
|
882
|
+
suggested: "Suggested",
|
|
883
|
+
title: "Share this page"
|
|
884
|
+
};
|
|
885
|
+
const destinationStringPattern = /^(?:destination|hint)\.(.+)$/u;
|
|
886
|
+
function isShareStringKey(key) {
|
|
887
|
+
return Object.hasOwn(shareKitStringDefaults, key);
|
|
888
|
+
}
|
|
889
|
+
function isDestinationStringKey(key, ids) {
|
|
890
|
+
const id = destinationStringPattern.exec(key)?.[1];
|
|
891
|
+
return id !== void 0 && ids.has(id);
|
|
892
|
+
}
|
|
893
|
+
function getShareStrings(overrides, ids) {
|
|
894
|
+
const strings = { ...shareKitStringDefaults };
|
|
895
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
896
|
+
if (typeof value !== "string") continue;
|
|
897
|
+
if (isShareStringKey(key) || isDestinationStringKey(key, ids)) strings[key] = value;
|
|
898
|
+
}
|
|
899
|
+
return strings;
|
|
900
|
+
}
|
|
901
|
+
function getDestinationLabel(strings, { id, label }) {
|
|
902
|
+
return strings[`destination.${id}`] ?? label;
|
|
903
|
+
}
|
|
904
|
+
function getDestinationHint(strings, { hint, id }) {
|
|
905
|
+
return strings[`hint.${id}`] ?? hint;
|
|
906
|
+
}
|
|
907
|
+
function formatShareString(template, values) {
|
|
908
|
+
return template.replaceAll(/\{(\w+)\}/gu, (match, name) => values[name] ?? match);
|
|
909
|
+
}
|
|
719
910
|
// src/render.ts
|
|
720
|
-
|
|
911
|
+
function renderDestinationIcon({ icons, isIconsLoading }, name) {
|
|
912
|
+
const path = icons[name];
|
|
913
|
+
return path || isIconsLoading ? createIconSlot(name, path) : void 0;
|
|
914
|
+
}
|
|
721
915
|
function renderAction({ classNames }, { action, describedBy, label }) {
|
|
722
916
|
return createElement("button", {
|
|
723
917
|
"aria-describedby": describedBy,
|
|
@@ -732,7 +926,7 @@ function renderHeading({ classNames }, children, id) {
|
|
|
732
926
|
id
|
|
733
927
|
}, children);
|
|
734
928
|
}
|
|
735
|
-
function renderIconButton({ action, attributes, fallback, icon, label }) {
|
|
929
|
+
function renderIconButton({ icons }, { action, attributes, fallback, icon, label }) {
|
|
736
930
|
return createElement("button", {
|
|
737
931
|
...attributes,
|
|
738
932
|
"aria-label": label,
|
|
@@ -740,52 +934,51 @@ function renderIconButton({ action, attributes, fallback, icon, label }) {
|
|
|
740
934
|
"data-share-kit-action": action,
|
|
741
935
|
title: label,
|
|
742
936
|
type: "button"
|
|
743
|
-
}, [icon
|
|
937
|
+
}, [createIconSlot(icon, icons[icon], fallback)]);
|
|
744
938
|
}
|
|
745
939
|
function renderHeader(context) {
|
|
746
|
-
const { capabilities,
|
|
940
|
+
const { capabilities, id, strings } = context;
|
|
747
941
|
const heading = createElement("h2", {
|
|
748
942
|
class: "share-kit-title",
|
|
749
943
|
id: `${id}-title`,
|
|
750
944
|
tabindex: "-1"
|
|
751
945
|
}, [strings.title]);
|
|
752
946
|
return {
|
|
753
|
-
header: createElement("header", { class: "share-kit-header" }, [heading, createElement("div", { class: "share-kit-header-actions" }, [capabilities.has("
|
|
947
|
+
header: createElement("header", { class: "share-kit-header" }, [heading, createElement("div", { class: "share-kit-header-actions" }, [capabilities.has("mobile") ? void 0 : renderIconButton(context, {
|
|
754
948
|
action: "qr",
|
|
755
949
|
attributes: { "aria-haspopup": "dialog" },
|
|
756
950
|
fallback: "QR",
|
|
757
|
-
icon:
|
|
951
|
+
icon: "qr",
|
|
758
952
|
label: strings.qrToggle
|
|
759
|
-
}), renderIconButton({
|
|
953
|
+
}), renderIconButton(context, {
|
|
760
954
|
action: "close",
|
|
761
955
|
fallback: "×",
|
|
762
|
-
icon:
|
|
956
|
+
icon: "close",
|
|
763
957
|
label: strings.close
|
|
764
958
|
})])]),
|
|
765
959
|
heading
|
|
766
960
|
};
|
|
767
961
|
}
|
|
768
962
|
function renderDestination(context, destination) {
|
|
769
|
-
const { classNames,
|
|
770
|
-
const icon = icons[destination];
|
|
963
|
+
const { classNames, strings } = context;
|
|
771
964
|
return createElement("button", {
|
|
772
965
|
"aria-checked": "false",
|
|
773
966
|
class: joinClasses("share-kit-destination", classNames.destination),
|
|
774
967
|
"data-share-kit-action": "destination",
|
|
775
|
-
"data-share-kit-destination": destination,
|
|
968
|
+
"data-share-kit-destination": destination.id,
|
|
776
969
|
role: "radio",
|
|
777
970
|
tabindex: "-1",
|
|
778
971
|
type: "button"
|
|
779
|
-
}, [
|
|
972
|
+
}, [renderDestinationIcon(context, destination.id), createElement("span", {}, [getDestinationLabel(strings, destination)])]);
|
|
780
973
|
}
|
|
781
974
|
function renderDestinations(context) {
|
|
782
|
-
const { destinations, destinationsMore,
|
|
975
|
+
const { destinations, destinationsMore, id, strings } = context;
|
|
783
976
|
const headingId = `${id}-destinations`;
|
|
784
977
|
const more = destinationsMore.length > 0 ? createElement("button", {
|
|
785
978
|
class: joinClasses("share-kit-destination", context.classNames.destination),
|
|
786
979
|
"data-share-kit-action": "more",
|
|
787
980
|
type: "button"
|
|
788
|
-
}, [
|
|
981
|
+
}, [renderDestinationIcon(context, "more"), createElement("span", {}, [strings.destinationsMore])]) : void 0;
|
|
789
982
|
const extra = destinationsMore.map((destination) => {
|
|
790
983
|
const button = renderDestination(context, destination);
|
|
791
984
|
button.hidden = true;
|
|
@@ -842,14 +1035,13 @@ function renderCaption(context) {
|
|
|
842
1035
|
])
|
|
843
1036
|
};
|
|
844
1037
|
}
|
|
845
|
-
function renderTile(context, { actions, asset,
|
|
846
|
-
const labelId = `${context.id}-asset-${
|
|
1038
|
+
function renderTile(context, { actions, asset, index }) {
|
|
1039
|
+
const labelId = `${context.id}-asset-${String(index)}`;
|
|
847
1040
|
const badge = createElement("span", { class: "share-kit-badge" }, [context.strings.suggested]);
|
|
848
1041
|
badge.hidden = true;
|
|
849
1042
|
return createElement("li", {
|
|
850
1043
|
class: "share-kit-tile",
|
|
851
|
-
"data-share-kit-asset":
|
|
852
|
-
"data-share-kit-role": asset.role
|
|
1044
|
+
"data-share-kit-asset": asset.id
|
|
853
1045
|
}, [
|
|
854
1046
|
createElement("img", {
|
|
855
1047
|
alt: asset.alt,
|
|
@@ -872,25 +1064,19 @@ function getAssetActions({ capabilities, strings }) {
|
|
|
872
1064
|
const actions = [];
|
|
873
1065
|
const canShare = capabilities.has("files");
|
|
874
1066
|
if (canShare) actions.push(["share", strings.assetShare]);
|
|
875
|
-
if (capabilities.has("copy-image") && !(canShare && capabilities.has("
|
|
1067
|
+
if (capabilities.has("copy-image") && !(canShare && capabilities.has("mobile"))) actions.push(["copy", strings.assetCopy]);
|
|
876
1068
|
actions.push(["save", strings.assetSave]);
|
|
877
1069
|
return actions;
|
|
878
1070
|
}
|
|
879
1071
|
function renderImages(context) {
|
|
880
1072
|
const { payload, strings } = context;
|
|
881
1073
|
const actions = getAssetActions(context);
|
|
882
|
-
const counts = /* @__PURE__ */ new Map();
|
|
883
1074
|
const assets = /* @__PURE__ */ new Map();
|
|
884
|
-
for (const asset of payload.assets) {
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
actions,
|
|
890
|
-
asset,
|
|
891
|
-
key
|
|
892
|
-
}), asset);
|
|
893
|
-
}
|
|
1075
|
+
for (const [index, asset] of payload.assets.entries()) assets.set(renderTile(context, {
|
|
1076
|
+
actions,
|
|
1077
|
+
asset,
|
|
1078
|
+
index
|
|
1079
|
+
}), asset);
|
|
894
1080
|
const grid = createElement("ul", { class: "share-kit-grid" }, [...assets.keys()]);
|
|
895
1081
|
return {
|
|
896
1082
|
assets,
|
|
@@ -961,15 +1147,11 @@ const primaryLabels = {
|
|
|
961
1147
|
"share-link": "primaryShareLink",
|
|
962
1148
|
"share-text": "primaryShareText"
|
|
963
1149
|
};
|
|
964
|
-
function getSuggestedAsset(assets, destination) {
|
|
965
|
-
const { assetRole } = getShareDestination(destination);
|
|
966
|
-
return assets.find((asset) => asset.dataset.shareKitRole === assetRole) ?? assets.find((asset) => asset.dataset.shareKitRole === "landscape") ?? assets[0];
|
|
967
|
-
}
|
|
968
1150
|
function syncCaptionView({ destination, parts, primaryAction, strings }) {
|
|
969
|
-
const { limit } =
|
|
970
|
-
const count =
|
|
1151
|
+
const { limit } = destination;
|
|
1152
|
+
const count = destination.count(parts.caption.value);
|
|
971
1153
|
const isOver = limit !== void 0 && count > limit;
|
|
972
|
-
const label = strings
|
|
1154
|
+
const label = getDestinationLabel(strings, destination);
|
|
973
1155
|
parts.count.textContent = limit === void 0 ? String(count) : `${String(count)} / ${String(limit)}`;
|
|
974
1156
|
parts.count.toggleAttribute("data-share-kit-over", isOver);
|
|
975
1157
|
parts.caption.setAttribute("aria-invalid", String(isOver));
|
|
@@ -985,12 +1167,11 @@ function syncCaptionView({ destination, parts, primaryAction, strings }) {
|
|
|
985
1167
|
}
|
|
986
1168
|
function syncDestinationView({ assets, destination, parts, strings, suggested }) {
|
|
987
1169
|
for (const radio of parts.dialog.querySelectorAll("[role=\"radio\"]")) {
|
|
988
|
-
const isSelected = radio.dataset.shareKitDestination === destination;
|
|
1170
|
+
const isSelected = radio.dataset.shareKitDestination === destination.id;
|
|
989
1171
|
radio.setAttribute("aria-checked", String(isSelected));
|
|
990
1172
|
radio.tabIndex = isSelected ? 0 : -1;
|
|
991
1173
|
}
|
|
992
|
-
|
|
993
|
-
parts.hint.textContent = isShareStringKey(hintKey) ? strings[hintKey] : "";
|
|
1174
|
+
parts.hint.textContent = getDestinationHint(strings, destination) ?? "";
|
|
994
1175
|
parts.hint.hidden = parts.hint.textContent === "";
|
|
995
1176
|
for (const asset of assets) {
|
|
996
1177
|
const badge = asset.querySelector(".share-kit-badge");
|
|
@@ -1002,6 +1183,11 @@ function syncDestinationView({ assets, destination, parts, strings, suggested })
|
|
|
1002
1183
|
// src/types.ts
|
|
1003
1184
|
const shareKitActionEventName = "share-kit:action";
|
|
1004
1185
|
// src/element.ts
|
|
1186
|
+
const iconIntentEvents = [
|
|
1187
|
+
"focusin",
|
|
1188
|
+
"pointerdown",
|
|
1189
|
+
"pointerover"
|
|
1190
|
+
];
|
|
1005
1191
|
const outcomeMessages = {
|
|
1006
1192
|
error: "error",
|
|
1007
1193
|
retry: "retry",
|
|
@@ -1014,7 +1200,8 @@ function isKeyOf(record, key) {
|
|
|
1014
1200
|
}
|
|
1015
1201
|
const BaseElement = typeof HTMLElement === "undefined" ? Object : HTMLElement;
|
|
1016
1202
|
var ShareKitElement = class extends BaseElement {
|
|
1017
|
-
|
|
1203
|
+
catalogue = /* @__PURE__ */ new Map();
|
|
1204
|
+
iconSource = new ShareIconSource({});
|
|
1018
1205
|
#abortController;
|
|
1019
1206
|
#actions = {
|
|
1020
1207
|
close: () => {
|
|
@@ -1034,9 +1221,9 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1034
1221
|
this.#feedback?.copyText(this.#payload?.url ?? "", button);
|
|
1035
1222
|
},
|
|
1036
1223
|
destination: (button) => {
|
|
1037
|
-
const
|
|
1038
|
-
if (!
|
|
1039
|
-
this.#selectDestination(
|
|
1224
|
+
const destination = this.#getRendered(button.dataset.shareKitDestination);
|
|
1225
|
+
if (!destination) return;
|
|
1226
|
+
this.#selectDestination(destination);
|
|
1040
1227
|
this.#track("destination");
|
|
1041
1228
|
},
|
|
1042
1229
|
more: (button) => {
|
|
@@ -1060,11 +1247,13 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1060
1247
|
}
|
|
1061
1248
|
};
|
|
1062
1249
|
#capabilities = /* @__PURE__ */ new Set();
|
|
1063
|
-
#destination
|
|
1250
|
+
#destination;
|
|
1064
1251
|
#feedback;
|
|
1065
1252
|
#files;
|
|
1066
1253
|
#isCaptionEdited = false;
|
|
1254
|
+
#isIconsRequested = false;
|
|
1067
1255
|
#isOver = false;
|
|
1256
|
+
#lists;
|
|
1068
1257
|
#parts;
|
|
1069
1258
|
#payload;
|
|
1070
1259
|
#primaryActions = {
|
|
@@ -1073,11 +1262,11 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1073
1262
|
this.#copyCaption();
|
|
1074
1263
|
},
|
|
1075
1264
|
"open-composer": () => {
|
|
1076
|
-
const
|
|
1077
|
-
if (!
|
|
1265
|
+
const href = this.#getComposerUrl();
|
|
1266
|
+
if (!href) return;
|
|
1078
1267
|
this.#track("open-composer");
|
|
1079
1268
|
this.#copyCaption();
|
|
1080
|
-
|
|
1269
|
+
openComposer(href);
|
|
1081
1270
|
},
|
|
1082
1271
|
"share-image": () => {
|
|
1083
1272
|
const tile = this.#getSuggestedAsset();
|
|
@@ -1103,9 +1292,15 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1103
1292
|
this.#retryWhenParsed();
|
|
1104
1293
|
return;
|
|
1105
1294
|
}
|
|
1295
|
+
const lists = getDestinationLists(payload.settings, this.catalogue);
|
|
1296
|
+
if (!lists) return;
|
|
1106
1297
|
this.#abortController = new AbortController();
|
|
1107
|
-
this.#
|
|
1108
|
-
this.#
|
|
1298
|
+
this.#lists = lists;
|
|
1299
|
+
this.#payload = {
|
|
1300
|
+
...payload,
|
|
1301
|
+
assets: getShareAssets(payload.assets)
|
|
1302
|
+
};
|
|
1303
|
+
this.#strings = getShareStrings(payload.strings ?? {}, new Set(this.catalogue.keys()));
|
|
1109
1304
|
this.#feedback = new ShareFeedback(() => this.#parts?.status, this.#strings);
|
|
1110
1305
|
const { signal } = this.#abortController;
|
|
1111
1306
|
this.#capabilities = new Set(getShareCapabilities(this.dataset.can));
|
|
@@ -1122,6 +1317,7 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1122
1317
|
signal
|
|
1123
1318
|
});
|
|
1124
1319
|
this.addEventListener("pointerover", ({ target }) => this.#files?.warm(target), { signal });
|
|
1320
|
+
for (const type of iconIntentEvents) this.addEventListener(type, this.#handleTriggerIntent, { signal });
|
|
1125
1321
|
window.addEventListener("hashchange", this.#handleHashChange, { signal });
|
|
1126
1322
|
this.querySelector("[data-share-kit-trigger]")?.toggleAttribute("disabled", false);
|
|
1127
1323
|
queueMicrotask(() => {
|
|
@@ -1138,8 +1334,10 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1138
1334
|
this.#feedback = void 0;
|
|
1139
1335
|
this.#files?.dispose();
|
|
1140
1336
|
this.#files = void 0;
|
|
1141
|
-
this.#destination =
|
|
1337
|
+
this.#destination = void 0;
|
|
1338
|
+
this.#lists = void 0;
|
|
1142
1339
|
this.#isCaptionEdited = false;
|
|
1340
|
+
this.#isIconsRequested = false;
|
|
1143
1341
|
this.#isOver = false;
|
|
1144
1342
|
}
|
|
1145
1343
|
async #copyCaption(button) {
|
|
@@ -1151,31 +1349,51 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1151
1349
|
}
|
|
1152
1350
|
#getComposerUrl() {
|
|
1153
1351
|
const parts = this.#parts;
|
|
1154
|
-
|
|
1352
|
+
const payload = this.#payload;
|
|
1353
|
+
const composer = this.#destination?.composer;
|
|
1354
|
+
if (!parts || !payload || !composer) return;
|
|
1155
1355
|
const suggested = this.#getSuggestedAsset();
|
|
1156
1356
|
const media = suggested ? parts.assets.get(suggested)?.src : void 0;
|
|
1157
|
-
return
|
|
1158
|
-
|
|
1357
|
+
return composer({
|
|
1358
|
+
...payload.description === void 0 ? {} : { description: payload.description },
|
|
1359
|
+
device: this.#getDevice(),
|
|
1360
|
+
...media === void 0 ? {} : { media: new URL(media, location.href).href },
|
|
1159
1361
|
text: parts.caption.value,
|
|
1362
|
+
title: payload.title,
|
|
1160
1363
|
url: this.#getUrl()
|
|
1161
1364
|
});
|
|
1162
1365
|
}
|
|
1366
|
+
#getDevice() {
|
|
1367
|
+
return this.#capabilities.has("mobile") ? "mobile" : "desktop";
|
|
1368
|
+
}
|
|
1163
1369
|
#getPrimaryAction() {
|
|
1164
|
-
|
|
1165
|
-
|
|
1370
|
+
const destination = this.#destination;
|
|
1371
|
+
if (!destination) return "copy-caption";
|
|
1372
|
+
return getPrimaryAction(destination.actions[this.#getDevice()], {
|
|
1373
|
+
canShareFiles: this.#capabilities.has("files"),
|
|
1166
1374
|
canShareText: this.#capabilities.has("link"),
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
isHandheld: this.#capabilities.has("handheld")
|
|
1375
|
+
composerUrl: this.#getComposerUrl(),
|
|
1376
|
+
hasSuggestedImage: this.#getSuggestedAsset() !== void 0
|
|
1170
1377
|
});
|
|
1171
1378
|
}
|
|
1379
|
+
#getRendered(id) {
|
|
1380
|
+
const lists = this.#lists;
|
|
1381
|
+
if (!id || !lists) return;
|
|
1382
|
+
return [...lists.main, ...lists.more].find((destination) => destination.id === id);
|
|
1383
|
+
}
|
|
1172
1384
|
#getSuggestedAsset() {
|
|
1173
|
-
|
|
1385
|
+
const parts = this.#parts;
|
|
1386
|
+
const destination = this.#destination;
|
|
1387
|
+
if (!parts || !destination) return;
|
|
1388
|
+
const tiles = this.#getAssets();
|
|
1389
|
+
const suggested = getSuggestedImage(tiles.flatMap((tile) => parts.assets.get(tile) ?? []), destination.images);
|
|
1390
|
+
return tiles.find((tile) => parts.assets.get(tile) === suggested);
|
|
1174
1391
|
}
|
|
1175
1392
|
#getUrl() {
|
|
1176
1393
|
const payload = this.#payload;
|
|
1177
|
-
|
|
1178
|
-
|
|
1394
|
+
const destination = this.#destination;
|
|
1395
|
+
if (!payload || !destination) return payload?.url ?? "";
|
|
1396
|
+
return getDestinationUrl(payload.url, destination, payload.settings.utmMedium);
|
|
1179
1397
|
}
|
|
1180
1398
|
#handleClick = (event) => {
|
|
1181
1399
|
if (!(event.target instanceof Element)) return;
|
|
@@ -1203,10 +1421,10 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1203
1421
|
if (!move || !(event.target instanceof HTMLElement) || event.target.role !== "radio") return;
|
|
1204
1422
|
const radios = [...this.querySelectorAll("[role=\"radio\"]:not([hidden])")];
|
|
1205
1423
|
const next = radios[move(radios.indexOf(event.target), radios.length)];
|
|
1206
|
-
const
|
|
1207
|
-
if (!next || !
|
|
1424
|
+
const destination = this.#getRendered(next?.dataset.shareKitDestination);
|
|
1425
|
+
if (!next || !destination) return;
|
|
1208
1426
|
event.preventDefault();
|
|
1209
|
-
this.#selectDestination(
|
|
1427
|
+
this.#selectDestination(destination);
|
|
1210
1428
|
next.focus();
|
|
1211
1429
|
};
|
|
1212
1430
|
#handlePreviewError = (event) => {
|
|
@@ -1216,7 +1434,19 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1216
1434
|
tile.dataset.shareKitState = "failed";
|
|
1217
1435
|
this.#syncDestination();
|
|
1218
1436
|
};
|
|
1437
|
+
#handleTriggerIntent = ({ target }) => {
|
|
1438
|
+
if (target instanceof Element && target.closest("[data-share-kit-trigger]")) this.#loadIcons();
|
|
1439
|
+
};
|
|
1440
|
+
#loadIcons() {
|
|
1441
|
+
const signal = this.#abortController?.signal;
|
|
1442
|
+
if (!signal || this.#isIconsRequested) return;
|
|
1443
|
+
this.#isIconsRequested = true;
|
|
1444
|
+
this.iconSource.load().then((icons) => {
|
|
1445
|
+
if (!signal.aborted && this.#parts) patchIcons(this.#parts.dialog, icons);
|
|
1446
|
+
});
|
|
1447
|
+
}
|
|
1219
1448
|
#open() {
|
|
1449
|
+
this.#loadIcons();
|
|
1220
1450
|
const parts = this.#parts ?? this.#render();
|
|
1221
1451
|
if (!parts || parts.dialog.open) return;
|
|
1222
1452
|
parts.dialog.showModal();
|
|
@@ -1234,29 +1464,32 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1234
1464
|
}
|
|
1235
1465
|
}
|
|
1236
1466
|
#prefetch() {
|
|
1237
|
-
if (!this.#capabilities.has("files") || !this.#capabilities.has("
|
|
1467
|
+
if (!this.#capabilities.has("files") || !this.#capabilities.has("mobile")) return;
|
|
1238
1468
|
const tile = this.#getSuggestedAsset();
|
|
1239
1469
|
if (tile) this.#files?.load(tile);
|
|
1240
1470
|
}
|
|
1241
1471
|
#render() {
|
|
1242
1472
|
const payload = this.#payload;
|
|
1473
|
+
const lists = this.#lists;
|
|
1243
1474
|
const signal = this.#abortController?.signal;
|
|
1244
|
-
if (!payload || !signal) return;
|
|
1245
|
-
const { main, more } =
|
|
1475
|
+
if (!payload || !lists || !signal) return;
|
|
1476
|
+
const { main, more } = lists;
|
|
1477
|
+
const { icons, isLoading } = this.iconSource;
|
|
1246
1478
|
dialogCount += 1;
|
|
1247
1479
|
const id = `share-kit-${String(dialogCount)}`;
|
|
1248
1480
|
const parts = renderShareKit({
|
|
1249
1481
|
capabilities: this.#capabilities,
|
|
1250
1482
|
destinations: main,
|
|
1251
1483
|
destinationsMore: more,
|
|
1252
|
-
icons
|
|
1484
|
+
icons,
|
|
1253
1485
|
id,
|
|
1486
|
+
isIconsLoading: isLoading,
|
|
1254
1487
|
payload,
|
|
1255
1488
|
strings: this.#strings
|
|
1256
1489
|
});
|
|
1257
1490
|
this.#qr = createShareQr({
|
|
1258
1491
|
capabilities: this.#capabilities,
|
|
1259
|
-
closeIcon:
|
|
1492
|
+
closeIcon: icons.close,
|
|
1260
1493
|
id: `${id}-qr`,
|
|
1261
1494
|
payload,
|
|
1262
1495
|
signal,
|
|
@@ -1267,7 +1500,7 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1267
1500
|
polyfillLightDismiss(parts.dialog, signal);
|
|
1268
1501
|
this.append(parts.dialog);
|
|
1269
1502
|
this.#parts = parts;
|
|
1270
|
-
this.#destination = main[0]
|
|
1503
|
+
this.#destination = main[0];
|
|
1271
1504
|
this.#syncDestination();
|
|
1272
1505
|
return parts;
|
|
1273
1506
|
}
|
|
@@ -1284,10 +1517,10 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1284
1517
|
this.#track("save", tile);
|
|
1285
1518
|
this.#report(await this.#files?.save(tile));
|
|
1286
1519
|
}
|
|
1287
|
-
#selectDestination(
|
|
1520
|
+
#selectDestination(destination) {
|
|
1288
1521
|
const previousUrl = this.#getUrl();
|
|
1289
1522
|
const caption = this.#parts?.caption;
|
|
1290
|
-
this.#destination =
|
|
1523
|
+
this.#destination = destination;
|
|
1291
1524
|
if (caption && this.#isCaptionEdited) {
|
|
1292
1525
|
const url = this.#getUrl();
|
|
1293
1526
|
caption.value = caption.value.replaceAll(previousUrl, () => url);
|
|
@@ -1316,9 +1549,10 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1316
1549
|
}
|
|
1317
1550
|
#syncCaption() {
|
|
1318
1551
|
const parts = this.#parts;
|
|
1319
|
-
|
|
1552
|
+
const destination = this.#destination;
|
|
1553
|
+
if (!parts || !destination) return;
|
|
1320
1554
|
const { isOver, message } = syncCaptionView({
|
|
1321
|
-
destination
|
|
1555
|
+
destination,
|
|
1322
1556
|
parts,
|
|
1323
1557
|
primaryAction: this.#getPrimaryAction(),
|
|
1324
1558
|
strings: this.#strings
|
|
@@ -1329,10 +1563,11 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1329
1563
|
#syncDestination() {
|
|
1330
1564
|
const parts = this.#parts;
|
|
1331
1565
|
const payload = this.#payload;
|
|
1332
|
-
|
|
1566
|
+
const destination = this.#destination;
|
|
1567
|
+
if (!parts || !payload || !destination) return;
|
|
1333
1568
|
syncDestinationView({
|
|
1334
1569
|
assets: this.#getAssets(),
|
|
1335
|
-
destination
|
|
1570
|
+
destination,
|
|
1336
1571
|
parts,
|
|
1337
1572
|
strings: this.#strings,
|
|
1338
1573
|
suggested: this.#getSuggestedAsset()
|
|
@@ -1341,14 +1576,15 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1341
1576
|
description: payload.description,
|
|
1342
1577
|
title: payload.title,
|
|
1343
1578
|
url: this.#getUrl()
|
|
1344
|
-
},
|
|
1579
|
+
}, destination, payload.locale).text;
|
|
1345
1580
|
this.#syncCaption();
|
|
1346
1581
|
this.#prefetch();
|
|
1347
1582
|
}
|
|
1348
1583
|
#track(action, tile) {
|
|
1584
|
+
if (!this.#destination) return;
|
|
1349
1585
|
const detail = {
|
|
1350
1586
|
action,
|
|
1351
|
-
destination: this.#destination
|
|
1587
|
+
destination: this.#destination.id
|
|
1352
1588
|
};
|
|
1353
1589
|
if (tile?.dataset.shareKitAsset) detail.asset = tile.dataset.shareKitAsset;
|
|
1354
1590
|
this.dispatchEvent(new CustomEvent(shareKitActionEventName, {
|
|
@@ -1360,20 +1596,41 @@ var ShareKitElement = class extends BaseElement {
|
|
|
1360
1596
|
};
|
|
1361
1597
|
// src/define.ts
|
|
1362
1598
|
const tagName = "share-kit";
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1599
|
+
const reservedIds = new Set(shareKitInterfaceIconNames);
|
|
1600
|
+
function getCatalogue(destinations) {
|
|
1601
|
+
const catalogue = /* @__PURE__ */ new Map();
|
|
1602
|
+
for (const destination of destinations) {
|
|
1603
|
+
if (reservedIds.has(destination.id) || catalogue.has(destination.id)) {
|
|
1604
|
+
console.error(`share-kit: destination id "${destination.id}" is repeated or reserved for an icon, so <share-kit> is not defined`);
|
|
1605
|
+
return;
|
|
1606
|
+
}
|
|
1607
|
+
catalogue.set(destination.id, destination);
|
|
1608
|
+
}
|
|
1609
|
+
return catalogue;
|
|
1368
1610
|
}
|
|
1369
|
-
|
|
1370
|
-
const
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
};
|
|
1377
|
-
|
|
1611
|
+
function warnMissingComposers(destinations) {
|
|
1612
|
+
for (const { actions, composer, id } of destinations) if ([...actions.desktop, ...actions.mobile].includes("open-composer") && !composer) console.warn(`share-kit: "${id}" lists open-composer but has no composer`);
|
|
1613
|
+
}
|
|
1614
|
+
function createElementClass(catalogue, iconSource) {
|
|
1615
|
+
return class extends ShareKitElement {
|
|
1616
|
+
catalogue = catalogue;
|
|
1617
|
+
iconSource = iconSource;
|
|
1618
|
+
};
|
|
1619
|
+
}
|
|
1620
|
+
function defineShareKit({ destinations, icons = {} }) {
|
|
1621
|
+
if (customElements.get(tagName)) {
|
|
1622
|
+
console.warn("share-kit: <share-kit> is already defined, so this defineShareKit call is ignored");
|
|
1623
|
+
return;
|
|
1624
|
+
}
|
|
1625
|
+
if (destinations.length === 0) {
|
|
1626
|
+
console.error("share-kit: no destinations registered, so <share-kit> is not defined");
|
|
1627
|
+
return;
|
|
1628
|
+
}
|
|
1629
|
+
const catalogue = getCatalogue(destinations);
|
|
1630
|
+
if (!catalogue) return;
|
|
1631
|
+
warnMissingComposers(destinations);
|
|
1632
|
+
customElements.define(tagName, createElementClass(catalogue, new ShareIconSource(icons)));
|
|
1633
|
+
}
|
|
1634
|
+
export { defineShareKit, defineShareKitDestination, getShareKitImageFormats, serializeShareKitPayload, shareKitActionEventName, shareKitAnywhere, shareKitBluesky, shareKitCountGraphemes, shareKitCountLength, shareKitCountStringz, shareKitCountUtf8, shareKitCountWeighted, shareKitFacebook, shareKitInstagram, shareKitInstagramStory, shareKitInterfaceIcons, shareKitLandscapeFormat, shareKitLinkedin, shareKitMastodon, shareKitMessages, shareKitPinFormat, shareKitPinterest, shareKitPortraitFormat, shareKitReddit, shareKitShortenedLinkCost, shareKitStoryFormat, shareKitThreads, shareKitWithLinkCost, shareKitX };
|
|
1378
1635
|
|
|
1379
1636
|
//# sourceMappingURL=index.mjs.map
|