From 3cc8a0a4d2b2938291803fd8357a9a584cd2b5f2 Mon Sep 17 00:00:00 2001 From: Arno Gourdol Date: Tue, 12 Nov 2024 08:24:04 -0800 Subject: [PATCH] fix: follow up to fix for fractionNavigationOrder --- src/atoms/genfrac.ts | 4 ++-- src/core/atom-class.ts | 3 +-- src/core/math-environment.ts | 8 ++++++++ src/editor-mathfield/render.ts | 6 ++++++ src/public/mathfield-element.ts | 28 ++++++++++++++++++++++------ src/public/mathlive-ssr.ts | 2 ++ 6 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 src/core/math-environment.ts diff --git a/src/atoms/genfrac.ts b/src/atoms/genfrac.ts index 41be9d977..39fabc548 100644 --- a/src/atoms/genfrac.ts +++ b/src/atoms/genfrac.ts @@ -7,7 +7,7 @@ import { makeCustomSizedDelim, makeNullDelimiter } from '../core/delimiters'; import { Context } from '../core/context'; import { AXIS_HEIGHT } from '../core/font-metrics'; import type { AtomJson } from 'core/types'; -import { MathfieldElement } from 'public/mathlive'; +import { _MathEnvironment } from 'core/math-environment'; export type GenfracOptions = { continuousFraction?: boolean; @@ -91,7 +91,7 @@ export class GenfracAtom extends Atom { if (this._children) return this._children; const result: Atom[] = []; - if (MathfieldElement.fractionNavigationOrder === 'denominator-numerator') { + if (_MathEnvironment.fractionNavigationOrder === 'denominator-numerator') { for (const x of this.below!) { result.push(...x.children); result.push(x); diff --git a/src/core/atom-class.ts b/src/core/atom-class.ts index 68e747b2a..d5c3df27f 100644 --- a/src/core/atom-class.ts +++ b/src/core/atom-class.ts @@ -4,7 +4,7 @@ import { PT_PER_EM, X_HEIGHT } from './font-metrics'; import { boxType, Box } from './box'; import { makeLimitsStack, VBox } from './v-box'; import { joinLatex, latexCommand } from './tokenizer'; -import { Mode, weightString } from './modes-utils'; +import { Mode } from './modes-utils'; import { getDefinition } from '../latex-commands/definitions-utils'; import { Context } from './context'; @@ -20,7 +20,6 @@ import type { Branch, } from './types'; import type { Argument } from 'latex-commands/types'; -import { addBold } from 'editor-model/styling'; /** * The order of these branches specify the default keyboard navigation order. diff --git a/src/core/math-environment.ts b/src/core/math-environment.ts new file mode 100644 index 000000000..35197a5b6 --- /dev/null +++ b/src/core/math-environment.ts @@ -0,0 +1,8 @@ +/** + * Math environment is a collection of settings that are used to configure the math rendering and thay may apply even when there is no mathfield (i.e. in SSR). + */ +export const _MathEnvironment: { + fractionNavigationOrder: 'denominator-numerator' | 'numerator-denominator'; +} = { + fractionNavigationOrder: 'numerator-denominator', +}; diff --git a/src/editor-mathfield/render.ts b/src/editor-mathfield/render.ts index 998dbe856..1baa533be 100644 --- a/src/editor-mathfield/render.ts +++ b/src/editor-mathfield/render.ts @@ -424,3 +424,9 @@ export function reparse(mathfield: _Mathfield | null): void { model.silenceNotifications = wasSilent; requestUpdate(mathfield); } + +export function reparseAllMathfields(): void { + for (const mathfield of document.querySelectorAll('.ML__mathfield')) { + if ('_mathfield' in mathfield) reparse(mathfield._mathfield as _Mathfield); + } +} diff --git a/src/public/mathfield-element.ts b/src/public/mathfield-element.ts index ad0e3a1ce..14e8a00b3 100644 --- a/src/public/mathfield-element.ts +++ b/src/public/mathfield-element.ts @@ -37,7 +37,10 @@ import { isTouchCapable, } from '../ui/utils/capabilities'; import { resolveUrl } from '../common/script-url'; -import { reparse, requestUpdate } from '../editor-mathfield/render'; +import { + reparseAllMathfields, + requestUpdate, +} from '../editor-mathfield/render'; import { reloadFonts, loadFonts } from '../core/fonts'; import { defaultSpeakHook } from '../editor/speech'; import { defaultReadAloudHook } from '../editor/speech-read-aloud'; @@ -49,6 +52,7 @@ import { Scrim } from '../ui/utils/scrim'; import { isOffset, isRange, isSelection } from 'editor-model/selection-utils'; import { KeyboardModifiers } from './ui-events-types'; import { defaultInsertStyleHook } from 'editor-mathfield/styling'; +import { _MathEnvironment } from 'core/math-environment'; /** @category MathJSON */ export declare type Expression = @@ -1046,9 +1050,23 @@ export class MathfieldElement extends HTMLElement implements Mathfield { * **Default**: `"numerator-denominator"` * @category Localization */ - static fractionNavigationOrder: + static get fractionNavigationOrder(): | 'numerator-denominator' - | 'denominator-numerator' = 'numerator-denominator'; + | 'denominator-numerator' { + return _MathEnvironment.fractionNavigationOrder; + } + static set fractionNavigationOrder( + s: 'numerator-denominator' | 'denominator-numerator' + ) { + if (s !== 'numerator-denominator' && s !== 'denominator-numerator') + throw new Error('Invalid value'); + if (_MathEnvironment.fractionNavigationOrder === s) return; + + _MathEnvironment.fractionNavigationOrder = s; + + // Invalidate all mathfields on the page + reparseAllMathfields(); + } /** * A custom compute engine instance. If none is provided, a default one is @@ -1098,9 +1116,7 @@ export class MathfieldElement extends HTMLElement implements Mathfield { static set isFunction(value: (command: string) => boolean) { this._isFunction = value; - document.querySelectorAll('math-field').forEach((el) => { - if (el instanceof MathfieldElement) reparse(el._mathfield); - }); + reparseAllMathfields(); } static async loadSound( diff --git a/src/public/mathlive-ssr.ts b/src/public/mathlive-ssr.ts index 48f2dcf7c..a6b7fd2cd 100644 --- a/src/public/mathlive-ssr.ts +++ b/src/public/mathlive-ssr.ts @@ -6,6 +6,8 @@ * */ +import '../core/math-environment'; + import { Atom } from '../core/atom-class'; import '../latex-commands/definitions';