From 277e4e847462a30714104d340e00733426cfa8f2 Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Fri, 10 May 2024 12:43:48 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=F0=9F=9A=80=20v-copy=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/directives/index.ts | 16 ++++++++++ src/directives/modules/copy.ts | 49 +++++++++++++++++++++++++++++++ src/main.ts | 3 +- src/views/bunny/default/My.vue | 20 +++++++++++-- src/views/bunny/default/index.vue | 5 +++- 5 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 src/directives/index.ts create mode 100644 src/directives/modules/copy.ts diff --git a/src/directives/index.ts b/src/directives/index.ts new file mode 100644 index 0000000..369e613 --- /dev/null +++ b/src/directives/index.ts @@ -0,0 +1,16 @@ +import { App, Directive } from 'vue'; +import copy from './modules/copy'; + +const directivesList: { [key: string]: Directive } = { copy }; + +/** + * 挂载自定义事件 + */ +const directives = { + install: function (app: App) { + Object.keys(directivesList).forEach(key => { + app.directive(key, directivesList[key]); + }); + }, +}; +export default directives; diff --git a/src/directives/modules/copy.ts b/src/directives/modules/copy.ts new file mode 100644 index 0000000..f7795d7 --- /dev/null +++ b/src/directives/modules/copy.ts @@ -0,0 +1,49 @@ +import type { DirectiveBinding } from 'vue'; +import { Directive } from 'vue'; + +interface ElType extends HTMLElement { + copyData: string | number; // 定义一个属性,用于存储需要复制的数据 + __handleClick__: any; // 定义一个属性,用于存储事件处理函数 +} + +/** + * 复制自定义指令 + */ +const copy: Directive = { + mounted(el: ElType, binding: DirectiveBinding) { + el.copyData = binding.value; + el.addEventListener('click', handelClick); + }, + // 指令所在组件VNode 更新时触发 + updated(el: ElType, binding: DirectiveBinding) { + el.copyData = binding.value; // 更新需要复制的数据 + }, + // 指令与元素绑定时触发 + beforeMount(el: ElType) { + // 移除点击事件 + el.removeEventListener('click', el.__handleClick__); + }, +}; + +/** + * 点击事件 + * @param this 当前作用域 + */ +function handelClick(this: any) { + // 创建一个 input 元素 + const input = document.createElement('input'); + // 将需要复制的数据赋值给 input 的 value 属性 + input.value = this.copyData.toLocaleString(); + // 将 input 元素添加到 body 中 + document.body.appendChild(input); + // 选中 input 元素中的文本 + input.select(); + // 执行复制操作 + document.execCommand('Copy'); + // 将 input 元素从 body 中移除 + document.body.removeChild(input); + + // TODO: 如需自定义请修改提示方式 + alert('复制成功'); +} +export default copy; diff --git a/src/main.ts b/src/main.ts index 27bd4af..20b727e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,4 @@ +import directives from '@/directives'; import { createPinia } from 'pinia'; import { createApp } from 'vue'; import App from './App.vue'; @@ -8,5 +9,5 @@ import router from './router'; const pinia = createPinia(); const app = createApp(App); -app.use(router).use(pinia) +app.use(router).use(pinia).use(directives); app.mount('#app'); diff --git a/src/views/bunny/default/My.vue b/src/views/bunny/default/My.vue index 03f149c..4c508f3 100644 --- a/src/views/bunny/default/My.vue +++ b/src/views/bunny/default/My.vue @@ -1,4 +1,18 @@ - +