white7292_obsidian-hd-unfil.../main.ts

155 lines
4 KiB
TypeScript
Raw Normal View History

2023-03-26 14:00:09 +00:00
import { App, MarkdownView, Plugin, PluginSettingTab, Setting } from "obsidian";
2022-04-15 18:13:31 +00:00
// Remember to rename these classes and interfaces!
2023-03-26 14:00:09 +00:00
interface EmptyStatsHighlighterSettings {
unfilledStatPrefix: string;
templatesDirectory: string;
2022-04-15 18:13:31 +00:00
}
2023-03-26 14:00:09 +00:00
const DEFAULT_SETTINGS: EmptyStatsHighlighterSettings = {
unfilledStatPrefix: "==",
templatesDirectory: "Templates",
};
2022-04-15 18:13:31 +00:00
2023-03-26 14:00:09 +00:00
export default class EmptyStatsHighlighter extends Plugin {
settings: EmptyStatsHighlighterSettings;
2022-04-15 18:13:31 +00:00
2023-03-26 14:00:09 +00:00
// Runs whenever the user starts using the plugin in Obsidian.
// This is where you'll configure most of the plugin's capabilities.
// This function is also called when the plugin is updated.
2022-04-15 18:13:31 +00:00
async onload() {
await this.loadSettings();
// This adds a settings tab so the user can configure various aspects of the plugin
2023-03-26 14:00:09 +00:00
this.addSettingTab(new SettingsTab(this.app, this));
2022-04-15 18:13:31 +00:00
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
// Using this function will automatically remove the event listener when this plugin is disabled.
2023-03-26 14:00:09 +00:00
this.registerDomEvent(document, "click", (evt: MouseEvent) => {
// console.log("click", evt);
const activeFile = this.app.workspace.getActiveFile();
if (
!activeFile?.path.contains(
`${this.settings.templatesDirectory}/`
)
) {
const view =
this.app.workspace.getActiveViewOfType(MarkdownView);
// Make sure the user is editing a Markdown file.
if (view) {
const editor = view.editor;
if (editor) {
for (let i = 0; i < editor.lineCount(); i++) {
const line = editor.getLine(i);
if (
!line.startsWith(
this.settings.unfilledStatPrefix
) &&
line.match("^.*\\: $")
) {
editor.setLine(
i,
`${this.settings.unfilledStatPrefix}${line}`
);
}
if (
line.startsWith(
this.settings.unfilledStatPrefix
) &&
!line.match("^.*\\: $")
) {
editor.setLine(
i,
line.substring(
this.settings.unfilledStatPrefix.length
)
);
}
}
}
}
}
2022-04-15 18:13:31 +00:00
});
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
2023-03-26 14:00:09 +00:00
this.registerInterval(
window.setInterval(() => console.log("setInterval"), 5 * 60 * 1000)
);
2022-04-15 18:13:31 +00:00
}
2023-03-26 14:00:09 +00:00
// Runs when the plugin is disabled.
// Any resources that your plugin is using must be released here to avoid
// affecting the performance of Obsidian after your plugin has been disabled.
onunload() {}
2022-04-15 18:13:31 +00:00
async loadSettings() {
2023-03-26 14:00:09 +00:00
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
2022-04-15 18:13:31 +00:00
}
async saveSettings() {
await this.saveData(this.settings);
}
}
2023-03-26 14:00:09 +00:00
class SettingsTab extends PluginSettingTab {
plugin: EmptyStatsHighlighter;
2022-04-15 18:13:31 +00:00
2023-03-26 14:00:09 +00:00
constructor(app: App, plugin: EmptyStatsHighlighter) {
2022-04-15 18:13:31 +00:00
super(app, plugin);
this.plugin = plugin;
}
display(): void {
2023-03-26 14:00:09 +00:00
const { containerEl } = this;
2022-04-15 18:13:31 +00:00
containerEl.empty();
2023-03-26 14:00:09 +00:00
containerEl.createEl("h2", {
text: "Empty Stats Highlighter Settings",
});
new Setting(containerEl)
.setName("Unfilled Stat Prefix")
.setDesc("Prefixes stats that haven't been filled out yet.")
.addText((text) =>
text
.setPlaceholder("Prefix")
.setValue(this.plugin.settings.unfilledStatPrefix)
.onChange(async (value) => {
// const oldPrefix =
// this.plugin.settings.unfilledStatPrefix;
this.plugin.settings.unfilledStatPrefix = value;
await this.plugin.saveSettings();
// TODO: Remove old prefixes.
})
);
2022-04-15 18:13:31 +00:00
new Setting(containerEl)
2023-03-26 14:00:09 +00:00
.setName("Templates Directory Name")
.setDesc(
"The folder that contains your templates (we won't highlight files here)."
)
.addText((text) =>
text
.setPlaceholder("Directory")
.setValue(this.plugin.settings.templatesDirectory)
.onChange(async (value) => {
this.plugin.settings.templatesDirectory = value;
await this.plugin.saveSettings();
})
);
2022-04-15 18:13:31 +00:00
}
}