mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-17 00:12:11 +08:00
code refactor
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user