mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-14 06:43:25 +08:00
Refactor code
This commit is contained in:
parent
6a7a5b9249
commit
110e4ee586
17
src/d3.js
vendored
17
src/d3.js
vendored
@ -3,7 +3,6 @@ const d3 = require('d3')
|
|||||||
module.exports = d3;
|
module.exports = d3;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
D3 Text Wrap
|
D3 Text Wrap
|
||||||
By Vijith Assar
|
By Vijith Assar
|
||||||
http://www.vijithassar.com
|
http://www.vijithassar.com
|
||||||
@ -11,7 +10,6 @@ module.exports = d3;
|
|||||||
@vijithassar
|
@vijithassar
|
||||||
|
|
||||||
Detailed instructions at http://www.github.com/vijithassar/d3textwrap
|
Detailed instructions at http://www.github.com/vijithassar/d3textwrap
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
(function () {
|
(function () {
|
||||||
@ -313,8 +311,6 @@ module.exports = d3;
|
|||||||
// adjust the length by the offset we've tracked
|
// adjust the length by the offset we've tracked
|
||||||
// due to the misreported length discussed above
|
// due to the misreported length discussed above
|
||||||
|
|
||||||
// var testWidth = newWidth - totalOffset
|
|
||||||
|
|
||||||
// if our latest version of the string is too
|
// if our latest version of the string is too
|
||||||
// big for the bounds, use the previous
|
// big for the bounds, use the previous
|
||||||
// version of the string (without the newest word
|
// version of the string (without the newest word
|
||||||
@ -363,9 +359,6 @@ module.exports = d3;
|
|||||||
textNodeSelected.text('')
|
textNodeSelected.text('')
|
||||||
for (i = 0; i < substrings.length; i++) {
|
for (i = 0; i < substrings.length; i++) {
|
||||||
substring = substrings[i].string
|
substring = substrings[i].string
|
||||||
// if (i > 0) {
|
|
||||||
// var previousSubstring = substrings[i - 1]
|
|
||||||
// }
|
|
||||||
// only append if we're sure it won't make the tspans
|
// only append if we're sure it won't make the tspans
|
||||||
// overflow the bounds.
|
// overflow the bounds.
|
||||||
if ((i) * lineHeight < bounds.height - (lineHeight * 1.5)) {
|
if ((i) * lineHeight < bounds.height - (lineHeight * 1.5)) {
|
||||||
@ -387,15 +380,6 @@ module.exports = d3;
|
|||||||
if (padding) { xOffset += padding }
|
if (padding) { xOffset += padding }
|
||||||
return xOffset
|
return xOffset
|
||||||
})
|
})
|
||||||
// .attr('dx', function() {
|
|
||||||
// if(i == 0) {
|
|
||||||
// var render_offset = 0;
|
|
||||||
// } else if(i > 0) {
|
|
||||||
// render_offset = substrings[i - 1].width;
|
|
||||||
// render_offset = render_offset * -1;
|
|
||||||
// }
|
|
||||||
// return render_offset;
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -461,4 +445,3 @@ module.exports = d3;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
/* jshint ignore:end */
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
function decodeHTMLEntities (str) {
|
function decodeHTMLEntities (str) {
|
||||||
if (str && typeof str === 'string') {
|
if (str && typeof str === 'string') {
|
||||||
// strip script/html tags
|
// strip script/html tags
|
||||||
var element = document.querySelector('.editor')
|
var element = document.querySelector('.editor')
|
||||||
|
|
||||||
str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '')
|
str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '')
|
||||||
|
@ -10,29 +10,14 @@
|
|||||||
* => [2011-3-3T20:24:4.810 error (5021)] booom
|
* => [2011-3-3T20:24:4.810 error (5021)] booom
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// const LEVELS = {
|
|
||||||
// debug: 1,
|
|
||||||
// info: 2,
|
|
||||||
// warn: 3,
|
|
||||||
// error: 4,
|
|
||||||
// fatal: 5,
|
|
||||||
// default: 5
|
|
||||||
// }
|
|
||||||
|
|
||||||
// var defaultLevel = LEVELS.error
|
|
||||||
|
|
||||||
// exports.setLogLevel = function (level) {
|
|
||||||
// defaultLevel = level;
|
|
||||||
// };
|
|
||||||
|
|
||||||
function formatTime (timestamp) {
|
function formatTime (timestamp) {
|
||||||
var hh = timestamp.getUTCHours()
|
var hh = timestamp.getUTCHours()
|
||||||
var mm = timestamp.getUTCMinutes()
|
var mm = timestamp.getUTCMinutes()
|
||||||
var ss = timestamp.getSeconds()
|
var ss = timestamp.getSeconds()
|
||||||
var ms = timestamp.getMilliseconds()
|
var ms = timestamp.getMilliseconds()
|
||||||
// If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
|
// If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
|
||||||
// if (hh > 12) {hh = hh % 12;}
|
// if (hh > 12) {hh = hh % 12;}
|
||||||
// These lines ensure you have two-digits
|
// These lines ensure you have two-digits
|
||||||
if (hh < 10) {
|
if (hh < 10) {
|
||||||
hh = '0' + hh
|
hh = '0' + hh
|
||||||
}
|
}
|
||||||
@ -48,7 +33,7 @@ function formatTime (timestamp) {
|
|||||||
if (ms < 10) {
|
if (ms < 10) {
|
||||||
ms = '00' + ms
|
ms = '00' + ms
|
||||||
}
|
}
|
||||||
// This formats your string to HH:MM:SS
|
// This formats your string to HH:MM:SS
|
||||||
var t = hh + ':' + mm + ':' + ss + ' (' + ms + ')'
|
var t = hh + ':' + mm + ':' + ss + ' (' + ms + ')'
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
@ -58,11 +43,11 @@ function format (level) {
|
|||||||
return '%c ' + time + ' :%c' + level + ': '
|
return '%c ' + time + ' :%c' + level + ': '
|
||||||
}
|
}
|
||||||
|
|
||||||
var debug = function () {}
|
var debug = function () { }
|
||||||
var info = function () {}
|
var info = function () { }
|
||||||
var warn = function () {}
|
var warn = function () { }
|
||||||
var error = function () {}
|
var error = function () { }
|
||||||
var fatal = function () {}
|
var fatal = function () { }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* logLevel , decides the amount of logging to be used.
|
* logLevel , decides the amount of logging to be used.
|
||||||
|
@ -38,7 +38,7 @@ var init = function () {
|
|||||||
log.debug('Starting rendering diagrams')
|
log.debug('Starting rendering diagrams')
|
||||||
var nodes
|
var nodes
|
||||||
if (arguments.length >= 2) {
|
if (arguments.length >= 2) {
|
||||||
/*! sequence config was passed as #1 */
|
/*! sequence config was passed as #1 */
|
||||||
if (typeof arguments[0] !== 'undefined') {
|
if (typeof arguments[0] !== 'undefined') {
|
||||||
global.mermaid.sequenceConfig = arguments[0]
|
global.mermaid.sequenceConfig = arguments[0]
|
||||||
}
|
}
|
||||||
@ -48,7 +48,7 @@ var init = function () {
|
|||||||
nodes = arguments[0]
|
nodes = arguments[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// if last argument is a function this is the callback function
|
// if last argument is a function this is the callback function
|
||||||
var callback
|
var callback
|
||||||
if (typeof arguments[arguments.length - 1] === 'function') {
|
if (typeof arguments[arguments.length - 1] === 'function') {
|
||||||
callback = arguments[arguments.length - 1]
|
callback = arguments[arguments.length - 1]
|
||||||
@ -64,23 +64,21 @@ var init = function () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
nodes = nodes === undefined ? document.querySelectorAll('.mermaid')
|
nodes = nodes === undefined ? document.querySelectorAll('.mermaid')
|
||||||
: typeof nodes === 'string' ? document.querySelectorAll(nodes)
|
: typeof nodes === 'string' ? document.querySelectorAll(nodes)
|
||||||
: nodes instanceof window.Node ? [nodes]
|
: nodes instanceof window.Node ? [nodes]
|
||||||
: nodes // Last case - sequence config was passed pick next
|
: nodes // Last case - sequence config was passed pick next
|
||||||
|
|
||||||
var i
|
|
||||||
|
|
||||||
if (typeof global.mermaid_config !== 'undefined') {
|
if (typeof global.mermaid_config !== 'undefined') {
|
||||||
mermaidAPI.initialize(global.mermaid_config)
|
mermaidAPI.initialize(global.mermaid_config)
|
||||||
}
|
}
|
||||||
log.debug('Start On Load before: ' + global.mermaid.startOnLoad)
|
log.debug('Start On Load before: ' + global.mermaid.startOnLoad)
|
||||||
if (typeof global.mermaid.startOnLoad !== 'undefined') {
|
if (typeof global.mermaid.startOnLoad !== 'undefined') {
|
||||||
log.debug('Start On Load inner: ' + global.mermaid.startOnLoad)
|
log.debug('Start On Load inner: ' + global.mermaid.startOnLoad)
|
||||||
mermaidAPI.initialize({startOnLoad: global.mermaid.startOnLoad})
|
mermaidAPI.initialize({ startOnLoad: global.mermaid.startOnLoad })
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof global.mermaid.ganttConfig !== 'undefined') {
|
if (typeof global.mermaid.ganttConfig !== 'undefined') {
|
||||||
mermaidAPI.initialize({gantt: global.mermaid.ganttConfig})
|
mermaidAPI.initialize({ gantt: global.mermaid.ganttConfig })
|
||||||
}
|
}
|
||||||
|
|
||||||
var txt
|
var txt
|
||||||
@ -92,10 +90,10 @@ var init = function () {
|
|||||||
bindFunctions(element)
|
bindFunctions(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < nodes.length; i++) {
|
for (var i = 0; i < nodes.length; i++) {
|
||||||
var element = nodes[i]
|
var element = nodes[i]
|
||||||
|
|
||||||
/*! Check if previously processed */
|
/*! Check if previously processed */
|
||||||
if (!element.getAttribute('data-processed')) {
|
if (!element.getAttribute('data-processed')) {
|
||||||
element.setAttribute('data-processed', true)
|
element.setAttribute('data-processed', true)
|
||||||
} else {
|
} else {
|
||||||
@ -104,16 +102,11 @@ var init = function () {
|
|||||||
|
|
||||||
var id = 'mermaidChart' + nextId++
|
var id = 'mermaidChart' + nextId++
|
||||||
|
|
||||||
// Fetch the graph definition including tags
|
// Fetch the graph definition including tags
|
||||||
txt = element.innerHTML
|
txt = element.innerHTML
|
||||||
|
|
||||||
// console.warn('delivererd from the browser: ');
|
// transforms the html to pure text
|
||||||
// console.warn(txt);
|
|
||||||
|
|
||||||
// transforms the html to pure text
|
|
||||||
txt = he.decode(txt).trim()
|
txt = he.decode(txt).trim()
|
||||||
// console.warn('he decode: ');
|
|
||||||
// console.warn(txt);
|
|
||||||
|
|
||||||
mermaidAPI.render(id, txt, insertSvg, element)
|
mermaidAPI.render(id, txt, insertSvg, element)
|
||||||
}
|
}
|
||||||
@ -204,7 +197,7 @@ exports.parseError = global.mermaid.parseError
|
|||||||
*/
|
*/
|
||||||
exports.contentLoaded = function () {
|
exports.contentLoaded = function () {
|
||||||
var config
|
var config
|
||||||
// Check state of start config mermaid namespace
|
// Check state of start config mermaid namespace
|
||||||
if (typeof global.mermaid_config !== 'undefined') {
|
if (typeof global.mermaid_config !== 'undefined') {
|
||||||
if (equals(false, global.mermaid_config.htmlLabels)) {
|
if (equals(false, global.mermaid_config.htmlLabels)) {
|
||||||
global.mermaid.htmlLabels = false
|
global.mermaid.htmlLabels = false
|
||||||
@ -212,43 +205,35 @@ exports.contentLoaded = function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (global.mermaid.startOnLoad) {
|
if (global.mermaid.startOnLoad) {
|
||||||
// For backwards compatability reasons also check mermaid_config variable
|
// For backwards compatability reasons also check mermaid_config variable
|
||||||
if (typeof global.mermaid_config !== 'undefined') {
|
if (typeof global.mermaid_config !== 'undefined') {
|
||||||
// Check if property startOnLoad is set
|
// Check if property startOnLoad is set
|
||||||
if (equals(true, global.mermaid_config.startOnLoad)) {
|
if (equals(true, global.mermaid_config.startOnLoad)) {
|
||||||
global.mermaid.init()
|
global.mermaid.init()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// No config found, do check API config
|
// No config found, do check API config
|
||||||
config = mermaidAPI.getConfig()
|
config = mermaidAPI.getConfig()
|
||||||
if (config.startOnLoad) {
|
if (config.startOnLoad) {
|
||||||
global.mermaid.init()
|
global.mermaid.init()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// if(typeof global.mermaid === 'undefined' ){
|
|
||||||
if (typeof global.mermaid.startOnLoad === 'undefined') {
|
if (typeof global.mermaid.startOnLoad === 'undefined') {
|
||||||
log.debug('In start, no config')
|
log.debug('In start, no config')
|
||||||
config = mermaidAPI.getConfig()
|
config = mermaidAPI.getConfig()
|
||||||
if (config.startOnLoad) {
|
if (config.startOnLoad) {
|
||||||
global.mermaid.init()
|
global.mermaid.init()
|
||||||
}
|
}
|
||||||
// }else{
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof document !== 'undefined') {
|
if (typeof document !== 'undefined') {
|
||||||
/*!
|
/*!
|
||||||
* Wait for document loaded before starting the execution
|
* Wait for document loaded before starting the execution
|
||||||
*/
|
*/
|
||||||
window.addEventListener('load', function () {
|
window.addEventListener('load', function () {
|
||||||
exports.contentLoaded()
|
exports.contentLoaded()
|
||||||
}, false)
|
}, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Your actual module
|
|
||||||
// return module.exports;
|
|
||||||
// }));
|
|
||||||
|
@ -5,40 +5,20 @@
|
|||||||
/**
|
/**
|
||||||
* Created by knut on 14-11-23.
|
* Created by knut on 14-11-23.
|
||||||
*/
|
*/
|
||||||
// var rewire = require('rewire');
|
|
||||||
var mermaid = require('./mermaid')
|
var mermaid = require('./mermaid')
|
||||||
// var log = require('./logger').create();
|
|
||||||
|
|
||||||
describe('when using mermaid and ', function () {
|
describe('when using mermaid and ', function () {
|
||||||
describe('when detecting chart type ', function () {
|
describe('when detecting chart type ', function () {
|
||||||
// var mermaid;
|
|
||||||
// var document;
|
|
||||||
// var window;
|
|
||||||
beforeEach(function () {
|
|
||||||
// var MockBrowser = require('mock-browser').mocks.MockBrowser;
|
|
||||||
// var mock = new MockBrowser();
|
|
||||||
//
|
|
||||||
// delete global.mermaid_config;
|
|
||||||
//
|
|
||||||
/// / and in the run-code inside some object
|
|
||||||
// global.document = mock.getDocument();
|
|
||||||
// global.window = mock.getWindow();
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should not start rendering with mermaid_config.startOnLoad set to false', function () {
|
it('should not start rendering with mermaid_config.startOnLoad set to false', function () {
|
||||||
// mermaid = rewire('./mermaid');
|
|
||||||
global.mermaid_config = {startOnLoad: false}
|
global.mermaid_config = {startOnLoad: false}
|
||||||
|
|
||||||
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
|
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
|
||||||
spyOn(global.mermaid, 'init')
|
spyOn(global.mermaid, 'init')
|
||||||
// log.debug(mermaid);
|
|
||||||
mermaid.contentLoaded()
|
mermaid.contentLoaded()
|
||||||
expect(global.mermaid.init).not.toHaveBeenCalled()
|
expect(global.mermaid.init).not.toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not start rendering with mermaid.startOnLoad set to false', function () {
|
it('should not start rendering with mermaid.startOnLoad set to false', function () {
|
||||||
// mermaid = rewire('./mermaid');
|
|
||||||
global.mermaid.startOnLoad = false
|
global.mermaid.startOnLoad = false
|
||||||
global.mermaid_config = {startOnLoad: true}
|
global.mermaid_config = {startOnLoad: true}
|
||||||
|
|
||||||
@ -49,7 +29,6 @@ describe('when using mermaid and ', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should start rendering with both startOnLoad set', function () {
|
it('should start rendering with both startOnLoad set', function () {
|
||||||
// mermaid = rewire('./mermaid');
|
|
||||||
global.mermaid.startOnLoad = true
|
global.mermaid.startOnLoad = true
|
||||||
global.mermaid_config = {startOnLoad: true}
|
global.mermaid_config = {startOnLoad: true}
|
||||||
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
|
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
|
||||||
@ -59,7 +38,6 @@ describe('when using mermaid and ', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should start rendering with mermaid.startOnLoad set and no mermaid_config defined', function () {
|
it('should start rendering with mermaid.startOnLoad set and no mermaid_config defined', function () {
|
||||||
// mermaid = rewire('./mermaid');
|
|
||||||
global.mermaid.startOnLoad = true
|
global.mermaid.startOnLoad = true
|
||||||
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
|
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
|
||||||
spyOn(global.mermaid, 'init')
|
spyOn(global.mermaid, 'init')
|
||||||
@ -68,7 +46,6 @@ describe('when using mermaid and ', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should start rendering as a default with no changes performed', function () {
|
it('should start rendering as a default with no changes performed', function () {
|
||||||
// mermaid = rewire('./mermaid');
|
|
||||||
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
|
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>'
|
||||||
spyOn(global.mermaid, 'init')
|
spyOn(global.mermaid, 'init')
|
||||||
mermaid.contentLoaded()
|
mermaid.contentLoaded()
|
||||||
@ -83,12 +60,8 @@ describe('when using mermaid and ', function () {
|
|||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
global.mermaid_config = {startOnLoad: false}
|
global.mermaid_config = {startOnLoad: false}
|
||||||
// var MockBrowser = require('mock-browser').mocks.MockBrowser;
|
|
||||||
// var mock = new MockBrowser();
|
|
||||||
flow.parser.yy = graph
|
flow.parser.yy = graph
|
||||||
graph.clear()
|
graph.clear()
|
||||||
// global.document = mock.getDocument();
|
|
||||||
// mermaid = rewire('./mermaid');
|
|
||||||
})
|
})
|
||||||
it('it should handle edges with text', function () {
|
it('it should handle edges with text', function () {
|
||||||
flow.parser.parse('graph TD;A-->|text ex|B;')
|
flow.parser.parse('graph TD;A-->|text ex|B;')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user