Merge pull request #1273 from hgiesel/macshortcuts

Execute Ctrl+C/V/X/A via execCommand in editor
This commit is contained in:
Damien Elmes 2021-07-04 16:02:52 +10:00 committed by GitHub
commit 581c3e2486
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 17 deletions

View File

@ -210,7 +210,9 @@ class WebContent:
class AnkiWebView(QWebEngineView):
def __init__(
self, parent: Optional[QWidget] = None, title: str = "default"
self,
parent: Optional[QWidget] = None,
title: str = "default",
) -> None:
QWebEngineView.__init__(self, parent=parent)
self.set_title(title)
@ -238,22 +240,6 @@ class AnkiWebView(QWebEngineView):
context=Qt.WidgetWithChildrenShortcut,
activated=self.onEsc,
)
if isMac:
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]

View File

@ -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