From dfcf1193dca57ed086bbea4ca4226ff61920a88c Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Wed, 17 Oct 2018 08:01:18 +0100 Subject: [PATCH] Electron: Handle internal anchors --- .../popup/src/randomClipperPort.js | 24 +++++++++---------- ElectronClient/app/gui/NoteText.jsx | 2 ++ ElectronClient/app/gui/note-viewer/index.html | 14 ++++++++++- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Clipper/joplin-webclipper/popup/src/randomClipperPort.js b/Clipper/joplin-webclipper/popup/src/randomClipperPort.js index 8a8167338..87380d701 100644 --- a/Clipper/joplin-webclipper/popup/src/randomClipperPort.js +++ b/Clipper/joplin-webclipper/popup/src/randomClipperPort.js @@ -1,20 +1,20 @@ function randomClipperPort(state, env) { - const startPorts = { - prod: 41184, - dev: 27583, - }; + const startPorts = { + prod: 41184, + dev: 27583, + }; - const startPort = env === 'prod' ? startPorts.prod : startPorts.dev; + const startPort = env === 'prod' ? startPorts.prod : startPorts.dev; - if (!state) { - state = { offset: 0 }; - } else { - state.offset++; - } + if (!state) { + state = { offset: 0 }; + } else { + state.offset++; + } - state.port = startPort + state.offset; + state.port = startPort + state.offset; - return state; + return state; } module.exports = randomClipperPort; \ No newline at end of file diff --git a/ElectronClient/app/gui/NoteText.jsx b/ElectronClient/app/gui/NoteText.jsx index f7c5158ec..374cd14a6 100644 --- a/ElectronClient/app/gui/NoteText.jsx +++ b/ElectronClient/app/gui/NoteText.jsx @@ -595,6 +595,8 @@ class NoteTextComponent extends React.Component { } else { require('electron').shell.openExternal(msg); } + } else if (msg.indexOf('#') === 0) { + // This is an internal anchor, which is handled by the WebView so skip this case } else { bridge().showErrorMessageBox(_('Unsupported link or message: %s', msg)); } diff --git a/ElectronClient/app/gui/note-viewer/index.html b/ElectronClient/app/gui/note-viewer/index.html index 3b66f3136..791a4d4dd 100644 --- a/ElectronClient/app/gui/note-viewer/index.html +++ b/ElectronClient/app/gui/note-viewer/index.html @@ -276,12 +276,24 @@ } }); - // Prevent URLs added via tags from being opened within the application itself document.addEventListener('click', function(event) { const t = event.target; + + // Prevent URLs added via tags from being opened within the application itself if (t && t.nodeName === 'A' && !t.hasAttribute('data-from-md')) { event.preventDefault(); ipcProxySendToHost(t.getAttribute('href')); + return; + } + + // IF this is an internal link, jump to the anchor directly + if (t && t.nodeName === 'A' && t.hasAttribute('data-from-md')) { + const href = t.getAttribute('href'); + if (href.indexOf('#') === 0) { + event.preventDefault(); + location.hash = href; + return; + } } });