mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-18 00:34:28 +08:00
save / load column mapping file for delimiter-separated values (dsv) file
This commit is contained in:
@@ -4,6 +4,8 @@ import { ThemeType } from '@/core/theme.ts';
|
||||
|
||||
import { type AmountColor, PresetAmountColor } from '@/core/color.ts';
|
||||
|
||||
import logger from '../logger.ts';
|
||||
|
||||
export function getSystemTheme(): ThemeType {
|
||||
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||
return ThemeType.Dark;
|
||||
@@ -84,6 +86,43 @@ export function copyTextToClipboard(text: string, container?: Element | null): v
|
||||
});
|
||||
}
|
||||
|
||||
export function openTextFileContent({ allowedExtensions }: { allowedExtensions: string }): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fileInput = document.createElement('input');
|
||||
|
||||
fileInput.style.display = 'none';
|
||||
fileInput.type = 'file';
|
||||
fileInput.accept = allowedExtensions;
|
||||
|
||||
fileInput.onchange = (event) => {
|
||||
const el = event.target as HTMLInputElement;
|
||||
|
||||
if (el.files && el.files.length > 0) {
|
||||
const file = el.files[0];
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = (e) => {
|
||||
const content = e.target?.result;
|
||||
|
||||
if (typeof content === 'string') {
|
||||
resolve(content);
|
||||
} else {
|
||||
reject(new Error('failed to load file, because file reader result is not string'));
|
||||
}
|
||||
};
|
||||
|
||||
reader.onerror = (error) => {
|
||||
logger.error('failed to load file', error);
|
||||
};
|
||||
|
||||
reader.readAsText(file);
|
||||
}
|
||||
};
|
||||
|
||||
fileInput.click();
|
||||
});
|
||||
}
|
||||
|
||||
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