使用 MathJax 的数学方程式的所见即所得编辑器
WYSIWYG Editor for Mathematical Equations using MathJax
我正在尝试构建 WYSIWYG 编辑器以使用 MathJax 编写数学公式。我正在尝试 select 文本区域中的随机内容并将其粘贴到 div 中以将其转换为数学方程式。
<!DOCTYPE html>
<html>
<head>
<title>Wrap Selected Content with Dummy Editor</title>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>;
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: { inlineMath: [ ['$','$'], ['\(','\)'] ] } });
MathJax.Hub.Config({tex2jax: { displayMath: [ ['$$','$$'], ['\(','\)'] ] } });
</script>
</head>
<body>
<!--Select some random text from this textarea and click button -->
<textarea id="wrapSelectedContent"></textarea>
<!-- Content should be load here using JavaScript and Convert it into Mathematical Function -->
<div id="pasteSelectedContent" style="width:300px; height:300px; border:2px solid #000;"></div>
<!-- Static Content displayed Successfully -->
<p>$$\sum(a+b)(c+d)$$</p>
<button onclick="wrapContent();">Put Summation Sign</button>
<script type="text/javascript">
function wrapContent(){
var selectedContent = document.getElementById("wrapSelectedContent");
var pasteselectedContent = document.getElementById("pasteSelectedContent");
var textlength = selectedContent.textLength;
var start = selectedContent.selectionStart;
var end = selectedContent.selectionEnd;
selectedContent.focus();
selectedContent.setSelectionRange(start,end);
var selectedContentValue = selectedContent.value.substring(start, end);
var replace = "$$\sum" + selectedContentValue + "$$";
pasteselectedContent.textContent = selectedContent.value.substring(0,start) + " " + replace + " " + selectedContent.value.substring(end,textlength);
console.log("Start Index : " + start);
console.log("End Index : " + end);
console.log("Text Content Length : " + textlength);
console.log(selectedContentValue);
}
</script>
</body>
</html>
那么我如何使用数学方程式转换 div#pasteSelectedContent 中显示的文本
请帮助我构建这个 WYSIWYG 编辑器
问题已解决
只需插入
MathJax.Hub.Queue(["Typeset", MathJax.Hub, "pasteselectedContent"]);
第 32 行之后
pasteselectedContent.textContent = selectedContent.value.substring(0,start) + " " + replace + " " + selectedContent.value.substring(end,textlength);
我正在尝试构建 WYSIWYG 编辑器以使用 MathJax 编写数学公式。我正在尝试 select 文本区域中的随机内容并将其粘贴到 div 中以将其转换为数学方程式。
<!DOCTYPE html>
<html>
<head>
<title>Wrap Selected Content with Dummy Editor</title>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>;
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: { inlineMath: [ ['$','$'], ['\(','\)'] ] } });
MathJax.Hub.Config({tex2jax: { displayMath: [ ['$$','$$'], ['\(','\)'] ] } });
</script>
</head>
<body>
<!--Select some random text from this textarea and click button -->
<textarea id="wrapSelectedContent"></textarea>
<!-- Content should be load here using JavaScript and Convert it into Mathematical Function -->
<div id="pasteSelectedContent" style="width:300px; height:300px; border:2px solid #000;"></div>
<!-- Static Content displayed Successfully -->
<p>$$\sum(a+b)(c+d)$$</p>
<button onclick="wrapContent();">Put Summation Sign</button>
<script type="text/javascript">
function wrapContent(){
var selectedContent = document.getElementById("wrapSelectedContent");
var pasteselectedContent = document.getElementById("pasteSelectedContent");
var textlength = selectedContent.textLength;
var start = selectedContent.selectionStart;
var end = selectedContent.selectionEnd;
selectedContent.focus();
selectedContent.setSelectionRange(start,end);
var selectedContentValue = selectedContent.value.substring(start, end);
var replace = "$$\sum" + selectedContentValue + "$$";
pasteselectedContent.textContent = selectedContent.value.substring(0,start) + " " + replace + " " + selectedContent.value.substring(end,textlength);
console.log("Start Index : " + start);
console.log("End Index : " + end);
console.log("Text Content Length : " + textlength);
console.log(selectedContentValue);
}
</script>
</body>
</html>
那么我如何使用数学方程式转换 div#pasteSelectedContent 中显示的文本 请帮助我构建这个 WYSIWYG 编辑器
问题已解决
只需插入
MathJax.Hub.Queue(["Typeset", MathJax.Hub, "pasteselectedContent"]);
第 32 行之后
pasteselectedContent.textContent = selectedContent.value.substring(0,start) + " " + replace + " " + selectedContent.value.substring(end,textlength);