react-smart-copy 1.14.0 → 1.15.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 +76 -2
- package/dist/index.cjs +523 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +145 -1
- package/dist/index.d.ts +145 -1
- package/dist/index.js +523 -35
- package/dist/index.js.map +1 -1
- package/dist/link.cjs +2 -2
- package/dist/link.cjs.map +1 -1
- package/dist/link.js +2 -2
- package/dist/link.js.map +1 -1
- package/dist/markdown.cjs +17 -1
- package/dist/markdown.cjs.map +1 -1
- package/dist/markdown.js +17 -1
- package/dist/markdown.js.map +1 -1
- package/dist/share.cjs +641 -0
- package/dist/share.cjs.map +1 -0
- package/dist/share.d.cts +248 -0
- package/dist/share.d.ts +248 -0
- package/dist/share.js +638 -0
- package/dist/share.js.map +1 -0
- package/package.json +39 -4
package/dist/index.cjs
CHANGED
|
@@ -135,40 +135,40 @@ function isBlob(value) {
|
|
|
135
135
|
function isThenable(value) {
|
|
136
136
|
return (typeof value === "object" || typeof value === "function") && value !== null && typeof value.then === "function";
|
|
137
137
|
}
|
|
138
|
-
const invalid$
|
|
138
|
+
const invalid$5 = (message) => createCopyError("invalid-payload", message);
|
|
139
139
|
/**
|
|
140
140
|
* Structural validation. Typed callers can't produce most of these states,
|
|
141
141
|
* but plain-JS callers and runtime data can, so the check is always on.
|
|
142
142
|
*/
|
|
143
143
|
function validatePayload(payload) {
|
|
144
|
-
if (typeof payload !== "object" || payload === null) return invalid$
|
|
144
|
+
if (typeof payload !== "object" || payload === null) return invalid$5("Copy input must be a string or a payload object.");
|
|
145
145
|
const candidate = payload;
|
|
146
146
|
switch (candidate["kind"]) {
|
|
147
|
-
case "text": return typeof candidate["value"] === "string" && candidate["value"].length > 0 ? null : invalid$
|
|
147
|
+
case "text": return typeof candidate["value"] === "string" && candidate["value"].length > 0 ? null : invalid$5("Text payload must be a non-empty string.");
|
|
148
148
|
case "html":
|
|
149
|
-
if (typeof candidate["html"] !== "string" || candidate["html"].length === 0) return invalid$
|
|
150
|
-
return typeof candidate["text"] === "string" && candidate["text"].length > 0 ? null : invalid$
|
|
149
|
+
if (typeof candidate["html"] !== "string" || candidate["html"].length === 0) return invalid$5("HTML payload requires a non-empty `html` string.");
|
|
150
|
+
return typeof candidate["text"] === "string" && candidate["text"].length > 0 ? null : invalid$5("HTML payload requires a non-empty plain-text `text` fallback.");
|
|
151
151
|
case "image": {
|
|
152
152
|
const source = candidate["blob"];
|
|
153
|
-
return isBlob(source) || typeof source === "function" || isThenable(source) ? null : invalid$
|
|
153
|
+
return isBlob(source) || typeof source === "function" || isThenable(source) ? null : invalid$5("Image payload requires a Blob, a Promise<Blob> or a function returning one.");
|
|
154
154
|
}
|
|
155
|
-
case "json": return candidate["value"] === void 0 ? invalid$
|
|
155
|
+
case "json": return candidate["value"] === void 0 ? invalid$5("JSON payload value is undefined.") : null;
|
|
156
156
|
case "multi": {
|
|
157
157
|
const items = candidate["items"];
|
|
158
|
-
if (!Array.isArray(items) || items.length === 0) return invalid$
|
|
158
|
+
if (!Array.isArray(items) || items.length === 0) return invalid$5("Multi payload requires at least one item.");
|
|
159
159
|
const seen = /* @__PURE__ */ new Set();
|
|
160
160
|
for (const item of items) {
|
|
161
161
|
const entry = typeof item === "object" && item !== null ? item : {};
|
|
162
162
|
const mimeType = entry["mimeType"];
|
|
163
163
|
const data = entry["data"];
|
|
164
|
-
if (typeof mimeType !== "string" || mimeType.length === 0) return invalid$
|
|
165
|
-
if (seen.has(mimeType)) return invalid$
|
|
164
|
+
if (typeof mimeType !== "string" || mimeType.length === 0) return invalid$5("Every multi item needs a non-empty `mimeType`.");
|
|
165
|
+
if (seen.has(mimeType)) return invalid$5(`Duplicate MIME type "${mimeType}" in multi payload.`);
|
|
166
166
|
seen.add(mimeType);
|
|
167
|
-
if (typeof data !== "string" && !isBlob(data)) return invalid$
|
|
167
|
+
if (typeof data !== "string" && !isBlob(data)) return invalid$5(`Multi item "${mimeType}" must contain a string or a Blob.`);
|
|
168
168
|
}
|
|
169
169
|
return null;
|
|
170
170
|
}
|
|
171
|
-
default: return invalid$
|
|
171
|
+
default: return invalid$5(`Unknown payload kind: ${String(candidate["kind"])}.`);
|
|
172
172
|
}
|
|
173
173
|
}
|
|
174
174
|
/** Serialises a JSON payload, throwing a classified failure instead of a raw TypeError. */
|
|
@@ -283,7 +283,7 @@ async function sniffImageType(blob) {
|
|
|
283
283
|
//#region src/core/clipboard-adapter.ts
|
|
284
284
|
const fail = (type, message, cause) => new CopyFailure(createCopyError(type, message, cause));
|
|
285
285
|
const noop = () => void 0;
|
|
286
|
-
function defaultEnvironment$
|
|
286
|
+
function defaultEnvironment$2() {
|
|
287
287
|
return typeof window === "undefined" ? void 0 : window;
|
|
288
288
|
}
|
|
289
289
|
function getRichClipboard(env, clipboard) {
|
|
@@ -366,7 +366,7 @@ async function writeToClipboard(payload, env, degradeHtmlToText, context) {
|
|
|
366
366
|
}
|
|
367
367
|
/** The default adapter: async Clipboard API with feature detection and honest errors. */
|
|
368
368
|
function createBrowserClipboardAdapter(options = {}) {
|
|
369
|
-
const getEnvironment = options.getEnvironment ?? defaultEnvironment$
|
|
369
|
+
const getEnvironment = options.getEnvironment ?? defaultEnvironment$2;
|
|
370
370
|
const degradeHtmlToText = options.degradeHtmlToText ?? true;
|
|
371
371
|
return { write: (payload, context) => writeToClipboard(payload, getEnvironment(), degradeHtmlToText, context) };
|
|
372
372
|
}
|
|
@@ -738,7 +738,7 @@ function readCopySelection(event) {
|
|
|
738
738
|
return read ? read.selection : null;
|
|
739
739
|
}
|
|
740
740
|
const nonEmpty$1 = (value) => typeof value === "string" && value.length > 0;
|
|
741
|
-
const invalid$
|
|
741
|
+
const invalid$4 = (message, cause) => ({
|
|
742
742
|
ok: false,
|
|
743
743
|
error: createCopyError("invalid-payload", message, cause)
|
|
744
744
|
});
|
|
@@ -756,8 +756,8 @@ function prepare(decision) {
|
|
|
756
756
|
kind: "text",
|
|
757
757
|
value: decision
|
|
758
758
|
} : decision;
|
|
759
|
-
if (typeof candidate !== "object" || candidate === null) return invalid$
|
|
760
|
-
if (typeof candidate.then === "function") return invalid$
|
|
759
|
+
if (typeof candidate !== "object" || candidate === null) return invalid$4("Copy transforms must return a string, a payload, false, or nothing.");
|
|
760
|
+
if (typeof candidate.then === "function") return invalid$4("Copy transforms must be synchronous; the clipboard closes when the event ends.");
|
|
761
761
|
const payload = candidate;
|
|
762
762
|
const fields = candidate;
|
|
763
763
|
const ok = (entries) => ({
|
|
@@ -766,8 +766,8 @@ function prepare(decision) {
|
|
|
766
766
|
entries
|
|
767
767
|
});
|
|
768
768
|
switch (fields["kind"]) {
|
|
769
|
-
case "text": return nonEmpty$1(fields["value"]) ? ok([["text/plain", fields["value"]]]) : invalid$
|
|
770
|
-
case "html": return nonEmpty$1(fields["html"]) && nonEmpty$1(fields["text"]) ? ok([["text/html", fields["html"]], ["text/plain", fields["text"]]]) : invalid$
|
|
769
|
+
case "text": return nonEmpty$1(fields["value"]) ? ok([["text/plain", fields["value"]]]) : invalid$4("Text payload must be a non-empty string.");
|
|
770
|
+
case "html": return nonEmpty$1(fields["html"]) && nonEmpty$1(fields["text"]) ? ok([["text/html", fields["html"]], ["text/plain", fields["text"]]]) : invalid$4("HTML payload requires non-empty `html` and plain-text `text`.");
|
|
771
771
|
case "json": {
|
|
772
772
|
const pretty = fields["pretty"];
|
|
773
773
|
const indent = pretty === true ? 2 : typeof pretty === "number" ? pretty : void 0;
|
|
@@ -775,17 +775,17 @@ function prepare(decision) {
|
|
|
775
775
|
try {
|
|
776
776
|
json = JSON.stringify(fields["value"], null, indent);
|
|
777
777
|
} catch (cause) {
|
|
778
|
-
return invalid$
|
|
778
|
+
return invalid$4("JSON payload could not be serialised (circular reference or BigInt?).", cause);
|
|
779
779
|
}
|
|
780
|
-
return typeof json === "string" ? ok([["text/plain", json]]) : invalid$
|
|
780
|
+
return typeof json === "string" ? ok([["text/plain", json]]) : invalid$4("JSON payload serialised to nothing.");
|
|
781
781
|
}
|
|
782
782
|
case "multi": {
|
|
783
783
|
const items = fields["items"];
|
|
784
|
-
if (!Array.isArray(items) || items.length === 0) return invalid$
|
|
784
|
+
if (!Array.isArray(items) || items.length === 0) return invalid$4("Multi payload requires at least one item.");
|
|
785
785
|
const entries = [];
|
|
786
786
|
for (const item of items) {
|
|
787
787
|
const { mimeType, data } = item ?? {};
|
|
788
|
-
if (!nonEmpty$1(mimeType) || entries.some(([seen]) => seen === mimeType)) return invalid$
|
|
788
|
+
if (!nonEmpty$1(mimeType) || entries.some(([seen]) => seen === mimeType)) return invalid$4("Every multi item needs a unique, non-empty `mimeType`.");
|
|
789
789
|
if (typeof data !== "string") return unsupported$1(`Multi item "${mimeType}" must be a string during a copy event.`);
|
|
790
790
|
entries.push([mimeType, data]);
|
|
791
791
|
}
|
|
@@ -965,7 +965,7 @@ function registerCopyInterceptor(root, options) {
|
|
|
965
965
|
* HTML5 drag-and-drop (all modern browsers and IE11+).
|
|
966
966
|
*/
|
|
967
967
|
const nonEmpty = (value) => typeof value === "string" && value.length > 0;
|
|
968
|
-
const invalid = (message, cause) => ({
|
|
968
|
+
const invalid$3 = (message, cause) => ({
|
|
969
969
|
ok: false,
|
|
970
970
|
error: createCopyError("invalid-payload", message, cause)
|
|
971
971
|
});
|
|
@@ -978,8 +978,8 @@ function prepareDragDecision(decision) {
|
|
|
978
978
|
kind: "text",
|
|
979
979
|
value: decision
|
|
980
980
|
} : decision;
|
|
981
|
-
if (typeof candidate !== "object" || candidate === null) return invalid("Drag source must return a string, a payload, false, or nothing.");
|
|
982
|
-
if (typeof candidate.then === "function") return invalid("Drag sources must be synchronous; DataTransfer closes when dragstart returns.");
|
|
981
|
+
if (typeof candidate !== "object" || candidate === null) return invalid$3("Drag source must return a string, a payload, false, or nothing.");
|
|
982
|
+
if (typeof candidate.then === "function") return invalid$3("Drag sources must be synchronous; DataTransfer closes when dragstart returns.");
|
|
983
983
|
const payload = candidate;
|
|
984
984
|
const fields = candidate;
|
|
985
985
|
const ok = (items) => ({
|
|
@@ -991,14 +991,14 @@ function prepareDragDecision(decision) {
|
|
|
991
991
|
case "text": return nonEmpty(fields["value"]) ? ok([{
|
|
992
992
|
mimeType: "text/plain",
|
|
993
993
|
data: fields["value"]
|
|
994
|
-
}]) : invalid("Text payload must be a non-empty string.");
|
|
994
|
+
}]) : invalid$3("Text payload must be a non-empty string.");
|
|
995
995
|
case "html": return nonEmpty(fields["html"]) && nonEmpty(fields["text"]) ? ok([{
|
|
996
996
|
mimeType: "text/html",
|
|
997
997
|
data: fields["html"]
|
|
998
998
|
}, {
|
|
999
999
|
mimeType: "text/plain",
|
|
1000
1000
|
data: fields["text"]
|
|
1001
|
-
}]) : invalid("HTML payload requires non-empty `html` and `text`.");
|
|
1001
|
+
}]) : invalid$3("HTML payload requires non-empty `html` and `text`.");
|
|
1002
1002
|
case "json": {
|
|
1003
1003
|
const pretty = fields["pretty"];
|
|
1004
1004
|
const indent = pretty === true ? 2 : typeof pretty === "number" ? pretty : void 0;
|
|
@@ -1006,20 +1006,20 @@ function prepareDragDecision(decision) {
|
|
|
1006
1006
|
try {
|
|
1007
1007
|
json = JSON.stringify(fields["value"], null, indent);
|
|
1008
1008
|
} catch (cause) {
|
|
1009
|
-
return invalid("JSON payload could not be serialised (circular reference or BigInt?).", cause);
|
|
1009
|
+
return invalid$3("JSON payload could not be serialised (circular reference or BigInt?).", cause);
|
|
1010
1010
|
}
|
|
1011
1011
|
return typeof json === "string" ? ok([{
|
|
1012
1012
|
mimeType: "text/plain",
|
|
1013
1013
|
data: json
|
|
1014
|
-
}]) : invalid("JSON payload serialised to nothing.");
|
|
1014
|
+
}]) : invalid$3("JSON payload serialised to nothing.");
|
|
1015
1015
|
}
|
|
1016
1016
|
case "multi": {
|
|
1017
1017
|
const items = fields["items"];
|
|
1018
|
-
if (!Array.isArray(items) || items.length === 0) return invalid("Multi payload requires at least one item.");
|
|
1018
|
+
if (!Array.isArray(items) || items.length === 0) return invalid$3("Multi payload requires at least one item.");
|
|
1019
1019
|
const out = [];
|
|
1020
1020
|
for (const item of items) {
|
|
1021
1021
|
const { mimeType, data } = item ?? {};
|
|
1022
|
-
if (!nonEmpty(mimeType) || out.some((i) => i.mimeType === mimeType)) return invalid("Every multi item needs a unique, non-empty `mimeType`.");
|
|
1022
|
+
if (!nonEmpty(mimeType) || out.some((i) => i.mimeType === mimeType)) return invalid$3("Every multi item needs a unique, non-empty `mimeType`.");
|
|
1023
1023
|
if (typeof data !== "string") return unsupported(`Multi item "${mimeType}" must be a string; DataTransfer does not accept Blobs.`);
|
|
1024
1024
|
out.push({
|
|
1025
1025
|
mimeType,
|
|
@@ -1368,7 +1368,7 @@ async function finalizePaste(entries, source, options) {
|
|
|
1368
1368
|
}
|
|
1369
1369
|
//#endregion
|
|
1370
1370
|
//#region src/core/paste-adapter.ts
|
|
1371
|
-
function defaultEnvironment() {
|
|
1371
|
+
function defaultEnvironment$1() {
|
|
1372
1372
|
return typeof window === "undefined" ? void 0 : window;
|
|
1373
1373
|
}
|
|
1374
1374
|
const canRead = (clipboard) => typeof clipboard.read === "function";
|
|
@@ -1410,7 +1410,7 @@ function readFromClipboard(env, options, signal) {
|
|
|
1410
1410
|
}
|
|
1411
1411
|
/** The default paste adapter: async Clipboard API with feature detection and honest errors. */
|
|
1412
1412
|
function createBrowserPasteAdapter(options = {}) {
|
|
1413
|
-
const getEnvironment = options.getEnvironment ?? defaultEnvironment;
|
|
1413
|
+
const getEnvironment = options.getEnvironment ?? defaultEnvironment$1;
|
|
1414
1414
|
return { read: (resolved, context) => {
|
|
1415
1415
|
try {
|
|
1416
1416
|
return readFromClipboard(getEnvironment(), resolved, context?.signal);
|
|
@@ -2623,6 +2623,494 @@ function useDragCopy(options) {
|
|
|
2623
2623
|
};
|
|
2624
2624
|
}
|
|
2625
2625
|
//#endregion
|
|
2626
|
+
//#region src/link/url.ts
|
|
2627
|
+
/** Schemes a copied or pasted link may use by default. Never `javascript:`, `data:`, `vbscript:` or `file:`. */
|
|
2628
|
+
const DEFAULT_LINK_PROTOCOLS = /* @__PURE__ */ Object.freeze([
|
|
2629
|
+
"https:",
|
|
2630
|
+
"http:",
|
|
2631
|
+
"mailto:",
|
|
2632
|
+
"tel:"
|
|
2633
|
+
]);
|
|
2634
|
+
/**
|
|
2635
|
+
* Query parameters that only track where a click came from: the defaults of
|
|
2636
|
+
* {@link cleanLinkUrl}. `utm_` matches every parameter starting with it.
|
|
2637
|
+
*/
|
|
2638
|
+
const TRACKING_PARAMETERS = /* @__PURE__ */ Object.freeze([
|
|
2639
|
+
"utm_",
|
|
2640
|
+
"fbclid",
|
|
2641
|
+
"gclid",
|
|
2642
|
+
"gclsrc",
|
|
2643
|
+
"dclid",
|
|
2644
|
+
"gbraid",
|
|
2645
|
+
"wbraid",
|
|
2646
|
+
"msclkid",
|
|
2647
|
+
"yclid",
|
|
2648
|
+
"twclid",
|
|
2649
|
+
"ttclid",
|
|
2650
|
+
"li_fat_id",
|
|
2651
|
+
"igshid",
|
|
2652
|
+
"igsh",
|
|
2653
|
+
"mc_cid",
|
|
2654
|
+
"mc_eid",
|
|
2655
|
+
"_hsenc",
|
|
2656
|
+
"_hsmi",
|
|
2657
|
+
"mkt_tok",
|
|
2658
|
+
"oly_anon_id",
|
|
2659
|
+
"oly_enc_id",
|
|
2660
|
+
"vero_id",
|
|
2661
|
+
"wickedid",
|
|
2662
|
+
"_gl"
|
|
2663
|
+
]);
|
|
2664
|
+
const invalid$2 = (message) => copyFailure("invalid-payload", message);
|
|
2665
|
+
function baseUrl(base) {
|
|
2666
|
+
if (base !== void 0) {
|
|
2667
|
+
if (typeof base === "string" || base instanceof URL) return String(base);
|
|
2668
|
+
throw invalid$2("`base` must be a string or a URL.");
|
|
2669
|
+
}
|
|
2670
|
+
return typeof location === "undefined" ? void 0 : location.href;
|
|
2671
|
+
}
|
|
2672
|
+
function protocolSet(protocols) {
|
|
2673
|
+
const list = protocols ?? DEFAULT_LINK_PROTOCOLS;
|
|
2674
|
+
const valid = (protocol) => typeof protocol === "string" && /^[a-z][a-z\d+.-]*:$/i.test(protocol);
|
|
2675
|
+
if (!Array.isArray(list) || list.length === 0 || !list.every(valid)) throw invalid$2("`protocols` must be a non-empty list of schemes with their colon, like \"https:\".");
|
|
2676
|
+
return new Set(list.map((protocol) => protocol.toLowerCase()));
|
|
2677
|
+
}
|
|
2678
|
+
function matcher(params) {
|
|
2679
|
+
const list = params ?? TRACKING_PARAMETERS;
|
|
2680
|
+
if (!Array.isArray(list) || !list.every((param) => typeof param === "string" || param instanceof RegExp)) throw invalid$2("`clean.params` must be a list of strings and regular expressions.");
|
|
2681
|
+
return (name) => {
|
|
2682
|
+
const lower = name.toLowerCase();
|
|
2683
|
+
return list.some((param) => {
|
|
2684
|
+
if (typeof param !== "string") return new RegExp(param.source, param.flags.replace(/[gy]/g, "")).test(name);
|
|
2685
|
+
const wanted = param.toLowerCase();
|
|
2686
|
+
return wanted.endsWith("_") ? lower.startsWith(wanted) : lower === wanted;
|
|
2687
|
+
});
|
|
2688
|
+
};
|
|
2689
|
+
}
|
|
2690
|
+
/** The cleaning step `clean` asks for, or `null`. Throws for an invalid option. */
|
|
2691
|
+
function cleaner(option) {
|
|
2692
|
+
const cleaning = option ?? false;
|
|
2693
|
+
if (cleaning === false) return null;
|
|
2694
|
+
if (cleaning !== true && (typeof cleaning !== "object" || cleaning === null)) throw invalid$2("`clean` must be a boolean or an options object.");
|
|
2695
|
+
const settings = cleaning === true ? {} : cleaning;
|
|
2696
|
+
const tracking = matcher(settings.params);
|
|
2697
|
+
const keep = settings.keep ?? [];
|
|
2698
|
+
if (!Array.isArray(keep) || !keep.every((name) => typeof name === "string")) throw invalid$2("`clean.keep` must be a list of strings.");
|
|
2699
|
+
const kept = new Set(keep.map((name) => name.toLowerCase()));
|
|
2700
|
+
return (url) => {
|
|
2701
|
+
for (const name of [...new Set(url.searchParams.keys())]) if (tracking(name) && !kept.has(name.toLowerCase())) url.searchParams.delete(name);
|
|
2702
|
+
};
|
|
2703
|
+
}
|
|
2704
|
+
/**
|
|
2705
|
+
* Validates `options` once (throwing a typed `invalid-payload` error for invalid
|
|
2706
|
+
* ones) and returns a resolver that throws only for invalid links.
|
|
2707
|
+
* @internal
|
|
2708
|
+
*/
|
|
2709
|
+
function linkUrlResolver(options = {}) {
|
|
2710
|
+
const protocols = protocolSet(options.protocols);
|
|
2711
|
+
const base = baseUrl(options.base);
|
|
2712
|
+
const clean = cleaner(options.clean);
|
|
2713
|
+
return (input) => {
|
|
2714
|
+
if (typeof input !== "string" && !(input instanceof URL)) throw invalid$2("A link must be a string or a URL.");
|
|
2715
|
+
const raw = String(input).trim();
|
|
2716
|
+
if (raw === "") throw invalid$2("A link must not be empty.");
|
|
2717
|
+
let url;
|
|
2718
|
+
try {
|
|
2719
|
+
url = new URL(raw, base);
|
|
2720
|
+
} catch (cause) {
|
|
2721
|
+
throw copyFailure("invalid-payload", `Not a valid link: ${JSON.stringify(raw.slice(0, 200))}.`, cause);
|
|
2722
|
+
}
|
|
2723
|
+
if (!protocols.has(url.protocol)) throw invalid$2(`Links with the scheme "${url.protocol}" are not allowed (allowed: ${[...protocols].join(", ")}).`);
|
|
2724
|
+
url.username = "";
|
|
2725
|
+
url.password = "";
|
|
2726
|
+
clean?.(url);
|
|
2727
|
+
return url.href;
|
|
2728
|
+
};
|
|
2729
|
+
}
|
|
2730
|
+
/**
|
|
2731
|
+
* The link as an absolute, safe URL string, or a typed `invalid-payload` error:
|
|
2732
|
+
* relative links are resolved, the scheme must be allowed, user names and
|
|
2733
|
+
* passwords are always removed, and tracking parameters optionally.
|
|
2734
|
+
*/
|
|
2735
|
+
function resolveLinkUrl(input, options = {}) {
|
|
2736
|
+
return linkUrlResolver(options)(input);
|
|
2737
|
+
}
|
|
2738
|
+
const invalid$1 = (message) => copyFailure("invalid-payload", message);
|
|
2739
|
+
/**
|
|
2740
|
+
* Characters a title never keeps: controls, bidi overrides and isolates (which can
|
|
2741
|
+
* make a title read differently), and zero-width characters.
|
|
2742
|
+
*/
|
|
2743
|
+
const HIDDEN = /[\0-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f\u061C\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u2069\uFEFF]/g;
|
|
2744
|
+
/** A title as one clean line: hidden characters removed, whitespace collapsed, length capped. */
|
|
2745
|
+
function cleanTitle(title, maxLength) {
|
|
2746
|
+
const clean = title.replace(HIDDEN, "").replace(/\s+/g, " ").trim();
|
|
2747
|
+
if (clean.length <= maxLength) return clean;
|
|
2748
|
+
return `${Array.from(clean).slice(0, maxLength - 1).join("").trimEnd()}\u2026`;
|
|
2749
|
+
}
|
|
2750
|
+
function maxTitle(value) {
|
|
2751
|
+
if (value === void 0) return 300;
|
|
2752
|
+
if (Number.isInteger(value) && value >= 1) return value;
|
|
2753
|
+
throw invalid$1(`\`maxTitleLength\` must be a positive integer (received ${String(value)}).`);
|
|
2754
|
+
}
|
|
2755
|
+
function isAnchor(source) {
|
|
2756
|
+
return typeof source === "object" && source !== null && source.nodeName === "A" && typeof source.href === "string";
|
|
2757
|
+
}
|
|
2758
|
+
/**
|
|
2759
|
+
* Validates a link and its title: see {@link resolveLinkUrl} for the URL rules.
|
|
2760
|
+
* An `<a>` element gives its `href` and its text (or `title` attribute).
|
|
2761
|
+
*/
|
|
2762
|
+
function resolveLink(source, options = {}) {
|
|
2763
|
+
let url;
|
|
2764
|
+
let title;
|
|
2765
|
+
if (typeof source === "string" || source instanceof URL) url = source;
|
|
2766
|
+
else if (isAnchor(source)) {
|
|
2767
|
+
url = source.getAttribute("href") ?? "";
|
|
2768
|
+
title = source.textContent.trim() !== "" ? source.textContent : source.getAttribute("title") ?? void 0;
|
|
2769
|
+
if (url !== "" && options.base === void 0) url = source.href;
|
|
2770
|
+
} else if (typeof source === "object" && source !== null && "url" in source) {
|
|
2771
|
+
url = source.url;
|
|
2772
|
+
title = source.title;
|
|
2773
|
+
} else throw invalid$1("A link must be a URL, a { url, title } object or an <a> element.");
|
|
2774
|
+
if (title !== void 0 && typeof title !== "string") throw invalid$1("A link title must be a string.");
|
|
2775
|
+
const href = resolveLinkUrl(url, options);
|
|
2776
|
+
const clean = title === void 0 ? "" : cleanTitle(title, maxTitle(options.maxTitleLength));
|
|
2777
|
+
return {
|
|
2778
|
+
url: href,
|
|
2779
|
+
title: clean === "" ? href : clean,
|
|
2780
|
+
titled: clean !== ""
|
|
2781
|
+
};
|
|
2782
|
+
}
|
|
2783
|
+
const escapeHtml = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
2784
|
+
/** `[title](url)`, safe in every CommonMark renderer: brackets and backslashes escaped, parentheses and spaces encoded. */
|
|
2785
|
+
function markdownLink(link) {
|
|
2786
|
+
return `[${link.title.replace(/[\\[\]]/g, "\\$&")}](${link.url.replace(/[()<> ]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`)})`;
|
|
2787
|
+
}
|
|
2788
|
+
function plainText(link, format) {
|
|
2789
|
+
switch (format) {
|
|
2790
|
+
case "url": return link.url;
|
|
2791
|
+
case "title": return link.title;
|
|
2792
|
+
case "title-and-url": return link.titled ? `${link.title}\n${link.url}` : link.url;
|
|
2793
|
+
case "markdown": return markdownLink(link);
|
|
2794
|
+
default: throw invalid$1(`\`text\` must be "url", "title", "title-and-url" or "markdown" (received ${JSON.stringify(format)}).`);
|
|
2795
|
+
}
|
|
2796
|
+
}
|
|
2797
|
+
function linkPayload(source, options = {}) {
|
|
2798
|
+
const target = options.target ?? "clipboard";
|
|
2799
|
+
if (target !== "drag" && target !== "clipboard") throw invalid$1("`target` must be \"clipboard\" or \"drag\".");
|
|
2800
|
+
const link = resolveLink(source, options);
|
|
2801
|
+
const text = plainText(link, options.text ?? "url");
|
|
2802
|
+
const html = `<a href="${escapeHtml(link.url)}">${escapeHtml(link.title)}</a>`;
|
|
2803
|
+
if (target === "clipboard") return {
|
|
2804
|
+
kind: "html",
|
|
2805
|
+
html,
|
|
2806
|
+
text
|
|
2807
|
+
};
|
|
2808
|
+
return {
|
|
2809
|
+
kind: "multi",
|
|
2810
|
+
items: [
|
|
2811
|
+
{
|
|
2812
|
+
mimeType: "text/plain",
|
|
2813
|
+
data: text
|
|
2814
|
+
},
|
|
2815
|
+
{
|
|
2816
|
+
mimeType: "text/html",
|
|
2817
|
+
data: html
|
|
2818
|
+
},
|
|
2819
|
+
{
|
|
2820
|
+
mimeType: "text/uri-list",
|
|
2821
|
+
data: link.url,
|
|
2822
|
+
optional: true
|
|
2823
|
+
},
|
|
2824
|
+
{
|
|
2825
|
+
mimeType: "text/x-moz-url",
|
|
2826
|
+
data: `${link.url}\n${link.title}`,
|
|
2827
|
+
optional: true
|
|
2828
|
+
}
|
|
2829
|
+
]
|
|
2830
|
+
};
|
|
2831
|
+
}
|
|
2832
|
+
//#endregion
|
|
2833
|
+
//#region src/share/share.ts
|
|
2834
|
+
/** Phones and tablets: a touch screen is the main input and nothing hovers. */
|
|
2835
|
+
const TOUCH_FIRST = "(hover: none) and (pointer: coarse)";
|
|
2836
|
+
const defaultEnvironment = () => typeof window === "undefined" ? void 0 : window;
|
|
2837
|
+
const invalid = (message) => createCopyError("invalid-payload", message);
|
|
2838
|
+
/** Validates and cleans what to share, or returns the typed error for invalid input. */
|
|
2839
|
+
function resolveContent(content, options) {
|
|
2840
|
+
const value = content;
|
|
2841
|
+
if (typeof value !== "object" || value === null) return invalid("Share content must be an object with a url, text or files.");
|
|
2842
|
+
const { url, title, text, files } = value;
|
|
2843
|
+
let href = null;
|
|
2844
|
+
if (url !== void 0 && url !== null) try {
|
|
2845
|
+
href = linkUrlResolver(options)(url);
|
|
2846
|
+
} catch (error) {
|
|
2847
|
+
return toCopyError(error);
|
|
2848
|
+
}
|
|
2849
|
+
if (title !== void 0 && typeof title !== "string") return invalid("`title` must be a string.");
|
|
2850
|
+
if (text !== void 0 && typeof text !== "string") return invalid("`text` must be a string.");
|
|
2851
|
+
const list = files ?? [];
|
|
2852
|
+
if (!Array.isArray(list) || !list.every((file) => typeof File !== "undefined" && file instanceof File)) return invalid("`files` must be a list of File objects.");
|
|
2853
|
+
const cleanName = title === void 0 ? "" : cleanTitle(title, 300);
|
|
2854
|
+
const message = text === void 0 || text.trim() === "" ? null : text;
|
|
2855
|
+
if (href === null && message === null && list.length === 0) return invalid("There is nothing to share: give a url, text or files.");
|
|
2856
|
+
return {
|
|
2857
|
+
url: href,
|
|
2858
|
+
title: cleanName === "" ? null : cleanName,
|
|
2859
|
+
text: message,
|
|
2860
|
+
files: list
|
|
2861
|
+
};
|
|
2862
|
+
}
|
|
2863
|
+
/** The `ShareData` the browser receives: only the fields that are set. */
|
|
2864
|
+
function shareData(content) {
|
|
2865
|
+
return {
|
|
2866
|
+
...content.url === null ? {} : { url: content.url },
|
|
2867
|
+
...content.title === null ? {} : { title: content.title },
|
|
2868
|
+
...content.text === null ? {} : { text: content.text },
|
|
2869
|
+
...content.files.length === 0 ? {} : { files: [...content.files] }
|
|
2870
|
+
};
|
|
2871
|
+
}
|
|
2872
|
+
/** Whether this browser can open its share sheet for `data`. */
|
|
2873
|
+
function canShareNatively(environment, data) {
|
|
2874
|
+
const nav = environment?.navigator;
|
|
2875
|
+
if (typeof nav?.share !== "function") return false;
|
|
2876
|
+
if (data === null) return true;
|
|
2877
|
+
return typeof nav.canShare === "function" ? nav.canShare(data) : data.files === void 0;
|
|
2878
|
+
}
|
|
2879
|
+
function isTouchFirst(environment) {
|
|
2880
|
+
return typeof environment?.matchMedia === "function" && environment.matchMedia("(hover: none) and (pointer: coarse)").matches;
|
|
2881
|
+
}
|
|
2882
|
+
function resolveMode(mode) {
|
|
2883
|
+
if (mode === void 0) return "auto";
|
|
2884
|
+
return mode === "auto" || mode === "native" || mode === "copy" ? mode : invalid("`mode` must be \"auto\", \"native\" or \"copy\".");
|
|
2885
|
+
}
|
|
2886
|
+
/**
|
|
2887
|
+
* How {@link shareOrCopy} would share right now: `"native"` (the share sheet) or
|
|
2888
|
+
* `"copy"`. Use it to label the button ("Share…" or "Copy link"). Pass the
|
|
2889
|
+
* content when it has files, since not every share sheet takes them.
|
|
2890
|
+
*/
|
|
2891
|
+
function shareMethod(options = {}, content) {
|
|
2892
|
+
const mode = resolveMode(options.mode);
|
|
2893
|
+
if (typeof mode !== "string" || mode === "copy") return "copy";
|
|
2894
|
+
const environment = options.environment ?? defaultEnvironment();
|
|
2895
|
+
if (mode === "auto" && !isTouchFirst(environment)) return "copy";
|
|
2896
|
+
const resolved = content === void 0 ? null : resolveContent(content, {});
|
|
2897
|
+
const data = resolved === null ? null : "type" in resolved ? void 0 : shareData(resolved);
|
|
2898
|
+
return data !== void 0 && canShareNatively(environment, data) ? "native" : "copy";
|
|
2899
|
+
}
|
|
2900
|
+
/** The default copy: a titled link when there is a URL, otherwise the text (one of them is always set here). */
|
|
2901
|
+
function defaultCopy(content) {
|
|
2902
|
+
if (content.url !== null) return linkPayload({
|
|
2903
|
+
url: content.url,
|
|
2904
|
+
...content.title === null ? {} : { title: content.title }
|
|
2905
|
+
});
|
|
2906
|
+
return content.text;
|
|
2907
|
+
}
|
|
2908
|
+
async function copyContent(content, reason, options) {
|
|
2909
|
+
if (options.copy === void 0 && content.url === null && content.text === null) return {
|
|
2910
|
+
status: "error",
|
|
2911
|
+
method: "copy",
|
|
2912
|
+
error: createCopyError("unsupported-format", "Files can only be shared through a share sheet, and this browser has none for them.")
|
|
2913
|
+
};
|
|
2914
|
+
let payload;
|
|
2915
|
+
try {
|
|
2916
|
+
payload = normalizePayload((options.copy ?? defaultCopy)(content));
|
|
2917
|
+
} catch (error) {
|
|
2918
|
+
return {
|
|
2919
|
+
status: "error",
|
|
2920
|
+
method: "copy",
|
|
2921
|
+
error: toCopyError(error)
|
|
2922
|
+
};
|
|
2923
|
+
}
|
|
2924
|
+
const problem = validatePayload(payload);
|
|
2925
|
+
if (problem !== null) return {
|
|
2926
|
+
status: "error",
|
|
2927
|
+
method: "copy",
|
|
2928
|
+
error: problem
|
|
2929
|
+
};
|
|
2930
|
+
if (options.signal?.aborted === true) return {
|
|
2931
|
+
status: "error",
|
|
2932
|
+
method: "copy",
|
|
2933
|
+
error: createCopyError("aborted", "The share was aborted.")
|
|
2934
|
+
};
|
|
2935
|
+
try {
|
|
2936
|
+
await (options.adapter ?? createBrowserClipboardAdapter()).write(payload, options.signal === void 0 ? void 0 : { signal: options.signal });
|
|
2937
|
+
} catch (error) {
|
|
2938
|
+
return {
|
|
2939
|
+
status: "error",
|
|
2940
|
+
method: "copy",
|
|
2941
|
+
error: toCopyError(error)
|
|
2942
|
+
};
|
|
2943
|
+
}
|
|
2944
|
+
return {
|
|
2945
|
+
status: "copied",
|
|
2946
|
+
method: "copy",
|
|
2947
|
+
reason,
|
|
2948
|
+
payload,
|
|
2949
|
+
content
|
|
2950
|
+
};
|
|
2951
|
+
}
|
|
2952
|
+
/**
|
|
2953
|
+
* Shares through the device's share sheet where people expect one, and copies a
|
|
2954
|
+
* titled link everywhere else. Call it directly in a click handler: the share
|
|
2955
|
+
* sheet needs the user's gesture.
|
|
2956
|
+
*
|
|
2957
|
+
* It never rejects. It resolves to `shared`, `copied` (with why), `cancelled`
|
|
2958
|
+
* (the user closed the share sheet: nothing to report) or `error` with a typed
|
|
2959
|
+
* {@link CopyError}.
|
|
2960
|
+
*
|
|
2961
|
+
* @example
|
|
2962
|
+
* button.addEventListener('click', async () => {
|
|
2963
|
+
* const outcome = await shareOrCopy({ url: location.href, title: document.title });
|
|
2964
|
+
* if (outcome.status === 'copied') toast('Link copied');
|
|
2965
|
+
* });
|
|
2966
|
+
*/
|
|
2967
|
+
async function shareOrCopy(content, options = {}) {
|
|
2968
|
+
const mode = resolveMode(options.mode);
|
|
2969
|
+
if (typeof mode !== "string") return {
|
|
2970
|
+
status: "error",
|
|
2971
|
+
method: "copy",
|
|
2972
|
+
error: mode
|
|
2973
|
+
};
|
|
2974
|
+
const resolved = resolveContent(content, options);
|
|
2975
|
+
if ("type" in resolved) return {
|
|
2976
|
+
status: "error",
|
|
2977
|
+
method: "copy",
|
|
2978
|
+
error: resolved
|
|
2979
|
+
};
|
|
2980
|
+
const environment = options.environment ?? defaultEnvironment();
|
|
2981
|
+
if (mode === "copy" || mode === "auto" && !isTouchFirst(environment)) return copyContent(resolved, "preferred", options);
|
|
2982
|
+
const data = shareData(resolved);
|
|
2983
|
+
if (!canShareNatively(environment, data)) return copyContent(resolved, "unsupported", options);
|
|
2984
|
+
try {
|
|
2985
|
+
await (environment?.navigator?.share)(data);
|
|
2986
|
+
return {
|
|
2987
|
+
status: "shared",
|
|
2988
|
+
method: "native",
|
|
2989
|
+
content: resolved
|
|
2990
|
+
};
|
|
2991
|
+
} catch (error) {
|
|
2992
|
+
if (readName(error) === "AbortError") return {
|
|
2993
|
+
status: "cancelled",
|
|
2994
|
+
method: "native"
|
|
2995
|
+
};
|
|
2996
|
+
if (options.fallback === false) return {
|
|
2997
|
+
status: "error",
|
|
2998
|
+
method: "native",
|
|
2999
|
+
error: shareError(error)
|
|
3000
|
+
};
|
|
3001
|
+
return copyContent(resolved, "fallback", options);
|
|
3002
|
+
}
|
|
3003
|
+
}
|
|
3004
|
+
function readName(error) {
|
|
3005
|
+
const name = typeof error === "object" && error !== null ? error.name : void 0;
|
|
3006
|
+
return typeof name === "string" ? name : void 0;
|
|
3007
|
+
}
|
|
3008
|
+
/** Classifies a share sheet failure. */
|
|
3009
|
+
function shareError(cause) {
|
|
3010
|
+
switch (readName(cause)) {
|
|
3011
|
+
case "NotAllowedError": return createCopyError("permission-denied", "The share sheet was blocked: it needs a click, and an iframe needs the web-share permission.", cause);
|
|
3012
|
+
case "InvalidStateError": return createCopyError("unknown", "Another share sheet is already open.", cause);
|
|
3013
|
+
case "DataError":
|
|
3014
|
+
case "TypeError": return createCopyError("unsupported-format", "The share sheet rejected this content.", cause);
|
|
3015
|
+
default: return toCopyError(cause);
|
|
3016
|
+
}
|
|
3017
|
+
}
|
|
3018
|
+
//#endregion
|
|
3019
|
+
//#region src/react/useShare.ts
|
|
3020
|
+
const IDLE = {
|
|
3021
|
+
status: "idle",
|
|
3022
|
+
outcome: null
|
|
3023
|
+
};
|
|
3024
|
+
const IGNORED = /* @__PURE__ */ Object.freeze({
|
|
3025
|
+
status: "ignored",
|
|
3026
|
+
reason: "in-flight"
|
|
3027
|
+
});
|
|
3028
|
+
function resetDelay(value) {
|
|
3029
|
+
if (value === false || value === Infinity) return null;
|
|
3030
|
+
return value === void 0 || Number.isNaN(value) ? DEFAULT_RESET_AFTER_MS : Math.max(0, value);
|
|
3031
|
+
}
|
|
3032
|
+
const statusOf = (outcome) => outcome.status === "cancelled" ? "idle" : outcome.status;
|
|
3033
|
+
/**
|
|
3034
|
+
* Share with the device's share sheet on phones and tablets, and copy a titled
|
|
3035
|
+
* link everywhere else, with honest state for the button. Options may change on
|
|
3036
|
+
* every render; the latest ones are always used.
|
|
3037
|
+
*
|
|
3038
|
+
* @example
|
|
3039
|
+
* const { share, status, method } = useShare();
|
|
3040
|
+
* <button onClick={() => share({ url, title })}>
|
|
3041
|
+
* {status === 'copied' ? 'Link copied' : method === 'native' ? 'Share…' : 'Copy link'}
|
|
3042
|
+
* </button>
|
|
3043
|
+
*/
|
|
3044
|
+
function useShare(options = {}) {
|
|
3045
|
+
const optionsRef = (0, react.useRef)(options);
|
|
3046
|
+
useIsomorphicLayoutEffect(() => {
|
|
3047
|
+
optionsRef.current = options;
|
|
3048
|
+
});
|
|
3049
|
+
const [state, setState] = (0, react.useState)(IDLE);
|
|
3050
|
+
const mounted = (0, react.useRef)(false);
|
|
3051
|
+
const inFlight = (0, react.useRef)(false);
|
|
3052
|
+
const generation = (0, react.useRef)(0);
|
|
3053
|
+
const timer = (0, react.useRef)(void 0);
|
|
3054
|
+
(0, react.useEffect)(() => {
|
|
3055
|
+
mounted.current = true;
|
|
3056
|
+
return () => {
|
|
3057
|
+
mounted.current = false;
|
|
3058
|
+
generation.current += 1;
|
|
3059
|
+
clearTimeout(timer.current);
|
|
3060
|
+
};
|
|
3061
|
+
}, []);
|
|
3062
|
+
const subscribe = (0, react.useCallback)((onChange) => {
|
|
3063
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function") return () => void 0;
|
|
3064
|
+
const list = window.matchMedia(TOUCH_FIRST);
|
|
3065
|
+
list.addEventListener("change", onChange);
|
|
3066
|
+
return () => {
|
|
3067
|
+
list.removeEventListener("change", onChange);
|
|
3068
|
+
};
|
|
3069
|
+
}, []);
|
|
3070
|
+
const method = (0, react.useSyncExternalStore)(subscribe, () => shareMethod(optionsRef.current), () => "copy");
|
|
3071
|
+
const share = (0, react.useCallback)(async (content) => {
|
|
3072
|
+
if (inFlight.current) return IGNORED;
|
|
3073
|
+
inFlight.current = true;
|
|
3074
|
+
const current = generation.current += 1;
|
|
3075
|
+
clearTimeout(timer.current);
|
|
3076
|
+
setState({
|
|
3077
|
+
status: "sharing",
|
|
3078
|
+
outcome: null
|
|
3079
|
+
});
|
|
3080
|
+
const outcome = await shareOrCopy(content, optionsRef.current);
|
|
3081
|
+
inFlight.current = false;
|
|
3082
|
+
if (!mounted.current || current !== generation.current) return outcome;
|
|
3083
|
+
setState({
|
|
3084
|
+
status: statusOf(outcome),
|
|
3085
|
+
outcome
|
|
3086
|
+
});
|
|
3087
|
+
const delay = resetDelay(optionsRef.current.resetAfterMs);
|
|
3088
|
+
if ((outcome.status === "shared" || outcome.status === "copied") && delay !== null) timer.current = setTimeout(() => {
|
|
3089
|
+
setState(IDLE);
|
|
3090
|
+
}, delay);
|
|
3091
|
+
optionsRef.current.onShare?.(outcome);
|
|
3092
|
+
return outcome;
|
|
3093
|
+
}, []);
|
|
3094
|
+
const reset = (0, react.useCallback)(() => {
|
|
3095
|
+
generation.current += 1;
|
|
3096
|
+
clearTimeout(timer.current);
|
|
3097
|
+
setState(IDLE);
|
|
3098
|
+
}, []);
|
|
3099
|
+
return (0, react.useMemo)(() => ({
|
|
3100
|
+
status: state.status,
|
|
3101
|
+
error: state.outcome?.status === "error" ? state.outcome.error : null,
|
|
3102
|
+
outcome: state.outcome,
|
|
3103
|
+
method,
|
|
3104
|
+
share,
|
|
3105
|
+
reset
|
|
3106
|
+
}), [
|
|
3107
|
+
state,
|
|
3108
|
+
method,
|
|
3109
|
+
share,
|
|
3110
|
+
reset
|
|
3111
|
+
]);
|
|
3112
|
+
}
|
|
3113
|
+
//#endregion
|
|
2626
3114
|
exports.CopyFailure = CopyFailure;
|
|
2627
3115
|
exports.CopyField = CopyField;
|
|
2628
3116
|
exports.CopyGroup = CopyGroup;
|
|
@@ -2687,6 +3175,7 @@ exports.usePaste = usePaste;
|
|
|
2687
3175
|
exports.usePasteDisplayStatus = usePasteDisplayStatus;
|
|
2688
3176
|
exports.usePasteField = usePasteField;
|
|
2689
3177
|
exports.useRevealOnInteraction = useRevealOnInteraction;
|
|
3178
|
+
exports.useShare = useShare;
|
|
2690
3179
|
exports.validatePayload = validatePayload;
|
|
2691
3180
|
exports.visuallyHiddenStyle = visuallyHiddenStyle;
|
|
2692
3181
|
|