Skip to content

feat(ui): wire CodeMirror into anon landing SQL editor#63

Merged
ringo380 merged 1 commit into
mainfrom
feat/anon-codemirror
May 14, 2026
Merged

feat(ui): wire CodeMirror into anon landing SQL editor#63
ringo380 merged 1 commit into
mainfrom
feat/anon-codemirror

Conversation

@ringo380
Copy link
Copy Markdown
Owner

Why

PR #55's minimalist anon landing intentionally shipped a plain <textarea> for the inline-grading flow. The downside: the rich editor (syntax highlighting, line numbers, bracket matching, hints) only appears once a user signs up and reaches /grade/ — a jarring feature regression on the surface most prospects see first. User reported the absence directly.

What changed

Mirror the /grade/ CodeMirror setup on the anon textarea (index.html only — base.html and other pages stay light):

  • Load CodeMirror CSS (codemirror.min, material-darker, show-hint) in extra_head, gated to {% if not user.is_authenticated %}.
  • Load JS bundle just before the existing inline IIFE: codemirror.min, sql mode, show-hint, sql-hint, matchbrackets, closebrackets.
  • Initialize on #id_sql_query with the same options as /grade/ (SQL mode, window.QG.cmTheme(), indent 2, line wrapping, matchBrackets, autoCloseBrackets, sql-hint tables).
  • Cmd/Ctrl+Enter wired via extraKeys; the legacy keydown listener kept only as a fallback for the unenhanced textarea path.
  • editor.save() on every change so the underlying textarea stays in sync — new FormData(form) continues to read from it.
  • Registered via window.QG.registerEditor() so the light/dark toggle flips the editor theme alongside the page.
  • New accessors sqlValue() / setSqlValue() / focusSql() abstract textarea-vs-editor so existing reset and error-handling paths work transparently.

Out of scope (deliberately omitted vs /grade/)

Code folding and fullscreen mode. The anon flow is meant to be a single-screen quick taste; folding/fullscreen are friction without payoff for one-query users.

Verification

  • python manage.py check → 0 issues
  • Smoke render GET / → 200, body contains CodeMirror CSS + JS + CodeMirror.fromTextArea init call
  • AJAX submit path unchanged (editor.save() syncs the textarea before new FormData reads it)

Test plan

  • Anon / shows the SQL example query with line numbers + syntax highlighting + bracket matching
  • Cmd/Ctrl+Enter submits the form (CodeMirror extraKeys)
  • Type a partial keyword like SEL — sql-hint dropdown appears
  • Click "Grade my query" with an empty editor → red border + error message
  • After a successful grade, "Grade another query" link clears the editor and refocuses it
  • Toggle dark/light mode — editor theme follows
  • Authenticated / (dashboard) does NOT load CodeMirror (extra_head is gated)

PR #55's minimalist anon landing intentionally shipped a plain
<textarea> for the inline-grading flow — fast page weight, simple
AJAX path. The downside is that the rich editor experience (syntax
highlighting, line numbers, bracket matching, auto-close, SQL hints)
disappears the moment someone tries the product, and only reappears
once they've signed up and reached /grade/. The drop-off is jarring
and the user-visible feature regression isn't worth the bytes saved.

Mirror the /grade/ CodeMirror setup on the anon textarea:

- Load codemirror.min + material-darker theme + show-hint CSS in
  extra_head (anon-only — authenticated home stays unaffected).
- Load codemirror.min + sql mode + show-hint + sql-hint + matchbrackets
  + closebrackets JS just before the existing inline IIFE.
- Initialize on #id_sql_query with: SQL mode, theme via
  window.QG.cmTheme(), line numbers, indent 2, line wrapping,
  matchBrackets, autoCloseBrackets, sql-hint tables matching /grade/.
- Cmd/Ctrl+Enter is wired via extraKeys; the legacy keydown listener
  is kept only as a fallback if CodeMirror failed to load.
- Editor.save() on every change so the underlying textarea stays in
  sync — new FormData(form) continues to read from it for AJAX submit.
- Register via window.QG.registerEditor() so the light/dark toggle
  flips the editor theme alongside the rest of the page.
- New accessors sqlValue() / setSqlValue() / focusSql() abstract
  textarea-vs-editor so the existing error-handling and reset paths
  work transparently.

Out of scope (deliberately omitted vs /grade/): code folding, fullscreen
mode. The anon flow is meant to be a single-screen quick taste; those
features are friction without payoff for one-query users.
@ringo380 ringo380 merged commit 355585b into main May 14, 2026
@ringo380 ringo380 deleted the feat/anon-codemirror branch May 14, 2026 04:40
ringo380 added a commit that referenced this pull request May 14, 2026
PR #63 wired CodeMirror into the anon page but the editor rendered empty
because CodeMirror 5 needs the `placeholder` addon for the `placeholder:`
option to take effect — without it the option is silently ignored.

Load `addon/display/placeholder.min.js` alongside the other CodeMirror
scripts so the SELECT example query renders in the editor body as a
light-gray hint, matching the prior textarea behaviour.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant