sample-dynamic-2a.html 3.8 KB

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