react-smart-copy 1.17.2 → 1.19.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 +86 -5
- package/dist/capture.cjs +30 -153
- package/dist/capture.cjs.map +1 -1
- package/dist/capture.d.cts +2 -25
- package/dist/capture.d.ts +2 -25
- package/dist/capture.js +5 -128
- package/dist/capture.js.map +1 -1
- package/dist/chunks/clipboard-adapter-Cfz-rzw3.cjs +102 -0
- package/dist/chunks/clipboard-adapter-Cfz-rzw3.cjs.map +1 -0
- package/dist/chunks/clipboard-adapter-CtH4VELE.d.cts +43 -0
- package/dist/chunks/clipboard-adapter-DEXkXxwV.js +97 -0
- package/dist/chunks/clipboard-adapter-DEXkXxwV.js.map +1 -0
- package/dist/chunks/clipboard-adapter-Ds0irroN.d.ts +43 -0
- package/dist/chunks/contrast-C6CVklEI.cjs +842 -0
- package/dist/chunks/contrast-C6CVklEI.cjs.map +1 -0
- package/dist/chunks/contrast-DpK2JIVu.js +789 -0
- package/dist/chunks/contrast-DpK2JIVu.js.map +1 -0
- package/dist/chunks/copy-interceptor-C6NABy1l.d.ts +141 -0
- package/dist/chunks/copy-interceptor-CZWzGOkL.d.cts +141 -0
- package/dist/chunks/core-Cp2HSpaz.js +1035 -0
- package/dist/chunks/core-Cp2HSpaz.js.map +1 -0
- package/dist/chunks/core-DYf_Lhbe.cjs +1142 -0
- package/dist/chunks/core-DYf_Lhbe.cjs.map +1 -0
- package/dist/chunks/deadline-C29AlS_Q.js +46 -0
- package/dist/chunks/deadline-C29AlS_Q.js.map +1 -0
- package/dist/chunks/deadline-DkiB8ZHE.cjs +51 -0
- package/dist/chunks/deadline-DkiB8ZHE.cjs.map +1 -0
- package/dist/chunks/errors-9h8sdFFX.js +120 -0
- package/dist/chunks/errors-9h8sdFFX.js.map +1 -0
- package/dist/chunks/errors-BwOF6T_M.cjs +167 -0
- package/dist/chunks/errors-BwOF6T_M.cjs.map +1 -0
- package/dist/chunks/index-mr7sSAgP.d.ts +426 -0
- package/dist/chunks/index-vz0iW5V2.d.cts +426 -0
- package/dist/chunks/inert-html-D75NyVvT.js +34 -0
- package/dist/chunks/inert-html-D75NyVvT.js.map +1 -0
- package/dist/chunks/inert-html-DyxjWDok.cjs +39 -0
- package/dist/chunks/inert-html-DyxjWDok.cjs.map +1 -0
- package/dist/chunks/mime--LB7kdqG.cjs +153 -0
- package/dist/chunks/mime--LB7kdqG.cjs.map +1 -0
- package/dist/chunks/mime-aa-HA2dm.js +100 -0
- package/dist/chunks/mime-aa-HA2dm.js.map +1 -0
- package/dist/chunks/paste-adapter--SPL4PRE.d.cts +35 -0
- package/dist/chunks/paste-adapter-uahXnTYk.d.ts +35 -0
- package/dist/chunks/paste-reader-Bo01IMC_.cjs +289 -0
- package/dist/chunks/paste-reader-Bo01IMC_.cjs.map +1 -0
- package/dist/chunks/paste-reader-D9i7yMXF.js +224 -0
- package/dist/chunks/paste-reader-D9i7yMXF.js.map +1 -0
- package/dist/chunks/paste-reader-DcXnRWQr.d.cts +96 -0
- package/dist/chunks/paste-reader-DcXnRWQr.d.ts +96 -0
- package/dist/chunks/payload-CUymxwim.cjs +100 -0
- package/dist/chunks/payload-CUymxwim.cjs.map +1 -0
- package/dist/chunks/payload-CcwKJ_bU.d.cts +162 -0
- package/dist/chunks/payload-Cl_8hCI5.d.ts +162 -0
- package/dist/chunks/payload-DGw-JOo7.js +71 -0
- package/dist/chunks/payload-DGw-JOo7.js.map +1 -0
- package/dist/chunks/payload-DI5AorDz.js +103 -0
- package/dist/chunks/payload-DI5AorDz.js.map +1 -0
- package/dist/chunks/payload-J6GjHuh3.cjs +132 -0
- package/dist/chunks/payload-J6GjHuh3.cjs.map +1 -0
- package/dist/chunks/share-Bop47Znn.js +208 -0
- package/dist/chunks/share-Bop47Znn.js.map +1 -0
- package/dist/chunks/share-C0esXvRK.d.cts +115 -0
- package/dist/chunks/share-ayDz974N.cjs +231 -0
- package/dist/chunks/share-ayDz974N.cjs.map +1 -0
- package/dist/chunks/share-e7FenIr2.d.ts +115 -0
- package/dist/chunks/text-field-Ba8W3v4R.cjs +77 -0
- package/dist/chunks/text-field-Ba8W3v4R.cjs.map +1 -0
- package/dist/chunks/text-field-C0SGmvvR.d.cts +18 -0
- package/dist/chunks/text-field-C0SGmvvR.d.ts +18 -0
- package/dist/chunks/text-field-DUvrMMaK.js +66 -0
- package/dist/chunks/text-field-DUvrMMaK.js.map +1 -0
- package/dist/chunks/url-B9jdFGQy.cjs +86 -0
- package/dist/chunks/url-B9jdFGQy.cjs.map +1 -0
- package/dist/chunks/url-BcMYVCXQ.js +135 -0
- package/dist/chunks/url-BcMYVCXQ.js.map +1 -0
- package/dist/chunks/url-D4PGsRm8.cjs +164 -0
- package/dist/chunks/url-D4PGsRm8.cjs.map +1 -0
- package/dist/chunks/url-DWdElCT3.d.cts +43 -0
- package/dist/chunks/url-DWdElCT3.d.ts +43 -0
- package/dist/chunks/url-vJaFPbNL.js +57 -0
- package/dist/chunks/url-vJaFPbNL.js.map +1 -0
- package/dist/code.cjs +22 -113
- package/dist/code.cjs.map +1 -1
- package/dist/code.d.cts +4 -28
- package/dist/code.d.ts +4 -28
- package/dist/code.js +5 -96
- package/dist/code.js.map +1 -1
- package/dist/color.cjs +21 -893
- package/dist/color.cjs.map +1 -1
- package/dist/color.d.cts +4 -28
- package/dist/color.d.ts +4 -28
- package/dist/color.js +4 -876
- package/dist/color.js.map +1 -1
- package/dist/core.cjs +52 -1659
- package/dist/core.d.cts +8 -878
- package/dist/core.d.ts +8 -878
- package/dist/core.js +7 -1614
- package/dist/image.cjs +23 -94
- package/dist/image.cjs.map +1 -1
- package/dist/image.d.cts +1 -0
- package/dist/image.d.ts +1 -0
- package/dist/image.js +3 -74
- package/dist/image.js.map +1 -1
- package/dist/index.cjs +74 -2072
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -978
- package/dist/index.d.ts +8 -978
- package/dist/index.js +7 -2005
- package/dist/index.js.map +1 -1
- package/dist/link.cjs +26 -339
- package/dist/link.cjs.map +1 -1
- package/dist/link.d.cts +5 -83
- package/dist/link.d.ts +5 -83
- package/dist/link.js +7 -320
- package/dist/link.js.map +1 -1
- package/dist/markdown.cjs +19 -159
- package/dist/markdown.cjs.map +1 -1
- package/dist/markdown.d.cts +3 -16
- package/dist/markdown.d.ts +3 -16
- package/dist/markdown.js +6 -146
- package/dist/markdown.js.map +1 -1
- package/dist/rich.cjs +1123 -940
- package/dist/rich.cjs.map +1 -1
- package/dist/rich.d.cts +101 -91
- package/dist/rich.d.ts +101 -91
- package/dist/rich.js +1099 -919
- package/dist/rich.js.map +1 -1
- package/dist/share.cjs +5 -640
- package/dist/share.d.cts +3 -248
- package/dist/share.d.ts +3 -248
- package/dist/share.js +1 -637
- package/dist/structured.cjs +13 -95
- package/dist/structured.cjs.map +1 -1
- package/dist/structured.d.cts +2 -11
- package/dist/structured.d.ts +2 -11
- package/dist/structured.js +4 -86
- package/dist/structured.js.map +1 -1
- package/dist/table.cjs +13 -40
- package/dist/table.cjs.map +1 -1
- package/dist/table.d.cts +2 -12
- package/dist/table.d.ts +2 -12
- package/dist/table.js +4 -31
- package/dist/table.js.map +1 -1
- package/dist/testing.cjs +10 -43
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +6 -181
- package/dist/testing.d.ts +6 -181
- package/dist/testing.js +3 -36
- package/dist/testing.js.map +1 -1
- package/package.json +37 -5
- package/dist/core.cjs.map +0 -1
- package/dist/core.js.map +0 -1
- package/dist/share.cjs.map +0 -1
- package/dist/share.js.map +0 -1
|
@@ -0,0 +1,1035 @@
|
|
|
1
|
+
|
|
2
|
+
import { c as toCopyError, n as copyFailure, o as isCopyFailure, r as createCopyError, s as isRetryableError, t as CopyFailure } from "./errors-9h8sdFFX.js";
|
|
3
|
+
import { a as validatePayload, r as normalizePayload } from "./payload-DGw-JOo7.js";
|
|
4
|
+
import { a as matchesMimePattern, i as isWebCustomFormat } from "./mime-aa-HA2dm.js";
|
|
5
|
+
import { t as createBrowserClipboardAdapter } from "./clipboard-adapter-DEXkXxwV.js";
|
|
6
|
+
import { a as collectClipboardItems, c as finalizePaste, l as resolvePasteReadOptions, o as completePasteResult, s as enforcePasteLimits, u as snapshotDataTransfer } from "./paste-reader-D9i7yMXF.js";
|
|
7
|
+
//#region src/core/copy-coordinator.ts
|
|
8
|
+
function createCopyCoordinator() {
|
|
9
|
+
let active = null;
|
|
10
|
+
return {
|
|
11
|
+
activate: (member) => {
|
|
12
|
+
const previous = active;
|
|
13
|
+
active = member;
|
|
14
|
+
if (previous !== null && previous !== member) previous.release();
|
|
15
|
+
},
|
|
16
|
+
deactivate: (member) => {
|
|
17
|
+
if (active === member) active = null;
|
|
18
|
+
},
|
|
19
|
+
getActive: () => active
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/core/machine-shared.ts
|
|
24
|
+
/**
|
|
25
|
+
* Internals shared by the copy and paste state machines. Kept in one module so
|
|
26
|
+
* apps that use both don't ship two copies.
|
|
27
|
+
*/
|
|
28
|
+
const DEFAULT_MAX_RETRIES = 3;
|
|
29
|
+
function resolveMaxRetries(value) {
|
|
30
|
+
if (value === Infinity) return Infinity;
|
|
31
|
+
if (value === void 0 || !Number.isFinite(value)) return 3;
|
|
32
|
+
return Math.max(0, Math.floor(value));
|
|
33
|
+
}
|
|
34
|
+
function reportCallbackError(error) {
|
|
35
|
+
const scope = globalThis;
|
|
36
|
+
if (typeof scope.reportError === "function") scope.reportError(error);
|
|
37
|
+
else setTimeout(() => {
|
|
38
|
+
throw error;
|
|
39
|
+
}, 0);
|
|
40
|
+
}
|
|
41
|
+
/** User callbacks must never corrupt machine state; their errors are reported, not swallowed. */
|
|
42
|
+
function invoke(fn, ...args) {
|
|
43
|
+
if (!fn) return;
|
|
44
|
+
try {
|
|
45
|
+
fn(...args);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
reportCallbackError(error);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** One cancellable operation at a time. Signals only fire while their operation is unfinished. */
|
|
51
|
+
function createOperations() {
|
|
52
|
+
let controller;
|
|
53
|
+
const abort = () => {
|
|
54
|
+
if (!controller) return false;
|
|
55
|
+
controller.abort();
|
|
56
|
+
controller = void 0;
|
|
57
|
+
return true;
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
start: () => {
|
|
61
|
+
abort();
|
|
62
|
+
controller = new AbortController();
|
|
63
|
+
return { signal: controller.signal };
|
|
64
|
+
},
|
|
65
|
+
end: (context) => {
|
|
66
|
+
if (controller?.signal === context.signal) controller = void 0;
|
|
67
|
+
},
|
|
68
|
+
abort
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/core/source-error.ts
|
|
73
|
+
/**
|
|
74
|
+
* The error for a user-supplied source (copy source, drag source, copy transform)
|
|
75
|
+
* that threw. A thrown `CopyFailure` keeps its classification, so a helper such as
|
|
76
|
+
* `tablePayload()` can report `too-large` instead of a generic `invalid-payload`.
|
|
77
|
+
* Internal: not part of the public API.
|
|
78
|
+
*/
|
|
79
|
+
function sourceError(cause, message) {
|
|
80
|
+
return isCopyFailure(cause) ? cause.copyError : createCopyError("invalid-payload", message, cause);
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/core/copy-machine.ts
|
|
84
|
+
const DEFAULT_RESET_AFTER_MS = 2e3;
|
|
85
|
+
const IDLE_STATE = /* @__PURE__ */ Object.freeze({ status: "idle" });
|
|
86
|
+
const IGNORED_IN_FLIGHT$1 = /* @__PURE__ */ Object.freeze({
|
|
87
|
+
status: "ignored",
|
|
88
|
+
reason: "in-flight"
|
|
89
|
+
});
|
|
90
|
+
const IGNORED_NOTHING$1 = /* @__PURE__ */ Object.freeze({
|
|
91
|
+
status: "ignored",
|
|
92
|
+
reason: "nothing-to-retry"
|
|
93
|
+
});
|
|
94
|
+
const IGNORED_NOT_RETRYABLE$1 = /* @__PURE__ */ Object.freeze({
|
|
95
|
+
status: "ignored",
|
|
96
|
+
reason: "not-retryable"
|
|
97
|
+
});
|
|
98
|
+
const IGNORED_CANCELLED$1 = /* @__PURE__ */ Object.freeze({
|
|
99
|
+
status: "ignored",
|
|
100
|
+
reason: "cancelled"
|
|
101
|
+
});
|
|
102
|
+
function resolveResetDelay$1(value) {
|
|
103
|
+
if (value === false || value === Infinity) return null;
|
|
104
|
+
if (value === void 0 || Number.isNaN(value)) return DEFAULT_RESET_AFTER_MS;
|
|
105
|
+
return Math.max(0, value);
|
|
106
|
+
}
|
|
107
|
+
/** Pure helper: can `retry()` do anything from this state? */
|
|
108
|
+
function canRetryState(state, maxRetries) {
|
|
109
|
+
return state.status === "error" && state.payload !== null && isRetryableError(state.error) && state.retryCount < resolveMaxRetries(maxRetries);
|
|
110
|
+
}
|
|
111
|
+
let defaultAdapter$1;
|
|
112
|
+
const getDefaultAdapter$1 = () => defaultAdapter$1 ?? (defaultAdapter$1 = createBrowserClipboardAdapter());
|
|
113
|
+
/** Framework-agnostic copy state machine. */
|
|
114
|
+
function createCopyMachine(options = {}) {
|
|
115
|
+
const read = typeof options === "function" ? options : () => options;
|
|
116
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
117
|
+
let state = IDLE_STATE;
|
|
118
|
+
let generation = 0;
|
|
119
|
+
let resetTimer;
|
|
120
|
+
const operations = createOperations();
|
|
121
|
+
const now = () => (read().now ?? Date.now)();
|
|
122
|
+
let joined = null;
|
|
123
|
+
const member = { release: () => {
|
|
124
|
+
if (state.status === "copied") {
|
|
125
|
+
clearResetTimer();
|
|
126
|
+
setState(IDLE_STATE);
|
|
127
|
+
}
|
|
128
|
+
} };
|
|
129
|
+
const setState = (next) => {
|
|
130
|
+
if (next === state) return;
|
|
131
|
+
const previous = state;
|
|
132
|
+
state = next;
|
|
133
|
+
if (previous.status === "copied" && next.status !== "copied" && joined) {
|
|
134
|
+
joined.deactivate(member);
|
|
135
|
+
joined = null;
|
|
136
|
+
}
|
|
137
|
+
for (const listener of Array.from(listeners)) listener();
|
|
138
|
+
};
|
|
139
|
+
const joinCoordinator = () => {
|
|
140
|
+
joined = read().coordinator ?? null;
|
|
141
|
+
joined?.activate(member);
|
|
142
|
+
};
|
|
143
|
+
const clearResetTimer = () => {
|
|
144
|
+
if (resetTimer !== void 0) {
|
|
145
|
+
clearTimeout(resetTimer);
|
|
146
|
+
resetTimer = void 0;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const scheduleReset = (copied) => {
|
|
150
|
+
clearResetTimer();
|
|
151
|
+
const delay = resolveResetDelay$1(read().resetAfterMs);
|
|
152
|
+
if (delay === null) return;
|
|
153
|
+
const remaining = Math.max(0, delay - (now() - copied.at));
|
|
154
|
+
resetTimer = setTimeout(() => {
|
|
155
|
+
resetTimer = void 0;
|
|
156
|
+
setState(IDLE_STATE);
|
|
157
|
+
}, remaining);
|
|
158
|
+
};
|
|
159
|
+
const settleError = (error, payload, retryCount) => {
|
|
160
|
+
setState({
|
|
161
|
+
status: "error",
|
|
162
|
+
payload,
|
|
163
|
+
error,
|
|
164
|
+
retryCount
|
|
165
|
+
});
|
|
166
|
+
invoke(read().onError, error, payload);
|
|
167
|
+
};
|
|
168
|
+
const run = (source, retryCount) => {
|
|
169
|
+
clearResetTimer();
|
|
170
|
+
const current = ++generation;
|
|
171
|
+
let payload;
|
|
172
|
+
try {
|
|
173
|
+
const resolved = typeof source === "function" ? source() : source;
|
|
174
|
+
const resolvedUnknown = resolved;
|
|
175
|
+
if (resolvedUnknown !== null && typeof resolvedUnknown === "object" && typeof resolvedUnknown.then === "function") {
|
|
176
|
+
const error = createCopyError("invalid-payload", "The copy source function must be synchronous. Returning a Promise breaks the user-gesture window required for clipboard writes. Generate your content before calling copy().");
|
|
177
|
+
settleError(error, null, retryCount);
|
|
178
|
+
return Promise.resolve({
|
|
179
|
+
status: "error",
|
|
180
|
+
error
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
payload = normalizePayload(resolved);
|
|
184
|
+
} catch (cause) {
|
|
185
|
+
const error = sourceError(cause, "The copy source function threw.");
|
|
186
|
+
settleError(error, null, retryCount);
|
|
187
|
+
return Promise.resolve({
|
|
188
|
+
status: "error",
|
|
189
|
+
error
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
const invalid = validatePayload(payload);
|
|
193
|
+
if (invalid) {
|
|
194
|
+
settleError(invalid, payload, retryCount);
|
|
195
|
+
return Promise.resolve({
|
|
196
|
+
status: "error",
|
|
197
|
+
error: invalid
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
setState({
|
|
201
|
+
status: "copying",
|
|
202
|
+
payload,
|
|
203
|
+
retryCount
|
|
204
|
+
});
|
|
205
|
+
const context = operations.start();
|
|
206
|
+
let pending;
|
|
207
|
+
try {
|
|
208
|
+
pending = (read().adapter ?? getDefaultAdapter$1()).write(payload, context);
|
|
209
|
+
} catch (cause) {
|
|
210
|
+
pending = Promise.reject(cause);
|
|
211
|
+
}
|
|
212
|
+
return pending.then(() => {
|
|
213
|
+
operations.end(context);
|
|
214
|
+
if (current === generation) {
|
|
215
|
+
const copied = {
|
|
216
|
+
status: "copied",
|
|
217
|
+
payload,
|
|
218
|
+
at: now()
|
|
219
|
+
};
|
|
220
|
+
setState(copied);
|
|
221
|
+
joinCoordinator();
|
|
222
|
+
scheduleReset(copied);
|
|
223
|
+
invoke(read().onCopy, payload);
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
status: "copied",
|
|
227
|
+
payload
|
|
228
|
+
};
|
|
229
|
+
}, (cause) => {
|
|
230
|
+
operations.end(context);
|
|
231
|
+
if (context.signal.aborted) return IGNORED_CANCELLED$1;
|
|
232
|
+
const error = toCopyError(cause);
|
|
233
|
+
settleError(error, payload, retryCount);
|
|
234
|
+
return {
|
|
235
|
+
status: "error",
|
|
236
|
+
error
|
|
237
|
+
};
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
const copy = (source) => state.status === "copying" ? Promise.resolve(IGNORED_IN_FLIGHT$1) : run(source, 0);
|
|
241
|
+
const retry = () => {
|
|
242
|
+
if (state.status !== "error") return Promise.resolve(IGNORED_NOTHING$1);
|
|
243
|
+
if (state.payload === null || !isRetryableError(state.error)) return Promise.resolve(IGNORED_NOT_RETRYABLE$1);
|
|
244
|
+
if (state.retryCount >= resolveMaxRetries(read().maxRetries)) {
|
|
245
|
+
const error = createCopyError("max-retries-exceeded", `Copy failed after ${String(state.retryCount)} retries.`, state.error);
|
|
246
|
+
settleError(error, state.payload, state.retryCount);
|
|
247
|
+
return Promise.resolve({
|
|
248
|
+
status: "error",
|
|
249
|
+
error
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
return run(state.payload, state.retryCount + 1);
|
|
253
|
+
};
|
|
254
|
+
const reset = () => {
|
|
255
|
+
++generation;
|
|
256
|
+
const cancelled = operations.abort();
|
|
257
|
+
clearResetTimer();
|
|
258
|
+
const wasIdle = state.status === "idle";
|
|
259
|
+
setState(IDLE_STATE);
|
|
260
|
+
if (cancelled) invoke(read().onCancel, "reset");
|
|
261
|
+
if (!wasIdle) invoke(read().onReset);
|
|
262
|
+
};
|
|
263
|
+
const connect = () => {
|
|
264
|
+
if (state.status === "copied") scheduleReset(state);
|
|
265
|
+
return () => {
|
|
266
|
+
++generation;
|
|
267
|
+
const cancelled = operations.abort();
|
|
268
|
+
clearResetTimer();
|
|
269
|
+
if (state.status === "copying") setState(IDLE_STATE);
|
|
270
|
+
if (cancelled) invoke(read().onCancel, "disconnect");
|
|
271
|
+
};
|
|
272
|
+
};
|
|
273
|
+
return {
|
|
274
|
+
getSnapshot: () => state,
|
|
275
|
+
subscribe: (listener) => {
|
|
276
|
+
listeners.add(listener);
|
|
277
|
+
return () => {
|
|
278
|
+
listeners.delete(listener);
|
|
279
|
+
};
|
|
280
|
+
},
|
|
281
|
+
copy,
|
|
282
|
+
retry,
|
|
283
|
+
reset,
|
|
284
|
+
connect
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
//#endregion
|
|
288
|
+
//#region src/core/copy-interceptor.ts
|
|
289
|
+
const DEFAULT_INTERCEPTED_EVENTS = ["copy", "cut"];
|
|
290
|
+
function isNode(value) {
|
|
291
|
+
return typeof value === "object" && value !== null && typeof value.nodeType === "number";
|
|
292
|
+
}
|
|
293
|
+
function readTextField(target) {
|
|
294
|
+
if (!isNode(target) || target.nodeType !== 1) return null;
|
|
295
|
+
const tag = target.tagName;
|
|
296
|
+
if (tag !== "INPUT" && tag !== "TEXTAREA") return null;
|
|
297
|
+
const field = target;
|
|
298
|
+
try {
|
|
299
|
+
const { selectionStart: start, selectionEnd: end } = field;
|
|
300
|
+
return typeof start === "number" && typeof end === "number" ? {
|
|
301
|
+
field,
|
|
302
|
+
start,
|
|
303
|
+
end
|
|
304
|
+
} : null;
|
|
305
|
+
} catch {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
function documentOf(target) {
|
|
310
|
+
if (isNode(target)) return target.nodeType === 9 ? target : target.ownerDocument;
|
|
311
|
+
return typeof document === "undefined" ? null : document;
|
|
312
|
+
}
|
|
313
|
+
function isEditableNode(node) {
|
|
314
|
+
return (node.nodeType === 1 ? node : node.parentElement)?.isContentEditable === true;
|
|
315
|
+
}
|
|
316
|
+
function selectedRanges(doc) {
|
|
317
|
+
const selection = doc?.getSelection();
|
|
318
|
+
if (!selection || selection.isCollapsed) return null;
|
|
319
|
+
const ranges = [];
|
|
320
|
+
for (let index = 0; index < selection.rangeCount; index += 1) ranges.push(selection.getRangeAt(index));
|
|
321
|
+
return {
|
|
322
|
+
selection,
|
|
323
|
+
ranges
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
function readSelection(event) {
|
|
327
|
+
const kind = event.type === "cut" ? "cut" : "copy";
|
|
328
|
+
const fieldSelection = readTextField(event.target);
|
|
329
|
+
if (fieldSelection) {
|
|
330
|
+
const { field, start, end } = fieldSelection;
|
|
331
|
+
const text = field.value.slice(Math.min(start, end), Math.max(start, end));
|
|
332
|
+
if (text.length === 0) return null;
|
|
333
|
+
return {
|
|
334
|
+
selection: {
|
|
335
|
+
kind,
|
|
336
|
+
text,
|
|
337
|
+
html: null,
|
|
338
|
+
fragment: null,
|
|
339
|
+
field,
|
|
340
|
+
editable: !field.readOnly && !field.disabled
|
|
341
|
+
},
|
|
342
|
+
doc: field.ownerDocument
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
const doc = documentOf(event.target);
|
|
346
|
+
const selected = selectedRanges(doc);
|
|
347
|
+
const first = selected?.ranges[0];
|
|
348
|
+
if (!doc || !selected || !first) return null;
|
|
349
|
+
const text = selected.selection.toString();
|
|
350
|
+
if (text.length === 0) return null;
|
|
351
|
+
const container = doc.createElement("div");
|
|
352
|
+
for (const range of selected.ranges) container.appendChild(range.cloneContents());
|
|
353
|
+
const html = container.innerHTML;
|
|
354
|
+
const fragment = doc.createDocumentFragment();
|
|
355
|
+
while (container.firstChild) fragment.appendChild(container.firstChild);
|
|
356
|
+
return {
|
|
357
|
+
selection: {
|
|
358
|
+
kind,
|
|
359
|
+
text,
|
|
360
|
+
html,
|
|
361
|
+
fragment,
|
|
362
|
+
field: null,
|
|
363
|
+
editable: isEditableNode(first.startContainer)
|
|
364
|
+
},
|
|
365
|
+
doc
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
/** Reads the selection behind a copy event, or `null` if nothing readable is selected. */
|
|
369
|
+
function readCopySelection(event) {
|
|
370
|
+
const read = readSelection(event);
|
|
371
|
+
return read ? read.selection : null;
|
|
372
|
+
}
|
|
373
|
+
const nonEmpty$1 = (value) => typeof value === "string" && value.length > 0;
|
|
374
|
+
const invalid$1 = (message, cause) => ({
|
|
375
|
+
ok: false,
|
|
376
|
+
error: createCopyError("invalid-payload", message, cause)
|
|
377
|
+
});
|
|
378
|
+
const unsupported$1 = (message) => ({
|
|
379
|
+
ok: false,
|
|
380
|
+
error: createCopyError("unsupported-format", message)
|
|
381
|
+
});
|
|
382
|
+
/**
|
|
383
|
+
* Validates a decision and turns it into `[mimeType, data]` pairs. Kept
|
|
384
|
+
* self-contained (no shared validator) so the interceptor stays small; the
|
|
385
|
+
* checks still run for plain-JS callers the types can't protect.
|
|
386
|
+
*/
|
|
387
|
+
function prepare(decision) {
|
|
388
|
+
const candidate = typeof decision === "string" ? {
|
|
389
|
+
kind: "text",
|
|
390
|
+
value: decision
|
|
391
|
+
} : decision;
|
|
392
|
+
if (typeof candidate !== "object" || candidate === null) return invalid$1("Copy transforms must return a string, a payload, false, or nothing.");
|
|
393
|
+
if (typeof candidate.then === "function") return invalid$1("Copy transforms must be synchronous; the clipboard closes when the event ends.");
|
|
394
|
+
const payload = candidate;
|
|
395
|
+
const fields = candidate;
|
|
396
|
+
const ok = (entries) => ({
|
|
397
|
+
ok: true,
|
|
398
|
+
payload,
|
|
399
|
+
entries
|
|
400
|
+
});
|
|
401
|
+
switch (fields["kind"]) {
|
|
402
|
+
case "text": return nonEmpty$1(fields["value"]) ? ok([["text/plain", fields["value"]]]) : invalid$1("Text payload must be a non-empty string.");
|
|
403
|
+
case "html": return nonEmpty$1(fields["html"]) && nonEmpty$1(fields["text"]) ? ok([["text/html", fields["html"]], ["text/plain", fields["text"]]]) : invalid$1("HTML payload requires non-empty `html` and plain-text `text`.");
|
|
404
|
+
case "json": {
|
|
405
|
+
const pretty = fields["pretty"];
|
|
406
|
+
const indent = pretty === true ? 2 : typeof pretty === "number" ? pretty : void 0;
|
|
407
|
+
let json;
|
|
408
|
+
try {
|
|
409
|
+
json = JSON.stringify(fields["value"], null, indent);
|
|
410
|
+
} catch (cause) {
|
|
411
|
+
return invalid$1("JSON payload could not be serialised (circular reference or BigInt?).", cause);
|
|
412
|
+
}
|
|
413
|
+
return typeof json === "string" ? ok([["text/plain", json]]) : invalid$1("JSON payload serialised to nothing.");
|
|
414
|
+
}
|
|
415
|
+
case "multi": {
|
|
416
|
+
const items = fields["items"];
|
|
417
|
+
if (!Array.isArray(items) || items.length === 0) return invalid$1("Multi payload requires at least one item.");
|
|
418
|
+
const entries = [];
|
|
419
|
+
for (const item of items) {
|
|
420
|
+
const { mimeType, data } = item ?? {};
|
|
421
|
+
if (!nonEmpty$1(mimeType) || entries.some(([seen]) => seen === mimeType)) return invalid$1("Every multi item needs a unique, non-empty `mimeType`.");
|
|
422
|
+
if (typeof data !== "string") return unsupported$1(`Multi item "${mimeType}" must be a string during a copy event.`);
|
|
423
|
+
entries.push([mimeType, data]);
|
|
424
|
+
}
|
|
425
|
+
const writable = entries.filter(([mimeType]) => !isWebCustomFormat(mimeType));
|
|
426
|
+
return writable.length > 0 ? ok(writable) : unsupported$1("Web custom formats need the async Clipboard API (useCopy).");
|
|
427
|
+
}
|
|
428
|
+
default: return unsupported$1(`"${String(fields["kind"])}" payloads cannot be written during a copy event; use useCopy() for images.`);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
/** Deletes the selection the way a native cut would (keeps undo history, fires `input`). */
|
|
432
|
+
function deleteSelection(doc) {
|
|
433
|
+
const legacy = doc;
|
|
434
|
+
try {
|
|
435
|
+
return legacy.execCommand?.("delete") === true;
|
|
436
|
+
} catch {
|
|
437
|
+
return false;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Applies a transform to one `copy`/`cut` event. Use directly from an
|
|
442
|
+
* `onCopy` handler, or let {@link registerCopyInterceptor} scope it.
|
|
443
|
+
*/
|
|
444
|
+
function interceptCopyEvent(event, options) {
|
|
445
|
+
if (event.type !== "copy" && event.type !== "cut") return {
|
|
446
|
+
status: "passed",
|
|
447
|
+
reason: "unsupported-event",
|
|
448
|
+
selection: null
|
|
449
|
+
};
|
|
450
|
+
if (event.defaultPrevented) return {
|
|
451
|
+
status: "passed",
|
|
452
|
+
reason: "already-handled",
|
|
453
|
+
selection: null
|
|
454
|
+
};
|
|
455
|
+
const read = readSelection(event);
|
|
456
|
+
if (!read) return {
|
|
457
|
+
status: "passed",
|
|
458
|
+
reason: "no-selection",
|
|
459
|
+
selection: null
|
|
460
|
+
};
|
|
461
|
+
const { selection, doc } = read;
|
|
462
|
+
const fail = (error) => {
|
|
463
|
+
const fallback = options.failureMode ?? "block";
|
|
464
|
+
if (fallback === "block") event.preventDefault();
|
|
465
|
+
return {
|
|
466
|
+
status: "failed",
|
|
467
|
+
selection,
|
|
468
|
+
error,
|
|
469
|
+
fallback
|
|
470
|
+
};
|
|
471
|
+
};
|
|
472
|
+
let decision;
|
|
473
|
+
try {
|
|
474
|
+
decision = options.transform(selection);
|
|
475
|
+
} catch (cause) {
|
|
476
|
+
return fail(sourceError(cause, "The copy transform threw."));
|
|
477
|
+
}
|
|
478
|
+
if (decision === null || decision === void 0) return {
|
|
479
|
+
status: "passed",
|
|
480
|
+
reason: "declined",
|
|
481
|
+
selection
|
|
482
|
+
};
|
|
483
|
+
if (decision === false) {
|
|
484
|
+
event.preventDefault();
|
|
485
|
+
return {
|
|
486
|
+
status: "blocked",
|
|
487
|
+
selection
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
const prepared = prepare(decision);
|
|
491
|
+
if (!prepared.ok) return fail(prepared.error);
|
|
492
|
+
const data = event.clipboardData;
|
|
493
|
+
if (!data) return fail(createCopyError("unsupported", "This browser exposes no clipboardData on copy events."));
|
|
494
|
+
event.preventDefault();
|
|
495
|
+
try {
|
|
496
|
+
for (const [mimeType, value] of prepared.entries) data.setData(mimeType, value);
|
|
497
|
+
} catch (cause) {
|
|
498
|
+
try {
|
|
499
|
+
data.clearData?.();
|
|
500
|
+
} catch {}
|
|
501
|
+
return {
|
|
502
|
+
status: "failed",
|
|
503
|
+
selection,
|
|
504
|
+
error: createCopyError("unknown", "The browser rejected the clipboard write.", cause),
|
|
505
|
+
fallback: "block"
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
const deleted = selection.kind === "cut" && selection.editable && deleteSelection(doc);
|
|
509
|
+
return {
|
|
510
|
+
status: "written",
|
|
511
|
+
selection,
|
|
512
|
+
payload: prepared.payload,
|
|
513
|
+
deleted
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
const registries = /* @__PURE__ */ new WeakMap();
|
|
517
|
+
function touches(root, event) {
|
|
518
|
+
const fieldSelection = readTextField(event.target);
|
|
519
|
+
if (fieldSelection) return root.contains(fieldSelection.field);
|
|
520
|
+
const selected = selectedRanges(root.ownerDocument);
|
|
521
|
+
return selected !== null && selected.ranges.some((range) => range.intersectsNode(root));
|
|
522
|
+
}
|
|
523
|
+
const priorityOf = (scope) => scope.read().priority ?? 0;
|
|
524
|
+
/**
|
|
525
|
+
* Highest `priority` first; then the innermost scope; then document order;
|
|
526
|
+
* then registration order (same root). A total order, so the result never
|
|
527
|
+
* depends on effect timing.
|
|
528
|
+
*/
|
|
529
|
+
function compareScopes(a, b) {
|
|
530
|
+
const byPriority = priorityOf(b) - priorityOf(a);
|
|
531
|
+
if (byPriority !== 0) return byPriority;
|
|
532
|
+
const position = a.root.compareDocumentPosition(b.root);
|
|
533
|
+
if (position === 0) return 0;
|
|
534
|
+
if (position & 16) return 1;
|
|
535
|
+
if (position & 8) return -1;
|
|
536
|
+
return position & 4 ? -1 : 1;
|
|
537
|
+
}
|
|
538
|
+
function dispatch(registry, event) {
|
|
539
|
+
if (event.defaultPrevented) return;
|
|
540
|
+
const kind = event.type;
|
|
541
|
+
const [scope] = [...registry.scopes].filter((scope) => (scope.read().events ?? DEFAULT_INTERCEPTED_EVENTS).includes(kind) && touches(scope.root, event)).sort(compareScopes);
|
|
542
|
+
if (!scope) return;
|
|
543
|
+
const options = scope.read();
|
|
544
|
+
const outcome = interceptCopyEvent(event, options);
|
|
545
|
+
invoke(options.onIntercept, outcome);
|
|
546
|
+
if (outcome.status === "failed") invoke(options.onError, outcome.error);
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Intercepts native copies and cuts whose selection touches `root`. Returns
|
|
550
|
+
* an unsubscribe function. Pass a getter to always read the latest options.
|
|
551
|
+
*
|
|
552
|
+
* One listener per document serves every scope, so overlapping scopes
|
|
553
|
+
* resolve deterministically (see `priority`) regardless of registration order.
|
|
554
|
+
* Handlers that call `preventDefault()` first (e.g. a React `onCopy`) win.
|
|
555
|
+
*/
|
|
556
|
+
function registerCopyInterceptor(root, options) {
|
|
557
|
+
const doc = root.ownerDocument;
|
|
558
|
+
let registry = registries.get(doc);
|
|
559
|
+
if (!registry) {
|
|
560
|
+
const created = {
|
|
561
|
+
scopes: /* @__PURE__ */ new Set(),
|
|
562
|
+
listener: (event) => {
|
|
563
|
+
dispatch(created, event);
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
registry = created;
|
|
567
|
+
registries.set(doc, created);
|
|
568
|
+
doc.addEventListener("copy", created.listener);
|
|
569
|
+
doc.addEventListener("cut", created.listener);
|
|
570
|
+
}
|
|
571
|
+
const scope = {
|
|
572
|
+
root,
|
|
573
|
+
read: typeof options === "function" ? options : () => options
|
|
574
|
+
};
|
|
575
|
+
registry.scopes.add(scope);
|
|
576
|
+
const active = registry;
|
|
577
|
+
let registered = true;
|
|
578
|
+
return () => {
|
|
579
|
+
if (!registered) return;
|
|
580
|
+
registered = false;
|
|
581
|
+
active.scopes.delete(scope);
|
|
582
|
+
if (active.scopes.size === 0) {
|
|
583
|
+
doc.removeEventListener("copy", active.listener);
|
|
584
|
+
doc.removeEventListener("cut", active.listener);
|
|
585
|
+
registries.delete(doc);
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
//#endregion
|
|
590
|
+
//#region src/core/drag-copy.ts
|
|
591
|
+
/**
|
|
592
|
+
* Drag-to-copy: populate `DataTransfer` when the user starts dragging an
|
|
593
|
+
* element, so whatever they drop it into receives structured data — plain text,
|
|
594
|
+
* HTML, JSON, or multiple MIME types — instead of the browser's default
|
|
595
|
+
* text-extraction of the dragged DOM.
|
|
596
|
+
*
|
|
597
|
+
* No Clipboard API, no permission prompt, works in every browser that supports
|
|
598
|
+
* HTML5 drag-and-drop (all modern browsers and IE11+).
|
|
599
|
+
*/
|
|
600
|
+
const nonEmpty = (value) => typeof value === "string" && value.length > 0;
|
|
601
|
+
const invalid = (message, cause) => ({
|
|
602
|
+
ok: false,
|
|
603
|
+
error: createCopyError("invalid-payload", message, cause)
|
|
604
|
+
});
|
|
605
|
+
const unsupported = (message) => ({
|
|
606
|
+
ok: false,
|
|
607
|
+
error: createCopyError("unsupported-format", message)
|
|
608
|
+
});
|
|
609
|
+
function prepareDragDecision(decision) {
|
|
610
|
+
const candidate = typeof decision === "string" ? {
|
|
611
|
+
kind: "text",
|
|
612
|
+
value: decision
|
|
613
|
+
} : decision;
|
|
614
|
+
if (typeof candidate !== "object" || candidate === null) return invalid("Drag source must return a string, a payload, false, or nothing.");
|
|
615
|
+
if (typeof candidate.then === "function") return invalid("Drag sources must be synchronous; DataTransfer closes when dragstart returns.");
|
|
616
|
+
const payload = candidate;
|
|
617
|
+
const fields = candidate;
|
|
618
|
+
const ok = (items) => ({
|
|
619
|
+
ok: true,
|
|
620
|
+
payload,
|
|
621
|
+
items
|
|
622
|
+
});
|
|
623
|
+
switch (fields["kind"]) {
|
|
624
|
+
case "text": return nonEmpty(fields["value"]) ? ok([{
|
|
625
|
+
mimeType: "text/plain",
|
|
626
|
+
data: fields["value"]
|
|
627
|
+
}]) : invalid("Text payload must be a non-empty string.");
|
|
628
|
+
case "html": return nonEmpty(fields["html"]) && nonEmpty(fields["text"]) ? ok([{
|
|
629
|
+
mimeType: "text/html",
|
|
630
|
+
data: fields["html"]
|
|
631
|
+
}, {
|
|
632
|
+
mimeType: "text/plain",
|
|
633
|
+
data: fields["text"]
|
|
634
|
+
}]) : invalid("HTML payload requires non-empty `html` and `text`.");
|
|
635
|
+
case "json": {
|
|
636
|
+
const pretty = fields["pretty"];
|
|
637
|
+
const indent = pretty === true ? 2 : typeof pretty === "number" ? pretty : void 0;
|
|
638
|
+
let json;
|
|
639
|
+
try {
|
|
640
|
+
json = JSON.stringify(fields["value"], null, indent);
|
|
641
|
+
} catch (cause) {
|
|
642
|
+
return invalid("JSON payload could not be serialised (circular reference or BigInt?).", cause);
|
|
643
|
+
}
|
|
644
|
+
return typeof json === "string" ? ok([{
|
|
645
|
+
mimeType: "text/plain",
|
|
646
|
+
data: json
|
|
647
|
+
}]) : invalid("JSON payload serialised to nothing.");
|
|
648
|
+
}
|
|
649
|
+
case "multi": {
|
|
650
|
+
const items = fields["items"];
|
|
651
|
+
if (!Array.isArray(items) || items.length === 0) return invalid("Multi payload requires at least one item.");
|
|
652
|
+
const out = [];
|
|
653
|
+
for (const item of items) {
|
|
654
|
+
const { mimeType, data } = item ?? {};
|
|
655
|
+
if (!nonEmpty(mimeType) || out.some((i) => i.mimeType === mimeType)) return invalid("Every multi item needs a unique, non-empty `mimeType`.");
|
|
656
|
+
if (typeof data !== "string") return unsupported(`Multi item "${mimeType}" must be a string; DataTransfer does not accept Blobs.`);
|
|
657
|
+
out.push({
|
|
658
|
+
mimeType,
|
|
659
|
+
data
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
const writable = out.filter((item) => !isWebCustomFormat(item.mimeType));
|
|
663
|
+
return writable.length > 0 ? ok(writable) : unsupported("Web custom formats need the async Clipboard API (useCopy).");
|
|
664
|
+
}
|
|
665
|
+
default: return unsupported(`"${String(fields["kind"])}" payloads cannot be used for drag-copy; use useCopy() for images.`);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Populates a `dragstart` event's `DataTransfer` with the given payload.
|
|
670
|
+
* Use this from a raw `dragstart` handler or a React `onDragStart` prop.
|
|
671
|
+
* Returns the outcome so you can inspect what happened.
|
|
672
|
+
*/
|
|
673
|
+
function applyDragCopy(event, options) {
|
|
674
|
+
if (event.defaultPrevented) return {
|
|
675
|
+
status: "passed",
|
|
676
|
+
reason: "declined"
|
|
677
|
+
};
|
|
678
|
+
let decision;
|
|
679
|
+
try {
|
|
680
|
+
decision = options.source(event);
|
|
681
|
+
} catch (cause) {
|
|
682
|
+
return {
|
|
683
|
+
status: "failed",
|
|
684
|
+
error: sourceError(cause, "The drag source threw.")
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
if (decision === null || decision === void 0) return {
|
|
688
|
+
status: "passed",
|
|
689
|
+
reason: "declined"
|
|
690
|
+
};
|
|
691
|
+
if (decision === false) {
|
|
692
|
+
event.preventDefault();
|
|
693
|
+
return {
|
|
694
|
+
status: "passed",
|
|
695
|
+
reason: "cancelled"
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
const transfer = event.dataTransfer;
|
|
699
|
+
if (!transfer) return {
|
|
700
|
+
status: "passed",
|
|
701
|
+
reason: "no-transfer"
|
|
702
|
+
};
|
|
703
|
+
const prepared = prepareDragDecision(decision);
|
|
704
|
+
if (!prepared.ok) return {
|
|
705
|
+
status: "failed",
|
|
706
|
+
error: prepared.error
|
|
707
|
+
};
|
|
708
|
+
try {
|
|
709
|
+
transfer.effectAllowed = options.effectAllowed ?? "copy";
|
|
710
|
+
for (const { mimeType, data } of prepared.items) transfer.setData(mimeType, data);
|
|
711
|
+
} catch (cause) {
|
|
712
|
+
try {
|
|
713
|
+
transfer.clearData?.();
|
|
714
|
+
} catch {}
|
|
715
|
+
return {
|
|
716
|
+
status: "failed",
|
|
717
|
+
error: createCopyError("unknown", "The browser rejected the DataTransfer write.", cause)
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
return {
|
|
721
|
+
status: "written",
|
|
722
|
+
payload: prepared.payload
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Attaches `dragstart` and `dragend` listeners to `element`, calling
|
|
727
|
+
* `applyDragCopy` on every drag start. Returns an object with an
|
|
728
|
+
* `unsubscribe` function.
|
|
729
|
+
*
|
|
730
|
+
* Pass an options getter so the latest options are always used without
|
|
731
|
+
* re-registering — safe to pass an inline object from a React render.
|
|
732
|
+
*
|
|
733
|
+
* ```ts
|
|
734
|
+
* const { unsubscribe } = registerDragCopy(el, () => ({
|
|
735
|
+
* source: () => `${title}\n${url}`,
|
|
736
|
+
* }));
|
|
737
|
+
* ```
|
|
738
|
+
*/
|
|
739
|
+
function registerDragCopy(element, options) {
|
|
740
|
+
const read = typeof options === "function" ? options : () => options;
|
|
741
|
+
let unsubscribed = false;
|
|
742
|
+
let currentOutcome = null;
|
|
743
|
+
function onDragStart(raw) {
|
|
744
|
+
const event = raw;
|
|
745
|
+
const opts = read();
|
|
746
|
+
const outcome = applyDragCopy({
|
|
747
|
+
type: event.type,
|
|
748
|
+
dataTransfer: event.dataTransfer,
|
|
749
|
+
get defaultPrevented() {
|
|
750
|
+
return event.defaultPrevented;
|
|
751
|
+
},
|
|
752
|
+
preventDefault: () => {
|
|
753
|
+
event.preventDefault();
|
|
754
|
+
}
|
|
755
|
+
}, opts);
|
|
756
|
+
currentOutcome = outcome;
|
|
757
|
+
const state = {
|
|
758
|
+
status: "dragging",
|
|
759
|
+
outcome
|
|
760
|
+
};
|
|
761
|
+
if (outcome.status === "failed") invoke(opts.onError, outcome.error);
|
|
762
|
+
invoke(opts.onDrag, state);
|
|
763
|
+
}
|
|
764
|
+
function onDragEnd() {
|
|
765
|
+
const opts = read();
|
|
766
|
+
const state = {
|
|
767
|
+
status: "done",
|
|
768
|
+
outcome: currentOutcome
|
|
769
|
+
};
|
|
770
|
+
invoke(opts.onDrag, state);
|
|
771
|
+
currentOutcome = null;
|
|
772
|
+
}
|
|
773
|
+
element.setAttribute("draggable", "true");
|
|
774
|
+
element.addEventListener("dragstart", onDragStart);
|
|
775
|
+
element.addEventListener("dragend", onDragEnd);
|
|
776
|
+
return { unsubscribe() {
|
|
777
|
+
if (unsubscribed) return;
|
|
778
|
+
unsubscribed = true;
|
|
779
|
+
element.removeEventListener("dragstart", onDragStart);
|
|
780
|
+
element.removeEventListener("dragend", onDragEnd);
|
|
781
|
+
element.removeAttribute("draggable");
|
|
782
|
+
} };
|
|
783
|
+
}
|
|
784
|
+
//#endregion
|
|
785
|
+
//#region src/core/paste-adapter.ts
|
|
786
|
+
function defaultEnvironment() {
|
|
787
|
+
return typeof window === "undefined" ? void 0 : window;
|
|
788
|
+
}
|
|
789
|
+
const canRead = (clipboard) => typeof clipboard.read === "function";
|
|
790
|
+
const canReadText = (clipboard) => typeof clipboard.readText === "function";
|
|
791
|
+
async function readPlainText(clipboard, options) {
|
|
792
|
+
let text;
|
|
793
|
+
try {
|
|
794
|
+
text = await clipboard.readText();
|
|
795
|
+
} catch (cause) {
|
|
796
|
+
throw new CopyFailure(toCopyError(cause, "read"));
|
|
797
|
+
}
|
|
798
|
+
const entries = text.length > 0 ? [{
|
|
799
|
+
type: "text/plain",
|
|
800
|
+
data: text,
|
|
801
|
+
file: false
|
|
802
|
+
}] : [];
|
|
803
|
+
enforcePasteLimits(entries, options);
|
|
804
|
+
return finalizePaste(entries, "clipboard", options);
|
|
805
|
+
}
|
|
806
|
+
async function readRich(clipboard, options, signal) {
|
|
807
|
+
let items;
|
|
808
|
+
try {
|
|
809
|
+
items = await clipboard.read();
|
|
810
|
+
} catch (cause) {
|
|
811
|
+
throw new CopyFailure(toCopyError(cause, "read"));
|
|
812
|
+
}
|
|
813
|
+
const entries = await collectClipboardItems(items, options, signal);
|
|
814
|
+
return finalizePaste(entries, "clipboard", options);
|
|
815
|
+
}
|
|
816
|
+
function readFromClipboard(env, options, signal) {
|
|
817
|
+
if (!env) throw copyFailure("unsupported", "No browser environment: the clipboard only exists in the browser.");
|
|
818
|
+
if (env.isSecureContext === false) throw copyFailure("insecure-context", "The Clipboard API requires a secure context (HTTPS or localhost).");
|
|
819
|
+
const clipboard = env.navigator?.clipboard;
|
|
820
|
+
if (!clipboard) throw copyFailure("unsupported", "navigator.clipboard is not available in this browser.");
|
|
821
|
+
const plainOnly = options.accept.every((pattern) => pattern === "text/plain");
|
|
822
|
+
const acceptsPlain = options.accept.some((pattern) => matchesMimePattern("text/plain", pattern));
|
|
823
|
+
if (canRead(clipboard) && !(plainOnly && canReadText(clipboard))) return readRich(clipboard, options, signal);
|
|
824
|
+
if (canReadText(clipboard) && acceptsPlain) return readPlainText(clipboard, options);
|
|
825
|
+
throw canReadText(clipboard) ? copyFailure("unsupported-format", "Reading non-text clipboard content requires navigator.clipboard.read().") : copyFailure("unsupported", "This browser cannot read the clipboard.");
|
|
826
|
+
}
|
|
827
|
+
/** The default paste adapter: async Clipboard API with feature detection and honest errors. */
|
|
828
|
+
function createBrowserPasteAdapter(options = {}) {
|
|
829
|
+
const getEnvironment = options.getEnvironment ?? defaultEnvironment;
|
|
830
|
+
return { read: (resolved, context) => {
|
|
831
|
+
try {
|
|
832
|
+
return readFromClipboard(getEnvironment(), resolved, context?.signal);
|
|
833
|
+
} catch (cause) {
|
|
834
|
+
return Promise.reject(cause);
|
|
835
|
+
}
|
|
836
|
+
} };
|
|
837
|
+
}
|
|
838
|
+
//#endregion
|
|
839
|
+
//#region src/core/paste-machine.ts
|
|
840
|
+
const PASTE_IDLE_STATE = /* @__PURE__ */ Object.freeze({ status: "idle" });
|
|
841
|
+
const PASTE_READING_STATE = /* @__PURE__ */ Object.freeze({ status: "reading" });
|
|
842
|
+
const IGNORED_IN_FLIGHT = /* @__PURE__ */ Object.freeze({
|
|
843
|
+
status: "ignored",
|
|
844
|
+
reason: "in-flight"
|
|
845
|
+
});
|
|
846
|
+
const IGNORED_NO_CONTENT = /* @__PURE__ */ Object.freeze({
|
|
847
|
+
status: "ignored",
|
|
848
|
+
reason: "no-accepted-content"
|
|
849
|
+
});
|
|
850
|
+
const IGNORED_NOTHING = /* @__PURE__ */ Object.freeze({
|
|
851
|
+
status: "ignored",
|
|
852
|
+
reason: "nothing-to-retry"
|
|
853
|
+
});
|
|
854
|
+
const IGNORED_NOT_RETRYABLE = /* @__PURE__ */ Object.freeze({
|
|
855
|
+
status: "ignored",
|
|
856
|
+
reason: "not-retryable"
|
|
857
|
+
});
|
|
858
|
+
const IGNORED_CANCELLED = /* @__PURE__ */ Object.freeze({
|
|
859
|
+
status: "ignored",
|
|
860
|
+
reason: "cancelled"
|
|
861
|
+
});
|
|
862
|
+
function resolveResetDelay(value) {
|
|
863
|
+
if (value === void 0 || value === false || value === Infinity || Number.isNaN(value)) return null;
|
|
864
|
+
return Math.max(0, value);
|
|
865
|
+
}
|
|
866
|
+
/** Pure helper: can `retry()` do anything from this state? */
|
|
867
|
+
function canRetryPasteState(state, maxRetries) {
|
|
868
|
+
return state.status === "error" && state.source === "clipboard" && isRetryableError(state.error) && state.retryCount < resolveMaxRetries(maxRetries);
|
|
869
|
+
}
|
|
870
|
+
let defaultAdapter;
|
|
871
|
+
const getDefaultAdapter = () => defaultAdapter ?? (defaultAdapter = createBrowserPasteAdapter());
|
|
872
|
+
/** Framework-agnostic paste state machine. */
|
|
873
|
+
function createPasteMachine(options = {}) {
|
|
874
|
+
const read = typeof options === "function" ? options : () => options;
|
|
875
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
876
|
+
let state = PASTE_IDLE_STATE;
|
|
877
|
+
let generation = 0;
|
|
878
|
+
let resetTimer;
|
|
879
|
+
const operations = createOperations();
|
|
880
|
+
const now = () => (read().now ?? Date.now)();
|
|
881
|
+
const setState = (next) => {
|
|
882
|
+
if (next === state) return;
|
|
883
|
+
state = next;
|
|
884
|
+
for (const listener of Array.from(listeners)) listener();
|
|
885
|
+
};
|
|
886
|
+
const clearResetTimer = () => {
|
|
887
|
+
if (resetTimer !== void 0) {
|
|
888
|
+
clearTimeout(resetTimer);
|
|
889
|
+
resetTimer = void 0;
|
|
890
|
+
}
|
|
891
|
+
};
|
|
892
|
+
const scheduleReset = (readState) => {
|
|
893
|
+
clearResetTimer();
|
|
894
|
+
const delay = resolveResetDelay(read().resetAfterMs);
|
|
895
|
+
if (delay === null) return;
|
|
896
|
+
const remaining = Math.max(0, delay - (now() - readState.at));
|
|
897
|
+
resetTimer = setTimeout(() => {
|
|
898
|
+
resetTimer = void 0;
|
|
899
|
+
setState(PASTE_IDLE_STATE);
|
|
900
|
+
}, remaining);
|
|
901
|
+
};
|
|
902
|
+
const settleError = (error, source, retryCount) => {
|
|
903
|
+
setState({
|
|
904
|
+
status: "error",
|
|
905
|
+
error,
|
|
906
|
+
source,
|
|
907
|
+
retryCount
|
|
908
|
+
});
|
|
909
|
+
invoke(read().onError, error);
|
|
910
|
+
};
|
|
911
|
+
/** Runs one read. `produce` is invoked synchronously so the user gesture is preserved. */
|
|
912
|
+
const run = (produce, source, retryCount) => {
|
|
913
|
+
clearResetTimer();
|
|
914
|
+
const current = ++generation;
|
|
915
|
+
const superseded = operations.abort();
|
|
916
|
+
const context = operations.start();
|
|
917
|
+
setState(PASTE_READING_STATE);
|
|
918
|
+
if (superseded) invoke(read().onCancel, "superseded");
|
|
919
|
+
let pending;
|
|
920
|
+
try {
|
|
921
|
+
pending = produce(context);
|
|
922
|
+
} catch (cause) {
|
|
923
|
+
pending = Promise.reject(cause);
|
|
924
|
+
}
|
|
925
|
+
return pending.then(completePasteResult).then((result) => {
|
|
926
|
+
operations.end(context);
|
|
927
|
+
if (current === generation) {
|
|
928
|
+
const readState = {
|
|
929
|
+
status: "read",
|
|
930
|
+
result,
|
|
931
|
+
at: now()
|
|
932
|
+
};
|
|
933
|
+
setState(readState);
|
|
934
|
+
scheduleReset(readState);
|
|
935
|
+
invoke(read().onPaste, result);
|
|
936
|
+
}
|
|
937
|
+
return {
|
|
938
|
+
status: "read",
|
|
939
|
+
result
|
|
940
|
+
};
|
|
941
|
+
}, (cause) => {
|
|
942
|
+
operations.end(context);
|
|
943
|
+
if (context.signal.aborted) return IGNORED_CANCELLED;
|
|
944
|
+
const error = toCopyError(cause, "read");
|
|
945
|
+
settleError(error, source, retryCount);
|
|
946
|
+
return {
|
|
947
|
+
status: "error",
|
|
948
|
+
error
|
|
949
|
+
};
|
|
950
|
+
});
|
|
951
|
+
};
|
|
952
|
+
const readClipboard = (retryCount) => run((context) => (read().adapter ?? getDefaultAdapter()).read(resolvePasteReadOptions(read()), context), "clipboard", retryCount);
|
|
953
|
+
const paste = () => state.status === "reading" ? Promise.resolve(IGNORED_IN_FLIGHT) : readClipboard(0);
|
|
954
|
+
const retry = () => {
|
|
955
|
+
if (state.status !== "error") return Promise.resolve(IGNORED_NOTHING);
|
|
956
|
+
if (state.source !== "clipboard" || !isRetryableError(state.error)) return Promise.resolve(IGNORED_NOT_RETRYABLE);
|
|
957
|
+
if (state.retryCount >= resolveMaxRetries(read().maxRetries)) {
|
|
958
|
+
const error = createCopyError("max-retries-exceeded", `Paste failed after ${String(state.retryCount)} retries.`, state.error);
|
|
959
|
+
settleError(error, "clipboard", state.retryCount);
|
|
960
|
+
return Promise.resolve({
|
|
961
|
+
status: "error",
|
|
962
|
+
error
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
return readClipboard(state.retryCount + 1);
|
|
966
|
+
};
|
|
967
|
+
/** Shared by paste and drop events: their `DataTransfer` has the same shape and lifetime. */
|
|
968
|
+
const transfer = (data, event, source) => {
|
|
969
|
+
const current = read();
|
|
970
|
+
if (source === "drop" && current.preventDefault !== false) event.preventDefault();
|
|
971
|
+
if (!data) return Promise.resolve(IGNORED_NO_CONTENT);
|
|
972
|
+
let resolved;
|
|
973
|
+
try {
|
|
974
|
+
resolved = resolvePasteReadOptions(current);
|
|
975
|
+
} catch (cause) {
|
|
976
|
+
const error = toCopyError(cause, "read");
|
|
977
|
+
++generation;
|
|
978
|
+
const superseded = operations.abort();
|
|
979
|
+
clearResetTimer();
|
|
980
|
+
settleError(error, source, 0);
|
|
981
|
+
if (superseded) invoke(current.onCancel, "superseded");
|
|
982
|
+
return Promise.resolve({
|
|
983
|
+
status: "error",
|
|
984
|
+
error
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
const entries = snapshotDataTransfer(data, resolved);
|
|
988
|
+
if (entries.length === 0) return Promise.resolve(IGNORED_NO_CONTENT);
|
|
989
|
+
if (source === "event" && current.preventDefault !== false) event.preventDefault();
|
|
990
|
+
return run(() => {
|
|
991
|
+
enforcePasteLimits(entries, resolved);
|
|
992
|
+
return finalizePaste(entries, source, resolved);
|
|
993
|
+
}, source, 0);
|
|
994
|
+
};
|
|
995
|
+
const pasteEvent = (event) => transfer(event.clipboardData, event, "event");
|
|
996
|
+
const dropEvent = (event) => transfer(event.dataTransfer, event, "drop");
|
|
997
|
+
const reset = () => {
|
|
998
|
+
++generation;
|
|
999
|
+
const cancelled = operations.abort();
|
|
1000
|
+
clearResetTimer();
|
|
1001
|
+
const wasIdle = state.status === "idle";
|
|
1002
|
+
setState(PASTE_IDLE_STATE);
|
|
1003
|
+
if (cancelled) invoke(read().onCancel, "reset");
|
|
1004
|
+
if (!wasIdle) invoke(read().onReset);
|
|
1005
|
+
};
|
|
1006
|
+
const connect = () => {
|
|
1007
|
+
if (state.status === "read") scheduleReset(state);
|
|
1008
|
+
return () => {
|
|
1009
|
+
++generation;
|
|
1010
|
+
const cancelled = operations.abort();
|
|
1011
|
+
clearResetTimer();
|
|
1012
|
+
if (state.status === "reading") setState(PASTE_IDLE_STATE);
|
|
1013
|
+
if (cancelled) invoke(read().onCancel, "disconnect");
|
|
1014
|
+
};
|
|
1015
|
+
};
|
|
1016
|
+
return {
|
|
1017
|
+
getSnapshot: () => state,
|
|
1018
|
+
subscribe: (listener) => {
|
|
1019
|
+
listeners.add(listener);
|
|
1020
|
+
return () => {
|
|
1021
|
+
listeners.delete(listener);
|
|
1022
|
+
};
|
|
1023
|
+
},
|
|
1024
|
+
paste,
|
|
1025
|
+
pasteEvent,
|
|
1026
|
+
dropEvent,
|
|
1027
|
+
retry,
|
|
1028
|
+
reset,
|
|
1029
|
+
connect
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
//#endregion
|
|
1033
|
+
export { resolveMaxRetries as _, applyDragCopy as a, interceptCopyEvent as c, DEFAULT_RESET_AFTER_MS as d, IDLE_STATE as f, invoke as g, DEFAULT_MAX_RETRIES as h, createBrowserPasteAdapter as i, readCopySelection as l, createCopyMachine as m, canRetryPasteState as n, registerDragCopy as o, canRetryState as p, createPasteMachine as r, DEFAULT_INTERCEPTED_EVENTS as s, PASTE_IDLE_STATE as t, registerCopyInterceptor as u, createCopyCoordinator as v };
|
|
1034
|
+
|
|
1035
|
+
//# sourceMappingURL=core-Cp2HSpaz.js.map
|