Persist field states with SessionOptions object (#2241)
* Persist collapsed- and field states with SessionOptions object * Format types.ts * Replace format function with f-string * Give setters more descriptive parameter names * Do not use default prefix for descriptions and fonts since they are not meant to be changed via Svelte.
This commit is contained in:
parent
e059aab184
commit
d2fa50dd9f
|
@ -523,35 +523,23 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
|
|||
text_color = self.mw.pm.profile.get("lastTextColor", "#00f")
|
||||
highlight_color = self.mw.pm.profile.get("lastHighlightColor", "#00f")
|
||||
|
||||
js = """
|
||||
setFields({});
|
||||
setCollapsed({});
|
||||
setPlainTexts({});
|
||||
setDescriptions({});
|
||||
setFonts({});
|
||||
focusField({});
|
||||
setNoteId({});
|
||||
setColorButtons({});
|
||||
setTags({});
|
||||
setTagsCollapsed({});
|
||||
setMathjaxEnabled({});
|
||||
setShrinkImages({});
|
||||
setCloseHTMLTags({});
|
||||
""".format(
|
||||
json.dumps(data),
|
||||
json.dumps(collapsed),
|
||||
json.dumps(plain_texts),
|
||||
json.dumps(descriptions),
|
||||
json.dumps(self.fonts()),
|
||||
json.dumps(focusTo),
|
||||
json.dumps(self.note.id),
|
||||
json.dumps([text_color, highlight_color]),
|
||||
json.dumps(self.note.tags),
|
||||
json.dumps(self.mw.pm.tags_collapsed(self.editorMode)),
|
||||
json.dumps(self.mw.col.get_config("renderMathjax", True)),
|
||||
json.dumps(self.mw.col.get_config("shrinkEditorImages", True)),
|
||||
json.dumps(self.mw.col.get_config("closeHTMLTags", True)),
|
||||
)
|
||||
js = f"""
|
||||
saveSession();
|
||||
setFields({json.dumps(data)});
|
||||
setNotetypeId({json.dumps(self.note.mid)});
|
||||
setCollapsed({json.dumps(collapsed)});
|
||||
setPlainTexts({json.dumps(plain_texts)});
|
||||
setDescriptions({json.dumps(descriptions)});
|
||||
setFonts({json.dumps(self.fonts())});
|
||||
focusField({json.dumps(focusTo)});
|
||||
setNoteId({json.dumps(self.note.id)});
|
||||
setColorButtons({json.dumps([text_color, highlight_color])});
|
||||
setTags({json.dumps(self.note.tags)});
|
||||
setTagsCollapsed({json.dumps(self.mw.pm.tags_collapsed(self.editorMode))});
|
||||
setMathjaxEnabled({json.dumps(self.mw.col.get_config("renderMathjax", True))});
|
||||
setShrinkImages({json.dumps(self.mw.col.get_config("shrinkEditorImages", True))});
|
||||
setCloseHTMLTags({json.dumps(self.mw.col.get_config("closeHTMLTags", True))});
|
||||
"""
|
||||
|
||||
if self.addMode:
|
||||
sticky = [field["sticky"] for field in self.note.note_type()["flds"]]
|
||||
|
|
|
@ -71,6 +71,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|||
import RichTextInput, { editingInputIsRichText } from "./rich-text-input";
|
||||
import RichTextBadge from "./RichTextBadge.svelte";
|
||||
import SymbolsOverlay from "./symbols-overlay";
|
||||
import type { SessionOptions } from "./types";
|
||||
|
||||
function quoteFontFamily(fontFamily: string): string {
|
||||
// generic families (e.g. sans-serif) must not be quoted
|
||||
|
@ -83,6 +84,20 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|||
const size = 1.6;
|
||||
const wrap = true;
|
||||
|
||||
const sessionOptions: SessionOptions = {};
|
||||
export function saveSession(): void {
|
||||
if (notetypeId) {
|
||||
sessionOptions[notetypeId] = {
|
||||
fieldsCollapsed,
|
||||
fieldStates: {
|
||||
richTextsHidden,
|
||||
plainTextsHidden,
|
||||
plainTextDefaults,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const fieldStores: Writable<string>[] = [];
|
||||
let fieldNames: string[] = [];
|
||||
export function setFields(fs: [string, string][]): void {
|
||||
|
@ -119,18 +134,26 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|||
}
|
||||
|
||||
let fieldsCollapsed: boolean[] = [];
|
||||
export function setCollapsed(fs: boolean[]): void {
|
||||
fieldsCollapsed = fs;
|
||||
export function setCollapsed(defaultCollapsed: boolean[]): void {
|
||||
fieldsCollapsed =
|
||||
sessionOptions[notetypeId!]?.fieldsCollapsed ?? defaultCollapsed;
|
||||
}
|
||||
|
||||
let richTextsHidden: boolean[] = [];
|
||||
let plainTextsHidden: boolean[] = [];
|
||||
let plainTextDefaults: boolean[] = [];
|
||||
|
||||
export function setPlainTexts(fs: boolean[]): void {
|
||||
richTextsHidden = fs;
|
||||
plainTextsHidden = Array.from(fs, (v) => !v);
|
||||
plainTextDefaults = [...richTextsHidden];
|
||||
export function setPlainTexts(defaultPlainTexts: boolean[]): void {
|
||||
const states = sessionOptions[notetypeId!]?.fieldStates;
|
||||
if (states) {
|
||||
richTextsHidden = states.richTextsHidden;
|
||||
plainTextsHidden = states.plainTextsHidden;
|
||||
plainTextDefaults = states.plainTextDefaults;
|
||||
} else {
|
||||
plainTextDefaults = defaultPlainTexts;
|
||||
richTextsHidden = defaultPlainTexts;
|
||||
plainTextsHidden = Array.from(defaultPlainTexts, (v) => !v);
|
||||
}
|
||||
}
|
||||
|
||||
function setMathjaxEnabled(enabled: boolean): void {
|
||||
|
@ -138,8 +161,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|||
}
|
||||
|
||||
let fieldDescriptions: string[] = [];
|
||||
export function setDescriptions(fs: string[]): void {
|
||||
fieldDescriptions = fs;
|
||||
export function setDescriptions(descriptions: string[]): void {
|
||||
fieldDescriptions = descriptions;
|
||||
}
|
||||
|
||||
let fonts: [string, number, boolean][] = [];
|
||||
|
@ -187,6 +210,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|||
noteId = ntid;
|
||||
}
|
||||
|
||||
let notetypeId: number | null = null;
|
||||
export function setNotetypeId(mid: number): void {
|
||||
notetypeId = mid;
|
||||
}
|
||||
|
||||
let insertSymbols = false;
|
||||
|
||||
function setInsertSymbolsEnabled() {
|
||||
|
@ -299,6 +327,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|||
}
|
||||
|
||||
Object.assign(globalThis, {
|
||||
saveSession,
|
||||
setFields,
|
||||
setCollapsed,
|
||||
setPlainTexts,
|
||||
|
@ -313,6 +342,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|||
focusIfField,
|
||||
getNoteId,
|
||||
setNoteId,
|
||||
setNotetypeId,
|
||||
wrap,
|
||||
setMathjaxEnabled,
|
||||
setInsertSymbolsEnabled,
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
// Copyright: Ankitects Pty Ltd and contributors
|
||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
export type EditorOptions = {
|
||||
fieldsCollapsed: boolean[];
|
||||
fieldStates: {
|
||||
richTextsHidden: boolean[];
|
||||
plainTextsHidden: boolean[];
|
||||
plainTextDefaults: boolean[];
|
||||
};
|
||||
};
|
||||
|
||||
export type SessionOptions = {
|
||||
[key: number]: EditorOptions;
|
||||
};
|
Loading…
Reference in New Issue