From 375a96e2638fd140178a0abcc2077bd07eabd9c0 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Mon, 28 Jun 2021 14:58:15 +0200 Subject: [PATCH] Set tags from Python --- qt/aqt/editor.py | 3 ++- ts/editor/TagEditor.svelte | 29 ++++++++++++++++------------- ts/editor/index.ts | 4 ++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index b15c1aae2..6eddf3159 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -481,12 +481,13 @@ $editorToolbar.then(({{ toolbar }}) => toolbar.appendGroup({{ 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);" % ( + js = "setFields(%s); setFonts(%s); focusField(%s); setNoteId(%s); setColorButtons(%s); setTags(%s); " % ( json.dumps(data), json.dumps(self.fonts()), json.dumps(focusTo), json.dumps(self.note.id), json.dumps([text_color, highlight_color]), + json.dumps(self.mw.col.tags.canonify(self.note.tags)), ) if self.addMode: diff --git a/ts/editor/TagEditor.svelte b/ts/editor/TagEditor.svelte index c11bd1bb0..b4f742b89 100644 --- a/ts/editor/TagEditor.svelte +++ b/ts/editor/TagEditor.svelte @@ -14,25 +14,20 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import type { Tag as TagType } from "./tags"; import { attachId, getName } from "./tags"; - export let initialNames = ["en::foobar", "test", "def"]; - export let suggestions = ["en::idioms", "anki::functionality", "math"]; - export let size = isApplePlatform() ? 1.6 : 2.0; - let input: HTMLInputElement; - let tags = initialNames.map(attachId); + export let suggestions = ["en::idioms", "anki::functionality", "math"]; - function isFirst(index: number): boolean { - return index === 0; - } + export let tags: TagType[] = []; - function isLast(index: number): boolean { - return index === tags.length - 1; + export function resetTags(names: string[]) { + tags = names.map(attachId); } let active: number | null = null; let activeAfterBlur: number | null = null; let activeName = ""; + let activeInput: HTMLInputElement; function onAutocomplete({ detail }) { const activeTag = tags[active!]; @@ -114,7 +109,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // splitOff tag was rejected return; } - input.setSelectionRange(0, 0); + activeInput.setSelectionRange(0, 0); } function insertTag(index: number): void { @@ -136,6 +131,14 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html return deleted; } + function isFirst(index: number): boolean { + return index === 0; + } + + function isLast(index: number): boolean { + return index === tags.length - 1; + } + function joinWithPreviousTag(index: number): void { if (isFirst(index)) { return; @@ -182,7 +185,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html active = null; await tick(); - input.setSelectionRange(0, 0); + activeInput.setSelectionRange(0, 0); } function deleteTagIfNotUnique(tag: TagType, index: number): void { @@ -220,7 +223,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html (activeName = tag.name)} on:keydown={updateAutocomplete} on:input={() => updateWithTagName(tag)} diff --git a/ts/editor/index.ts b/ts/editor/index.ts index 4d9c159da..95e41f7ad 100644 --- a/ts/editor/index.ts +++ b/ts/editor/index.ts @@ -195,6 +195,10 @@ export function setFormat(cmd: string, arg?: string, nosave = false): void { } } +export function setTags(tags: string[]): void { + $tagEditor.then((tagEditor: TagEditor): void => tagEditor.resetTags(tags)); +} + export const i18n = setupI18n({ modules: [ ModuleName.EDITING,