Skip to content

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

feat(web): apply feedback from #1878 (#1942)

Elijah Potterb873d8cbed35
diff --git a/packages/web/src/lib/Editor.svelte b/packages/web/src/lib/Editor.svelte
index 2c34c91fc..d9c0b17ca 100644
--- a/packages/web/src/lib/Editor.svelte
+++ b/packages/web/src/lib/Editor.svelte
@@ -18,22 +18,35 @@ let linter: WorkerLinter;
// Live list of lints from the framework's lint callback
let lints: UnpackedLint[] = [];
-let openIndex: number | null = null;
-
-let lfw = new LintFramework(async (text) => {
- // Guard until the linter is ready
- if (!linter) return [];
-
- const raw = await linter.lint(text);
- // The framework expects "unpacked" lints with plain fields
- const unpacked = await Promise.all(
- raw.map((lint) => unpackLint(window.location.hostname, lint, linter)),
- );
-
- lints = unpacked;
-
- return unpacked;
-}, {});
+// Track which lint cards are open by index
+let openSet: Set<number> = new Set();
+
+let lfw = new LintFramework(
+ async (text) => {
+ if (!linter) return [];
+
+ const raw = await linter.lint(text);
+ // The framework expects "unpacked" lints with plain fields
+ const unpacked = await Promise.all(raw.map((lint) => unpackLint(text, lint, linter)));
+
+ lints = unpacked;
+
+ return unpacked;
+ },
+ {
+ ignoreLint: async (hash: string) => {
+ if (!linter) return;
+ try {
+ await linter.ignoreLintHash(BigInt(hash));
+ console.log(`Ignored ${hash}`);
+ // Re-run linting to hide ignored lint immediately
+ lfw.update();
+ } catch (e) {
+ console.error('Failed to ignore lint', e);
+ }
+ },
+ },
+);
(async () => {
let { WorkerLinter, binary } = await import('harper.js');
@@ -74,6 +87,68 @@ function createSnippetFor(lint: UnpackedLint) {
suffixEllipsis: end < content.length,
};
}
+
+function jumpTo(lint: UnpackedLint) {
+ if (!editor) return;
+ const start = lint.span.start;
+ const end = lint.span.end;
+ // Focus and select; most browsers will scroll selection into view on focus
+ editor.focus();
+ editor.setSelectionRange(start, end);
+ // As a fallback, nudge scroll to selection if needed
+ try {
+ const approxLineHeight = 20;
+ const beforeText = content.slice(0, start);
+ const line = (beforeText.match(/\n/g)?.length ?? 0) + 1;
+ const targetTop = Math.max(0, (line - 3) * approxLineHeight);
+ (editor as HTMLTextAreaElement).scrollTop = targetTop;
+ } catch {}
+}
+
+function toggleCard(i: number) {
+ const wasOpen = openSet.has(i);
+ if (wasOpen) {
+ const ns = new Set(openSet);
+ ns.delete(i);
+ openSet = ns;
+ } else {
+ const ns = new Set(openSet);
+ ns.add(i);
+ openSet = ns;
+ }
+}
+
+$: allOpen = lints.length > 0 && openSet.size === lints.length;
+
+function toggleAll() {
+ if (allOpen) {
+ openSet = new Set();
+ } else {
+ openSet = new Set(lints.map((_, i) => i));
+ }
+}
+
+async function ignoreAll() {
+ if (!linter || lints.length === 0) return;
+ try {
+ const hashes = Array.from(new Set(lints.map((l) => l.context_hash)));
+ await Promise.all(hashes.map((h) => linter.ignoreLintHash(BigInt(h))));
+ // Refresh to hide ignored lints immediately
+ lfw.update();
+ } catch (e) {
+ console.error('Failed to ignore all lints', e);
+ }
+}
+
+// Keep openSet in range if lint list changes
+$: if (openSet.size > 0) {
+ const max = lints.length;
+ const next = new Set<number>();
+ for (const idx of openSet) {
+ if (idx >= 0 && idx < max) next.add(idx);
+ }
+ if (next.size !== openSet.size) openSet = next;
+}
</script>
<div class="flex flex-row h-full max-w-full">
@@ -86,7 +161,26 @@ function createSnippetFor(lint: UnpackedLint) {
</Card>
<Card class="hidden md:flex md:flex-col md:w-1/3 h-full p-5 z-10">
- <div class="text-base font-semibold mb-3">Problems</div>
+ <div class="flex items-center justify-between mb-3">
+ <div class="text-base font-semibold">Problems</div>
+ <div class="flex items-center gap-2">
+ <button
+ class="text-xs px-2 py-1 rounded border border-gray-300 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-[#0b0f14]"
+ on:click={toggleAll}
+ aria-label={allOpen ? 'Collapse all lint cards' : 'Open all lint cards'}
+ >
+ {allOpen ? 'Collapse all' : 'Open all'}
+ </button>
+ <button
+ class="text-xs px-2 py-1 rounded border border-gray-300 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-[#0b0f14]"
+ on:click={ignoreAll}
+ disabled={lints.length === 0}
+ aria-label="Ignore all current lints"
+ >
+ Ignore all
+ </button>
+ </div>
+ </div>
<div class="flex-1 overflow-y-auto pr-1">
{#if lints.length === 0}
<p class="text-sm text-gray-500">No lints yet.</p>
@@ -96,8 +190,9 @@ function createSnippetFor(lint: UnpackedLint) {
<LintCard
{lint}
snippet={createSnippetFor(lint)}
- open={openIndex === i}
- onToggle={() => (openIndex = openIndex === i ? null : i)}
+ open={openSet.has(i)}
+ onToggleOpen={() => toggleCard(i)}
+ focusError={() => jumpTo(lint)}
onApply={(s) => applySug(lint, s)}
/>
{/each}
diff --git a/packages/web/src/lib/LintCard.svelte b/packages/web/src/lib/LintCard.svelte
index 858066a18..085ca0e39 100644
--- a/packages/web/src/lib/LintCard.svelte
+++ b/packages/web/src/lib/LintCard.svelte
@@ -4,7 +4,8 @@ import { slide } from 'svelte/transition';
export let lint: UnpackedLint;
export let open = false;
-export let onToggle: () => void;
+export let onToggleOpen: () => void;
+export let focusError: () => void;
export let onApply: (s: UnpackedSuggestion) => void;
export let snippet: {
prefix: string;
@@ -19,14 +20,17 @@ function suggestionText(s: UnpackedSuggestion): string {
}
</script>
-<div class="rounded-lg border border-gray-300 dark:border-gray-700 shadow-sm bg-white dark:bg-[#0d1117]">
+<div
+ class="rounded-lg border border-gray-300 dark:border-gray-700 shadow-sm bg-white dark:bg-[#0d1117]"
+ on:click={() => focusError?.()}
+>
<div
role="button"
tabindex="0"
class="flex items-center justify-between p-3 cursor-pointer select-none"
aria-expanded={open}
- on:click={() => onToggle?.()}
- on:keydown={(e) => (e.key === 'Enter' || e.key === ' ') && (e.preventDefault(), onToggle?.())}
+ on:click={() => onToggleOpen?.()}
+ on:keydown={(e) => (e.key === 'Enter' || e.key === ' ') && (e.preventDefault(), onToggleOpen?.())}
>
<div
class="text-sm font-semibold pb-1"
diff --git a/packages/web/src/routes/+page.svelte b/packages/web/src/routes/+page.svelte
index 37203dd37..a3a390845 100644
--- a/packages/web/src/routes/+page.svelte
+++ b/packages/web/src/routes/+page.svelte
@@ -110,9 +110,9 @@ function agentHas(keyword) {
>, so you can get fantastic grammar checking anywhere you work.
<br /><br /> That said, we take extra care to make sure the
<a href="/docs/integrations/visual-studio-code"
- >Install in Visual Studio Code</a
- >, <a href="/docs/integrations/neovim">Use in Neovim</a>,
- <a href="/docs/integrations/obsidian">Install in Obsidian</a>, and <a href="/docs/integrations/chrome-extension">Add to Chrome</a> integrations are amazing.
+ >Visual Studio Code</a
+ >, <a href="/docs/integrations/neovim">Neovim</a>,
+ <a href="/docs/integrations/obsidian">Obsidian</a>, and <a href="/docs/integrations/chrome-extension">Chrome</a> extensions are amazing.
</span>
<img