react-smart-copy 1.3.0 → 1.4.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 +48 -13
- package/dist/capture.cjs.map +1 -1
- package/dist/capture.js.map +1 -1
- package/dist/core.cjs +47 -11
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +115 -85
- package/dist/core.d.ts +115 -85
- package/dist/core.js +47 -12
- package/dist/core.js.map +1 -1
- package/dist/index.cjs +129 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +147 -90
- package/dist/index.d.ts +147 -90
- package/dist/index.js +129 -20
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -103,9 +103,19 @@ const PASTE_DESCRIPTIONS = {
|
|
|
103
103
|
function describeCopyError(error) {
|
|
104
104
|
return COPY_DESCRIPTIONS[error.type];
|
|
105
105
|
}
|
|
106
|
-
/**
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
/** Drops can only fail in these ways; the rest of the paste wording would mention the clipboard. */
|
|
107
|
+
const DROP_DESCRIPTIONS = {
|
|
108
|
+
"invalid-payload": "The dropped content couldn't be read.",
|
|
109
|
+
"no-content": "Nothing that was dropped can be used here.",
|
|
110
|
+
"too-large": "The dropped content is too large.",
|
|
111
|
+
unknown: "Couldn't read the dropped content. Try again."
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Default English, user-facing sentence for a paste error. Pass the error
|
|
115
|
+
* state's `source` to word drag-and-drop failures as drops. Replace for i18n.
|
|
116
|
+
*/
|
|
117
|
+
function describePasteError(error, source) {
|
|
118
|
+
return (source === "drop" ? DROP_DESCRIPTIONS[error.type] : void 0) ?? PASTE_DESCRIPTIONS[error.type];
|
|
109
119
|
}
|
|
110
120
|
//#endregion
|
|
111
121
|
//#region src/core/payload.ts
|
|
@@ -692,6 +702,27 @@ function enforcePasteLimits(entries, options) {
|
|
|
692
702
|
for (const entry of entries) budget.charge(sizeOf(entry.data));
|
|
693
703
|
}
|
|
694
704
|
/**
|
|
705
|
+
* Whether a drag in progress could drop anything `accept`s. Uses only what
|
|
706
|
+
* browsers expose before the drop (item kinds and types, never the data).
|
|
707
|
+
* A file whose type is not known yet (some browsers hide it while dragging)
|
|
708
|
+
* counts as a maybe: the drop itself is checked against `accept`.
|
|
709
|
+
*/
|
|
710
|
+
function canAcceptDrag(data, options) {
|
|
711
|
+
const items = data.items ?? Array.from(data.types, (type) => type === "Files" ? {
|
|
712
|
+
kind: "file",
|
|
713
|
+
type: ""
|
|
714
|
+
} : {
|
|
715
|
+
kind: "string",
|
|
716
|
+
type
|
|
717
|
+
});
|
|
718
|
+
return Array.from(items).some(({ kind, type: raw }) => {
|
|
719
|
+
const file = kind === "file";
|
|
720
|
+
const type = normalizeMimeType(raw);
|
|
721
|
+
if (type === null) return file;
|
|
722
|
+
return (file || isTextualMimeType(type)) && isAccepted(type, options);
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
695
726
|
* Synchronously copies the accepted parts of a paste event's `DataTransfer`.
|
|
696
727
|
* It must run during the event: browsers empty the object afterwards.
|
|
697
728
|
*/
|
|
@@ -789,7 +820,7 @@ async function finalizePaste(entries, source, options) {
|
|
|
789
820
|
data
|
|
790
821
|
});
|
|
791
822
|
}
|
|
792
|
-
if (items.length === 0) throw copyFailure("no-content", "The
|
|
823
|
+
if (items.length === 0) throw copyFailure("no-content", "The content holds nothing that matches `accept`.");
|
|
793
824
|
return {
|
|
794
825
|
source,
|
|
795
826
|
items,
|
|
@@ -981,10 +1012,11 @@ function createPasteMachine(options = {}) {
|
|
|
981
1012
|
}
|
|
982
1013
|
return readClipboard(state.retryCount + 1);
|
|
983
1014
|
};
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
if (!data) return Promise.resolve(IGNORED_NO_CONTENT);
|
|
1015
|
+
/** Shared by paste and drop events: their `DataTransfer` has the same shape and lifetime. */
|
|
1016
|
+
const transfer = (data, event, source) => {
|
|
987
1017
|
const current = read();
|
|
1018
|
+
if (source === "drop" && current.preventDefault !== false) event.preventDefault();
|
|
1019
|
+
if (!data) return Promise.resolve(IGNORED_NO_CONTENT);
|
|
988
1020
|
let resolved;
|
|
989
1021
|
try {
|
|
990
1022
|
resolved = resolvePasteReadOptions(current);
|
|
@@ -993,7 +1025,7 @@ function createPasteMachine(options = {}) {
|
|
|
993
1025
|
++generation;
|
|
994
1026
|
const superseded = operations.abort();
|
|
995
1027
|
clearResetTimer();
|
|
996
|
-
settleError(error,
|
|
1028
|
+
settleError(error, source, 0);
|
|
997
1029
|
if (superseded) invoke(current.onCancel, "superseded");
|
|
998
1030
|
return Promise.resolve({
|
|
999
1031
|
status: "error",
|
|
@@ -1002,12 +1034,14 @@ function createPasteMachine(options = {}) {
|
|
|
1002
1034
|
}
|
|
1003
1035
|
const entries = snapshotDataTransfer(data, resolved);
|
|
1004
1036
|
if (entries.length === 0) return Promise.resolve(IGNORED_NO_CONTENT);
|
|
1005
|
-
if (current.preventDefault !== false) event.preventDefault();
|
|
1037
|
+
if (source === "event" && current.preventDefault !== false) event.preventDefault();
|
|
1006
1038
|
return run(() => {
|
|
1007
1039
|
enforcePasteLimits(entries, resolved);
|
|
1008
|
-
return finalizePaste(entries,
|
|
1009
|
-
},
|
|
1040
|
+
return finalizePaste(entries, source, resolved);
|
|
1041
|
+
}, source, 0);
|
|
1010
1042
|
};
|
|
1043
|
+
const pasteEvent = (event) => transfer(event.clipboardData, event, "event");
|
|
1044
|
+
const dropEvent = (event) => transfer(event.dataTransfer, event, "drop");
|
|
1011
1045
|
const reset = () => {
|
|
1012
1046
|
++generation;
|
|
1013
1047
|
const cancelled = operations.abort();
|
|
@@ -1037,6 +1071,7 @@ function createPasteMachine(options = {}) {
|
|
|
1037
1071
|
},
|
|
1038
1072
|
paste,
|
|
1039
1073
|
pasteEvent,
|
|
1074
|
+
dropEvent,
|
|
1040
1075
|
retry,
|
|
1041
1076
|
reset,
|
|
1042
1077
|
connect
|
|
@@ -1501,10 +1536,21 @@ const CopyField = {
|
|
|
1501
1536
|
//#endregion
|
|
1502
1537
|
//#region src/react/usePaste.ts
|
|
1503
1538
|
const getServerSnapshot = () => PASTE_IDLE_STATE;
|
|
1539
|
+
const DRAG_END_EVENTS = ["drop", "dragend"];
|
|
1540
|
+
/** A text field or editable region nested inside the drop target, which should keep its native drop. */
|
|
1541
|
+
const isNestedEditable = (event) => {
|
|
1542
|
+
const { target, currentTarget } = event;
|
|
1543
|
+
if (target === currentTarget || !(target instanceof HTMLElement)) return false;
|
|
1544
|
+
if (target.tagName === "INPUT" || target.tagName === "TEXTAREA") return true;
|
|
1545
|
+
return target.isContentEditable && !(currentTarget instanceof HTMLElement && currentTarget.isContentEditable);
|
|
1546
|
+
};
|
|
1547
|
+
/** The drop effect the drag source allows, preferring copy: an effect it disallows cancels the drop. */
|
|
1548
|
+
const dropEffectFor = (allowed) => /^(all|uninitialized|copy)/.test(allowed) ? "copy" : /move/i.test(allowed) ? "move" : allowed === "link" ? "link" : "none";
|
|
1504
1549
|
/**
|
|
1505
1550
|
* Paste state machine bound to a component. Supports a "Paste" button
|
|
1506
|
-
* (`paste()`, may prompt for permission)
|
|
1507
|
-
*
|
|
1551
|
+
* (`paste()`, may prompt for permission), keyboard paste (`targetProps` or
|
|
1552
|
+
* `listenOnDocument`, no prompt) and drag-and-drop (`dropTargetProps`).
|
|
1553
|
+
* Options may change on every render.
|
|
1508
1554
|
*
|
|
1509
1555
|
* Everything in the result is untrusted user input.
|
|
1510
1556
|
*/
|
|
@@ -1531,6 +1577,48 @@ function usePaste(options = {}) {
|
|
|
1531
1577
|
const targetProps = (0, react.useMemo)(() => ({ onPaste: (event) => {
|
|
1532
1578
|
machine.pasteEvent(event);
|
|
1533
1579
|
} }), [machine]);
|
|
1580
|
+
const [isDragOver, setIsDragOver] = (0, react.useState)(false);
|
|
1581
|
+
const dragDepth = (0, react.useRef)(0);
|
|
1582
|
+
const [endDrag] = (0, react.useState)(() => () => {
|
|
1583
|
+
dragDepth.current = 0;
|
|
1584
|
+
setIsDragOver(false);
|
|
1585
|
+
});
|
|
1586
|
+
const dropTargetProps = (0, react.useMemo)(() => {
|
|
1587
|
+
const accepts = (data) => {
|
|
1588
|
+
try {
|
|
1589
|
+
return canAcceptDrag(data, resolvePasteReadOptions(optionsRef.current));
|
|
1590
|
+
} catch {
|
|
1591
|
+
return true;
|
|
1592
|
+
}
|
|
1593
|
+
};
|
|
1594
|
+
return {
|
|
1595
|
+
onDragEnter: (event) => {
|
|
1596
|
+
dragDepth.current += 1;
|
|
1597
|
+
if (accepts(event.dataTransfer)) setIsDragOver(true);
|
|
1598
|
+
},
|
|
1599
|
+
onDragOver: (event) => {
|
|
1600
|
+
if (isNestedEditable(event)) return;
|
|
1601
|
+
event.preventDefault();
|
|
1602
|
+
event.dataTransfer.dropEffect = accepts(event.dataTransfer) ? dropEffectFor(event.dataTransfer.effectAllowed) : "none";
|
|
1603
|
+
},
|
|
1604
|
+
onDragLeave: (event) => {
|
|
1605
|
+
const next = event.relatedTarget;
|
|
1606
|
+
dragDepth.current -= 1;
|
|
1607
|
+
if (dragDepth.current <= 0 || next instanceof Node && !event.currentTarget.contains(next)) endDrag();
|
|
1608
|
+
},
|
|
1609
|
+
onDrop: (event) => {
|
|
1610
|
+
endDrag();
|
|
1611
|
+
if (!isNestedEditable(event)) machine.dropEvent(event);
|
|
1612
|
+
}
|
|
1613
|
+
};
|
|
1614
|
+
}, [machine, endDrag]);
|
|
1615
|
+
(0, react.useEffect)(() => {
|
|
1616
|
+
if (!isDragOver) return void 0;
|
|
1617
|
+
for (const type of DRAG_END_EVENTS) window.addEventListener(type, endDrag, true);
|
|
1618
|
+
return () => {
|
|
1619
|
+
for (const type of DRAG_END_EVENTS) window.removeEventListener(type, endDrag, true);
|
|
1620
|
+
};
|
|
1621
|
+
}, [isDragOver, endDrag]);
|
|
1534
1622
|
return (0, react.useMemo)(() => ({
|
|
1535
1623
|
state,
|
|
1536
1624
|
status: state.status,
|
|
@@ -1539,14 +1627,19 @@ function usePaste(options = {}) {
|
|
|
1539
1627
|
canRetry,
|
|
1540
1628
|
paste: machine.paste,
|
|
1541
1629
|
pasteEvent: machine.pasteEvent,
|
|
1630
|
+
dropEvent: machine.dropEvent,
|
|
1542
1631
|
retry: machine.retry,
|
|
1543
1632
|
reset: machine.reset,
|
|
1544
|
-
targetProps
|
|
1633
|
+
targetProps,
|
|
1634
|
+
dropTargetProps,
|
|
1635
|
+
isDragOver
|
|
1545
1636
|
}), [
|
|
1546
1637
|
state,
|
|
1547
1638
|
canRetry,
|
|
1548
1639
|
machine,
|
|
1549
|
-
targetProps
|
|
1640
|
+
targetProps,
|
|
1641
|
+
dropTargetProps,
|
|
1642
|
+
isDragOver
|
|
1550
1643
|
]);
|
|
1551
1644
|
}
|
|
1552
1645
|
//#endregion
|
|
@@ -1602,6 +1695,7 @@ function usePasteDisplayStatus(status, options = {}) {
|
|
|
1602
1695
|
//#region src/react/PasteField.tsx
|
|
1603
1696
|
const defaultPasteFieldMessages = {
|
|
1604
1697
|
pasted: "Pasted from clipboard",
|
|
1698
|
+
dropped: "Dropped content added",
|
|
1605
1699
|
error: describePasteError,
|
|
1606
1700
|
triggerLabel: (label) => `Paste ${label}`,
|
|
1607
1701
|
zoneLabel: (label) => `Paste area for ${label}`
|
|
@@ -1625,6 +1719,7 @@ const Root = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldRoot({ lab
|
|
|
1625
1719
|
const zoneId = `${baseId}-zone`;
|
|
1626
1720
|
const messages = (0, react.useMemo)(() => ({
|
|
1627
1721
|
...defaultPasteFieldMessages,
|
|
1722
|
+
...messageOverrides?.pasted === void 0 ? {} : { dropped: messageOverrides.pasted },
|
|
1628
1723
|
...messageOverrides
|
|
1629
1724
|
}), [messageOverrides]);
|
|
1630
1725
|
const { paste: machinePaste } = pasteState;
|
|
@@ -1653,7 +1748,11 @@ const Root = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldRoot({ lab
|
|
|
1653
1748
|
doPaste
|
|
1654
1749
|
]);
|
|
1655
1750
|
const { state } = pasteState;
|
|
1656
|
-
|
|
1751
|
+
let announcement = "";
|
|
1752
|
+
if (state.status === "read") {
|
|
1753
|
+
const message = (state.result.source === "drop" ? messages.dropped : void 0) ?? messages.pasted;
|
|
1754
|
+
announcement = typeof message === "function" ? message(state.result) : message;
|
|
1755
|
+
} else if (state.status === "error") announcement = messages.error(state.error, state.source);
|
|
1657
1756
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(PasteFieldContext.Provider, {
|
|
1658
1757
|
value: context,
|
|
1659
1758
|
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
@@ -1702,15 +1801,23 @@ const Status = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldStatus({
|
|
|
1702
1801
|
});
|
|
1703
1802
|
/**
|
|
1704
1803
|
* A focusable paste target. Accepts keyboard paste (Ctrl/⌘+V) without a
|
|
1705
|
-
* permission prompt and
|
|
1804
|
+
* permission prompt and, unless `droppable={false}`, drag-and-drop. Renders
|
|
1805
|
+
* `data-zone` and `data-drag-over` for styling.
|
|
1706
1806
|
*
|
|
1707
1807
|
* Accessible name: `aria-label` prop, else `messages.zoneLabel(label)`
|
|
1708
1808
|
* ("Paste area for Notes"). Pass `aria-labelledby` explicitly to name it from
|
|
1709
1809
|
* another element instead.
|
|
1710
1810
|
*/
|
|
1711
|
-
const Zone = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldZone({ children, focusable = true, onPaste, "aria-label": ariaLabel, tabIndex, ...rest }, ref) {
|
|
1811
|
+
const Zone = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldZone({ children, focusable = true, droppable = true, onPaste, onDragEnter, onDragOver, onDragLeave, onDrop, "aria-label": ariaLabel, tabIndex, ...rest }, ref) {
|
|
1712
1812
|
const field = usePasteField();
|
|
1713
|
-
const { targetProps } = field;
|
|
1813
|
+
const { targetProps, dropTargetProps } = field;
|
|
1814
|
+
const drop = {
|
|
1815
|
+
onDragEnter,
|
|
1816
|
+
onDragOver,
|
|
1817
|
+
onDragLeave,
|
|
1818
|
+
onDrop
|
|
1819
|
+
};
|
|
1820
|
+
if (droppable) for (const key of Object.keys(dropTargetProps)) drop[key] = composeEventHandlers(drop[key], dropTargetProps[key]);
|
|
1714
1821
|
const safeAriaLabel = (ariaLabel ?? field.messages.zoneLabel(field.label)) || defaultPasteFieldMessages.zoneLabel(field.label);
|
|
1715
1822
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
1716
1823
|
"aria-keyshortcuts": "Control+V Meta+V",
|
|
@@ -1722,8 +1829,10 @@ const Zone = /* @__PURE__ */ (0, react.forwardRef)(function PasteFieldZone({ chi
|
|
|
1722
1829
|
"data-zone": "",
|
|
1723
1830
|
"data-state": field.status,
|
|
1724
1831
|
"data-display-state": field.displayStatus,
|
|
1832
|
+
"data-drag-over": droppable && field.isDragOver ? "" : void 0,
|
|
1725
1833
|
tabIndex: tabIndex ?? (focusable ? 0 : void 0),
|
|
1726
1834
|
onPaste: composeEventHandlers(onPaste, targetProps.onPaste),
|
|
1835
|
+
...drop,
|
|
1727
1836
|
children
|
|
1728
1837
|
});
|
|
1729
1838
|
});
|
|
@@ -1865,6 +1974,7 @@ exports.LiveRegion = LiveRegion;
|
|
|
1865
1974
|
exports.PASTE_IDLE_STATE = PASTE_IDLE_STATE;
|
|
1866
1975
|
exports.PasteField = PasteField;
|
|
1867
1976
|
exports.SVG_MIME_TYPE = SVG_MIME_TYPE;
|
|
1977
|
+
exports.canAcceptDrag = canAcceptDrag;
|
|
1868
1978
|
exports.canRetryPasteState = canRetryPasteState;
|
|
1869
1979
|
exports.canRetryState = canRetryState;
|
|
1870
1980
|
exports.copyFailure = copyFailure;
|