mathlive icon indicating copy to clipboard operation
mathlive copied to clipboard

Inconsistent Behavior of Enter Key vs Ctrl+Enter for Line Breaks in <math-field>

Open andrea78 opened this issue 9 months ago • 2 comments

There appears to be an inconsistency in how the Enter key and Ctrl+Enter are handled for inserting line breaks in <math-field>, as observed on the official demo page (https://cortexjs.io/mathfield/demo/).

Steps to Reproduce:

  1. Go to https://cortexjs.io/mathfield/demo/.
  2. In the <math-field> input, type an expression (e.g., "x = 1").
  3. Press Enter (without Ctrl).
  4. Press Ctrl+Enter to insert a new line.
  5. In the second line, type another expression (e.g., "y = 2").
  6. Press Enter (without Ctrl) to insert a new line.

Expected Behavior:

Both Enter and Ctrl+Enter should consistently insert a new line, regardless of whether it's the first or subsequent line, or there should be clear documentation explaining the differing behavior.

Actual Behavior:

On the first line, only Ctrl+Enter inserts a new line; pressing Enter alone does nothing. On the second line and beyond, both Enter and Ctrl+Enter insert a new line.

Additional Details:

It’s unclear if this is intentional or a bug, but the inconsistency can confuse users expecting uniform key behavior.

Suggested Improvement:

Standardize the behavior of Enter and Ctrl+Enter across all lines (e.g., both always insert a new line). Alternatively, document the intended behavior in the MathLive documentation if this is by design.

Environment:

MathLive version: 0.105.0 Browser: [Chrome 134.0.6998.89, Firefox 136.0.1, Opera 117.0.5408.93] OS: [Windows 11]

andrea78 avatar Mar 27 '25 15:03 andrea78

The current behavior is intentional, but I can see why it could be confusing.

A mathfield can be either in single line mode or in multiline mode. When in single line mode, the return key triggers an input event. To enter in multiline mode from single line mode, you can press ctrl+enter. Once in multiline mode, pressing enter no longer triggers an input event, but add a new line instead. Note that once in multiline mode, even if there's a single line, pressing enter will add a new line.

Alternatively, you can start the mathfield in multiline mode by setting the value to \displaylines{}.

I'm not sure how best to improve over the current behavior. One option would be to require an explicit setting to allow multiline mode. If the multiline attribute is set to false, pressing ctrl+enter would do nothing, and pressing enter would trigger the input event. If the multiline attribute is set to true, pressing enter would always add a new line. Would that be a better behavior?

arnog avatar Mar 27 '25 16:03 arnog

Thanks @arnog for the detailed explanation! I understand now that the behavior is intentional and tied to the distinction between single-line and multiline modes, with Ctrl+Enter serving as the transition mechanism. It makes sense in the context of mathematical input, but I agree it can still catch users off guard due to the shift in Enter’s behavior after the first line break.

Your suggestion about an explicit multiline attribute sounds like a promising improvement. Having a clear setting to toggle multiline mode would make the behavior more predictable:

  • If multiline="false" (default), Enter always triggers an input event, and Ctrl+Enter does nothing, keeping it strictly single-line.
  • If multiline="true", Enter consistently adds a new line (even from the start), and Ctrl+Enter could either do the same or be reserved for a different action if needed.

This way, users could opt into multiline mode explicitly (e.g., via config or \displaylines{}), and the Enter key’s role would remain consistent within the chosen mode. It might also help to mention this distinction briefly in the demo or docs, as new users might not expect the mode switch.

It's a balance between flexibility and clarity. Thanks for addressing this.

andrea78 avatar Mar 27 '25 18:03 andrea78