Skip to content

Commit

Permalink
Configure CodeMirror to treat dataviewjs as javascript (#2329)
Browse files Browse the repository at this point in the history
* Configure CodeMirror to treat dataviewjs as javascript and revert on unload

* Run prettier

* Handle custom dataviewjs block name
  • Loading branch information
mnaoumov authored May 26, 2024
1 parent b0c36b7 commit 61291ae
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ export default class DataviewPlugin extends Plugin {
});
})
);

this.registerDataviewjsCodeHighlighting();
this.register(() => this.unregisterDataviewjsCodeHighlighting());
}

public registerDataviewjsCodeHighlighting(): void {
window.CodeMirror.defineMode(this.settings.dataviewJsKeyword, config =>
window.CodeMirror.getMode(config, "javascript")
);
}

public unregisterDataviewjsCodeHighlighting(): void {
window.CodeMirror.defineMode(this.settings.dataviewJsKeyword, config =>
window.CodeMirror.getMode(config, "null")
);
}

private debouncedRefresh: () => void = () => null;
Expand Down Expand Up @@ -378,7 +393,9 @@ class GeneralSettingsTab extends PluginSettingTab {
.setValue(this.plugin.settings.dataviewJsKeyword)
.onChange(async value => {
if (value.length == 0) return;
this.plugin.unregisterDataviewjsCodeHighlighting();
await this.plugin.updateSettings({ dataviewJsKeyword: value });
this.plugin.registerDataviewjsCodeHighlighting();
})
);

Expand Down

0 comments on commit 61291ae

Please sign in to comment.