react-smart-copy 1.8.1 → 1.10.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/dist/index.cjs CHANGED
@@ -200,19 +200,43 @@ function normalizeMimeType(value) {
200
200
  const base = (separator === -1 ? value : value.slice(0, separator)).trim().toLowerCase();
201
201
  return MIME_SHAPE.test(base) ? base : null;
202
202
  }
203
+ /** Prefix of the async Clipboard API's web custom formats (`web application/vnd.acme+json`). */
204
+ const WEB_CUSTOM_FORMAT_PREFIX = "web ";
205
+ /** Whether a clipboard type is a web custom format (`web type/subtype`). */
206
+ function isWebCustomFormat(type) {
207
+ return type.slice(0, 4).toLowerCase() === WEB_CUSTOM_FORMAT_PREFIX;
208
+ }
209
+ /**
210
+ * Like {@link normalizeMimeType}, but also accepts web custom formats
211
+ * (`web type/subtype`), returned as `web ` + the normalised MIME type.
212
+ */
213
+ function normalizeClipboardType(value) {
214
+ if (!isWebCustomFormat(value)) return normalizeMimeType(value);
215
+ const inner = normalizeMimeType(value.slice(4));
216
+ return inner === null ? null : WEB_CUSTOM_FORMAT_PREFIX + inner;
217
+ }
218
+ /**
219
+ * Types a wildcard never matches, so accepting them must be explicit:
220
+ * `image/svg+xml` (scriptable XML), web custom formats and structured `+json` types
221
+ * (application-specific data that only the app that defined them should read).
222
+ */
223
+ function requiresExactMatch(mimeType) {
224
+ return mimeType === "image/svg+xml" || isWebCustomFormat(mimeType) || mimeType.endsWith("+json");
225
+ }
203
226
  /**
204
227
  * Does `mimeType` (already normalised) match `pattern` (`type/subtype`, `type/*` or `*\/*`)?
205
- * Wildcards never match `image/svg+xml`: accepting SVG must be explicit.
228
+ * Wildcards never match `image/svg+xml`, web custom formats or `+json` types: those must be listed exactly.
206
229
  */
207
230
  function matchesMimePattern(mimeType, pattern) {
208
231
  if (pattern === mimeType) return true;
209
- if (mimeType === "image/svg+xml") return false;
232
+ if (requiresExactMatch(mimeType)) return false;
210
233
  if (pattern === "*/*") return true;
211
234
  return pattern.endsWith("/*") && mimeType.startsWith(pattern.slice(0, -1));
212
235
  }
213
236
  /** Types whose clipboard data is text and is therefore surfaced as a `string`. */
214
237
  function isTextualMimeType(mimeType) {
215
- return mimeType.startsWith("text/") || mimeType === "image/svg+xml" || mimeType === "application/json" || mimeType === "application/xml";
238
+ const type = isWebCustomFormat(mimeType) ? mimeType.slice(4) : mimeType;
239
+ return type.startsWith("text/") || type === "image/svg+xml" || type === "application/json" || type === "application/xml" || type.endsWith("+json");
216
240
  }
217
241
  const SNIFF_BYTES = 16;
218
242
  function startsWith(bytes, signature, offset = 0) {
@@ -328,10 +352,14 @@ async function writeToClipboard(payload, env, degradeHtmlToText, context) {
328
352
  const rich = getRichClipboard(env, clipboard);
329
353
  if (!rich) throw fail("unsupported-format", "Multi-format copy requires ClipboardItem and navigator.clipboard.write().");
330
354
  const record = Object.create(null);
331
- for (const { mimeType, data } of payload.items) {
355
+ let written = 0;
356
+ for (const { mimeType, data, optional } of payload.items) {
357
+ if (optional === true && !(typeof rich.Item.supports === "function" && rich.Item.supports(mimeType))) continue;
332
358
  if (!isTypeSupported(rich.Item, mimeType)) throw fail("unsupported-format", `This browser cannot write "${mimeType}" to the clipboard.`);
333
359
  record[mimeType] = typeof data === "string" ? new Blob([data], { type: mimeType }) : data;
360
+ written += 1;
334
361
  }
362
+ if (written === 0) throw fail("unsupported-format", "This browser can write none of the payload types.");
335
363
  return rich.write([new rich.Item(record)]);
336
364
  }
337
365
  }
@@ -408,6 +436,17 @@ function createOperations() {
408
436
  };
409
437
  }
410
438
  //#endregion
439
+ //#region src/core/source-error.ts
440
+ /**
441
+ * The error for a user-supplied source (copy source, drag source, copy transform)
442
+ * that threw. A thrown `CopyFailure` keeps its classification, so a helper such as
443
+ * `tablePayload()` can report `too-large` instead of a generic `invalid-payload`.
444
+ * Internal: not part of the public API.
445
+ */
446
+ function sourceError(cause, message) {
447
+ return isCopyFailure(cause) ? cause.copyError : createCopyError("invalid-payload", message, cause);
448
+ }
449
+ //#endregion
411
450
  //#region src/core/copy-machine.ts
412
451
  const DEFAULT_RESET_AFTER_MS = 2e3;
413
452
  const IDLE_STATE = /* @__PURE__ */ Object.freeze({ status: "idle" });
@@ -510,7 +549,7 @@ function createCopyMachine(options = {}) {
510
549
  }
511
550
  payload = normalizePayload(resolved);
512
551
  } catch (cause) {
513
- const error = createCopyError("invalid-payload", "The copy source function threw.", cause);
552
+ const error = sourceError(cause, "The copy source function threw.");
514
553
  settleError(error, null, retryCount);
515
554
  return Promise.resolve({
516
555
  status: "error",
@@ -750,7 +789,8 @@ function prepare(decision) {
750
789
  if (typeof data !== "string") return unsupported$1(`Multi item "${mimeType}" must be a string during a copy event.`);
751
790
  entries.push([mimeType, data]);
752
791
  }
753
- return ok(entries);
792
+ const writable = entries.filter(([mimeType]) => !isWebCustomFormat(mimeType));
793
+ return writable.length > 0 ? ok(writable) : unsupported$1("Web custom formats need the async Clipboard API (useCopy).");
754
794
  }
755
795
  default: return unsupported$1(`"${String(fields["kind"])}" payloads cannot be written during a copy event; use useCopy() for images.`);
756
796
  }
@@ -800,7 +840,7 @@ function interceptCopyEvent(event, options) {
800
840
  try {
801
841
  decision = options.transform(selection);
802
842
  } catch (cause) {
803
- return fail(createCopyError("invalid-payload", "The copy transform threw.", cause));
843
+ return fail(sourceError(cause, "The copy transform threw."));
804
844
  }
805
845
  if (decision === null || decision === void 0) return {
806
846
  status: "passed",
@@ -986,7 +1026,8 @@ function prepareDragDecision(decision) {
986
1026
  data
987
1027
  });
988
1028
  }
989
- return ok(out);
1029
+ const writable = out.filter((item) => !isWebCustomFormat(item.mimeType));
1030
+ return writable.length > 0 ? ok(writable) : unsupported("Web custom formats need the async Clipboard API (useCopy).");
990
1031
  }
991
1032
  default: return unsupported(`"${String(fields["kind"])}" payloads cannot be used for drag-copy; use useCopy() for images.`);
992
1033
  }
@@ -1007,7 +1048,7 @@ function applyDragCopy(event, options) {
1007
1048
  } catch (cause) {
1008
1049
  return {
1009
1050
  status: "failed",
1010
- error: createCopyError("invalid-payload", "The drag source threw.", cause)
1051
+ error: sourceError(cause, "The drag source threw.")
1011
1052
  };
1012
1053
  }
1013
1054
  if (decision === null || decision === void 0) return {
@@ -1136,7 +1177,7 @@ function resolvePattern(entry) {
1136
1177
  const type = entry.slice(0, -2).toLowerCase();
1137
1178
  return TYPE_TOKEN.test(type) ? `${type}/*` : null;
1138
1179
  }
1139
- return normalizeMimeType(entry);
1180
+ return normalizeClipboardType(entry);
1140
1181
  }
1141
1182
  function resolveLimit(value, fallback, name) {
1142
1183
  if (value === void 0) return fallback;
@@ -1212,7 +1253,7 @@ function canAcceptDrag(data, options) {
1212
1253
  });
1213
1254
  return Array.from(items).some(({ kind, type: raw }) => {
1214
1255
  const file = kind === "file";
1215
- const type = normalizeMimeType(raw);
1256
+ const type = normalizeClipboardType(raw);
1216
1257
  if (type === null) return file;
1217
1258
  return (file || isTextualMimeType(type)) && isAccepted(type, options);
1218
1259
  });
@@ -1225,7 +1266,7 @@ function snapshotDataTransfer(data, options) {
1225
1266
  const entries = [];
1226
1267
  const seen = /* @__PURE__ */ new Set();
1227
1268
  for (const raw of Array.from(data.types)) {
1228
- const type = normalizeMimeType(raw);
1269
+ const type = normalizeClipboardType(raw);
1229
1270
  if (type === null || seen.has(type) || !isTextualMimeType(type) || !isAccepted(type, options)) continue;
1230
1271
  seen.add(type);
1231
1272
  const text = data.getData(raw);
@@ -1254,7 +1295,7 @@ async function collectClipboardItems(items, options, signal) {
1254
1295
  const entries = [];
1255
1296
  const seen = /* @__PURE__ */ new Set();
1256
1297
  for (const item of items) for (const raw of item.types) {
1257
- const type = normalizeMimeType(raw);
1298
+ const type = normalizeClipboardType(raw);
1258
1299
  if (type === null || seen.has(type) || !isAccepted(type, options)) continue;
1259
1300
  seen.add(type);
1260
1301
  let blob;
@@ -2601,6 +2642,7 @@ exports.LiveRegion = LiveRegion;
2601
2642
  exports.PASTE_IDLE_STATE = PASTE_IDLE_STATE;
2602
2643
  exports.PasteField = PasteField;
2603
2644
  exports.SVG_MIME_TYPE = SVG_MIME_TYPE;
2645
+ exports.WEB_CUSTOM_FORMAT_PREFIX = WEB_CUSTOM_FORMAT_PREFIX;
2604
2646
  exports.applyDragCopy = applyDragCopy;
2605
2647
  exports.canAcceptDrag = canAcceptDrag;
2606
2648
  exports.canRetryPasteState = canRetryPasteState;
@@ -2621,7 +2663,9 @@ exports.isBlob = isBlob;
2621
2663
  exports.isCopyFailure = isCopyFailure;
2622
2664
  exports.isRetryableError = isRetryableError;
2623
2665
  exports.isTextualMimeType = isTextualMimeType;
2666
+ exports.isWebCustomFormat = isWebCustomFormat;
2624
2667
  exports.matchesMimePattern = matchesMimePattern;
2668
+ exports.normalizeClipboardType = normalizeClipboardType;
2625
2669
  exports.normalizeMimeType = normalizeMimeType;
2626
2670
  exports.normalizePayload = normalizePayload;
2627
2671
  exports.readCopySelection = readCopySelection;