sample-dynamic-2.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Dynamic Preview of Textarea with MathJax Content</title>
  5. <!-- Copyright (c) 2012-2017 The MathJax Consortium -->
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  8. <meta name="viewport" content="width=device-width, initial-scale=1">
  9. <script type="text/x-mathjax-config">
  10. MathJax.Hub.Config({
  11. showProcessingMessages: false,
  12. tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] }
  13. });
  14. </script>
  15. <script type="text/javascript" src="../MathJax.js?config=TeX-MML-AM_HTMLorMML"></script>
  16. <script>
  17. var Preview = {
  18. delay: 150, // delay after keystroke before updating
  19. preview: null, // filled in by Init below
  20. buffer: null, // filled in by Init below
  21. timeout: null, // store setTimout id
  22. mjRunning: false, // true when MathJax is processing
  23. mjPending: false, // true when a typeset has been queued
  24. oldText: null, // used to check if an update is needed
  25. //
  26. // Get the preview and buffer DIV's
  27. //
  28. Init: function () {
  29. this.preview = document.getElementById("MathPreview");
  30. this.buffer = document.getElementById("MathBuffer");
  31. },
  32. //
  33. // Switch the buffer and preview, and display the right one.
  34. // (We use visibility:hidden rather than display:none since
  35. // the results of running MathJax are more accurate that way.)
  36. //
  37. SwapBuffers: function () {
  38. var buffer = this.preview, preview = this.buffer;
  39. this.buffer = buffer; this.preview = preview;
  40. buffer.style.visibility = "hidden"; buffer.style.position = "absolute";
  41. preview.style.position = ""; preview.style.visibility = "";
  42. },
  43. //
  44. // This gets called when a key is pressed in the textarea.
  45. // We check if there is already a pending update and clear it if so.
  46. // Then set up an update to occur after a small delay (so if more keys
  47. // are pressed, the update won't occur until after there has been
  48. // a pause in the typing).
  49. // The callback function is set up below, after the Preview object is set up.
  50. //
  51. Update: function () {
  52. if (this.timeout) {clearTimeout(this.timeout)}
  53. this.timeout = setTimeout(this.callback,this.delay);
  54. },
  55. //
  56. // Creates the preview and runs MathJax on it.
  57. // If MathJax is already trying to render the code, return
  58. // If the text hasn't changed, return
  59. // Otherwise, indicate that MathJax is running, and start the
  60. // typesetting. After it is done, call PreviewDone.
  61. //
  62. CreatePreview: function () {
  63. Preview.timeout = null;
  64. if (this.mjPending) return;
  65. var text = document.getElementById("MathInput").value;
  66. if (text === this.oldtext) return;
  67. if (this.mjRunning) {
  68. this.mjPending = true;
  69. MathJax.Hub.Queue(["CreatePreview",this]);
  70. } else {
  71. this.buffer.innerHTML = this.oldtext = text;
  72. this.mjRunning = true;
  73. MathJax.Hub.Queue(
  74. ["Typeset",MathJax.Hub,this.buffer],
  75. ["PreviewDone",this]
  76. );
  77. }
  78. },
  79. //
  80. // Indicate that MathJax is no longer running,
  81. // and swap the buffers to show the results.
  82. //
  83. PreviewDone: function () {
  84. this.mjRunning = this.mjPending = false;
  85. this.SwapBuffers();
  86. }
  87. };
  88. //
  89. // Cache a callback to the CreatePreview action
  90. //
  91. Preview.callback = MathJax.Callback(["CreatePreview",Preview]);
  92. Preview.callback.autoReset = true; // make sure it can run more than once
  93. </script>
  94. </head>
  95. <body>
  96. <p> Type text (mixed with MathML, TeX or asciimath) in the box below for a live preview.</p>
  97. <textarea id="MathInput" cols="60" rows="10" onkeyup="Preview.Update()" style="margin-top:5px">
  98. </textarea>
  99. <p>Configured delimiters:
  100. <ul>
  101. <li>TeX, inline mode: <code>\(...\)</code> or <code>$...$</code></li>
  102. <li>TeX, display mode: <code>\[...\]</code> or <code> $$...$$</code></li>
  103. <li>Asciimath: <code>`...`</code>.</li>
  104. </ul>
  105. </p>
  106. <p>Preview is shown here:</p>
  107. <div id="MathPreview" style="border:1px solid; padding: 3px; width:50%; margin-top:5px"></div>
  108. <div id="MathBuffer" style="border:1px solid; padding: 3px; width:50%; margin-top:5px;
  109. visibility:hidden; position:absolute; top:0; left: 0"></div>
  110. <script>
  111. Preview.Init();
  112. </script>
  113. </body>
  114. </html>