react-smart-copy 1.11.0 → 1.13.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 +169 -3
- package/dist/code.cjs +1181 -0
- package/dist/code.cjs.map +1 -0
- package/dist/code.d.cts +292 -0
- package/dist/code.d.ts +292 -0
- package/dist/code.js +1169 -0
- package/dist/code.js.map +1 -0
- package/dist/image.cjs +1293 -0
- package/dist/image.cjs.map +1 -0
- package/dist/image.d.cts +217 -0
- package/dist/image.d.ts +217 -0
- package/dist/image.js +1282 -0
- package/dist/image.js.map +1 -0
- package/dist/markdown.cjs +59 -57
- package/dist/markdown.cjs.map +1 -1
- package/dist/markdown.d.cts +15 -9
- package/dist/markdown.d.ts +15 -9
- package/dist/markdown.js +59 -57
- package/dist/markdown.js.map +1 -1
- package/package.json +87 -18
package/README.md
CHANGED
|
@@ -28,6 +28,8 @@ ID PLT-29018 [Copy]
|
|
|
28
28
|
- **`react-smart-copy/table`**: copy rows that paste into Excel and Google Sheets as cells, and read pasted ranges back as typed records. Works with AG Grid, TanStack Table and any `<table>`
|
|
29
29
|
- **`react-smart-copy/structured`**: copy your own objects and paste them back as the same typed, validated objects in another tab or app, the way design tools do. Versioned, with migrations, and works with Zod, Valibot or ArkType
|
|
30
30
|
- **`react-smart-copy/markdown`**: paste from Google Docs, Word, Notion or any web page into a Markdown field and get clean Markdown, with undo intact: `<textarea onPaste={pasteAsMarkdown} />`
|
|
31
|
+
- **`react-smart-copy/code`**: copy code blocks the way developers expect (a terminal session copies as its commands, gutters and prompts stay behind, colours survive in Docs and Slack), paste code cleaned and indented, and catch Trojan Source, invisible identifiers and lookalike letters
|
|
32
|
+
- **`react-smart-copy/image`**: get pasted photos and screenshots ready to upload. It removes GPS location, camera data and hidden thumbnails without touching a pixel, and fits size and byte limits, re-encoding only when it has to
|
|
31
33
|
- **`react-smart-copy/capture`**: copy an `<svg>` or any piece of your UI as a PNG
|
|
32
34
|
- Every failure is a typed, classified error. Nothing silently no-ops
|
|
33
35
|
- Unstyled. Works with Tailwind, CSS modules, CSS-in-JS, shadcn, MUI, Ant Design
|
|
@@ -47,6 +49,8 @@ This package only promises what browsers actually support.
|
|
|
47
49
|
| Several formats at once | ✅ | `kind: 'multi'`; each MIME type is checked with `ClipboardItem.supports()` first |
|
|
48
50
|
| Your app's own data (typed) | ✅ | `react-smart-copy/structured`: a web custom format, a custom type and an HTML envelope, so every browser and paste path gets one |
|
|
49
51
|
| Rich text as Markdown | ✅ paste | `react-smart-copy/markdown`: CommonMark + GFM from Docs, Word, web pages and mail; plain-text pastes stay untouched |
|
|
52
|
+
| Code (blocks, sessions, diffs) | ✅ | `react-smart-copy/code`: plain text plus monospaced, highlighted HTML; commands without prompts or output; Trojan Source checks on paste |
|
|
53
|
+
| Pasted images, made safe | ✅ paste | `react-smart-copy/image`: removes location and camera metadata losslessly; resizes and compresses under a byte budget |
|
|
50
54
|
| SVG (icons, charts, logos) | ✅ as PNG | `svgToPngBlob()` from `react-smart-copy/capture` rasterises natively, no dependency. Raw SVG is not a clipboard image format |
|
|
51
55
|
| A section of your UI as an image | ✅ bring a rasteriser | Browsers have no "screenshot this element" API. `captureElement()` wraps one you choose (e.g. `modern-screenshot`) with limits and checks |
|
|
52
56
|
| Files (PDF, DOCX, ZIP) | ❌ | Not writable to the system clipboard by any browser API |
|
|
@@ -613,7 +617,7 @@ coercing schema (`z.coerce.date()`), or convert in `encode`.
|
|
|
613
617
|
`react-smart-copy/markdown` turns what people paste into comment boxes, editors and CMS
|
|
614
618
|
fields into clean Markdown: headings, bold and italic, links, lists (nested and task
|
|
615
619
|
lists), tables, code blocks and quotes, from Google Docs, Word, Notion, GitHub, web pages
|
|
616
|
-
and mail. No dependencies, about 7.
|
|
620
|
+
and mail. No dependencies, about 7.6 kB.
|
|
617
621
|
|
|
618
622
|
```tsx
|
|
619
623
|
import { pasteAsMarkdown } from "react-smart-copy/markdown";
|
|
@@ -675,6 +679,166 @@ documents with a CommonMark parser and compare the result character by character
|
|
|
675
679
|
| `hardBreak` | `"backslash"` | `"spaces"` for two trailing spaces |
|
|
676
680
|
| `codeAsText` (paste) | `true` | `false` turns code-only pastes into fenced code blocks |
|
|
677
681
|
|
|
682
|
+
## Copy and paste code
|
|
683
|
+
|
|
684
|
+
`react-smart-copy/code` is for the copy button on every code block and for fields people
|
|
685
|
+
paste code into. It also catches code that reads differently from how it runs. No
|
|
686
|
+
dependencies, about 7.4 kB (`inspectCode` alone is about 2.5 kB).
|
|
687
|
+
|
|
688
|
+
```tsx
|
|
689
|
+
import { useCopy } from "react-smart-copy";
|
|
690
|
+
import { codePayload } from "react-smart-copy/code";
|
|
691
|
+
|
|
692
|
+
const { copy, status } = useCopy();
|
|
693
|
+
<pre ref={preRef}>…</pre>
|
|
694
|
+
<button onClick={() => copy(() => codePayload(preRef.current!, { language: "tsx" }))}>Copy</button>
|
|
695
|
+
```
|
|
696
|
+
|
|
697
|
+
What gets copied is the code the reader means to run:
|
|
698
|
+
|
|
699
|
+
- **A terminal or REPL session copies as its commands.** `$ npm test` followed by its output copies as `npm test`. Prompts from `bash`/`zsh` (`$`, `user@host:~$`, `(venv) $`), PowerShell, `cmd`, Python, IPython and Node are recognised. Continued commands stay whole: a trailing `\`, a heredoc or an open quote. `prompt: "auto"` (the default) only acts when the first line starts with a prompt
|
|
700
|
+
- **A highlighted block copies without its chrome.** Line-number gutters (Prism, highlight.js, Rouge, react-syntax-highlighter and others), `user-select: none` and `aria-hidden` parts, hidden parts, buttons and anything with `data-copy-ignore` are left out. `ignore` takes a CSS selector for more
|
|
701
|
+
- **It pastes well everywhere.** Plain text for editors and terminals, plus monospaced HTML with the block's syntax colours for Google Docs, Word, Slack, Teams and mail. Both flavours always hold the same code
|
|
702
|
+
- **Diffs, line numbers, indentation.** `diff: "after"` (or `"before"`) copies one side of a unified diff. `lineNumbers: true` removes typed line numbers when they count up on every line. Shared indentation, trailing whitespace and blank edges are removed, and line endings are normalised (`lineEnding: "crlf"` for Windows)
|
|
703
|
+
|
|
704
|
+
For a code field, `pasteAsCode` is a ready paste handler:
|
|
705
|
+
|
|
706
|
+
```tsx
|
|
707
|
+
import { pasteAsCode } from "react-smart-copy/code";
|
|
708
|
+
|
|
709
|
+
<textarea onPaste={pasteAsCode} />
|
|
710
|
+
<textarea onPaste={(event) => pasteAsCode(event, { onPaste: (r) => !r.danger || confirm("Paste anyway?") })} />
|
|
711
|
+
```
|
|
712
|
+
|
|
713
|
+
It fixes what chat apps and word processors do to code (curly quotes, `—` for `--`,
|
|
714
|
+
no-break spaces, zero-width characters) and dedents the paste. Each line after the first is
|
|
715
|
+
indented to the cursor line, as code editors do. The paste is one step on the browser's
|
|
716
|
+
undo stack. `onPaste` sees every finding first and can cancel. For CodeMirror, Monaco or
|
|
717
|
+
`usePaste`, `pasteToCode(result)` returns the same `{ code, findings, fixed, remaining, danger }`.
|
|
718
|
+
|
|
719
|
+
**Security: code that runs differently from how it reads.** `inspectCode` reports, with
|
|
720
|
+
line, column and a label such as `U+202E RIGHT-TO-LEFT OVERRIDE`:
|
|
721
|
+
|
|
722
|
+
| Kind | Severity | What it catches |
|
|
723
|
+
| ---- | -------- | --------------- |
|
|
724
|
+
| `bidi-control` | danger | Trojan Source ([CVE-2021-42574](https://trojansource.codes/)): overrides and isolates that reorder how code is shown. Implicit marks are a warning |
|
|
725
|
+
| `invisible` | danger / warning | Hangul fillers that make invisible identifiers, and tag characters that hide ASCII (including hidden instructions in prompts) are danger. Zero-width spaces, joiners outside words and emoji, and runs of variation selectors that smuggle data are warnings |
|
|
726
|
+
| `mixed-script` | danger / warning | A Cyrillic or Greek letter inside a Latin word (`аdmin` with a Cyrillic `а`). A Latin lookalike is danger; another letter is a warning |
|
|
727
|
+
| `control` | warning | Control characters other than tab, line feed, carriage return and form feed, and Unicode line separators |
|
|
728
|
+
| `unusual-space` | warning | No-break, em, ideographic and similar spaces |
|
|
729
|
+
| `confusable-punctuation` | warning | Curly quotes, dashes, `…`, the Greek question mark that looks like `;`, full-width punctuation outside CJK text |
|
|
730
|
+
|
|
731
|
+
Joiners inside Arabic, Persian or Indic words and emoji, a single emoji variation
|
|
732
|
+
selector, and full-width punctuation in CJK comments are left alone. `cleanCode` applies
|
|
733
|
+
the safe replacement of every finding. It runs until nothing fixable is left, because
|
|
734
|
+
removing a zero-width space can expose a mixed word. It never guesses a letter that has no
|
|
735
|
+
Latin lookalike; those stay in `remaining`. Copying doesn't clean by default
|
|
736
|
+
(`clean: true` turns it on). Pasting does (`clean: false` turns it off).
|
|
737
|
+
|
|
738
|
+
```ts
|
|
739
|
+
import { cleanCode, inspectCode, normalizeCode } from "react-smart-copy/code";
|
|
740
|
+
|
|
741
|
+
inspectCode(source).danger; // true for Trojan Source, lookalikes, invisible identifiers
|
|
742
|
+
cleanCode("const s = “hi”;").code; // 'const s = "hi";'
|
|
743
|
+
normalizeCode("$ npm i\nadded 1 package"); // "npm i" (also works on the server)
|
|
744
|
+
```
|
|
745
|
+
|
|
746
|
+
Pasted code is never cut: past `maxLength` (2,000,000 characters) `pasteToCode` throws a
|
|
747
|
+
`too-large` error, and `pasteAsCode` leaves the paste to the browser. HTML is parsed
|
|
748
|
+
inertly and only its text is used.
|
|
749
|
+
|
|
750
|
+
| Option | Default | |
|
|
751
|
+
| ------ | ------- | - |
|
|
752
|
+
| `prompt` | `"auto"` copy, `false` paste | a style (`"shell"`, `"powershell"`, `"cmd"`, `"python"`, `"ipython"`, `"node"`), `{ command, continuation }` regexes, or `false` |
|
|
753
|
+
| `diff` | `false` | `"after"` or `"before"` |
|
|
754
|
+
| `lineNumbers` | `false` | remove typed line numbers that count up |
|
|
755
|
+
| `dedent` / `trimTrailingWhitespace` / `trimBlankLines` | `true` | |
|
|
756
|
+
| `lineEnding` / `finalNewline` | `"lf"` / `false` | |
|
|
757
|
+
| `clean` | `false` copy, `true` paste | `true`, `false` or a list of kinds |
|
|
758
|
+
| `html` / `language` (copy) | `true` / none | add the HTML flavour; `class="language-…"` on `<code>` |
|
|
759
|
+
| `indentToCursor` / `onPaste` (`pasteAsCode`) | `true` / none | |
|
|
760
|
+
|
|
761
|
+
## Pasted images: private and small
|
|
762
|
+
|
|
763
|
+
`react-smart-copy/image` gets pasted and dropped images ready to upload. A photo from a
|
|
764
|
+
phone usually carries the exact location it was taken, the device and its serial number,
|
|
765
|
+
and an embedded preview that can show what was cropped out. This module removes that
|
|
766
|
+
data, and makes the image fit your size and byte limits. No dependencies, about 7.3 kB
|
|
767
|
+
(about 4.8 kB for the server-safe `stripMetadata` and `readImageInfo`).
|
|
768
|
+
|
|
769
|
+
```ts
|
|
770
|
+
import { usePaste } from "react-smart-copy";
|
|
771
|
+
import { prepareImage } from "react-smart-copy/image";
|
|
772
|
+
|
|
773
|
+
usePaste({
|
|
774
|
+
accept: ["image"],
|
|
775
|
+
onPaste: async ({ imageFiles }) => {
|
|
776
|
+
const image = await prepareImage(imageFiles[0], { maxWidth: 2048, maxBytes: 1_000_000 });
|
|
777
|
+
upload(image.file); // a File named like the original, metadata removed
|
|
778
|
+
if (image.original.info.metadata.includes("gps")) notice("Location removed from the photo.");
|
|
779
|
+
},
|
|
780
|
+
});
|
|
781
|
+
```
|
|
782
|
+
|
|
783
|
+
**Lossless whenever it can be.** When only the metadata has to go, nothing is
|
|
784
|
+
re-encoded. JPEG, PNG, WebP and GIF are rewritten without their metadata, and every
|
|
785
|
+
pixel stays the same, so screenshots stay sharp and photos lose no quality. The image is
|
|
786
|
+
decoded and encoded again only when it has to change: to fit `maxWidth`, `maxHeight`,
|
|
787
|
+
`maxPixels` or `maxBytes`, to change its format, or because it is AVIF or HEIC.
|
|
788
|
+
|
|
789
|
+
**What is removed:** Exif (camera, lens, dates, serial numbers), the GPS location, embedded
|
|
790
|
+
thumbnails, XMP (edit history, authors), IPTC (captions, credits), comments, PNG text chunks,
|
|
791
|
+
multi-picture and depth images (MPF), content credentials (C2PA), and data appended after
|
|
792
|
+
the image (motion-photo videos). The colour profile is kept (`keepColorProfile: false`
|
|
793
|
+
removes it). The orientation is kept in a 26-byte Exif block holding nothing else, so a
|
|
794
|
+
photo still displays the right way up (`keepOrientation: false` removes it).
|
|
795
|
+
|
|
796
|
+
**Fits a byte budget.** For `maxBytes`, JPEG and WebP search for the highest quality that
|
|
797
|
+
fits (down to `minQuality`, 0.5 by default), and only then reduce the size. A screenshot
|
|
798
|
+
stays PNG when it fits. When it doesn't, `type: "auto"` turns it into WebP, or JPEG where
|
|
799
|
+
the browser can't encode WebP. If the limit can't be met, you get a `too-large` error,
|
|
800
|
+
never a file that is still too big. Large reductions are drawn in halving steps, so text
|
|
801
|
+
and edges stay clean.
|
|
802
|
+
|
|
803
|
+
**Safe with hostile files.** The format is read from the bytes, never from the file name
|
|
804
|
+
or the claimed type. Sizes come from the header and are checked against
|
|
805
|
+
`maxInputPixels` (100 million) *before* decoding, which stops decompression bombs. Every
|
|
806
|
+
parser read is bounded by its own box or chunk. Fuzz tests feed random and mutated files
|
|
807
|
+
to every parser. Animations keep every frame by default (`animated: "keep"`): a resize
|
|
808
|
+
they would need is a `too-large` error, `"first-frame"` makes a still image, and
|
|
809
|
+
`"reject"` refuses them. Every failure is a typed error: `too-large`,
|
|
810
|
+
`unsupported-format`, `unsupported` (re-encoding outside a browser), `timeout` or
|
|
811
|
+
`aborted` (`signal`).
|
|
812
|
+
|
|
813
|
+
```ts
|
|
814
|
+
import { readImageInfo, stripMetadata } from "react-smart-copy/image";
|
|
815
|
+
|
|
816
|
+
// Anywhere, including the server (Node 18+): no decoding, no canvas.
|
|
817
|
+
const info = await readImageInfo(file);
|
|
818
|
+
// { format: "jpeg", width: 4032, height: 3024, orientation: 6, animated: false,
|
|
819
|
+
// metadata: ["exif", "gps", "thumbnail", "icc", "other"] }
|
|
820
|
+
|
|
821
|
+
const { blob, removed } = await stripMetadata(file); // bit-for-bit the same picture
|
|
822
|
+
```
|
|
823
|
+
|
|
824
|
+
| Option (`prepareImage`) | Default | |
|
|
825
|
+
| ----------------------- | ------- | - |
|
|
826
|
+
| `maxWidth` / `maxHeight` | none | only ever makes images smaller |
|
|
827
|
+
| `maxPixels` | 16,777,216 | Safari's canvas limit |
|
|
828
|
+
| `maxBytes` | none | quality first, then size; `too-large` when it can't |
|
|
829
|
+
| `type` | `"auto"` | `"image/jpeg"`, `"image/png"` or `"image/webp"` |
|
|
830
|
+
| `quality` / `minQuality` | 0.85 / 0.5 | JPEG and WebP |
|
|
831
|
+
| `background` | `"#fff"` | under transparent pixels in JPEG |
|
|
832
|
+
| `animated` | `"keep"` | `"first-frame"` or `"reject"` |
|
|
833
|
+
| `keepColorProfile` | `true` | re-encoded images are written in sRGB |
|
|
834
|
+
| `maxInputPixels` / `maxInputBytes` | 100,000,000 / 100 MiB | checked before decoding |
|
|
835
|
+
| `timeoutMs` / `signal` | 30 s / none | |
|
|
836
|
+
| `environment` | the browser | your own decoder (e.g. WebAssembly HEIC) or a test double |
|
|
837
|
+
|
|
838
|
+
`width` and `height` in the result are the displayed size. Browsers turn JPEG and PNG by
|
|
839
|
+
their orientation, and Chromium doesn't turn WebP. `removed` is read back from the
|
|
840
|
+
output, not assumed, because some browsers' encoders add their own colour profile.
|
|
841
|
+
|
|
678
842
|
## Only one "Copied" at a time
|
|
679
843
|
|
|
680
844
|
Wrap a table or list in `<CopyGroup>`. A new successful copy returns the previously copied
|
|
@@ -1123,7 +1287,9 @@ embedded webviews. Where a capability is missing you get a typed error, never a
|
|
|
1123
1287
|
- ~~**1.8** `useDragCopy` / `registerDragCopy` — populate `DataTransfer` on drag-start; no Clipboard API needed~~ shipped
|
|
1124
1288
|
- ~~**1.9** `react-smart-copy/table` — Excel- and Sheets-ready copy and paste, AG Grid and TanStack Table adapters, typed record import~~ shipped
|
|
1125
1289
|
- ~~**1.10** `react-smart-copy/structured` — typed, versioned, validated application data on the clipboard~~ shipped
|
|
1126
|
-
-
|
|
1290
|
+
- ~~**1.11** `react-smart-copy/markdown` — paste rich content as Markdown, with undo intact~~ shipped
|
|
1291
|
+
- ~~**1.12** `react-smart-copy/code` — copy code blocks and sessions cleanly, paste code safely, detect Trojan Source and homoglyphs~~ shipped
|
|
1292
|
+
- **1.13** `react-smart-copy/image` — pasted images without location or camera metadata, fitted to size and byte limits
|
|
1127
1293
|
|
|
1128
1294
|
Full history: [CHANGELOG.md](./CHANGELOG.md) or the [Releases page](https://suprabhat02.github.io/react-smart-copy/#releases).
|
|
1129
1295
|
|
|
@@ -1131,7 +1297,7 @@ Full history: [CHANGELOG.md](./CHANGELOG.md) or the [Releases page](https://supr
|
|
|
1131
1297
|
|
|
1132
1298
|
```bash
|
|
1133
1299
|
npm install
|
|
1134
|
-
npm run verify # lint, types,
|
|
1300
|
+
npm run verify # lint, types, tests at 100% coverage, build, publint + attw, consumer types, size budgets
|
|
1135
1301
|
npm run changeset # describe your change for the changelog
|
|
1136
1302
|
npm run site:preview # docs site at http://localhost:3000
|
|
1137
1303
|
```
|