From 39e81af78275c597910de8af960d627d033f8a22 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 15 Jun 2025 20:37:06 +0800 Subject: [PATCH] code refactor --- src/components/mobile/InformationSheet.vue | 43 ++--------- src/lib/clipboard.ts | 76 ------------------- src/lib/ui/common.ts | 8 ++ .../tabs/UserTwoFactorAuthSettingTab.vue | 45 ++--------- 4 files changed, 22 insertions(+), 150 deletions(-) delete mode 100644 src/lib/clipboard.ts diff --git a/src/components/mobile/InformationSheet.vue b/src/components/mobile/InformationSheet.vue index 9d06e407..964ddaa3 100644 --- a/src/components/mobile/InformationSheet.vue +++ b/src/components/mobile/InformationSheet.vue @@ -9,9 +9,9 @@

{{ hint }} -

@@ -25,11 +25,9 @@ diff --git a/src/lib/clipboard.ts b/src/lib/clipboard.ts deleted file mode 100644 index 4fe94fd0..00000000 --- a/src/lib/clipboard.ts +++ /dev/null @@ -1,76 +0,0 @@ -import Clipboard from 'clipboard'; - -export interface ClipboardCreateContext { - readonly el: string | Element; - readonly text: string; - readonly successCallback?: (e: ClipboardEvent) => void; - readonly errorCallback?: (e: ClipboardEvent) => void; -} - -export interface ClipboardEvent { - readonly text: string; - readonly action: string; -} - -export class ClipboardTextHolder { - private text: string; - - public constructor(text: string) { - this.text = text; - } - - public getText(): string { - return this.text; - } - - public setText(text: string): void { - this.text = text; - } -} - -export class ClipboardHolder { - private readonly textHolder: ClipboardTextHolder; - private readonly clipboard: Clipboard; - - private constructor(textHolder: ClipboardTextHolder, clipboard: Clipboard) { - this.textHolder = textHolder; - this.clipboard = clipboard; - } - - public setClipboardText(text: string): void { - this.textHolder.setText(text); - } - - public destroy(): void { - this.clipboard.destroy(); - } - - public static create({ el, text, successCallback, errorCallback }: ClipboardCreateContext): ClipboardHolder { - const textHolder = new ClipboardTextHolder(text); - const clipboard = new Clipboard(el, { - text: function () { - return textHolder.getText(); - } - }); - - clipboard.on('success', (e) => { - if (successCallback) { - successCallback({ - text: e.text, - action: e.action - }); - } - }); - - clipboard.on('error', (e) => { - if (errorCallback) { - errorCallback({ - text: e.text, - action: e.action - }); - } - }); - - return new ClipboardHolder(textHolder, clipboard); - } -} diff --git a/src/lib/ui/common.ts b/src/lib/ui/common.ts index 42f7a3e9..1df48d33 100644 --- a/src/lib/ui/common.ts +++ b/src/lib/ui/common.ts @@ -1,3 +1,5 @@ +import Clipboard from 'clipboard'; + import { ThemeType } from '@/core/theme.ts'; import { type AmountColor, PresetAmountColor } from '@/core/color.ts'; @@ -76,6 +78,12 @@ export function setExpenseAndIncomeAmountColor(expenseAmountColorType: number, i } } +export function copyTextToClipboard(text: string, container?: Element | null): void { + Clipboard.copy(text, { + container: container || document.body + }); +} + export function startDownloadFile(fileName: string, fileData: Blob): void { const dataObjectUrl = URL.createObjectURL(fileData); const dataLink = document.createElement('a'); diff --git a/src/views/desktop/user/settings/tabs/UserTwoFactorAuthSettingTab.vue b/src/views/desktop/user/settings/tabs/UserTwoFactorAuthSettingTab.vue index 379c9fec..ec2c2e1d 100644 --- a/src/views/desktop/user/settings/tabs/UserTwoFactorAuthSettingTab.vue +++ b/src/views/desktop/user/settings/tabs/UserTwoFactorAuthSettingTab.vue @@ -81,9 +81,8 @@