mirror of
https://github.com/ankitects/anki.git
synced 2025-09-23 16:26:40 -04:00
latex; save after format update
This commit is contained in:
parent
c08e810503
commit
cf6fccf454
1 changed files with 43 additions and 57 deletions
100
aqt/editor.py
100
aqt/editor.py
|
@ -60,8 +60,11 @@ function sendState() {
|
||||||
py.run("state:" + JSON.stringify(r));
|
py.run("state:" + JSON.stringify(r));
|
||||||
};
|
};
|
||||||
|
|
||||||
function setFormat(cmd, arg) {
|
function setFormat(cmd, arg, nosave) {
|
||||||
document.execCommand(cmd, false, arg);
|
document.execCommand(cmd, false, arg);
|
||||||
|
if (!nosave) {
|
||||||
|
saveField('key');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function clearChangeTimer() {
|
function clearChangeTimer() {
|
||||||
|
@ -101,13 +104,16 @@ function saveField(type) {
|
||||||
py.run(type + ":" + currentField.id.substring(1) + ":" + currentField.innerHTML);
|
py.run(type + ":" + currentField.id.substring(1) + ":" + currentField.innerHTML);
|
||||||
};
|
};
|
||||||
|
|
||||||
function cloze() {
|
function wrap(front, back) {
|
||||||
var s = window.getSelection()
|
setFormat('removeFormat', null, true);
|
||||||
var r = s.getRangeAt(0).cloneContents();
|
var s = window.getSelection();
|
||||||
var c = document.createElement('div');
|
var r = s.getRangeAt(0);
|
||||||
c.appendChild(r);
|
var content = r.extractContents();
|
||||||
var txt = c.innerHTML;
|
var span = document.createElement("span")
|
||||||
py.run("cloze:" + currentField.id.substring(1) + ":" + txt);
|
span.appendChild(content);
|
||||||
|
s.removeAllRanges();
|
||||||
|
s.addRange(r);
|
||||||
|
setFormat('inserthtml', front + span.innerHTML + back);
|
||||||
};
|
};
|
||||||
|
|
||||||
function setFields(fields) {
|
function setFields(fields) {
|
||||||
|
@ -217,7 +223,7 @@ class Editor(object):
|
||||||
if not text:
|
if not text:
|
||||||
b.setIcon(QIcon(":/icons/%s.png" % name))
|
b.setIcon(QIcon(":/icons/%s.png" % name))
|
||||||
if key:
|
if key:
|
||||||
b.setShortcut(key)
|
b.setShortcut(QKeySequence(key))
|
||||||
if tip:
|
if tip:
|
||||||
b.setToolTip(tip)
|
b.setToolTip(tip)
|
||||||
if check:
|
if check:
|
||||||
|
@ -258,21 +264,15 @@ class Editor(object):
|
||||||
but = b("cloze", self.onCloze, "F9", _("Cloze (F9)"), text="[...]")
|
but = b("cloze", self.onCloze, "F9", _("Cloze (F9)"), text="[...]")
|
||||||
but.setFixedWidth(24)
|
but.setFixedWidth(24)
|
||||||
# fixme: better image names
|
# fixme: better image names
|
||||||
but = b("text-speak", self.onAddMedia, "F3", _("Add audio/video (F4)"))
|
b("text-speak", self.onAddMedia, "F3", _("Add audio/video (F4)"))
|
||||||
but = b("media-record", self.onRecSound, "F5", _("Record audio (F5)"))
|
b("media-record", self.onRecSound, "F5", _("Record audio (F5)"))
|
||||||
but = b("tex", self.latexMenu, "Ctrl+t", _("LaTeX (Ctrl+t)"))
|
b("tex", self.insertLatex, "Ctrl+t, t", _("LaTeX (Ctrl+t then t)"))
|
||||||
# insertLatex, insertLatexEqn, insertLatexMathEnv
|
b("math_sqrt", self.insertLatexEqn, "Ctrl+t, e",
|
||||||
|
_("LaTeX equation (Ctrl+t then e)"))
|
||||||
|
b("math_matrix", self.insertLatexMathEnv, "Ctrl+t, m",
|
||||||
|
_("LaTeX math environment (Ctrl+t then m)"))
|
||||||
but = b("text-xml", self.onHtmlEdit, "Ctrl+x", _("Source (Ctrl+x)"))
|
but = b("text-xml", self.onHtmlEdit, "Ctrl+x", _("Source (Ctrl+x)"))
|
||||||
|
|
||||||
def setupForegroundButton(self, but):
|
|
||||||
self.foregroundFrame = QFrame()
|
|
||||||
self.foregroundFrame.setAutoFillBackground(True)
|
|
||||||
self.colourChanged()
|
|
||||||
hbox = QHBoxLayout()
|
|
||||||
hbox.addWidget(self.foregroundFrame)
|
|
||||||
hbox.setMargin(5)
|
|
||||||
but.setLayout(hbox)
|
|
||||||
|
|
||||||
def enableButtons(self, val=True):
|
def enableButtons(self, val=True):
|
||||||
for b in self._buttons.values():
|
for b in self._buttons.values():
|
||||||
b.setEnabled(val)
|
b.setEnabled(val)
|
||||||
|
@ -515,9 +515,15 @@ class Editor(object):
|
||||||
self.web.eval("setFormat('underline');")
|
self.web.eval("setFormat('underline');")
|
||||||
|
|
||||||
def toggleSuper(self, bool):
|
def toggleSuper(self, bool):
|
||||||
|
if self._buttons['text_sub'].isChecked():
|
||||||
|
self._buttons['text_sub'].setChecked(False)
|
||||||
|
self.toggleSub(None)
|
||||||
self.web.eval("setFormat('superscript');")
|
self.web.eval("setFormat('superscript');")
|
||||||
|
|
||||||
def toggleSub(self, bool):
|
def toggleSub(self, bool):
|
||||||
|
# if self._buttons['text_super'].isChecked():
|
||||||
|
# self._buttons['text_super'].setChecked(False)
|
||||||
|
# self.toggleSuper(None)
|
||||||
self.web.eval("setFormat('subscript');")
|
self.web.eval("setFormat('subscript');")
|
||||||
|
|
||||||
def removeFormat(self):
|
def removeFormat(self):
|
||||||
|
@ -530,6 +536,15 @@ class Editor(object):
|
||||||
# Foreground colour
|
# Foreground colour
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
def setupForegroundButton(self, but):
|
||||||
|
self.foregroundFrame = QFrame()
|
||||||
|
self.foregroundFrame.setAutoFillBackground(True)
|
||||||
|
self.colourChanged()
|
||||||
|
hbox = QHBoxLayout()
|
||||||
|
hbox.addWidget(self.foregroundFrame)
|
||||||
|
hbox.setMargin(5)
|
||||||
|
but.setLayout(hbox)
|
||||||
|
|
||||||
def _updateForegroundButton(self, txtcol):
|
def _updateForegroundButton(self, txtcol):
|
||||||
self.foregroundFrame.setPalette(QPalette(QColor(txtcol)))
|
self.foregroundFrame.setPalette(QPalette(QColor(txtcol)))
|
||||||
self.foregroundFrame.setStyleSheet("* {background-color: %s}" %
|
self.foregroundFrame.setStyleSheet("* {background-color: %s}" %
|
||||||
|
@ -683,50 +698,23 @@ class Editor(object):
|
||||||
# LaTeX
|
# LaTeX
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
def latexMenu(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def insertLatex(self):
|
def insertLatex(self):
|
||||||
w = self.focusedEdit()
|
self.mw.deck.media.dir(create=True)
|
||||||
if w:
|
self.web.eval("wrap('[latex]', '[/latex]');")
|
||||||
selected = w.textCursor().selectedText()
|
|
||||||
self.deck.mediaDir(create=True)
|
|
||||||
cur = w.textCursor()
|
|
||||||
pos = cur.position()
|
|
||||||
w.insertHtml("[latex]%s[/latex]" % selected)
|
|
||||||
cur.setPosition(pos+7)
|
|
||||||
w.setTextCursor(cur)
|
|
||||||
|
|
||||||
def insertLatexEqn(self):
|
def insertLatexEqn(self):
|
||||||
w = self.focusedEdit()
|
self.mw.deck.media.dir(create=True)
|
||||||
if w:
|
self.web.eval("wrap('[$]', '[/$]');")
|
||||||
selected = w.textCursor().selectedText()
|
|
||||||
self.deck.mediaDir(create=True)
|
|
||||||
cur = w.textCursor()
|
|
||||||
pos = cur.position()
|
|
||||||
w.insertHtml("[$]%s[/$]" % selected)
|
|
||||||
cur.setPosition(pos+3)
|
|
||||||
w.setTextCursor(cur)
|
|
||||||
|
|
||||||
def insertLatexMathEnv(self):
|
def insertLatexMathEnv(self):
|
||||||
w = self.focusedEdit()
|
self.mw.deck.media.dir(create=True)
|
||||||
if w:
|
self.web.eval("wrap('[$$]', '[/$$]');")
|
||||||
selected = w.textCursor().selectedText()
|
|
||||||
self.deck.mediaDir(create=True)
|
|
||||||
cur = w.textCursor()
|
|
||||||
pos = cur.position()
|
|
||||||
w.insertHtml("[$$]%s[/$$]" % selected)
|
|
||||||
cur.setPosition(pos+4)
|
|
||||||
w.setTextCursor(cur)
|
|
||||||
|
|
||||||
# Pasting, drag & drop, and keyboard layouts
|
# Pasting, drag & drop, and keyboard layouts
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
class EditorWebView(AnkiWebView):
|
class EditorWebView(AnkiWebView):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, parent, editor):
|
def __init__(self, parent, editor):
|
||||||
AnkiWebView.__init__(self, parent)
|
AnkiWebView.__init__(self, parent)
|
||||||
self.editor = editor
|
self.editor = editor
|
||||||
|
@ -736,8 +724,6 @@ class EditorWebView(AnkiWebView):
|
||||||
# if sys.platform.startswith("win32"):
|
# if sys.platform.startswith("win32"):
|
||||||
# self._ownLayout = None
|
# self._ownLayout = None
|
||||||
|
|
||||||
# after the drop/copy, make sure data updated?
|
|
||||||
|
|
||||||
def keyPressEvent(self, evt):
|
def keyPressEvent(self, evt):
|
||||||
self._curKey = True
|
self._curKey = True
|
||||||
if evt.matches(QKeySequence.Paste):
|
if evt.matches(QKeySequence.Paste):
|
||||||
|
|
Loading…
Reference in a new issue