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