Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: created initial codemirror implementation
Elijah Potter3f0f03210150
diff --git a/web/package.json b/web/package.jsonindex e16ce8320..72ee6899e 100644--- a/web/package.json+++ b/web/package.json@@ -39,6 +39,9 @@},"type": "module","dependencies": {+ "@codemirror/lint": "^6.5.0",+ "@codemirror/state": "^6.4.1",+ "codemirror": "^6.0.1","lodash-es": "^4.17.21"}}diff --git a/web/src/lib/Editor.svelte b/web/src/lib/Editor.sveltedeleted file mode 100644index 552b3ba22..000000000--- a/web/src/lib/Editor.svelte+++ /dev/null@@ -1,106 +0,0 @@-<script lang="ts">- import { Card } from 'flowbite-svelte';- import demo from '../../../demo.md?raw';- import Underlines from '$lib/Underlines.svelte';- import { Button } from 'flowbite-svelte';- import { lintText, applySuggestion, spanContent } from '$lib/analysis';- import type { Lint } from '$lib/analysis';-- let content = demo;-- let lints: Lint[] = [];- let lintCards: HTMLButtonElement[] = [];- let focused: number | undefined;- let editor: HTMLTextAreaElement | null;-- $: lintText(content).then((newLints) => (lints = newLints));- $: boxHeight = calcHeight(content);- $: if (focused != null && lintCards[focused])- lintCards[focused].scrollIntoView({ behavior: 'smooth', block: 'center' });-- $: if (editor != null && focused != null) {- let lint = lints[focused % lints.length];- if (lint != null) {- let p = lint.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;- return newHeight;- }-</script>--<div class="flex flex-row w-full h-full p-5">- <Card- class="flex-grow h-full p-5 grid z-10 max-w-full text-lg overflow-auto mr-5"- on:click={() => editor && editor.focus()}- >- <textarea- bind:this={editor}- class="w-full text-nowrap m-0 rounded-none p-0 z-0 bg-transparent overflow-hidden 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-none basis-[400px] h-full p-0">- <h2 class="text-2xl font-bold m-1">Suggestions</h2>- <div class="flex flex-col overflow-y-scroll overflow-x-hidden m-0 p-0">- {#each lints as lint, i}- <button- class="block max-w-sm p-6 bg-white dark:bg-gray-800 border border-gray-200 rounded-lg shadow m-1 hover:translate-x-1 transition-all"- on:click={() => (focused = i)}- bind:this={lintCards[i]}- >- <div class="pl-2 border-l-[3px] border-l-primary-500">- <div class="flex flex-row">- <h3 class="font-bold">- {lint.lint_kind} - “<span class="italic">- {spanContent(lint.span, content)}- </span> ”- </h3>- </div>- <div- class="transition-all overflow-hidden flex flex-col justify-evenly"- style={`height: ${- focused === i ? `calc(55px * ${lint.suggestions.length + 1})` : '0px'- }`}- >- <p style="height: 50px" class="text-left">{lint.message}</p>- {#each lint.suggestions as suggestion}- <div class="w-full p-[4px]">- <Button- color="primary"- class="w-full"- style="height: 40px; margin: 5px 0px;"- on:click={() =>- applySuggestion(content, suggestion, lint.span).then(- (edited) => (content = edited)- )}- >- {#if suggestion == 'Remove'}- Remove "{content.substring(lint.span.start, lint.span.end)}"- {:else}- Replace "{content.substring(lint.span.start, lint.span.end)}" with "{suggestion.ReplaceWith.reduce(- (p, c) => p + c,- ''- )}"- {/if}- </Button>- </div>- {/each}- </div>- </div>- </button>- {/each}- </div>- </Card>-</div>diff --git a/web/src/lib/NewEditor.svelte b/web/src/lib/NewEditor.sveltenew file mode 100644index 000000000..e9ad90e90--- /dev/null+++ b/web/src/lib/NewEditor.svelte@@ -0,0 +1,64 @@+<script lang="ts">+ import { onMount } from 'svelte';+ import { minimalSetup, EditorView } from 'codemirror';+ import { contentToString, lintText, type Suggestion } from './analysis';+ import { linter } from '@codemirror/lint';++ let editorDiv: HTMLDivElement;++ function suggestionToLabel(sug: Suggestion) {+ if (sug === 'Remove') {+ return 'Remove';+ } else {+ return `Replace with "${contentToString(sug.ReplaceWith)}"`;+ }+ }++ const harperLinter = linter(+ async (view) => {+ let text = view.state.doc.sliceString(0);++ let lints = await lintText(text);++ return lints.map((lint) => {+ return {+ from: lint.span.start,+ to: lint.span.end,+ severity: 'warning',+ message: lint.message,+ actions: lint.suggestions.map((sug, i) => {+ return {+ name: suggestionToLabel(sug),+ apply: (view) => {+ if (sug === 'Remove') {+ view.dispatch({+ changes: { from: lint.span.start, to: lint.span.end, insert: '' }+ });+ } else {+ view.dispatch({+ changes: {+ from: lint.span.start,+ to: lint.span.end,+ insert: contentToString(sug.ReplaceWith)+ }+ });+ }+ }+ };+ })+ };+ });+ },+ { delay: 0 }+ );++ onMount(() => {+ new EditorView({+ doc: 'You cannot add new lines in this editor\n\n\n\n',+ extensions: [minimalSetup, harperLinter],+ parent: editorDiv+ });+ });+</script>++<div bind:this={editorDiv} />diff --git a/web/src/lib/analysis.ts b/web/src/lib/analysis.tsindex 48b034f2e..182d70f30 100644--- a/web/src/lib/analysis.ts+++ b/web/src/lib/analysis.ts@@ -1,4 +1,4 @@-const defaultUseWasm = false;+const defaultUseWasm = true;export interface ParseResponse {tokens: Token[];diff --git a/web/src/routes/+page.svelte b/web/src/routes/+page.svelteindex 06825a502..32374bf61 100644--- a/web/src/routes/+page.svelte+++ b/web/src/routes/+page.svelte@@ -2,6 +2,7 @@import Editor from '$lib/Editor.svelte';import GutterCenter from '$lib/GutterCenter.svelte';import Logo from '$lib/Logo.svelte';+ import NewEditor from '$lib/NewEditor.svelte';import { DarkMode } from 'flowbite-svelte';let width = window.innerWidth;@@ -47,13 +48,7 @@</div><div class="w-full h-[600px] overflow-hidden">- {#if width > 1080}- <Editor />- {:else}- <p class="italic text-lg w-full text-center dark:text-white">- Open this page on a bigger screen to start checking your work.- </p>- {/if}+ <NewEditor /></div></GutterCenter>diff --git a/web/yarn.lock b/web/yarn.lockindex 4a9d9850c..decd26004 100644--- a/web/yarn.lock+++ b/web/yarn.lock@@ -27,6 +27,70 @@dependencies:regenerator-runtime "^0.14.0"+"@codemirror/autocomplete@^6.0.0":+ version "6.15.0"+ resolved "https://registry.yarnpkg.com/@codemirror/autocomplete/-/autocomplete-6.15.0.tgz#37bc320f20cdda332d6bf4d1fc7f300f8fc5f04c"+ integrity sha512-G2Zm0mXznxz97JhaaOdoEG2cVupn4JjPaS4AcNvZzhOsnnG9YVN68VzfoUw6dYTsIxT6a/cmoFEN47KAWhXaOg==+ dependencies:+ "@codemirror/language" "^6.0.0"+ "@codemirror/state" "^6.0.0"+ "@codemirror/view" "^6.17.0"+ "@lezer/common" "^1.0.0"++"@codemirror/commands@^6.0.0":+ version "6.3.3"+ resolved "https://registry.yarnpkg.com/@codemirror/commands/-/commands-6.3.3.tgz#03face5bf5f3de0fc4e09b177b3c91eda2ceb7e9"+ integrity sha512-dO4hcF0fGT9tu1Pj1D2PvGvxjeGkbC6RGcZw6Qs74TH+Ed1gw98jmUgd2axWvIZEqTeTuFrg1lEB1KV6cK9h1A==+ dependencies:+ "@codemirror/language" "^6.0.0"+ "@codemirror/state" "^6.4.0"+ "@codemirror/view" "^6.0.0"+ "@lezer/common" "^1.1.0"++"@codemirror/language@^6.0.0":+ version "6.10.1"+ resolved "https://registry.yarnpkg.com/@codemirror/language/-/language-6.10.1.tgz#428c932a158cb75942387acfe513c1ece1090b05"+ integrity sha512-5GrXzrhq6k+gL5fjkAwt90nYDmjlzTIJV8THnxNFtNKWotMIlzzN+CpqxqwXOECnUdOndmSeWntVrVcv5axWRQ==+ dependencies:+ "@codemirror/state" "^6.0.0"+ "@codemirror/view" "^6.23.0"+ "@lezer/common" "^1.1.0"+ "@lezer/highlight" "^1.0.0"+ "@lezer/lr" "^1.0.0"+ style-mod "^4.0.0"++"@codemirror/lint@^6.0.0", "@codemirror/lint@^6.5.0":+ version "6.5.0"+ resolved "https://registry.yarnpkg.com/@codemirror/lint/-/lint-6.5.0.tgz#ea43b6e653dcc5bcd93456b55e9fe62e63f326d9"+ integrity sha512-+5YyicIaaAZKU8K43IQi8TBy6mF6giGeWAH7N96Z5LC30Wm5JMjqxOYIE9mxwMG1NbhT2mA3l9hA4uuKUM3E5g==+ dependencies:+ "@codemirror/state" "^6.0.0"+ "@codemirror/view" "^6.0.0"+ crelt "^1.0.5"++"@codemirror/search@^6.0.0":+ version "6.5.6"+ resolved "https://registry.yarnpkg.com/@codemirror/search/-/search-6.5.6.tgz#8f858b9e678d675869112e475f082d1e8488db93"+ integrity sha512-rpMgcsh7o0GuCDUXKPvww+muLA1pDJaFrpq/CCHtpQJYz8xopu4D1hPcKRoDD0YlF8gZaqTNIRa4VRBWyhyy7Q==+ dependencies:+ "@codemirror/state" "^6.0.0"+ "@codemirror/view" "^6.0.0"+ crelt "^1.0.5"++"@codemirror/state@^6.0.0", "@codemirror/state@^6.4.0", "@codemirror/state@^6.4.1":+ version "6.4.1"+ resolved "https://registry.yarnpkg.com/@codemirror/state/-/state-6.4.1.tgz#da57143695c056d9a3c38705ed34136e2b68171b"+ integrity sha512-QkEyUiLhsJoZkbumGZlswmAhA7CBU02Wrz7zvH4SrcifbsqwlXShVXg65f3v/ts57W3dqyamEriMhij1Z3Zz4A==++"@codemirror/view@^6.0.0", "@codemirror/view@^6.17.0", "@codemirror/view@^6.23.0":+ version "6.26.0"+ resolved "https://registry.yarnpkg.com/@codemirror/view/-/view-6.26.0.tgz#ab5a85aa8ebfb953cb5534e07d0a3751f9a3869a"+ integrity sha512-nSSmzONpqsNzshPOxiKhK203R6BvABepugAe34QfQDbNDslyjkqBuKgrK5ZBvqNXpfxz5iLrlGTmEfhbQyH46A==+ dependencies:+ "@codemirror/state" "^6.4.0"+ style-mod "^4.1.0"+ w3c-keyname "^2.2.4"+"@esbuild/[email protected]":version "0.18.20"resolved "https://registry.yarnpkg.com/@esbuild/android-arm64/-/android-arm64-0.18.20.tgz#984b4f9c8d0377443cc2dfcef266d02244593622"@@ -268,6 +332,25 @@"@jridgewell/resolve-uri" "^3.1.0""@jridgewell/sourcemap-codec" "^1.4.14"+"@lezer/common@^1.0.0", "@lezer/common@^1.1.0":+ version "1.2.1"+ resolved "https://registry.yarnpkg.com/@lezer/common/-/common-1.2.1.tgz#198b278b7869668e1bebbe687586e12a42731049"+ integrity sha512-yemX0ZD2xS/73llMZIK6KplkjIjf2EvAHcinDi/TfJ9hS25G0388+ClHt6/3but0oOxinTcQHJLDXh6w1crzFQ==++"@lezer/highlight@^1.0.0":+ version "1.2.0"+ resolved "https://registry.yarnpkg.com/@lezer/highlight/-/highlight-1.2.0.tgz#e5898c3644208b4b589084089dceeea2966f7780"+ integrity sha512-WrS5Mw51sGrpqjlh3d4/fOwpEV2Hd3YOkp9DBt4k8XZQcoTHZFB7sx030A6OcahF4J1nDQAa3jXlTVVYH50IFA==+ dependencies:+ "@lezer/common" "^1.0.0"++"@lezer/lr@^1.0.0":+ version "1.4.0"+ resolved "https://registry.yarnpkg.com/@lezer/lr/-/lr-1.4.0.tgz#ed52a75dbbfbb0d1eb63710ea84c35ee647cb67e"+ integrity sha512-Wst46p51km8gH0ZUmeNrtpRYmdlRHUpN1DQd3GFAyKANi8WVz8c2jHYTf1CVScFaCjQw1iO3ZZdqGDxQPRErTg==+ dependencies:+ "@lezer/common" "^1.0.0"+"@nodelib/[email protected]":version "2.1.5"resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"@@ -902,6 +985,19 @@ code-red@^1.0.3:estree-walker "^3.0.3"periscopic "^3.1.0"+codemirror@^6.0.1:+ version "6.0.1"+ resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-6.0.1.tgz#62b91142d45904547ee3e0e0e4c1a79158035a29"+ integrity sha512-J8j+nZ+CdWmIeFIGXEFbFPtpiYacFMDR8GlHK3IyHQJMCaVRfGx9NT+Hxivv1ckLWPvNdZqndbr/7lVhrf/Svg==+ dependencies:+ "@codemirror/autocomplete" "^6.0.0"+ "@codemirror/commands" "^6.0.0"+ "@codemirror/language" "^6.0.0"+ "@codemirror/lint" "^6.0.0"+ "@codemirror/search" "^6.0.0"+ "@codemirror/state" "^6.0.0"+ "@codemirror/view" "^6.0.0"+color-convert@^2.0.1:version "2.0.1"resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-2.0.1.tgz#72d3a68d598c9bdb3af2ad1e84f21d896abd4de3"@@ -934,6 +1030,11 @@ cookie@^0.5.0:resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.5.0.tgz#d1f5d71adec6558c58f389987c366aa47e994f8b"integrity sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==+crelt@^1.0.5:+ version "1.0.6"+ resolved "https://registry.yarnpkg.com/crelt/-/crelt-1.0.6.tgz#7cc898ea74e190fb6ef9dae57f8f81cf7302df72"+ integrity sha512-VQ2MBenTq1fWZUH9DJNGti7kKv6EeAuYr3cLwxUWhIu1baTaXh4Ib5W2CqHVqib4/MqbYGJqiL3Zb8GJZr3l4g==+cross-spawn@^7.0.0, cross-spawn@^7.0.2:version "7.0.3"resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"@@ -2251,6 +2352,11 @@ strip-json-comments@^3.1.1:resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==+style-mod@^4.0.0, style-mod@^4.1.0:+ version "4.1.2"+ resolved "https://registry.yarnpkg.com/style-mod/-/style-mod-4.1.2.tgz#ca238a1ad4786520f7515a8539d5a63691d7bf67"+ integrity sha512-wnD1HyVqpJUI2+eKZ+eo1UwghftP6yuFheBqqe+bWCotBjC2K1YnteJILRMs3SM4V/0dLEW1SC27MWP5y+mwmw==+sucrase@^3.32.0:version "3.35.0"resolved "https://registry.yarnpkg.com/sucrase/-/sucrase-3.35.0.tgz#57f17a3d7e19b36d8995f06679d121be914ae263"@@ -2567,6 +2673,11 @@ vitefu@^0.2.4:resolved "https://registry.yarnpkg.com/vitefu/-/vitefu-0.2.5.tgz#c1b93c377fbdd3e5ddd69840ea3aa70b40d90969"integrity sha512-SgHtMLoqaeeGnd2evZ849ZbACbnwQCIwRH57t18FxcXoZop0uQu0uzlIhJBlF/eWVzuce0sHeqPcDo+evVcg8Q==+w3c-keyname@^2.2.4:+ version "2.2.8"+ resolved "https://registry.yarnpkg.com/w3c-keyname/-/w3c-keyname-2.2.8.tgz#7b17c8c6883d4e8b86ac8aba79d39e880f8869c5"+ integrity sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==+"wasm@link:../harper-wasm/pkg":version "0.0.0"uid ""