-
Notifications
You must be signed in to change notification settings - Fork 0
Implement math formula insertion tool using KaTeX #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,11 +3,13 @@ | |||||||||||
| * - 1 textTool | ||||||||||||
| * - 2 rectTool | ||||||||||||
| * - 3 circleTool | ||||||||||||
| * - 4 mathsTool | ||||||||||||
| * ... | ||||||||||||
| */ | ||||||||||||
| var elID = tool = 0; | ||||||||||||
| var appSelector = "#app"; | ||||||||||||
| var contentSelector = appSelector + " .wbjs-content"; | ||||||||||||
| var mathInsertCoords = null; | ||||||||||||
|
|
||||||||||||
| // Helper: get page coordinates from mouse or touch event | ||||||||||||
| function getEventCoords(event) { | ||||||||||||
|
|
@@ -48,6 +50,10 @@ $(function(){ | |||||||||||
| tool = 3; | ||||||||||||
| $(contentSelector).css("cursor", "crosshair"); | ||||||||||||
| break; | ||||||||||||
| case "mathsTool": | ||||||||||||
| tool = 4; | ||||||||||||
| $(contentSelector).css("cursor", "crosshair"); | ||||||||||||
| break; | ||||||||||||
| //TODO: To be continued... | ||||||||||||
| } | ||||||||||||
| $(appSelector).css({"cursor": "text"}); | ||||||||||||
|
|
@@ -182,6 +188,12 @@ $(function(){ | |||||||||||
| newEl.resizable(); | ||||||||||||
| elID++; | ||||||||||||
| break; | ||||||||||||
| case 4: | ||||||||||||
| mathInsertCoords = { pageX: coords.pageX, pageY: coords.pageY, ctTop: ctTop, ctLeft: ctLeft }; | ||||||||||||
| $("#mathLatexInput").val(""); | ||||||||||||
| $("#mathPreview").html('<span class="text-muted">Preview will appear here</span>'); | ||||||||||||
| $("#mathModal").modal("show"); | ||||||||||||
| break; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
@@ -208,4 +220,69 @@ $(function(){ | |||||||||||
| } | ||||||||||||
| } | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| //-- Math formula modal: live preview | ||||||||||||
| $("#mathLatexInput").on("input", function() { | ||||||||||||
| var latex = $(this).val().trim(); | ||||||||||||
| if (latex) { | ||||||||||||
| try { | ||||||||||||
| katex.render(latex, document.getElementById("mathPreview"), { throwOnError: false, displayMode: true }); | ||||||||||||
| } catch(e) { | ||||||||||||
| $("#mathPreview").html('<span class="text-danger">Invalid formula</span>'); | ||||||||||||
| } | ||||||||||||
| } else { | ||||||||||||
| $("#mathPreview").html('<span class="text-muted">Preview will appear here</span>'); | ||||||||||||
| } | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| //-- Math formula modal: insert button | ||||||||||||
| $("#mathInsertBtn").on("click", function() { | ||||||||||||
| var latex = $("#mathLatexInput").val().trim(); | ||||||||||||
| if (!latex || !mathInsertCoords) return; | ||||||||||||
|
|
||||||||||||
| var rendered = $('<span></span>')[0]; | ||||||||||||
| try { | ||||||||||||
| katex.render(latex, rendered, { throwOnError: false, displayMode: true }); | ||||||||||||
| } catch(e) { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| var newEl = $(contentSelector).append( | ||||||||||||
| '<div class="wbjs-el" id="wbjs-el-'+elID+'">' + | ||||||||||||
| '<div class="btn btn-link position-absolute drg-btn"><i class="las la-arrows-alt"></i></div>' + | ||||||||||||
| '<div class="wbjs-math p-2"></div>' + | ||||||||||||
| '</div>' | ||||||||||||
| ).children(":last-child"); | ||||||||||||
|
|
||||||||||||
| newEl.find(".wbjs-math").append(rendered); | ||||||||||||
|
|
||||||||||||
| newEl.css({ | ||||||||||||
| "position": "absolute", | ||||||||||||
| "top": mathInsertCoords.ctTop + mathInsertCoords.pageY, | ||||||||||||
| "left": mathInsertCoords.ctLeft + mathInsertCoords.pageX | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| $("#mathModal").modal("hide"); | ||||||||||||
| $("#mouseTool").trigger("click"); | ||||||||||||
|
|
||||||||||||
| newEl.draggable({handle: "div.btn", scroll: false}); | ||||||||||||
| elID++; | ||||||||||||
| mathInsertCoords = null; | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| //-- Math formula modal: allow Enter key to insert | ||||||||||||
| $("#mathLatexInput").on("keydown", function(e) { | ||||||||||||
| if (e.key === "Enter") { | ||||||||||||
| e.preventDefault(); | ||||||||||||
| $("#mathInsertBtn").trigger("click"); | ||||||||||||
| } | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| //-- Math formula modal: reset tool on cancel | ||||||||||||
| $("#mathModal").on("hidden.bs.modal", function() { | ||||||||||||
| mathInsertCoords = null; | ||||||||||||
|
||||||||||||
| mathInsertCoords = null; | |
| mathInsertCoords = null; | |
| if (tool === 4) { | |
| $("#mouseTool").trigger("click"); | |
| } |
There was a problem hiding this comment.
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 bysrc/app.js) and the rootindex.html(used for the GitHub Pages demo). This PR enablesmathsToolonly insrc/index.html; the rootindex.htmlstill has the maths button disabled and doesn’t load KaTeX, so the hosted demo won’t show this feature. Consider updating the rootindex.htmlas well (or consolidating to avoid duplicated UI).