react-smart-copy 1.1.1 → 1.2.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 +46 -10
- package/dist/capture.cjs +36 -2
- package/dist/capture.cjs.map +1 -1
- package/dist/capture.d.cts +18 -4
- package/dist/capture.d.ts +18 -4
- package/dist/capture.js +36 -2
- package/dist/capture.js.map +1 -1
- package/dist/core.cjs +135 -80
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +62 -14
- package/dist/core.d.ts +62 -14
- package/dist/core.js +135 -81
- package/dist/core.js.map +1 -1
- package/dist/index.cjs +226 -104
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +90 -16
- package/dist/index.d.ts +90 -16
- package/dist/index.js +227 -106
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -267,20 +267,21 @@ function writeText(clipboard, text) {
|
|
|
267
267
|
if (typeof clipboard.writeText !== "function") throw fail("unsupported", "navigator.clipboard.writeText() is not available.");
|
|
268
268
|
return clipboard.writeText(text);
|
|
269
269
|
}
|
|
270
|
-
async function resolveImageBlob(source) {
|
|
270
|
+
async function resolveImageBlob(source, context) {
|
|
271
271
|
let blob;
|
|
272
272
|
try {
|
|
273
|
-
blob = await (typeof source === "function" ? source() : source);
|
|
273
|
+
blob = await (typeof source === "function" ? source(context ?? { signal: new AbortController().signal }) : source);
|
|
274
274
|
} catch (cause) {
|
|
275
|
+
if (context?.signal.aborted) throw fail("aborted", "The image copy was cancelled.", cause);
|
|
275
276
|
throw fail("blob-generation-failed", "The image source threw or rejected while producing a Blob.", cause);
|
|
276
277
|
}
|
|
277
278
|
if (!isBlob(blob)) throw fail("blob-generation-failed", "The image source did not produce a Blob.");
|
|
278
279
|
if (blob.type !== "image/png") throw fail("unsupported-format", `Only ${IMAGE_MIME_TYPE} can be written to the clipboard (received "${blob.type || "unknown"}"). Convert with canvas.toBlob(cb, "${IMAGE_MIME_TYPE}").`);
|
|
279
280
|
return blob;
|
|
280
281
|
}
|
|
281
|
-
async function writeImage(rich, source) {
|
|
282
|
+
async function writeImage(rich, source, context) {
|
|
282
283
|
if (!isTypeSupported(rich.Item, "image/png")) throw fail("unsupported-format", `This browser cannot write ${IMAGE_MIME_TYPE} to the clipboard.`);
|
|
283
|
-
const blob = resolveImageBlob(source);
|
|
284
|
+
const blob = resolveImageBlob(source, context);
|
|
284
285
|
blob.catch(noop);
|
|
285
286
|
try {
|
|
286
287
|
await rich.write([new rich.Item({ [IMAGE_MIME_TYPE]: blob })]);
|
|
@@ -289,7 +290,7 @@ async function writeImage(rich, source) {
|
|
|
289
290
|
}
|
|
290
291
|
await blob;
|
|
291
292
|
}
|
|
292
|
-
async function writeToClipboard(payload, env, degradeHtmlToText) {
|
|
293
|
+
async function writeToClipboard(payload, env, degradeHtmlToText, context) {
|
|
293
294
|
if (!env) throw fail("unsupported", "No browser environment: the clipboard only exists in the browser.");
|
|
294
295
|
if (env.isSecureContext === false) throw fail("insecure-context", "The Clipboard API requires a secure context (HTTPS or localhost).");
|
|
295
296
|
const clipboard = env.navigator?.clipboard;
|
|
@@ -311,7 +312,7 @@ async function writeToClipboard(payload, env, degradeHtmlToText) {
|
|
|
311
312
|
case "image": {
|
|
312
313
|
const rich = getRichClipboard(env, clipboard);
|
|
313
314
|
if (!rich) throw fail("unsupported-format", "Image copy requires ClipboardItem and navigator.clipboard.write().");
|
|
314
|
-
return writeImage(rich, payload.blob);
|
|
315
|
+
return writeImage(rich, payload.blob, context);
|
|
315
316
|
}
|
|
316
317
|
case "multi": {
|
|
317
318
|
const rich = getRichClipboard(env, clipboard);
|
|
@@ -329,7 +330,7 @@ async function writeToClipboard(payload, env, degradeHtmlToText) {
|
|
|
329
330
|
function createBrowserClipboardAdapter(options = {}) {
|
|
330
331
|
const getEnvironment = options.getEnvironment ?? defaultEnvironment$1;
|
|
331
332
|
const degradeHtmlToText = options.degradeHtmlToText ?? true;
|
|
332
|
-
return { write: (payload) => writeToClipboard(payload, getEnvironment(), degradeHtmlToText) };
|
|
333
|
+
return { write: (payload, context) => writeToClipboard(payload, getEnvironment(), degradeHtmlToText, context) };
|
|
333
334
|
}
|
|
334
335
|
//#endregion
|
|
335
336
|
//#region src/core/copy-coordinator.ts
|
|
@@ -348,27 +349,72 @@ function createCopyCoordinator() {
|
|
|
348
349
|
};
|
|
349
350
|
}
|
|
350
351
|
//#endregion
|
|
352
|
+
//#region src/core/machine-shared.ts
|
|
353
|
+
/**
|
|
354
|
+
* Internals shared by the copy and paste state machines. Kept in one module so
|
|
355
|
+
* apps that use both don't ship two copies.
|
|
356
|
+
*/
|
|
357
|
+
const DEFAULT_MAX_RETRIES = 3;
|
|
358
|
+
function resolveMaxRetries(value) {
|
|
359
|
+
if (value === Infinity) return Infinity;
|
|
360
|
+
if (value === void 0 || !Number.isFinite(value)) return 3;
|
|
361
|
+
return Math.max(0, Math.floor(value));
|
|
362
|
+
}
|
|
363
|
+
function reportCallbackError(error) {
|
|
364
|
+
const scope = globalThis;
|
|
365
|
+
if (typeof scope.reportError === "function") scope.reportError(error);
|
|
366
|
+
else setTimeout(() => {
|
|
367
|
+
throw error;
|
|
368
|
+
}, 0);
|
|
369
|
+
}
|
|
370
|
+
/** User callbacks must never corrupt machine state; their errors are reported, not swallowed. */
|
|
371
|
+
function invoke(fn, ...args) {
|
|
372
|
+
if (!fn) return;
|
|
373
|
+
try {
|
|
374
|
+
fn(...args);
|
|
375
|
+
} catch (error) {
|
|
376
|
+
reportCallbackError(error);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/** One cancellable operation at a time. Signals only fire while their operation is unfinished. */
|
|
380
|
+
function createOperations() {
|
|
381
|
+
let controller;
|
|
382
|
+
const abort = () => {
|
|
383
|
+
controller?.abort();
|
|
384
|
+
controller = void 0;
|
|
385
|
+
};
|
|
386
|
+
return {
|
|
387
|
+
start: () => {
|
|
388
|
+
abort();
|
|
389
|
+
controller = new AbortController();
|
|
390
|
+
return { signal: controller.signal };
|
|
391
|
+
},
|
|
392
|
+
end: (context) => {
|
|
393
|
+
if (controller?.signal === context.signal) controller = void 0;
|
|
394
|
+
},
|
|
395
|
+
abort
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
//#endregion
|
|
351
399
|
//#region src/core/copy-machine.ts
|
|
352
400
|
const DEFAULT_RESET_AFTER_MS = 2e3;
|
|
353
|
-
const DEFAULT_MAX_RETRIES = 3;
|
|
354
401
|
const IDLE_STATE = /* @__PURE__ */ Object.freeze({ status: "idle" });
|
|
355
402
|
const IGNORED_IN_FLIGHT$1 = /* @__PURE__ */ Object.freeze({
|
|
356
403
|
status: "ignored",
|
|
357
404
|
reason: "in-flight"
|
|
358
405
|
});
|
|
359
|
-
const IGNORED_NOTHING = /* @__PURE__ */ Object.freeze({
|
|
406
|
+
const IGNORED_NOTHING$1 = /* @__PURE__ */ Object.freeze({
|
|
360
407
|
status: "ignored",
|
|
361
408
|
reason: "nothing-to-retry"
|
|
362
409
|
});
|
|
363
|
-
const IGNORED_NOT_RETRYABLE = /* @__PURE__ */ Object.freeze({
|
|
410
|
+
const IGNORED_NOT_RETRYABLE$1 = /* @__PURE__ */ Object.freeze({
|
|
364
411
|
status: "ignored",
|
|
365
412
|
reason: "not-retryable"
|
|
366
413
|
});
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
414
|
+
const IGNORED_CANCELLED$1 = /* @__PURE__ */ Object.freeze({
|
|
415
|
+
status: "ignored",
|
|
416
|
+
reason: "cancelled"
|
|
417
|
+
});
|
|
372
418
|
function resolveResetDelay$1(value) {
|
|
373
419
|
if (value === false || value === Infinity) return null;
|
|
374
420
|
if (value === void 0 || Number.isNaN(value)) return DEFAULT_RESET_AFTER_MS;
|
|
@@ -378,22 +424,6 @@ function resolveResetDelay$1(value) {
|
|
|
378
424
|
function canRetryState(state, maxRetries) {
|
|
379
425
|
return state.status === "error" && state.payload !== null && isRetryableError(state.error) && state.retryCount < resolveMaxRetries(maxRetries);
|
|
380
426
|
}
|
|
381
|
-
function reportCallbackError$1(error) {
|
|
382
|
-
const scope = globalThis;
|
|
383
|
-
if (typeof scope.reportError === "function") scope.reportError(error);
|
|
384
|
-
else setTimeout(() => {
|
|
385
|
-
throw error;
|
|
386
|
-
}, 0);
|
|
387
|
-
}
|
|
388
|
-
/** User callbacks must never corrupt machine state; their errors are reported, not swallowed. */
|
|
389
|
-
function invoke$1(fn, ...args) {
|
|
390
|
-
if (!fn) return;
|
|
391
|
-
try {
|
|
392
|
-
fn(...args);
|
|
393
|
-
} catch (error) {
|
|
394
|
-
reportCallbackError$1(error);
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
427
|
let defaultAdapter$1;
|
|
398
428
|
const getDefaultAdapter$1 = () => defaultAdapter$1 ?? (defaultAdapter$1 = createBrowserClipboardAdapter());
|
|
399
429
|
/** Framework-agnostic copy state machine. */
|
|
@@ -403,6 +433,7 @@ function createCopyMachine(options = {}) {
|
|
|
403
433
|
let state = IDLE_STATE;
|
|
404
434
|
let generation = 0;
|
|
405
435
|
let resetTimer;
|
|
436
|
+
const operations = createOperations();
|
|
406
437
|
const now = () => (read().now ?? Date.now)();
|
|
407
438
|
let joined = null;
|
|
408
439
|
const member = { release: () => {
|
|
@@ -448,7 +479,7 @@ function createCopyMachine(options = {}) {
|
|
|
448
479
|
error,
|
|
449
480
|
retryCount
|
|
450
481
|
});
|
|
451
|
-
invoke
|
|
482
|
+
invoke(read().onError, error, payload);
|
|
452
483
|
};
|
|
453
484
|
const run = (source, retryCount) => {
|
|
454
485
|
clearResetTimer();
|
|
@@ -487,13 +518,15 @@ function createCopyMachine(options = {}) {
|
|
|
487
518
|
payload,
|
|
488
519
|
retryCount
|
|
489
520
|
});
|
|
521
|
+
const context = operations.start();
|
|
490
522
|
let pending;
|
|
491
523
|
try {
|
|
492
|
-
pending = (read().adapter ?? getDefaultAdapter$1()).write(payload);
|
|
524
|
+
pending = (read().adapter ?? getDefaultAdapter$1()).write(payload, context);
|
|
493
525
|
} catch (cause) {
|
|
494
526
|
pending = Promise.reject(cause);
|
|
495
527
|
}
|
|
496
528
|
return pending.then(() => {
|
|
529
|
+
operations.end(context);
|
|
497
530
|
if (current === generation) {
|
|
498
531
|
const copied = {
|
|
499
532
|
status: "copied",
|
|
@@ -503,15 +536,17 @@ function createCopyMachine(options = {}) {
|
|
|
503
536
|
setState(copied);
|
|
504
537
|
joinCoordinator();
|
|
505
538
|
scheduleReset(copied);
|
|
506
|
-
invoke
|
|
539
|
+
invoke(read().onCopy, payload);
|
|
507
540
|
}
|
|
508
541
|
return {
|
|
509
542
|
status: "copied",
|
|
510
543
|
payload
|
|
511
544
|
};
|
|
512
545
|
}, (cause) => {
|
|
546
|
+
operations.end(context);
|
|
547
|
+
if (context.signal.aborted) return IGNORED_CANCELLED$1;
|
|
513
548
|
const error = toCopyError(cause);
|
|
514
|
-
|
|
549
|
+
settleError(error, payload, retryCount);
|
|
515
550
|
return {
|
|
516
551
|
status: "error",
|
|
517
552
|
error
|
|
@@ -520,8 +555,8 @@ function createCopyMachine(options = {}) {
|
|
|
520
555
|
};
|
|
521
556
|
const copy = (source) => state.status === "copying" ? Promise.resolve(IGNORED_IN_FLIGHT$1) : run(source, 0);
|
|
522
557
|
const retry = () => {
|
|
523
|
-
if (state.status !== "error") return Promise.resolve(IGNORED_NOTHING);
|
|
524
|
-
if (state.payload === null || !isRetryableError(state.error)) return Promise.resolve(IGNORED_NOT_RETRYABLE);
|
|
558
|
+
if (state.status !== "error") return Promise.resolve(IGNORED_NOTHING$1);
|
|
559
|
+
if (state.payload === null || !isRetryableError(state.error)) return Promise.resolve(IGNORED_NOT_RETRYABLE$1);
|
|
525
560
|
if (state.retryCount >= resolveMaxRetries(read().maxRetries)) {
|
|
526
561
|
const error = createCopyError("max-retries-exceeded", `Copy failed after ${String(state.retryCount)} retries.`, state.error);
|
|
527
562
|
settleError(error, state.payload, state.retryCount);
|
|
@@ -534,6 +569,7 @@ function createCopyMachine(options = {}) {
|
|
|
534
569
|
};
|
|
535
570
|
const reset = () => {
|
|
536
571
|
++generation;
|
|
572
|
+
operations.abort();
|
|
537
573
|
clearResetTimer();
|
|
538
574
|
setState(IDLE_STATE);
|
|
539
575
|
};
|
|
@@ -541,6 +577,7 @@ function createCopyMachine(options = {}) {
|
|
|
541
577
|
if (state.status === "copied") scheduleReset(state);
|
|
542
578
|
return () => {
|
|
543
579
|
++generation;
|
|
580
|
+
operations.abort();
|
|
544
581
|
clearResetTimer();
|
|
545
582
|
if (state.status === "copying") setState(IDLE_STATE);
|
|
546
583
|
};
|
|
@@ -654,7 +691,7 @@ function snapshotDataTransfer(data, options) {
|
|
|
654
691
|
* Reads the accepted types from `navigator.clipboard.read()` items, enforcing
|
|
655
692
|
* the limits *before* decoding any text so an oversized paste is never buffered twice.
|
|
656
693
|
*/
|
|
657
|
-
async function collectClipboardItems(items, options) {
|
|
694
|
+
async function collectClipboardItems(items, options, signal) {
|
|
658
695
|
const budget = createBudget(options);
|
|
659
696
|
const entries = [];
|
|
660
697
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -668,6 +705,7 @@ async function collectClipboardItems(items, options) {
|
|
|
668
705
|
} catch {
|
|
669
706
|
continue;
|
|
670
707
|
}
|
|
708
|
+
if (signal?.aborted) throw copyFailure("aborted", "The paste was cancelled.");
|
|
671
709
|
if (blob.size === 0) continue;
|
|
672
710
|
budget.charge(blob.size);
|
|
673
711
|
entries.push({
|
|
@@ -751,32 +789,32 @@ async function readPlainText(clipboard, options) {
|
|
|
751
789
|
enforcePasteLimits(entries, options);
|
|
752
790
|
return finalizePaste(entries, "clipboard", options);
|
|
753
791
|
}
|
|
754
|
-
async function readRich(clipboard, options) {
|
|
792
|
+
async function readRich(clipboard, options, signal) {
|
|
755
793
|
let items;
|
|
756
794
|
try {
|
|
757
795
|
items = await clipboard.read();
|
|
758
796
|
} catch (cause) {
|
|
759
797
|
throw new CopyFailure(toCopyError(cause, "read"));
|
|
760
798
|
}
|
|
761
|
-
return finalizePaste(await collectClipboardItems(items, options), "clipboard", options);
|
|
799
|
+
return finalizePaste(await collectClipboardItems(items, options, signal), "clipboard", options);
|
|
762
800
|
}
|
|
763
|
-
function readFromClipboard(env, options) {
|
|
801
|
+
function readFromClipboard(env, options, signal) {
|
|
764
802
|
if (!env) throw copyFailure("unsupported", "No browser environment: the clipboard only exists in the browser.");
|
|
765
803
|
if (env.isSecureContext === false) throw copyFailure("insecure-context", "The Clipboard API requires a secure context (HTTPS or localhost).");
|
|
766
804
|
const clipboard = env.navigator?.clipboard;
|
|
767
805
|
if (!clipboard) throw copyFailure("unsupported", "navigator.clipboard is not available in this browser.");
|
|
768
806
|
const plainOnly = options.accept.every((pattern) => pattern === "text/plain");
|
|
769
807
|
const acceptsPlain = options.accept.some((pattern) => matchesMimePattern("text/plain", pattern));
|
|
770
|
-
if (canRead(clipboard) && !(plainOnly && canReadText(clipboard))) return readRich(clipboard, options);
|
|
808
|
+
if (canRead(clipboard) && !(plainOnly && canReadText(clipboard))) return readRich(clipboard, options, signal);
|
|
771
809
|
if (canReadText(clipboard) && acceptsPlain) return readPlainText(clipboard, options);
|
|
772
810
|
throw canReadText(clipboard) ? copyFailure("unsupported-format", "Reading non-text clipboard content requires navigator.clipboard.read().") : copyFailure("unsupported", "This browser cannot read the clipboard.");
|
|
773
811
|
}
|
|
774
812
|
/** The default paste adapter: async Clipboard API with feature detection and honest errors. */
|
|
775
813
|
function createBrowserPasteAdapter(options = {}) {
|
|
776
814
|
const getEnvironment = options.getEnvironment ?? defaultEnvironment;
|
|
777
|
-
return { read: (resolved) => {
|
|
815
|
+
return { read: (resolved, context) => {
|
|
778
816
|
try {
|
|
779
|
-
return readFromClipboard(getEnvironment(), resolved);
|
|
817
|
+
return readFromClipboard(getEnvironment(), resolved, context?.signal);
|
|
780
818
|
} catch (cause) {
|
|
781
819
|
return Promise.reject(cause);
|
|
782
820
|
}
|
|
@@ -794,25 +832,25 @@ const IGNORED_NO_CONTENT = /* @__PURE__ */ Object.freeze({
|
|
|
794
832
|
status: "ignored",
|
|
795
833
|
reason: "no-accepted-content"
|
|
796
834
|
});
|
|
835
|
+
const IGNORED_NOTHING = /* @__PURE__ */ Object.freeze({
|
|
836
|
+
status: "ignored",
|
|
837
|
+
reason: "nothing-to-retry"
|
|
838
|
+
});
|
|
839
|
+
const IGNORED_NOT_RETRYABLE = /* @__PURE__ */ Object.freeze({
|
|
840
|
+
status: "ignored",
|
|
841
|
+
reason: "not-retryable"
|
|
842
|
+
});
|
|
843
|
+
const IGNORED_CANCELLED = /* @__PURE__ */ Object.freeze({
|
|
844
|
+
status: "ignored",
|
|
845
|
+
reason: "cancelled"
|
|
846
|
+
});
|
|
797
847
|
function resolveResetDelay(value) {
|
|
798
848
|
if (value === void 0 || value === false || value === Infinity || Number.isNaN(value)) return null;
|
|
799
849
|
return Math.max(0, value);
|
|
800
850
|
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
else setTimeout(() => {
|
|
805
|
-
throw error;
|
|
806
|
-
}, 0);
|
|
807
|
-
}
|
|
808
|
-
/** User callbacks must never corrupt machine state; their errors are reported, not swallowed. */
|
|
809
|
-
function invoke(fn, ...args) {
|
|
810
|
-
if (!fn) return;
|
|
811
|
-
try {
|
|
812
|
-
fn(...args);
|
|
813
|
-
} catch (error) {
|
|
814
|
-
reportCallbackError(error);
|
|
815
|
-
}
|
|
851
|
+
/** Pure helper: can `retry()` do anything from this state? */
|
|
852
|
+
function canRetryPasteState(state, maxRetries) {
|
|
853
|
+
return state.status === "error" && state.source === "clipboard" && isRetryableError(state.error) && state.retryCount < resolveMaxRetries(maxRetries);
|
|
816
854
|
}
|
|
817
855
|
let defaultAdapter;
|
|
818
856
|
const getDefaultAdapter = () => defaultAdapter ?? (defaultAdapter = createBrowserPasteAdapter());
|
|
@@ -823,6 +861,7 @@ function createPasteMachine(options = {}) {
|
|
|
823
861
|
let state = PASTE_IDLE_STATE;
|
|
824
862
|
let generation = 0;
|
|
825
863
|
let resetTimer;
|
|
864
|
+
const operations = createOperations();
|
|
826
865
|
const now = () => (read().now ?? Date.now)();
|
|
827
866
|
const setState = (next) => {
|
|
828
867
|
if (next === state) return;
|
|
@@ -845,18 +884,29 @@ function createPasteMachine(options = {}) {
|
|
|
845
884
|
setState(PASTE_IDLE_STATE);
|
|
846
885
|
}, remaining);
|
|
847
886
|
};
|
|
887
|
+
const settleError = (error, source, retryCount) => {
|
|
888
|
+
setState({
|
|
889
|
+
status: "error",
|
|
890
|
+
error,
|
|
891
|
+
source,
|
|
892
|
+
retryCount
|
|
893
|
+
});
|
|
894
|
+
invoke(read().onError, error);
|
|
895
|
+
};
|
|
848
896
|
/** Runs one read. `produce` is invoked synchronously so the user gesture is preserved. */
|
|
849
|
-
const run = (produce) => {
|
|
897
|
+
const run = (produce, source, retryCount) => {
|
|
850
898
|
clearResetTimer();
|
|
851
899
|
const current = ++generation;
|
|
900
|
+
const context = operations.start();
|
|
852
901
|
setState(PASTE_READING_STATE);
|
|
853
902
|
let pending;
|
|
854
903
|
try {
|
|
855
|
-
pending = produce();
|
|
904
|
+
pending = produce(context);
|
|
856
905
|
} catch (cause) {
|
|
857
906
|
pending = Promise.reject(cause);
|
|
858
907
|
}
|
|
859
908
|
return pending.then((result) => {
|
|
909
|
+
operations.end(context);
|
|
860
910
|
if (current === generation) {
|
|
861
911
|
const readState = {
|
|
862
912
|
status: "read",
|
|
@@ -872,26 +922,30 @@ function createPasteMachine(options = {}) {
|
|
|
872
922
|
result
|
|
873
923
|
};
|
|
874
924
|
}, (cause) => {
|
|
925
|
+
operations.end(context);
|
|
926
|
+
if (context.signal.aborted) return IGNORED_CANCELLED;
|
|
875
927
|
const error = toCopyError(cause, "read");
|
|
876
|
-
|
|
877
|
-
setState({
|
|
878
|
-
status: "error",
|
|
879
|
-
error
|
|
880
|
-
});
|
|
881
|
-
invoke(read().onError, error);
|
|
882
|
-
}
|
|
928
|
+
settleError(error, source, retryCount);
|
|
883
929
|
return {
|
|
884
930
|
status: "error",
|
|
885
931
|
error
|
|
886
932
|
};
|
|
887
933
|
});
|
|
888
934
|
};
|
|
889
|
-
const
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
935
|
+
const readClipboard = (retryCount) => run((context) => (read().adapter ?? getDefaultAdapter()).read(resolvePasteReadOptions(read()), context), "clipboard", retryCount);
|
|
936
|
+
const paste = () => state.status === "reading" ? Promise.resolve(IGNORED_IN_FLIGHT) : readClipboard(0);
|
|
937
|
+
const retry = () => {
|
|
938
|
+
if (state.status !== "error") return Promise.resolve(IGNORED_NOTHING);
|
|
939
|
+
if (state.source !== "clipboard" || !isRetryableError(state.error)) return Promise.resolve(IGNORED_NOT_RETRYABLE);
|
|
940
|
+
if (state.retryCount >= resolveMaxRetries(read().maxRetries)) {
|
|
941
|
+
const error = createCopyError("max-retries-exceeded", `Paste failed after ${String(state.retryCount)} retries.`, state.error);
|
|
942
|
+
settleError(error, "clipboard", state.retryCount);
|
|
943
|
+
return Promise.resolve({
|
|
944
|
+
status: "error",
|
|
945
|
+
error
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
return readClipboard(state.retryCount + 1);
|
|
895
949
|
};
|
|
896
950
|
const pasteEvent = (event) => {
|
|
897
951
|
const data = event.clipboardData;
|
|
@@ -903,12 +957,9 @@ function createPasteMachine(options = {}) {
|
|
|
903
957
|
} catch (cause) {
|
|
904
958
|
const error = toCopyError(cause, "read");
|
|
905
959
|
++generation;
|
|
960
|
+
operations.abort();
|
|
906
961
|
clearResetTimer();
|
|
907
|
-
|
|
908
|
-
status: "error",
|
|
909
|
-
error
|
|
910
|
-
});
|
|
911
|
-
invoke(current.onError, error);
|
|
962
|
+
settleError(error, "event", 0);
|
|
912
963
|
return Promise.resolve({
|
|
913
964
|
status: "error",
|
|
914
965
|
error
|
|
@@ -920,10 +971,11 @@ function createPasteMachine(options = {}) {
|
|
|
920
971
|
return run(() => {
|
|
921
972
|
enforcePasteLimits(entries, resolved);
|
|
922
973
|
return finalizePaste(entries, "event", resolved);
|
|
923
|
-
});
|
|
974
|
+
}, "event", 0);
|
|
924
975
|
};
|
|
925
976
|
const reset = () => {
|
|
926
977
|
++generation;
|
|
978
|
+
operations.abort();
|
|
927
979
|
clearResetTimer();
|
|
928
980
|
setState(PASTE_IDLE_STATE);
|
|
929
981
|
};
|
|
@@ -931,6 +983,7 @@ function createPasteMachine(options = {}) {
|
|
|
931
983
|
if (state.status === "read") scheduleReset(state);
|
|
932
984
|
return () => {
|
|
933
985
|
++generation;
|
|
986
|
+
operations.abort();
|
|
934
987
|
clearResetTimer();
|
|
935
988
|
if (state.status === "reading") setState(PASTE_IDLE_STATE);
|
|
936
989
|
};
|
|
@@ -945,6 +998,7 @@ function createPasteMachine(options = {}) {
|
|
|
945
998
|
},
|
|
946
999
|
paste,
|
|
947
1000
|
pasteEvent,
|
|
1001
|
+
retry,
|
|
948
1002
|
reset,
|
|
949
1003
|
connect
|
|
950
1004
|
};
|
|
@@ -1008,7 +1062,7 @@ function useCopy(options = {}) {
|
|
|
1008
1062
|
const [machine] = (0, react.useState)(() => createCopyMachine(() => optionsRef.current));
|
|
1009
1063
|
(0, react.useEffect)(() => machine.connect(), [machine]);
|
|
1010
1064
|
const state = (0, react.useSyncExternalStore)(machine.subscribe, machine.getSnapshot, getServerSnapshot$2);
|
|
1011
|
-
const canRetry = canRetryState(state,
|
|
1065
|
+
const canRetry = canRetryState(state, effective.maxRetries);
|
|
1012
1066
|
return (0, react.useMemo)(() => ({
|
|
1013
1067
|
state,
|
|
1014
1068
|
status: state.status,
|
|
@@ -1434,6 +1488,7 @@ function usePaste(options = {}) {
|
|
|
1434
1488
|
};
|
|
1435
1489
|
}, [listenOnDocument, machine]);
|
|
1436
1490
|
const state = (0, react.useSyncExternalStore)(machine.subscribe, machine.getSnapshot, getServerSnapshot);
|
|
1491
|
+
const canRetry = canRetryPasteState(state, options.maxRetries);
|
|
1437
1492
|
const targetProps = (0, react.useMemo)(() => ({ onPaste: (event) => {
|
|
1438
1493
|
machine.pasteEvent(event);
|
|
1439
1494
|
} }), [machine]);
|
|
@@ -1442,12 +1497,15 @@ function usePaste(options = {}) {
|
|
|
1442
1497
|
status: state.status,
|
|
1443
1498
|
result: state.status === "read" ? state.result : null,
|
|
1444
1499
|
error: state.status === "error" ? state.error : null,
|
|
1500
|
+
canRetry,
|
|
1445
1501
|
paste: machine.paste,
|
|
1446
1502
|
pasteEvent: machine.pasteEvent,
|
|
1503
|
+
retry: machine.retry,
|
|
1447
1504
|
reset: machine.reset,
|
|
1448
1505
|
targetProps
|
|
1449
1506
|
}), [
|
|
1450
1507
|
state,
|
|
1508
|
+
canRetry,
|
|
1451
1509
|
machine,
|
|
1452
1510
|
targetProps
|
|
1453
1511
|
]);
|
|
@@ -1636,6 +1694,56 @@ const DEFAULT_TRIGGER_TEXT = {
|
|
|
1636
1694
|
read: "Pasted",
|
|
1637
1695
|
error: "Retry"
|
|
1638
1696
|
};
|
|
1697
|
+
const Trigger = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldTrigger({ children, onClick, "aria-label": ariaLabel, ...rest }, ref) {
|
|
1698
|
+
const field = usePasteField();
|
|
1699
|
+
const renderProps = {
|
|
1700
|
+
status: field.status,
|
|
1701
|
+
displayStatus: field.displayStatus,
|
|
1702
|
+
state: field.state,
|
|
1703
|
+
revealed: field.revealed,
|
|
1704
|
+
canRetry: field.canRetry
|
|
1705
|
+
};
|
|
1706
|
+
const safeAriaLabel = (ariaLabel ?? field.messages.triggerLabel(field.label)) || defaultPasteFieldMessages.triggerLabel(field.label);
|
|
1707
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
|
|
1708
|
+
...rest,
|
|
1709
|
+
ref,
|
|
1710
|
+
type: "button",
|
|
1711
|
+
"aria-label": safeAriaLabel,
|
|
1712
|
+
"aria-busy": field.status === "reading" ? true : void 0,
|
|
1713
|
+
"data-state": field.status,
|
|
1714
|
+
"data-display-state": field.displayStatus,
|
|
1715
|
+
"data-revealed": field.revealed ? "" : void 0,
|
|
1716
|
+
onClick: composeEventHandlers(onClick, () => {
|
|
1717
|
+
field.doPaste();
|
|
1718
|
+
}),
|
|
1719
|
+
children: typeof children === "function" ? children(renderProps) : children ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TriggerLabel, {
|
|
1720
|
+
status: field.displayStatus,
|
|
1721
|
+
labels: DEFAULT_TRIGGER_TEXT
|
|
1722
|
+
})
|
|
1723
|
+
});
|
|
1724
|
+
});
|
|
1725
|
+
const NO_URLS = [];
|
|
1726
|
+
/**
|
|
1727
|
+
* Object URLs for blobs, revoked when the blobs change or the component
|
|
1728
|
+
* unmounts. URLs are only returned for the blobs they were made from, so they
|
|
1729
|
+
* never pair with the wrong result.
|
|
1730
|
+
*/
|
|
1731
|
+
function useObjectUrls(blobs) {
|
|
1732
|
+
const [entry, setEntry] = (0, react.useState)({ urls: NO_URLS });
|
|
1733
|
+
(0, react.useEffect)(() => {
|
|
1734
|
+
if (!blobs?.length || typeof URL.createObjectURL !== "function") return void 0;
|
|
1735
|
+
const urls = blobs.map((blob) => URL.createObjectURL(blob));
|
|
1736
|
+
setEntry({
|
|
1737
|
+
blobs,
|
|
1738
|
+
urls
|
|
1739
|
+
});
|
|
1740
|
+
return () => {
|
|
1741
|
+
for (const url of urls) URL.revokeObjectURL(url);
|
|
1742
|
+
};
|
|
1743
|
+
}, [blobs]);
|
|
1744
|
+
return entry.blobs === blobs ? entry.urls : NO_URLS;
|
|
1745
|
+
}
|
|
1746
|
+
const defaultImageAlt = (index, total) => `Pasted image ${String(index + 1)} of ${String(total)}`;
|
|
1639
1747
|
/**
|
|
1640
1748
|
* Headless compound component for a labelled, paste-ready field.
|
|
1641
1749
|
*
|
|
@@ -1650,6 +1758,7 @@ const DEFAULT_TRIGGER_TEXT = {
|
|
|
1650
1758
|
* <PasteField.Label />
|
|
1651
1759
|
* <PasteField.Zone />
|
|
1652
1760
|
* <PasteField.Trigger />
|
|
1761
|
+
* <PasteField.Preview placeholder="Nothing pasted yet" />
|
|
1653
1762
|
* </PasteField.Root>
|
|
1654
1763
|
* ```
|
|
1655
1764
|
*/
|
|
@@ -1658,31 +1767,43 @@ const PasteField = {
|
|
|
1658
1767
|
Label,
|
|
1659
1768
|
Status,
|
|
1660
1769
|
Zone,
|
|
1661
|
-
Trigger
|
|
1662
|
-
|
|
1663
|
-
const
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1770
|
+
Trigger,
|
|
1771
|
+
Preview: /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldPreview({ children, placeholder = null, maxTextLength = 2e3, imageAlt = defaultImageAlt, ...rest }, ref) {
|
|
1772
|
+
const { result, status } = usePasteField();
|
|
1773
|
+
const imageUrls = useObjectUrls(result?.images);
|
|
1774
|
+
let content = placeholder;
|
|
1775
|
+
if (result) {
|
|
1776
|
+
if (children) content = children({
|
|
1777
|
+
result,
|
|
1778
|
+
imageUrls
|
|
1779
|
+
});
|
|
1780
|
+
else {
|
|
1781
|
+
const { text } = result;
|
|
1782
|
+
const files = result.files.filter((file) => !file.type.startsWith("image/"));
|
|
1783
|
+
content = /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
|
|
1784
|
+
text ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("pre", {
|
|
1785
|
+
"data-preview-text": "",
|
|
1786
|
+
children: text.length > maxTextLength ? `${text.slice(0, maxTextLength)}…` : text
|
|
1787
|
+
}) : null,
|
|
1788
|
+
imageUrls.map((url, index) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", {
|
|
1789
|
+
"data-preview-image": "",
|
|
1790
|
+
src: url,
|
|
1791
|
+
alt: imageAlt(index, imageUrls.length)
|
|
1792
|
+
}, url)),
|
|
1793
|
+
files.length > 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
|
|
1794
|
+
"data-preview-files": "",
|
|
1795
|
+
children: files.map((file, index) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("li", { children: file.name }, `${String(index)}-${file.name}`))
|
|
1796
|
+
}) : null
|
|
1797
|
+
] });
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
1671
1801
|
...rest,
|
|
1672
1802
|
ref,
|
|
1673
|
-
|
|
1674
|
-
"
|
|
1675
|
-
"
|
|
1676
|
-
|
|
1677
|
-
"data-display-state": field.displayStatus,
|
|
1678
|
-
"data-revealed": field.revealed ? "" : void 0,
|
|
1679
|
-
onClick: composeEventHandlers(onClick, () => {
|
|
1680
|
-
field.doPaste();
|
|
1681
|
-
}),
|
|
1682
|
-
children: typeof children === "function" ? children(renderProps) : children ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TriggerLabel, {
|
|
1683
|
-
status: field.displayStatus,
|
|
1684
|
-
labels: DEFAULT_TRIGGER_TEXT
|
|
1685
|
-
})
|
|
1803
|
+
"data-preview": "",
|
|
1804
|
+
"data-state": status,
|
|
1805
|
+
"data-empty": result ? void 0 : "",
|
|
1806
|
+
children: content
|
|
1686
1807
|
});
|
|
1687
1808
|
})
|
|
1688
1809
|
};
|
|
@@ -1705,6 +1826,7 @@ exports.LiveRegion = LiveRegion;
|
|
|
1705
1826
|
exports.PASTE_IDLE_STATE = PASTE_IDLE_STATE;
|
|
1706
1827
|
exports.PasteField = PasteField;
|
|
1707
1828
|
exports.SVG_MIME_TYPE = SVG_MIME_TYPE;
|
|
1829
|
+
exports.canRetryPasteState = canRetryPasteState;
|
|
1708
1830
|
exports.canRetryState = canRetryState;
|
|
1709
1831
|
exports.copyFailure = copyFailure;
|
|
1710
1832
|
exports.createBrowserClipboardAdapter = createBrowserClipboardAdapter;
|