Fix a bug where images could no longer be dragged and dropped onto the initial image box

This commit is contained in:
cmdr2 2022-11-08 18:14:26 +05:30
parent 57c3acd9d8
commit fa080e380c
2 changed files with 13 additions and 10 deletions

View File

@ -280,7 +280,7 @@
<script src="media/js/auto-save.js?v=4"></script> <script src="media/js/auto-save.js?v=4"></script>
<script src="media/js/main.js?v=8"></script> <script src="media/js/main.js?v=8"></script>
<script src="media/js/themes.js?v=3"></script> <script src="media/js/themes.js?v=3"></script>
<script src="media/js/dnd.js?v=4"></script> <script src="media/js/dnd.js?v=5"></script>
<script> <script>
async function init() { async function init() {
await initSettings() await initSettings()

View File

@ -359,18 +359,21 @@ async function readFile(file, i) {
function dropHandler(ev) { function dropHandler(ev) {
console.log('Content dropped...') console.log('Content dropped...')
// Prevent default behavior (Prevent file/content from being opened) let items = []
ev.preventDefault()
if (ev?.dataTransfer?.items) { // Use DataTransferItemList interface if (ev?.dataTransfer?.items) { // Use DataTransferItemList interface
Array.from(ev.dataTransfer.items).forEach(function(item, i) { items = Array.from(ev.dataTransfer.items)
if (item.kind === 'file') { items = items.filter(item => item.kind === 'file')
const file = item.getAsFile() items = items.map(item => item.getAsFile())
readFile(file, i)
}
})
} else if (ev?.dataTransfer?.files) { // Use DataTransfer interface } else if (ev?.dataTransfer?.files) { // Use DataTransfer interface
Array.from(ev.dataTransfer.files).forEach(readFile) items = Array.from(ev.dataTransfer.files)
}
items = items.filter(item => item.name.toLowerCase().endsWith('.txt') || item.name.toLowerCase().endsWith('.json'))
if (items.length > 0) {
ev.preventDefault() // Prevent default behavior (Prevent file/content from being opened)
items.forEach(readFile)
} }
} }
function dragOverHandler(ev) { function dragOverHandler(ev) {