sample-dynamic-2.html 3.6 KB

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