Submissions draft editor: autosave-refresh steals focus + add click-variable-in-preview → jump to field #92
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
m's report (2026-05-25 14:58)
Scope — two related polish fixes to the submission-draft editor
A. Autosave-refresh steals focus
The variable-editor sidebar autosaves on edit; the autosave triggers a preview re-render; the re-render replaces preview DOM and (apparently) blows away the focused form-field reference. User has to click back into the input to keep typing.
Likely cause options:
Fix the smallest correct one. Add a regression test or playwright smoke if practical.
B. Click-variable-in-preview → jump to form field
The preview pane renders the merged template with substituted values. Make each rendered variable a tiny clickable target so the user can click
{{project.case_number}}'s rendered value and the variable-editor sidebar scrolls to + focuses that field.Implementation sketch:
<span class="draft-var" data-var="project.case_number">…</span>(preview-only; export-to-.docx unchanged)..draft-varreadsdata-var, scrolls the sidebar to the matching input, focuses it, and adds a brief lime-accent flash.{{firm.name}}from config) get a non-clickable rendering OR a tooltip explaining "from config".Decide whether to use
data-varor a different attribute; coder picks the cleanest collision-free name.Files most likely touched
frontend/src/client/submission-draft.ts— autosave debounce + preview render handler + new click handlerfrontend/src/submission-draft.tsx— if the preview pane container needs structural changeinternal/services/submission_draft_service.go/internal/services/submission_render.go— render-to-HTML output: wrap substitutions in<span class="draft-var" data-var="...">(only the preview path, NOT the export-to-.docx path)frontend/src/styles/global.css—.draft-var:hover+.draft-var--flashaccent.draft-varHard rules
<span class="draft-var">is HTML-preview-only. The Word .docx must not carry these spans.go build ./... && go test ./internal/... && cd frontend && bun run buildclean.mai/<worker>/draft-editor-focus-jump.Out of scope
Reporting
mai report completedwith branch + SHAs + UX path: A) type in a field, wait for autosave + preview refresh, confirm cursor stays in the field; B) click on a substituted variable in the preview, confirm sidebar scrolls + focuses the corresponding input with a brief flash.Done — both polish fixes shipped together. m/paliad#92 has two parts; commit covers both.
(A) Autosave focus preservation
Root cause —
flushAutosave→paintVariables()doeshost.innerHTML = html, which replaces every input element. The focused-input reference dies with the old DOM tree, the cursor drops out. (Confirmed infrontend/src/client/submission-draft.ts:594.)Fix — option 3 from the issue's three sketches. New helpers
captureVarFocus()/restoreVarFocus():data-varkey +selectionStart+selectionEnd+selectionDirection.data-var, callfocus()+setSelectionRange(start, end, dir).<input>and<textarea>via the sharedHTMLInputElement | HTMLTextAreaElementcontract (per hard-rule in the issue).flushAutosave— the only spot wherepaintVariables()runs while a user is mid-edit. Rename/reset go through dedicated flows that don't preserve the typing position (intentional — they don't reuse the same input).(B) Click variable in preview → jump
Renderer side (
internal/services/submission_merge.go) — added avalueWrapperFnplumbed through the substitution chain (substituteInDocumentXML→substituteInTextNodes/substituteAcrossRuns→replacePlaceholders).Render()(.docx export) passesnil.RenderHTML()(preview) passeshtmlPreviewWrapperwhich wraps each substituted value with three Private-Use-Area sentinels (U+E100, U+E101, U+E102).emitTextWithDraftVars()— invoked insidedocXMLToHTML— converts each[BEGIN]key[MID]value[END]triplet into<span class="draft-var" data-var="<key>">value-html-escaped</span>. PUA chars survivexmlEncode/xmlDecode/htmlEscapeunchanged and are stripped at the HTML emission step.Why sentinels and not e.g. a second-pass regex on the placeholder map: the value itself can contain anything (
UPC_CFI_123/2025,M&S <Inc> "X"); regex-matching the value back to a key is fragile. Sentinels carry the key inline, survive XML round-trip, never collide with template text.Hard rule (.docx unchanged) — new test
TestRender_DocxOutputUnchangedByPreviewWrapasserts the .docx export carries<w:t>HLC</w:t>and contains zero occurrences ofdraft-var/data-var/ any of the three sentinels. Passes — the wrap is HTML-preview-only.Missing markers —
[KEIN WERT: foo]text is also wrapped in<span class="draft-var">, so clicking a missing variable in the preview jumps to its empty input. Useful in practice: lawyer scans preview, sees red marker, clicks it, fills the value. New testTestRenderHTML_WrapsMissingMarkercovers this.Client side (
frontend/src/client/submission-draft.ts) —wireDraftVars()runs at the end ofpaintPreview(). For each.draft-var:.submission-draft-var-input[data-var="<key>"]exists in the sidebar: add.draft-var--has-input(cursor: pointer + brighter hover) androle=button+tabindex=0+aria-label.today.isothat aren't inVARIABLE_GROUPS): the span stays visually distinct (subtle lime tint) but non-interactive — silent no-op on click.scrollIntoView({ behavior: "smooth", block: "center" })→setTimeout(focus + select, 50)→flashRow()(lime accent fade-out).CSS — base
.draft-vargetsrgba(198, 244, 28, 0.12)background tint + 2px padding + box-decoration-break: clone (so multi-line spans render cleanly)..draft-var--has-inputlayers cursor: pointer + brighter hover. Row flash uses 1.2spaliad-var-flashkeyframe withprefers-reduced-motionfallback to asteps(1, end)non-animating swap.Verification
Playwright on the built
dist/submission-draft.htmlwith an injected populated state (sidebar inputs + preview HTML containing five.draft-varspans, one of which —today.iso— has no matching input):.draft-var--has-inputapplied to sidebar-backed keys.draft-var--has-inputNOT applied to derivedproject.case_numberspan → focus moves to that input.draft-varbackground tint visible on all five spans in previewBuild status
cd frontend && bun run build— cleango build ./...— cleango test -short ./internal/...— all packages pass, including 4 new/updated tests insubmission_merge_test.goBranch / commit
Branch:
mai/hermes/gitster-draft-editor-focus-jumpCommit:
7e66da8defReady for review / merge.