mirror of
https://github.com/easydiffusion/easydiffusion.git
synced 2025-04-09 18:49:11 +02:00
Add paste button next to copy button
This commit is contained in:
parent
a19ba40672
commit
1c3d5cd851
@ -396,12 +396,36 @@ const TASK_REQ_NO_EXPORT = [
|
|||||||
"save_to_disk_path"
|
"save_to_disk_path"
|
||||||
]
|
]
|
||||||
|
|
||||||
// Adds a copy icon if the browser grants permission to write to clipboard.
|
// Retrieve clipboard content and try to parse it
|
||||||
|
async function pasteFromClipboard() {
|
||||||
|
//const text = await navigator.clipboard.readText()
|
||||||
|
let text = await navigator.clipboard.readText();
|
||||||
|
text=text.trim();
|
||||||
|
if (text.startsWith('{') && text.endsWith('}')) {
|
||||||
|
try {
|
||||||
|
const task = JSON.parse(text)
|
||||||
|
restoreTaskToUI(task)
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(`Clipboard JSON couldn't be parsed.`, e)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Normal txt file.
|
||||||
|
const task = parseTaskFromText(text)
|
||||||
|
if (task) {
|
||||||
|
restoreTaskToUI(task)
|
||||||
|
} else {
|
||||||
|
console.warn(`Clipboard content - File couldn't be parsed.`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adds a copy and a paste icon if the browser grants permission to write to clipboard.
|
||||||
function checkWriteToClipboardPermission (result) {
|
function checkWriteToClipboardPermission (result) {
|
||||||
if (result.state == "granted" || result.state == "prompt") {
|
if (result.state == "granted" || result.state == "prompt") {
|
||||||
const resetSettings = document.getElementById('reset-image-settings')
|
const resetSettings = document.getElementById('reset-image-settings')
|
||||||
|
|
||||||
|
// COPY ICON
|
||||||
const copyIcon = document.createElement('i')
|
const copyIcon = document.createElement('i')
|
||||||
// copyIcon.id = 'copy-image-settings'
|
|
||||||
copyIcon.className = 'fa-solid fa-clipboard section-button'
|
copyIcon.className = 'fa-solid fa-clipboard section-button'
|
||||||
copyIcon.innerHTML = `<span class="simple-tooltip right">Copy Image Settings</span>`
|
copyIcon.innerHTML = `<span class="simple-tooltip right">Copy Image Settings</span>`
|
||||||
copyIcon.addEventListener('click', (event) => {
|
copyIcon.addEventListener('click', (event) => {
|
||||||
@ -415,8 +439,20 @@ function checkWriteToClipboardPermission (result) {
|
|||||||
navigator.clipboard.writeText(JSON.stringify(uiState, undefined, 4))
|
navigator.clipboard.writeText(JSON.stringify(uiState, undefined, 4))
|
||||||
})
|
})
|
||||||
resetSettings.parentNode.insertBefore(copyIcon, resetSettings)
|
resetSettings.parentNode.insertBefore(copyIcon, resetSettings)
|
||||||
|
|
||||||
|
// PASTE ICON
|
||||||
|
const pasteIcon = document.createElement('i')
|
||||||
|
pasteIcon.className = 'fa-solid fa-paste section-button'
|
||||||
|
pasteIcon.innerHTML = `<span class="simple-tooltip right">Paste Image Settings</span>`
|
||||||
|
pasteIcon.addEventListener('click', (event) => {
|
||||||
|
event.stopPropagation()
|
||||||
|
pasteFromClipboard()
|
||||||
|
})
|
||||||
|
resetSettings.parentNode.insertBefore(pasteIcon, resetSettings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine which access we have to the clipboard. Clipboard access is only available on localhost or via TLS.
|
||||||
navigator.permissions.query({ name: "clipboard-write" }).then(checkWriteToClipboardPermission, (e) => {
|
navigator.permissions.query({ name: "clipboard-write" }).then(checkWriteToClipboardPermission, (e) => {
|
||||||
if (e instanceof TypeError && typeof navigator?.clipboard?.writeText === 'function') {
|
if (e instanceof TypeError && typeof navigator?.clipboard?.writeText === 'function') {
|
||||||
// Fix for firefox https://bugzilla.mozilla.org/show_bug.cgi?id=1560373
|
// Fix for firefox https://bugzilla.mozilla.org/show_bug.cgi?id=1560373
|
||||||
|
Loading…
Reference in New Issue
Block a user