Editor engine: markdown text as the live doc, hidden syntax, offline, sync #33

Closed
kayg wants to merge 0 commits from issue/7-editor-core-3 into dev
Owner

Part of #7 (engine only; the look waits for the mockup in #13).

What's in it

packages/editor (CodeMirror 6 + Yjs, ADR 0018–0022, 0036)

  • One shared text (md) per note. The editor is built from it and rebuilt from it on reattach, never the other way round (inSync() checks it).
  • Default mode hides markdown: #, **, _, ~~, inline code marks, link syntax, >, bullets become a bullet, - [ ]/[x]/[/]/[-] become a checkbox, --- becomes a rule, fences are hidden. Replace decorations + atomic ranges, built from a fully parsed tree before first paint. A lone cursor is kept out of hidden leading syntax. Frontmatter is hidden as a block. Source mode shows everything.
  • Block controls in the margins (handle, "Make it a note", "More"), absolutely positioned, one measure per hover, never on the H1, blank lines or frontmatter; plain unstyled buttons with labels.
  • Typing shortcuts: finishing [[Title]] and picking from [[/@ both write [Title](<Title.md>), paths relative to the note, no ./. Only on the person's own keystrokes (input handler + completion source); remote text is never transformed.
  • Undo tracks only this editor's changes. IME: remote and other-tab changes wait behind a gate while a composition is open.
  • Sync client: y-websocket protocol, plus save checks 100 <id> → 101 <id> so "Saved" is claimed only after the server confirms (works with servers that don't know them: they just never say saved). Lineage on the URL; a 4409 close means the server's copy was replaced, and only the changes since the last confirmed base are replayed (diff-match-patch), or kept as a copy if they can't be placed.
  • Local copy: own IndexedDB persistence (replaces y-indexeddb). Found and fixed a data-loss trap: a tab closed within milliseconds of typing offline lost its last letters (4/40 runs). Unconfirmed changes are now written synchronously to localStorage on pagehide/hidden, and rescued on open (now 40/40). Compaction merges what's stored, so another tab's changes are never dropped.
  • Tabs share a note through BroadcastChannel. Client ids are never stored. Persistent storage is requested.
  • test-server/: a stand-in note room server speaking exactly the contract above, for tests until #6.

apps/web: plain Svelte 5 + Vite shell: tiny router (/, /notes/<id>, ?mode=source, #block), one store keyed by id fed by journal entries (dedupes by seq, resets on expired token), live socket client for /api/v1/live, a note page with a source-mode toggle and an honest status line (role=status), explicit ready signals (html[data-ready], [data-synced]).

Contract for #6 (the Rust notes server)

  • GET /api/v1/notes/<id>/sync?lineage=<l> websocket, y-websocket messages; send step 1 on connect; broadcast updates to every connection.
  • Answer 100 <id> with 101 <id> in order, after the changes before it are durable (history written).
  • Close with 4409 (reason = current lineage) when the client's lineage differs. Only the server seeds a doc from file text, and it writes meta.lineage.

Tests

  • Unit (vitest): 45 passing: hidden-syntax ranges, prefixes, blocks, links/paths, replay and emoji-safe edits, sync (convergence, saved-only-when-confirmed, offline + reconnect, offline delete not resurrected, IME gate, lineage refusal, lineage replay), editor (shared text, [[ ]], remote text untouched, undo only own typing, list continuation, modes, cursor), local copy (reopen, rescue, pagehide flush, compaction across tabs).
  • Browser (Playwright, Chromium + WebKit, 390 + 1440): 12 tests × 4 projects, all green, run 3× without a flake. Locators by role and name, waits on the ready signal. CLS and IME tests are Chromium-only (WebKit has no Layout Instability API; IME is driven through CDP).
  • Typecheck: tsc (editor) and svelte-check (web), clean.

Perf (keystroke to next frame, 200-paragraph note, PERF=1, 1 worker)

p50 p95 max
Chromium 1440 11.6 ms 18.7 ms 21.5 ms
Chromium 390 10.7 ms 18.2 ms 19.5 ms
WebKit 1440 (software GL) 20 ms 47 ms 204 ms

Measured on the shared dev box under load from other jobs; the real gate belongs on the perf VM. Chromium p95 < 50 ms is asserted; WebKit is reported only (software rendering). Bundle: 524 KB min / 172 KB gzip (avoided markdown(), which pulled in HTML/CSS/JS languages, ~180 KB).

CI

Adds a web job using calternal-ci-web:1.63.0 built from ci/web.Dockerfile (Playwright 1.63.0 image + bun 1.4.2). The image needs building on the CI host before this job can go green.

Deferred

  • Multiplexing note sync over the one live socket (the Transport interface is ready; needs #6).
  • Leader-tab election (tabs share via BroadcastChannel; each tab writes its own changes).
  • Note picker UI for [[/@ (engine exposes search/resolve), block actions behaviour (→ make note, ⋯ menu), keyboard access to block controls, all styling: after #13.
  • Images/embeds rendering.

🤖 Generated with Claude Code

https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T

Part of #7 (engine only; the look waits for the mockup in #13). ## What's in it **`packages/editor`** (CodeMirror 6 + Yjs, ADR 0018–0022, 0036) - One shared text (`md`) per note. The editor is built from it and rebuilt from it on reattach, never the other way round (`inSync()` checks it). - **Default mode** hides markdown: `#`, `**`, `_`, `~~`, inline code marks, link syntax, `>`, bullets become a bullet, `- [ ]`/`[x]`/`[/]`/`[-]` become a checkbox, `---` becomes a rule, fences are hidden. Replace decorations + atomic ranges, built from a fully parsed tree before first paint. A lone cursor is kept out of hidden leading syntax. Frontmatter is hidden as a block. **Source mode** shows everything. - **Block controls** in the margins (handle, "Make it a note", "More"), absolutely positioned, one measure per hover, never on the H1, blank lines or frontmatter; plain unstyled buttons with labels. - **Typing shortcuts**: finishing `[[Title]]` and picking from `[[`/`@` both write `[Title](<Title.md>)`, paths relative to the note, no `./`. Only on the person's own keystrokes (input handler + completion source); remote text is never transformed. - **Undo** tracks only this editor's changes. **IME**: remote and other-tab changes wait behind a gate while a composition is open. - **Sync client**: y-websocket protocol, plus save checks `100 <id>` → `101 <id>` so "Saved" is claimed only after the server confirms (works with servers that don't know them: they just never say saved). Lineage on the URL; a 4409 close means the server's copy was replaced, and only the changes since the last confirmed base are replayed (diff-match-patch), or kept as a copy if they can't be placed. - **Local copy**: own IndexedDB persistence (replaces y-indexeddb). Found and fixed a data-loss trap: a tab closed within milliseconds of typing offline lost its last letters (4/40 runs). Unconfirmed changes are now written synchronously to localStorage on `pagehide`/hidden, and rescued on open (now 40/40). Compaction merges what's stored, so another tab's changes are never dropped. - Tabs share a note through BroadcastChannel. Client ids are never stored. Persistent storage is requested. - `test-server/`: a stand-in note room server speaking exactly the contract above, for tests until #6. **`apps/web`**: plain Svelte 5 + Vite shell: tiny router (`/`, `/notes/<id>`, `?mode=source`, `#block`), one store keyed by id fed by journal entries (dedupes by seq, resets on expired token), live socket client for `/api/v1/live`, a note page with a source-mode toggle and an honest status line (`role=status`), explicit ready signals (`html[data-ready]`, `[data-synced]`). ## Contract for #6 (the Rust notes server) - `GET /api/v1/notes/<id>/sync?lineage=<l>` websocket, y-websocket messages; send step 1 on connect; broadcast updates to every connection. - Answer `100 <id>` with `101 <id>` in order, **after** the changes before it are durable (history written). - Close with **4409** (reason = current lineage) when the client's lineage differs. Only the server seeds a doc from file text, and it writes `meta.lineage`. ## Tests - Unit (vitest): 45 passing: hidden-syntax ranges, prefixes, blocks, links/paths, replay and emoji-safe edits, sync (convergence, saved-only-when-confirmed, offline + reconnect, offline delete not resurrected, IME gate, lineage refusal, lineage replay), editor (shared text, `[[ ]]`, remote text untouched, undo only own typing, list continuation, modes, cursor), local copy (reopen, rescue, pagehide flush, compaction across tabs). - Browser (Playwright, Chromium + WebKit, 390 + 1440): 12 tests × 4 projects, all green, run 3× without a flake. Locators by role and name, waits on the ready signal. CLS and IME tests are Chromium-only (WebKit has no Layout Instability API; IME is driven through CDP). - Typecheck: `tsc` (editor) and `svelte-check` (web), clean. ## Perf (keystroke to next frame, 200-paragraph note, `PERF=1`, 1 worker) | | p50 | p95 | max | |---|---|---|---| | Chromium 1440 | 11.6 ms | 18.7 ms | 21.5 ms | | Chromium 390 | 10.7 ms | 18.2 ms | 19.5 ms | | WebKit 1440 (software GL) | 20 ms | 47 ms | 204 ms | Measured on the shared dev box under load from other jobs; the real gate belongs on the perf VM. Chromium p95 < 50 ms is asserted; WebKit is reported only (software rendering). Bundle: 524 KB min / 172 KB gzip (avoided `markdown()`, which pulled in HTML/CSS/JS languages, ~180 KB). ## CI Adds a `web` job using **`calternal-ci-web:1.63.0`** built from `ci/web.Dockerfile` (Playwright 1.63.0 image + bun 1.4.2). The image needs building on the CI host before this job can go green. ## Deferred - Multiplexing note sync over the one live socket (the `Transport` interface is ready; needs #6). - Leader-tab election (tabs share via BroadcastChannel; each tab writes its own changes). - Note picker UI for `[[`/`@` (engine exposes `search`/`resolve`), block actions behaviour (→ make note, ⋯ menu), keyboard access to block controls, all styling: after #13. - Images/embeds rendering. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Editor engine: markdown text as the live doc, hidden syntax, offline, sync
Some checks failed
CI / rust (pull_request) Successful in 37s
CI / web (pull_request) Failing after 44s
1b0aa28953
packages/editor (CodeMirror 6 + Yjs):
- One shared text per note; the editor is built from it and rebuilt on
  reattach, never the other way round.
- Default mode hides markdown syntax (replace decorations + atomic ranges,
  parsed before first paint); source mode shows it all, frontmatter too.
- Margin block controls (handle, make-note, more) that never move text and
  never show on the title or blank lines.
- [[Title]] and @ produce standard links, only on the person's own typing.
- Undo tracks only local changes; remote changes wait during IME composition.
- y-websocket sync plus save checks (100/101) so "Saved" is only claimed
  when the server confirms; lineage mismatch replays only offline changes.
- Local copy in IndexedDB with a synchronous write-ahead list on page hide
  (fixes losing the last keystrokes when a tab closes right after typing).
- Tabs share the note over BroadcastChannel.
- A stand-in note server for tests until #6 lands.

apps/web: plain Svelte 5 shell with a tiny router, one journal-fed store,
the live socket client, and a note page. The look waits for #13.

CI: a web job in calternal-ci-web (ci/web.Dockerfile, to be built on the
CI host).

Part of #7

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Author
Owner

Superseded by #34: same work plus the root-cause fix for the layout shift CI caught (lines drawn one frame before their decorations), a CPU-throttled regression test, and the date format. Closing this one.

Superseded by #34: same work plus the root-cause fix for the layout shift CI caught (lines drawn one frame before their decorations), a CPU-throttled regression test, and the date format. Closing this one.
kayg closed this pull request 2026-10-13 17:20:41 +00:00
Some checks failed
CI / rust (pull_request) Successful in 37s
CI / web (pull_request) Failing after 44s

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kayg/calternal!33
No description provided.