mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
Add an option to show image from editor in folder (#3412)
* Add "Show in folder" option to images in editor Credits: @abdnh's Reveal in File Manager add-on (https://github.com/abdnh/anki-misc/tree/master/reveal_in_file_manager) * Refactor
This commit is contained in:
parent
5bd66db3c6
commit
02d2566998
3 changed files with 53 additions and 17 deletions
|
@ -36,6 +36,7 @@ editing-mathjax-chemistry = MathJax chemistry
|
|||
editing-mathjax-inline = MathJax inline
|
||||
editing-mathjax-placeholder = Press { $accept } to accept, { $newline } for new line.
|
||||
editing-media = Media
|
||||
editing-show-in-folder = Show in folder
|
||||
editing-ordered-list = Ordered list
|
||||
editing-outdent = Decrease indent
|
||||
editing-paste = Paste
|
||||
|
|
|
@ -36,8 +36,8 @@ from anki.hooks import runFilter
|
|||
from anki.httpclient import HttpClient
|
||||
from anki.models import NotetypeId, StockNotetype
|
||||
from anki.notes import Note, NoteFieldsCheckResult, NoteId
|
||||
from anki.utils import checksum, is_lin, is_win, namedtmp
|
||||
from aqt import AnkiQt, colors, gui_hooks
|
||||
from anki.utils import checksum, is_lin, is_mac, is_win, namedtmp
|
||||
from aqt import AnkiQt, colors, gui_hooks, mw
|
||||
from aqt.operations import QueryOp
|
||||
from aqt.operations.note import update_note
|
||||
from aqt.operations.notetype import update_notetype_legacy
|
||||
|
@ -55,6 +55,7 @@ from aqt.utils import (
|
|||
saveGeom,
|
||||
shortcut,
|
||||
showInfo,
|
||||
showinFolder,
|
||||
showWarning,
|
||||
tooltip,
|
||||
tr,
|
||||
|
@ -1599,28 +1600,34 @@ class EditorWebView(AnkiWebView):
|
|||
|
||||
def contextMenuEvent(self, evt: QContextMenuEvent) -> None:
|
||||
m = QMenu(self)
|
||||
self._maybe_add_cut_action(m)
|
||||
self._maybe_add_copy_action(m)
|
||||
if self.hasSelection():
|
||||
self._add_cut_action(m)
|
||||
self._add_copy_action(m)
|
||||
a = m.addAction(tr.editing_paste())
|
||||
qconnect(a.triggered, self.onPaste)
|
||||
self._maybe_add_copy_image_action(m)
|
||||
if self._opened_context_menu_on_image():
|
||||
self._add_image_menu(AnkiWebView(self), m)
|
||||
gui_hooks.editor_will_show_context_menu(self, m)
|
||||
m.popup(QCursor.pos())
|
||||
|
||||
def _maybe_add_cut_action(self, menu: QMenu) -> None:
|
||||
if self.hasSelection():
|
||||
a = menu.addAction(tr.editing_cut())
|
||||
qconnect(a.triggered, self.onCut)
|
||||
def _add_cut_action(self, menu: QMenu) -> None:
|
||||
a = menu.addAction(tr.editing_cut())
|
||||
qconnect(a.triggered, self.onCut)
|
||||
|
||||
def _maybe_add_copy_action(self, menu: QMenu) -> None:
|
||||
if self.hasSelection():
|
||||
a = menu.addAction(tr.actions_copy())
|
||||
qconnect(a.triggered, self.onCopy)
|
||||
def _add_copy_action(self, menu: QMenu) -> None:
|
||||
a = menu.addAction(tr.actions_copy())
|
||||
qconnect(a.triggered, self.onCopy)
|
||||
|
||||
def _maybe_add_copy_image_action(self, menu: QMenu) -> None:
|
||||
if self._opened_context_menu_on_image():
|
||||
a = menu.addAction(tr.editing_copy_image())
|
||||
qconnect(a.triggered, self.on_copy_image)
|
||||
def _add_image_menu(self, webview: AnkiWebView, menu: QMenu) -> None:
|
||||
a = menu.addAction(tr.editing_copy_image())
|
||||
qconnect(a.triggered, self.on_copy_image)
|
||||
|
||||
if is_win or is_mac:
|
||||
url = webview.lastContextMenuRequest().mediaUrl()
|
||||
file_name = url.fileName()
|
||||
path = os.path.join(mw.col.media.dir(), file_name)
|
||||
a = menu.addAction(tr.editing_show_in_folder())
|
||||
qconnect(a.triggered, lambda: showinFolder(path))
|
||||
|
||||
|
||||
# QFont returns "Kozuka Gothic Pro L" but WebEngine expects "Kozuka Gothic Pro Light"
|
||||
|
|
|
@ -21,6 +21,7 @@ from anki._legacy import DeprecatedNamesMixinForModule
|
|||
from anki.collection import Collection, HelpPage
|
||||
from anki.lang import TR, tr_legacyglobal # pylint: disable=unused-import
|
||||
from anki.utils import (
|
||||
call,
|
||||
invalid_filename,
|
||||
is_mac,
|
||||
is_win,
|
||||
|
@ -885,6 +886,33 @@ def openFolder(path: str) -> None:
|
|||
QDesktopServices.openUrl(QUrl(f"file://{path}"))
|
||||
|
||||
|
||||
def showinFolder(path: str) -> None:
|
||||
if is_win:
|
||||
call(["explorer", "/select,", f"file://{path}"])
|
||||
elif is_mac:
|
||||
script = f"""
|
||||
tell application "Finder"
|
||||
activate
|
||||
select POSIX file '{path}'
|
||||
end tell
|
||||
"""
|
||||
call(osascript_to_args(script))
|
||||
else:
|
||||
# Just open the file in any other platform
|
||||
with no_bundled_libs():
|
||||
QDesktopServices.openUrl(QUrl(f"file://{path}"))
|
||||
|
||||
|
||||
def osascript_to_args(script: str):
|
||||
args = [
|
||||
item
|
||||
for line in script.splitlines()
|
||||
for item in ("-e", line.strip())
|
||||
if line.strip()
|
||||
]
|
||||
return ["osascript"] + args
|
||||
|
||||
|
||||
def shortcut(key: str) -> str:
|
||||
if is_mac:
|
||||
return re.sub("(?i)ctrl", "Command", key)
|
||||
|
|
Loading…
Reference in a new issue