mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 23:42:23 -04:00
Deduplicate some code in aqt/editor.py
This commit is contained in:
parent
af4d4af55d
commit
899305ec41
1 changed files with 52 additions and 55 deletions
107
aqt/editor.py
107
aqt/editor.py
|
@ -75,13 +75,20 @@ class Editor:
|
||||||
self.web.onBridgeCmd = self.onBridgeCmd
|
self.web.onBridgeCmd = self.onBridgeCmd
|
||||||
self.outerLayout.addWidget(self.web, 1)
|
self.outerLayout.addWidget(self.web, 1)
|
||||||
|
|
||||||
righttopbtns: List[str] = list()
|
righttopbtns: List[str] = [
|
||||||
righttopbtns.append(self._addButton('text_bold', 'bold', _("Bold text (Ctrl+B)"), id='bold'))
|
self._addButton('text_bold', 'bold',
|
||||||
righttopbtns.append(self._addButton('text_italic', 'italic', _("Italic text (Ctrl+I)"), id='italic'))
|
_("Bold text (Ctrl+B)"), id='bold'),
|
||||||
righttopbtns.append(self._addButton('text_under', 'underline', _("Underline text (Ctrl+U)"), id='underline'))
|
self._addButton('text_italic', 'italic',
|
||||||
righttopbtns.append(self._addButton('text_super', 'super', _("Superscript (Ctrl++)"), id='superscript'))
|
_("Italic text (Ctrl+I)"), id='italic'),
|
||||||
righttopbtns.append(self._addButton('text_sub', 'sub', _("Subscript (Ctrl+=)"), id='subscript'))
|
self._addButton('text_under', 'underline',
|
||||||
righttopbtns.append(self._addButton('text_clear', 'clear', _("Remove formatting (Ctrl+R)")))
|
_("Underline text (Ctrl+U)"), id='underline'),
|
||||||
|
self._addButton('text_super', 'super',
|
||||||
|
_("Superscript (Ctrl++)"), id='superscript'),
|
||||||
|
self._addButton('text_sub', 'sub',
|
||||||
|
_("Subscript (Ctrl+=)"), id='subscript'),
|
||||||
|
self._addButton('text_clear', 'clear',
|
||||||
|
_("Remove formatting (Ctrl+R)"))
|
||||||
|
]
|
||||||
# The color selection buttons do not use an icon so the HTML must be specified manually
|
# The color selection buttons do not use an icon so the HTML must be specified manually
|
||||||
tip = _("Set foreground colour (F7)")
|
tip = _("Set foreground colour (F7)")
|
||||||
righttopbtns.append('''<button tabindex=-1 class=linkb title="{}"
|
righttopbtns.append('''<button tabindex=-1 class=linkb title="{}"
|
||||||
|
@ -89,14 +96,18 @@ class Editor:
|
||||||
<div id=forecolor style="display:inline-block; background: #000;border-radius: 5px;"
|
<div id=forecolor style="display:inline-block; background: #000;border-radius: 5px;"
|
||||||
class=topbut></div></button>'''.format(tip))
|
class=topbut></div></button>'''.format(tip))
|
||||||
tip = _("Change colour (F8)")
|
tip = _("Change colour (F8)")
|
||||||
righttopbtns.append('''<button tabindex=-1 class=linkb title="{}"
|
righttopbtns.append(
|
||||||
type="button" onclick="pycmd('changeCol');return false;">
|
'''<button tabindex=-1 class=linkb title="{}"
|
||||||
<div style="display:inline-block; border-radius: 5px;"
|
type="button" onclick="pycmd('changeCol');return false;">
|
||||||
class="topbut rainbow"></div></button>'''.format(tip))
|
<div style="display:inline-block; border-radius: 5px;"
|
||||||
righttopbtns.append(self._addButton('text_cloze', 'cloze', _("Cloze deletion (Ctrl+Shift+C)")))
|
class="topbut rainbow"></div></button>'''.format(tip),
|
||||||
righttopbtns.append(self._addButton('paperclip', 'attach', _("Attach pictures/audio/video (F3)")))
|
self._addButton('text_cloze', 'cloze',
|
||||||
righttopbtns.append(self._addButton('media-record', 'record', _("Record audio (F5)")))
|
_("Cloze deletion (Ctrl+Shift+C)")),
|
||||||
righttopbtns.append(self._addButton('more', 'more'))
|
self._addButton('paperclip', 'attach',
|
||||||
|
_("Attach pictures/audio/video (F3)")),
|
||||||
|
self._addButton('media-record', 'record', _("Record audio (F5)")),
|
||||||
|
self._addButton('more', 'more')
|
||||||
|
)
|
||||||
righttopbtns = runFilter("setupEditorButtons", righttopbtns, self)
|
righttopbtns = runFilter("setupEditorButtons", righttopbtns, self)
|
||||||
topbuts = """
|
topbuts = """
|
||||||
<div id="topbutsleft" style="float:left;">
|
<div id="topbutsleft" style="float:left;">
|
||||||
|
@ -106,15 +117,13 @@ class Editor:
|
||||||
<div id="topbutsright" style="float:right;">
|
<div id="topbutsright" style="float:right;">
|
||||||
%(rightbts)s
|
%(rightbts)s
|
||||||
</div>
|
</div>
|
||||||
""" % dict(flds=_("Fields"), cards=_("Cards"), rightbts="".join(righttopbtns),
|
""" % dict(flds=_("Fields"), cards=_("Cards"),
|
||||||
|
rightbts="".join(righttopbtns),
|
||||||
fldsTitle=_("Customize Fields"),
|
fldsTitle=_("Customize Fields"),
|
||||||
cardsTitle=shortcut(_("Customize Card Templates (Ctrl+L)")))
|
cardsTitle=shortcut(_("Customize Card Templates (Ctrl+L)")))
|
||||||
bgcol = self.mw.app.palette().window().color().name()
|
bgcol = self.mw.app.palette().window().color().name()
|
||||||
# then load page
|
# then load page
|
||||||
self.web.stdHtml(_html % (
|
self.web.stdHtml(_html % (bgcol, bgcol, topbuts, _("Show Duplicates")),
|
||||||
bgcol, bgcol,
|
|
||||||
topbuts,
|
|
||||||
_("Show Duplicates")),
|
|
||||||
css=["editor.css"],
|
css=["editor.css"],
|
||||||
js=["jquery.js", "editor.js"])
|
js=["jquery.js", "editor.js"])
|
||||||
|
|
||||||
|
@ -322,9 +331,8 @@ class Editor:
|
||||||
if not self.note:
|
if not self.note:
|
||||||
return
|
return
|
||||||
|
|
||||||
data = []
|
data = [(fld, self.mw.col.media.escapeImages(val))
|
||||||
for fld, val in list(self.note.items()):
|
for fld, val in self.note.items()]
|
||||||
data.append((fld, self.mw.col.media.escapeImages(val)))
|
|
||||||
self.widget.show()
|
self.widget.show()
|
||||||
self.updateTags()
|
self.updateTags()
|
||||||
|
|
||||||
|
@ -337,11 +345,12 @@ class Editor:
|
||||||
self.web.setFocus()
|
self.web.setFocus()
|
||||||
runHook("loadNote", self)
|
runHook("loadNote", self)
|
||||||
|
|
||||||
self.web.evalWithCallback("setFields(%s); setFonts(%s); focusField(%s); setNoteId(%s)" % (
|
self.web.evalWithCallback(
|
||||||
json.dumps(data),
|
"setFields(%s); setFonts(%s); focusField(%s); setNoteId(%s)" % (
|
||||||
json.dumps(self.fonts()), json.dumps(focusTo),
|
json.dumps(data),
|
||||||
json.dumps(self.note.id)),
|
json.dumps(self.fonts()), json.dumps(focusTo),
|
||||||
oncallback)
|
json.dumps(self.note.id)),
|
||||||
|
oncallback)
|
||||||
|
|
||||||
def fonts(self):
|
def fonts(self):
|
||||||
return [(runFilter("mungeEditingFontName", f['font']),
|
return [(runFilter("mungeEditingFontName", f['font']),
|
||||||
|
@ -358,10 +367,7 @@ class Editor:
|
||||||
self.web.evalWithCallback("saveNow(%d)" % keepFocus, lambda res: callback())
|
self.web.evalWithCallback("saveNow(%d)" % keepFocus, lambda res: callback())
|
||||||
|
|
||||||
def checkValid(self):
|
def checkValid(self):
|
||||||
cols = []
|
cols = ['#fff'] * len(self.note.fields)
|
||||||
err = None
|
|
||||||
for f in self.note.fields:
|
|
||||||
cols.append("#fff")
|
|
||||||
err = self.note.dupeOrEmpty()
|
err = self.note.dupeOrEmpty()
|
||||||
if err == 2:
|
if err == 2:
|
||||||
cols[0] = "#fcc"
|
cols[0] = "#fcc"
|
||||||
|
@ -374,8 +380,7 @@ class Editor:
|
||||||
contents = stripHTMLMedia(self.note.fields[0])
|
contents = stripHTMLMedia(self.note.fields[0])
|
||||||
browser = aqt.dialogs.open("Browser", self.mw)
|
browser = aqt.dialogs.open("Browser", self.mw)
|
||||||
browser.form.searchEdit.lineEdit().setText(
|
browser.form.searchEdit.lineEdit().setText(
|
||||||
'"dupe:%s,%s"' % (self.note.model()['id'],
|
'"dupe:%s,%s"' % (self.note.model()['id'], contents))
|
||||||
contents))
|
|
||||||
browser.onSearchActivated()
|
browser.onSearchActivated()
|
||||||
|
|
||||||
def fieldsAreBlank(self, previousNote=None):
|
def fieldsAreBlank(self, previousNote=None):
|
||||||
|
@ -767,27 +772,19 @@ to a cloze type first, via Edit>Change Note Type."""))
|
||||||
|
|
||||||
def onAdvanced(self):
|
def onAdvanced(self):
|
||||||
m = QMenu(self.mw)
|
m = QMenu(self.mw)
|
||||||
a = m.addAction(_("MathJax inline"))
|
|
||||||
a.triggered.connect(self.insertMathjaxInline)
|
for text, handler, shortcut in (
|
||||||
a.setShortcut(QKeySequence("Ctrl+M, M"))
|
(_("MathJax inline"), self.insertMathjaxInline, "Ctrl+M, M"),
|
||||||
a = m.addAction(_("MathJax block"))
|
(_("MathJax block"), self.insertMathjaxBlock, "Ctrl+M, E"),
|
||||||
a.triggered.connect(self.insertMathjaxBlock)
|
(_("MathJax chemistry"), self.insertMathjaxChemistry, "Ctrl+M, C"),
|
||||||
a.setShortcut(QKeySequence("Ctrl+M, E"))
|
(_("LaTeX"), self.insertLatex, "Ctrl+T, T"),
|
||||||
a = m.addAction(_("MathJax chemistry"))
|
(_("LaTeX equation"), self.insertLatexEqn, "Ctrl+T, E"),
|
||||||
a.triggered.connect(self.insertMathjaxChemistry)
|
(_("LaTeX math env."), self.insertLatexMathEnv, "Ctrl+T, M"),
|
||||||
a.setShortcut(QKeySequence("Ctrl+M, C"))
|
(_("Edit HTML"), self.onHtmlEdit, "Ctrl+Shift+X")
|
||||||
a = m.addAction(_("LaTeX"))
|
):
|
||||||
a.triggered.connect(self.insertLatex)
|
a = m.addAction(text)
|
||||||
a.setShortcut(QKeySequence("Ctrl+T, T"))
|
a.triggered.connect(handler)
|
||||||
a = m.addAction(_("LaTeX equation"))
|
a.setShortcut(QKeySequence(shortcut))
|
||||||
a.triggered.connect(self.insertLatexEqn)
|
|
||||||
a.setShortcut(QKeySequence("Ctrl+T, E"))
|
|
||||||
a = m.addAction(_("LaTeX math env."))
|
|
||||||
a.triggered.connect(self.insertLatexMathEnv)
|
|
||||||
a.setShortcut(QKeySequence("Ctrl+T, M"))
|
|
||||||
a = m.addAction(_("Edit HTML"))
|
|
||||||
a.triggered.connect(self.onHtmlEdit)
|
|
||||||
a.setShortcut(QKeySequence("Ctrl+Shift+X"))
|
|
||||||
|
|
||||||
qtMenuShortcutWorkaround(m)
|
qtMenuShortcutWorkaround(m)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue