Use the document paste event for pasting json and text data.

This commit is contained in:
Marc-Andre Ferland 2022-11-17 02:16:20 -05:00
parent 1364fd5c45
commit c13f662e2d

View File

@ -332,17 +332,20 @@ async function parseContent(text) {
try {
const task = JSON.parse(text)
restoreTaskToUI(task)
return true
} catch (e) {
console.warn(`JSON text content couldn't be parsed.`, e)
}
return
return false
}
// Normal txt file.
const task = parseTaskFromText(text)
if (task) {
restoreTaskToUI(task)
return true
} else {
console.warn(`Raw text content couldn't be parsed.`)
return false
}
}
@ -423,6 +426,17 @@ function checkReadTextClipboardPermission (result) {
}
navigator.permissions.query({ name: "clipboard-read" }).then(checkReadTextClipboardPermission, (reason) => console.log('clipboard-read is not available. %o', reason))
document.addEventListener('paste', (event) => {
const paste = (event.clipboardData || window.clipboardData).getData('text')
const selection = window.getSelection()
console.log(selection)
console.log(selection.toString())
if (selection.toString().trim().length <= 0 && parseContent(paste)) {
event.preventDefault();
return;
}
});
// Adds a copy and a paste icon if the browser grants permission to write to clipboard.
function checkWriteToClipboardPermission (result) {
if (result.state != "granted" && result.state != "prompt") {