Skip to content

Commit

Permalink
fix: follow up to fix for fractionNavigationOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
arnog committed Nov 12, 2024
1 parent 256b09b commit 3cc8a0a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/atoms/genfrac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/core/atom-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/core/math-environment.ts
Original file line number Diff line number Diff line change
@@ -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',
};
6 changes: 6 additions & 0 deletions src/editor-mathfield/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
28 changes: 22 additions & 6 deletions src/public/mathfield-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/public/mathlive-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*
*/

import '../core/math-environment';

import { Atom } from '../core/atom-class';

import '../latex-commands/definitions';
Expand Down

0 comments on commit 3cc8a0a

Please sign in to comment.