Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/arnog/mathlive
Browse files Browse the repository at this point in the history
  • Loading branch information
arnog committed Apr 17, 2024
2 parents 867c638 + 3298a06 commit 9ed7914
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 341 deletions.
672 changes: 356 additions & 316 deletions CHANGELOG.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/core/context-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function getDefaultContext(): ContextInterface {
placeholderSymbol: '▢',
letterShapeStyle: l10n.locale.startsWith('fr') ? 'french' : 'tex',
minFontScale: 0,
maxMatrixCols: 10,
colorMap: defaultColorMap,
backgroundColorMap: defaultBackgroundColorMap,
getMacro: (token) => getMacroDefinition(token, getMacros()),
Expand Down
2 changes: 2 additions & 0 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class Context implements ContextInterface {
readonly color: string;
readonly backgroundColor: string;
readonly minFontScale: number;
readonly maxMatrixCols: number;

// `size` is the "base" font size (need to add `mathstyle.sizeDelta`
// to get effective size)
Expand Down Expand Up @@ -129,6 +130,7 @@ export class Context implements ContextInterface {

this.letterShapeStyle = template.letterShapeStyle;
this.minFontScale = template.minFontScale;
this.maxMatrixCols = template.maxMatrixCols;

if (style?.color && style.color !== 'none') this.color = style.color;
else this.color = this.parent?.color ?? '';
Expand Down
8 changes: 7 additions & 1 deletion src/core/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,13 @@ export class Parser {

this.endContext();

return def.createAtom(envName, array, rowGaps, args);
return def.createAtom(
envName,
array,
rowGaps,
args,
this.context.maxMatrixCols
);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export interface ContextInterface {
readonly smartFence: boolean;
readonly letterShapeStyle: 'tex' | 'french' | 'iso' | 'upright';
readonly minFontScale: number;
readonly maxMatrixCols: number;
readonly placeholderSymbol: string;
readonly colorMap: (name: string) => string | undefined;
readonly backgroundColorMap: (name: string) => string | undefined;
Expand Down
6 changes: 6 additions & 0 deletions src/editor-mathfield/mathfield-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,10 @@ If you are using Vue, this may be because you are using the runtime-only build o
return this.options.minFontScale;
}

get maxMatrixCols(): number {
return this.options.maxMatrixCols;
}

/** Returns styles shared by all selected atoms */
get selectionStyle(): Readonly<Style> {
if (this.model.selectionIsCollapsed) return this.effectiveStyle;
Expand Down Expand Up @@ -767,6 +771,7 @@ If you are using Vue, this may be because you are using the runtime-only build o
'backgroundColorMap' in config ||
'letterShapeStyle' in config ||
'minFontScale' in config ||
'maxMatrixCols' in config ||
'readOnly' in config ||
'placeholderSymbol' in config
)
Expand Down Expand Up @@ -1854,6 +1859,7 @@ If you are using Vue, this may be because you are using the runtime-only build o
smartFence: this.smartFence,
letterShapeStyle: this.letterShapeStyle,
minFontScale: this.minFontScale,
maxMatrixCols: this.maxMatrixCols,
placeholderSymbol: this.options.placeholderSymbol ?? '▢',
colorMap: (name) => this.colorMap(name),
backgroundColorMap: (name) => this.backgroundColorMap(name),
Expand Down
1 change: 1 addition & 0 deletions src/editor-mathfield/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export function getDefault(): Required<_MathfieldOptions> {
backgroundColorMap: defaultBackgroundColorMap,
letterShapeStyle: l10n.locale.startsWith('fr') ? 'french' : 'tex',
minFontScale: 0,
maxMatrixCols: 10,

smartMode: false,
smartFence: true,
Expand Down
37 changes: 14 additions & 23 deletions src/latex-commands/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ export function makeEnvironment(
name: Environment,
content: (readonly Atom[])[][] = [[[]]],
rowGaps: readonly Dimension[] = [],
args: readonly (null | Argument)[] = []
args: readonly (null | Argument)[] = [],
maxMatrixCols?: number
): ArrayAtom {
content = defaultContent(
content,
Expand Down Expand Up @@ -272,39 +273,39 @@ export function makeEnvironment(
mathstyleName: 'textstyle',
leftDelim: '(',
rightDelim: ')',
columns: defaultColumns(args[0]),
columns: defaultColumns(args[0], maxMatrixCols),
});
case 'bmatrix':
case 'bmatrix*':
return new ArrayAtom(name, content, rowGaps, {
mathstyleName: 'textstyle',
leftDelim: '[',
rightDelim: ']',
columns: defaultColumns(args[0]),
columns: defaultColumns(args[0], maxMatrixCols),
});
case 'Bmatrix':
case 'Bmatrix*':
return new ArrayAtom(name, content, rowGaps, {
mathstyleName: 'textstyle',
leftDelim: '\\lbrace',
rightDelim: '\\rbrace',
columns: defaultColumns(args[0]),
columns: defaultColumns(args[0], maxMatrixCols),
});
case 'vmatrix':
case 'vmatrix*':
return new ArrayAtom(name, content, rowGaps, {
mathstyleName: 'textstyle',
leftDelim: '\\vert',
rightDelim: '\\vert',
columns: defaultColumns(args[0]),
columns: defaultColumns(args[0], maxMatrixCols),
});
case 'Vmatrix':
case 'Vmatrix*':
return new ArrayAtom(name, content, rowGaps, {
mathstyleName: 'textstyle',
leftDelim: '\\Vert',
rightDelim: '\\Vert',
columns: defaultColumns(args[0]),
columns: defaultColumns(args[0], maxMatrixCols),
});
case 'matrix':
case 'matrix*':
Expand All @@ -314,13 +315,13 @@ export function makeEnvironment(
mathstyleName: 'textstyle',
leftDelim: '.',
rightDelim: '.',
columns: defaultColumns(args?.[0]),
columns: defaultColumns(args?.[0], maxMatrixCols),
});
case 'smallmatrix':
case 'smallmatrix*':
return new ArrayAtom(name, content, rowGaps, {
mathstyleName: 'scriptstyle',
columns: defaultColumns(args?.[0]),
columns: defaultColumns(args?.[0], maxMatrixCols),
colSeparationType: 'small',
arraystretch: 0.5,
});
Expand Down Expand Up @@ -384,19 +385,9 @@ export function makeEnvironment(
});
}

function defaultColumns(args: null | Argument): ColumnFormat[] {
return (
(args as ColumnFormat[]) ?? [
{ align: 'c' },
{ align: 'c' },
{ align: 'c' },
{ align: 'c' },
{ align: 'c' },
{ align: 'c' },
{ align: 'c' },
{ align: 'c' },
{ align: 'c' },
{ align: 'c' },
]
);
function defaultColumns(
args: null | Argument,
maxMatrixCols: number = 10
): ColumnFormat[] {
return (args as ColumnFormat[]) ?? Array(maxMatrixCols).fill({ align: 'c' });
}
3 changes: 2 additions & 1 deletion src/latex-commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@ export type EnvironmentConstructor = (
name: Environment,
array: (readonly Atom[])[][],
rowGaps: readonly Dimension[],
args: readonly (null | Argument)[]
args: readonly (null | Argument)[],
maxMatrixCols: number
) => Atom | null;
13 changes: 13 additions & 0 deletions src/public/mathfield-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export interface MathfieldElementAttributes {
'default-mode': string;
'letter-shape-style': string;
'min-font-scale': number;
'max-matrix-cols': number;
'popover-policy': string;
/**
* The LaTeX string to insert when the spacebar is pressed (on the physical or
Expand Down Expand Up @@ -468,6 +469,7 @@ const DEPRECATED_OPTIONS = {
* | `default-mode` | `mf.defaultMode` |
* | `letter-shape-style` | `mf.letterShapeStyle` |
* | `min-font-scale` | `mf.minFontScale` |
* | `max-matrix-cols` | `mf.maxMatrixCols` |
* | `popover-policy` | `mf.popoverPolicy` |
* | `math-mode-space` | `mf.mathModeSpace` |
* | `read-only` | `mf.readOnly` |
Expand Down Expand Up @@ -544,6 +546,7 @@ export class MathfieldElement extends HTMLElement implements Mathfield {
'default-mode': 'string',
'letter-shape-style': 'string',
'min-font-scale': 'number',
'max-matrix-cols': 'number',
'popover-policy': 'string',

'math-mode-space': 'string',
Expand Down Expand Up @@ -2228,6 +2231,16 @@ import 'https://unpkg.com/@cortex-js/compute-engine?module';
this._setOptions({ minFontScale: value });
}

/** @category Customization
* @inheritDoc LayoutOptions.maxMatrixCols
*/
get maxMatrixCols(): number {
return this._getOption('maxMatrixCols');
}
set maxMatrixCols(value: number) {
this._setOptions({ maxMatrixCols: value });
}

/** @category Customization
* @inheritDoc EditingOptions.smartMode
*/
Expand Down
7 changes: 7 additions & 0 deletions src/public/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,13 @@ mf.macros = {
* **Default**: `0`
*/
minFontScale: number;

/**
* Sets the maximum number of columns for the matrix environment. The default is
* 10 columns to match the behavior of the amsmath matrix environment.
* **Default**: `10`
*/
maxMatrixCols: number;
};

/**
Expand Down
5 changes: 5 additions & 0 deletions test/playwright-test-page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ <h1>Playwright End-to-end Test</h1>
#mf-5: latex mode disabled
</div>

<div>
<math-field max-matrix-cols="11" id="mf-6""></math-field>
#mf-6: max-matrix-cols=11
</div>

<textarea readonly disabled id="ta-1">hello world</textarea>

<math-field id="mf-prompt" readonly virtual-keyboard-mode="onfocus"
Expand Down
32 changes: 32 additions & 0 deletions test/playwright-tests/max-matrix-cols.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { MathfieldElement } from '../../src/public/mathfield-element';

import { test, expect } from '@playwright/test';

test('max-matrix-rows attribute', async ({ page }) => {
await page.goto('/dist/playwright-test-page/');

const testLatex = String.raw`\begin{bmatrix} a & b & c & d & e & f & g & h & i & j & k \\ l & m & n & o & p & q & r & s & t & u & v \end{bmatrix}`;

await page
.locator('#mf-1')
.evaluate(
(e: MathfieldElement, latex: string) => (e.value = latex),
testLatex
);

await page
.locator('#mf-6')
.evaluate(
(e: MathfieldElement, latex: string) => (e.value = latex),
testLatex
);

// check latex (default #mf-1 should wrap after 10 columns, #mf-6 with max-matrix-cols=11 should not wrap)
expect(
await page.locator('#mf-1').evaluate((e: MathfieldElement) => e.getValue('latex-expanded'))
).toBe("\\begin{bmatrix}a & b & c & d & e & f & g & h & i & j\\\\ k & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{}\\\\ l & m & n & o & p & q & r & s & t & u\\\\ v & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{} & \\placeholder{}\\end{bmatrix}");

expect(
await page.locator('#mf-6').evaluate((e: MathfieldElement) => e.getValue('latex-expanded'))
).toBe("\\begin{bmatrix}a & b & c & d & e & f & g & h & i & j & k\\\\ l & m & n & o & p & q & r & s & t & u & v\\end{bmatrix}");
});

0 comments on commit 9ed7914

Please sign in to comment.