Set tags from Python

This commit is contained in:
Henrik Giesel 2021-06-28 14:58:15 +02:00
parent a034c93eb7
commit 375a96e263
3 changed files with 22 additions and 14 deletions

View file

@ -481,12 +481,13 @@ $editorToolbar.then(({{ toolbar }}) => toolbar.appendGroup({{
text_color = self.mw.pm.profile.get("lastTextColor", "#00f") text_color = self.mw.pm.profile.get("lastTextColor", "#00f")
highlight_color = self.mw.pm.profile.get("lastHighlightColor", "#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(data),
json.dumps(self.fonts()), json.dumps(self.fonts()),
json.dumps(focusTo), json.dumps(focusTo),
json.dumps(self.note.id), json.dumps(self.note.id),
json.dumps([text_color, highlight_color]), json.dumps([text_color, highlight_color]),
json.dumps(self.mw.col.tags.canonify(self.note.tags)),
) )
if self.addMode: if self.addMode:

View file

@ -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 type { Tag as TagType } from "./tags";
import { attachId, getName } 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; export let size = isApplePlatform() ? 1.6 : 2.0;
let input: HTMLInputElement; export let suggestions = ["en::idioms", "anki::functionality", "math"];
let tags = initialNames.map(attachId);
function isFirst(index: number): boolean { export let tags: TagType[] = [];
return index === 0;
}
function isLast(index: number): boolean { export function resetTags(names: string[]) {
return index === tags.length - 1; tags = names.map(attachId);
} }
let active: number | null = null; let active: number | null = null;
let activeAfterBlur: number | null = null; let activeAfterBlur: number | null = null;
let activeName = ""; let activeName = "";
let activeInput: HTMLInputElement;
function onAutocomplete({ detail }) { function onAutocomplete({ detail }) {
const activeTag = tags[active!]; 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 // splitOff tag was rejected
return; return;
} }
input.setSelectionRange(0, 0); activeInput.setSelectionRange(0, 0);
} }
function insertTag(index: number): void { 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; 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 { function joinWithPreviousTag(index: number): void {
if (isFirst(index)) { if (isFirst(index)) {
return; return;
@ -182,7 +185,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
active = null; active = null;
await tick(); await tick();
input.setSelectionRange(0, 0); activeInput.setSelectionRange(0, 0);
} }
function deleteTagIfNotUnique(tag: TagType, index: number): void { 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
<TagInput <TagInput
id={tag.id} id={tag.id}
bind:name={activeName} bind:name={activeName}
bind:input bind:input={activeInput}
on:focus={() => (activeName = tag.name)} on:focus={() => (activeName = tag.name)}
on:keydown={updateAutocomplete} on:keydown={updateAutocomplete}
on:input={() => updateWithTagName(tag)} on:input={() => updateWithTagName(tag)}

View file

@ -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({ export const i18n = setupI18n({
modules: [ modules: [
ModuleName.EDITING, ModuleName.EDITING,