mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-20 06:51:46 +01:00
feat(plugins): clipboard (#29)
This commit is contained in:
committed by
GitHub
parent
ed499b3b21
commit
832ffe4323
@@ -183,6 +183,7 @@ export default defineNuxtModule<ModuleOptions>({
|
||||
})
|
||||
|
||||
addPlugin(resolve(runtimeDir, 'plugins', 'toast.client'))
|
||||
addPlugin(resolve(runtimeDir, 'plugins', 'clipboard.client'))
|
||||
|
||||
addComponentsDir({
|
||||
path: resolve(runtimeDir, 'components', 'elements'),
|
||||
|
||||
33
src/runtime/plugins/clipboard.client.ts
Normal file
33
src/runtime/plugins/clipboard.client.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { defineNuxtPlugin } from '#app'
|
||||
import { ClipboardPlugin } from '../types/clipboard'
|
||||
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
function copy (text: string, success: { title?: string, description?: string } = {}, failure: { title?: string, description?: string } = {}) {
|
||||
if (!navigator?.clipboard) {
|
||||
return
|
||||
}
|
||||
|
||||
navigator.clipboard.writeText(text).then(() => {
|
||||
if (!success.title && !success.description) {
|
||||
return
|
||||
}
|
||||
|
||||
nuxtApp.$toast.success(success)
|
||||
}, function (e) {
|
||||
nuxtApp.$toast.error({
|
||||
...failure,
|
||||
description: failure.description || e.message
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
nuxtApp.provide('clipboard', {
|
||||
copy
|
||||
})
|
||||
})
|
||||
|
||||
declare module '#app' {
|
||||
interface NuxtApp {
|
||||
$clipboard: ClipboardPlugin
|
||||
}
|
||||
}
|
||||
3
src/runtime/types/clipboard.d.ts
vendored
Normal file
3
src/runtime/types/clipboard.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface ClipboardPlugin {
|
||||
copy: (text: string, success?: { title?: string, description?: string }, failure?: { title?: string, description?: string }) => void
|
||||
}
|
||||
Reference in New Issue
Block a user