diff --git a/lib/phantomscript.js b/lib/phantomscript.js index 8d2d7f774..8f8746825 100644 --- a/lib/phantomscript.js +++ b/lib/phantomscript.js @@ -58,7 +58,7 @@ page.content = [ , '' ].join('\n') -page.injectJs('../dist/mermaid.full.js') +page.injectJs('../dist/mermaid-legacy.full.js') page.onConsoleMessage = function(msg, lineNum, sourceId) { console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")'); }; diff --git a/src/d3.js b/src/d3.js new file mode 100644 index 000000000..121c74ca5 --- /dev/null +++ b/src/d3.js @@ -0,0 +1,27 @@ +/* global window */ +console.log('Setting up d3'); +var d3; + +if (require) { + try { + d3 = require("d3"); + } catch (e) { + console.log('Exception ... but ok'); + //console.log(e); + } +} + +//console.log(d3); + +if (!d3) { + //if(typeof window !== 'undefined') + d3 = window.d3; +} + +//if(typeof window === 'undefined'){ +// window = {}; +// window.d3 = d3; +//} +//console.log('window'); +//console.log(window); +module.exports = d3; diff --git a/src/mermaid.js b/src/mermaid.js new file mode 100644 index 000000000..275721177 --- /dev/null +++ b/src/mermaid.js @@ -0,0 +1,155 @@ +var he = require('he'); +var mermaidAPI = require('./mermaidAPI'); +var nextId = 0; + +/** + * Function that goes through the document to find the chart definitions in there and render them. + * + * The function tags the processed attributes with the attribute data-processed and ignores found elements with the + * attribute already set. This way the init function can be triggered several times. + * + * Optionally, `init` can accept in the second argument one of the following: + * - a DOM Node + * - an array of DOM nodes (as would come from a jQuery selector) + * - a W3C selector, a la `.mermaid` + * + * ``` + * graph LR; + * a(Find elements)-->b{Processed}; + * b-->|Yes|c(Leave element); + * c-->|No |d(Transform); + * ``` + */ + +/** + * Renders the mermaid diagrams + * @* param nodes- a css selector or an array of nodes + */ +var init = function () { + var nodes; + if(arguments.length === 2){ + // sequence config was passed as #1 + if(typeof arguments[0] !== 'undefined'){ + mermaid.sequenceConfig = arguments[0]; + } + + nodes = arguments[1]; + } + else{ + nodes = arguments[0]; + } + + nodes = nodes === undefined ? document.querySelectorAll('.mermaid') + : typeof nodes === "string" ? document.querySelectorAll(nodes) + : nodes instanceof Node ? [nodes] + // Last case - sequence config was passed pick next + : nodes; + + var i; + + console.log('Found ',nodes.length,' nodes'); + var insertSvg = function(svgCode){ + element.innerHTML = svgCode; + }; + + for (i = 0; i < nodes.length; i++) { + var element = nodes[i]; + + // Check if previously processed + if(!element.getAttribute("data-processed")) { + element.setAttribute("data-processed", true); + } else { + continue; + } + + var id = 'mermaidChart' + nextId++; + + var txt = element.innerHTML; + txt = txt.replace(/>/g,'>'); + txt = txt.replace(/'; + htmlStub = ''; + // // html file skull with a container div for the d3 dataviz + // + // pass the html stub to jsDom + /* jsdom.env({ + features : { QuerySelectorAll : true }, + html : htmlStub, + done : function(errors, win) { + // process the html document, like if we were at client side + // code to generate the dataviz and process the resulting html file to be added here + //var d3 = require('d3'); + //console.log('Here we go: '+JSON.stringify(d3)); + + global.document = win.document; + global.window = win; + + var element = win.document.createElement('div'); + element.setAttribute('id','did'); + //document. + console.log(document.body.innerHTML); + //console.log('Element:',element); + //console.log(win); + //mermaid.init(); + //render(win.document, 'myId', text, callback); + + } + });*/ + //var jsdom = require('jsdom').jsdom; + //global.document = jsdom(htmlStub); + //global.window = document.parentWindow; + // + //render(id, text, cb); + //var element = win.document.createElement('div'); + //element.setAttribute('id','did'); + //document. + } + else{ + // In browser + render( id, text, cb); + } +}; + +global.api = { + render : exports.render, + detectType: utils.detectType +}; + +console.log('APA'); + +//var getBBox = function(selector){ +// var xmin, xmax, ymin, ymax,p; +// // clean up path +// var t = d3.select(selector).attr("d"); // get svg line's code +// console.log(t) +// t = t.replace(/[a-z].*/g," ") // remove relative coords, could rather tag it for later processing to absolute! +// .replace(/[\sA-Z]+/gi," ").trim().split(" "); // remove letters and simplify spaces. +// console.log(t) +// +// for(var i in t){ // set valid initial values +// if(t[i].length>1){ +// p = t[i].split(","); +// xmin = xmax = p[0]; ymin = ymax = p[1]; } +// } +// for(var i in t){ // update xmin,xmax,ymin,ymax +// p = t[i].split(","); +// if(!p[1]){ p[0]=xmin; p[1] = ymin;} // ignore relative jumps such h20 v-10 +// xmin = Math.min(xmin, p[0]); +// xmax = Math.max(xmax, p[0]); +// ymin = Math.min(ymin, p[1]); +// ymax = Math.max(ymax, p[1]); +// } return [[xmin,ymax],[xmax,ymin]]; // [[left, bottom], [right, top]] as for https://github.com/mbostock/d3/wiki/Geo-Paths#bounds +//} +//var bb = getBBox("path"); \ No newline at end of file