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

191 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-03-26 15:24:14 +00:00
import {
App,
MarkdownView,
Plugin,
PluginSettingTab,
Setting,
normalizePath,
} from "obsidian";
interface UnfilledStatsHighlighterSettings {
2023-03-26 14:50:42 +00:00
statRegex: string;
2023-03-26 14:00:09 +00:00
unfilledStatPrefix: string;
templatesDirectory: string;
2023-03-26 14:50:42 +00:00
targetHighlightingDirectory: string;
2022-04-15 18:13:31 +00:00
}
2023-03-26 15:24:14 +00:00
const DEFAULT_SETTINGS: UnfilledStatsHighlighterSettings = {
2023-03-26 14:50:42 +00:00
statRegex: "^.*\\: $",
unfilledStatPrefix: "__",
2023-03-26 14:00:09 +00:00
templatesDirectory: "Templates",
2023-03-26 14:50:42 +00:00
targetHighlightingDirectory: "Journaling",
2023-03-26 14:00:09 +00:00
};
2022-04-15 18:13:31 +00:00
2023-03-26 15:24:14 +00:00
export default class UnfilledStatsHighlighter extends Plugin {
settings: UnfilledStatsHighlighterSettings;
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();
2023-03-26 14:50:42 +00:00
// This adds a settings tab so the user can configure various aspects of
// the plugin.
2023-03-26 15:24:14 +00:00
this.addSettingTab(
new UnfilledStatsHighlighterSettingsTab(this.app, this)
);
2022-04-15 18:13:31 +00:00
2023-03-26 14:50:42 +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.
this.registerEvent(
this.app.workspace.on("editor-change", () => {
const activeFile = this.app.workspace.getActiveFile();
2023-03-26 14:00:09 +00:00
if (
!activeFile?.path.contains(
`${this.settings.templatesDirectory}/`
) &&
activeFile?.path.contains(
`${this.settings.targetHighlightingDirectory}/`
)
) {
const view =
this.app.workspace.getActiveViewOfType(MarkdownView);
2023-03-26 14:00:09 +00:00
// 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);
2023-03-26 14:00:09 +00:00
if (
!line.startsWith(
this.settings.unfilledStatPrefix
) &&
line.match(this.settings.statRegex)
) {
editor.setLine(
i,
`${this.settings.unfilledStatPrefix}${line}`
);
} else if (
line.startsWith(
this.settings.unfilledStatPrefix
) &&
!line.match(this.settings.statRegex)
) {
editor.setLine(
i,
line.substring(
this.settings.unfilledStatPrefix
.length
)
);
}
2023-03-26 14:00:09 +00:00
}
}
}
}
})
);
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 15:24:14 +00:00
class UnfilledStatsHighlighterSettingsTab extends PluginSettingTab {
plugin: UnfilledStatsHighlighter;
2022-04-15 18:13:31 +00:00
2023-03-26 15:24:14 +00:00
constructor(app: App, plugin: UnfilledStatsHighlighter) {
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", {
2023-03-26 15:24:14 +00:00
text: "Unfilled Stats Highlighter Settings",
2023-03-26 14:00:09 +00:00
});
2023-03-26 14:50:42 +00:00
new Setting(containerEl)
.setName("Stat Regex")
.setDesc(
"Lines that match this javascript regex are considered stats."
)
.addText((text) =>
text
.setPlaceholder("Regex")
.setValue(this.plugin.settings.statRegex)
.onChange(async (value) => {
this.plugin.settings.statRegex = value;
await this.plugin.saveSettings();
})
);
2023-03-26 14:00:09 +00:00
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) => {
this.plugin.settings.unfilledStatPrefix = value;
await this.plugin.saveSettings();
})
);
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) => {
2023-03-26 15:24:14 +00:00
this.plugin.settings.templatesDirectory =
normalizePath(value);
2023-03-26 14:00:09 +00:00
await this.plugin.saveSettings();
})
);
2023-03-26 14:50:42 +00:00
new Setting(containerEl)
.setName("Target Highlighting Directory Name")
.setDesc("The folder that contains your stats to highlight.")
.addText((text) =>
text
.setPlaceholder("Stats Directory")
.setValue(this.plugin.settings.targetHighlightingDirectory)
.onChange(async (value) => {
this.plugin.settings.targetHighlightingDirectory =
2023-03-26 15:24:14 +00:00
normalizePath(value);
2023-03-26 14:50:42 +00:00
await this.plugin.saveSettings();
})
);
2022-04-15 18:13:31 +00:00
}
}