Skip to content

Commit 60c7eca

Browse files
committed
feat: add relative hooks and utils
1 parent 8b5a7e7 commit 60c7eca

24 files changed

Lines changed: 1018 additions & 5 deletions

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
"types": "./dist/index.d.ts",
2727
"exports": {
2828
".": "./dist/index.js",
29-
"./http-status": "./dist/http-status.js",
29+
"./configuration": "./dist/configuration/index.js",
30+
"./react": "./dist/react/index.js",
31+
"./js": "./dist/js/index.js",
32+
"./browser": "./dist/browser/index.js",
3033
"./package.json": "./package.json"
3134
},
3235
"publishConfig": {
@@ -40,10 +43,17 @@
4043
"release": "pnpm build && bumpp && npm publish"
4144
},
4245
"devDependencies": {
46+
"@types/lodash-es": "^4.17.12",
4347
"@types/node": "^22.15.17",
4448
"bumpp": "^10.1.0",
4549
"tsdown": "^0.13.1",
4650
"typescript": "^5.8.3",
4751
"vitest": "^3.1.3"
52+
},
53+
"dependencies": {
54+
"ahooks": "^3.9.0",
55+
"deepmerge-ts": "^7.1.5",
56+
"lodash-es": "^4.17.21",
57+
"react-fast-compare": "^3.2.2"
4858
}
4959
}

pnpm-lock.yaml

Lines changed: 125 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/browser/copy-to-clipboard.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { isBrowser } from './is-browser'
2+
3+
export const copyToClipboard = async (text: string, isRichText = false) => {
4+
if (!isBrowser()) {
5+
throw new Error('copyToClipboard only work in browser environment')
6+
}
7+
8+
try {
9+
if (isRichText) {
10+
const blob = new Blob([text], { type: 'text/html' })
11+
const data = [
12+
new ClipboardItem({
13+
'text/html': blob,
14+
'text/plain': new Blob([text], { type: 'text/plain' }),
15+
}),
16+
]
17+
await navigator.clipboard.write(data)
18+
} else {
19+
await navigator.clipboard.writeText(text)
20+
}
21+
return true
22+
} catch (err) {
23+
console.error('复制失败:', err)
24+
const textarea = document.createElement('textarea')
25+
textarea.value = text
26+
textarea.style.position = 'fixed'
27+
textarea.style.opacity = '0'
28+
document.body.appendChild(textarea)
29+
textarea.select()
30+
try {
31+
const successful = document.execCommand('copy')
32+
document.body.removeChild(textarea)
33+
return successful
34+
} catch (err) {
35+
document.body.removeChild(textarea)
36+
return false
37+
}
38+
}
39+
}

src/browser/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './copy-to-clipboard'

src/browser/is-browser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function isBrowser() {
2+
const hasWindow = typeof window !== 'undefined' && window !== null
3+
const hasDocument = typeof document !== 'undefined' && document !== null
4+
5+
if (!hasWindow || !hasDocument) return false
6+
7+
return true
8+
}

src/configuration/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './http-status'

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export * from './http-status'
1+
export * from './configuration'
2+
export * from './react'
3+
export * from './js'
4+
export * from './browser'

0 commit comments

Comments
 (0)