@pie-lib/math-input-mathlive 2.0.1-next.20261110124518 → 2.0.1-next.20261112105900

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ import MathInput from './math-input.js';
4
4
  import HorizontalKeypad from './horizontal-keypad.js';
5
5
  import KeyPad from './keypad/index.js';
6
6
  import * as mf from './mf/index.js';
7
+ import { deepActiveElement } from './mf/inactive-caret.js';
7
8
  import { registerEmbed, applyStaticMath, latexToMarkup, loadMathLive, configureFonts, getMacros, PIE_MACROS } from './mathlive-instance.js';
8
9
  import { toMathLive, fromMathLive, fieldIds, keyToAction, answerBlockLatex, NEWLINE_EMBED } from './latex-bridge.js';
9
10
  declare const addBrackets: (s: any) => any;
@@ -17,4 +18,4 @@ declare const removeBrackets: (s: any) => any;
17
18
  * export is kept so callers do not break.
18
19
  */
19
20
  declare const updateSpans: () => void;
20
- export { keysForGrade, addBrackets, removeBrackets, keys, MathInput, HorizontalKeypad, KeyPad, updateSpans, mf, mf as mq, registerEmbed, applyStaticMath, latexToMarkup, loadMathLive, configureFonts, getMacros, PIE_MACROS, toMathLive, fromMathLive, fieldIds, keyToAction, answerBlockLatex, NEWLINE_EMBED, };
21
+ export { keysForGrade, addBrackets, removeBrackets, keys, MathInput, HorizontalKeypad, KeyPad, updateSpans, mf, mf as mq, registerEmbed, applyStaticMath, latexToMarkup, loadMathLive, configureFonts, getMacros, PIE_MACROS, toMathLive, fromMathLive, fieldIds, keyToAction, answerBlockLatex, NEWLINE_EMBED, deepActiveElement, };
package/dist/index.js CHANGED
@@ -4,8 +4,9 @@ import { MathInput as n } from "./math-input.js";
4
4
  import r from "./horizontal-keypad.js";
5
5
  import { KeyPad as i } from "./keypad/index.js";
6
6
  import { mf_exports as a } from "./mf/index.js";
7
- import { PIE_MACROS as o, applyStaticMath as s, configureFonts as c, getMacros as l, latexToMarkup as u, loadMathLive as d, registerEmbed as f } from "./mathlive-instance.js";
8
- import { NEWLINE_EMBED as p, answerBlockLatex as m, fieldIds as h, fromMathLive as g, keyToAction as _, toMathLive as v } from "./latex-bridge.js";
7
+ import { deepActiveElement as o } from "./mf/inactive-caret.js";
8
+ import { PIE_MACROS as s, applyStaticMath as c, configureFonts as l, getMacros as u, latexToMarkup as d, loadMathLive as f, registerEmbed as p } from "./mathlive-instance.js";
9
+ import { NEWLINE_EMBED as m, answerBlockLatex as h, fieldIds as g, fromMathLive as _, keyToAction as v, toMathLive as y } from "./latex-bridge.js";
9
10
  // src/index.tsx
10
- var y = (e) => e.indexOf("\\(") === 0 ? e : `\\(${e}`, b = (e) => e.indexOf("\\)") === e.length - 2 ? e : `${e}\\)`, x = (e) => e.indexOf("\\(") === 0 ? e.substring(2) : e, S = (e) => e.indexOf("\\)") === e.length - 2 ? e.substring(0, e.length - 2) : e, C = (e) => b(y(e)), w = (e) => S(x(e)), T = () => {};
11
- export { r as HorizontalKeypad, i as KeyPad, n as MathInput, p as NEWLINE_EMBED, o as PIE_MACROS, C as addBrackets, m as answerBlockLatex, s as applyStaticMath, c as configureFonts, h as fieldIds, g as fromMathLive, l as getMacros, _ as keyToAction, t as keys, e as keysForGrade, u as latexToMarkup, d as loadMathLive, a as mf, a as mq, f as registerEmbed, w as removeBrackets, v as toMathLive, T as updateSpans };
11
+ var b = (e) => e.indexOf("\\(") === 0 ? e : `\\(${e}`, x = (e) => e.indexOf("\\)") === e.length - 2 ? e : `${e}\\)`, S = (e) => e.indexOf("\\(") === 0 ? e.substring(2) : e, C = (e) => e.indexOf("\\)") === e.length - 2 ? e.substring(0, e.length - 2) : e, w = (e) => x(b(e)), T = (e) => C(S(e)), E = () => {};
12
+ export { r as HorizontalKeypad, i as KeyPad, n as MathInput, m as NEWLINE_EMBED, s as PIE_MACROS, w as addBrackets, h as answerBlockLatex, c as applyStaticMath, l as configureFonts, o as deepActiveElement, g as fieldIds, _ as fromMathLive, u as getMacros, v as keyToAction, t as keys, e as keysForGrade, d as latexToMarkup, f as loadMathLive, a as mf, a as mq, p as registerEmbed, T as removeBrackets, y as toMathLive, E as updateSpans };
@@ -95,6 +95,18 @@ export declare const fieldTemplate: (latex: any) => any;
95
95
  * @returns {string}
96
96
  */
97
97
  export declare const fillFields: (latex: any, values: any) => string;
98
+ /**
99
+ * Stored latex with each answer block replaced by its answer, `values[id]`, and
100
+ * no `\MathQuillMathField` wrapper left around it: `x=\MathQuillMathField[r1]{}`
101
+ * with `{ r1: '2' }` gives `x=2`. This is what MathQuill's `latex()` returned for
102
+ * a static field, since a MathQuillMathField serialised as its content only,
103
+ * and it is the form math-inline scores in Advanced Multi.
104
+ *
105
+ * @param {string} latex
106
+ * @param {Record<string, string>} values
107
+ * @returns {string}
108
+ */
109
+ export declare const inlineFields: (latex: any, values: any) => string;
98
110
  /**
99
111
  * Latex for a text alternative: the same expression without the display-only
100
112
  * markup that MathLive's MathML and speech conversions drop with its content.
@@ -140,6 +152,16 @@ declare const MATHQUILL_ALIASES: {
140
152
  * @returns {string}
141
153
  */
142
154
  export declare const toCanonicalCommands: (latex: any) => any;
155
+ /**
156
+ * Put braces around every argument of the commands in `BRACED_ARGUMENT_COUNT`:
157
+ * `\frac12` -> `\frac{1}{2}`, `2\frac34` -> `2\frac{3}{4}`, `\sqrt2` ->
158
+ * `\sqrt{2}`, at any depth. Latex whose arguments are already braced comes back
159
+ * unchanged, whitespace included.
160
+ *
161
+ * @param {string} latex
162
+ * @returns {string}
163
+ */
164
+ export declare const braceArguments: (latex: any) => any;
143
165
  /**
144
166
  * Convert latex out of MathLive back into the stored form, so items authored
145
167
  * with the MathLive editor remain readable by @pie-lib/math-rendering and by
@@ -90,9 +90,9 @@ var e = "\\embed{newLine}[]", t = /^\\displaylines\{([\s\S]*)\}$/, n = "\\MathQu
90
90
  }, A = (e) => {
91
91
  let t = {};
92
92
  return typeof e == "string" && i(e, n, (e, n) => (t[e || "r1"] = n, "")), t;
93
- }, j = (e) => typeof e == "string" ? i(e, n, (e) => `${n}[${e || "r1"}]{}`) : e, M = (e, t) => i(e, n, (e, r) => {
94
- let i = e || "r1";
95
- return `${n}[${i}]{${t[i] === void 0 ? r : t[i]}}`;
93
+ }, j = (e) => typeof e == "string" ? i(e, n, (e) => `${n}[${e || "r1"}]{}`) : e, M = (e, t) => i(e, n, (e, n) => {
94
+ let r = e || "r1";
95
+ return t[r] === void 0 ? n : t[r];
96
96
  }), N = (e) => {
97
97
  if (typeof e != "string" || !e) return e || "";
98
98
  let t = (e, t, n) => {
@@ -157,11 +157,80 @@ var e = "\\embed{newLine}[]", t = /^\\displaylines\{([\s\S]*)\}$/, n = "\\MathQu
157
157
  } else i === "{" ? t.push(/[\^_]\s*$/.test(n)) : i === "}" && t.pop(), n += i, r += 1;
158
158
  }
159
159
  return n;
160
- }, H = (e) => typeof e == "string" ? V(e.replace(I, (e) => F[e]).replace(L, (e, t) => z(t))) : e, U = (n) => {
160
+ }, H = (e) => typeof e == "string" ? V(e.replace(I, (e) => F[e]).replace(L, (e, t) => z(t))) : e, U = {
161
+ "\\frac": 2,
162
+ "\\dfrac": 2,
163
+ "\\tfrac": 2,
164
+ "\\cfrac": 2,
165
+ "\\binom": 2,
166
+ "\\dbinom": 2,
167
+ "\\tbinom": 2,
168
+ "\\sqrt": 1,
169
+ "\\overline": 1,
170
+ "\\underline": 1,
171
+ "\\overrightarrow": 1,
172
+ "\\overleftrightarrow": 1,
173
+ "\\overparen": 1,
174
+ "\\vec": 1,
175
+ "\\hat": 1,
176
+ "\\bar": 1
177
+ }, W = /^\\(?:[a-zA-Z]+|.)/, G = (e, t) => {
178
+ let n = 0;
179
+ for (let r = t + 1; r < e.length; r++) {
180
+ let t = e[r];
181
+ if (t === "\\") r += 1;
182
+ else if (t === "{") n += 1;
183
+ else if (t === "}") --n;
184
+ else if (t === "]" && n === 0) return r;
185
+ }
186
+ return -1;
187
+ }, K = (e, t) => {
188
+ let n = e[t];
189
+ if (n === "{") {
190
+ let n = r(e, t);
191
+ return n === -1 ? void 0 : {
192
+ content: e.slice(t + 1, n),
193
+ end: n + 1
194
+ };
195
+ }
196
+ return n !== void 0 && /^[0-9a-zA-Z]$/.test(n) ? {
197
+ content: n,
198
+ end: t + 1
199
+ } : void 0;
200
+ }, q = (e) => {
201
+ if (typeof e != "string" || e.indexOf("\\") === -1) return e;
202
+ let t = "", n = 0;
203
+ for (; n < e.length;) {
204
+ if (e[n] !== "\\") {
205
+ t += e[n], n += 1;
206
+ continue;
207
+ }
208
+ let r = e.slice(n).match(W), i = r ? r[0] : "\\";
209
+ t += i, n += i.length;
210
+ let a = U[i];
211
+ if (!a) continue;
212
+ let o = () => {
213
+ let r = n;
214
+ for (; e[n] === " ";) n += 1;
215
+ t += e.slice(r, n);
216
+ };
217
+ if (i === "\\sqrt" && (o(), e[n] === "[")) {
218
+ let r = G(e, n);
219
+ r !== -1 && (t += `[${q(e.slice(n + 1, r))}]`, n = r + 1);
220
+ }
221
+ for (let r = 0; r < a; r++) {
222
+ o();
223
+ let r = K(e, n);
224
+ if (!r) break;
225
+ t += `{${q(r.content)}}`, n = r.end;
226
+ }
227
+ }
228
+ return t;
229
+ }, J = (n) => {
161
230
  if (typeof n != "string" || !n) return n || "";
162
231
  let r = n.trim().match(t), o = n;
163
- return r && (o = a(r[1], "\\\\").map((e) => e.trim()).join(e)), o = i(o, "\\placeholder", (e, t) => `\\MathQuillMathField[${e || "r1"}]{${t}}`), o = o.replace(d, (e, t) => `\\embed{answerBlock}[${t}]`), o = o.replace(c, (e, t) => t), o = o.replace(l, ""), H(o);
164
- }, W = {
232
+ return r && (o = a(r[1], "\\\\").map((e) => e.trim()).join(e)), o = i(o, "\\placeholder", (e, t) => `\\MathQuillMathField[${e || "r1"}]{${t}}`), o = o.replace(d, (e, t) => `\\embed{answerBlock}[${t}]`), o = o.replace(c, (e, t) => t), o = o.replace(l, ""), o = q(o), H(o);
233
+ }, Y = {
165
234
  "\\frac": "\\frac{#?}{#?}",
166
235
  "/": "\\frac{#@}{#?}",
167
236
  "\\sqrt": "\\sqrt{#?}",
@@ -176,14 +245,14 @@ var e = "\\embed{newLine}[]", t = /^\\displaylines\{([\s\S]*)\}$/, n = "\\MathQu
176
245
  "(": "\\left(#?\\right)",
177
246
  "[": "\\left[#?\\right]",
178
247
  "|": "\\left|#?\\right|"
179
- }, G = {
248
+ }, X = {
180
249
  Left: "moveToPreviousChar",
181
250
  Right: "moveToNextChar",
182
251
  Backspace: "deleteBackward",
183
252
  Delete: "deleteForward",
184
253
  Up: "moveUp",
185
254
  Down: "moveDown"
186
- }, K = (e) => {
255
+ }, Z = (e) => {
187
256
  if (e) {
188
257
  if (e.latex && !e.command) return {
189
258
  type: "insert",
@@ -195,10 +264,10 @@ var e = "\\embed{newLine}[]", t = /^\\displaylines\{([\s\S]*)\}$/, n = "\\MathQu
195
264
  };
196
265
  if (e.command) return {
197
266
  type: "insert",
198
- value: (Array.isArray(e.command) ? e.command : [e.command]).map((e) => W[e] || F[e] || e).join("")
267
+ value: (Array.isArray(e.command) ? e.command : [e.command]).map((e) => Y[e] || F[e] || e).join("")
199
268
  };
200
269
  if (e.keystroke) {
201
- let t = G[e.keystroke];
270
+ let t = X[e.keystroke];
202
271
  return t ? {
203
272
  type: "command",
204
273
  value: t
@@ -206,4 +275,4 @@ var e = "\\embed{newLine}[]", t = /^\\displaylines\{([\s\S]*)\}$/, n = "\\MathQu
206
275
  }
207
276
  }
208
277
  };
209
- export { W as COMMANDS_WITH_ARGS, G as KEYSTROKES, F as MATHQUILL_ALIASES, e as NEWLINE_EMBED, f as answerBlockLatex, k as fieldIds, j as fieldTemplate, A as fieldValues, M as fillFields, U as fromMathLive, K as keyToAction, N as toAccessibleLatex, H as toCanonicalCommands, P as toMathLive, E as toNativeCommands, T as withVisibleEmptySlots };
278
+ export { Y as COMMANDS_WITH_ARGS, X as KEYSTROKES, F as MATHQUILL_ALIASES, e as NEWLINE_EMBED, f as answerBlockLatex, q as braceArguments, k as fieldIds, j as fieldTemplate, A as fieldValues, J as fromMathLive, M as inlineFields, Z as keyToAction, N as toAccessibleLatex, H as toCanonicalCommands, P as toMathLive, E as toNativeCommands, T as withVisibleEmptySlots };
@@ -8,6 +8,8 @@ export declare const KEYPAD_CARET_CLASS = "pie-caret-for-keypad";
8
8
  * it reads differently from a focused field.
9
9
  */
10
10
  export declare const KEYPAD_CARET_CSS: string;
11
+ /** The focused element, looking through shadow roots. */
12
+ export declare const deepActiveElement: () => Element | null;
11
13
  /**
12
14
  * MathLive hides its caret when a field blurs, though it keeps the insertion point. While a keypad
13
15
  * button has focus, keep the caret showing there. Focus going anywhere else removes it.
@@ -28,4 +28,4 @@ function o(e) {
28
28
  e.removeEventListener("blur", l), o();
29
29
  };
30
30
  }
31
- export { e as KEYPAD_ATTRIBUTE, t as KEYPAD_CARET_CLASS, n as KEYPAD_CARET_CSS, o as keepCaretWhileKeypadFocused };
31
+ export { e as KEYPAD_ATTRIBUTE, t as KEYPAD_CARET_CLASS, n as KEYPAD_CARET_CSS, a as deepActiveElement, o as keepCaretWhileKeypadFocused };
@@ -79,7 +79,13 @@ export default class Static extends React.Component {
79
79
  renderMarkup(): void;
80
80
  /** MathQuill's `innerFields` equivalent, keyed by prompt id. */
81
81
  values(): {};
82
- /** The whole field's latex, in stored form. MathQuill's `mathField.latex()`. */
82
+ /**
83
+ * The whole expression with the current answers in place, as MathQuill's
84
+ * `mathField.latex()` returned it: an answer block serialises as its answer
85
+ * alone, with no `\MathQuillMathField` wrapper. math-inline stores this as
86
+ * `completeAnswer` and scores it in Advanced Multi, so a wrapper left in would
87
+ * make every answer wrong.
88
+ */
83
89
  latex(): string;
84
90
  /** Put the selection inside prompt `id`, unless it is there already, so edits land in it. */
85
91
  selectPrompt(id: any): void;
package/dist/mf/static.js CHANGED
@@ -2,7 +2,7 @@ import { applyShadowStyles as e, createMathField as t, latexToMarkup as n, latex
2
2
  import { fixLongDivisionWhenReady as o, trackLongDivision as s } from "../longdiv.js";
3
3
  import { keepCaretWhileKeypadFocused as c } from "./inactive-caret.js";
4
4
  import { describeMathField as l, labelMathField as u } from "./field-label.js";
5
- import { KEYSTROKES as d, fieldIds as f, fieldTemplate as p, fieldValues as m, fillFields as h, fromMathLive as g, keyToAction as _, toMathLive as v } from "../latex-bridge.js";
5
+ import { KEYSTROKES as d, fieldIds as f, fieldTemplate as p, fieldValues as m, fromMathLive as h, inlineFields as g, keyToAction as _, toMathLive as v } from "../latex-bridge.js";
6
6
  import { parallelStyles as y, placeholderStyles as b, rootIndexStyles as x } from "./common-styles.js";
7
7
  import S from "react";
8
8
  import C from "prop-types";
@@ -64,7 +64,7 @@ var D = T("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
64
64
  let e = m(this.props.latex);
65
65
  Object.keys(e).forEach((t) => {
66
66
  let n = e[t];
67
- n !== this.promptValues[t] && n !== g(this.mathField.getPromptValue(t)) && (this.mathField.setPromptValue(t, v(n), {}), this.promptValues[t] = n);
67
+ n !== this.promptValues[t] && n !== h(this.mathField.getPromptValue(t)) && (this.mathField.setPromptValue(t, v(n), {}), this.promptValues[t] = n);
68
68
  });
69
69
  }
70
70
  componentWillUnmount() {
@@ -112,11 +112,11 @@ var D = T("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
112
112
  if (!this.mathField) return {};
113
113
  let e = {};
114
114
  return (this.mathField.getPrompts() || []).forEach((t) => {
115
- e[t] = g(this.mathField.getPromptValue(t));
115
+ e[t] = h(this.mathField.getPromptValue(t));
116
116
  }), e;
117
117
  }
118
118
  latex() {
119
- return this.mathField ? h(this.props.latex, this.values()) : "";
119
+ return this.mathField ? g(this.props.latex, this.values()) : "";
120
120
  }
121
121
  selectPrompt(e) {
122
122
  this.promptContaining(this.mathField.position) !== e && this.focusPrompt(e);
@@ -146,7 +146,7 @@ var D = T("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
146
146
  this.mathField && (this.mathField.focus(), this.selectPrompt(e));
147
147
  },
148
148
  el: () => this.mathField,
149
- latex: (t) => this.mathField ? t === void 0 ? g(this.mathField.getPromptValue(e)) : (this.mathField.setPromptValue(e, v(t), {}), t) : ""
149
+ latex: (t) => this.mathField ? t === void 0 ? h(this.mathField.getPromptValue(e)) : (this.mathField.setPromptValue(e, v(t), {}), t) : ""
150
150
  };
151
151
  }
152
152
  get innerFields() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-lib/math-input-mathlive",
3
- "version": "2.0.1-next.20261110124518",
3
+ "version": "2.0.1-next.20261112105900",
4
4
  "description": "MathLive-based math input (drop-in alternative to @pie-lib/math-input)",
5
5
  "license": "MIT",
6
6
  "repository": {