react-smart-copy 1.1.0 → 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 +91 -6
- 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 +66 -15
- package/dist/core.d.ts +66 -15
- package/dist/core.js +135 -81
- package/dist/core.js.map +1 -1
- package/dist/index.cjs +267 -165
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +94 -17
- package/dist/index.d.ts +94 -17
- package/dist/index.js +268 -167
- package/dist/index.js.map +1 -1
- package/package.json +9 -8
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,
|
|
@@ -1219,6 +1273,36 @@ function LiveRegion({ message, politeness = "polite", visuallyHidden = true, sty
|
|
|
1219
1273
|
});
|
|
1220
1274
|
}
|
|
1221
1275
|
//#endregion
|
|
1276
|
+
//#region src/react/TriggerLabel.tsx
|
|
1277
|
+
/**
|
|
1278
|
+
* Default trigger label shared by `CopyField` and `PasteField`: every state's
|
|
1279
|
+
* text is stacked in one grid cell, so the button is always as wide as its
|
|
1280
|
+
* longest label and never resizes (no layout shift). Only the active label is
|
|
1281
|
+
* visible and exposed to assistive tech; the crossfade is skipped when the
|
|
1282
|
+
* user prefers reduced motion.
|
|
1283
|
+
*/
|
|
1284
|
+
function TriggerLabel({ status, labels }) {
|
|
1285
|
+
const reduceMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
|
|
1286
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
1287
|
+
style: { display: "inline-grid" },
|
|
1288
|
+
children: Object.keys(labels).map((candidate) => {
|
|
1289
|
+
const active = candidate === status;
|
|
1290
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
1291
|
+
"data-trigger-label": candidate,
|
|
1292
|
+
"data-active": active ? "" : void 0,
|
|
1293
|
+
"aria-hidden": !active || void 0,
|
|
1294
|
+
style: {
|
|
1295
|
+
gridArea: "1 / 1",
|
|
1296
|
+
opacity: active ? 1 : 0,
|
|
1297
|
+
visibility: active ? "visible" : "hidden",
|
|
1298
|
+
...reduceMotion ? {} : { transition: "opacity 150ms ease, visibility 150ms" }
|
|
1299
|
+
},
|
|
1300
|
+
children: labels[candidate]
|
|
1301
|
+
}, candidate);
|
|
1302
|
+
})
|
|
1303
|
+
});
|
|
1304
|
+
}
|
|
1305
|
+
//#endregion
|
|
1222
1306
|
//#region src/react/CopyField.tsx
|
|
1223
1307
|
const defaultCopyFieldMessages = {
|
|
1224
1308
|
copied: "Copied to clipboard",
|
|
@@ -1334,38 +1418,6 @@ const DEFAULT_TRIGGER_TEXT$1 = {
|
|
|
1334
1418
|
copied: "Copied",
|
|
1335
1419
|
error: "Retry"
|
|
1336
1420
|
};
|
|
1337
|
-
const TRIGGER_STATUSES = [
|
|
1338
|
-
"idle",
|
|
1339
|
-
"copying",
|
|
1340
|
-
"copied",
|
|
1341
|
-
"error"
|
|
1342
|
-
];
|
|
1343
|
-
/**
|
|
1344
|
-
* Default label: every state's text is stacked in the same grid cell, so the
|
|
1345
|
-
* button is always as wide as its longest label and never resizes (no layout
|
|
1346
|
-
* shift). Only the active one is visible; the others crossfade out.
|
|
1347
|
-
*/
|
|
1348
|
-
function DefaultTriggerLabel$1({ status }) {
|
|
1349
|
-
const reduceMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
|
|
1350
|
-
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
1351
|
-
style: { display: "inline-grid" },
|
|
1352
|
-
children: TRIGGER_STATUSES.map((candidate) => {
|
|
1353
|
-
const active = candidate === status;
|
|
1354
|
-
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
1355
|
-
"data-trigger-label": candidate,
|
|
1356
|
-
"data-active": active ? "" : void 0,
|
|
1357
|
-
"aria-hidden": active ? void 0 : true,
|
|
1358
|
-
style: {
|
|
1359
|
-
gridArea: "1 / 1",
|
|
1360
|
-
opacity: active ? 1 : 0,
|
|
1361
|
-
visibility: active ? "visible" : "hidden",
|
|
1362
|
-
...reduceMotion ? {} : { transition: "opacity 150ms ease, visibility 150ms" }
|
|
1363
|
-
},
|
|
1364
|
-
children: DEFAULT_TRIGGER_TEXT$1[candidate]
|
|
1365
|
-
}, candidate);
|
|
1366
|
-
})
|
|
1367
|
-
});
|
|
1368
|
-
}
|
|
1369
1421
|
/**
|
|
1370
1422
|
* Headless compound component for a labelled, copyable value.
|
|
1371
1423
|
*
|
|
@@ -1387,20 +1439,23 @@ const CopyField = {
|
|
|
1387
1439
|
revealed: field.revealed,
|
|
1388
1440
|
canRetry: field.canRetry
|
|
1389
1441
|
};
|
|
1390
|
-
const
|
|
1391
|
-
const safeAriaLabel = computedAriaLabel.length > 0 ? computedAriaLabel : `Copy ${field.label}`;
|
|
1442
|
+
const safeAriaLabel = (ariaLabel ?? field.messages.triggerLabel(field.label)) || defaultCopyFieldMessages.triggerLabel(field.label);
|
|
1392
1443
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
|
|
1393
1444
|
...rest,
|
|
1394
1445
|
ref,
|
|
1395
1446
|
type: "button",
|
|
1396
1447
|
"aria-label": safeAriaLabel,
|
|
1448
|
+
"aria-busy": field.status === "copying" ? true : void 0,
|
|
1397
1449
|
"data-state": field.status,
|
|
1398
1450
|
"data-display-state": field.displayStatus,
|
|
1399
1451
|
"data-revealed": field.revealed ? "" : void 0,
|
|
1400
1452
|
onClick: composeEventHandlers(onClick, () => {
|
|
1401
1453
|
field.copyValue();
|
|
1402
1454
|
}),
|
|
1403
|
-
children: typeof children === "function" ? children(renderProps) : children ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(
|
|
1455
|
+
children: typeof children === "function" ? children(renderProps) : children ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TriggerLabel, {
|
|
1456
|
+
status: field.displayStatus,
|
|
1457
|
+
labels: DEFAULT_TRIGGER_TEXT$1
|
|
1458
|
+
})
|
|
1404
1459
|
});
|
|
1405
1460
|
})
|
|
1406
1461
|
};
|
|
@@ -1433,6 +1488,7 @@ function usePaste(options = {}) {
|
|
|
1433
1488
|
};
|
|
1434
1489
|
}, [listenOnDocument, machine]);
|
|
1435
1490
|
const state = (0, react.useSyncExternalStore)(machine.subscribe, machine.getSnapshot, getServerSnapshot);
|
|
1491
|
+
const canRetry = canRetryPasteState(state, options.maxRetries);
|
|
1436
1492
|
const targetProps = (0, react.useMemo)(() => ({ onPaste: (event) => {
|
|
1437
1493
|
machine.pasteEvent(event);
|
|
1438
1494
|
} }), [machine]);
|
|
@@ -1441,12 +1497,15 @@ function usePaste(options = {}) {
|
|
|
1441
1497
|
status: state.status,
|
|
1442
1498
|
result: state.status === "read" ? state.result : null,
|
|
1443
1499
|
error: state.status === "error" ? state.error : null,
|
|
1500
|
+
canRetry,
|
|
1444
1501
|
paste: machine.paste,
|
|
1445
1502
|
pasteEvent: machine.pasteEvent,
|
|
1503
|
+
retry: machine.retry,
|
|
1446
1504
|
reset: machine.reset,
|
|
1447
1505
|
targetProps
|
|
1448
1506
|
}), [
|
|
1449
1507
|
state,
|
|
1508
|
+
canRetry,
|
|
1450
1509
|
machine,
|
|
1451
1510
|
targetProps
|
|
1452
1511
|
]);
|
|
@@ -1603,22 +1662,24 @@ const Status = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldStatus({
|
|
|
1603
1662
|
});
|
|
1604
1663
|
});
|
|
1605
1664
|
/**
|
|
1606
|
-
* A
|
|
1607
|
-
*
|
|
1608
|
-
*
|
|
1665
|
+
* A focusable paste target. Accepts keyboard paste (Ctrl/⌘+V) without a
|
|
1666
|
+
* permission prompt and renders `data-zone` for styling.
|
|
1667
|
+
*
|
|
1668
|
+
* Accessible name: `aria-label` prop, else `messages.zoneLabel(label)`
|
|
1669
|
+
* ("Paste area for Notes"). Pass `aria-labelledby` explicitly to name it from
|
|
1670
|
+
* another element instead.
|
|
1609
1671
|
*/
|
|
1610
1672
|
const Zone = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldZone({ children, focusable = true, onPaste, "aria-label": ariaLabel, tabIndex, ...rest }, ref) {
|
|
1611
1673
|
const field = usePasteField();
|
|
1612
1674
|
const { targetProps } = field;
|
|
1613
|
-
const
|
|
1614
|
-
const safeAriaLabel = computedAriaLabel.length > 0 ? computedAriaLabel : `Paste area for ${field.label}`;
|
|
1675
|
+
const safeAriaLabel = (ariaLabel ?? field.messages.zoneLabel(field.label)) || defaultPasteFieldMessages.zoneLabel(field.label);
|
|
1615
1676
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
1677
|
+
"aria-keyshortcuts": "Control+V Meta+V",
|
|
1616
1678
|
...rest,
|
|
1617
1679
|
ref,
|
|
1618
1680
|
id: field.zoneId,
|
|
1619
1681
|
role: "region",
|
|
1620
1682
|
"aria-label": safeAriaLabel,
|
|
1621
|
-
"aria-labelledby": field.labelId,
|
|
1622
1683
|
"data-zone": "",
|
|
1623
1684
|
"data-state": field.status,
|
|
1624
1685
|
"data-display-state": field.displayStatus,
|
|
@@ -1633,32 +1694,56 @@ const DEFAULT_TRIGGER_TEXT = {
|
|
|
1633
1694
|
read: "Pasted",
|
|
1634
1695
|
error: "Retry"
|
|
1635
1696
|
};
|
|
1636
|
-
const
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
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
|
|
1659
1722
|
})
|
|
1660
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;
|
|
1661
1745
|
}
|
|
1746
|
+
const defaultImageAlt = (index, total) => `Pasted image ${String(index + 1)} of ${String(total)}`;
|
|
1662
1747
|
/**
|
|
1663
1748
|
* Headless compound component for a labelled, paste-ready field.
|
|
1664
1749
|
*
|
|
@@ -1673,6 +1758,7 @@ function DefaultTriggerLabel({ status }) {
|
|
|
1673
1758
|
* <PasteField.Label />
|
|
1674
1759
|
* <PasteField.Zone />
|
|
1675
1760
|
* <PasteField.Trigger />
|
|
1761
|
+
* <PasteField.Preview placeholder="Nothing pasted yet" />
|
|
1676
1762
|
* </PasteField.Root>
|
|
1677
1763
|
* ```
|
|
1678
1764
|
*/
|
|
@@ -1681,28 +1767,43 @@ const PasteField = {
|
|
|
1681
1767
|
Label,
|
|
1682
1768
|
Status,
|
|
1683
1769
|
Zone,
|
|
1684
|
-
Trigger
|
|
1685
|
-
|
|
1686
|
-
const
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
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", {
|
|
1695
1801
|
...rest,
|
|
1696
1802
|
ref,
|
|
1697
|
-
|
|
1698
|
-
"
|
|
1699
|
-
"data-
|
|
1700
|
-
|
|
1701
|
-
"data-revealed": field.revealed ? "" : void 0,
|
|
1702
|
-
onClick: composeEventHandlers(onClick, () => {
|
|
1703
|
-
field.doPaste();
|
|
1704
|
-
}),
|
|
1705
|
-
children: typeof children === "function" ? children(renderProps) : children ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultTriggerLabel, { status: field.displayStatus })
|
|
1803
|
+
"data-preview": "",
|
|
1804
|
+
"data-state": status,
|
|
1805
|
+
"data-empty": result ? void 0 : "",
|
|
1806
|
+
children: content
|
|
1706
1807
|
});
|
|
1707
1808
|
})
|
|
1708
1809
|
};
|
|
@@ -1725,6 +1826,7 @@ exports.LiveRegion = LiveRegion;
|
|
|
1725
1826
|
exports.PASTE_IDLE_STATE = PASTE_IDLE_STATE;
|
|
1726
1827
|
exports.PasteField = PasteField;
|
|
1727
1828
|
exports.SVG_MIME_TYPE = SVG_MIME_TYPE;
|
|
1829
|
+
exports.canRetryPasteState = canRetryPasteState;
|
|
1728
1830
|
exports.canRetryState = canRetryState;
|
|
1729
1831
|
exports.copyFailure = copyFailure;
|
|
1730
1832
|
exports.createBrowserClipboardAdapter = createBrowserClipboardAdapter;
|