lazily load assets for mermaid, mathjax, and markmap

pull/1/head
ansuz 5 years ago
parent 81cb7d2bb3
commit a528d857a7

@ -19,42 +19,120 @@ define([
var renderer = new Marked.Renderer(); var renderer = new Marked.Renderer();
var restrictedRenderer = new Marked.Renderer(); var restrictedRenderer = new Marked.Renderer();
var Mermaid = { var argsMap = {};
init: function () {}
};
var MarkMapTransform = {
transform: function () {}
};
var Markmap = {
markmap: function () {}
};
var Mathjax = {
tex2svg: function () {}
};
var mermaidThemeCSS = //".node rect { fill: #DDD; stroke: #AAA; } " + var mermaidThemeCSS = //".node rect { fill: #DDD; stroke: #AAA; } " +
"rect.task, rect.task0, rect.task2 { stroke-width: 1 !important; rx: 0 !important; } " + "rect.task, rect.task0, rect.task2 { stroke-width: 1 !important; rx: 0 !important; } " +
"g.grid g.tick line { opacity: 0.25; }" + "g.grid g.tick line { opacity: 0.25; }" +
"g.today line { stroke: red; stroke-width: 1; stroke-dasharray: 3; opacity: 0.5; }"; "g.today line { stroke: red; stroke-width: 1; stroke-dasharray: 3; opacity: 0.5; }";
require([ var Mermaid = {
'mermaid', init: function () {
'/lib/markmap/transform.min.js', argsMap.mermaid = Util.slice(arguments);
'/lib/markmap/view.min.js', require([
'/bower_components/MathJax/es5/tex-svg.js', 'mermaid',
'css!/code/mermaid-new.css' 'css!/code/mermaid-new.css'
], function (_Mermaid, _Transform, _View) { ], function (_Mermaid) {
Mathjax = window.MathJax; console.debug("loaded mermaid");
Mermaid = _Mermaid;
MarkMapTransform = _Transform; Mermaid.initialize({
Markmap = _View; gantt: { axisFormat: '%m-%d', },
"themeCSS": mermaidThemeCSS,
Mermaid = _Mermaid; });
Mermaid.initialize({ Mermaid.init.call(argsMap.mermaid);
gantt: { axisFormat: '%m-%d', }, delete argsMap.mermaid;
"themeCSS": mermaidThemeCSS, });
}
};
var Mathjax = {
tex2svg: function () {
argsMap.Mathjax = Util.slice(arguments);
require([
'/bower_components/MathJax/es5/tex-svg.js',
], function () {
console.debug("Loaded mathjax");
Mathjax = window.MathJax;
if (!argsMap.Mathjax) { return; }
Mathjax.tex2svg(argsMap.Mathjax[0], argsMap.Mathjax[1]);
delete argsMap.Mathjax;
});
}
};
var drawMarkmap;
var MarkMapTransform;
var Markmap;
var markmapLoaded = false;
var loadMarkmap = function () {
require([
'/lib/markmap/transform.min.js',
'/lib/markmap/view.min.js',
], function (_Transform, _View) {
console.debug("Loaded markmap");
MarkMapTransform = _Transform;
Markmap = _View;
markmapLoaded = true;
drawMarkmap(argsMap.markmap);
delete argsMap.markmap;
}); });
}); };
var sfCommon;
var fixMathjaxClickables = function ($svg) {
// find all links in the tree and do the following for each one
var onClick = function (e) {
e.preventDefault();
e.stopImmediatePropagation();
var $el = $(e.target);
// Open links only from the preview modal
if (!sfCommon) { return void console.error('No sfCommon'); }
var href = $el.attr('href');
if (!href || !/^(https?:\/\/|\/)/.test(href)) { return; }
if (/^http/.test(href)) {
sfCommon.openUnsafeURL(href);
return;
}
sfCommon.openURL(href);
};
$svg.find('a').click(onClick);
// make sure the links added later by collapsing/expading the map are also safe
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
var n;
for (var i = 0; i < mutation.addedNodes.length; i++) {
n = mutation.addedNodes[i];
if (n.nodeName === "A") { return void n.addEventListener('click', onClick); }
$(n).find('a').click(onClick);
}
}
});
});
observer.observe($svg[0], {
childList: true,
subtree: true
});
};
drawMarkmap = function ($el) {
if (!markmapLoaded) {
argsMap.markmap = $el; //Util.slice(arguments);
loadMarkmap();
return;
}
if (!$el) { return console.error("no element provided"); }
var data = MarkMapTransform.transform($el[0].getAttribute("markmap-source"));
$el[0].innerHTML = "<svg width='100%' height='600'/>";
Markmap.markmap($el[0].firstChild, data);
fixMathjaxClickables($el);
};
var highlighter = function () { var highlighter = function () {
return function(code, lang) { return function(code, lang) {
@ -225,7 +303,6 @@ define([
return renderParagraph(p); return renderParagraph(p);
}; };
var MutationObserver = window.MutationObserver;
var forbiddenTags = [ var forbiddenTags = [
'SCRIPT', 'SCRIPT',
'IFRAME', 'IFRAME',
@ -270,7 +347,7 @@ define([
parent.removeChild(node); parent.removeChild(node);
}; };
var removeForbiddenTags = function (root) { var removeForbiddenTags = function (root) { // YYY
if (!root) { return; } if (!root) { return; }
if (forbiddenTags.indexOf(root.nodeName.toUpperCase()) !== -1) { removeNode(root); } if (forbiddenTags.indexOf(root.nodeName.toUpperCase()) !== -1) { removeNode(root); }
slice(root.children).forEach(removeForbiddenTags); slice(root.children).forEach(removeForbiddenTags);
@ -355,57 +432,14 @@ define([
} }
}; };
var sfCommon;
var fixMathjaxClickables = function ($svg) {
// find all links in the tree and do the following for each one
var onClick = function (e) {
e.preventDefault();
e.stopImmediatePropagation();
var $el = $(e.target);
// Open links only from the preview modal
if (!sfCommon) { return void console.error('No sfCommon'); }
var href = $el.attr('href');
if (!href || !/^(https?:\/\/|\/)/.test(href)) { return; }
if (/^http/.test(href)) {
sfCommon.openUnsafeURL(href);
return;
}
sfCommon.openURL(href);
};
$svg.find('a').click(onClick);
// make sure the links added later by collapsing/expading the map are also safe
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
var n;
for (var i = 0; i < mutation.addedNodes.length; i++) {
n = mutation.addedNodes[i];
if (n.nodeName === "A") { return void n.addEventListener('click', onClick); }
$(n).find('a').click(onClick);
}
}
});
});
observer.observe($svg[0], {
childList: true,
subtree: true
});
};
plugins.markmap = { plugins.markmap = {
name: 'markmap', name: 'markmap',
attr: 'markmap-source', attr: 'markmap-source',
render: function ($el) { render: function ($el) {
var data = MarkMapTransform.transform($el[0].getAttribute("markmap-source")); drawMarkmap($el);
$el[0].innerHTML = "<svg width='100%' height='600'/>";
Markmap.markmap($el[0].firstChild, data);
fixMathjaxClickables($el);
} }
}; };
DiffMd.apply = function (newHtml, $content, common) { DiffMd.apply = function (newHtml, $content, common) {
if (!sfCommon) { sfCommon = common; } if (!sfCommon) { sfCommon = common; }
@ -483,6 +517,7 @@ define([
var oldDom = domFromHTML($content[0].outerHTML); var oldDom = domFromHTML($content[0].outerHTML);
var MutationObserver = window.MutationObserver;
var onPreview = function ($mt) { var onPreview = function ($mt) {
return function () { return function () {
var mts = []; var mts = [];

Loading…
Cancel
Save