From 7f7e5c2702c27da135a441c58c026742e7cd7a58 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 2 Jul 2021 17:30:49 +0200 Subject: [PATCH 1/3] Execute Ctrl+C/V/X/A through execCommand in editor (so we can customize it) --- qt/aqt/editor.py | 2 +- qt/aqt/webview.py | 5 +++-- ts/editor/index.ts | 7 +++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index 7766dd9e8..c2e6f8799 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -1052,7 +1052,7 @@ $editorToolbar.then(({{ toolbar }}) => toolbar.appendGroup({{ class EditorWebView(AnkiWebView): def __init__(self, parent: QWidget, editor: Editor) -> None: - AnkiWebView.__init__(self, title="editor") + AnkiWebView.__init__(self, title="editor", mac_default_shortcuts=False) self.editor = editor self.setAcceptDrops(True) self._markInternal = False diff --git a/qt/aqt/webview.py b/qt/aqt/webview.py index ccfd86c88..9f17ed6b9 100644 --- a/qt/aqt/webview.py +++ b/qt/aqt/webview.py @@ -210,7 +210,7 @@ class WebContent: class AnkiWebView(QWebEngineView): def __init__( - self, parent: Optional[QWidget] = None, title: str = "default" + self, parent: Optional[QWidget] = None, title: str = "default", *, mac_default_shortcuts: bool = True ) -> None: QWebEngineView.__init__(self, parent=parent) self.set_title(title) @@ -238,7 +238,7 @@ class AnkiWebView(QWebEngineView): context=Qt.WidgetWithChildrenShortcut, activated=self.onEsc, ) - if isMac: + if isMac and mac_default_shortcuts: for key, fn in [ (QKeySequence.Copy, self.onCopy), (QKeySequence.Paste, self.onPaste), @@ -248,6 +248,7 @@ class AnkiWebView(QWebEngineView): QShortcut( # type: ignore key, self, context=Qt.WidgetWithChildrenShortcut, activated=fn ) + QShortcut( # type: ignore QKeySequence("ctrl+shift+v"), self, diff --git a/ts/editor/index.ts b/ts/editor/index.ts index 14ec2e16b..af7341f5f 100644 --- a/ts/editor/index.ts +++ b/ts/editor/index.ts @@ -8,6 +8,8 @@ import { filterHTML } from "html-filter"; import { updateActiveButtons, disableButtons } from "./toolbar"; import { setupI18n, ModuleName } from "lib/i18n"; +import { registerShortcut } from "lib/shortcuts"; +import { bridgeCommand } from "./lib"; import "./fields.css"; @@ -40,6 +42,11 @@ customElements.define("anki-editing-area", EditingArea, { extends: "div" }); customElements.define("anki-label-container", LabelContainer, { extends: "div" }); customElements.define("anki-editor-field", EditorField, { extends: "div" }); +registerShortcut(() => document.execCommand("copy"), "Control+C"); +registerShortcut(() => document.execCommand("cut"), "Control+X"); +registerShortcut(() => document.execCommand("selectAll"), "Control+A"); +registerShortcut(() => bridgeCommand("paste"), "Control+Shift+V"); + export function getCurrentField(): EditingArea | null { return document.activeElement instanceof EditingArea ? document.activeElement From 5989fb8ef6b801aaae7113a9217e1dfb29884b3c Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sat, 3 Jul 2021 01:54:10 +0200 Subject: [PATCH 2/3] Satisfy qt formatter --- qt/aqt/webview.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/qt/aqt/webview.py b/qt/aqt/webview.py index 9f17ed6b9..aec378c2c 100644 --- a/qt/aqt/webview.py +++ b/qt/aqt/webview.py @@ -210,7 +210,11 @@ class WebContent: class AnkiWebView(QWebEngineView): def __init__( - self, parent: Optional[QWidget] = None, title: str = "default", *, mac_default_shortcuts: bool = True + self, + parent: Optional[QWidget] = None, + title: str = "default", + *, + mac_default_shortcuts: bool = True, ) -> None: QWebEngineView.__init__(self, parent=parent) self.set_title(title) From a54f181ae0c83e81e108ed93569a3de5cfa5c357 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 4 Jul 2021 16:00:23 +1000 Subject: [PATCH 3/3] remove old Mac shortcut code completely The shortcuts seem to work correctly without it on Qt 5.14, so this code seems to have only been required for older Qt releases. --- qt/aqt/editor.py | 2 +- qt/aqt/webview.py | 19 ------------------- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index c2e6f8799..7766dd9e8 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -1052,7 +1052,7 @@ $editorToolbar.then(({{ toolbar }}) => toolbar.appendGroup({{ class EditorWebView(AnkiWebView): def __init__(self, parent: QWidget, editor: Editor) -> None: - AnkiWebView.__init__(self, title="editor", mac_default_shortcuts=False) + AnkiWebView.__init__(self, title="editor") self.editor = editor self.setAcceptDrops(True) self._markInternal = False diff --git a/qt/aqt/webview.py b/qt/aqt/webview.py index aec378c2c..a5be5e21c 100644 --- a/qt/aqt/webview.py +++ b/qt/aqt/webview.py @@ -213,8 +213,6 @@ class AnkiWebView(QWebEngineView): self, parent: Optional[QWidget] = None, title: str = "default", - *, - mac_default_shortcuts: bool = True, ) -> None: QWebEngineView.__init__(self, parent=parent) self.set_title(title) @@ -242,23 +240,6 @@ class AnkiWebView(QWebEngineView): context=Qt.WidgetWithChildrenShortcut, activated=self.onEsc, ) - if isMac and mac_default_shortcuts: - for key, fn in [ - (QKeySequence.Copy, self.onCopy), - (QKeySequence.Paste, self.onPaste), - (QKeySequence.Cut, self.onCut), - (QKeySequence.SelectAll, self.onSelectAll), - ]: - QShortcut( # type: ignore - key, self, context=Qt.WidgetWithChildrenShortcut, activated=fn - ) - - QShortcut( # type: ignore - QKeySequence("ctrl+shift+v"), - self, - context=Qt.WidgetWithChildrenShortcut, - activated=self.onPaste, - ) def set_title(self, title: str) -> None: self.title = title # type: ignore[assignment]