@pie-lib/math-input-mathlive 2.0.4-next.20261203201341 → 2.0.4-next.20261204091207
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/mf/static.js +27 -26
- package/dist/mf/vertical-move.d.ts +63 -0
- package/dist/mf/vertical-move.js +58 -0
- package/package.json +2 -2
package/dist/mf/static.js
CHANGED
|
@@ -5,16 +5,17 @@ import r from "debug";
|
|
|
5
5
|
import { applyShadowStyles as i, createMathField as a, latexToMarkup as o, latexToMathMl as s, loadMathLive as c, trackPlaceholderCaret as l } from "../mathlive-instance.js";
|
|
6
6
|
import { fixLongDivisionWhenReady as u, trackLongDivision as d } from "../longdiv.js";
|
|
7
7
|
import { keepCaretWhileKeypadFocused as f } from "./inactive-caret.js";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
8
|
+
import { trackVerticalMoves as p } from "./vertical-move.js";
|
|
9
|
+
import { describeMathField as m, labelMathField as h } from "./field-label.js";
|
|
10
|
+
import { KEYSTROKES as g, fieldIds as _, fieldTemplate as v, fieldValues as y, fromMathLive as b, inlineFields as x, keyToAction as S, toMathLive as C } from "../latex-bridge.js";
|
|
11
|
+
import { parallelStyles as w, placeholderStyles as T, rootIndexStyles as E } from "./common-styles.js";
|
|
12
|
+
import { jsx as D } from "react/jsx-runtime";
|
|
12
13
|
// src/mf/static.tsx
|
|
13
|
-
var
|
|
14
|
+
var O = r("pie-lib:math-input-mathlive:static"), k = "position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0", A = n("span")({
|
|
14
15
|
display: "inline-block",
|
|
15
|
-
...w,
|
|
16
16
|
...T,
|
|
17
|
-
...
|
|
17
|
+
...E,
|
|
18
|
+
...w,
|
|
18
19
|
"& math-field": {
|
|
19
20
|
display: "inline-block",
|
|
20
21
|
border: "none",
|
|
@@ -22,7 +23,7 @@ var D = r("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
|
|
|
22
23
|
},
|
|
23
24
|
"& math-field::part(virtual-keyboard-toggle)": { display: "none" },
|
|
24
25
|
"& math-field::part(menu-toggle)": { display: "none" }
|
|
25
|
-
}),
|
|
26
|
+
}), j = class extends e.Component {
|
|
26
27
|
static propTypes = {
|
|
27
28
|
latex: t.string.isRequired,
|
|
28
29
|
className: t.string,
|
|
@@ -36,7 +37,7 @@ var D = r("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
|
|
|
36
37
|
setInput: t.func
|
|
37
38
|
};
|
|
38
39
|
static defaultProps = { getFieldName: () => {} };
|
|
39
|
-
relabel = () =>
|
|
40
|
+
relabel = () => h(this.mathField, this.props.language);
|
|
40
41
|
constructor(t) {
|
|
41
42
|
super(t), this.holderRef = e.createRef(), this.state = { markup: "" }, this.promptValues = {};
|
|
42
43
|
}
|
|
@@ -50,10 +51,10 @@ var D = r("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
|
|
|
50
51
|
return;
|
|
51
52
|
}
|
|
52
53
|
let t = await c();
|
|
53
|
-
e === this.mountGeneration && t?.MathfieldElement && this.holderRef.current && !this.mathField && (this.mathField = a(t),
|
|
54
|
+
e === this.mountGeneration && t?.MathfieldElement && this.holderRef.current && !this.mathField && (this.mathField = a(t), h(this.mathField, this.props.language, this.relabel), m(this.mathField, this.props.ariaDescribedBy), this.mathField.readOnly = !0, this.mathField.mathVirtualKeyboardPolicy = "manual", this.mathField.mathModeSpace = "\\ ", this.mathField.smartSuperscript = !1, this.mathField.popoverPolicy = "off", this.mathField.value = C(this.props.latex), i(this.mathField), this.untrackCaret = l(this.mathField), this.untrackLongDivision = d(this.mathField), this.stopKeepingCaret = f(this.mathField), this.untrackVerticalMoves = p(this.mathField), this.mathField.addEventListener("input", this.onPromptInput), this.mathField.addEventListener("pointerdown", this.onPointerDown, !0), this.mathField.addEventListener("focusin", this.onPromptFocus), this.mathField.addEventListener("selection-change", this.onSelectionChange), this.mathField.addEventListener("keydown", this.onKeyDown), this.holderRef.current.replaceChildren(this.mathField), h(this.mathField, this.props.language, this.relabel), this.parkCaret(), this.createLiveRegion());
|
|
54
55
|
}
|
|
55
56
|
componentDidUpdate(e, t) {
|
|
56
|
-
!this.mathField && this.state.markup !== (t && t.markup) && u(this.holderRef.current), this.mathField && e.language !== this.props.language &&
|
|
57
|
+
!this.mathField && this.state.markup !== (t && t.markup) && u(this.holderRef.current), this.mathField && e.language !== this.props.language && h(this.mathField, this.props.language, this.relabel), this.mathField && e.ariaDescribedBy !== this.props.ariaDescribedBy && m(this.mathField, this.props.ariaDescribedBy), e.latex !== this.props.latex && (this.isInteractive() === this.interactive ? this.mathField ? v(this.props.latex) === v(e.latex) ? this.syncPrompts() : (this.mathField.value = C(this.props.latex), this.parkCaret()) : this.renderMarkup() : (this.unmountField(), this.setState({ markup: "" }), this.mount()));
|
|
57
58
|
}
|
|
58
59
|
parkCaret() {
|
|
59
60
|
if (!this.mathField || this.mathField.hasFocus?.()) return;
|
|
@@ -61,17 +62,17 @@ var D = r("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
|
|
|
61
62
|
t && (this.mathField.selection = { ranges: [[t[0], t[0]]] });
|
|
62
63
|
}
|
|
63
64
|
syncPrompts() {
|
|
64
|
-
let e =
|
|
65
|
+
let e = y(this.props.latex);
|
|
65
66
|
Object.keys(e).forEach((t) => {
|
|
66
67
|
let n = e[t];
|
|
67
|
-
n !== this.promptValues[t] && n !==
|
|
68
|
+
n !== this.promptValues[t] && n !== b(this.mathField.getPromptValue(t)) && (this.mathField.setPromptValue(t, C(n), {}), this.promptValues[t] = n);
|
|
68
69
|
});
|
|
69
70
|
}
|
|
70
71
|
componentWillUnmount() {
|
|
71
72
|
this.unmountField(), this.removeLiveRegion();
|
|
72
73
|
}
|
|
73
74
|
unmountField() {
|
|
74
|
-
this.mountGeneration = (this.mountGeneration || 0) + 1, this.untrackCaret &&= (this.untrackCaret(), void 0), this.untrackLongDivision &&= (this.untrackLongDivision(), void 0), this.stopKeepingCaret &&= (this.stopKeepingCaret(), void 0), this.mathField &&= (this.mathField.removeEventListener("input", this.onPromptInput), this.mathField.removeEventListener("pointerdown", this.onPointerDown, !0), this.mathField.removeEventListener("focusin", this.onPromptFocus), this.mathField.removeEventListener("selection-change", this.onSelectionChange), this.mathField.removeEventListener("keydown", this.onKeyDown), this.mathField.remove(), void 0), this.promptValues = {}, this.focusedPrompt = void 0;
|
|
75
|
+
this.mountGeneration = (this.mountGeneration || 0) + 1, this.untrackCaret &&= (this.untrackCaret(), void 0), this.untrackLongDivision &&= (this.untrackLongDivision(), void 0), this.stopKeepingCaret &&= (this.stopKeepingCaret(), void 0), this.untrackVerticalMoves &&= (this.untrackVerticalMoves(), void 0), this.mathField &&= (this.mathField.removeEventListener("input", this.onPromptInput), this.mathField.removeEventListener("pointerdown", this.onPointerDown, !0), this.mathField.removeEventListener("focusin", this.onPromptFocus), this.mathField.removeEventListener("selection-change", this.onSelectionChange), this.mathField.removeEventListener("keydown", this.onKeyDown), this.mathField.remove(), void 0), this.promptValues = {}, this.focusedPrompt = void 0;
|
|
75
76
|
}
|
|
76
77
|
createLiveRegion = () => {
|
|
77
78
|
typeof document > "u" || this.liveRegion || (this.liveRegion = document.createElement("div"), Object.assign(this.liveRegion.style, {
|
|
@@ -102,21 +103,21 @@ var D = r("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
|
|
|
102
103
|
};
|
|
103
104
|
isInteractive() {
|
|
104
105
|
let e = this.props.latex || "";
|
|
105
|
-
return
|
|
106
|
+
return _(e).length > 0 || e.indexOf("\\placeholder") !== -1;
|
|
106
107
|
}
|
|
107
108
|
renderMarkup() {
|
|
108
|
-
let e = o(this.props.latex), t = e && s(this.props.latex), n = t ? `<span aria-hidden="true">${e}</span><span style="${
|
|
109
|
+
let e = o(this.props.latex), t = e && s(this.props.latex), n = t ? `<span aria-hidden="true">${e}</span><span style="${k}">${t}</span>` : e;
|
|
109
110
|
n !== this.state.markup && this.setState({ markup: n });
|
|
110
111
|
}
|
|
111
112
|
values() {
|
|
112
113
|
if (!this.mathField) return {};
|
|
113
114
|
let e = {};
|
|
114
115
|
return (this.mathField.getPrompts() || []).forEach((t) => {
|
|
115
|
-
e[t] =
|
|
116
|
+
e[t] = b(this.mathField.getPromptValue(t));
|
|
116
117
|
}), e;
|
|
117
118
|
}
|
|
118
119
|
latex() {
|
|
119
|
-
return this.mathField ?
|
|
120
|
+
return this.mathField ? x(this.props.latex, this.values()) : "";
|
|
120
121
|
}
|
|
121
122
|
selectPrompt(e) {
|
|
122
123
|
this.promptContaining(this.mathField.position) !== e && this.focusPrompt(e);
|
|
@@ -129,14 +130,14 @@ var D = r("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
|
|
|
129
130
|
id: e,
|
|
130
131
|
write: (e) => t((t) => (Array.isArray(e) ? e : [e]).forEach((e) => t.insert(e, { focus: !0 }))),
|
|
131
132
|
cmd: (e) => t((t) => {
|
|
132
|
-
let n =
|
|
133
|
+
let n = S({ command: e });
|
|
133
134
|
n && t.insert(n.value, { focus: !0 });
|
|
134
135
|
}),
|
|
135
136
|
keystroke: (e) => t((t) => {
|
|
136
137
|
try {
|
|
137
|
-
t.executeCommand(
|
|
138
|
+
t.executeCommand(g[e] || e);
|
|
138
139
|
} catch (t) {
|
|
139
|
-
|
|
140
|
+
O("unknown keystroke/selector \"%s\": %s", e, t && t.message);
|
|
140
141
|
}
|
|
141
142
|
}),
|
|
142
143
|
clear: () => t((t) => {
|
|
@@ -146,7 +147,7 @@ var D = r("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
|
|
|
146
147
|
this.mathField && (this.mathField.focus(), this.selectPrompt(e));
|
|
147
148
|
},
|
|
148
149
|
el: () => this.mathField,
|
|
149
|
-
latex: (t) => this.mathField ? t === void 0 ?
|
|
150
|
+
latex: (t) => this.mathField ? t === void 0 ? b(this.mathField.getPromptValue(e)) : (this.mathField.setPromptValue(e, C(t), {}), t) : ""
|
|
150
151
|
};
|
|
151
152
|
}
|
|
152
153
|
get innerFields() {
|
|
@@ -186,7 +187,7 @@ var D = r("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
|
|
|
186
187
|
let i = this.promptHandle(r), a = n(i, this.innerFields) || r;
|
|
187
188
|
this.props.setInput && this.props.setInput(i), t(a, i);
|
|
188
189
|
} catch (e) {
|
|
189
|
-
|
|
190
|
+
O("error resolving focused prompt: %s", e.message);
|
|
190
191
|
}
|
|
191
192
|
}
|
|
192
193
|
caretOffset() {
|
|
@@ -214,12 +215,12 @@ var D = r("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
|
|
|
214
215
|
}
|
|
215
216
|
render() {
|
|
216
217
|
let { className: e, onFocus: t, onBlur: n } = this.props, { markup: r } = this.state;
|
|
217
|
-
return this.mathField || this.isInteractive() ? /* @__PURE__ */
|
|
218
|
+
return this.mathField || this.isInteractive() ? /* @__PURE__ */ D(A, {
|
|
218
219
|
className: e,
|
|
219
220
|
onFocus: t,
|
|
220
221
|
onBlur: n,
|
|
221
222
|
ref: this.holderRef
|
|
222
|
-
}) : /* @__PURE__ */
|
|
223
|
+
}) : /* @__PURE__ */ D(A, {
|
|
223
224
|
className: e,
|
|
224
225
|
onFocus: t,
|
|
225
226
|
onBlur: n,
|
|
@@ -228,4 +229,4 @@ var D = r("pie-lib:math-input-mathlive:static"), O = "position:absolute;width:1p
|
|
|
228
229
|
});
|
|
229
230
|
}
|
|
230
231
|
};
|
|
231
|
-
export {
|
|
232
|
+
export { j as default };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Up and Down inside an answer block.
|
|
3
|
+
*
|
|
4
|
+
* In a read-only field with answer blocks (MathLive prompts), MathLive moves the caret up or down
|
|
5
|
+
* only to a branch that holds a prompt: from one block to another one under an authored fraction
|
|
6
|
+
* bar. A fraction typed inside a block holds no prompt, so Up from its denominator does nothing
|
|
7
|
+
* and Down from its numerator leaves the field. An editable field moves between them, as
|
|
8
|
+
* MathQuill did in both.
|
|
9
|
+
*
|
|
10
|
+
* Where the move stays inside the caret's block, the field's prompts are locked while MathLive's
|
|
11
|
+
* own `moveUp` / `moveDown` runs. With no editable prompt left, MathLive moves as in an editable
|
|
12
|
+
* field: to the nearest atom across the bar, with its usual announcement. Every other Up and
|
|
13
|
+
* Down is left to MathLive, including its `move-out` event where there is nowhere to go.
|
|
14
|
+
*/
|
|
15
|
+
/** A caret move, as MathLive's `moveUp` and `moveDown` make it. */
|
|
16
|
+
export type VerticalDirection = 'up' | 'down';
|
|
17
|
+
/** What MathLive's `getElementInfo(offset)` tells about the atom ending at caret `offset`. */
|
|
18
|
+
export type AtomInfo = {
|
|
19
|
+
depth?: number;
|
|
20
|
+
latex?: string;
|
|
21
|
+
} | undefined;
|
|
22
|
+
/** The parts of a `<math-field>` this module uses. */
|
|
23
|
+
export interface VerticalMoveField {
|
|
24
|
+
readonly readOnly: boolean;
|
|
25
|
+
readonly mode?: string;
|
|
26
|
+
readonly selection: {
|
|
27
|
+
readonly ranges: readonly (readonly [number, number])[];
|
|
28
|
+
};
|
|
29
|
+
getElementInfo(offset: number): AtomInfo;
|
|
30
|
+
getPrompts(): string[];
|
|
31
|
+
getPromptRange(id: string): [number, number];
|
|
32
|
+
getPromptState(id: string): [string | undefined, boolean | undefined];
|
|
33
|
+
setPromptState(id: string, state: undefined, locked: boolean): void;
|
|
34
|
+
executeCommand(command: string): boolean;
|
|
35
|
+
addEventListener(type: 'keydown', listener: (event: KeyboardEvent) => void, capture: boolean): void;
|
|
36
|
+
removeEventListener(type: 'keydown', listener: (event: KeyboardEvent) => void, capture: boolean): void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether MathLive's vertical move from `position`, once the prompts are locked, stays inside the
|
|
40
|
+
* answer block spanning `promptRange` (as `getPromptRange` gives it).
|
|
41
|
+
*
|
|
42
|
+
* The tree is recovered from `getElementInfo`, as in `selection.ts`: each branch opens with an
|
|
43
|
+
* empty-latex atom, and the atom owning a branch takes the offset right after its branches.
|
|
44
|
+
* MathLive walks up from the caret to the first `above` branch, for Down, or `below` branch, for
|
|
45
|
+
* Up, or to the first matrix cell. The move stays inside the block when that walk stops inside it:
|
|
46
|
+
*
|
|
47
|
+
* - at a numerator, for Down, or a denominator, for Up;
|
|
48
|
+
* - at a matrix cell, where MathLive moves between rows.
|
|
49
|
+
*
|
|
50
|
+
* A body, such as a root's radicand, and a script, such as an exponent or the limits of `\sum`,
|
|
51
|
+
* are walked through, as MathLive does. Down from a root's index, or from an atom whose branches
|
|
52
|
+
* this cannot name, is left to MathLive.
|
|
53
|
+
*
|
|
54
|
+
* Pure, so it can be tested without a browser.
|
|
55
|
+
*/
|
|
56
|
+
export declare function movesWithinPrompt(position: number, direction: VerticalDirection, info: (offset: number) => AtomInfo, promptRange: [number, number]): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Let Up and Down move across a fraction (or between matrix rows) typed inside an answer block of
|
|
59
|
+
* a read-only `<math-field>`, as they do in an editable one. See the module comment.
|
|
60
|
+
*
|
|
61
|
+
* @returns A function that removes the listener.
|
|
62
|
+
*/
|
|
63
|
+
export declare function trackVerticalMoves(mathField: VerticalMoveField | null | undefined): () => void;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// src/mf/vertical-move.ts
|
|
2
|
+
var e = /^\\(?:[dtc]?frac|[dt]?binom)(?![a-zA-Z])/, t = /^\\begin\{/, n = /^\\sqrt(?![a-zA-Z])/, r = /^[\^_]/, i = /^\\[a-zA-Z]+\s*(?:\\(?:no|display)?limits\s*)?[\^_]/, a = (t, a, o) => e.test(t) ? o === 2 ? a === 0 ? "above" : "below" : void 0 : n.test(t) ? o > 2 ? void 0 : o === 2 && a === 0 ? "above" : "through" : r.test(t) || i.test(t) || o === 1 ? "through" : void 0;
|
|
3
|
+
function o(n, r, i, o) {
|
|
4
|
+
let [s, c] = o, l = /* @__PURE__ */ new Map(), u = (e) => (l.has(e) || l.set(e, i(e)), l.get(e)), d = (e) => u(e)?.depth ?? 0, f = (e) => u(e)?.latex ?? "", p = (e, t) => d(e) === t && f(e) === "";
|
|
5
|
+
if (n < s || n > c) return !1;
|
|
6
|
+
let m = n, h = d(m);
|
|
7
|
+
for (; h > 0;) {
|
|
8
|
+
let n = m;
|
|
9
|
+
for (; n > s && !p(n, h);) --n;
|
|
10
|
+
let i = m + 1;
|
|
11
|
+
for (; i <= c && d(i) >= h;) i += 1;
|
|
12
|
+
if (i > c || d(i) !== h - 1 || !p(n, h)) return !1;
|
|
13
|
+
let o = [];
|
|
14
|
+
for (let e = i - 1; e >= s && d(e) >= h; --e) p(e, h) && o.unshift(e);
|
|
15
|
+
let l = f(i);
|
|
16
|
+
if (t.test(l)) return !0;
|
|
17
|
+
let u = a(l, o.indexOf(n), o.length);
|
|
18
|
+
if (!u) return !1;
|
|
19
|
+
if (u === (r === "down" ? "above" : "below")) return e.test(l);
|
|
20
|
+
m = i, --h;
|
|
21
|
+
}
|
|
22
|
+
return !1;
|
|
23
|
+
}
|
|
24
|
+
var s = (e, t) => (e.getPrompts() || []).find((n) => {
|
|
25
|
+
let r = e.getPromptRange(n);
|
|
26
|
+
return !!r && t >= r[0] && t <= r[1];
|
|
27
|
+
}), c = (e, t) => {
|
|
28
|
+
let n = e.getPrompts() || [], r = n.map((t) => !!e.getPromptState(t)[1]);
|
|
29
|
+
n.forEach((t) => {
|
|
30
|
+
e.setPromptState(t, void 0, !0);
|
|
31
|
+
});
|
|
32
|
+
try {
|
|
33
|
+
e.executeCommand(t);
|
|
34
|
+
} finally {
|
|
35
|
+
n.forEach((t, n) => {
|
|
36
|
+
e.setPromptState(t, void 0, r[n]);
|
|
37
|
+
}), e.executeCommand("scrollIntoView");
|
|
38
|
+
}
|
|
39
|
+
}, l = {
|
|
40
|
+
ArrowUp: "up",
|
|
41
|
+
ArrowDown: "down"
|
|
42
|
+
}, u = (e, t) => {
|
|
43
|
+
let n = e.selection?.ranges ?? [];
|
|
44
|
+
if (n.length !== 1) return;
|
|
45
|
+
let [r, i] = n[0];
|
|
46
|
+
return t === "up" ? Math.min(r, i) : Math.max(r, i);
|
|
47
|
+
};
|
|
48
|
+
function d(e) {
|
|
49
|
+
if (!e || typeof e.addEventListener != "function") return () => {};
|
|
50
|
+
let t = e, n = (e) => {
|
|
51
|
+
let n = l[e.key];
|
|
52
|
+
if (!n || e.defaultPrevented || e.isComposing || e.shiftKey || e.altKey || e.ctrlKey || e.metaKey || t.mode === "latex" || !t.readOnly) return;
|
|
53
|
+
let r = u(t, n), i = r === void 0 ? void 0 : s(t, r);
|
|
54
|
+
r !== void 0 && i && o(r, n, (e) => t.getElementInfo(e), t.getPromptRange(i)) && (e.preventDefault(), c(t, n === "up" ? "moveUp" : "moveDown"));
|
|
55
|
+
};
|
|
56
|
+
return t.addEventListener("keydown", n, !0), () => t.removeEventListener("keydown", n, !0);
|
|
57
|
+
}
|
|
58
|
+
export { o as movesWithinPrompt, d as trackVerticalMoves };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pie-lib/math-input-mathlive",
|
|
3
|
-
"version": "2.0.4-next.
|
|
3
|
+
"version": "2.0.4-next.20261204091207",
|
|
4
4
|
"description": "MathLive-based math input (drop-in alternative to @pie-lib/math-input)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"mathlive": "0.111.4",
|
|
18
18
|
"prop-types": "^15.7.2",
|
|
19
19
|
"@pie-element/shared-lodash": "0.1.7-next.20261202142832",
|
|
20
|
-
"@pie-lib/render-ui": "8.0.11-next.
|
|
20
|
+
"@pie-lib/render-ui": "8.0.11-next.20261204091207",
|
|
21
21
|
"@pie-element/shared-i18n": "0.1.6-next.20261202142832"
|
|
22
22
|
},
|
|
23
23
|
"type": "module",
|