Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mathjax-toolbar-labels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@prairielearn/sketchresponse": minor
---

Render toolbar labels with a host-provided MathJax instance, including dropdown and responsive overflow labels.
5 changes: 5 additions & 0 deletions sketch_tool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions sketch_tool/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
height: 2rem;
}
</style>
<script>
window.MathJax = {
tex: {
inlineMath: [
['$', '$'],
['\\(', '\\)'],
],
},
};
</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@4.1.3/tex-svg.js"></script>
<script src="sketchresponse.min.js"></script>
</head>
<body class="bg-body-tertiary">
Expand Down
61 changes: 53 additions & 8 deletions sketch_tool/lib/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -139,6 +171,7 @@ export default class Toolbar {
}
this.items.push(item);
this.render();
this.queueLabelTypesetting();
}

openDropdown(id) {
Expand Down Expand Up @@ -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',
{
Expand Down
14 changes: 9 additions & 5 deletions sketch_tool/pl-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\\)',
},
],
};

Expand Down
Loading