CHTML-preview.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80: */
  3. /*************************************************************
  4. *
  5. * MathJax/extensions/CHTML-preview.js
  6. *
  7. * Implements a fast preview using the Common-HTML output jax
  8. * and then a slower update to the more accurate HTML-CSS output
  9. * (or whatever the user has selected).
  10. *
  11. * ---------------------------------------------------------------------
  12. *
  13. * Copyright (c) 2014-2015 The MathJax Consortium
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. (function (HUB,HTML,BROWSER) {
  28. var SETTINGS = HUB.config.menuSettings;
  29. var CHTMLpreview = MathJax.Extension["CHTML-preview"] = {
  30. version: "2.5.2",
  31. //
  32. // Configuration for the chunking of the main output
  33. // after the previews have been created, and other configuration.
  34. //
  35. config: HUB.CombineConfig("CHTML-preview",{
  36. Chunks: {EqnChunk: 10000, EqnChunkFactor: 1, EqnChunkDelay: 0},
  37. color: "inherit!important",
  38. updateTime: 30, updateDelay: 6,
  39. messageStyle: "none",
  40. disabled: BROWSER.isMSIE && !BROWSER.versionAtLeast("8.0")
  41. }),
  42. //
  43. // Ajust the chunking of the output jax
  44. //
  45. Config: function () {
  46. HUB.Config({
  47. "HTML-CSS": this.config.Chunks,
  48. SVG: this.config.Chunks
  49. });
  50. MathJax.Ajax.Styles({".MathJax_Preview .MJXc-math":{color:this.config.color}});
  51. var update, delay, style, done, saved;
  52. var config = this.config;
  53. if (!config.disabled && SETTINGS.CHTMLpreview == null)
  54. HUB.Config({menuSettings:{CHTMLpreview:true}});
  55. HUB.Register.MessageHook("Begin Math Output",function () {
  56. if (!done && SETTINGS.CHTMLpreview && SETTINGS.renderer !== "CommonHTML") {
  57. update = HUB.processUpdateTime; delay = HUB.processUpdateDelay;
  58. style = HUB.config.messageStyle;
  59. HUB.processUpdateTime = config.updateTime;
  60. HUB.processUpdateDelay = config.updateDelay;
  61. HUB.Config({messageStyle: config.messageStyle});
  62. MathJax.Message.Clear(0,0);
  63. saved = true;
  64. }
  65. });
  66. HUB.Register.MessageHook("End Math Output",function () {
  67. if (!done && saved) {
  68. HUB.processUpdateTime = update;
  69. HUB.processUpdateDelay = delay;
  70. HUB.Config({messageStyle: style});
  71. done = true;
  72. }
  73. });
  74. },
  75. //
  76. // Insert a preview span, if there isn't one already,
  77. // and call the CommonHTML output jax to create the preview
  78. //
  79. Preview: function (data) {
  80. if (!SETTINGS.CHTMLpreview || SETTINGS.renderer === "CommonHTML") return;
  81. var preview = data.script.MathJax.preview || data.script.previousSibling;
  82. if (!preview || preview.className !== MathJax.Hub.config.preRemoveClass) {
  83. preview = HTML.Element("span",{className:MathJax.Hub.config.preRemoveClass});
  84. data.script.parentNode.insertBefore(preview,data.script);
  85. data.script.MathJax.preview = preview;
  86. }
  87. preview.innerHTML = ""; preview.style.color = "inherit";
  88. return this.postFilter(preview,data);
  89. },
  90. postFilter: function (preview,data) {
  91. //
  92. // Load the CommonHTML jax if it is not already loaded
  93. //
  94. if (!data.math.root.toCommonHTML) {
  95. var queue = MathJax.Callback.Queue();
  96. queue.Push(
  97. ["Require",MathJax.Ajax,"[MathJax]/jax/output/CommonHTML/config.js"],
  98. ["Require",MathJax.Ajax,"[MathJax]/jax/output/CommonHTML/jax.js"]
  99. );
  100. HUB.RestartAfter(queue.Push({}));
  101. }
  102. data.math.root.toCommonHTML(preview);
  103. },
  104. //
  105. // Hook into the input jax postFilter to create the previews as
  106. // the input jax are processed.
  107. //
  108. Register: function (name) {
  109. HUB.Register.StartupHook(name+" Jax Require",function () {
  110. var jax = MathJax.InputJax[name];
  111. jax.postfilterHooks.Add(["Preview",MathJax.Extension["CHTML-preview"]],50);
  112. });
  113. }
  114. }
  115. //
  116. // Hook into each input jax
  117. //
  118. CHTMLpreview.Register("TeX");
  119. CHTMLpreview.Register("MathML");
  120. CHTMLpreview.Register("AsciiMath");
  121. HUB.Register.StartupHook("End Config",["Config",CHTMLpreview]);
  122. HUB.Startup.signal.Post("CHTML-preview Ready");
  123. })(MathJax.Hub,MathJax.HTML,MathJax.Hub.Browser);
  124. MathJax.Ajax.loadComplete("[MathJax]/extensions/CHTML-preview.js");