From 025d5aeeafe838ae102cc95e1b7e10f93110f1fc Mon Sep 17 00:00:00 2001 From: invalid w Date: Fri, 13 Sep 2024 16:11:52 +0800 Subject: [PATCH] =?UTF-8?q?chore(@vben/common-ui):=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=8B=96=E6=8B=BD=E6=A0=A1=E9=AA=8C=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/effects/common-ui/package.json | 1 + .../effects/common-ui/src/components/index.ts | 1 + .../components/verify/drag-verify/action.vue | 9 + .../src/components/verify/drag-verify/bar.vue | 9 + .../components/verify/drag-verify/content.vue | 14 + .../components/verify/drag-verify/index.vue | 314 ++++++++++++++++++ .../common-ui/src/components/verify/index.ts | 2 + .../common-ui/src/components/verify/typing.ts | 14 + .../components/verify/use-event-listener.ts | 61 ++++ .../src/router/routes/modules/examples.ts | 19 ++ .../examples/verify/drag-verify/index.vue | 36 ++ pnpm-lock.yaml | 3 + 12 files changed, 483 insertions(+) create mode 100644 packages/effects/common-ui/src/components/verify/drag-verify/action.vue create mode 100644 packages/effects/common-ui/src/components/verify/drag-verify/bar.vue create mode 100644 packages/effects/common-ui/src/components/verify/drag-verify/content.vue create mode 100644 packages/effects/common-ui/src/components/verify/drag-verify/index.vue create mode 100644 packages/effects/common-ui/src/components/verify/index.ts create mode 100644 packages/effects/common-ui/src/components/verify/typing.ts create mode 100644 packages/effects/common-ui/src/components/verify/use-event-listener.ts create mode 100644 playground/src/views/examples/verify/drag-verify/index.vue diff --git a/packages/effects/common-ui/package.json b/packages/effects/common-ui/package.json index 9bc0a8a03de..10d3d1049fb 100644 --- a/packages/effects/common-ui/package.json +++ b/packages/effects/common-ui/package.json @@ -27,6 +27,7 @@ "@vben/icons": "workspace:*", "@vben/locales": "workspace:*", "@vben/types": "workspace:*", + "@vueuse/core": "^11.0.3", "@vueuse/integrations": "^11.0.3", "qrcode": "^1.5.4", "vue": "^3.5.4", diff --git a/packages/effects/common-ui/src/components/index.ts b/packages/effects/common-ui/src/components/index.ts index 865adf409db..cefe1ce3fc6 100644 --- a/packages/effects/common-ui/src/components/index.ts +++ b/packages/effects/common-ui/src/components/index.ts @@ -1,6 +1,7 @@ export * from './captcha'; export * from './ellipsis-text'; export * from './page'; +export * from './verify'; export * from '@vben-core/form-ui'; export * from '@vben-core/popup-ui'; diff --git a/packages/effects/common-ui/src/components/verify/drag-verify/action.vue b/packages/effects/common-ui/src/components/verify/drag-verify/action.vue new file mode 100644 index 00000000000..7e861709390 --- /dev/null +++ b/packages/effects/common-ui/src/components/verify/drag-verify/action.vue @@ -0,0 +1,9 @@ + + + + + diff --git a/packages/effects/common-ui/src/components/verify/drag-verify/bar.vue b/packages/effects/common-ui/src/components/verify/drag-verify/bar.vue new file mode 100644 index 00000000000..e1617fbe5fa --- /dev/null +++ b/packages/effects/common-ui/src/components/verify/drag-verify/bar.vue @@ -0,0 +1,9 @@ + + + + + diff --git a/packages/effects/common-ui/src/components/verify/drag-verify/content.vue b/packages/effects/common-ui/src/components/verify/drag-verify/content.vue new file mode 100644 index 00000000000..fba8e411da2 --- /dev/null +++ b/packages/effects/common-ui/src/components/verify/drag-verify/content.vue @@ -0,0 +1,14 @@ + + + + + diff --git a/packages/effects/common-ui/src/components/verify/drag-verify/index.vue b/packages/effects/common-ui/src/components/verify/drag-verify/index.vue new file mode 100644 index 00000000000..ec073a8bb0d --- /dev/null +++ b/packages/effects/common-ui/src/components/verify/drag-verify/index.vue @@ -0,0 +1,314 @@ + + + diff --git a/packages/effects/common-ui/src/components/verify/index.ts b/packages/effects/common-ui/src/components/verify/index.ts new file mode 100644 index 00000000000..991ba7a6be1 --- /dev/null +++ b/packages/effects/common-ui/src/components/verify/index.ts @@ -0,0 +1,2 @@ +export { default as DragVerify } from './drag-verify/index.vue'; +export * from './typing'; diff --git a/packages/effects/common-ui/src/components/verify/typing.ts b/packages/effects/common-ui/src/components/verify/typing.ts new file mode 100644 index 00000000000..42256968bef --- /dev/null +++ b/packages/effects/common-ui/src/components/verify/typing.ts @@ -0,0 +1,14 @@ +export interface DragVerifyActionType { + resume: () => void; +} + +export interface VerifyPassingData { + isPassing: boolean; + time: number; +} + +export interface VerifyMoveData { + event: MouseEvent | TouchEvent; + moveDistance: number; + moveX: number; +} diff --git a/packages/effects/common-ui/src/components/verify/use-event-listener.ts b/packages/effects/common-ui/src/components/verify/use-event-listener.ts new file mode 100644 index 00000000000..180023527d0 --- /dev/null +++ b/packages/effects/common-ui/src/components/verify/use-event-listener.ts @@ -0,0 +1,61 @@ +import type { Ref } from 'vue'; +import { ref, unref, watch } from 'vue'; + +import { useDebounceFn, useThrottleFn } from '@vueuse/core'; + +export type RemoveEventFn = () => void; +export interface UseEventParams { + el?: any | Element | Ref | Window; + name: string; + listener: EventListener; + options?: AddEventListenerOptions | boolean; + autoRemove?: boolean; + isDebounce?: boolean; + wait?: number; +} +export function useEventListener({ + autoRemove = true, + el = window, + isDebounce = true, + listener, + name, + options, + wait = 80, +}: UseEventParams): { removeEvent: RemoveEventFn } { + let remove: RemoveEventFn = () => {}; + const isAddRef = ref(false); + + if (el) { + const element = ref(el as Element) as Ref; + + const handler = isDebounce + ? useDebounceFn(listener, wait) + : useThrottleFn(listener, wait); + const realHandler = wait ? handler : listener; + const removeEventListener = (e: Element) => { + isAddRef.value = true; + e.removeEventListener(name, realHandler, options); + }; + const addEventListener = (e: Element) => + e.addEventListener(name, realHandler, options); + + const removeWatch = watch( + element, + (v, _ov, cleanUp) => { + if (v) { + !unref(isAddRef) && addEventListener(v); + cleanUp(() => { + autoRemove && removeEventListener(v); + }); + } + }, + { immediate: true }, + ); + + remove = () => { + removeEventListener(element.value); + removeWatch(); + }; + } + return { removeEvent: remove }; +} diff --git a/playground/src/router/routes/modules/examples.ts b/playground/src/router/routes/modules/examples.ts index c946e79c2bc..1e53131cc40 100644 --- a/playground/src/router/routes/modules/examples.ts +++ b/playground/src/router/routes/modules/examples.ts @@ -101,6 +101,25 @@ const routes: RouteRecordRaw[] = [ }, ], }, + { + name: 'VerifyExample', + path: '/examples/verify', + meta: { + icon: 'logos:recaptcha', + title: '验证组件', + }, + children: [ + { + name: 'DragVerifyExample', + path: '/examples/verify/drag-verify', + component: () => + import('#/views/examples/verify/drag-verify/index.vue'), + meta: { + title: '拖拽校验', + }, + }, + ], + }, ], }, ]; diff --git a/playground/src/views/examples/verify/drag-verify/index.vue b/playground/src/views/examples/verify/drag-verify/index.vue new file mode 100644 index 00000000000..823b839e6d7 --- /dev/null +++ b/playground/src/views/examples/verify/drag-verify/index.vue @@ -0,0 +1,36 @@ + + + diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9ac4c17b74c..3da5bae4e90 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -983,6 +983,9 @@ importers: '@vben/types': specifier: workspace:* version: link:../../types + '@vueuse/core': + specifier: ^11.0.3 + version: 11.0.3(vue@3.5.4(typescript@5.6.2)) '@vueuse/integrations': specifier: ^11.0.3 version: 11.0.3(async-validator@4.2.5)(axios@1.7.7)(focus-trap@7.5.4)(nprogress@0.2.0)(qrcode@1.5.4)(sortablejs@1.15.3)(vue@3.5.4(typescript@5.6.2))