Replaced keyup event handler with editor-change event handler.

This commit is contained in:
Zachary Hynes 2023-03-28 06:30:43 -04:00
parent 29d8ebcf90
commit 4e678e6e60

87
main.ts
View file

@ -40,55 +40,58 @@ export default class UnfilledStatsHighlighter extends Plugin {
// doesn't belong to this plugin) // doesn't belong to this plugin)
// Using this function will automatically remove the event listener when // Using this function will automatically remove the event listener when
// this plugin is disabled. // this plugin is disabled.
this.registerDomEvent(document, "keyup", (evt: KeyboardEvent) => { this.registerEvent(
const activeFile = this.app.workspace.getActiveFile(); this.app.workspace.on("editor-change", () => {
const activeFile = this.app.workspace.getActiveFile();
if ( if (
!activeFile?.path.contains( !activeFile?.path.contains(
`${this.settings.templatesDirectory}/` `${this.settings.templatesDirectory}/`
) && ) &&
activeFile?.path.contains( activeFile?.path.contains(
`${this.settings.targetHighlightingDirectory}/` `${this.settings.targetHighlightingDirectory}/`
) )
) { ) {
const view = const view =
this.app.workspace.getActiveViewOfType(MarkdownView); this.app.workspace.getActiveViewOfType(MarkdownView);
// Make sure the user is editing a Markdown file. // Make sure the user is editing a Markdown file.
if (view) { if (view) {
const editor = view.editor; const editor = view.editor;
if (editor) { if (editor) {
for (let i = 0; i < editor.lineCount(); i++) { for (let i = 0; i < editor.lineCount(); i++) {
const line = editor.getLine(i); const line = editor.getLine(i);
if ( if (
!line.startsWith( !line.startsWith(
this.settings.unfilledStatPrefix this.settings.unfilledStatPrefix
) && ) &&
line.match(this.settings.statRegex) line.match(this.settings.statRegex)
) { ) {
editor.setLine( editor.setLine(
i, i,
`${this.settings.unfilledStatPrefix}${line}` `${this.settings.unfilledStatPrefix}${line}`
); );
} else if ( } else if (
line.startsWith( line.startsWith(
this.settings.unfilledStatPrefix this.settings.unfilledStatPrefix
) && ) &&
!line.match(this.settings.statRegex) !line.match(this.settings.statRegex)
) { ) {
editor.setLine( editor.setLine(
i, i,
line.substring( line.substring(
this.settings.unfilledStatPrefix.length this.settings.unfilledStatPrefix
) .length
); )
);
}
} }
} }
} }
} }
} })
}); );
} }
// Runs when the plugin is disabled. // Runs when the plugin is disabled.