Skip to content

Commit

Permalink
Merge pull request #1 from droundy/math-tool
Browse files Browse the repository at this point in the history
Math tool
  • Loading branch information
dazsmith authored Oct 21, 2020
2 parents 8b8955e + 95966bb commit 3f3bebd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion client-data/board.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<link rel="alternate" hreflang="{{.}}" href="{{../boardUriComponent}}?lang={{.}}" />
{{/languages}}
<script src="../polyfill.min.js"></script>
<script type="text/javascript" id="MathJax-script" src="https://mj.dasmithmaths.com/mathjax/tex-svg-full.js"></script>
<script type="text/javascript" id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg-full.js"></script>
</head>

<body>
Expand Down
29 changes: 14 additions & 15 deletions client-data/tools/mathematics/mathematics.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
(function () { //Code isolation
var board = Tools.board;

var input = document.createElement("input");
var input = document.createElement("textarea");
input.id = "textToolInput";
input.type = "text";
input.setAttribute("autocomplete", "off");

var curText = {
Expand Down Expand Up @@ -106,15 +105,19 @@
active = true;
if (!input.parentNode) board.appendChild(input);
input.value = "";
var left = curText.x - document.documentElement.scrollLeft + 'px';
var clientW = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var x = curText.x * Tools.scale - document.documentElement.scrollLeft;
if (x + 250 > clientW) {
x = Math.max(60, clientW - 260)
if (x < 360) {
x = Math.max(60, clientW - 320);
} else {
x = 60;
}

input.style.opacity = '0.5';
input.style.left = x + 'px';
input.style.top = curText.y * Tools.scale - document.documentElement.scrollTop + 20 + 'px';
input.style.top = curText.y * Tools.scale - document.documentElement.scrollTop - 20 + 'px';
input.style.height = '150px';
input.style.width = '300px';
input.focus();
input.addEventListener("keyup", textChangeHandler);
input.addEventListener("blur", textChangeHandler);
Expand All @@ -138,18 +141,14 @@

function textChangeHandler(evt) {
input.value = removeDoubleQuotes(input.value); // remove all double quotes; they are unnecessary in (La)TeX and difficult to escape
if (evt.which === 13) { // enter
curText.y += 1.5 * curText.size;
stopEdit();
startEdit();
} else if (evt.which === 27) { // escape
if (evt.which === 27) { // escape
stopEdit();
}
if (performance.now() - curText.lastSending > 1000) {
if (curText.sentText !== input.value) {
//If the user clicked where there was no text, then create a new text field
if (curText.id === 0) {
curText.id = Tools.generateUID("m"); //"t" for text
curText.id = Tools.generateUID("m"); //"m" for math
Tools.drawAndSend({
'type': 'new',
'id': curText.id,
Expand All @@ -160,11 +159,11 @@
'y': curText.y
})
}
let mathematicsSVG = getSVGFromMathJax(input.value.slice(0, 280));
let mathematicsSVG = getSVGFromMathJax(input.value);
Tools.drawAndSend({
'type': "update",
'id': curText.id,
'txt': input.value.slice(0, 280),
'txt': input.value,
'mWidth': mathematicsSVG.getAttribute('width'),
'mHeight': mathematicsSVG.getAttribute('height'),
'mViewBox': mathematicsSVG.getAttribute('viewBox'),
Expand All @@ -184,7 +183,7 @@

function getSVGFromMathJax(rawTeX) {
let userColor = Tools.getColor();
let svgFromMathJax = MathJax.tex2svg("\\color{" + userColor + "}" + rawTeX, {display: true});
let svgFromMathJax = MathJax.tex2svg("\\color{" + userColor + "}\\begin{align}" + rawTeX + '\\end{align}', {display: true});
let svgOnly = svgFromMathJax.children[0];
// Split the viewBox into separate strings
var strArrViewBox = svgOnly.getAttribute("viewBox").split(" ");
Expand Down

0 comments on commit 3f3bebd

Please sign in to comment.