From bcc67d916a62b7d1b0843660452afa668f305f12 Mon Sep 17 00:00:00 2001 From: Arno Gourdol Date: Wed, 15 Nov 2023 10:56:15 -0800 Subject: [PATCH] feat: Simplify syntax for modifying registers --- CHANGELOG.md | 8 ++++++++ src/common/stylesheet.ts | 3 +++ src/editor-mathfield/render.ts | 3 ++- src/public/core-types.ts | 25 ++++++++++++++++++------- src/public/mathfield-element.ts | 26 +++++++++++++++++++++++--- 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f95ec7623..a02ab108d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## [Unreleased] + +### Improvements + +- Simplified the syntax to modify registers. Use `mf.registers.arraystretch = 1.5` + instead of mf.registers = {...mf.registers, arraystretch: 1.5}` + + ## 0.96.0 (2023-11-14) ### Breaking Changes diff --git a/src/common/stylesheet.ts b/src/common/stylesheet.ts index 5aba5051c..ef7a62b1f 100644 --- a/src/common/stylesheet.ts +++ b/src/common/stylesheet.ts @@ -82,6 +82,7 @@ export function getStylesheet(id: StylesheetId): CSSStyleSheet { gStylesheets[id] = new CSSStyleSheet(); + // @ts-ignore gStylesheets[id]!.replaceSync(getStylesheetContent(id)); return gStylesheets[id]!; @@ -103,6 +104,7 @@ export function injectStylesheet(id: StylesheetId): void { if ((gInjectedStylesheets[id] ?? 0) !== 0) gInjectedStylesheets[id]! += 1; else { const stylesheet = getStylesheet(id); + // @ts-ignore document.adoptedStyleSheets = [...document.adoptedStyleSheets, stylesheet]; gInjectedStylesheets[id] = 1; } @@ -116,6 +118,7 @@ export function releaseStylesheet(id: StylesheetId): void { gInjectedStylesheets[id]! -= 1; if (gInjectedStylesheets[id]! <= 0) { const stylesheet = gStylesheets[id]!; + // @ts-ignore document.adoptedStyleSheets = document.adoptedStyleSheets.filter( (x) => x !== stylesheet ); diff --git a/src/editor-mathfield/render.ts b/src/editor-mathfield/render.ts index 3a042a89d..983489ea7 100644 --- a/src/editor-mathfield/render.ts +++ b/src/editor-mathfield/render.ts @@ -30,9 +30,10 @@ function hash(latex: string): number { } export function requestUpdate( - mathfield: MathfieldPrivate, + mathfield: MathfieldPrivate | undefined | null, options?: { interactive: boolean } ): void { + if (!mathfield) return; if (mathfield.dirty) return; mathfield.dirty = true; requestAnimationFrame(() => { diff --git a/src/public/core-types.ts b/src/public/core-types.ts index aab85b054..e6e653da2 100644 --- a/src/public/core-types.ts +++ b/src/public/core-types.ts @@ -300,19 +300,30 @@ export type LatexValue = { relax?: boolean } & ( // } /** - * TeX registers represent 'variables' and 'constants'. + * TeX registers represent "variables" and "constants". * * Changing the values of some registers can modify the layout * of math expressions. * * The following registers might be of interest: * - * - `thinmuskip` - * - `medmuskip` - * - `thickmuskip` - * - `nulldelimiterspace` - * - `delimitershortfall` - * - `jot` + * - `thinmuskip`: space between items of math lists + * - `medmuskip`: space between binary operations + * - `thickmuskip`: space between relational operators + * - `nulldelimiterspace`: minimum space to leave blank in delimiter constructions, for example around a fraction + * - `delimitershortfall`: maximum space to overlap adjacent elements when a delimiter is too short + * - `jot`: space between lines in an array, or between rows in a multiline construct + * - `arraycolsep`: space between columns in an array + * - `arraystretch`: factor by which to stretch the height of each row in an array + * + * To modify a register, use: + * + * ```javascript + * mf.registers.arraystretch = 1.5; + * mf.registers.thinmuskip = { dimension: 2, unit: "mu" }; + * mf.registers.medmuskip = "3mu"; + *``` + * */ export type Registers = Record; diff --git a/src/public/mathfield-element.ts b/src/public/mathfield-element.ts index b421a7608..8bd6708c7 100644 --- a/src/public/mathfield-element.ts +++ b/src/public/mathfield-element.ts @@ -1,6 +1,7 @@ import type { Selector } from './commands'; import type { LatexSyntaxError, + LatexValue, MacroDictionary, ParseMode, Registers, @@ -1129,11 +1130,13 @@ export class MathfieldElement extends HTMLElement implements Mathfield { this.attachShadow({ mode: 'open', delegatesFocus: true }); if (this.shadowRoot && 'adoptedStyleSheets' in this.shadowRoot) { + // @ts-ignore this.shadowRoot!.adoptedStyleSheets = [ getStylesheet('core'), getStylesheet('mathfield'), getStylesheet('mathfield-element'), ]; + // @ts-ignore this.shadowRoot!.innerHTML = ``; } else { this.shadowRoot!.innerHTML = `