Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Fix Tauri #11

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare module "*.svg";

declare interface Window {
__TAURI__?: {
[x: string]: any;
writeText(text: string): Promise<void>;
};
}
4 changes: 4 additions & 0 deletions app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ const cn = {
Success: "已写入剪切板",
Failed: "复制失败,请赋予剪切板权限",
},
Download: {
Success: "内容已下载到您的目录。",
Failed: "下载失败。",
},
Context: {
Toast: (x: any) => `包含 ${x} 条预设提示词`,
Edit: "当前对话设置",
Expand Down
4 changes: 4 additions & 0 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ const en: LocaleType = {
Success: "Copied to clipboard",
Failed: "Copy failed, please grant permission to access clipboard",
},
Download: {
Success: "Content downloaded to your directory.",
Failed: "Download failed.",
},
Context: {
Toast: (x: any) => `With ${x} contextual prompts`,
Edit: "Current Chat Settings",
Expand Down
4 changes: 4 additions & 0 deletions app/locales/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ const id: PartialLocaleType = {
Failed:
"Gagal menyalin, mohon berikan izin untuk mengakses clipboard atau Clipboard API tidak didukung (Tauri)",
},
Download: {
Success: "Konten berhasil diunduh ke direktori Anda.",
Failed: "Unduhan gagal.",
},
Context: {
Toast: (x: any) => `Dengan ${x} promp kontekstual`,
Edit: "Pengaturan Obrolan Saat Ini",
Expand Down
38 changes: 31 additions & 7 deletions app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,39 @@ export async function copyToClipboard(text: string) {
}
}
//To ensure the expected functionality, the default file format must be JSON.
export function downloadAs(text: object, filename: string) {
export async function downloadAs(text: object, filename: string) {
const json = JSON.stringify(text);
const blob = new Blob([json], { type: "application/json" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `${filename}.json`;
link.click();
URL.revokeObjectURL(url);
const arrayBuffer = await blob.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer);

try {
if (window.__TAURI__) {
const result = await window.__TAURI__.dialog.save({
defaultPath: `${filename}.json`,
});

if (result !== null) {
await window.__TAURI__.fs.writeBinaryFile(
result,
Array.from(uint8Array),
);
showToast(Locale.Download.Success);
} else {
showToast(Locale.Download.Failed);
}
} else {
const url = URL.createObjectURL(blob);
const anchor = document.createElement("a");
anchor.href = url;
anchor.download = `${filename}.json`;
anchor.click();
URL.revokeObjectURL(url);
showToast(Locale.Download.Success);
}
} catch (error) {
showToast(Locale.Download.Failed);
}
}

export function readFromFile() {
Expand Down