From e80f43e8fcbfcf883efcc355d8fb40928ac9a62d Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Thu, 6 May 2021 16:10:26 +0200 Subject: [PATCH] Enable setting of PreviewButton and hiding of cloze button again --- qt/aqt/browser/browser.py | 14 +---------- qt/aqt/editor.py | 13 ++++------ ts/components/ButtonGroup.svelte | 24 ++++++++++++------- ts/components/ButtonGroupItem.svelte | 3 ++- .../{RawButton.svelte => Raw.svelte} | 5 ---- ts/components/WithTheming.svelte | 3 ++- ts/editor/ColorButtons.svelte | 4 +++- ts/editor/EditorToolbar.svelte | 21 ++++++++++++---- ts/editor/PreviewButton.svelte | 23 ++++++++++++++++++ ts/editor/TemplateButtons.svelte | 6 +++-- ts/editor/index.ts | 4 +++- 11 files changed, 74 insertions(+), 46 deletions(-) rename ts/components/{RawButton.svelte => Raw.svelte} (56%) create mode 100644 ts/editor/PreviewButton.svelte diff --git a/qt/aqt/browser/browser.py b/qt/aqt/browser/browser.py index d5fa1436d..9d67630bf 100644 --- a/qt/aqt/browser/browser.py +++ b/qt/aqt/browser/browser.py @@ -383,23 +383,11 @@ class Browser(QMainWindow): editor._links["preview"] = lambda _editor: self.onTogglePreview() editor.web.eval( - f""" -$editorToolbar.then(({{ addButton }}) => addButton(editorToolbar.labelButton({{ - label: `{tr.actions_preview()}`, - tooltip: `{tr.browsing_preview_selected_card(val=shortcut(preview_shortcut))}`, - onClick: () => bridgeCommand("preview"), - disables: false, -}}), "notetype", -1)); -""" + f"$editorToolbar.then(({{ notetypeButtons }}) => notetypeButtons.appendButton({{ component: editorToolbar.PreviewButton }}, -1));" ) - def add_preview_shortcut(cuts: List[Tuple], editor: Editor) -> None: - cuts.append(("Ctrl+Shift+P", self.onTogglePreview, True)) - gui_hooks.editor_did_init.append(add_preview_button) - gui_hooks.editor_did_init_shortcuts.append(add_preview_shortcut) self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self) - gui_hooks.editor_did_init_shortcuts.remove(add_preview_shortcut) gui_hooks.editor_did_init.remove(add_preview_button) @ensure_editor_saved diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index 338868cf2..62bf0b9a3 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -145,7 +145,7 @@ class Editor: gui_hooks.editor_did_init_left_buttons(lefttopbtns, self) lefttopbtns_defs = [ - f"$editorToolbar.then(({{ addButton }}) => addButton(editorToolbar.rawButton({{ html: `{button}` }}), 'notetype', -1));" + f"$editorToolbar.then(({{ notetypeButtons }}) => notetypeButtons.addButton({{ component: editorToolbar.Raw, props: {{ html: `{button}` }} }}, -1));" for button in lefttopbtns ] lefttopbtns_js = "\n".join(lefttopbtns_defs) @@ -156,10 +156,7 @@ class Editor: righttopbtns = runFilter("setupEditorButtons", righttopbtns, self) righttopbtns_defs = "\n".join( - [ - f"editorToolbar.rawButton({{ html: `{button}` }})," - for button in righttopbtns - ] + [f"editorToolbar.Raw({{ html: `{button}` }})," for button in righttopbtns] ) righttopbtns_js = ( f""" @@ -172,7 +169,7 @@ $editorToolbar.then(({{ addButton }}) => addButton(editorToolbar.buttonGroup({{ else "" ) - self.web.eval(f"{lefttopbtns_js} {righttopbtns_js}") + self.web.eval(f"{lefttopbtns_js}") # Top buttons ###################################################################### @@ -1268,11 +1265,11 @@ gui_hooks.editor_will_munge_html.append(reverse_url_quoting) def set_cloze_button(editor: Editor) -> None: if editor.note.model()["type"] == MODEL_CLOZE: editor.web.eval( - '$editorToolbar.then(({ showButton }) => showButton("template", "cloze")); ' + '$editorToolbar.then(({ templateButtons }) => templateButtons.showButton("cloze")); ' ) else: editor.web.eval( - '$editorToolbar.then(({ hideButton }) => hideButton("template", "cloze")); ' + '$editorToolbar.then(({ templateButtons }) => templateButtons.hideButton("cloze")); ' ) diff --git a/ts/components/ButtonGroup.svelte b/ts/components/ButtonGroup.svelte index 120618bd1..c8ff5e9ea 100644 --- a/ts/components/ButtonGroup.svelte +++ b/ts/components/ButtonGroup.svelte @@ -51,11 +51,17 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html return registration; } + interface SvelteComponent { + id: string | undefined; + component: SvelteComponentTyped; + props: Record; + } + const dynamicItems: ButtonRegistration[] = []; - let dynamic: SvelteComponentTyped[] = []; + let dynamic: SvelteComponent[] = []; function addButton( - button: SvelteComponentTyped, + button: SvelteComponent, add: (added: Element, parent: Element) => number ): void { const registration = makeRegistration(); @@ -86,10 +92,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html dynamic = [...dynamic, button]; } - const insertButton = (button: SvelteComponentTyped, id: Identifier = 0) => - addButton(button, (added, parent) => insert(added, parent, id)); - const appendButton = (button: SvelteComponentTyped, id: Identifier = -1) => - addButton(button, (added, parent) => add(added, parent, id)); + const insertButton = (button: SvelteComponent, position: Identifier = 0) => + addButton(button, (added, parent) => insert(added, parent, position)); + const appendButton = (button: SvelteComponent, position: Identifier = -1) => + addButton(button, (added, parent) => add(added, parent, position)); function updateRegistration( f: (registration: ButtonRegistration) => void, @@ -137,9 +143,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
- {#each dynamic as component, i} - - + {#each dynamic as item, i} + + {/each}
diff --git a/ts/components/ButtonGroupItem.svelte b/ts/components/ButtonGroupItem.svelte index 70baad1b7..b50e4024d 100644 --- a/ts/components/ButtonGroupItem.svelte +++ b/ts/components/ButtonGroupItem.svelte @@ -12,6 +12,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import { getContext, hasContext } from "svelte"; import { buttonGroupKey } from "./contextKeys"; + export let id: string | undefined = undefined; export let registration: ButtonRegistration | undefined = undefined; let detach_: boolean; @@ -53,7 +54,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html - + diff --git a/ts/components/RawButton.svelte b/ts/components/Raw.svelte similarity index 56% rename from ts/components/RawButton.svelte rename to ts/components/Raw.svelte index cdee475ea..54908eb3e 100644 --- a/ts/components/RawButton.svelte +++ b/ts/components/Raw.svelte @@ -3,12 +3,7 @@ Copyright: Ankitects Pty Ltd and contributors License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html --> {@html html} diff --git a/ts/components/WithTheming.svelte b/ts/components/WithTheming.svelte index 2beae19aa..1f95c2334 100644 --- a/ts/components/WithTheming.svelte +++ b/ts/components/WithTheming.svelte @@ -3,6 +3,7 @@ Copyright: Ankitects Pty Ltd and contributors License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html --> @@ -12,6 +13,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html } -
+
diff --git a/ts/editor/ColorButtons.svelte b/ts/editor/ColorButtons.svelte index 0d5a0ae4a..9c15bb22d 100644 --- a/ts/editor/ColorButtons.svelte +++ b/ts/editor/ColorButtons.svelte @@ -16,6 +16,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import "./color.css"; + export let api = {}; + const foregroundColorKeyword = "--foreground-color"; let color = "black"; @@ -32,7 +34,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html } - + + + + bridgeCommand('preview')} + on:mount={createShortcut}> + {tr.actionsPreview()} + + diff --git a/ts/editor/TemplateButtons.svelte b/ts/editor/TemplateButtons.svelte index bd426ea81..d473bd93a 100644 --- a/ts/editor/TemplateButtons.svelte +++ b/ts/editor/TemplateButtons.svelte @@ -19,6 +19,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import { appendInParentheses } from "./helpers"; import { paperclipIcon, micIcon, functionIcon, xmlIcon } from "./icons"; + export let api = {}; + function onAttachment(): void { bridgeCommand("attach"); } @@ -32,7 +34,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 b6ec76c5b..413fe3404 100644 --- a/ts/editor/index.ts +++ b/ts/editor/index.ts @@ -166,7 +166,9 @@ export function setFormat(cmd: string, arg?: any, nosave: boolean = false): void } } -const i18n = setupI18n({ modules: [ModuleName.EDITING, ModuleName.KEYBOARD] }); +const i18n = setupI18n({ + modules: [ModuleName.EDITING, ModuleName.KEYBOARD, ModuleName.ACTIONS, ModuleName.BROWSING], +}); import type EditorToolbar from "./EditorToolbar.svelte";