fix: native template autocomplete and durable indicator migration
Template file fields used an unstyled <datalist>, not Obsidian's native suggest popup. Replace it with TemplateFileSuggest, an AbstractInputSuggest<TFile> matching Templater's own FileSuggest/FolderSuggest pattern exactly. Requires Obsidian >=1.4.10; minAppVersion corrected. The coloured-indicator default never reached existing installs: every install that loaded before that default changed had already persisted the prior default (indicatorMode: 'any') to data.json as if it were a deliberate choice, and Object.assign-based settings merging always prefers a value already on disk over a new code default. Hand-editing data.json directly does not fix this durably either -- Obsidian holds settings in memory while a vault is open and overwrites external edits on its own save cycle. Add a one-time, code-level migration: on load, an install with a persisted indicatorMode of 'any' and no indicatorModeMigrated flag is promoted to 'systems' and the flag is persisted immediately, so it survives even a session with no further settings changes. Any indicatorMode chosen after the flag exists, any (deliberately) included, is never touched again.
This commit is contained in:
1 parent
e8ba4cd2ad
commit
621bef0d29
5 files changed
+94
-59
No files matched your search
+39
-35
@@ -1,8 +1,36 @@
|
||||
import { Setting, PluginSettingTab, App, setIcon } from 'obsidian';
|
||||
import { Setting, PluginSettingTab, App, setIcon, AbstractInputSuggest, TFile } from 'obsidian';
|
||||
import type WaypointPlugin from 'src/main';
|
||||
import { WaypointSettings, DEFAULT_SETTINGS, DEFAULT_DATE_SYSTEM, PeriodNoteSettings } from 'src/settings';
|
||||
import { formatHasDateToken } from 'src/utils/date-systems';
|
||||
|
||||
/**
|
||||
* Vault-wide Markdown-file suggester for template-path fields, matching
|
||||
* Obsidian's native suggest popup (same base class Templater's own template
|
||||
* pickers use) rather than the browser's unstyled `<datalist>` dropdown.
|
||||
*/
|
||||
class TemplateFileSuggest extends AbstractInputSuggest<TFile> {
|
||||
constructor(app: App, private inputEl: HTMLInputElement) {
|
||||
super(app, inputEl);
|
||||
}
|
||||
|
||||
getSuggestions(query: string): TFile[] {
|
||||
const q = query.toLowerCase();
|
||||
return this.app.vault.getMarkdownFiles()
|
||||
.filter(file => file.path.toLowerCase().includes(q))
|
||||
.slice(0, 200);
|
||||
}
|
||||
|
||||
renderSuggestion(file: TFile, el: HTMLElement): void {
|
||||
el.setText(file.path.replace(/\.md$/, ''));
|
||||
}
|
||||
|
||||
selectSuggestion(file: TFile): void {
|
||||
this.setValue(file.path.replace(/\.md$/, ''));
|
||||
this.inputEl.trigger('input');
|
||||
this.close();
|
||||
}
|
||||
}
|
||||
|
||||
export class WaypointSettingTab extends PluginSettingTab {
|
||||
private plugin: WaypointPlugin;
|
||||
private settings: WaypointSettings;
|
||||
@@ -129,20 +157,14 @@ export class WaypointSettingTab extends PluginSettingTab {
|
||||
// ═══════════════════════════════
|
||||
|
||||
private renderPeriodicTab(container: HTMLElement): void {
|
||||
const templateSuggestionsId = this.createTemplateSuggestions(container);
|
||||
this.addPeriodNoteSettings(container, 'Daily', this.settings.daily, templateSuggestionsId);
|
||||
this.addPeriodNoteSettings(container, 'Weekly', this.settings.weekly, templateSuggestionsId);
|
||||
this.addPeriodNoteSettings(container, 'Monthly', this.settings.monthly, templateSuggestionsId);
|
||||
this.addPeriodNoteSettings(container, 'Quarterly', this.settings.quarterly, templateSuggestionsId);
|
||||
this.addPeriodNoteSettings(container, 'Yearly', this.settings.yearly, templateSuggestionsId);
|
||||
this.addPeriodNoteSettings(container, 'Daily', this.settings.daily);
|
||||
this.addPeriodNoteSettings(container, 'Weekly', this.settings.weekly);
|
||||
this.addPeriodNoteSettings(container, 'Monthly', this.settings.monthly);
|
||||
this.addPeriodNoteSettings(container, 'Quarterly', this.settings.quarterly);
|
||||
this.addPeriodNoteSettings(container, 'Yearly', this.settings.yearly);
|
||||
}
|
||||
|
||||
private addPeriodNoteSettings(
|
||||
container: HTMLElement,
|
||||
label: string,
|
||||
period: PeriodNoteSettings,
|
||||
templateSuggestionsId: string,
|
||||
): void {
|
||||
private addPeriodNoteSettings(container: HTMLElement, label: string, period: PeriodNoteSettings): void {
|
||||
new Setting(container).setHeading().setName(label);
|
||||
|
||||
new Setting(container)
|
||||
@@ -175,7 +197,7 @@ export class WaypointSettingTab extends PluginSettingTab {
|
||||
.addText((text) => {
|
||||
text.setPlaceholder('Templates/Daily note');
|
||||
text.setValue(period.templateFile);
|
||||
text.inputEl.setAttr('list', templateSuggestionsId);
|
||||
new TemplateFileSuggest(this.app, text.inputEl);
|
||||
text.onChange((value) => {
|
||||
period.templateFile = value;
|
||||
this.saveAndRefresh();
|
||||
@@ -211,8 +233,6 @@ export class WaypointSettingTab extends PluginSettingTab {
|
||||
.setName('Date systems')
|
||||
.setDesc(intro);
|
||||
|
||||
const templateSuggestionsId = this.createTemplateSuggestions(container);
|
||||
|
||||
this.settings.dateSystems.forEach((system, i, arr) => {
|
||||
new Setting(container)
|
||||
.setHeading()
|
||||
@@ -279,7 +299,7 @@ export class WaypointSettingTab extends PluginSettingTab {
|
||||
|
||||
this.addSystemTextSetting(container,
|
||||
'Template file', 'Path to the template file. The .md extension is optional.',
|
||||
system, 'templateFile', 'resources/template/journal', templateSuggestionsId,
|
||||
system, 'templateFile', 'resources/template/journal', true,
|
||||
);
|
||||
this.addSystemTextSetting(container,
|
||||
'Type property', `Fallback value for the 'type' frontmatter property, used when no template is found.`,
|
||||
@@ -327,7 +347,7 @@ export class WaypointSettingTab extends PluginSettingTab {
|
||||
obj: Record<K, string>,
|
||||
key: K,
|
||||
placeholder: string,
|
||||
templateSuggestionsId?: string,
|
||||
suggestTemplates?: boolean,
|
||||
): Setting {
|
||||
return new Setting(container)
|
||||
.setName(name)
|
||||
@@ -335,7 +355,7 @@ export class WaypointSettingTab extends PluginSettingTab {
|
||||
.addText((text) => {
|
||||
text.setPlaceholder(placeholder);
|
||||
text.setValue(obj[key]);
|
||||
if (templateSuggestionsId) text.inputEl.setAttr('list', templateSuggestionsId);
|
||||
if (suggestTemplates) new TemplateFileSuggest(this.app, text.inputEl);
|
||||
text.onChange((value) => {
|
||||
obj[key] = value;
|
||||
this.saveAndRefresh();
|
||||
@@ -343,22 +363,6 @@ export class WaypointSettingTab extends PluginSettingTab {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Native datalist keeps template paths searchable without another custom
|
||||
* modal, and accepts templates stored anywhere in the vault.
|
||||
*/
|
||||
private createTemplateSuggestions(container: HTMLElement): string {
|
||||
const id = 'waypoint-template-suggestions';
|
||||
const list = container.createEl('datalist', { attr: { id } });
|
||||
const paths = this.app.vault.getMarkdownFiles()
|
||||
.map(file => file.path.replace(/\.md$/, ''))
|
||||
.sort((a, b) => a.localeCompare(b));
|
||||
for (const path of paths) {
|
||||
list.createEl('option', { value: path });
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════
|
||||
// Recent Files tab
|
||||
// ═══════════════════════════════
|
||||
|
||||
Reference in new issue
Block a user