mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
Enhance copyToClipboard with file support and error handling
This commit is contained in:
parent
c39486f95a
commit
d853b1d020
@ -1,8 +1,48 @@
|
||||
export default (text) => {
|
||||
if (!navigator.clipboard?.writeText) {
|
||||
return app.message.error("Clipboard API not supported")
|
||||
}
|
||||
/**
|
||||
* Copies content to clipboard, supporting both text and file data
|
||||
* @param {string|File|Blob} content - The content to copy (text string or file/blob data)
|
||||
* @param {Object} options - Optional configuration
|
||||
* @param {string} options.successMessage - Custom success message
|
||||
* @returns {Promise<boolean>} - Promise resolving to success state
|
||||
*/
|
||||
export default async (content, options = {}) => {
|
||||
const { successMessage = "Copied to clipboard" } = options
|
||||
|
||||
navigator.clipboard.writeText(text)
|
||||
app.message.success("Copied to clipboard")
|
||||
}
|
||||
try {
|
||||
if (!navigator.clipboard) {
|
||||
app.message.error("Clipboard API not supported")
|
||||
return false
|
||||
}
|
||||
|
||||
if (typeof content === "string") {
|
||||
await navigator.clipboard.writeText(content)
|
||||
|
||||
app.message.success(successMessage)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
if (content instanceof File || content instanceof Blob) {
|
||||
const clipboardItem = new ClipboardItem({
|
||||
[content.type]: content,
|
||||
})
|
||||
|
||||
if (!navigator.clipboard.write) {
|
||||
app.message.error("File copying not supported in this browser")
|
||||
return false
|
||||
}
|
||||
|
||||
await navigator.clipboard.write([clipboardItem])
|
||||
app.message.success(successMessage)
|
||||
return true
|
||||
}
|
||||
|
||||
app.message.error("Unsupported content type")
|
||||
return false
|
||||
} catch (error) {
|
||||
console.error("Clipboard operation failed:", error)
|
||||
app.message.error("Failed to copy to clipboard")
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user