fast-preview.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/fast-preview.js
  6. *
  7. * Implements a fast preview using the PreviewHTML 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-2018 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 JAX = MathJax.OutputJax;
  30. var msieColorBug = BROWSER.isMSIE && (document.documentMode||0) < 8;
  31. var FastPreview = MathJax.Extension["fast-preview"] = {
  32. version: "2.7.5",
  33. enabled: true,
  34. //
  35. // Configuration for the chunking of the main output
  36. // after the previews have been created, and other configuration.
  37. //
  38. config: HUB.CombineConfig("fast-preview",{
  39. Chunks: {EqnChunk: 10000, EqnChunkFactor: 1, EqnChunkDelay: 0},
  40. color: "inherit!important",
  41. updateTime: 30, updateDelay: 6,
  42. messageStyle: "none",
  43. disabled: BROWSER.isMSIE && !BROWSER.versionAtLeast("8.0")
  44. }),
  45. //
  46. // Ajust the chunking of the output jax
  47. //
  48. Config: function () {
  49. if (HUB.config["CHTML-preview"])
  50. MathJax.Hub.Config({"fast-preview": HUB.config["CHTML-preview"]});
  51. var update, delay, style, done, saved;
  52. var config = this.config;
  53. if (!config.disabled && SETTINGS.FastPreview == null)
  54. HUB.Config({menuSettings:{FastPreview:true}});
  55. if (SETTINGS.FastPreview) {
  56. MathJax.Ajax.Styles({".MathJax_Preview .MJXf-math":{color:config.color}});
  57. HUB.Config({"HTML-CSS": config.Chunks, CommonHTML: config.Chunks, SVG: config.Chunks});
  58. }
  59. HUB.Register.MessageHook("Begin Math Output",function () {
  60. if (!done && FastPreview.Active()) {
  61. update = HUB.processUpdateTime; delay = HUB.processUpdateDelay;
  62. style = HUB.config.messageStyle;
  63. HUB.processUpdateTime = config.updateTime;
  64. HUB.processUpdateDelay = config.updateDelay;
  65. HUB.Config({messageStyle: config.messageStyle});
  66. MathJax.Message.Clear(0,0);
  67. saved = true;
  68. }
  69. });
  70. HUB.Register.MessageHook("End Math Output",function () {
  71. if (!done && saved) {
  72. HUB.processUpdateTime = update;
  73. HUB.processUpdateDelay = delay;
  74. HUB.Config({messageStyle: style});
  75. done = true;
  76. }
  77. });
  78. },
  79. //
  80. // Allow page to override user settings (for things like editor previews)
  81. //
  82. Disable: function () {this.enabled = false},
  83. Enable: function () {this.enabled = true},
  84. Active: function () {
  85. return SETTINGS.FastPreview && this.enabled &&
  86. !(JAX[SETTINGS.renderer]||{}).noFastPreview;
  87. },
  88. //
  89. // Insert a preview span, if there isn't one already,
  90. // and call the PreviewHTML output jax to create the preview
  91. //
  92. Preview: function (data) {
  93. if (!this.Active() || !data.script.parentNode) return;
  94. var preview = data.script.MathJax.preview || data.script.previousSibling;
  95. if (!preview || preview.className !== MathJax.Hub.config.preRemoveClass) {
  96. preview = HTML.Element("span",{className:MathJax.Hub.config.preRemoveClass});
  97. data.script.parentNode.insertBefore(preview,data.script);
  98. data.script.MathJax.preview = preview;
  99. }
  100. preview.innerHTML = "";
  101. preview.style.color = (msieColorBug ? "black" : "inherit");
  102. return this.postFilter(preview,data);
  103. },
  104. postFilter: function (preview,data) {
  105. //
  106. // Load the PreviewHTML jax if it is not already loaded
  107. //
  108. if (!data.math.root.toPreviewHTML) {
  109. var queue = MathJax.Callback.Queue();
  110. queue.Push(
  111. ["Require",MathJax.Ajax,"[MathJax]/jax/output/PreviewHTML/config.js"],
  112. ["Require",MathJax.Ajax,"[MathJax]/jax/output/PreviewHTML/jax.js"]
  113. );
  114. HUB.RestartAfter(queue.Push({}));
  115. }
  116. data.math.root.toPreviewHTML(preview);
  117. },
  118. //
  119. // Hook into the input jax postFilter to create the previews as
  120. // the input jax are processed.
  121. //
  122. Register: function (name) {
  123. HUB.Register.StartupHook(name+" Jax Require",function () {
  124. var jax = MathJax.InputJax[name];
  125. jax.postfilterHooks.Add(["Preview",MathJax.Extension["fast-preview"]],50);
  126. });
  127. }
  128. }
  129. //
  130. // Hook into each input jax
  131. //
  132. FastPreview.Register("TeX");
  133. FastPreview.Register("MathML");
  134. FastPreview.Register("AsciiMath");
  135. HUB.Register.StartupHook("End Config",["Config",FastPreview]);
  136. HUB.Startup.signal.Post("fast-preview Ready");
  137. })(MathJax.Hub,MathJax.HTML,MathJax.Hub.Browser);
  138. MathJax.Ajax.loadComplete("[MathJax]/extensions/fast-preview.js");