Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
Obsidian sidebar (#3915)
Luke Benhase0c9fbced3ae0
diff --git a/packages/obsidian-plugin/src/SidebarView.ts b/packages/obsidian-plugin/src/SidebarView.tsnew file mode 100644index 000000000..5ce14ea28--- /dev/null+++ b/packages/obsidian-plugin/src/SidebarView.ts@@ -0,0 +1,257 @@+import { type IconName, ItemView, Menu, setIcon, type WorkspaceLeaf } from 'obsidian';+import type HarperPlugin from './index';+import { LINT_KIND_COLORS } from './lintKindColor';++export class SidebarView extends ItemView {+ constructor(leaf: WorkspaceLeaf, _plugin: HarperPlugin) {+ super(leaf);+ }++ getViewType() {+ return 'harper-sidebar-view';+ }++ getDisplayText() {+ return 'Harper Grammar';+ }++ getIcon(): IconName {+ return 'harper-logo';+ }++ async onOpen() {+ this.update();+ }++ update() {+ const container = this.containerEl;+ container.empty();++ container.style.padding = '0';+ container.style.display = 'flex';+ container.style.flexDirection = 'column';+ container.style.justifyContent = 'flex-start';++ const listContainer = document.createElement('div');++ // initial card+ const initialCard = createCard();+ const initialTitle = createTitle('Loading errors...');+ initialCard.appendChild(initialTitle);++ listContainer.appendChild(initialCard);++ listContainer.id = 'harper-error-list';+ listContainer.style.height = '100%';+ listContainer.style.overflowY = 'auto';++ container.appendChild(listContainer);++ this.registerEvent(+ this.app.workspace.on('harper:lint-updated', (errors: any[], editorView: any) => {+ listContainer.innerHTML = '';++ if (!errors || errors.length === 0) {+ const card = createCard();+ const title = createTitle('No grammar errors found!');+ card.appendChild(title);+ listContainer.appendChild(card);+ return;+ }++ errors.forEach((error) => {+ createErrorCard(error, editorView, listContainer);+ });+ }),+ );+ }++ async onClose() {}+}++function createCard(): HTMLDivElement {+ const card = document.createElement('div');+ card.style.display = 'flex';+ card.style.flexDirection = 'column';+ card.style.padding = '12px 16px';+ card.style.borderBottom = '1px solid var(--background-modifier-border)';+ card.style.gap = '8px';+ return card;+}++function createTitle(text: string, color?: string): HTMLSpanElement {+ const title = document.createElement('span');+ title.style.fontWeight = 'bold';+ if (color) {+ title.style.color = color;+ }+ title.textContent = text;+ return title;+}++function getSeverityColor(error: any) {+ // find card color+ let severityColor = '';++ if (error.markClass) {+ const classes = error.markClass.split(' ');++ const harperClass = classes.find((c: string) => c.startsWith('harper-lintRange-'));+ if (harperClass) {+ const lintKind = harperClass.replace('harper-lintRange-', '');+ if (LINT_KIND_COLORS?.[lintKind]) {+ severityColor = LINT_KIND_COLORS[lintKind];+ }+ }+ }+ return severityColor;+}++function createTitleDiv(error: any): HTMLDivElement {+ const titleDiv = document.createElement('div');+ titleDiv.style.display = 'flex';+ titleDiv.style.flexWrap = 'wrap';+ titleDiv.style.gap = '6px';+ titleDiv.style.marginTop = '4px';++ const title = createTitle(error.title || 'Harper Suggestion', getSeverityColor(error));++ // add the options ignore diagnostic and Disable Rule in a dropdown btn+ if (error.ignore || error.disable) {+ const btn = document.createElement('div');+ setIcon(btn, 'more-vertical');+ btn.style.cursor = 'var(--cursor)';+ btn.style.color = 'var(--text-muted)';+ btn.style.display = 'flex';+ btn.style.alignItems = 'center';+ btn.style.padding = '2px 4px';+ btn.style.borderRadius = 'var(--radius-s)';+ btn.style.marginLeft = 'auto';++ btn.onmouseover = () => {+ btn.style.backgroundColor = 'var(--background-modifier-hover)';+ };+ btn.onmouseout = () => {+ btn.style.backgroundColor = 'transparent';+ };++ btn.onclick = (e) => {+ const menu = new Menu();++ if (error.ignore) {+ menu.addItem((item) => {+ item+ .setTitle('Ignore Diagnostic')+ .setIcon('eye-off')+ .onClick(() => {+ error.ignore();+ });+ });+ }+ if (error.disable) {+ menu.addItem((item) => {+ item+ .setTitle('Disable Rule')+ .setIcon('ban')+ .onClick(() => {+ error.disable();+ });+ });+ }++ menu.showAtMouseEvent(e);+ };++ titleDiv.appendChild(title);+ titleDiv.appendChild(btn);+ }+ return titleDiv;+}++function getWordsArroundError(error: any, editorView: any) {+ const doc = editorView.state.doc;+ const problemText = doc.sliceString(error.from, error.to);++ // get 18 char before and after the word+ const rawPrefix = doc.sliceString(Math.max(0, error.from - 18), error.from);+ const rawSuffix = doc.sliceString(error.to, Math.min(doc.length, error.to + 18));+ // trim to be only 3 words before and after.+ let prefix = rawPrefix.split(/[.!?\n]/).pop() || '';+ const prefixWords = prefix+ .trim()+ .split(/\s+/)+ .filter((w) => w.length > 0);+ prefix = prefixWords.slice(-3).join(' ');+ if (prefix.length > 0) prefix += ' ';++ let suffix = rawSuffix.split(/[.!?\n]/)[0] || '';+ const suffixWords = suffix+ .trim()+ .split(/\s+/)+ .filter((w) => w.length > 0);+ suffix = suffixWords.slice(0, 3).join(' ');+ if (suffix.length > 0) suffix = ` ${suffix}`;++ // text container+ const textContainer = document.createElement('span');+ textContainer.style.fontSize = 'var(--font-ui-small)';++ if (prefix) {+ textContainer.appendChild(document.createTextNode(prefix));+ }++ const boldWord = document.createElement('strong');+ boldWord.textContent = problemText;+ boldWord.style.color = getSeverityColor(error);+ textContainer.appendChild(boldWord);++ if (suffix) {+ textContainer.appendChild(document.createTextNode(suffix));+ }+ return textContainer;+}++function getErrorActions(error: any, editorView: any): HTMLDivElement {+ if (error.actions && error.actions.length > 0) {+ const actionConst = document.createElement('div');+ actionConst.style.display = 'flex';+ actionConst.style.flexWrap = 'wrap';+ actionConst.style.gap = '6px';+ actionConst.style.marginTop = '4px';++ error.actions.forEach((action: any) => {+ const btn = document.createElement('button');+ btn.textContent = action.name;+ btn.title = action.title;++ btn.style.fontSize = 'var(--font-ui-smaller)';+ btn.style.cursor = 'var(--cursor)';++ btn.onclick = () => {+ action.apply(editorView, error.from, error.to);+ };++ actionConst.appendChild(btn);+ });+ return actionConst;+ }+ return document.createElement('div');+}++function createErrorCard(error: any, editorView: any, listContainer: HTMLDivElement) {+ try {+ const card = createCard();++ const titleDiv = createTitleDiv(error);+ const textContainer = getWordsArroundError(error, editorView);+ const actionConst = getErrorActions(error, editorView);++ card.appendChild(titleDiv);+ card.appendChild(textContainer);+ card.appendChild(actionConst);++ listContainer.appendChild(card);+ } catch (err) {+ console.error('Harper Sidebar failed to read: ', err, error);+ }+}diff --git a/packages/obsidian-plugin/src/index.ts b/packages/obsidian-plugin/src/index.tsindex 2671514c1..49971b1db 100644--- a/packages/obsidian-plugin/src/index.ts+++ b/packages/obsidian-plugin/src/index.ts@@ -1,6 +1,6 @@import type { EditorView } from '@codemirror/view';import { Dialect } from 'harper.js';-import { editorInfoField, MarkdownView, Menu, Notice, Plugin } from 'obsidian';+import { addIcon, editorInfoField, MarkdownView, Menu, Notice, Plugin } from 'obsidian';import logoSvg from '../logo.svg?raw';import logoSvgDisabled from '../logo-disabled.svg?raw';import { HarperSettingTab } from './HarperSettingTab';@@ -16,6 +16,7 @@ import {ignoreVisibleTooltipDiagnostic,navigateDiagnostic,} from './lint';+import { SidebarView } from './SidebarView';import State from './State';export default class HarperPlugin extends Plugin {@@ -30,6 +31,8 @@ export default class HarperPlugin extends Plugin {return;}+ addIcon('harper-logo', logoSvg);+this.app.workspace.onLayoutReady(async () => {this.state = new State((n) => this.saveData(n),@@ -45,9 +48,37 @@ export default class HarperPlugin extends Plugin {this.settings = new HarperSettingTab(this.app, this);this.addSettingTab(this.settings);+ this.registerView('harper-sidebar-view', (leaf) => new SidebarView(leaf, this));+this.setupCommands();}+ async activateSidebarView() {+ if (this.app.workspace.getLeavesOfType('harper-sidebar-view').length > 0) {+ return;+ }+ const leaf = this.app.workspace.getRightLeaf(false);+ await leaf.setViewState({+ type: 'harper-sidebar-view',+ active: true,+ });+ this.app.workspace.revealLeaf(leaf);+ this.updateSidebar();+ }++ private updateSidebar() {+ const editorView = this.getActiveEditorView();+ if (!editorView) return;++ const leaves = this.app.workspace.getLeavesOfType('harper-sidebar-view');+ if (leaves.length > 0) {+ const sidebar = leaves[0].view as SidebarView;+ if (sidebar) {+ sidebar.update();+ }+ }+ }+async onExternalSettingsChange() {await this.reloadSettingsFromDisk();}@@ -142,6 +173,18 @@ export default class HarperPlugin extends Plugin {this.updateStatusBar();}+ private async toggleSidebar() {+ const existingLeaves = this.app.workspace.getLeavesOfType('harper-sidebar-view');+ if (existingLeaves.length > 0) {+ existingLeaves.forEach((leaf) => {+ leaf.detach();+ });+ } else {+ await this.activateSidebarView();+ await this.state.reinitialize();+ }+ }+private setupCommands() {this.addCommand({id: 'harper-toggle-auto-lint',@@ -151,6 +194,14 @@ export default class HarperPlugin extends Plugin {},});+ this.addCommand({+ id: 'harper-toggle-sidebar',+ name: 'Toggle spellcheck sidebar',+ callback: () => {+ this.toggleSidebar();+ },+ });+this.addCommand({id: 'harper-ignore-all-in-buffer',name: 'Ignore all errors in the open file',diff --git a/packages/obsidian-plugin/src/lint.ts b/packages/obsidian-plugin/src/lint.tsindex 21b9a3e9e..f718bbf99 100644--- a/packages/obsidian-plugin/src/lint.ts+++ b/packages/obsidian-plugin/src/lint.ts@@ -323,6 +323,9 @@ const lintPlugin = ViewPlugin.fromClass(Promise.all(sources.map((source) => Promise.resolve(source(this.view)))).then((annotations) => {const all = annotations.reduce((a, b) => a.concat(b));+ if ((window as any).app) {+ (window as any).app.workspace.trigger('harper:lint-updated', all, this.view);+ }if (this.view.state.doc == state.doc)this.view.dispatch(setDiagnostics(this.view.state, all));},