Skip to content

Commit

Permalink
feat: add copyText util function (#25)
Browse files Browse the repository at this point in the history
* feat: add copyText util function

* Update copy-text.md

* Update copy-text.md

---------

Co-authored-by: liusen <[email protected]>
  • Loading branch information
LiuSen688 and liusen authored Nov 3, 2024
1 parent a972e82 commit 178facd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/items/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export const utilItems = [
{ text: 'getAllParentScroller', link: '/util/get-all-parent-scroller' },
{ text: 'prettyJSONObject', link: '/util/pretty-JSON-object' },
{ text: 'tryParseJSON', link: '/util/try-parse-JSON' },
{ text: 'copyText', link: '/util/copy-text' },
]
23 changes: 23 additions & 0 deletions docs/util/copy-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# copyText

Copies text to the clipboard.

### Usage

```js
import { copyText } from 'rattail'

copyText('Hello, clipboard!') // The string will be written to the clipboard
```

### Arguments

| Arg | Type | Defaults |
| ------ | -------- | -------- |
| `text` | `string` | |

### Return

| Type |
| ------ |
| `-` |
23 changes: 23 additions & 0 deletions docs/zh/util/copy-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# copyText

复制文本到剪贴板

### 用法

```js
import { copyText } from 'rattail'

copyText('Hello, clipboard!') // 字符串会被写入剪切板
```

### 参数

| 参数 | 类型 | 默认值 |
| ------ | -------- | ------ |
| `text` | `string` | |

### 返回值

| 类型 |
| ------ |
| `-` |
12 changes: 12 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,15 @@ export function createStorage(storage: globalThis.Storage): Storage {
export const sessionStorage = createStorage(globalThis.sessionStorage)

export const localStorage = createStorage(globalThis.localStorage)

export function copyText(value: string) {
if (!value) return
const textArea = document.createElement('textarea')
textArea.value = value
textArea.style.position = 'fixed'
textArea.style.opacity = '0'
document.body.appendChild(textArea)
textArea.select()
document.execCommand('copy')
document.body.removeChild(textArea)
}
19 changes: 19 additions & 0 deletions tests/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
createStorage,
tryParseJSON,
prettyJSONObject,
copyText,
} from '../src'

it('requestAnimationFrame', () => {
Expand Down Expand Up @@ -244,3 +245,21 @@ describe('JSON utility functions', () => {
expect(prettyString).toBe('{\n "key": "value",\n "number": 42\n}')
})
})

describe('copyText', () => {
it('should copy text successfully', () => {
const execCommandMock = vi.fn().mockImplementation(() => true)
document.execCommand = execCommandMock
copyText('Hello, world!')
expect(execCommandMock).toHaveBeenCalledWith('copy')
execCommandMock.mockRestore()
})

it('should not execute copy when no text is provided', () => {
const execCommandMock = vi.fn()
document.execCommand = execCommandMock
copyText('')
expect(execCommandMock).not.toHaveBeenCalled()
execCommandMock.mockRestore()
})
})

0 comments on commit 178facd

Please sign in to comment.