Skip to content

Commit

Permalink
include a11y in default settings, and fix a number of problesm with c…
Browse files Browse the repository at this point in the history
…hanging menu values. (mathjax/MathJax#3310)
  • Loading branch information
dpvc committed Nov 21, 2024
1 parent b5c184b commit d76a606
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 22 deletions.
3 changes: 2 additions & 1 deletion ts/components/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,13 @@ export abstract class Startup {
public static typesetPromise(elements: any[]): Promise<void> {
Startup.document.options.elements = elements;
Startup.document.reset();
return Startup.mathjax.handleRetriesFor(() => {
Startup.rerenderPromise = Startup.promise = Startup.mathjax.handleRetriesFor(() => {
Startup.document.render();
const promise = Promise.all(Startup.document.renderPromises);
Startup.document.renderPromises = [];
return promise;
});
return Startup.promise;
}

/**
Expand Down
23 changes: 21 additions & 2 deletions ts/output/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,17 @@ export abstract class CommonOutputJax<
if (linebreak) {
this.getLinebreakWidth();
}
const inlineMarked = !!math.root.getProperty('inlineMarked');
if (
this.options.linebreaks.inline &&
!math.display &&
!math.outputData.inlineMarked
!inlineMarked
) {
this.markInlineBreaks(math.root.childNodes?.[0]);
math.outputData.inlineMarked = true;
math.root.setProperty('inlineMarked', true);
} else if (!this.options.linebreaks.inline && inlineMarked) {
this.unmarkInlineBreaks(math.root);
math.root.setProperty('inlineMarked', false);
}
math.root.setTeXclass(null);
const wrapper = this.factory.wrap(math.root);
Expand Down Expand Up @@ -588,6 +592,21 @@ export abstract class CommonOutputJax<
return marked;
}

/**
* @param {MmlNode} node The node where inline breaks are to be removed
*/
public unmarkInlineBreaks(node: MmlNode) {
if (!node) return;
node.removeProperty('forcebreak');
node.removeProperty('breakable');
if (node.getProperty('process-breaks')) {
node.removeProperty('process-breaks');
for (const child of node.childNodes) {
this.unmarkInlineBreaks(child);
}
}
}

/**
* @override
*/
Expand Down
4 changes: 2 additions & 2 deletions ts/output/common/Wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,12 +1220,12 @@ export class CommonWrapper<
return ['left', 0];
}
if (!align || align === 'auto') {
align = this.jax.math.outputData.inlineMarked
align = this.jax.math.root.getProperty('inlineMarked')
? 'left'
: this.jax.options.displayAlign;
}
if (!shift || shift === 'auto') {
shift = this.jax.math.outputData.inlineMarked
shift = this.jax.math.root.getProperty('inlineMarked')
? '0'
: this.jax.options.displayIndent;
}
Expand Down
21 changes: 19 additions & 2 deletions ts/ui/menu/MJContextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export class MJContextMenu extends ContextMenu {
*/
public mathItem: MathItem<HTMLElement, Text, Document> = null;

/**
* True when the menu is posted while the explorer is in operation
*/
public refocus: boolean = false;

/**
* The document options
*/
Expand All @@ -80,6 +85,7 @@ export class MJContextMenu extends ContextMenu {
*/
public post(x?: any, y?: number) {
if (this.mathItem) {
this.refocus = document.activeElement.nodeName.toLowerCase() !== 'mjx-container';
if (y !== undefined) {
this.getOriginalMenu();
this.getSemanticsMenu();
Expand All @@ -99,9 +105,20 @@ export class MJContextMenu extends ContextMenu {
* @override
*/
public unpost() {
super.unpost();
this.mathItem?.typesetRoot?.blur();
this.mathItem = null;
if (this.refocus) {
super.unpost();
return;
}
//
// Prevent store.active.focus() from refocusing the menu in super.unpost()
// (no pretty way to do it without making changes to mj-context-menu)
//
const store = this.store;
const active = store.active;
(store as any)._active = document.body;
super.unpost();
store.active = active;
}

/*======================================================================*/
Expand Down
43 changes: 28 additions & 15 deletions ts/ui/menu/Menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,11 @@ export class Menu {
this.jax[jax.name] = jax;
this.settings.renderer = jax.name;
this.settings.scale = jax.options.scale;
this.defaultSettings = Object.assign({}, this.settings);
this.settings.overflow =
jax.options.displayOverflow.substring(0, 1).toUpperCase() +
jax.options.displayOverflow.substring(1).toLowerCase();
this.settings.breakInline = jax.options.linebreaks.inline;
this.defaultSettings = Object.assign({}, this.document.options.a11y, this.settings);
}

/**
Expand Down Expand Up @@ -1019,23 +1019,29 @@ export class Menu {
*/
protected setOverflow(overflow: string) {
this.document.outputJax.options.displayOverflow = overflow.toLowerCase();
this.document.rerender();
if (!Menu.loading) {
this.document.rerender();
}
}

/**
* @param {boolean} breaks The new in-line break value
*/
protected setInlineBreaks(breaks: boolean) {
this.document.outputJax.options.linebreaks.inline = breaks;
this.document.rerender();
if (!Menu.loading) {
this.document.rerender();
}
}

/**
* @param {string} scale The new scaling value
*/
protected setScale(scale: string) {
this.document.outputJax.options.scale = parseFloat(scale);
this.document.rerender();
if (!Menu.loading) {
this.document.rerender();
}
}

/**
Expand Down Expand Up @@ -1277,15 +1283,17 @@ export class Menu {
Menu.loading++; // pretend we're loading, to suppress rerendering for each variable change
const pool = this.menu.pool;
const settings = this.defaultSettings;
for (const name of Object.keys(this.settings) as (keyof MenuSettings)[]) {
for (const name of Object.keys(settings) as (keyof MenuSettings)[]) {
const variable = pool.lookup(name);
if (variable) {
variable.setValue(settings[name] as string | boolean);
const item = (variable as any).items[0];
if (item) {
item.executeCallbacks_();
if (variable.getValue() !== settings[name]) {
variable.setValue(settings[name] as string | boolean);
const item = (variable as any).items[0];
if (item) {
item.executeCallbacks_();
}
}
} else {
} else if (Object.hasOwn(this.settings, name)) {
(this.settings as any)[name] = settings[name];
}
}
Expand Down Expand Up @@ -1366,6 +1374,13 @@ export class Menu {
this.document = startup.document = startup.getDocument();
this.document.menu = this;
this.setA11y(this.settings);
this.defaultSettings =
Object.assign(
{},
this.document.options.a11y,
MathJax.config?.options?.a11y || {},
this.defaultSettings
);
this.document.outputJax.reset();
this.transferMathList(document);
this.document.processed = document.processed;
Expand Down Expand Up @@ -1468,11 +1483,8 @@ export class Menu {
math.root = root.copy(true);
math.root.setInheritedAttributes({}, math.display, 0, false);
if (breaks) {
math.root.walkTree((n) => {
n.removeProperty('process-breaks');
n.removeProperty('forcebreak');
n.removeProperty('breakable');
});
jax.unmarkInlineBreaks(math.root);
math.root.setProperty('inlineMarked', false);
}
const promise = mathjax.handleRetriesFor(() => {
jax.toDOM(math, div, jax.document);
Expand Down Expand Up @@ -1554,6 +1566,7 @@ export class Menu {
* @param {number=} start The state at which to start rerendering
*/
protected rerender(start: number = STATE.TYPESET) {
this.menu.refocus = false;
this.rerenderStart = Math.min(start, this.rerenderStart);
const startup = MathJax.startup;
if (!Menu.loading && startup.rerenderPromise) {
Expand Down

0 comments on commit d76a606

Please sign in to comment.