react-smart-copy 1.1.1 → 1.3.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 +80 -12
- 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 +176 -82
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +108 -15
- package/dist/core.d.ts +108 -15
- package/dist/core.js +176 -83
- package/dist/core.js.map +1 -1
- package/dist/index.cjs +267 -106
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +136 -17
- package/dist/index.d.ts +136 -17
- package/dist/index.js +268 -108
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/dist/core.cjs
CHANGED
|
@@ -264,20 +264,21 @@ function writeText(clipboard, text) {
|
|
|
264
264
|
if (typeof clipboard.writeText !== "function") throw fail("unsupported", "navigator.clipboard.writeText() is not available.");
|
|
265
265
|
return clipboard.writeText(text);
|
|
266
266
|
}
|
|
267
|
-
async function resolveImageBlob(source) {
|
|
267
|
+
async function resolveImageBlob(source, context) {
|
|
268
268
|
let blob;
|
|
269
269
|
try {
|
|
270
|
-
blob = await (typeof source === "function" ? source() : source);
|
|
270
|
+
blob = await (typeof source === "function" ? source(context ?? { signal: new AbortController().signal }) : source);
|
|
271
271
|
} catch (cause) {
|
|
272
|
+
if (context?.signal.aborted) throw fail("aborted", "The image copy was cancelled.", cause);
|
|
272
273
|
throw fail("blob-generation-failed", "The image source threw or rejected while producing a Blob.", cause);
|
|
273
274
|
}
|
|
274
275
|
if (!isBlob(blob)) throw fail("blob-generation-failed", "The image source did not produce a Blob.");
|
|
275
276
|
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}").`);
|
|
276
277
|
return blob;
|
|
277
278
|
}
|
|
278
|
-
async function writeImage(rich, source) {
|
|
279
|
+
async function writeImage(rich, source, context) {
|
|
279
280
|
if (!isTypeSupported(rich.Item, "image/png")) throw fail("unsupported-format", `This browser cannot write ${IMAGE_MIME_TYPE} to the clipboard.`);
|
|
280
|
-
const blob = resolveImageBlob(source);
|
|
281
|
+
const blob = resolveImageBlob(source, context);
|
|
281
282
|
blob.catch(noop);
|
|
282
283
|
try {
|
|
283
284
|
await rich.write([new rich.Item({ [IMAGE_MIME_TYPE]: blob })]);
|
|
@@ -286,7 +287,7 @@ async function writeImage(rich, source) {
|
|
|
286
287
|
}
|
|
287
288
|
await blob;
|
|
288
289
|
}
|
|
289
|
-
async function writeToClipboard(payload, env, degradeHtmlToText) {
|
|
290
|
+
async function writeToClipboard(payload, env, degradeHtmlToText, context) {
|
|
290
291
|
if (!env) throw fail("unsupported", "No browser environment: the clipboard only exists in the browser.");
|
|
291
292
|
if (env.isSecureContext === false) throw fail("insecure-context", "The Clipboard API requires a secure context (HTTPS or localhost).");
|
|
292
293
|
const clipboard = env.navigator?.clipboard;
|
|
@@ -308,7 +309,7 @@ async function writeToClipboard(payload, env, degradeHtmlToText) {
|
|
|
308
309
|
case "image": {
|
|
309
310
|
const rich = getRichClipboard(env, clipboard);
|
|
310
311
|
if (!rich) throw fail("unsupported-format", "Image copy requires ClipboardItem and navigator.clipboard.write().");
|
|
311
|
-
return writeImage(rich, payload.blob);
|
|
312
|
+
return writeImage(rich, payload.blob, context);
|
|
312
313
|
}
|
|
313
314
|
case "multi": {
|
|
314
315
|
const rich = getRichClipboard(env, clipboard);
|
|
@@ -326,7 +327,7 @@ async function writeToClipboard(payload, env, degradeHtmlToText) {
|
|
|
326
327
|
function createBrowserClipboardAdapter(options = {}) {
|
|
327
328
|
const getEnvironment = options.getEnvironment ?? defaultEnvironment$1;
|
|
328
329
|
const degradeHtmlToText = options.degradeHtmlToText ?? true;
|
|
329
|
-
return { write: (payload) => writeToClipboard(payload, getEnvironment(), degradeHtmlToText) };
|
|
330
|
+
return { write: (payload, context) => writeToClipboard(payload, getEnvironment(), degradeHtmlToText, context) };
|
|
330
331
|
}
|
|
331
332
|
//#endregion
|
|
332
333
|
//#region src/core/copy-coordinator.ts
|
|
@@ -345,27 +346,74 @@ function createCopyCoordinator() {
|
|
|
345
346
|
};
|
|
346
347
|
}
|
|
347
348
|
//#endregion
|
|
349
|
+
//#region src/core/machine-shared.ts
|
|
350
|
+
/**
|
|
351
|
+
* Internals shared by the copy and paste state machines. Kept in one module so
|
|
352
|
+
* apps that use both don't ship two copies.
|
|
353
|
+
*/
|
|
354
|
+
const DEFAULT_MAX_RETRIES = 3;
|
|
355
|
+
function resolveMaxRetries(value) {
|
|
356
|
+
if (value === Infinity) return Infinity;
|
|
357
|
+
if (value === void 0 || !Number.isFinite(value)) return 3;
|
|
358
|
+
return Math.max(0, Math.floor(value));
|
|
359
|
+
}
|
|
360
|
+
function reportCallbackError(error) {
|
|
361
|
+
const scope = globalThis;
|
|
362
|
+
if (typeof scope.reportError === "function") scope.reportError(error);
|
|
363
|
+
else setTimeout(() => {
|
|
364
|
+
throw error;
|
|
365
|
+
}, 0);
|
|
366
|
+
}
|
|
367
|
+
/** User callbacks must never corrupt machine state; their errors are reported, not swallowed. */
|
|
368
|
+
function invoke(fn, ...args) {
|
|
369
|
+
if (!fn) return;
|
|
370
|
+
try {
|
|
371
|
+
fn(...args);
|
|
372
|
+
} catch (error) {
|
|
373
|
+
reportCallbackError(error);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
/** One cancellable operation at a time. Signals only fire while their operation is unfinished. */
|
|
377
|
+
function createOperations() {
|
|
378
|
+
let controller;
|
|
379
|
+
const abort = () => {
|
|
380
|
+
if (!controller) return false;
|
|
381
|
+
controller.abort();
|
|
382
|
+
controller = void 0;
|
|
383
|
+
return true;
|
|
384
|
+
};
|
|
385
|
+
return {
|
|
386
|
+
start: () => {
|
|
387
|
+
abort();
|
|
388
|
+
controller = new AbortController();
|
|
389
|
+
return { signal: controller.signal };
|
|
390
|
+
},
|
|
391
|
+
end: (context) => {
|
|
392
|
+
if (controller?.signal === context.signal) controller = void 0;
|
|
393
|
+
},
|
|
394
|
+
abort
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
//#endregion
|
|
348
398
|
//#region src/core/copy-machine.ts
|
|
349
399
|
const DEFAULT_RESET_AFTER_MS = 2e3;
|
|
350
|
-
const DEFAULT_MAX_RETRIES = 3;
|
|
351
400
|
const IDLE_STATE = /* @__PURE__ */ Object.freeze({ status: "idle" });
|
|
352
401
|
const IGNORED_IN_FLIGHT$1 = /* @__PURE__ */ Object.freeze({
|
|
353
402
|
status: "ignored",
|
|
354
403
|
reason: "in-flight"
|
|
355
404
|
});
|
|
356
|
-
const IGNORED_NOTHING = /* @__PURE__ */ Object.freeze({
|
|
405
|
+
const IGNORED_NOTHING$1 = /* @__PURE__ */ Object.freeze({
|
|
357
406
|
status: "ignored",
|
|
358
407
|
reason: "nothing-to-retry"
|
|
359
408
|
});
|
|
360
|
-
const IGNORED_NOT_RETRYABLE = /* @__PURE__ */ Object.freeze({
|
|
409
|
+
const IGNORED_NOT_RETRYABLE$1 = /* @__PURE__ */ Object.freeze({
|
|
361
410
|
status: "ignored",
|
|
362
411
|
reason: "not-retryable"
|
|
363
412
|
});
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
}
|
|
413
|
+
const IGNORED_CANCELLED$1 = /* @__PURE__ */ Object.freeze({
|
|
414
|
+
status: "ignored",
|
|
415
|
+
reason: "cancelled"
|
|
416
|
+
});
|
|
369
417
|
function resolveResetDelay$1(value) {
|
|
370
418
|
if (value === false || value === Infinity) return null;
|
|
371
419
|
if (value === void 0 || Number.isNaN(value)) return DEFAULT_RESET_AFTER_MS;
|
|
@@ -375,22 +423,6 @@ function resolveResetDelay$1(value) {
|
|
|
375
423
|
function canRetryState(state, maxRetries) {
|
|
376
424
|
return state.status === "error" && state.payload !== null && isRetryableError(state.error) && state.retryCount < resolveMaxRetries(maxRetries);
|
|
377
425
|
}
|
|
378
|
-
function reportCallbackError$1(error) {
|
|
379
|
-
const scope = globalThis;
|
|
380
|
-
if (typeof scope.reportError === "function") scope.reportError(error);
|
|
381
|
-
else setTimeout(() => {
|
|
382
|
-
throw error;
|
|
383
|
-
}, 0);
|
|
384
|
-
}
|
|
385
|
-
/** User callbacks must never corrupt machine state; their errors are reported, not swallowed. */
|
|
386
|
-
function invoke$1(fn, ...args) {
|
|
387
|
-
if (!fn) return;
|
|
388
|
-
try {
|
|
389
|
-
fn(...args);
|
|
390
|
-
} catch (error) {
|
|
391
|
-
reportCallbackError$1(error);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
426
|
let defaultAdapter$1;
|
|
395
427
|
const getDefaultAdapter$1 = () => defaultAdapter$1 ?? (defaultAdapter$1 = createBrowserClipboardAdapter());
|
|
396
428
|
/** Framework-agnostic copy state machine. */
|
|
@@ -400,6 +432,7 @@ function createCopyMachine(options = {}) {
|
|
|
400
432
|
let state = IDLE_STATE;
|
|
401
433
|
let generation = 0;
|
|
402
434
|
let resetTimer;
|
|
435
|
+
const operations = createOperations();
|
|
403
436
|
const now = () => (read().now ?? Date.now)();
|
|
404
437
|
let joined = null;
|
|
405
438
|
const member = { release: () => {
|
|
@@ -445,7 +478,7 @@ function createCopyMachine(options = {}) {
|
|
|
445
478
|
error,
|
|
446
479
|
retryCount
|
|
447
480
|
});
|
|
448
|
-
invoke
|
|
481
|
+
invoke(read().onError, error, payload);
|
|
449
482
|
};
|
|
450
483
|
const run = (source, retryCount) => {
|
|
451
484
|
clearResetTimer();
|
|
@@ -484,13 +517,15 @@ function createCopyMachine(options = {}) {
|
|
|
484
517
|
payload,
|
|
485
518
|
retryCount
|
|
486
519
|
});
|
|
520
|
+
const context = operations.start();
|
|
487
521
|
let pending;
|
|
488
522
|
try {
|
|
489
|
-
pending = (read().adapter ?? getDefaultAdapter$1()).write(payload);
|
|
523
|
+
pending = (read().adapter ?? getDefaultAdapter$1()).write(payload, context);
|
|
490
524
|
} catch (cause) {
|
|
491
525
|
pending = Promise.reject(cause);
|
|
492
526
|
}
|
|
493
527
|
return pending.then(() => {
|
|
528
|
+
operations.end(context);
|
|
494
529
|
if (current === generation) {
|
|
495
530
|
const copied = {
|
|
496
531
|
status: "copied",
|
|
@@ -500,15 +535,17 @@ function createCopyMachine(options = {}) {
|
|
|
500
535
|
setState(copied);
|
|
501
536
|
joinCoordinator();
|
|
502
537
|
scheduleReset(copied);
|
|
503
|
-
invoke
|
|
538
|
+
invoke(read().onCopy, payload);
|
|
504
539
|
}
|
|
505
540
|
return {
|
|
506
541
|
status: "copied",
|
|
507
542
|
payload
|
|
508
543
|
};
|
|
509
544
|
}, (cause) => {
|
|
545
|
+
operations.end(context);
|
|
546
|
+
if (context.signal.aborted) return IGNORED_CANCELLED$1;
|
|
510
547
|
const error = toCopyError(cause);
|
|
511
|
-
|
|
548
|
+
settleError(error, payload, retryCount);
|
|
512
549
|
return {
|
|
513
550
|
status: "error",
|
|
514
551
|
error
|
|
@@ -517,8 +554,8 @@ function createCopyMachine(options = {}) {
|
|
|
517
554
|
};
|
|
518
555
|
const copy = (source) => state.status === "copying" ? Promise.resolve(IGNORED_IN_FLIGHT$1) : run(source, 0);
|
|
519
556
|
const retry = () => {
|
|
520
|
-
if (state.status !== "error") return Promise.resolve(IGNORED_NOTHING);
|
|
521
|
-
if (state.payload === null || !isRetryableError(state.error)) return Promise.resolve(IGNORED_NOT_RETRYABLE);
|
|
557
|
+
if (state.status !== "error") return Promise.resolve(IGNORED_NOTHING$1);
|
|
558
|
+
if (state.payload === null || !isRetryableError(state.error)) return Promise.resolve(IGNORED_NOT_RETRYABLE$1);
|
|
522
559
|
if (state.retryCount >= resolveMaxRetries(read().maxRetries)) {
|
|
523
560
|
const error = createCopyError("max-retries-exceeded", `Copy failed after ${String(state.retryCount)} retries.`, state.error);
|
|
524
561
|
settleError(error, state.payload, state.retryCount);
|
|
@@ -531,15 +568,21 @@ function createCopyMachine(options = {}) {
|
|
|
531
568
|
};
|
|
532
569
|
const reset = () => {
|
|
533
570
|
++generation;
|
|
571
|
+
const cancelled = operations.abort();
|
|
534
572
|
clearResetTimer();
|
|
573
|
+
const wasIdle = state.status === "idle";
|
|
535
574
|
setState(IDLE_STATE);
|
|
575
|
+
if (cancelled) invoke(read().onCancel, "reset");
|
|
576
|
+
if (!wasIdle) invoke(read().onReset);
|
|
536
577
|
};
|
|
537
578
|
const connect = () => {
|
|
538
579
|
if (state.status === "copied") scheduleReset(state);
|
|
539
580
|
return () => {
|
|
540
581
|
++generation;
|
|
582
|
+
const cancelled = operations.abort();
|
|
541
583
|
clearResetTimer();
|
|
542
584
|
if (state.status === "copying") setState(IDLE_STATE);
|
|
585
|
+
if (cancelled) invoke(read().onCancel, "disconnect");
|
|
543
586
|
};
|
|
544
587
|
};
|
|
545
588
|
return {
|
|
@@ -558,6 +601,14 @@ function createCopyMachine(options = {}) {
|
|
|
558
601
|
}
|
|
559
602
|
//#endregion
|
|
560
603
|
//#region src/core/paste-reader.ts
|
|
604
|
+
/** Adds the derived fields to an adapter's result. Any `imageFiles` it carried is recomputed. */
|
|
605
|
+
function completePasteResult(result) {
|
|
606
|
+
const images = new Set(result.images);
|
|
607
|
+
return {
|
|
608
|
+
...result,
|
|
609
|
+
imageFiles: result.files.filter((file) => images.has(file))
|
|
610
|
+
};
|
|
611
|
+
}
|
|
561
612
|
const DEFAULT_PASTE_ACCEPT = /* @__PURE__ */ Object.freeze(["text"]);
|
|
562
613
|
const DEFAULT_PASTE_MAX_BYTES = 33554432;
|
|
563
614
|
const DEFAULT_PASTE_MAX_ITEMS = 32;
|
|
@@ -613,7 +664,25 @@ function createBudget(options) {
|
|
|
613
664
|
if (bytes > options.maxBytes) throw copyFailure("too-large", `The paste exceeds the ${String(options.maxBytes)}-byte limit.`);
|
|
614
665
|
} };
|
|
615
666
|
}
|
|
616
|
-
|
|
667
|
+
/**
|
|
668
|
+
* UTF-8 byte length of a string, without allocating an encoded copy.
|
|
669
|
+
* Lone surrogates count 3 bytes, as `TextEncoder` / `Blob` encode them as U+FFFD.
|
|
670
|
+
*/
|
|
671
|
+
function utf8ByteLength(text) {
|
|
672
|
+
let bytes = 0;
|
|
673
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
674
|
+
const unit = text.charCodeAt(i);
|
|
675
|
+
if (unit < 128) bytes += 1;
|
|
676
|
+
else if (unit < 2048) bytes += 2;
|
|
677
|
+
else if (unit >= 55296 && unit <= 56319 && i + 1 < text.length && (text.charCodeAt(i + 1) & 64512) === 56320) {
|
|
678
|
+
bytes += 4;
|
|
679
|
+
i += 1;
|
|
680
|
+
} else bytes += 3;
|
|
681
|
+
}
|
|
682
|
+
return bytes;
|
|
683
|
+
}
|
|
684
|
+
/** Strings are measured as UTF-8, the same unit `Blob.size` reports for clipboard items. */
|
|
685
|
+
const sizeOf = (data) => typeof data === "string" ? utf8ByteLength(data) : data.size;
|
|
617
686
|
/** Throws `too-large` when the entries break `maxItems` / `maxBytes`. */
|
|
618
687
|
function enforcePasteLimits(entries, options) {
|
|
619
688
|
const budget = createBudget(options);
|
|
@@ -651,7 +720,7 @@ function snapshotDataTransfer(data, options) {
|
|
|
651
720
|
* Reads the accepted types from `navigator.clipboard.read()` items, enforcing
|
|
652
721
|
* the limits *before* decoding any text so an oversized paste is never buffered twice.
|
|
653
722
|
*/
|
|
654
|
-
async function collectClipboardItems(items, options) {
|
|
723
|
+
async function collectClipboardItems(items, options, signal) {
|
|
655
724
|
const budget = createBudget(options);
|
|
656
725
|
const entries = [];
|
|
657
726
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -665,6 +734,7 @@ async function collectClipboardItems(items, options) {
|
|
|
665
734
|
} catch {
|
|
666
735
|
continue;
|
|
667
736
|
}
|
|
737
|
+
if (signal?.aborted) throw copyFailure("aborted", "The paste was cancelled.");
|
|
668
738
|
if (blob.size === 0) continue;
|
|
669
739
|
budget.charge(blob.size);
|
|
670
740
|
entries.push({
|
|
@@ -748,32 +818,32 @@ async function readPlainText(clipboard, options) {
|
|
|
748
818
|
enforcePasteLimits(entries, options);
|
|
749
819
|
return finalizePaste(entries, "clipboard", options);
|
|
750
820
|
}
|
|
751
|
-
async function readRich(clipboard, options) {
|
|
821
|
+
async function readRich(clipboard, options, signal) {
|
|
752
822
|
let items;
|
|
753
823
|
try {
|
|
754
824
|
items = await clipboard.read();
|
|
755
825
|
} catch (cause) {
|
|
756
826
|
throw new CopyFailure(toCopyError(cause, "read"));
|
|
757
827
|
}
|
|
758
|
-
return finalizePaste(await collectClipboardItems(items, options), "clipboard", options);
|
|
828
|
+
return finalizePaste(await collectClipboardItems(items, options, signal), "clipboard", options);
|
|
759
829
|
}
|
|
760
|
-
function readFromClipboard(env, options) {
|
|
830
|
+
function readFromClipboard(env, options, signal) {
|
|
761
831
|
if (!env) throw copyFailure("unsupported", "No browser environment: the clipboard only exists in the browser.");
|
|
762
832
|
if (env.isSecureContext === false) throw copyFailure("insecure-context", "The Clipboard API requires a secure context (HTTPS or localhost).");
|
|
763
833
|
const clipboard = env.navigator?.clipboard;
|
|
764
834
|
if (!clipboard) throw copyFailure("unsupported", "navigator.clipboard is not available in this browser.");
|
|
765
835
|
const plainOnly = options.accept.every((pattern) => pattern === "text/plain");
|
|
766
836
|
const acceptsPlain = options.accept.some((pattern) => matchesMimePattern("text/plain", pattern));
|
|
767
|
-
if (canRead(clipboard) && !(plainOnly && canReadText(clipboard))) return readRich(clipboard, options);
|
|
837
|
+
if (canRead(clipboard) && !(plainOnly && canReadText(clipboard))) return readRich(clipboard, options, signal);
|
|
768
838
|
if (canReadText(clipboard) && acceptsPlain) return readPlainText(clipboard, options);
|
|
769
839
|
throw canReadText(clipboard) ? copyFailure("unsupported-format", "Reading non-text clipboard content requires navigator.clipboard.read().") : copyFailure("unsupported", "This browser cannot read the clipboard.");
|
|
770
840
|
}
|
|
771
841
|
/** The default paste adapter: async Clipboard API with feature detection and honest errors. */
|
|
772
842
|
function createBrowserPasteAdapter(options = {}) {
|
|
773
843
|
const getEnvironment = options.getEnvironment ?? defaultEnvironment;
|
|
774
|
-
return { read: (resolved) => {
|
|
844
|
+
return { read: (resolved, context) => {
|
|
775
845
|
try {
|
|
776
|
-
return readFromClipboard(getEnvironment(), resolved);
|
|
846
|
+
return readFromClipboard(getEnvironment(), resolved, context?.signal);
|
|
777
847
|
} catch (cause) {
|
|
778
848
|
return Promise.reject(cause);
|
|
779
849
|
}
|
|
@@ -791,25 +861,25 @@ const IGNORED_NO_CONTENT = /* @__PURE__ */ Object.freeze({
|
|
|
791
861
|
status: "ignored",
|
|
792
862
|
reason: "no-accepted-content"
|
|
793
863
|
});
|
|
864
|
+
const IGNORED_NOTHING = /* @__PURE__ */ Object.freeze({
|
|
865
|
+
status: "ignored",
|
|
866
|
+
reason: "nothing-to-retry"
|
|
867
|
+
});
|
|
868
|
+
const IGNORED_NOT_RETRYABLE = /* @__PURE__ */ Object.freeze({
|
|
869
|
+
status: "ignored",
|
|
870
|
+
reason: "not-retryable"
|
|
871
|
+
});
|
|
872
|
+
const IGNORED_CANCELLED = /* @__PURE__ */ Object.freeze({
|
|
873
|
+
status: "ignored",
|
|
874
|
+
reason: "cancelled"
|
|
875
|
+
});
|
|
794
876
|
function resolveResetDelay(value) {
|
|
795
877
|
if (value === void 0 || value === false || value === Infinity || Number.isNaN(value)) return null;
|
|
796
878
|
return Math.max(0, value);
|
|
797
879
|
}
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
else setTimeout(() => {
|
|
802
|
-
throw error;
|
|
803
|
-
}, 0);
|
|
804
|
-
}
|
|
805
|
-
/** User callbacks must never corrupt machine state; their errors are reported, not swallowed. */
|
|
806
|
-
function invoke(fn, ...args) {
|
|
807
|
-
if (!fn) return;
|
|
808
|
-
try {
|
|
809
|
-
fn(...args);
|
|
810
|
-
} catch (error) {
|
|
811
|
-
reportCallbackError(error);
|
|
812
|
-
}
|
|
880
|
+
/** Pure helper: can `retry()` do anything from this state? */
|
|
881
|
+
function canRetryPasteState(state, maxRetries) {
|
|
882
|
+
return state.status === "error" && state.source === "clipboard" && isRetryableError(state.error) && state.retryCount < resolveMaxRetries(maxRetries);
|
|
813
883
|
}
|
|
814
884
|
let defaultAdapter;
|
|
815
885
|
const getDefaultAdapter = () => defaultAdapter ?? (defaultAdapter = createBrowserPasteAdapter());
|
|
@@ -820,6 +890,7 @@ function createPasteMachine(options = {}) {
|
|
|
820
890
|
let state = PASTE_IDLE_STATE;
|
|
821
891
|
let generation = 0;
|
|
822
892
|
let resetTimer;
|
|
893
|
+
const operations = createOperations();
|
|
823
894
|
const now = () => (read().now ?? Date.now)();
|
|
824
895
|
const setState = (next) => {
|
|
825
896
|
if (next === state) return;
|
|
@@ -842,18 +913,31 @@ function createPasteMachine(options = {}) {
|
|
|
842
913
|
setState(PASTE_IDLE_STATE);
|
|
843
914
|
}, remaining);
|
|
844
915
|
};
|
|
916
|
+
const settleError = (error, source, retryCount) => {
|
|
917
|
+
setState({
|
|
918
|
+
status: "error",
|
|
919
|
+
error,
|
|
920
|
+
source,
|
|
921
|
+
retryCount
|
|
922
|
+
});
|
|
923
|
+
invoke(read().onError, error);
|
|
924
|
+
};
|
|
845
925
|
/** Runs one read. `produce` is invoked synchronously so the user gesture is preserved. */
|
|
846
|
-
const run = (produce) => {
|
|
926
|
+
const run = (produce, source, retryCount) => {
|
|
847
927
|
clearResetTimer();
|
|
848
928
|
const current = ++generation;
|
|
929
|
+
const superseded = operations.abort();
|
|
930
|
+
const context = operations.start();
|
|
849
931
|
setState(PASTE_READING_STATE);
|
|
932
|
+
if (superseded) invoke(read().onCancel, "superseded");
|
|
850
933
|
let pending;
|
|
851
934
|
try {
|
|
852
|
-
pending = produce();
|
|
935
|
+
pending = produce(context);
|
|
853
936
|
} catch (cause) {
|
|
854
937
|
pending = Promise.reject(cause);
|
|
855
938
|
}
|
|
856
|
-
return pending.then((result) => {
|
|
939
|
+
return pending.then(completePasteResult).then((result) => {
|
|
940
|
+
operations.end(context);
|
|
857
941
|
if (current === generation) {
|
|
858
942
|
const readState = {
|
|
859
943
|
status: "read",
|
|
@@ -869,26 +953,30 @@ function createPasteMachine(options = {}) {
|
|
|
869
953
|
result
|
|
870
954
|
};
|
|
871
955
|
}, (cause) => {
|
|
956
|
+
operations.end(context);
|
|
957
|
+
if (context.signal.aborted) return IGNORED_CANCELLED;
|
|
872
958
|
const error = toCopyError(cause, "read");
|
|
873
|
-
|
|
874
|
-
setState({
|
|
875
|
-
status: "error",
|
|
876
|
-
error
|
|
877
|
-
});
|
|
878
|
-
invoke(read().onError, error);
|
|
879
|
-
}
|
|
959
|
+
settleError(error, source, retryCount);
|
|
880
960
|
return {
|
|
881
961
|
status: "error",
|
|
882
962
|
error
|
|
883
963
|
};
|
|
884
964
|
});
|
|
885
965
|
};
|
|
886
|
-
const
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
966
|
+
const readClipboard = (retryCount) => run((context) => (read().adapter ?? getDefaultAdapter()).read(resolvePasteReadOptions(read()), context), "clipboard", retryCount);
|
|
967
|
+
const paste = () => state.status === "reading" ? Promise.resolve(IGNORED_IN_FLIGHT) : readClipboard(0);
|
|
968
|
+
const retry = () => {
|
|
969
|
+
if (state.status !== "error") return Promise.resolve(IGNORED_NOTHING);
|
|
970
|
+
if (state.source !== "clipboard" || !isRetryableError(state.error)) return Promise.resolve(IGNORED_NOT_RETRYABLE);
|
|
971
|
+
if (state.retryCount >= resolveMaxRetries(read().maxRetries)) {
|
|
972
|
+
const error = createCopyError("max-retries-exceeded", `Paste failed after ${String(state.retryCount)} retries.`, state.error);
|
|
973
|
+
settleError(error, "clipboard", state.retryCount);
|
|
974
|
+
return Promise.resolve({
|
|
975
|
+
status: "error",
|
|
976
|
+
error
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
return readClipboard(state.retryCount + 1);
|
|
892
980
|
};
|
|
893
981
|
const pasteEvent = (event) => {
|
|
894
982
|
const data = event.clipboardData;
|
|
@@ -900,12 +988,10 @@ function createPasteMachine(options = {}) {
|
|
|
900
988
|
} catch (cause) {
|
|
901
989
|
const error = toCopyError(cause, "read");
|
|
902
990
|
++generation;
|
|
991
|
+
const superseded = operations.abort();
|
|
903
992
|
clearResetTimer();
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
error
|
|
907
|
-
});
|
|
908
|
-
invoke(current.onError, error);
|
|
993
|
+
settleError(error, "event", 0);
|
|
994
|
+
if (superseded) invoke(current.onCancel, "superseded");
|
|
909
995
|
return Promise.resolve({
|
|
910
996
|
status: "error",
|
|
911
997
|
error
|
|
@@ -917,19 +1003,25 @@ function createPasteMachine(options = {}) {
|
|
|
917
1003
|
return run(() => {
|
|
918
1004
|
enforcePasteLimits(entries, resolved);
|
|
919
1005
|
return finalizePaste(entries, "event", resolved);
|
|
920
|
-
});
|
|
1006
|
+
}, "event", 0);
|
|
921
1007
|
};
|
|
922
1008
|
const reset = () => {
|
|
923
1009
|
++generation;
|
|
1010
|
+
const cancelled = operations.abort();
|
|
924
1011
|
clearResetTimer();
|
|
1012
|
+
const wasIdle = state.status === "idle";
|
|
925
1013
|
setState(PASTE_IDLE_STATE);
|
|
1014
|
+
if (cancelled) invoke(read().onCancel, "reset");
|
|
1015
|
+
if (!wasIdle) invoke(read().onReset);
|
|
926
1016
|
};
|
|
927
1017
|
const connect = () => {
|
|
928
1018
|
if (state.status === "read") scheduleReset(state);
|
|
929
1019
|
return () => {
|
|
930
1020
|
++generation;
|
|
1021
|
+
const cancelled = operations.abort();
|
|
931
1022
|
clearResetTimer();
|
|
932
1023
|
if (state.status === "reading") setState(PASTE_IDLE_STATE);
|
|
1024
|
+
if (cancelled) invoke(read().onCancel, "disconnect");
|
|
933
1025
|
};
|
|
934
1026
|
};
|
|
935
1027
|
return {
|
|
@@ -942,6 +1034,7 @@ function createPasteMachine(options = {}) {
|
|
|
942
1034
|
},
|
|
943
1035
|
paste,
|
|
944
1036
|
pasteEvent,
|
|
1037
|
+
retry,
|
|
945
1038
|
reset,
|
|
946
1039
|
connect
|
|
947
1040
|
};
|
|
@@ -957,6 +1050,7 @@ exports.IDLE_STATE = IDLE_STATE;
|
|
|
957
1050
|
exports.IMAGE_MIME_TYPE = IMAGE_MIME_TYPE;
|
|
958
1051
|
exports.PASTE_IDLE_STATE = PASTE_IDLE_STATE;
|
|
959
1052
|
exports.SVG_MIME_TYPE = SVG_MIME_TYPE;
|
|
1053
|
+
exports.canRetryPasteState = canRetryPasteState;
|
|
960
1054
|
exports.canRetryState = canRetryState;
|
|
961
1055
|
exports.copyFailure = copyFailure;
|
|
962
1056
|
exports.createBrowserClipboardAdapter = createBrowserClipboardAdapter;
|