Skip to content
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

Fix selection over on MathJax interaction #15

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/examples/rich_text_table/tasks.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"data": {
"text": "[[\"Asking question?<a href=\\\"javascript:alert('pwned')\\\">click</a>\", \"Answer! Look at some LaTex\\n\\n\\\\(\\\\dfrac{1}{2k - 6} = \\\\dfrac{1}{3}\\\\) and \\\\(\\\\dfrac{1.5}{2k - 6} = \\\\dfrac{1}{3}\\\\)\\n\\n\"], [\"Asking more question in LaTex?\\n\\n\\\\[\\n\\\\begin{array}{r}\\n 3.050 \\\\\\\\\\n- 0.338 \\\\\\\\\\n\\\\hline\\n\\\\end{array}\\n\\\\]\\n\\n\", \"More LaTex! $4 to $5. \\n\\n\\\\(\\\\dfrac{3}{2k - 6} = \\\\dfrac{1}{3}\\\\)\\n\\n\"], [\"Following is not Math\", \"$4 to $5\"]]"
"text": "[[\"Asking question?<a href=\\\"javascript:alert('pwned')\\\">click</a>\", \"Answer! Look at some LaTex\\n\\n\\\\(\\\\dfrac{1}{2k - 6} = \\\\dfrac{1}{3}\\\\) and \\\\(\\\\dfrac{1.5}{2k - 6} = \\\\dfrac{1}{3}\\\\)\\n\\n\"], [\"Asking more question in LaTex?\\n\\n\\\\[\\n\\\\begin{array}{r}\\n 3.050 \\\\\\\\\\n- 0.338 \\\\\\\\\\n\\\\hline\\n\\\\end{array}\\n\\\\]\\n\\n\", \"More LaTex! $4 to $5. \\n\\n\\\\(\\\\dfrac{3}{2k - 6} = \\\\dfrac{1}{3}\\\\)\\n\\n\"], [\"Following is not Math\", \"$4 to $5\"], [\"\\\\(\\\\dfrac{3}{2k - 6} = \\\\dfrac{1}{3}\\\\)\", \"\\\\(\\\\dfrac{3}{2k - 6} = \\\\dfrac{1}{3}\\\\)\"]]"
},
"predictions": [
{
Expand Down
12 changes: 11 additions & 1 deletion src/tags/object/RichText/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const NO_MATHJAX_CLASS = 'tex2jax_ignore';
// Extract math from conversation, alternate between math and non-math
// Khanmigo uses "\(.*?\)" and "\[.*?\]" as the marker for math
// For example, "What is \(2 + 2\)?" will split into ["What is ", "2 + 2", "?"]
//
// Note that even if this only contains MathJax, this will be wrapped in two
// empty strings, which is what we want.
// e.g. "\\(2 + 2\\)" will split into ["", "2 + 2", ""]
const parseConvoWithMath = (str) => {
// About the capture group: a cool behaviour of str.split is that if there's
// capturing group, the group is captured into the group, which is perfect
Expand Down Expand Up @@ -79,7 +83,13 @@ const renderTableValue = (val) => {
convoAndMathList.map((convo, i) => {
if (i % 2 === 0) {
// Non math
return <span key={`eq=${i}`} className={NO_MATHJAX_CLASS}>{convo}</span>;
if (!convo) {
// Need to present space here, so if selection happens we can
// still observe it. We tested that en quad space gives the most
// obvious selection.
return <span key={`eq-${i}`} className={NO_MATHJAX_CLASS}>&#x3000;</span>;
}
return <span key={`eq-${i}`} className={NO_MATHJAX_CLASS}>{convo}</span>;
} else {
// So for Math, we need to create a span as we want 2 piece of dom:
// 1. The hidden raw MathJax expression, to allow slot Label to work
Expand Down
14 changes: 12 additions & 2 deletions src/utils/selection-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ const trimSelectionLeft = (selection) => {
selection.removeAllRanges();
selection.collapse(resultRange.startContainer, resultRange.startOffset);
let currentRange = selection.getRangeAt(0);
let lastContainer = currentRange.startContainer;

do {
selection.collapse(currentRange.endContainer, currentRange.endOffset);
selection.modify('extend', 'forward', 'character');
currentRange = selection.getRangeAt(0);
} while (!isTextNode(currentRange.startContainer) || isSpace(currentRange.startContainer.textContent[currentRange.startOffset]));
lastContainer = currentRange.startContainer;
} while (
currentRange.startContainer !== lastContainer &&
(!isTextNode(currentRange.startContainer) || isSpace(currentRange.startContainer.textContent[currentRange.startOffset]))
);
Comment on lines +47 to +49

Choose a reason for hiding this comment

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

Could use a brief comment.

resultRange.setStart(currentRange.startContainer, currentRange.startOffset);
selection.removeAllRanges();
selection.addRange(resultRange);
Expand All @@ -51,12 +56,17 @@ const trimSelectionRight = (selection) => {
selection.removeAllRanges();
selection.collapse(resultRange.endContainer, resultRange.endOffset);
let currentRange = selection.getRangeAt(0);
let lastContainer = currentRange.startContainer;

do {
selection.collapse(currentRange.startContainer, currentRange.startOffset);
selection.modify('extend', 'backward', 'character');
currentRange = selection.getRangeAt(0);
} while (!isTextNode(currentRange.startContainer) || isSpace(currentRange.startContainer.textContent[currentRange.startOffset]));
lastContainer = currentRange.startContainer;
} while (
currentRange.startContainer !== lastContainer &&
(!isTextNode(currentRange.startContainer) || isSpace(currentRange.startContainer.textContent[currentRange.startOffset]))
);
resultRange.setEnd(currentRange.endContainer, currentRange.endOffset);
selection.removeAllRanges();
selection.addRange(resultRange);
Expand Down
Loading