diff --git a/mock/role/index.ts b/mock/role/index.ts index f12c426..9cd0271 100644 --- a/mock/role/index.ts +++ b/mock/role/index.ts @@ -361,6 +361,14 @@ const adminList = [ meta: { title: 'useCrudSchemas' } + }, + { + path: 'useClipboard', + component: 'views/hooks/useClipboard', + name: 'UseClipboard', + meta: { + title: 'useClipboard' + } } ] }, @@ -626,6 +634,7 @@ const testList: string[] = [ '/hooks/useTagsView', '/hooks/useValidator', '/hooks/useCrudSchemas', + '/hooks/useClipboard', '/level', '/level/menu1', '/level/menu1/menu1-1', diff --git a/src/hooks/web/useClipboard.ts b/src/hooks/web/useClipboard.ts new file mode 100644 index 0000000..fb8c3d6 --- /dev/null +++ b/src/hooks/web/useClipboard.ts @@ -0,0 +1,47 @@ +import { ref } from 'vue' + +const useClipboard = () => { + const copied = ref(false) + const text = ref('') + const isSupported = ref(false) + + if (!navigator.clipboard && !document.execCommand) { + isSupported.value = false + } else { + isSupported.value = true + } + + const copy = (str: string) => { + if (navigator.clipboard) { + navigator.clipboard.writeText(str).then(() => { + text.value = str + copied.value = true + resetCopied() + }) + return + } + const input = document.createElement('input') + input.setAttribute('readonly', 'readonly') + input.setAttribute('value', str) + document.body.appendChild(input) + input.select() + input.setSelectionRange(0, 9999) + if (document.execCommand('copy')) { + text.value = str + document.execCommand('copy') + copied.value = true + resetCopied() + } + document.body.removeChild(input) + } + + const resetCopied = () => { + setTimeout(() => { + copied.value = false + }, 1500) + } + + return { copy, text, copied, isSupported } +} + +export { useClipboard } diff --git a/src/router/index.ts b/src/router/index.ts index 669e318..27dec3b 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -403,6 +403,14 @@ export const asyncRouterMap: AppRouteRecordRaw[] = [ meta: { title: 'useCrudSchemas' } + }, + { + path: 'useClipboard', + component: () => import('@/views/hooks/useClipboard.vue'), + name: 'UseClipboard', + meta: { + title: 'useClipboard' + } } ] }, diff --git a/src/views/hooks/useClipboard.vue b/src/views/hooks/useClipboard.vue new file mode 100644 index 0000000..50b1800 --- /dev/null +++ b/src/views/hooks/useClipboard.vue @@ -0,0 +1,26 @@ + + + + + + + + 复制 + 已复制 + + + 当前已复制: {{ text || 'none' }} + + + 你的浏览器不支持 Clipboard API + +
+ 当前已复制: {{ text || 'none' }} +
{{ text || 'none' }}
你的浏览器不支持 Clipboard API