Skip to content

Commit

Permalink
feat(obsidian-plugin): added setting for whether to use Web Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-potter committed Dec 31, 2024
1 parent e4fbe73 commit 5f98d68
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/obsidian-plugin/src/HarperSettingTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export class HarperSettingTab extends PluginSettingTab {

console.log(this.settings.lintSettings);

new Setting(containerEl).setName('Use Web Worker').addToggle((toggle) =>
toggle.setValue(this.settings.useWebWorker).onChange(async (value) => {
this.settings.useWebWorker = value;
await this.plugin.setSettings(this.settings);
})
);

for (let setting of Object.keys(this.settings.lintSettings)) {
let value = this.settings.lintSettings[setting];
let description = this.descriptions[setting];
Expand Down
30 changes: 26 additions & 4 deletions packages/obsidian-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logoSvg from '../logo.svg';
import { linter } from './lint';
import { Plugin, addIcon, Menu } from 'obsidian';
import { WorkerLinter } from 'harper.js';
import { LocalLinter, WorkerLinter } from 'harper.js';
import { HarperSettingTab } from './HarperSettingTab';

function suggestionToLabel(sug) {
Expand All @@ -12,8 +12,20 @@ function suggestionToLabel(sug) {
}
}

let harper = new WorkerLinter();
harper.setup();
function initHarperInstance(useWebWorker) {
if (useWebWorker) {
console.log('Switching to `WorkerLinter`');
harper = new WorkerLinter();
} else {
console.log('Switching to `LocalLinter`');
harper = new LocalLinter();
}
harper.setup();
}

let harper;

initHarperInstance(true);

const harperLinter = (plugin) =>
linter(
Expand Down Expand Up @@ -91,7 +103,7 @@ export default class HarperPlugin extends Plugin {

let lintSettings = await harper.getLintConfig();

return { lintSettings };
return { ...this.settings, lintSettings };
}

/** @public
Expand All @@ -102,6 +114,10 @@ export default class HarperPlugin extends Plugin {
settings = {};
}

if (settings.useWebWorker == undefined) {
settings.useWebWorker = true;
}

if (settings.lintSettings == undefined) {
settings.lintSettings = {};
}
Expand All @@ -113,6 +129,12 @@ export default class HarperPlugin extends Plugin {
await harper.setLintConfig(settings.lintSettings);
this.lintSettingChanged();
this.saveData(settings);

if (this.settings?.useWebWorker != settings.useWebWorker) {
initHarperInstance(settings.useWebWorker);
}

this.settings = settings;
}

async onload() {
Expand Down

0 comments on commit 5f98d68

Please sign in to comment.