react-smart-copy 1.9.0 → 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
  }
@@ -761,7 +789,8 @@ function prepare(decision) {
761
789
  if (typeof data !== "string") return unsupported$1(`Multi item "${mimeType}" must be a string during a copy event.`);
762
790
  entries.push([mimeType, data]);
763
791
  }
764
- 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).");
765
794
  }
766
795
  default: return unsupported$1(`"${String(fields["kind"])}" payloads cannot be written during a copy event; use useCopy() for images.`);
767
796
  }
@@ -997,7 +1026,8 @@ function prepareDragDecision(decision) {
997
1026
  data
998
1027
  });
999
1028
  }
1000
- 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).");
1001
1031
  }
1002
1032
  default: return unsupported(`"${String(fields["kind"])}" payloads cannot be used for drag-copy; use useCopy() for images.`);
1003
1033
  }
@@ -1147,7 +1177,7 @@ function resolvePattern(entry) {
1147
1177
  const type = entry.slice(0, -2).toLowerCase();
1148
1178
  return TYPE_TOKEN.test(type) ? `${type}/*` : null;
1149
1179
  }
1150
- return normalizeMimeType(entry);
1180
+ return normalizeClipboardType(entry);
1151
1181
  }
1152
1182
  function resolveLimit(value, fallback, name) {
1153
1183
  if (value === void 0) return fallback;
@@ -1223,7 +1253,7 @@ function canAcceptDrag(data, options) {
1223
1253
  });
1224
1254
  return Array.from(items).some(({ kind, type: raw }) => {
1225
1255
  const file = kind === "file";
1226
- const type = normalizeMimeType(raw);
1256
+ const type = normalizeClipboardType(raw);
1227
1257
  if (type === null) return file;
1228
1258
  return (file || isTextualMimeType(type)) && isAccepted(type, options);
1229
1259
  });
@@ -1236,7 +1266,7 @@ function snapshotDataTransfer(data, options) {
1236
1266
  const entries = [];
1237
1267
  const seen = /* @__PURE__ */ new Set();
1238
1268
  for (const raw of Array.from(data.types)) {
1239
- const type = normalizeMimeType(raw);
1269
+ const type = normalizeClipboardType(raw);
1240
1270
  if (type === null || seen.has(type) || !isTextualMimeType(type) || !isAccepted(type, options)) continue;
1241
1271
  seen.add(type);
1242
1272
  const text = data.getData(raw);
@@ -1265,7 +1295,7 @@ async function collectClipboardItems(items, options, signal) {
1265
1295
  const entries = [];
1266
1296
  const seen = /* @__PURE__ */ new Set();
1267
1297
  for (const item of items) for (const raw of item.types) {
1268
- const type = normalizeMimeType(raw);
1298
+ const type = normalizeClipboardType(raw);
1269
1299
  if (type === null || seen.has(type) || !isAccepted(type, options)) continue;
1270
1300
  seen.add(type);
1271
1301
  let blob;
@@ -2612,6 +2642,7 @@ exports.LiveRegion = LiveRegion;
2612
2642
  exports.PASTE_IDLE_STATE = PASTE_IDLE_STATE;
2613
2643
  exports.PasteField = PasteField;
2614
2644
  exports.SVG_MIME_TYPE = SVG_MIME_TYPE;
2645
+ exports.WEB_CUSTOM_FORMAT_PREFIX = WEB_CUSTOM_FORMAT_PREFIX;
2615
2646
  exports.applyDragCopy = applyDragCopy;
2616
2647
  exports.canAcceptDrag = canAcceptDrag;
2617
2648
  exports.canRetryPasteState = canRetryPasteState;
@@ -2632,7 +2663,9 @@ exports.isBlob = isBlob;
2632
2663
  exports.isCopyFailure = isCopyFailure;
2633
2664
  exports.isRetryableError = isRetryableError;
2634
2665
  exports.isTextualMimeType = isTextualMimeType;
2666
+ exports.isWebCustomFormat = isWebCustomFormat;
2635
2667
  exports.matchesMimePattern = matchesMimePattern;
2668
+ exports.normalizeClipboardType = normalizeClipboardType;
2636
2669
  exports.normalizeMimeType = normalizeMimeType;
2637
2670
  exports.normalizePayload = normalizePayload;
2638
2671
  exports.readCopySelection = readCopySelection;