refactor: avoid recursive updates in mathLiveInput by normalizing value before updateing

This commit is contained in:
meinzzzz 2025-11-23 13:34:22 +01:00
parent 56834cb88a
commit 1471a72633

View File

@ -283,33 +283,29 @@ export default class MainFormView extends View {
const mathLiveInput = new MathLiveInputView( this.locale ); const mathLiveInput = new MathLiveInputView( this.locale );
const onInput = () => { const onInput = () => {
const rawValue = mathLiveInput.value ?? ''; let equationInput = ( mathLiveInput.value ?? '' ).trim();
let equationInput = rawValue.trim();
// If input has delimiters // If input has delimiters, strip them and update the display mode.
if ( hasDelimiters( equationInput ) ) { if ( hasDelimiters( equationInput ) ) {
// Get equation without delimiters
const params = extractDelimiters( equationInput ); const params = extractDelimiters( equationInput );
// Remove delimiters from input field
mathLiveInput.value = params.equation;
equationInput = params.equation; equationInput = params.equation;
// Update display button and preview
this.displayButtonView.isOn = params.display; this.displayButtonView.isOn = params.display;
} }
const normalizedEquation = equationInput.length ? equationInput : null; const normalizedEquation = equationInput.length ? equationInput : null;
// Update self if needed.
if ( mathLiveInput.value !== normalizedEquation ) { if ( mathLiveInput.value !== normalizedEquation ) {
mathLiveInput.value = normalizedEquation; mathLiveInput.value = normalizedEquation;
} }
// Sync to raw LaTeX textarea // Sync to raw LaTeX textarea if its value is different.
this.rawLatexInputView.value = equationInput; if ( this.rawLatexInputView.value !== equationInput ) {
this.rawLatexInputView.value = equationInput;
}
if ( this.previewEnabled && this.mathView ) { // Update preview if enabled and its value is different.
// Update preview if ( this.previewEnabled && this.mathView && this.mathView.value !== equationInput ) {
this.mathView.value = equationInput; this.mathView.value = equationInput;
} }
}; };