Fix JS drop event not firing in the reviewer (#1906)

* Allow webviews to opt in to default D&D handling

* Remove redundant webview.js include

* Block default drag & drop behavior in reviewing screens

* Fix mypy error
This commit is contained in:
Abdo 2022-06-10 16:33:53 +03:00 committed by GitHub
parent f15e294981
commit 1bab947c9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 2 deletions

View file

@ -132,6 +132,8 @@ class Previewer(QDialog):
],
context=self,
)
self._web.allow_drops = True
self._web.eval("_blockDefaultDragDropBehavior();")
self._web.set_bridge_command(self._on_bridge_cmd, self)
def _on_bridge_cmd(self, cmd: str) -> Any:

View file

@ -355,6 +355,8 @@ class CardLayout(QDialog):
],
context=self,
)
self.preview_web.allow_drops = True
self.preview_web.eval("_blockDefaultDragDropBehavior();")
self.preview_web.set_bridge_command(self._on_bridge_cmd, self)
if self._isCloze():

View file

@ -313,6 +313,9 @@ class Reviewer:
],
context=self,
)
# block default drag & drop behavior while allowing drop events to be received by JS handlers
self.web.allow_drops = True
self.web.eval("_blockDefaultDragDropBehavior();")
# show answer / ease buttons
self.bottom.web.show()
self.bottom.web.stdHtml(

View file

@ -47,7 +47,7 @@ class Toolbar:
self.web.stdHtml(
self._body % self._centerLinks(),
css=["css/toolbar.css"],
js=["js/webview.js", "js/vendor/jquery.min.js", "js/toolbar.js"],
js=["js/vendor/jquery.min.js", "js/toolbar.js"],
context=web_context,
)
self.web.adjustHeightToFit()

View file

@ -222,6 +222,8 @@ class WebContent:
class AnkiWebView(QWebEngineView):
allow_drops = False
def __init__(
self,
parent: Optional[QWidget] = None,
@ -322,7 +324,8 @@ class AnkiWebView(QWebEngineView):
m.popup(QCursor.pos())
def dropEvent(self, evt: QDropEvent) -> None:
pass
if self.allow_drops:
super().dropEvent(evt)
def setHtml(self, html: str) -> None: # type: ignore
# discard any previous pending actions
@ -331,6 +334,7 @@ class AnkiWebView(QWebEngineView):
self._queueAction("setHtml", html)
self.set_open_links_externally(True)
self.setZoomFactor(1)
self.allow_drops = False
self.show()
def _setHtml(self, html: str) -> None:
@ -357,6 +361,7 @@ class AnkiWebView(QWebEngineView):
def load_url(self, url: QUrl) -> None:
# allow queuing actions when loading url directly
self._domDone = False
self.allow_drops = False
super().load(url)
def app_zoom_factor(self) -> float:

View file

@ -227,3 +227,13 @@ export function _typeAnsPress(): void {
export function _emulateMobile(enabled: boolean): void {
document.documentElement.classList.toggle("mobile", enabled);
}
// Block Qt's default drag & drop behavior by default
export function _blockDefaultDragDropBehavior(): void {
function handler(evt: DragEvent) {
evt.preventDefault();
}
document.ondragenter = handler;
document.ondragover = handler;
document.ondrop = handler;
}