From 81ed10faee1a8d1c4954e551acb73f6cac53ae30 Mon Sep 17 00:00:00 2001 From: Simone Gaiarin Date: Sat, 7 Jan 2017 14:21:55 +0100 Subject: [PATCH 1/4] Add id param name and fix typo on duperscript id --- aqt/editor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aqt/editor.py b/aqt/editor.py index de3d39616..ac7168294 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -441,8 +441,8 @@ class Editor(object): righttopbtns = list() righttopbtns.append(self._addButton('text_bold', 'bold', "Bold text (Ctrl+B)", id='bold')) righttopbtns.append(self._addButton('text_italic', 'italic', "Italic text (Ctrl+I)", id='italic')) - righttopbtns.append(self._addButton('text_under', 'underline', "Underline text (Ctrl+U)", 'underline')) - righttopbtns.append(self._addButton('text_super', 'super', "Superscript (Ctrl+Shift+=)", 'superscipt')) + righttopbtns.append(self._addButton('text_under', 'underline', "Underline text (Ctrl+U)", id='underline')) + righttopbtns.append(self._addButton('text_super', 'super', "Superscript (Ctrl+Shift+=)", id='superscript')) righttopbtns.append(self._addButton('text_sub', 'sub', "Subscript (Ctrl+=)", id='subscript')) righttopbtns.append(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 From 67246f8c803f110e0260988f3f7ef04c3f61d48c Mon Sep 17 00:00:00 2001 From: Simone Gaiarin Date: Sun, 8 Jan 2017 13:52:33 +0100 Subject: [PATCH 2/4] Add method to convert resource to data uri, add button image with this --- aqt/editor.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/aqt/editor.py b/aqt/editor.py index ac7168294..c9045021c 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -8,6 +8,8 @@ import ctypes import urllib.request, urllib.parse, urllib.error import warnings import html +import mimetypes +import base64 from anki.lang import _ from aqt.qt import * @@ -476,9 +478,17 @@ class Editor(object): # Top buttons ###################################################################### + def resourceToData(self, path): + """Convert a file (specified by a path) into a data URI.""" + mime, _ = mimetypes.guess_type(path) + with open(path, 'rb') as fp: + data = fp.read() + data64 = b''.join(base64.encodestring(data).splitlines()) + return 'data:%s;base64,%s' % (mime, data64.decode('ascii')) + def _addButton(self, icon, cmd, tip="", id=None): if os.path.isabs(icon): - iconstr = icon + iconstr = self.resourceToData(icon) else: iconstr = "qrc:/icons/{}.png".format(icon) if id: From a5c24aaca01fe29e9384b4960c9d868d8088f951 Mon Sep 17 00:00:00 2001 From: Simone Gaiarin Date: Sun, 8 Jan 2017 14:33:45 +0100 Subject: [PATCH 3/4] Add option to make button toggleable --- aqt/editor.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/aqt/editor.py b/aqt/editor.py index c9045021c..9ade7b5ac 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -125,6 +125,14 @@ function updateButtonState() { // 'col': document.queryCommandValue("forecolor") }; +function toggleEditorButton(buttonid) { + if ($(buttonid).hasClass("highlighted")) { + $(buttonid).removeClass("highlighted"); + } else { + $(buttonid).addClass("highlighted"); + } +}; + function setFormat(cmd, arg, nosave) { document.execCommand(cmd, false, arg); if (!nosave) { @@ -486,7 +494,7 @@ class Editor(object): data64 = b''.join(base64.encodestring(data).splitlines()) return 'data:%s;base64,%s' % (mime, data64.decode('ascii')) - def _addButton(self, icon, cmd, tip="", id=None): + def _addButton(self, icon, cmd, tip="", id=None, toggleable=False): if os.path.isabs(icon): iconstr = self.resourceToData(icon) else: @@ -495,8 +503,12 @@ class Editor(object): idstr = 'id={}'.format(id) else: idstr = "" - return ''''''.format(icon=iconstr, cmd=cmd, tip=_(tip), id=idstr) + if toggleable: + toggleScript = 'toggleEditorButton(this);' + else: + toggleScript = '' + return ''''''.format(icon=iconstr, cmd=cmd, tip=_(tip), id=idstr, togglesc=toggleScript) def setupShortcuts(self): cuts = [ From b57ee28ae7a92cfc70fcce7ab28346bd42885ed9 Mon Sep 17 00:00:00 2001 From: Simone Gaiarin Date: Sat, 14 Jan 2017 21:16:50 +0100 Subject: [PATCH 4/4] Add check on file existence --- aqt/editor.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/aqt/editor.py b/aqt/editor.py index 9ade7b5ac..f687da67a 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -488,6 +488,8 @@ class Editor(object): def resourceToData(self, path): """Convert a file (specified by a path) into a data URI.""" + if not os.path.exists(path): + raise FileNotFoundError mime, _ = mimetypes.guess_type(path) with open(path, 'rb') as fp: data = fp.read()