Skip to content

Commit

Permalink
Add test clients for codemirror and prosemirror
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathonherbert committed Apr 16, 2024
1 parent 20c29ef commit 5031090
Show file tree
Hide file tree
Showing 24 changed files with 469 additions and 51 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,35 @@ Why would we like it to know less? B/c less coupling with language means
Future refactor. Connect a typeahead client first.

Date typeahead: autofocus when the value is not yet present. Display but do not autofocus when value is present (even if incorrect.)

The input/overlay combination has a few edge cases are hard to address:
- [ ] Chrome does not issue a scroll event when the selection is programmatically changed https://issues.chromium.org/issues/41081857

Using contenteditable will also make it possible to render chips inline, without needing to use e.g. a Threads component, and preserve the syntax highlighting.

How do we handle chips as plain text? Is it possible? Two problems:
- We must render things which aren't content but are interactive, e.g. 'remove' icons. Suspect easily solved w/ non-contenteditable additions to appropriate tokens renderings.
- We must render things which _are_ content (from the POV of language) but are perhaps best non-interactive, e.g. colon char between meta key and val.

Try a plaintext rendering, see how it goes. The closer we can be to plaintext, the simpler the implementation and the fewer edge cases.

## Or, use a library

Don't do that:
- bundle size

But, maybe _do:_
- less code to maintain
- robustly solve edge cases w/ contenteditable, which is [gnarly](https://www.youtube.com/watch?v=EEF2DlOUkag)

What to use?

### CodeMirror
- Designed for languages, syntax highlighting, etc.
- Kinda large for a bare install: dist/assets/index-BJKhd53Z.js 358.90 kB │ gzip: 116.51 kB

### ProseMirror
- Team already know it
- Kinda a bit smaller – dist/assets/index-BKb-Hbln.js 176.79 kB │ gzip: 54.46 kB

Hmm.
80 changes: 29 additions & 51 deletions client/src/lib/Cql.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
const exampleQuery = '@from-date';
onMount(async () => {
applyTokens(exampleQuery, []);
fetchLanguageServer(exampleQuery);
});
let overlayOffset = 0;
let cqlQuery = exampleQuery;
let ast = '';
let ast = {};
let tokenisedChars = [];
let currentSuggestions = undefined;
let typeaheadOffsetPx = 0;
let inputElement: HTMLInputElement;
let datePickerElement: HTMLInputElement;
let inputElement: HTMLDivElement;
let datePickerElement: HTMLInputElement;
let cursorMarker: HTMLSpanElement;
let inputContainerElement: HTMLDivElement;
let menuIndex = 0;
const handleInput = (e: InputEvent) => {
fetchLanguageServer(e.target.value);
applyTokens(e.target.innerText, ast?.tokens || []);
fetchLanguageServer(e.target.innerText);
};
const handleInputKeydown = (e) => {
Expand All @@ -40,14 +41,14 @@
if (menuIndex > 0) menuIndex--;
break;
case 'ArrowDown':
if (currentSuggestions.suggestions.TextSuggestion) {
if (menuIndex < currentSuggestions.suggestions.TextSuggestion.suggestions.length - 1)
menuIndex++;
}
// Focus the datepicker if it's not already focussed
if (currentSuggestions.suggestions.DateSuggestion) {
datePickerElement.focus();
}
if (currentSuggestions.suggestions.TextSuggestion) {
if (menuIndex < currentSuggestions.suggestions.TextSuggestion.suggestions.length - 1)
menuIndex++;
}
// Focus the datepicker if it's not already focussed
if (currentSuggestions.suggestions.DateSuggestion) {
datePickerElement.focus();
}
break;
case 'Enter':
replaceCurrentSuggestionTargetWithMenuItem(menuIndex);
Expand Down Expand Up @@ -99,9 +100,10 @@
const newCaretPosition = token.start + strToInsert.length + 2;
// Is there a better way of waiting for Svelte to update the DOM?
requestAnimationFrame(() => {
setTimeout(() => {
inputElement.focus();
inputElement.setSelectionRange(newCaretPosition, newCaretPosition);
});
}, 10);
currentSuggestions = undefined;
Expand Down Expand Up @@ -188,32 +190,26 @@
};
});
};
const syncScrollState = (e: ScrollEvent) => {
overlayOffset = e.target.scrollLeft;
};
</script>

<div class="Cql__input-container" bind:this={inputContainerElement}>
<input
<div
class="Cql__input"
value={cqlQuery}
contenteditable="true"
on:input={handleInput}
on:keydown={handleInputKeydown}
on:focus={watchSelectionChange}
on:blur={unwatchSelectionChange}
on:scroll={syncScrollState}
bind:this={inputElement}
/>
<div class="Cql__overlay-container">
<div class="Cql__overlay" contenteditable="true" style="left: -{overlayOffset}px">
{#each tokenisedChars || [] as { char, token }, index}
<Token str={char} {token} />{#if index === currentSuggestions?.from - 1}<span
class="Cql__cursor-marker"
bind:this={cursorMarker}
></span>{/if}
{/each}
</div>
role="textbox"
tabindex=0
>
{#each tokenisedChars || [] as { char, token }, index}
<Token str={char} {token} />{#if index === currentSuggestions?.from - 1}<span
class="Cql__cursor-marker"
bind:this={cursorMarker}
></span>{/if}
{/each}
</div>
{#if currentSuggestions !== undefined}<div
class={`Cql__typeahead Cql__typeahead-${Object.keys(currentSuggestions.suggestions).join('')}`}
Expand All @@ -237,7 +233,7 @@
class="Cql_typeahead-date"
type="date"
use:onDatePickerInit
bind:this={datePickerElement}
bind:this={datePickerElement}
on:keydown={handleDateKeydown}
/>
{/if}
Expand All @@ -256,31 +252,13 @@
position: relative;
}
.Cql__overlay-container {
position: absolute;
pointer-events: none;
top: 7px;
left: 7px;
width: var(--input-width);
overflow: hidden;
white-space: nowrap;
}
.Cql__overlay {
position: relative;
}
.Cql__input {
width: var(--input-width);
border: 2px solid #666;
margin: 0;
padding: 5px;
color: transparent;
caret-color: #333;
}
.Cql__input,
.Cql__overlay-container {
font-family: sans-serif;
font-size: 16px;
}
Expand Down
24 changes: 24 additions & 0 deletions codemirror-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
Binary file added codemirror-client/bun.lockb
Binary file not shown.
13 changes: 13 additions & 0 deletions codemirror-client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
19 changes: 19 additions & 0 deletions codemirror-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "codemirror-client",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"devDependencies": {
"typescript": "^5.2.2",
"vite": "^5.2.0"
},
"dependencies": {
"@codemirror/state": "^6.4.1",
"codemirror": "^6.0.1"
}
}
1 change: 1 addition & 0 deletions codemirror-client/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions codemirror-client/src/CqlInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { basicSetup, EditorView } from "codemirror";
import { EditorState, Compartment } from "@codemirror/state";

export class CqlInput extends HTMLElement {
connectedCallback() {
const shadow = this.attachShadow({ mode: "closed" });

shadow.innerHTML = `<div id="cql-input"></div>`;
const cqlInput = shadow.getElementById("cql-input")!;

this.setupEditor(cqlInput);
}

setupEditor = (mountEl: HTMLElement) => {
const tabSize = new Compartment();

let state = EditorState.create({
extensions: [basicSetup, tabSize.of(EditorState.tabSize.of(8))],
});

new EditorView({
state,
parent: mountEl,
});
};
}
8 changes: 8 additions & 0 deletions codemirror-client/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { CqlInput } from "./CqlInput";
import "./style.css";

customElements.define("cql-input", CqlInput);

document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
<cql-input />
`;
96 changes: 96 additions & 0 deletions codemirror-client/src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vanilla:hover {
filter: drop-shadow(0 0 2em #3178c6aa);
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
1 change: 1 addition & 0 deletions codemirror-client/src/typescript.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions codemirror-client/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
Loading

0 comments on commit 5031090

Please sign in to comment.