Skip to content

Commit

Permalink
Merge pull request #4 from DavidCEllis/fix_links
Browse files Browse the repository at this point in the history
Allow links to work and make them launch in an external browser.
  • Loading branch information
DavidCEllis authored Jun 8, 2022
2 parents 0d0d09b + 6ede2e4 commit bc3a29e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

__version__ = "0.6.0"
__version__ = "0.6.1"
__author__ = "DavidCEllis"


Expand Down
3 changes: 2 additions & 1 deletion src/splitnotes2/note_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


PERMITTED_TAGS = [
"p",
"br",
"del",
"ins",
Expand All @@ -30,7 +31,7 @@
]

PERMITTED_ATTRIBUTES = {
"*": ["class", "style"],
"*": ["class", "style", "href"],
"img": ["src", "alt", "width", "height"],
"video": [
"autoplay",
Expand Down
1 change: 1 addition & 0 deletions src/splitnotes2/ui/custom_elements/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .webenginepage import ExtLinkWebEnginePage
18 changes: 18 additions & 0 deletions src/splitnotes2/ui/custom_elements/webenginepage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Replace the standard QWebEnginePage with one that launches links in the user's default browser
"""
from PySide2.QtWebEngineWidgets import QWebEnginePage
from PySide2.QtGui import QDesktopServices


class ExtLinkWebEnginePage(QWebEnginePage):
"""
QWebEnginePage that launches links in an external browser
"""

def acceptNavigationRequest(self, url, _type, is_mainframe):
# Launch external browser for clicking links, otherwise do default behaviour
if _type == QWebEnginePage.NavigationTypeLinkClicked:
QDesktopServices.openUrl(url)
return False
return super().acceptNavigationRequest(url, _type, is_mainframe)
6 changes: 5 additions & 1 deletion src/splitnotes2/ui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from PySide2 import QtCore
from PySide2.QtGui import QCursor, QIcon
from PySide2.QtWidgets import QMainWindow, QFileDialog, QMenu
from .custom_elements import ExtLinkWebEnginePage

from ..settings import Settings
from .settings_ui import SettingsDialog
Expand Down Expand Up @@ -158,9 +159,12 @@ def resizeEvent(self, event):
event.accept()

def setup_actions(self):
"""Make the context menu for the UI the custom menu."""
"""Setup the browser element with custom options"""
# Replace the context menu with the app context menu
self.ui.notes.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.ui.notes.customContextMenuRequested.connect(self.show_menu)
# Allow links to open in an external browser
self.ui.notes.setPage(ExtLinkWebEnginePage(self))

def build_menu(self):
"""Create the custom context menu."""
Expand Down

0 comments on commit bc3a29e

Please sign in to comment.