react-smart-copy 1.17.1 → 1.18.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.
Files changed (146) hide show
  1. package/README.md +22 -3
  2. package/dist/capture.cjs +30 -153
  3. package/dist/capture.cjs.map +1 -1
  4. package/dist/capture.d.cts +2 -25
  5. package/dist/capture.d.ts +2 -25
  6. package/dist/capture.js +5 -128
  7. package/dist/capture.js.map +1 -1
  8. package/dist/chunks/clipboard-adapter-Cfz-rzw3.cjs +102 -0
  9. package/dist/chunks/clipboard-adapter-Cfz-rzw3.cjs.map +1 -0
  10. package/dist/chunks/clipboard-adapter-CtH4VELE.d.cts +43 -0
  11. package/dist/chunks/clipboard-adapter-DEXkXxwV.js +97 -0
  12. package/dist/chunks/clipboard-adapter-DEXkXxwV.js.map +1 -0
  13. package/dist/chunks/clipboard-adapter-Ds0irroN.d.ts +43 -0
  14. package/dist/chunks/contrast-C6CVklEI.cjs +842 -0
  15. package/dist/chunks/contrast-C6CVklEI.cjs.map +1 -0
  16. package/dist/chunks/contrast-DpK2JIVu.js +789 -0
  17. package/dist/chunks/contrast-DpK2JIVu.js.map +1 -0
  18. package/dist/chunks/copy-interceptor-C6NABy1l.d.ts +141 -0
  19. package/dist/chunks/copy-interceptor-CZWzGOkL.d.cts +141 -0
  20. package/dist/chunks/core-Cp2HSpaz.js +1035 -0
  21. package/dist/chunks/core-Cp2HSpaz.js.map +1 -0
  22. package/dist/chunks/core-DYf_Lhbe.cjs +1142 -0
  23. package/dist/chunks/core-DYf_Lhbe.cjs.map +1 -0
  24. package/dist/chunks/deadline-C29AlS_Q.js +46 -0
  25. package/dist/chunks/deadline-C29AlS_Q.js.map +1 -0
  26. package/dist/chunks/deadline-DkiB8ZHE.cjs +51 -0
  27. package/dist/chunks/deadline-DkiB8ZHE.cjs.map +1 -0
  28. package/dist/chunks/errors-9h8sdFFX.js +120 -0
  29. package/dist/chunks/errors-9h8sdFFX.js.map +1 -0
  30. package/dist/chunks/errors-BwOF6T_M.cjs +167 -0
  31. package/dist/chunks/errors-BwOF6T_M.cjs.map +1 -0
  32. package/dist/chunks/index-mr7sSAgP.d.ts +426 -0
  33. package/dist/chunks/index-vz0iW5V2.d.cts +426 -0
  34. package/dist/chunks/mime--LB7kdqG.cjs +153 -0
  35. package/dist/chunks/mime--LB7kdqG.cjs.map +1 -0
  36. package/dist/chunks/mime-aa-HA2dm.js +100 -0
  37. package/dist/chunks/mime-aa-HA2dm.js.map +1 -0
  38. package/dist/chunks/paste-adapter--SPL4PRE.d.cts +35 -0
  39. package/dist/chunks/paste-adapter-uahXnTYk.d.ts +35 -0
  40. package/dist/chunks/paste-reader-Bo01IMC_.cjs +289 -0
  41. package/dist/chunks/paste-reader-Bo01IMC_.cjs.map +1 -0
  42. package/dist/chunks/paste-reader-D9i7yMXF.js +224 -0
  43. package/dist/chunks/paste-reader-D9i7yMXF.js.map +1 -0
  44. package/dist/chunks/paste-reader-DcXnRWQr.d.cts +96 -0
  45. package/dist/chunks/paste-reader-DcXnRWQr.d.ts +96 -0
  46. package/dist/chunks/payload-CUymxwim.cjs +100 -0
  47. package/dist/chunks/payload-CUymxwim.cjs.map +1 -0
  48. package/dist/chunks/payload-CcwKJ_bU.d.cts +162 -0
  49. package/dist/chunks/payload-Cl_8hCI5.d.ts +162 -0
  50. package/dist/chunks/payload-DGw-JOo7.js +71 -0
  51. package/dist/chunks/payload-DGw-JOo7.js.map +1 -0
  52. package/dist/chunks/payload-DI5AorDz.js +103 -0
  53. package/dist/chunks/payload-DI5AorDz.js.map +1 -0
  54. package/dist/chunks/payload-J6GjHuh3.cjs +132 -0
  55. package/dist/chunks/payload-J6GjHuh3.cjs.map +1 -0
  56. package/dist/chunks/share-Bop47Znn.js +208 -0
  57. package/dist/chunks/share-Bop47Znn.js.map +1 -0
  58. package/dist/chunks/share-C0esXvRK.d.cts +115 -0
  59. package/dist/chunks/share-ayDz974N.cjs +231 -0
  60. package/dist/chunks/share-ayDz974N.cjs.map +1 -0
  61. package/dist/chunks/share-e7FenIr2.d.ts +115 -0
  62. package/dist/chunks/text-field-Ba8W3v4R.cjs +77 -0
  63. package/dist/chunks/text-field-Ba8W3v4R.cjs.map +1 -0
  64. package/dist/chunks/text-field-C0SGmvvR.d.cts +18 -0
  65. package/dist/chunks/text-field-C0SGmvvR.d.ts +18 -0
  66. package/dist/chunks/text-field-DUvrMMaK.js +66 -0
  67. package/dist/chunks/text-field-DUvrMMaK.js.map +1 -0
  68. package/dist/chunks/url-BcMYVCXQ.js +135 -0
  69. package/dist/chunks/url-BcMYVCXQ.js.map +1 -0
  70. package/dist/chunks/url-D4PGsRm8.cjs +164 -0
  71. package/dist/chunks/url-D4PGsRm8.cjs.map +1 -0
  72. package/dist/chunks/url-DWdElCT3.d.cts +43 -0
  73. package/dist/chunks/url-DWdElCT3.d.ts +43 -0
  74. package/dist/code.cjs +20 -109
  75. package/dist/code.cjs.map +1 -1
  76. package/dist/code.d.cts +4 -28
  77. package/dist/code.d.ts +4 -28
  78. package/dist/code.js +4 -93
  79. package/dist/code.js.map +1 -1
  80. package/dist/color.cjs +21 -893
  81. package/dist/color.cjs.map +1 -1
  82. package/dist/color.d.cts +4 -28
  83. package/dist/color.d.ts +4 -28
  84. package/dist/color.js +4 -876
  85. package/dist/color.js.map +1 -1
  86. package/dist/core.cjs +52 -1659
  87. package/dist/core.d.cts +8 -878
  88. package/dist/core.d.ts +8 -878
  89. package/dist/core.js +7 -1614
  90. package/dist/image.cjs +23 -91
  91. package/dist/image.cjs.map +1 -1
  92. package/dist/image.d.cts +1 -0
  93. package/dist/image.d.ts +1 -0
  94. package/dist/image.js +5 -73
  95. package/dist/image.js.map +1 -1
  96. package/dist/index.cjs +74 -2072
  97. package/dist/index.cjs.map +1 -1
  98. package/dist/index.d.cts +8 -978
  99. package/dist/index.d.ts +8 -978
  100. package/dist/index.js +7 -2005
  101. package/dist/index.js.map +1 -1
  102. package/dist/link.cjs +24 -338
  103. package/dist/link.cjs.map +1 -1
  104. package/dist/link.d.cts +5 -83
  105. package/dist/link.d.ts +5 -83
  106. package/dist/link.js +5 -319
  107. package/dist/link.js.map +1 -1
  108. package/dist/markdown.cjs +8 -97
  109. package/dist/markdown.cjs.map +1 -1
  110. package/dist/markdown.d.cts +3 -16
  111. package/dist/markdown.d.ts +3 -16
  112. package/dist/markdown.js +3 -92
  113. package/dist/markdown.js.map +1 -1
  114. package/dist/rich.cjs +26 -934
  115. package/dist/rich.cjs.map +1 -1
  116. package/dist/rich.d.cts +4 -90
  117. package/dist/rich.d.ts +4 -90
  118. package/dist/rich.js +4 -912
  119. package/dist/rich.js.map +1 -1
  120. package/dist/share.cjs +5 -640
  121. package/dist/share.d.cts +3 -248
  122. package/dist/share.d.ts +3 -248
  123. package/dist/share.js +1 -637
  124. package/dist/structured.cjs +13 -95
  125. package/dist/structured.cjs.map +1 -1
  126. package/dist/structured.d.cts +2 -11
  127. package/dist/structured.d.ts +2 -11
  128. package/dist/structured.js +4 -86
  129. package/dist/structured.js.map +1 -1
  130. package/dist/table.cjs +11 -39
  131. package/dist/table.cjs.map +1 -1
  132. package/dist/table.d.cts +2 -12
  133. package/dist/table.d.ts +2 -12
  134. package/dist/table.js +2 -30
  135. package/dist/table.js.map +1 -1
  136. package/dist/testing.cjs +10 -43
  137. package/dist/testing.cjs.map +1 -1
  138. package/dist/testing.d.cts +6 -181
  139. package/dist/testing.d.ts +6 -181
  140. package/dist/testing.js +3 -36
  141. package/dist/testing.js.map +1 -1
  142. package/package.json +20 -2
  143. package/dist/core.cjs.map +0 -1
  144. package/dist/core.js.map +0 -1
  145. package/dist/share.cjs.map +0 -1
  146. package/dist/share.js.map +0 -1
@@ -0,0 +1,132 @@
1
+
2
+ const require_errors = require("./errors-BwOF6T_M.cjs");
3
+ const require_url = require("./url-D4PGsRm8.cjs");
4
+ //#region src/link/payload.ts
5
+ const DEFAULT_LINK_TITLE_LENGTH = 300;
6
+ const invalid = (message) => require_errors.copyFailure("invalid-payload", message);
7
+ /**
8
+ * Characters a title never keeps: controls, bidi overrides and isolates (which can
9
+ * make a title read differently), and zero-width characters.
10
+ */
11
+ const HIDDEN = /[\0-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f\u061C\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u2069\uFEFF]/g;
12
+ /** A title as one clean line: hidden characters removed, whitespace collapsed, length capped. */
13
+ function cleanTitle(title, maxLength) {
14
+ const clean = title.replace(HIDDEN, "").replace(/\s+/g, " ").trim();
15
+ if (clean.length <= maxLength) return clean;
16
+ return `${Array.from(clean).slice(0, maxLength - 1).join("").trimEnd()}\u2026`;
17
+ }
18
+ function maxTitle(value) {
19
+ if (value === void 0) return 300;
20
+ if (Number.isInteger(value) && value >= 1) return value;
21
+ throw invalid(`\`maxTitleLength\` must be a positive integer (received ${String(value)}).`);
22
+ }
23
+ function isAnchor(source) {
24
+ return typeof source === "object" && source !== null && source.nodeName === "A" && typeof source.href === "string";
25
+ }
26
+ /**
27
+ * Validates a link and its title: see {@link resolveLinkUrl} for the URL rules.
28
+ * An `<a>` element gives its `href` and its text (or `title` attribute).
29
+ */
30
+ function resolveLink(source, options = {}) {
31
+ let url;
32
+ let title;
33
+ if (typeof source === "string" || source instanceof URL) url = source;
34
+ else if (isAnchor(source)) {
35
+ url = source.getAttribute("href") ?? "";
36
+ title = source.textContent.trim() !== "" ? source.textContent : source.getAttribute("title") ?? void 0;
37
+ if (url !== "" && options.base === void 0) url = source.href;
38
+ } else if (typeof source === "object" && source !== null && "url" in source) {
39
+ url = source.url;
40
+ title = source.title;
41
+ } else throw invalid("A link must be a URL, a { url, title } object or an <a> element.");
42
+ if (title !== void 0 && typeof title !== "string") throw invalid("A link title must be a string.");
43
+ const href = require_url.resolveLinkUrl(url, options);
44
+ const clean = title === void 0 ? "" : cleanTitle(title, maxTitle(options.maxTitleLength));
45
+ return {
46
+ url: href,
47
+ title: clean === "" ? href : clean,
48
+ titled: clean !== ""
49
+ };
50
+ }
51
+ const escapeHtml = (text) => text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
52
+ /** `[title](url)`, safe in every CommonMark renderer: brackets and backslashes escaped, parentheses and spaces encoded. */
53
+ function markdownLink(link) {
54
+ return `[${link.title.replace(/[\\[\]]/g, "\\$&")}](${link.url.replace(/[()<> ]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`)})`;
55
+ }
56
+ function plainText(link, format) {
57
+ switch (format) {
58
+ case "url": return link.url;
59
+ case "title": return link.title;
60
+ case "title-and-url": return link.titled ? `${link.title}\n${link.url}` : link.url;
61
+ case "markdown": return markdownLink(link);
62
+ default: throw invalid(`\`text\` must be "url", "title", "title-and-url" or "markdown" (received ${JSON.stringify(format)}).`);
63
+ }
64
+ }
65
+ function linkPayload(source, options = {}) {
66
+ const target = options.target ?? "clipboard";
67
+ if (target !== "drag" && target !== "clipboard") throw invalid("`target` must be \"clipboard\" or \"drag\".");
68
+ const link = resolveLink(source, options);
69
+ const text = plainText(link, options.text ?? "url");
70
+ const html = `<a href="${escapeHtml(link.url)}">${escapeHtml(link.title)}</a>`;
71
+ if (target === "clipboard") return {
72
+ kind: "html",
73
+ html,
74
+ text
75
+ };
76
+ return {
77
+ kind: "multi",
78
+ items: [
79
+ {
80
+ mimeType: "text/plain",
81
+ data: text
82
+ },
83
+ {
84
+ mimeType: "text/html",
85
+ data: html
86
+ },
87
+ {
88
+ mimeType: "text/uri-list",
89
+ data: link.url,
90
+ optional: true
91
+ },
92
+ {
93
+ mimeType: "text/x-moz-url",
94
+ data: `${link.url}\n${link.title}`,
95
+ optional: true
96
+ }
97
+ ]
98
+ };
99
+ }
100
+ //#endregion
101
+ Object.defineProperty(exports, "DEFAULT_LINK_TITLE_LENGTH", {
102
+ enumerable: true,
103
+ get: function() {
104
+ return DEFAULT_LINK_TITLE_LENGTH;
105
+ }
106
+ });
107
+ Object.defineProperty(exports, "cleanTitle", {
108
+ enumerable: true,
109
+ get: function() {
110
+ return cleanTitle;
111
+ }
112
+ });
113
+ Object.defineProperty(exports, "linkPayload", {
114
+ enumerable: true,
115
+ get: function() {
116
+ return linkPayload;
117
+ }
118
+ });
119
+ Object.defineProperty(exports, "markdownLink", {
120
+ enumerable: true,
121
+ get: function() {
122
+ return markdownLink;
123
+ }
124
+ });
125
+ Object.defineProperty(exports, "resolveLink", {
126
+ enumerable: true,
127
+ get: function() {
128
+ return resolveLink;
129
+ }
130
+ });
131
+
132
+ //# sourceMappingURL=payload-J6GjHuh3.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payload-J6GjHuh3.cjs","names":["copyFailure","resolveLinkUrl"],"sources":["../../src/link/payload.ts"],"sourcesContent":["import { copyFailure } from '../core/errors';\nimport type { HtmlPayload, MultiPayload } from '../core/payload';\nimport { resolveLinkUrl, type LinkUrlOptions } from './url';\n\n/** A link with an optional title. */\nexport interface Link {\n readonly url: string | URL;\n /** What the link says when pasted into a document or chat. Default: the URL. */\n readonly title?: string;\n}\n\n/** A link: a URL, a `{ url, title }` object, or an `<a>` element (its `href` and text). */\nexport type LinkSource = string | URL | Link | HTMLAnchorElement;\n\n/** A validated link: an absolute, safe URL and a clean title. */\nexport interface ResolvedLink {\n readonly url: string;\n /** Never empty: the URL when there is no title. */\n readonly title: string;\n /** Whether `title` came from the input (`false`: it is the URL). */\n readonly titled: boolean;\n}\n\n/**\n * What the plain-text flavour holds (editors, terminals and address bars paste it):\n * `'url'` (the default), `'title'`, `'title-and-url'` (the title, then the URL on\n * the next line) or `'markdown'` (`[Title](URL)`).\n */\nexport type LinkTextFormat = 'url' | 'title' | 'title-and-url' | 'markdown';\n\nexport interface LinkPayloadOptions extends LinkUrlOptions {\n /** Default `'url'`. */\n readonly text?: LinkTextFormat;\n /** Longest title kept, in characters; longer ones end in an ellipsis. Default {@link DEFAULT_LINK_TITLE_LENGTH}. */\n readonly maxTitleLength?: number;\n}\n\nexport interface LinkDragPayloadOptions extends LinkPayloadOptions {\n /**\n * `'drag'`: a multi-format payload that also carries `text/uri-list` and\n * Firefox's `text/x-moz-url`, for `useDragCopy` and `useCopyInterceptor` (a link\n * dropped on the tab bar opens). Clipboard writes skip those two where the\n * browser can't write them.\n */\n readonly target: 'drag';\n}\n\nexport const DEFAULT_LINK_TITLE_LENGTH = 300;\n\nconst invalid = (message: string) => copyFailure('invalid-payload', message);\n\n/**\n * Characters a title never keeps: controls, bidi overrides and isolates (which can\n * make a title read differently), and zero-width characters.\n */\n// eslint-disable-next-line no-control-regex -- matching control characters is the point\nconst HIDDEN = /[\\0-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f-\\x9f\\u061C\\u200B-\\u200F\\u202A-\\u202E\\u2060-\\u2064\\u2066-\\u2069\\uFEFF]/g;\n\n/** A title as one clean line: hidden characters removed, whitespace collapsed, length capped. */\nexport function cleanTitle(title: string, maxLength: number): string {\n const clean = title.replace(HIDDEN, '').replace(/\\s+/g, ' ').trim();\n if (clean.length <= maxLength) return clean;\n // Cut on a code point boundary and mark the cut.\n return `${Array.from(clean).slice(0, maxLength - 1).join('').trimEnd()}\\u2026`;\n}\n\nfunction maxTitle(value: number | undefined): number {\n if (value === undefined) return DEFAULT_LINK_TITLE_LENGTH;\n if (Number.isInteger(value) && value >= 1) return value;\n throw invalid(`\\`maxTitleLength\\` must be a positive integer (received ${String(value)}).`);\n}\n\nfunction isAnchor(source: unknown): source is HTMLAnchorElement {\n return typeof source === 'object' && source !== null && (source as { nodeName?: unknown }).nodeName === 'A' && typeof (source as { href?: unknown }).href === 'string';\n}\n\n/**\n * Validates a link and its title: see {@link resolveLinkUrl} for the URL rules.\n * An `<a>` element gives its `href` and its text (or `title` attribute).\n */\nexport function resolveLink(source: LinkSource, options: LinkPayloadOptions = {}): ResolvedLink {\n let url: string | URL;\n let title: unknown;\n if (typeof source === 'string' || source instanceof URL) {\n url = source;\n } else if (isAnchor(source)) {\n url = source.getAttribute('href') ?? '';\n title = source.textContent.trim() !== '' ? source.textContent : (source.getAttribute('title') ?? undefined);\n if (url !== '' && options.base === undefined) url = source.href; // Resolved against the anchor's own document.\n } else if (typeof (source as unknown) === 'object' && (source as unknown) !== null && 'url' in source) {\n url = source.url;\n title = source.title;\n } else {\n throw invalid('A link must be a URL, a { url, title } object or an <a> element.');\n }\n if (title !== undefined && typeof title !== 'string') throw invalid('A link title must be a string.');\n const href = resolveLinkUrl(url, options);\n const clean = title === undefined ? '' : cleanTitle(title, maxTitle(options.maxTitleLength));\n return { url: href, title: clean === '' ? href : clean, titled: clean !== '' };\n}\n\nexport const escapeHtml = (text: string): string =>\n text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\"/g, '&quot;').replace(/'/g, '&#39;');\n\n/** `[title](url)`, safe in every CommonMark renderer: brackets and backslashes escaped, parentheses and spaces encoded. */\nexport function markdownLink(link: ResolvedLink): string {\n const label = link.title.replace(/[\\\\[\\]]/g, '\\\\$&');\n const target = link.url.replace(/[()<> ]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);\n return `[${label}](${target})`;\n}\n\nfunction plainText(link: ResolvedLink, format: LinkTextFormat): string {\n switch (format) {\n case 'url':\n return link.url;\n case 'title':\n return link.title;\n case 'title-and-url':\n return link.titled ? `${link.title}\\n${link.url}` : link.url;\n case 'markdown':\n return markdownLink(link);\n default:\n throw invalid(`\\`text\\` must be \"url\", \"title\", \"title-and-url\" or \"markdown\" (received ${JSON.stringify(format)}).`);\n }\n}\n\n/**\n * A copy payload for a link: an HTML anchor, so Slack, Google Docs, Notion,\n * Teams, Word and mail paste a titled link, and plain text (the URL by default)\n * for editors and address bars. The URL is validated (allowed schemes only,\n * credentials removed, optionally cleaned of tracking) and the title is cleaned\n * of hidden characters; both are escaped.\n *\n * @example\n * copy(() => linkPayload({ url: location.href, title: document.title }, { clean: true }));\n * useDragCopy({ payload: () => linkPayload(anchor, { target: 'drag' }) });\n */\nexport function linkPayload(source: LinkSource, options: LinkDragPayloadOptions): MultiPayload;\nexport function linkPayload(source: LinkSource, options?: LinkPayloadOptions): HtmlPayload;\nexport function linkPayload(source: LinkSource, options: LinkPayloadOptions & { readonly target?: 'drag' | 'clipboard' } = {}): HtmlPayload | MultiPayload {\n const target: unknown = options.target ?? 'clipboard'; // Plain-JS callers can pass anything.\n if (target !== 'drag' && target !== 'clipboard') throw invalid('`target` must be \"clipboard\" or \"drag\".');\n const link = resolveLink(source, options);\n const text = plainText(link, options.text ?? 'url');\n const html = `<a href=\"${escapeHtml(link.url)}\">${escapeHtml(link.title)}</a>`;\n if (target === 'clipboard') return { kind: 'html', html, text };\n return {\n kind: 'multi',\n items: [\n { mimeType: 'text/plain', data: text },\n { mimeType: 'text/html', data: html },\n { mimeType: 'text/uri-list', data: link.url, optional: true },\n // Firefox's titled-link format: the URL, a line break, the title.\n { mimeType: 'text/x-moz-url', data: `${link.url}\\n${link.title}`, optional: true },\n ],\n };\n}\n"],"mappings":";;;;AA+CA,MAAa,4BAA4B;AAEzC,MAAM,WAAW,YAAoBA,eAAAA,YAAY,mBAAmB,OAAO;;;;;AAO3E,MAAM,SAAS;;AAGf,SAAgB,WAAW,OAAe,WAA2B;CACnE,MAAM,QAAQ,MAAM,QAAQ,QAAQ,EAAE,CAAC,CAAC,QAAQ,QAAQ,GAAG,CAAC,CAAC,KAAK;CAClE,IAAI,MAAM,UAAU,WAAW,OAAO;CAEtC,OAAO,GAAG,MAAM,KAAK,KAAK,CAAC,CAAC,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,QAAQ,EAAE;AACzE;AAEA,SAAS,SAAS,OAAmC;CACnD,IAAI,UAAU,KAAA,GAAW,OAAA;CACzB,IAAI,OAAO,UAAU,KAAK,KAAK,SAAS,GAAG,OAAO;CAClD,MAAM,QAAQ,2DAA2D,OAAO,KAAK,EAAE,GAAG;AAC5F;AAEA,SAAS,SAAS,QAA8C;CAC9D,OAAO,OAAO,WAAW,YAAY,WAAW,QAAS,OA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,QAAR;EACE,KAAK,OACH,OAAO,KAAK;EACd,KAAK,SACH,OAAO,KAAK;EACd,KAAK,iBACH,OAAO,KAAK,SAAS,GAAG,KAAK,MAAM,IAAI,KAAK,QAAQ,KAAK;EAC3D,KAAK,YACH,OAAO,aAAa,IAAI;EAC1B,SACE,MAAM,QAAQ,4EAA4E,KAAK,UAAU,MAAM,EAAE,GAAG;CACxH;AACF;AAeA,SAAgB,YAAY,QAAoB,UAA2E,CAAC,GAA+B;CACzJ,MAAM,SAAkB,QAAQ,UAAU;CAC1C,IAAI,WAAW,UAAU,WAAW,aAAa,MAAM,QAAQ,6CAAyC;CACxG,MAAM,OAAO,YAAY,QAAQ,OAAO;CACxC,MAAM,OAAO,UAAU,MAAM,QAAQ,QAAQ,KAAK;CAClD,MAAM,OAAO,YAAY,WAAW,KAAK,GAAG,EAAE,IAAI,WAAW,KAAK,KAAK,EAAE;CACzE,IAAI,WAAW,aAAa,OAAO;EAAE,MAAM;EAAQ;EAAM;CAAK;CAC9D,OAAO;EACL,MAAM;EACN,OAAO;GACL;IAAE,UAAU;IAAc,MAAM;GAAK;GACrC;IAAE,UAAU;IAAa,MAAM;GAAK;GACpC;IAAE,UAAU;IAAiB,MAAM,KAAK;IAAK,UAAU;GAAK;GAE5D;IAAE,UAAU;IAAkB,MAAM,GAAG,KAAK,IAAI,IAAI,KAAK;IAAS,UAAU;GAAK;EACnF;CACF;AACF"}
@@ -0,0 +1,208 @@
1
+
2
+ import { c as toCopyError, i as describeCopyError, r as createCopyError } from "./errors-9h8sdFFX.js";
3
+ import { a as validatePayload, r as normalizePayload } from "./payload-DGw-JOo7.js";
4
+ import { t as createBrowserClipboardAdapter } from "./clipboard-adapter-DEXkXxwV.js";
5
+ import { i as linkUrlResolver } from "./url-BcMYVCXQ.js";
6
+ import { n as cleanTitle, r as linkPayload } from "./payload-DI5AorDz.js";
7
+ //#region src/share/share.ts
8
+ /** Phones and tablets: a touch screen is the main input and nothing hovers. */
9
+ const TOUCH_FIRST = "(hover: none) and (pointer: coarse)";
10
+ const defaultEnvironment = () => typeof window === "undefined" ? void 0 : window;
11
+ const invalid = (message) => createCopyError("invalid-payload", message);
12
+ /** Validates and cleans what to share, or returns the typed error for invalid input. */
13
+ function resolveContent(content, options) {
14
+ const value = content;
15
+ if (typeof value !== "object" || value === null) return invalid("Share content must be an object with a url, text or files.");
16
+ const { url, title, text, files } = value;
17
+ let href = null;
18
+ if (url !== void 0 && url !== null) try {
19
+ href = linkUrlResolver(options)(url);
20
+ } catch (error) {
21
+ return toCopyError(error);
22
+ }
23
+ if (title !== void 0 && typeof title !== "string") return invalid("`title` must be a string.");
24
+ if (text !== void 0 && typeof text !== "string") return invalid("`text` must be a string.");
25
+ const list = files ?? [];
26
+ if (!Array.isArray(list) || !list.every((file) => typeof File !== "undefined" && file instanceof File)) return invalid("`files` must be a list of File objects.");
27
+ const cleanName = title === void 0 ? "" : cleanTitle(title, 300);
28
+ const message = text === void 0 || text.trim() === "" ? null : text;
29
+ if (href === null && message === null && list.length === 0) return invalid("There is nothing to share: give a url, text or files.");
30
+ return {
31
+ url: href,
32
+ title: cleanName === "" ? null : cleanName,
33
+ text: message,
34
+ files: list
35
+ };
36
+ }
37
+ /** The `ShareData` the browser receives: only the fields that are set. */
38
+ function shareData(content) {
39
+ return {
40
+ ...content.url === null ? {} : { url: content.url },
41
+ ...content.title === null ? {} : { title: content.title },
42
+ ...content.text === null ? {} : { text: content.text },
43
+ ...content.files.length === 0 ? {} : { files: [...content.files] }
44
+ };
45
+ }
46
+ /** Whether this browser can open its share sheet for `data`. */
47
+ function canShareNatively(environment, data) {
48
+ const nav = environment?.navigator;
49
+ if (typeof nav?.share !== "function") return false;
50
+ if (data === null) return true;
51
+ return typeof nav.canShare === "function" ? nav.canShare(data) : data.files === void 0;
52
+ }
53
+ function isTouchFirst(environment) {
54
+ return typeof environment?.matchMedia === "function" && environment.matchMedia("(hover: none) and (pointer: coarse)").matches;
55
+ }
56
+ function resolveMode(mode) {
57
+ if (mode === void 0) return "auto";
58
+ return mode === "auto" || mode === "native" || mode === "copy" ? mode : invalid("`mode` must be \"auto\", \"native\" or \"copy\".");
59
+ }
60
+ /**
61
+ * How {@link shareOrCopy} would share right now: `"native"` (the share sheet) or
62
+ * `"copy"`. Use it to label the button ("Share…" or "Copy link"). Pass the
63
+ * content when it has files, since not every share sheet takes them.
64
+ */
65
+ function shareMethod(options = {}, content) {
66
+ const mode = resolveMode(options.mode);
67
+ if (typeof mode !== "string" || mode === "copy") return "copy";
68
+ const environment = options.environment ?? defaultEnvironment();
69
+ if (mode === "auto" && !isTouchFirst(environment)) return "copy";
70
+ const resolved = content === void 0 ? null : resolveContent(content, {});
71
+ const data = resolved === null ? null : "type" in resolved ? void 0 : shareData(resolved);
72
+ return data !== void 0 && canShareNatively(environment, data) ? "native" : "copy";
73
+ }
74
+ /** The default copy: a titled link when there is a URL, otherwise the text (one of them is always set here). */
75
+ function defaultCopy(content) {
76
+ if (content.url !== null) return linkPayload({
77
+ url: content.url,
78
+ ...content.title === null ? {} : { title: content.title }
79
+ });
80
+ return content.text;
81
+ }
82
+ async function copyContent(content, reason, options) {
83
+ if (options.copy === void 0 && content.url === null && content.text === null) return {
84
+ status: "error",
85
+ method: "copy",
86
+ error: createCopyError("unsupported-format", "Files can only be shared through a share sheet, and this browser has none for them.")
87
+ };
88
+ let payload;
89
+ try {
90
+ payload = normalizePayload((options.copy ?? defaultCopy)(content));
91
+ } catch (error) {
92
+ return {
93
+ status: "error",
94
+ method: "copy",
95
+ error: toCopyError(error)
96
+ };
97
+ }
98
+ const problem = validatePayload(payload);
99
+ if (problem !== null) return {
100
+ status: "error",
101
+ method: "copy",
102
+ error: problem
103
+ };
104
+ if (options.signal?.aborted === true) return {
105
+ status: "error",
106
+ method: "copy",
107
+ error: createCopyError("aborted", "The share was aborted.")
108
+ };
109
+ try {
110
+ await (options.adapter ?? createBrowserClipboardAdapter()).write(payload, options.signal === void 0 ? void 0 : { signal: options.signal });
111
+ } catch (error) {
112
+ return {
113
+ status: "error",
114
+ method: "copy",
115
+ error: toCopyError(error)
116
+ };
117
+ }
118
+ return {
119
+ status: "copied",
120
+ method: "copy",
121
+ reason,
122
+ payload,
123
+ content
124
+ };
125
+ }
126
+ /**
127
+ * Shares through the device's share sheet where people expect one, and copies a
128
+ * titled link everywhere else. Call it directly in a click handler: the share
129
+ * sheet needs the user's gesture.
130
+ *
131
+ * It never rejects. It resolves to `shared`, `copied` (with why), `cancelled`
132
+ * (the user closed the share sheet: nothing to report) or `error` with a typed
133
+ * {@link CopyError}.
134
+ *
135
+ * @example
136
+ * button.addEventListener('click', async () => {
137
+ * const outcome = await shareOrCopy({ url: location.href, title: document.title });
138
+ * if (outcome.status === 'copied') toast('Link copied');
139
+ * });
140
+ */
141
+ async function shareOrCopy(content, options = {}) {
142
+ const mode = resolveMode(options.mode);
143
+ if (typeof mode !== "string") return {
144
+ status: "error",
145
+ method: "copy",
146
+ error: mode
147
+ };
148
+ const resolved = resolveContent(content, options);
149
+ if ("type" in resolved) return {
150
+ status: "error",
151
+ method: "copy",
152
+ error: resolved
153
+ };
154
+ const environment = options.environment ?? defaultEnvironment();
155
+ if (mode === "copy" || mode === "auto" && !isTouchFirst(environment)) return copyContent(resolved, "preferred", options);
156
+ const data = shareData(resolved);
157
+ if (!canShareNatively(environment, data)) return copyContent(resolved, "unsupported", options);
158
+ try {
159
+ await (environment?.navigator?.share)(data);
160
+ return {
161
+ status: "shared",
162
+ method: "native",
163
+ content: resolved
164
+ };
165
+ } catch (error) {
166
+ if (readName(error) === "AbortError") return {
167
+ status: "cancelled",
168
+ method: "native"
169
+ };
170
+ if (options.fallback === false) return {
171
+ status: "error",
172
+ method: "native",
173
+ error: shareError(error)
174
+ };
175
+ return copyContent(resolved, "fallback", options);
176
+ }
177
+ }
178
+ function readName(error) {
179
+ const name = typeof error === "object" && error !== null ? error.name : void 0;
180
+ return typeof name === "string" ? name : void 0;
181
+ }
182
+ /** Classifies a share sheet failure. */
183
+ function shareError(cause) {
184
+ switch (readName(cause)) {
185
+ case "NotAllowedError": return createCopyError("permission-denied", "The share sheet was blocked: it needs a click, and an iframe needs the web-share permission.", cause);
186
+ case "InvalidStateError": return createCopyError("unknown", "Another share sheet is already open.", cause);
187
+ case "DataError":
188
+ case "TypeError": return createCopyError("unsupported-format", "The share sheet rejected this content.", cause);
189
+ default: return toCopyError(cause);
190
+ }
191
+ }
192
+ const SHARE_DESCRIPTIONS = {
193
+ "permission-denied": "Sharing isn't allowed here.",
194
+ "unsupported-format": "This can't be shared from this browser.",
195
+ timeout: "Sharing took too long. Try again."
196
+ };
197
+ /**
198
+ * Default English, user-facing sentence for a failed share: a share sheet
199
+ * failure is described as sharing, a failed copy as copying. Replace for i18n.
200
+ */
201
+ function describeShareError(error, method) {
202
+ if (method === "copy") return describeCopyError(error);
203
+ return SHARE_DESCRIPTIONS[error.type] ?? "Couldn't share. Try again.";
204
+ }
205
+ //#endregion
206
+ export { shareOrCopy as i, describeShareError as n, shareMethod as r, TOUCH_FIRST as t };
207
+
208
+ //# sourceMappingURL=share-Bop47Znn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"share-Bop47Znn.js","names":[],"sources":["../../src/share/share.ts"],"sourcesContent":["import { createBrowserClipboardAdapter, type ClipboardAdapter } from '../core/clipboard-adapter';\nimport { createCopyError, describeCopyError, toCopyError, type CopyError, type CopyErrorType } from '../core/errors';\nimport { normalizePayload, validatePayload, type CopyInput, type CopyPayload } from '../core/payload';\nimport { cleanTitle, DEFAULT_LINK_TITLE_LENGTH, linkPayload } from '../link/payload';\nimport { linkUrlResolver, type LinkUrlOptions } from '../link/url';\n\n/** What to share: a link, a message, files, or any mix of them. */\nexport interface ShareContent {\n /** Validated like every link in this package: allowed schemes only, credentials removed. */\n readonly url?: string | URL;\n readonly title?: string;\n /** A message to share alongside (or instead of) the link. */\n readonly text?: string;\n /** Files for the share sheet, where the browser can share them (`navigator.canShare({ files })`). Never copied. */\n readonly files?: readonly File[];\n}\n\n/** The content after validation: what the share sheet or the clipboard receives. */\nexport interface ResolvedShare {\n readonly url: string | null;\n /** Cleaned of hidden characters; `null` when there is none. */\n readonly title: string | null;\n readonly text: string | null;\n readonly files: readonly File[];\n}\n\n/**\n * How to share:\n * - `auto` (default): the share sheet on phones and tablets, where people expect it; copy elsewhere.\n * - `native`: the share sheet wherever the browser has one (also desktop Safari, Edge and Chrome on Windows and macOS).\n * - `copy`: always copy.\n */\nexport type ShareMode = 'auto' | 'native' | 'copy';\n\nexport type ShareMethod = 'native' | 'copy';\n\n/** Structural view of what sharing needs from the browser; `window` by default. */\nexport interface ShareEnvironment {\n readonly navigator?: {\n readonly share?: (data: ShareData) => Promise<void>;\n readonly canShare?: (data: ShareData) => boolean;\n };\n readonly matchMedia?: (query: string) => { readonly matches: boolean };\n}\n\nexport interface ShareOptions extends LinkUrlOptions {\n /** Default `\"auto\"`. */\n readonly mode?: ShareMode;\n /** When the share sheet fails for a reason other than the user closing it, copy instead. Default `true`. */\n readonly fallback?: boolean;\n /**\n * What to copy. Default: a titled link (`linkPayload`) when there is a URL,\n * otherwise the text. Return a string or any copy payload.\n */\n readonly copy?: (content: ResolvedShare) => CopyInput;\n /** Default: the browser Clipboard API, as `useCopy` uses. */\n readonly adapter?: ClipboardAdapter;\n /** Default: `window`. For tests, webviews and other hosts. */\n readonly environment?: ShareEnvironment;\n /** Aborting it stops a copy in progress; the share sheet itself can only be closed by the user. */\n readonly signal?: AbortSignal;\n}\n\n/**\n * Why the content was copied rather than shared: `preferred` (the mode chose\n * copy, as on desktop in `auto`), `unsupported` (no share sheet, or it can't\n * take this content) or `fallback` (the share sheet failed).\n */\nexport type CopyReason = 'preferred' | 'unsupported' | 'fallback';\n\n/** What `shareOrCopy` resolves to. It never rejects. */\nexport type ShareOutcome =\n | { readonly status: 'shared'; readonly method: 'native'; readonly content: ResolvedShare }\n | { readonly status: 'copied'; readonly method: 'copy'; readonly reason: CopyReason; readonly payload: CopyPayload; readonly content: ResolvedShare }\n /** The user closed the share sheet. Nothing to report. */\n | { readonly status: 'cancelled'; readonly method: 'native' }\n | { readonly status: 'error'; readonly method: ShareMethod; readonly error: CopyError };\n\n/** Phones and tablets: a touch screen is the main input and nothing hovers. */\nexport const TOUCH_FIRST = '(hover: none) and (pointer: coarse)';\n\nconst defaultEnvironment = (): ShareEnvironment | undefined => (typeof window === 'undefined' ? undefined : window);\n\nconst invalid = (message: string): CopyError => createCopyError('invalid-payload', message);\n\n/** Validates and cleans what to share, or returns the typed error for invalid input. */\nfunction resolveContent(content: ShareContent, options: LinkUrlOptions): ResolvedShare | CopyError {\n const value: unknown = content;\n if (typeof value !== 'object' || value === null) return invalid('Share content must be an object with a url, text or files.');\n const { url, title, text, files } = value as Record<string, unknown>;\n let href: string | null = null;\n if (url !== undefined && url !== null) {\n try {\n href = linkUrlResolver(options)(url as string | URL);\n } catch (error) {\n return toCopyError(error);\n }\n }\n if (title !== undefined && typeof title !== 'string') return invalid('`title` must be a string.');\n if (text !== undefined && typeof text !== 'string') return invalid('`text` must be a string.');\n const list: unknown = files ?? [];\n if (!Array.isArray(list) || !list.every((file: unknown) => typeof File !== 'undefined' && file instanceof File)) {\n return invalid('`files` must be a list of File objects.');\n }\n const cleanName = title === undefined ? '' : cleanTitle(title, DEFAULT_LINK_TITLE_LENGTH);\n const message = text === undefined || text.trim() === '' ? null : text;\n if (href === null && message === null && list.length === 0) return invalid('There is nothing to share: give a url, text or files.');\n return { url: href, title: cleanName === '' ? null : cleanName, text: message, files: list as File[] };\n}\n\n/** The `ShareData` the browser receives: only the fields that are set. */\nfunction shareData(content: ResolvedShare): ShareData {\n return {\n ...(content.url === null ? {} : { url: content.url }),\n ...(content.title === null ? {} : { title: content.title }),\n ...(content.text === null ? {} : { text: content.text }),\n ...(content.files.length === 0 ? {} : { files: [...content.files] }),\n };\n}\n\n/** Whether this browser can open its share sheet for `data`. */\nfunction canShareNatively(environment: ShareEnvironment | undefined, data: ShareData | null): boolean {\n const nav = environment?.navigator;\n if (typeof nav?.share !== 'function') return false;\n if (data === null) return true;\n // Without canShare (older Safari), only links and text can be shared.\n return typeof nav.canShare === 'function' ? nav.canShare(data) : data.files === undefined;\n}\n\nfunction isTouchFirst(environment: ShareEnvironment | undefined): boolean {\n return typeof environment?.matchMedia === 'function' && environment.matchMedia(TOUCH_FIRST).matches;\n}\n\nfunction resolveMode(mode: unknown): ShareMode | CopyError {\n if (mode === undefined) return 'auto';\n return mode === 'auto' || mode === 'native' || mode === 'copy' ? mode : invalid('`mode` must be \"auto\", \"native\" or \"copy\".');\n}\n\n/**\n * How {@link shareOrCopy} would share right now: `\"native\"` (the share sheet) or\n * `\"copy\"`. Use it to label the button (\"Share…\" or \"Copy link\"). Pass the\n * content when it has files, since not every share sheet takes them.\n */\nexport function shareMethod(options: Pick<ShareOptions, 'mode' | 'environment'> = {}, content?: ShareContent): ShareMethod {\n const mode = resolveMode(options.mode);\n if (typeof mode !== 'string' || mode === 'copy') return 'copy';\n const environment = options.environment ?? defaultEnvironment();\n if (mode === 'auto' && !isTouchFirst(environment)) return 'copy';\n const resolved = content === undefined ? null : resolveContent(content, {});\n const data = resolved === null ? null : 'type' in resolved ? undefined : shareData(resolved);\n return data !== undefined && canShareNatively(environment, data) ? 'native' : 'copy';\n}\n\n/** The default copy: a titled link when there is a URL, otherwise the text (one of them is always set here). */\nfunction defaultCopy(content: ResolvedShare): CopyInput {\n if (content.url !== null) return linkPayload({ url: content.url, ...(content.title === null ? {} : { title: content.title }) });\n return content.text as string;\n}\n\nasync function copyContent(content: ResolvedShare, reason: CopyReason, options: ShareOptions): Promise<ShareOutcome> {\n if (options.copy === undefined && content.url === null && content.text === null) {\n return { status: 'error', method: 'copy', error: createCopyError('unsupported-format', 'Files can only be shared through a share sheet, and this browser has none for them.') };\n }\n let payload: CopyPayload;\n try {\n payload = normalizePayload((options.copy ?? defaultCopy)(content));\n } catch (error) {\n return { status: 'error', method: 'copy', error: toCopyError(error) };\n }\n const problem = validatePayload(payload);\n if (problem !== null) return { status: 'error', method: 'copy', error: problem };\n if (options.signal?.aborted === true) return { status: 'error', method: 'copy', error: createCopyError('aborted', 'The share was aborted.') };\n try {\n await (options.adapter ?? createBrowserClipboardAdapter()).write(payload, options.signal === undefined ? undefined : { signal: options.signal });\n } catch (error) {\n return { status: 'error', method: 'copy', error: toCopyError(error) };\n }\n return { status: 'copied', method: 'copy', reason, payload, content };\n}\n\n/**\n * Shares through the device's share sheet where people expect one, and copies a\n * titled link everywhere else. Call it directly in a click handler: the share\n * sheet needs the user's gesture.\n *\n * It never rejects. It resolves to `shared`, `copied` (with why), `cancelled`\n * (the user closed the share sheet: nothing to report) or `error` with a typed\n * {@link CopyError}.\n *\n * @example\n * button.addEventListener('click', async () => {\n * const outcome = await shareOrCopy({ url: location.href, title: document.title });\n * if (outcome.status === 'copied') toast('Link copied');\n * });\n */\nexport async function shareOrCopy(content: ShareContent, options: ShareOptions = {}): Promise<ShareOutcome> {\n const mode = resolveMode(options.mode);\n if (typeof mode !== 'string') return { status: 'error', method: 'copy', error: mode };\n const resolved = resolveContent(content, options);\n if ('type' in resolved) return { status: 'error', method: 'copy', error: resolved };\n const environment = options.environment ?? defaultEnvironment();\n if (mode === 'copy' || (mode === 'auto' && !isTouchFirst(environment))) return copyContent(resolved, 'preferred', options);\n const data = shareData(resolved);\n if (!canShareNatively(environment, data)) return copyContent(resolved, 'unsupported', options);\n try {\n // Called before any await: the share sheet needs the click's user activation.\n await (environment?.navigator?.share as (data: ShareData) => Promise<void>)(data);\n return { status: 'shared', method: 'native', content: resolved };\n } catch (error) {\n if (readName(error) === 'AbortError') return { status: 'cancelled', method: 'native' };\n if (options.fallback === false) return { status: 'error', method: 'native', error: shareError(error) };\n return copyContent(resolved, 'fallback', options);\n }\n}\n\nfunction readName(error: unknown): string | undefined {\n const name: unknown = typeof error === 'object' && error !== null ? (error as { name?: unknown }).name : undefined;\n return typeof name === 'string' ? name : undefined;\n}\n\n/** Classifies a share sheet failure. */\nfunction shareError(cause: unknown): CopyError {\n switch (readName(cause)) {\n case 'NotAllowedError':\n return createCopyError('permission-denied', 'The share sheet was blocked: it needs a click, and an iframe needs the web-share permission.', cause);\n case 'InvalidStateError':\n return createCopyError('unknown', 'Another share sheet is already open.', cause);\n case 'DataError':\n case 'TypeError':\n return createCopyError('unsupported-format', 'The share sheet rejected this content.', cause);\n default:\n return toCopyError(cause);\n }\n}\n\nconst SHARE_DESCRIPTIONS: Partial<Readonly<Record<CopyErrorType, string>>> = {\n 'permission-denied': \"Sharing isn't allowed here.\",\n 'unsupported-format': \"This can't be shared from this browser.\",\n timeout: 'Sharing took too long. Try again.',\n};\n\n/**\n * Default English, user-facing sentence for a failed share: a share sheet\n * failure is described as sharing, a failed copy as copying. Replace for i18n.\n */\nexport function describeShareError(error: CopyError, method: ShareMethod): string {\n if (method === 'copy') return describeCopyError(error);\n return SHARE_DESCRIPTIONS[error.type] ?? \"Couldn't share. Try again.\";\n}\n"],"mappings":";;;;;;;;AA+EA,MAAa,cAAc;AAE3B,MAAM,2BAA0D,OAAO,WAAW,cAAc,KAAA,IAAY;AAE5G,MAAM,WAAW,YAA+B,gBAAgB,mBAAmB,OAAO;;AAG1F,SAAS,eAAe,SAAuB,SAAoD;CACjG,MAAM,QAAiB;CACvB,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM,OAAO,QAAQ,4DAA4D;CAC5H,MAAM,EAAE,KAAK,OAAO,MAAM,UAAU;CACpC,IAAI,OAAsB;CAC1B,IAAI,QAAQ,KAAA,KAAa,QAAQ,MAC/B,IAAI;EACF,OAAO,gBAAgB,OAAO,CAAC,CAAC,GAAmB;CACrD,SAAS,OAAO;EACd,OAAO,YAAY,KAAK;CAC1B;CAEF,IAAI,UAAU,KAAA,KAAa,OAAO,UAAU,UAAU,OAAO,QAAQ,2BAA2B;CAChG,IAAI,SAAS,KAAA,KAAa,OAAO,SAAS,UAAU,OAAO,QAAQ,0BAA0B;CAC7F,MAAM,OAAgB,SAAS,CAAC;CAChC,IAAI,CAAC,MAAM,QAA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,OAAQ,MAA6B,OAAO,KAAA;CACzG,OAAO,OAAO,SAAS,WAAW,OAAO,KAAA;AAC3C;;AAGA,SAAS,WAAW,OAA2B;CAC7C,QAAQ,SAAS,KAAK,GAAtB;EACE,KAAK,mBACH,OAAO,gBAAgB,qBAAqB,gGAAgG,KAAK;EACnJ,KAAK,qBACH,OAAO,gBAAgB,WAAW,wCAAwC,KAAK;EACjF,KAAK;EACL,KAAK,aACH,OAAO,gBAAgB,sBAAsB,0CAA0C,KAAK;EAC9F,SACE,OAAO,YAAY,KAAK;CAC5B;AACF;AAEA,MAAM,qBAAuE;CAC3E,qBAAqB;CACrB,sBAAsB;CACtB,SAAS;AACX;;;;;AAMA,SAAgB,mBAAmB,OAAkB,QAA6B;CAChF,IAAI,WAAW,QAAQ,OAAO,kBAAkB,KAAK;CACrD,OAAO,mBAAmB,MAAM,SAAS;AAC3C"}
@@ -0,0 +1,115 @@
1
+
2
+ import { n as CopyInput, r as CopyPayload, v as CopyError } from "./payload-CcwKJ_bU.cjs";
3
+ import { n as ClipboardAdapter } from "./clipboard-adapter-CtH4VELE.cjs";
4
+ import { r as LinkUrlOptions } from "./url-DWdElCT3.cjs";
5
+ //#region src/share/share.d.ts
6
+ /** What to share: a link, a message, files, or any mix of them. */
7
+ interface ShareContent {
8
+ /** Validated like every link in this package: allowed schemes only, credentials removed. */
9
+ readonly url?: string | URL;
10
+ readonly title?: string;
11
+ /** A message to share alongside (or instead of) the link. */
12
+ readonly text?: string;
13
+ /** Files for the share sheet, where the browser can share them (`navigator.canShare({ files })`). Never copied. */
14
+ readonly files?: readonly File[];
15
+ }
16
+ /** The content after validation: what the share sheet or the clipboard receives. */
17
+ interface ResolvedShare {
18
+ readonly url: string | null;
19
+ /** Cleaned of hidden characters; `null` when there is none. */
20
+ readonly title: string | null;
21
+ readonly text: string | null;
22
+ readonly files: readonly File[];
23
+ }
24
+ /**
25
+ * How to share:
26
+ * - `auto` (default): the share sheet on phones and tablets, where people expect it; copy elsewhere.
27
+ * - `native`: the share sheet wherever the browser has one (also desktop Safari, Edge and Chrome on Windows and macOS).
28
+ * - `copy`: always copy.
29
+ */
30
+ type ShareMode = 'auto' | 'native' | 'copy';
31
+ type ShareMethod = 'native' | 'copy';
32
+ /** Structural view of what sharing needs from the browser; `window` by default. */
33
+ interface ShareEnvironment {
34
+ readonly navigator?: {
35
+ readonly share?: (data: ShareData) => Promise<void>;
36
+ readonly canShare?: (data: ShareData) => boolean;
37
+ };
38
+ readonly matchMedia?: (query: string) => {
39
+ readonly matches: boolean;
40
+ };
41
+ }
42
+ interface ShareOptions extends LinkUrlOptions {
43
+ /** Default `"auto"`. */
44
+ readonly mode?: ShareMode;
45
+ /** When the share sheet fails for a reason other than the user closing it, copy instead. Default `true`. */
46
+ readonly fallback?: boolean;
47
+ /**
48
+ * What to copy. Default: a titled link (`linkPayload`) when there is a URL,
49
+ * otherwise the text. Return a string or any copy payload.
50
+ */
51
+ readonly copy?: (content: ResolvedShare) => CopyInput;
52
+ /** Default: the browser Clipboard API, as `useCopy` uses. */
53
+ readonly adapter?: ClipboardAdapter;
54
+ /** Default: `window`. For tests, webviews and other hosts. */
55
+ readonly environment?: ShareEnvironment;
56
+ /** Aborting it stops a copy in progress; the share sheet itself can only be closed by the user. */
57
+ readonly signal?: AbortSignal;
58
+ }
59
+ /**
60
+ * Why the content was copied rather than shared: `preferred` (the mode chose
61
+ * copy, as on desktop in `auto`), `unsupported` (no share sheet, or it can't
62
+ * take this content) or `fallback` (the share sheet failed).
63
+ */
64
+ type CopyReason = 'preferred' | 'unsupported' | 'fallback';
65
+ /** What `shareOrCopy` resolves to. It never rejects. */
66
+ type ShareOutcome = {
67
+ readonly status: 'shared';
68
+ readonly method: 'native';
69
+ readonly content: ResolvedShare;
70
+ } | {
71
+ readonly status: 'copied';
72
+ readonly method: 'copy';
73
+ readonly reason: CopyReason;
74
+ readonly payload: CopyPayload;
75
+ readonly content: ResolvedShare;
76
+ } |
77
+ /** The user closed the share sheet. Nothing to report. */
78
+ {
79
+ readonly status: 'cancelled';
80
+ readonly method: 'native';
81
+ } | {
82
+ readonly status: 'error';
83
+ readonly method: ShareMethod;
84
+ readonly error: CopyError;
85
+ };
86
+ /**
87
+ * How {@link shareOrCopy} would share right now: `"native"` (the share sheet) or
88
+ * `"copy"`. Use it to label the button ("Share…" or "Copy link"). Pass the
89
+ * content when it has files, since not every share sheet takes them.
90
+ */
91
+ declare function shareMethod(options?: Pick<ShareOptions, 'mode' | 'environment'>, content?: ShareContent): ShareMethod;
92
+ /**
93
+ * Shares through the device's share sheet where people expect one, and copies a
94
+ * titled link everywhere else. Call it directly in a click handler: the share
95
+ * sheet needs the user's gesture.
96
+ *
97
+ * It never rejects. It resolves to `shared`, `copied` (with why), `cancelled`
98
+ * (the user closed the share sheet: nothing to report) or `error` with a typed
99
+ * {@link CopyError}.
100
+ *
101
+ * @example
102
+ * button.addEventListener('click', async () => {
103
+ * const outcome = await shareOrCopy({ url: location.href, title: document.title });
104
+ * if (outcome.status === 'copied') toast('Link copied');
105
+ * });
106
+ */
107
+ declare function shareOrCopy(content: ShareContent, options?: ShareOptions): Promise<ShareOutcome>;
108
+ /**
109
+ * Default English, user-facing sentence for a failed share: a share sheet
110
+ * failure is described as sharing, a failed copy as copying. Replace for i18n.
111
+ */
112
+ declare function describeShareError(error: CopyError, method: ShareMethod): string;
113
+ //#endregion
114
+ export { ShareMethod as a, ShareOutcome as c, shareOrCopy as d, ShareEnvironment as i, describeShareError as l, ResolvedShare as n, ShareMode as o, ShareContent as r, ShareOptions as s, CopyReason as t, shareMethod as u };
115
+ //# sourceMappingURL=share-C0esXvRK.d.cts.map