Skip to content

Commit

Permalink
Make it possible to open files via drag & drop
Browse files Browse the repository at this point in the history
  • Loading branch information
c3er committed Jul 31, 2023
1 parent 5143372 commit 047cdf4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 9 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ function domContentLoadedHandler() {
})
toc.updateTheme(chooseTheme(Boolean(match.matches)))

document.body.ondragover = event => {
event.preventDefault()
event.dataTransfer.dropEffect = "copy"
}
document.body.ondrop = event => {
event.preventDefault()
navigation.openFile(event.dataTransfer.files[0].path, false)
}

ipc.send(ipc.messages.finishLoad)
}

Expand Down
16 changes: 11 additions & 5 deletions app/lib/navigation/navigationRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ function isInternalLink(url) {
return url.startsWith("#")
}

function openFile(fullPath, shallOpenInNewWindow, scrollPosition = 0) {
ipc.send(
shallOpenInNewWindow ? ipc.messages.openFileInNewWindow : ipc.messages.openFile,
fullPath,
scrollPosition,
)
}

function dispatchLink(target, documentDirectory, shallOpenInNewWindow) {
const fullPath = path.join(documentDirectory, target)
const scrollPosition = renderer.contentElement().scrollTop
Expand All @@ -25,11 +33,7 @@ function dispatchLink(target, documentDirectory, shallOpenInNewWindow) {
} else if (!file.isMarkdown(fullPath) && !file.isText(fullPath)) {
electron.shell.openPath(fullPath)
} else {
ipc.send(
shallOpenInNewWindow ? ipc.messages.openFileInNewWindow : ipc.messages.openFile,
fullPath,
scrollPosition,
)
openFile(fullPath, shallOpenInNewWindow, scrollPosition)
}
}

Expand All @@ -49,3 +53,5 @@ exports.registerLink = (linkElement, target, documentDirectory) => {
}

exports.back = () => ipc.send(ipc.messages.navigateBack)

exports.openFile = openFile

0 comments on commit 047cdf4

Please sign in to comment.