From 4db7cebf624bc59dc692da5d894b21112a78b4c7 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Mon, 2 Aug 2021 23:12:00 +0200 Subject: [PATCH] Remember last text and highlight color again --- qt/aqt/editor.py | 14 +++++++++++++- qt/aqt/profiles.py | 4 +++- ts/editor/ColorButtons.svelte | 21 +++++++++++++-------- ts/editor/EditorToolbar.svelte | 5 ++++- ts/editor/index.ts | 7 +++++++ 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index 1cfc433b6..8b7a6406a 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -402,6 +402,14 @@ $editorToolbar.then(({{ toolbar }}) => toolbar.appendGroup({{ return new_state + elif cmd.startswith("lastTextColor"): + (_, textColor) = cmd.split(":", 1) + self.mw.pm.profile["lastTextColor"] = textColor + + elif cmd.startswith("lastHighlightColor"): + (_, highlightColor) = cmd.split(":", 1) + self.mw.pm.profile["lastHighlightColor"] = highlightColor + elif cmd in self._links: self._links[cmd](self) @@ -454,11 +462,15 @@ $editorToolbar.then(({{ toolbar }}) => toolbar.appendGroup({{ self.web.setFocus() gui_hooks.editor_did_load_note(self) - js = "setFields(%s); setFonts(%s); focusField(%s); setNoteId(%s);" % ( + text_color = self.mw.pm.profile.get("lastTextColor", "#00f") + highlight_color = self.mw.pm.profile.get("lastHighlightColor", "#00f") + + js = "setFields(%s); setFonts(%s); focusField(%s); setNoteId(%s); setColorButtons(%s);" % ( json.dumps(data), json.dumps(self.fonts()), json.dumps(focusTo), json.dumps(self.note.id), + json.dumps([text_color, highlight_color]), ) if self.addMode: diff --git a/qt/aqt/profiles.py b/qt/aqt/profiles.py index 2fce8ec77..4cc78dd39 100644 --- a/qt/aqt/profiles.py +++ b/qt/aqt/profiles.py @@ -90,7 +90,8 @@ profileConf: Dict[str, Any] = dict( lastOptimize=intTime(), # editing searchHistory=[], - lastColour="#00f", + lastTextColor="#00f", + lastHighlightColor="#00f", # syncing syncKey=None, syncMedia=True, @@ -100,6 +101,7 @@ profileConf: Dict[str, Any] = dict( importMode=1, # these are not used, but Anki 2.1.42 and below # expect these keys to exist + lastColour="#00f", stripHTML=True, deleteMedia=False, ) diff --git a/ts/editor/ColorButtons.svelte b/ts/editor/ColorButtons.svelte index ae6686302..48a304161 100644 --- a/ts/editor/ColorButtons.svelte +++ b/ts/editor/ColorButtons.svelte @@ -13,10 +13,16 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import WithColorHelper from "./WithColorHelper.svelte"; import OnlyEditable from "./OnlyEditable.svelte"; + import { bridgeCommand } from "lib/bridgecommand"; import { textColorIcon, highlightColorIcon, arrowIcon } from "./icons"; import { appendInParentheses } from "./helpers"; export let api = {}; + export let textColor: string; + export let highlightColor: string; + + $: forecolorWrap = wrapWithForecolor(textColor); + $: backcolorWrap = wrapWithBackcolor(highlightColor); const wrapWithForecolor = (color: string) => () => { document.execCommand("forecolor", false, color); @@ -25,15 +31,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html const wrapWithBackcolor = (color: string) => () => { document.execCommand("backcolor", false, color); }; - - const initialColor = "black"; - - let forecolorWrap = wrapWithForecolor(initialColor); - let backcolorWrap = wrapWithForecolor(initialColor); - + @@ -65,6 +66,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html {@html arrowIcon} { + const textColor = setColor(event); + bridgeCommand(`lastTextColor:${textColor}`); forecolorWrap = wrapWithForecolor(setColor(event)); forecolorWrap(); }} @@ -76,7 +79,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html - + { - backcolorWrap = wrapWithBackcolor(setColor(event)); + const highlightColor = setColor(event); + bridgeCommand(`lastHighlightColor:${highlightColor}`); + backcolorWrap = wrapWithBackcolor(highlightColor); backcolorWrap(); }} /> diff --git a/ts/editor/EditorToolbar.svelte b/ts/editor/EditorToolbar.svelte index bc6afd477..5c3728fd8 100644 --- a/ts/editor/EditorToolbar.svelte +++ b/ts/editor/EditorToolbar.svelte @@ -40,6 +40,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html export let size = isApplePlatform() ? 1.6 : 2.0; export let wrap = true; + export let textColor: string; + export let highlightColor: string; + export const toolbar = {}; export const notetypeButtons = {}; export const formatInlineButtons = {}; @@ -63,7 +66,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html - + diff --git a/ts/editor/index.ts b/ts/editor/index.ts index 2b3325620..e0713e48a 100644 --- a/ts/editor/index.ts +++ b/ts/editor/index.ts @@ -3,6 +3,7 @@ /* eslint @typescript-eslint/no-non-null-assertion: "off", +@typescript-eslint/no-explicit-any: "off", */ import { filterHTML } from "html-filter"; @@ -169,6 +170,12 @@ export function setFonts(fonts: [string, number, boolean][]): void { ); } +export function setColorButtons([textColor, highlightColor]: [string, string]): void { + $editorToolbar.then((editorToolbar) => + (editorToolbar as any).$set({ textColor, highlightColor }) + ); +} + export function setSticky(stickies: boolean[]): void { forEditorField(stickies, (field: EditorField, isSticky: boolean) => { field.labelContainer.activateSticky(isSticky);