Files
Folo/apps/desktop/layer/renderer/src/lib/export.ts
T
Innei 394d00f1a2 feat: support share actions and import from clipboard
- Added a new utility function `copyToClipboard` to handle clipboard operations with error handling.
- Refactored existing clipboard interactions across various components and hooks to utilize the new utility function.
- Introduced import/export functionality for action rules, allowing users to copy rules to clipboard and import from clipboard.
- Enhanced user feedback with toast notifications for clipboard actions.

This update improves code reusability and user experience when interacting with the clipboard.

Signed-off-by: Innei <tukon479@gmail.com>
2025-06-16 17:55:15 +08:00

35 lines
950 B
TypeScript

export const downloadJsonFile = (content: string, filename: string) => {
const blob = new Blob([content], { type: "application/json" })
const url = URL.createObjectURL(blob)
const link = document.createElement("a")
link.href = url
link.download = filename
document.body.append(link)
link.click()
link.remove()
URL.revokeObjectURL(url)
}
export const selectJsonFile = (): Promise<string> => {
return new Promise((resolve, reject) => {
const input = document.createElement("input")
input.type = "file"
input.accept = ".json,application/json"
input.onchange = async (event) => {
const file = (event.target as HTMLInputElement).files?.[0]
if (!file) {
reject(new Error("No file selected"))
return
}
try {
const content = await file.text()
resolve(content)
} catch {
reject(new Error("Failed to read file"))
}
}
input.click()
})
}