Skip to content

diagram: units editor accepts Enter, inserting literal newlines into the units string #829

Description

@bpowers

Problem

The units field in the variable details panel is semantically a single-line value, but it is rendered as a plain Slate editor with no single-line normalization and no onKeyDown handler:

  • src/diagram/VariableDetails.tsx: the units <Editable> (around line 534) has only renderLeaf, placeholder, spellCheck, and onBlur -- unlike the equation and notes editors, which deliberately handle newline chords. Pressing plain Enter (or Shift+Enter, which Slate handles by default) inserts a new block/newline.
  • stringFromDescendants -> plainSerialize (src/diagram/drawing/common.ts) joins Slate blocks with '\n', so on blur/save a literal \n is committed into the variable's units string.
  • src/diagram/ModuleDetails.tsx has the same pattern for its units editor (plainSerialize(unitsContents) at line ~109, no keydown handling).

Why it matters

Newlines inside a units expression are almost certainly meaningless or invalid to the engine's unit parser, so a stray Enter keypress silently corrupts the units string and can surface as a confusing unit error (or be persisted into the saved model). It's also a UX inconsistency: the sibling equation and documentation editors have explicit, commented newline-chord handling, while the single-line units field has none.

Possible approaches

  • Add an onKeyDown to the units <Editable> that intercepts Enter (and Shift/Cmd/Ctrl+Enter): either make Enter commit the edit (blur/save) or make it a no-op.
  • And/or add a withSingleLine Slate normalizer (normalizeNode merging blocks / stripping \n in insertText) so pasted multi-line content is also flattened -- keydown alone doesn't cover paste.
  • Alternatively, if multi-line units are intentionally tolerated, document that decision and verify the engine's unit parser treats \n as ordinary whitespace.

Applies to both VariableDetails.tsx and ModuleDetails.tsx units fields.

Discovery context

Identified while reviewing keyboard handling in VariableDetails.tsx (the equation editor's isNewlineChord handling made the units editor's complete absence of Enter handling stand out).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions