Skip to content

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

Added click underlines to select lint

Elijah Potter2579476f35ee
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 70147b38f..8d3ba321d 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -24,5 +24,7 @@ pub fn all_linters(document: &Document, dictionary: &Dictionary) -> Vec<Lint> {
lints.append(&mut linter(document, dictionary));
}
+ lints.sort_by_key(|lint| lint.span.start);
+
lints
}
diff --git a/web/src/app.css b/web/src/app.css
index 4cfbbf9b4..40ec3762a 100644
--- a/web/src/app.css
+++ b/web/src/app.css
@@ -14,6 +14,7 @@
.underlinespecial {
position: relative;
+ background-color: var(--bg-color);
}
.underlinespecial::after {
@@ -25,7 +26,7 @@
width: 100%;
height: var(--line-width);
border-radius: 1000px;
- background: var(--bg-color);
+ background: var(--line-color);
position: absolute;
bottom: -3px;
left: 0;
diff --git a/web/src/lib/Editor.svelte b/web/src/lib/Editor.svelte
index b2378ae0b..71b8ca6d6 100644
--- a/web/src/lib/Editor.svelte
+++ b/web/src/lib/Editor.svelte
@@ -15,6 +15,12 @@
$: lintText(content).then((newLints) => (lints = newLints));
$: boxHeight = calcHeight(content);
+ $: if (editor != null && focused != null) {
+ let p = lints[focused].span.end;
+ editor.selectionStart = p;
+ editor.selectionEnd = p;
+ }
+
function calcHeight(boxContent: string): number {
let numberOfLineBreaks = (boxContent.match(/\n/g) || []).length;
let newHeight = 20 + numberOfLineBreaks * 30 + 12 + 2;
@@ -28,16 +34,17 @@
class="flex-grow h-full p-5 grid z-10 max-w-full text-lg overflow-auto mr-5"
on:click={() => editor && editor.focus()}
>
- <div class="m-0 p-0" style="grid-row: 1; grid-column: 1">
- <Underlines {content} focusLintIndex={focused} />
- </div>
<textarea
bind:this={editor}
class="w-full m-0 rounded-none p-0 z-0 bg-transparent border-none text-lg resize-none focus:border-0"
spellcheck="false"
style={`grid-row: 1; grid-column: 1; height: ${boxHeight}px`}
+ on:keydown={() => (focused = undefined)}
bind:value={content}
></textarea>
+ <div class="m-0 p-0 z-10 pointer-events-none" style="grid-row: 1; grid-column: 1">
+ <Underlines {content} bind:focusLintIndex={focused} />
+ </div>
</Card>
<Card class="flex flex-col flex-none basis-[400px] overflow-auto h-full">
<h2 class="text-2xl font-bold m-1">Suggestions</h2>
diff --git a/web/src/lib/Underlines.svelte b/web/src/lib/Underlines.svelte
index 249ee7a7e..3479c5ec6 100644
--- a/web/src/lib/Underlines.svelte
+++ b/web/src/lib/Underlines.svelte
@@ -51,8 +51,9 @@
let lintContent = [
spanContent(lint.span, content).replaceAll(' ', '\u00A0'),
- 'red',
- lintIndex === focusLintIndex
+ '#0B2545',
+ lintIndex === focusLintIndex,
+ lintIndex
];
return [...prevContent, lintContent];
@@ -74,7 +75,7 @@
return results;
}
- // string | [string, string, string] | null
+ // string | [string, string, string, index] | null
$: modified = processString(lints, focusLintIndex);
</script>
@@ -86,10 +87,11 @@
{:else if typeof chunk == 'string'}
<span class="">{chunk}</span>
{:else}
- <span style={`margin-right: -4px;`}>
+ <span class="pointer-events-auto" style={`margin-right: -4px;`}>
<span
- class={`underlinespecial ${chunk[2] ? 'animate-after-bigbounce' : ''}`}
- style={`--bg-color: ${chunk[1]}; --line-width: ${chunk[2] ? '4px' : '2px'};`}
+ class={`underlinespecial transition-all rounded-sm ${chunk[2] ? 'animate-after-bigbounce text-white' : ''}`}
+ on:click={() => (focusLintIndex = chunk[3]) && console.log('hit')}
+ style={`--line-color: ${chunk[1]}; --line-width: ${chunk[2] ? '4px' : '2px'}; --bg-color: ${chunk[2] ? '#8DA9C4' : 'transparent'};`}
>
{chunk[0]}
</span>
diff --git a/web/src/routes/+page.svelte b/web/src/routes/+page.svelte
index 2085d651e..b02622f7a 100644
--- a/web/src/routes/+page.svelte
+++ b/web/src/routes/+page.svelte
@@ -8,7 +8,7 @@
<h2 class="text-3xl text-center">The Grammar Checker for Artists</h2>
<h2 class="text-2xl font-light italic text-center">Private. Free. Unobtrusive.</h2>
<div class="flex flex-row justify-evenly mt-5">
- <a href="https://github.com/chilipepperhott/lt-rs" class="hover:scale-105 transition-all"
+ <a href="https://github.com/chilipepperhott/harper" class="hover:scale-105 transition-all"
><img width="40" height="40" src="/icons/github.svg" alt="Project Repository" /></a
>
<a href="https://elijahpotter.dev" class="hover:scale-105 transition-all"