├── .gitignore
├── README.md
├── package.json
├── src
└── index.ts
├── tsconfig.json
└── yarn.lock
/.gitignore:
--------------------------------------------------------------------------------
1 | *.log
2 | .DS_Store
3 | node_modules
4 | .rts2_cache_cjs
5 | .rts2_cache_esm
6 | .rts2_cache_umd
7 | dist
8 | .idea
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # copy-lite
4 |
5 | `import {copyToClipboard} from 'copy-lite'`
6 |
7 | ## API
8 | `copyToClipboard(content: string, richHtml: boolean)`
9 |
10 | ## Examples
11 |
12 | ### Text
13 |
14 | `copyToClipboard('This is normal text')`
15 |
16 | ### Rich text
17 |
18 | `copyToClipboard('This text has a bold element and a link', true)`
19 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "copy-lite",
3 | "version": "0.1.2",
4 | "main": "dist/index.js",
5 | "module": "dist/copy-lite.esm.js",
6 | "typings": "dist/index.d.ts",
7 | "files": [
8 | "dist"
9 | ],
10 | "scripts": {
11 | "start": "tsdx watch",
12 | "build": "tsdx build",
13 | "test": "tsdx test"
14 | },
15 | "peerDependencies": {},
16 | "husky": {
17 | "hooks": {
18 | "pre-commit": "pretty-quick --staged"
19 | }
20 | },
21 | "prettier": {
22 | "printWidth": 80,
23 | "semi": true,
24 | "singleQuote": true,
25 | "trailingComma": "es5"
26 | },
27 | "devDependencies": {
28 | "@types/jest": "^24.0.17",
29 | "husky": "^3.0.2",
30 | "prettier": "^1.18.2",
31 | "pretty-quick": "^1.11.1",
32 | "tsdx": "^0.7.2",
33 | "tslib": "^1.10.0",
34 | "typescript": "^3.5.3"
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------
1 | export const copyToClipboard = (content: string, richHtml = false) => {
2 | if (!richHtml && navigator.clipboard) {
3 | return navigator.clipboard.writeText(content);
4 | }
5 | const textArea = document.createElement('textarea');
6 | textArea.style.maxHeight = '0';
7 | textArea.style.height = '0';
8 | textArea.style.opacity = '0';
9 | textArea.value = content;
10 | document.body.appendChild(textArea);
11 | textArea.select();
12 |
13 | const listener = (e: ClipboardEvent) => {
14 | e.preventDefault();
15 |
16 | if (e.clipboardData) {
17 | e.clipboardData.setData('text/html', content);
18 | e.clipboardData.setData('text/plain', content);
19 | }
20 | };
21 |
22 | const copy = () => document.execCommand('copy');
23 |
24 | if (richHtml) {
25 | document.addEventListener('copy', listener);
26 | copy();
27 | document.removeEventListener('copy', listener);
28 | } else {
29 | copy();
30 | }
31 |
32 | document.body.removeChild(textArea);
33 | };
34 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "include": ["src", "types"],
3 | "compilerOptions": {
4 | "target": "es5",
5 | "module": "esnext",
6 | "lib": ["dom", "esnext"],
7 | "importHelpers": true,
8 | "declaration": true,
9 | "sourceMap": true,
10 | "rootDir": "./",
11 | "strict": true,
12 | "noImplicitAny": true,
13 | "strictNullChecks": true,
14 | "strictFunctionTypes": true,
15 | "strictPropertyInitialization": true,
16 | "noImplicitThis": true,
17 | "alwaysStrict": true,
18 | "noUnusedLocals": true,
19 | "noUnusedParameters": true,
20 | "noImplicitReturns": true,
21 | "noFallthroughCasesInSwitch": true,
22 | "moduleResolution": "node",
23 | "baseUrl": "./",
24 | "paths": {
25 | "*": ["src/*", "node_modules/*"]
26 | },
27 | "jsx": "react",
28 | "esModuleInterop": true
29 | }
30 | }
31 |
--------------------------------------------------------------------------------