www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

fast-preview.js (5486B)


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