Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: added ability to toggle linting through command and status bar

Elijah Potter3fa1248e8ab0
diff --git a/packages/obsidian-plugin/logo.svg b/packages/obsidian-plugin/logo.svg
new file mode 100644
index 000000000..f11d98b29
--- /dev/null
+++ b/packages/obsidian-plugin/logo.svg
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg width="100%" height="100%" viewBox="0 0 695 411" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:1.5;" fill="#ffffff" stroke="currentColor">
+ <g transform="matrix(1,0,0,1,-5,-1720)">
+ <g id="Artboard1" transform="matrix(0.824576,0,0,0.749254,0.365685,430.856)">
+ <rect x="5.62" y="1720.57" width="842.425" height="547.24" style="fill:none;"/>
+ <g transform="matrix(1.21274,0,0,1.33466,-2183.71,393.157)">
+ <g transform="matrix(1,0,0,1,-22.3927,1.08043)">
+ <path d="M1930.93,1121.75C1930.93,1121.75 1974.66,1080.73 2041.34,1094.1C2086.61,1103.18 2122.83,1145.4 2122.83,1145.4" style="fill:none;stroke-width:22.92px;"/>
+ </g>
+ <g transform="matrix(1,0,0,1,-47.3485,12.3935)">
+ <path d="M2250.3,1107.1C2250.3,1107.1 2261.8,1065.58 2311.59,1047.05C2361.62,1028.44 2422.42,1051.13 2422.42,1051.13" style="fill:none;stroke-width:22.92px;"/>
+ </g>
+ <g transform="matrix(1.10085,0,0,1.10085,-212.096,-122.054)">
+ <g transform="matrix(1,0,0,1,14.3186,-0.853887)">
+ <ellipse cx="1981.62" cy="1247.49" rx="87.401" ry="87.881" style="fill:none;stroke-width:20.82px;"/>
+ </g>
+ <rect x="2083.34" y="1231.11" width="66.702" height="15.521" style="fill:none;stroke-width:20.82px;"/>
+ <rect x="1892.23" y="1208.69" width="16.306" height="30.182" style="fill:none;stroke-width:20.82px;"/>
+ <g transform="matrix(-1,0,0,1,4281.79,-0.853887)">
+ <ellipse cx="1981.62" cy="1247.49" rx="87.401" ry="87.881" style="fill:none;stroke-width:20.82px;"/>
+ </g>
+ <g transform="matrix(-1,0,0,1,4296.11,0)">
+ <rect x="2083.34" y="1231.11" width="66.702" height="15.521" style="fill:none;stroke-width:20.82px;"/>
+ </g>
+ <g transform="matrix(-1,0,0,1,4296.11,0)">
+ <rect x="1892.23" y="1208.69" width="16.306" height="30.182" style="fill:none;stroke-width:20.82px;"/>
+ </g>
+ </g>
+ </g>
+ </g>
+ </g>
+</svg>
diff --git a/packages/obsidian-plugin/package.json b/packages/obsidian-plugin/package.json
index 20823a461..c3b8f3fdf 100644
--- a/packages/obsidian-plugin/package.json
+++ b/packages/obsidian-plugin/package.json
@@ -8,7 +8,8 @@
"@rollup/plugin-typescript": "^11.1.6",
"@rollup/plugin-wasm": "^6.2.2",
"rollup": "^4.13.0",
- "rollup-plugin-peer-deps-external": "^2.2.4"
+ "rollup-plugin-peer-deps-external": "^2.2.4",
+ "rollup-plugin-svg-import": "^3.0.0"
},
"scripts": {
"build": "rollup -c",
diff --git a/packages/obsidian-plugin/rollup.config.mjs b/packages/obsidian-plugin/rollup.config.mjs
index ac62d195b..66b6e012f 100644
--- a/packages/obsidian-plugin/rollup.config.mjs
+++ b/packages/obsidian-plugin/rollup.config.mjs
@@ -2,6 +2,7 @@ import { wasm } from '@rollup/plugin-wasm';
import typescript from '@rollup/plugin-typescript';
import external from 'rollup-plugin-peer-deps-external';
import { nodeResolve } from '@rollup/plugin-node-resolve';
+import svg from 'rollup-plugin-svg-import';
export default {
input: 'src/index.js',
@@ -11,6 +12,9 @@ export default {
},
external: ['obsidian', 'electron'],
plugins: [
+ svg({
+ stringify: true
+ }),
external(),
wasm({ maxFileSize: Math.pow(2, 32), publicPath: './' }),
nodeResolve(),
diff --git a/packages/obsidian-plugin/src/HarperSettingTab.js b/packages/obsidian-plugin/src/HarperSettingTab.js
index 91568f560..a8003654c 100644
--- a/packages/obsidian-plugin/src/HarperSettingTab.js
+++ b/packages/obsidian-plugin/src/HarperSettingTab.js
@@ -1,5 +1,5 @@
-import HarperPlugin from './index.js';
-import { App, PluginSettingTab, Setting } from 'obsidian';
+import './index.js';
+import { PluginSettingTab, Setting } from 'obsidian';
export class HarperSettingTab extends PluginSettingTab {
/** @type HarperPlugin
diff --git a/packages/obsidian-plugin/src/index.js b/packages/obsidian-plugin/src/index.js
index 76582f470..c8e591a28 100644
--- a/packages/obsidian-plugin/src/index.js
+++ b/packages/obsidian-plugin/src/index.js
@@ -1,5 +1,6 @@
+import logoSvg from '../logo.svg';
import { linter } from './lint';
-import { Plugin } from 'obsidian';
+import { Plugin, addIcon, Menu } from 'obsidian';
import wasm from 'wasm/harper_wasm_bg.wasm';
import init, {
lint,
@@ -17,60 +18,77 @@ function suggestionToLabel(sug) {
}
}
-const harperLinter = linter(
- async (view) => {
- const text = view.state.doc.sliceString(-1);
+const harperLinter = (plugin) =>
+ linter(
+ async (view) => {
+ if (!plugin.shouldLint()) {
+ return [];
+ }
- await init(await wasm());
+ const text = view.state.doc.sliceString(-1);
+
+ await init(await wasm());
+
+ use_spell_check(false);
+ const lints = lint(text);
+
+ return lints.map((lint) => {
+ let span = lint.span();
- use_spell_check(false);
- const lints = lint(text);
-
- return lints.map((lint) => {
- let span = lint.span();
-
- return {
- from: span.start,
- to: span.end,
- severity: 'error',
- title: lint.lint_kind(),
- message: lint.message(),
- actions: lint.suggestions().map((sug) => {
- return {
- name: suggestionToLabel(sug),
- apply: (view) => {
- if (sug === 'Remove') {
- view.dispatch({
- changes: {
- from: span.start,
- to: span.end,
- insert: ''
- }
- });
- } else {
- view.dispatch({
- changes: {
- from: span.start,
- to: span.end,
- insert: sug.get_replacement_text()
- }
- });
+ return {
+ from: span.start,
+ to: span.end,
+ severity: 'error',
+ title: lint.lint_kind(),
+ message: lint.message(),
+ actions: lint.suggestions().map((sug) => {
+ return {
+ name: suggestionToLabel(sug),
+ apply: (view) => {
+ if (sug === 'Remove') {
+ view.dispatch({
+ changes: {
+ from: span.start,
+ to: span.end,
+ insert: ''
+ }
+ });
+ } else {
+ view.dispatch({
+ changes: {
+ from: span.start,
+ to: span.end,
+ insert: sug.get_replacement_text()
+ }
+ });
+ }
}
- }
- };
- })
- };
- });
- },
- { delay: -1 }
-);
+ };
+ })
+ };
+ });
+ },
+ {
+ delay: -1,
+ needsRefresh: () => {
+ let temp = plugin.lintSettingModified;
+ plugin.lintSettingModified = false;
+ return temp;
+ }
+ }
+ );
export default class HarperPlugin extends Plugin {
+ /** @private */
+ shouldAutoLint = true;
+ /** @public */
+ lintSettingModified = false;
+
/** @public
* @returns {Promise<Record<string, any>>} */
async getSettings() {
await init(await wasm());
-
+ this.lintSettingChanged();
return { lintSettings: await get_lint_config_as_object() };
}
@@ -79,14 +97,78 @@ export default class HarperPlugin extends Plugin {
* @returns {Promise<void>} */
async setSettings(settings) {
await init(await wasm());
- await set_lint_config_from_object(settings.lintSettings);
+ set_lint_config_from_object(settings.lintSettings);
+ this.lintSettingChanged();
this.saveData(settings);
}
async onload() {
- this.registerEditorExtension([harperLinter]);
+ console.log(harperLinter(this));
+
+ this.registerEditorExtension([harperLinter(this)]);
+ this.app.workspace.updateOptions();
+
+ addIcon('harper-logo', logoSvg);
+
+ this.setupCommands();
+ this.setupStatusBar();
this.addSettingTab(new HarperSettingTab(this.app, this));
await this.setSettings(await this.loadData());
}
+
+ setupCommands() {
+ this.addCommand({
+ id: 'harper-toggle-auto-lint',
+ name: 'Toggle automatic grammar checking',
+ callback: () => this.toggleAutoLint()
+ });
+ }
+
+ setupStatusBar() {
+ /** @type HTMLElement */
+ let statusBarItem = this.addStatusBarItem();
+ statusBarItem.className += ' mod-clickable';
+
+ let button = document.createElement('span');
+ button.style = 'width:24px';
+ button.innerHTML = logoSvg;
+
+ button.addEventListener('click', (event) => {
+ const menu = new Menu();
+
+ menu.addItem((item) =>
+ item
+ .setTitle(`${this.shouldAutoLint ? 'Disable' : 'Enable'} automatic checking`)
+ .setIcon('documents')
+ .onClick(() => {
+ this.toggleAutoLint();
+ })
+ );
+
+ menu.showAtMouseEvent(event);
+ });
+
+ statusBarItem.appendChild(button);
+ }
+
+ shouldLint() {
+ return this.shouldAutoLint;
+ }
+
+ /** @param {boolean} shouldAutoLint */
+ setAutoLint(shouldAutoLint) {
+ this.shouldAutoLint = shouldAutoLint;
+ this.lintSettingChanged();
+ }
+
+ toggleAutoLint() {
+ this.shouldAutoLint = !this.shouldAutoLint;
+ this.lintSettingChanged();
+ }
+
+ lintSettingChanged() {
+ this.lintSettingModified = true;
+ this.app.workspace.updateOptions();
+ }
}
diff --git a/packages/yarn.lock b/packages/yarn.lock
index a584e940d..54fa4ff91 100644
--- a/packages/yarn.lock
+++ b/packages/yarn.lock
@@ -4160,6 +4160,13 @@ rollup-plugin-peer-deps-external@^2.2.4:
resolved "https://registry.yarnpkg.com/rollup-plugin-peer-deps-external/-/rollup-plugin-peer-deps-external-2.2.4.tgz#8a420bbfd6dccc30aeb68c9bf57011f2f109570d"
integrity sha512-AWdukIM1+k5JDdAqV/Cxd+nejvno2FVLVeZ74NKggm3Q5s9cbbcOgUPGdbxPi4BXu7xGaZ8HG12F+thImYu/0g==
+rollup-plugin-svg-import@^3.0.0:
+ version "3.0.0"
+ resolved "https://registry.yarnpkg.com/rollup-plugin-svg-import/-/rollup-plugin-svg-import-3.0.0.tgz#4aa62b6ccc8b675fa8b579b23cd983bb2907d6f1"
+ integrity sha512-5fUESTM5hdqJojrwO53JQUO7NespLNx4iLeMsToQfuaGGqGT5sz85Ns5gCDNxLO6yBPbn7p0A/6YA+Rq3clg4Q==
+ dependencies:
+ "@rollup/pluginutils" "^5.0.1"
+
rollup@^3.27.1:
version "3.29.4"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.29.4.tgz#4d70c0f9834146df8705bfb69a9a19c9e1109981"
@@ -5112,6 +5119,7 @@ [email protected]:
"wasm@link:../harper-wasm/pkg":
version "0.0.0"
+ uid ""
whatwg-encoding@^3.1.1:
version "3.1.1"