Skip to content

Implement math formula insertion tool using KaTeX#3

Draft
Copilot wants to merge 2 commits into
masterfrom
copilot/add-square-root-functionality
Draft

Implement math formula insertion tool using KaTeX#3
Copilot wants to merge 2 commits into
masterfrom
copilot/add-square-root-functionality

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 3, 2026

The math tool button (la-square-root-alt) was present in the toolbar but disabled with no implementation. This wires it up end-to-end in both entrypoints.

Changes

  • KaTeX CDN — Added katex.min.css and katex.min.js (v0.16.21) to both src/index.html and root index.html
  • Enabled button — Removed disabled class from the mathsTool label in both entrypoints
  • Modal dialog — New Bootstrap modal with LaTeX input, live preview via katex.render(), Insert/Cancel actions, and Enter key support in both entrypoints
  • Tool wiring (jquery.whiteboard.js and root inline script):
    • Registered mathsTool as tool 4 in the radio change handler
    • case 4 in addElement() captures click coordinates and opens the modal
    • On insert: renders LaTeX into a <span>, wraps it in a draggable .wbjs-el container, positions it at click location
    • Follows existing patterns: auto-switches back to pointer, drag handle, element ID tracking
  • Modal cancel handling — Resets active tool back to mouse pointer when the math modal is dismissed, preventing users from getting stuck in math mode

Usage

Select √ tool → click canvas → type LaTeX (e.g. \frac{-b \pm \sqrt{b^2-4ac}}{2a}) → see live preview → Insert.

- Enable the mathsTool button (remove disabled class)
- Add KaTeX CSS/JS via CDN for LaTeX formula rendering
- Add modal dialog with live preview for entering LaTeX formulas
- Add tool=4 handler for mathsTool in tool switch
- Add case 4 in addElement() to show modal on canvas click
- Insert rendered formula as draggable element on the whiteboard
- Support Enter key to insert and cancel to reset state
- Update welcome modal text to mention math formulas

Agent-Logs-Url: https://github.com/ionutfinta/WhiteboardJS/sessions/eaf4b9ab-bbf0-4902-93a5-f99bada3a018

Co-authored-by: ionutfinta <56076718+ionutfinta@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements end-to-end math formula insertion on the whiteboard using KaTeX, wiring up the previously disabled √ toolbar tool.

Changes:

  • Adds KaTeX CSS/JS via CDN and introduces a new “Insert Math Formula” Bootstrap modal with live preview.
  • Enables the mathsTool button in the toolbar and wires it to tool ID 4.
  • Implements formula insertion in jquery.whiteboard.js, placing rendered KaTeX output at the click/tap location inside a draggable whiteboard element.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/index.html Enables the math tool button, adds KaTeX CDN assets, and adds the math insertion modal UI.
src/jquery.whiteboard.js Registers/selects the math tool and implements modal open, live preview, and insertion of a draggable rendered formula element.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/index.html
Comment on lines +48 to 50
<label class="btn btn-dark">
<input type="radio" name="toolRadio" id="mathsTool" autocomplete="off"><i class="las la-square-root-alt"></i>
</label>
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repo has two entrypoints: src/index.html (served by src/app.js) and the root index.html (used for the GitHub Pages demo). This PR enables mathsTool only in src/index.html; the root index.html still has the maths button disabled and doesn’t load KaTeX, so the hosted demo won’t show this feature. Consider updating the root index.html as well (or consolidating to avoid duplicated UI).

Copilot uses AI. Check for mistakes.
Comment thread src/jquery.whiteboard.js

//-- Math formula modal: reset tool on cancel
$("#mathModal").on("hidden.bs.modal", function() {
mathInsertCoords = null;
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says “reset tool on cancel”, but the handler only clears mathInsertCoords and leaves the current tool as mathsTool. Either update the comment to match the intended behavior, or actually reset the active tool (e.g., trigger #mouseTool on modal close / cancel) so users don’t get stuck repeatedly reopening the modal on the next click.

Suggested change
mathInsertCoords = null;
mathInsertCoords = null;
if (tool === 4) {
$("#mouseTool").trigger("click");
}

Copilot uses AI. Check for mistakes.
@ionutfinta
Copy link
Copy Markdown
Owner

@copilot apply changes based on the comments in this thread

…t tool on modal cancel

- Enable mathsTool button in root index.html (GitHub Pages demo)
- Add KaTeX CSS/JS CDN links to root index.html
- Add math formula modal to root index.html
- Add math tool handling (tool=4, modal, insert, preview) to inline script
- Fix modal dismiss handler to reset tool to mouse pointer on cancel

Agent-Logs-Url: https://github.com/ionutfinta/WhiteboardJS/sessions/f5f2b29a-17a2-43b7-b943-d51c160a3008

Co-authored-by: ionutfinta <56076718+ionutfinta@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 3, 2026

@copilot apply changes based on the comments in this thread

Applied both review comments in commit 26ad4b4:

  1. Root index.html updated — enabled the mathsTool button, added KaTeX CSS/JS CDN, added the math formula modal, and added math tool handling (tool selection, addElement case 4, live preview, insert, Enter key, cancel reset) to the inline script.

  2. Tool reset on modal cancel — the hidden.bs.modal handler now switches back to the mouse tool when tool === 4, preventing users from getting stuck reopening the modal on every click.

Copilot AI requested a review from ionutfinta April 3, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants