diff --git a/.changeset/mathjax-toolbar-labels.md b/.changeset/mathjax-toolbar-labels.md new file mode 100644 index 0000000..42f55d1 --- /dev/null +++ b/.changeset/mathjax-toolbar-labels.md @@ -0,0 +1,5 @@ +--- +"@prairielearn/sketchresponse": minor +--- + +Render toolbar labels with a host-provided MathJax instance, including dropdown and responsive overflow labels. diff --git a/sketch_tool/README.md b/sketch_tool/README.md index 530cc2b..c84e6bc 100644 --- a/sketch_tool/README.md +++ b/sketch_tool/README.md @@ -47,6 +47,11 @@ You'll need to have [Node.js](http://nodejs.org) and npm (which is now packaged - _coordinates:_ cartesian or polar. - _plugins:_ modules that extend drawing functionality either automatically (for example axes and image) or are placed in the top toolbar (freeform, horizontal and vertical lines) and let the user draw the corresponding shape on the canvas. + Toolbar labels are typeset with a host-provided `window.MathJax` when it exposes + `typesetPromise()`. Labels use the host's configured delimiters and remain plain text when + MathJax is unavailable. For example, a JavaScript label containing inline math can be written as + `label: 'Cubic branch for \\(x < 1\\)'`. + You can also load these additional configurations which show various features of the tool: - allPluginsLatex - initialState diff --git a/sketch_tool/index.html b/sketch_tool/index.html index 2c399a1..b35d5ef 100644 --- a/sketch_tool/index.html +++ b/sketch_tool/index.html @@ -19,6 +19,17 @@ height: 2rem; } + + diff --git a/sketch_tool/lib/toolbar.js b/sketch_tool/lib/toolbar.js index 7f8c530..746ce75 100644 --- a/sketch_tool/lib/toolbar.js +++ b/sketch_tool/lib/toolbar.js @@ -16,10 +16,20 @@ function renderIcon(id, src, alt) { }); } +// Labels are immutable after registration, so MathJax can own their contents. +function renderLabelText(id, text) { + return z('span.mathjax_process', { + id, + onmount: (element) => { + element.textContent = String(text); + }, + }); +} + function renderLabel(id, text, hasDropdown) { return z( 'div.label', - z('span', { id }, text), + renderLabelText(id, text), z.if( hasDropdown, z('span', { 'aria-label': 'Open dropdown menu' }, ' \u25be'), @@ -38,6 +48,7 @@ export default class Toolbar { this.focusedItemID = null; this.openDropdownID = null; // TODO: better name this.overflowOpen = false; + this.labelTypesetFrame = null; if (!params.readonly) { let resizeFrame; @@ -74,6 +85,27 @@ export default class Toolbar { app.__messageBus.on('closeDropdown', this.closeDropdown.bind(this)); } + queueLabelTypesetting() { + cancelAnimationFrame(this.labelTypesetFrame); + // The toolbar and its responsive menu render in consecutive frames. + this.labelTypesetFrame = requestAnimationFrame(() => { + this.updateOverflow(); + this.labelTypesetFrame = requestAnimationFrame(async () => { + this.labelTypesetFrame = null; + if (typeof window.MathJax?.typesetPromise !== 'function') return; + try { + await window.MathJax.typesetPromise([this.el]); + this.updateOverflow(); + } catch (error) { + console.error( + 'Unable to typeset SketchResponse toolbar labels with MathJax.', + error, + ); + } + }); + }); + } + // Called when the toolbar plugin itself is activated activate() { this.isActive = true; @@ -139,6 +171,7 @@ export default class Toolbar { } this.items.push(item); this.render(); + this.queueLabelTypesetting(); } openDropdown(id) { @@ -242,36 +275,48 @@ export default class Toolbar { elements.forEach((el, index) => { el.hidden = !visible.has(items[index].id); }); - const overflowItems = items.filter((item) => !visible.has(item.id)); - this.renderOverflowMenu(overflowItems); + // Keep every overflow copy mounted so it only needs to be typeset once. + this.renderOverflowMenu(items, visible); if (fits) this.setOverflowOpen(false); } - renderOverflowMenu(items) { + renderOverflowMenu(items, visible) { const menu = this.el.querySelector('.si-overflow-menu'); z.render( menu, z.each(items, (item) => { + const hidden = visible.has(item.id); if (item.type === 'splitbutton') { // Flatten tool groups into sections rather than nesting dropdowns. return z( 'div', - { role: 'group', 'aria-label': item.label }, - z('div.si-overflow-heading', item.label), + { + role: 'group', + 'aria-label': item.label, + ...(hidden ? { hidden: true } : {}), + }, + z( + 'div.si-overflow-heading', + renderLabelText( + `${this.id}-overflow-${item.id}-heading`, + item.label, + ), + ), z.each(item.items, (child) => this.renderDropdownButton(child, item.id, true), ), ); } - return this.renderDropdownButton(item, null, true); + return this.renderDropdownButton(item, null, true, hidden); }), ); } - renderDropdownButton(item, groupId, overflow = false) { + renderDropdownButton(item, groupId, overflow = false, hidden = false) { const id = `${this.id}-${overflow ? 'overflow' : groupId}-${item.id}`; return z( 'div.si-dropdown-item', + hidden ? { hidden: true } : {}, z( 'button.si-dropdown-button', { diff --git a/sketch_tool/pl-demo.js b/sketch_tool/pl-demo.js index 7163639..e7f8303 100644 --- a/sketch_tool/pl-demo.js +++ b/sketch_tool/pl-demo.js @@ -6,18 +6,22 @@ const config = { enforceBounds: true, plugins: [ { name: 'axes', id: 'axes' }, - { name: 'point', id: 'p' }, + { name: 'point', id: 'p', label: 'Point \\((x, y)\\)' }, { name: 'line-segment', id: 'line' }, { name: 'group', id: 'curves', - label: 'Curves', + label: 'Curves \\(f(x)\\)', plugins: [ - { name: 'polyline', id: 'poly' }, - { name: 'spline', id: 'spline' }, + { name: 'polyline', id: 'poly', label: 'Polyline \\(C_1\\)' }, + { name: 'spline', id: 'spline', label: 'Spline \\(C_2\\)' }, ], }, - { name: 'freeform', id: 'free' }, + { + name: 'freeform', + id: 'free', + label: 'Cubic branch for \\(x < 1\\)', + }, ], };