code refactor

This commit is contained in:
MaysWind
2025-06-15 20:37:06 +08:00
parent cc16f57a44
commit 39e81af782
4 changed files with 22 additions and 150 deletions
-76
View File
@@ -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);
}
}
+8
View File
@@ -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');