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