Skip to content

Instantly share code, notes, and snippets.

@kelciour
Last active October 6, 2019 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelciour/2bd7944d419df25872ea9a058402c6b7 to your computer and use it in GitHub Desktop.
Save kelciour/2bd7944d419df25872ea9a058402c6b7 to your computer and use it in GitHub Desktop.
import aqt.editor
from aqt.qt import *
def _flagAnkiText(self):
clip = self.editor.mw.app.clipboard()
mime = clip.mimeData()
if not mime.hasHtml():
return
html = mime.html()
if '<img' in html:
mime.setHtml("<!--anki-->" + mime.html())
def updateClipboard(self, mime, mode=QClipboard.Clipboard, restore=False):
clip = self.editor.mw.app.clipboard()
if mime.hasHtml() and self.savedClip.hasHtml():
if mime.html() == self.savedClip.html():
return
elif mime.hasText() and self.savedClip.hasText():
if mime.text().strip() == self.savedClip.text().strip():
return
if restore:
clip.setMimeData(self.savedClip, mode=mode)
else:
clip.setMimeData(mime, mode=mode)
def onPaste(self):
mime = self.mungeClip()
self.triggerPageAction(QWebPage.Paste)
self.restoreClip(mime)
def mungeClip(self, mode=QClipboard.Clipboard):
clip = self.editor.mw.app.clipboard()
mime = clip.mimeData(mode=mode)
self.saveClip(mode=mode)
mime = self._processMime(mime)
self.updateClipboard(mime, mode=mode)
return mime
def restoreClip(self, mime, mode=QClipboard.Clipboard):
self.updateClipboard(mime, mode=mode, restore=True)
aqt.editor.EditorWebView.updateClipboard = updateClipboard
aqt.editor.EditorWebView.onPaste = onPaste
aqt.editor.EditorWebView.mungeClip = mungeClip
aqt.editor.EditorWebView.restoreClip = restoreClip
aqt.editor.EditorWebView._flagAnkiText = _flagAnkiText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment