Refactor code

This commit is contained in:
Tyler Long
2017-04-16 23:24:47 +08:00
parent 110e4ee586
commit 87c7f73245
6 changed files with 124 additions and 159 deletions

View File

@@ -4,7 +4,6 @@
*/ */
var Logger = require('./logger') var Logger = require('./logger')
// Logger.setLogLevel(1)
var log = Logger.Log var log = Logger.Log
var mermaidAPI = require('./mermaidAPI') var mermaidAPI = require('./mermaidAPI')
var nextId = 0 var nextId = 0

View File

@@ -36,11 +36,6 @@ var gitGraphRenderer = require('./diagrams/gitGraph/gitGraphRenderer')
var gitGraphAst = require('./diagrams/gitGraph/gitGraphAst') var gitGraphAst = require('./diagrams/gitGraph/gitGraphAst')
var d3 = require('./d3') var d3 = require('./d3')
// https://github.com/cpettitt/dagre-d3/issues/202
// window.SVGElement.prototype.getTransformToElement = window.SVGElement.prototype.getTransformToElement || function (toElement) {
// return toElement.getScreenCTM().inverse().multiply(this.getScreenCTM())
// }
/** /**
* ## Configuration * ## Configuration
* These are the default options which can be overridden with the initialization call as in the example below: * These are the default options which can be overridden with the initialization call as in the example below:
@@ -398,8 +393,6 @@ var render = function (id, txt, cb, container) {
window.txt = txt window.txt = txt
txt = exports.encodeEntities(txt) txt = exports.encodeEntities(txt)
// console.warn('mermaid encode: ');
// console.warn(txt);
var element = d3.select('#d' + id).node() var element = d3.select('#d' + id).node()
var graphType = utils.detectType(txt) var graphType = utils.detectType(txt)
@@ -409,10 +402,6 @@ var render = function (id, txt, cb, container) {
config.flowchart.arrowMarkerAbsolute = config.arrowMarkerAbsolute config.flowchart.arrowMarkerAbsolute = config.arrowMarkerAbsolute
gitGraphRenderer.setConf(config.gitGraph) gitGraphRenderer.setConf(config.gitGraph)
gitGraphRenderer.draw(txt, id, false) gitGraphRenderer.draw(txt, id, false)
// if(config.cloneCssStyles){
// classes = gitGraphRenderer.getClasses(txt, false);
// utils.cloneCssStyles(element.firstChild, classes);
// }
break break
case 'graph': case 'graph':
config.flowchart.arrowMarkerAbsolute = config.arrowMarkerAbsolute config.flowchart.arrowMarkerAbsolute = config.arrowMarkerAbsolute
@@ -479,10 +468,6 @@ var render = function (id, txt, cb, container) {
svgCode = exports.decodeEntities(svgCode) svgCode = exports.decodeEntities(svgCode)
// console.warn('mermaid decode: ');
// console.warn(svgCode);
// var he = require('he');
// svgCode = he.decode(svgCode);
if (typeof cb !== 'undefined') { if (typeof cb !== 'undefined') {
cb(svgCode, graph.bindFunctions) cb(svgCode, graph.bindFunctions)
} else { } else {

View File

@@ -6,22 +6,11 @@
* Created by knut on 14-11-23. * Created by knut on 14-11-23.
*/ */
var api = require('./mermaidAPI.js') var api = require('./mermaidAPI.js')
// var log = require('./logger').create();
describe('when using mermaidAPI and ', function () { describe('when using mermaidAPI and ', function () {
describe('doing initialize ', function () { describe('doing initialize ', function () {
// var main;
// var document;
// var window;
beforeEach(function () { beforeEach(function () {
// var MockBrowser = require('mock-browser').mocks.MockBrowser;
// var mock = new MockBrowser();
delete global.mermaid_config delete global.mermaid_config
// and in the run-code inside some object
// global.document = mock.getDocument();
// global.window = mock.getWindow();
document.body.innerHTML = '' document.body.innerHTML = ''
}) })
@@ -29,7 +18,7 @@ describe('when using mermaidAPI and ', function () {
var orgConfig = api.getConfig() var orgConfig = api.getConfig()
expect(orgConfig.testLiteral).toBe(undefined) expect(orgConfig.testLiteral).toBe(undefined)
api.initialize({'testLiteral': true}) api.initialize({ 'testLiteral': true })
var config = api.getConfig() var config = api.getConfig()
expect(config.testLiteral).toBe(true) expect(config.testLiteral).toBe(true)
@@ -43,8 +32,8 @@ describe('when using mermaidAPI and ', function () {
test2: false test2: false
} }
api.initialize({'testObject': object}) api.initialize({ 'testObject': object })
api.initialize({'testObject': {'test3': true}}) api.initialize({ 'testObject': { 'test3': true } })
var config = api.getConfig() var config = api.getConfig()
expect(config.testObject.test1).toBe(1) expect(config.testObject.test1).toBe(1)
@@ -55,7 +44,7 @@ describe('when using mermaidAPI and ', function () {
}) })
describe('checking validity of input ', function () { describe('checking validity of input ', function () {
it('it should return false for an invalid definiton', function () { it('it should return false for an invalid definiton', function () {
global.mermaidAPI.parseError = function () {} global.mermaidAPI.parseError = function () { }
spyOn(global.mermaidAPI, 'parseError') spyOn(global.mermaidAPI, 'parseError')
var res = api.parse('this is not a mermaid diagram definition') var res = api.parse('this is not a mermaid diagram definition')

View File

@@ -28,17 +28,14 @@ var detectType = function (text) {
} }
if (text.match(/^\s*digraph/)) { if (text.match(/^\s*digraph/)) {
// log.debug('Detected dot syntax');
return 'dotGraph' return 'dotGraph'
} }
if (text.match(/^\s*info/)) { if (text.match(/^\s*info/)) {
// log.debug('Detected info syntax');
return 'info' return 'info'
} }
if (text.match(/^\s*gantt/)) { if (text.match(/^\s*gantt/)) {
// log.debug('Detected info syntax');
return 'gantt' return 'gantt'
} }

View File

@@ -4,13 +4,8 @@
*/ */
var utils = require('./utils') var utils = require('./utils')
// var log = require('./logger').create();
describe('when detecting chart type ', function () { describe('when detecting chart type ', function () {
var str var str
beforeEach(function () {
})
it('should handle a graph defintion', function () { it('should handle a graph defintion', function () {
str = 'graph TB\nbfs1:queue' str = 'graph TB\nbfs1:queue'
@@ -40,11 +35,6 @@ describe('when detecting chart type ', function () {
describe('when cloning CSS ', function () { describe('when cloning CSS ', function () {
beforeEach(function () { beforeEach(function () {
// var MockBrowser = require('mock-browser').mocks.MockBrowser;
// var mock = new MockBrowser();
// and in the run-code inside some object
// global.document = mock.getDocument();
document.body.innerHTML = '' document.body.innerHTML = ''
}) })
@@ -136,7 +126,7 @@ describe('when cloning CSS ', function () {
var svg = document.createElement('svg') var svg = document.createElement('svg')
svg.setAttribute('id', 'mermaid-01') svg.setAttribute('id', 'mermaid-01')
utils.cloneCssStyles(svg, {'default': {'styles': ['stroke:#fff', 'stroke-width:1.5px']}}) utils.cloneCssStyles(svg, { 'default': { 'styles': ['stroke:#fff', 'stroke-width:1.5px'] } })
expect(stylesToArray(svg)).toEqual(['#mermaid-01 .node>rect { stroke:#fff; stroke-width:1.5px; }']) expect(stylesToArray(svg)).toEqual(['#mermaid-01 .node>rect { stroke:#fff; stroke-width:1.5px; }'])
// Also verify the elements around the styling // Also verify the elements around the styling
expect(svg.innerHTML).toBe('<style type="text/css" title="mermaid-svg-internal-css">/* <![CDATA[ */\n#mermaid-01 .node>rect { stroke:#fff; stroke-width:1.5px; }\n/* ]]> */\n</style>') expect(svg.innerHTML).toBe('<style type="text/css" title="mermaid-svg-internal-css">/* <![CDATA[ */\n#mermaid-01 .node>rect { stroke:#fff; stroke-width:1.5px; }\n/* ]]> */\n</style>')
@@ -179,7 +169,7 @@ describe('when cloning CSS ', function () {
it('should handle a default class together with stylesheet in document with classes in SVG', function () { it('should handle a default class together with stylesheet in document with classes in SVG', function () {
var svg = generateSVG() var svg = generateSVG()
addStyleToDocument() addStyleToDocument()
utils.cloneCssStyles(svg, {'default': {'styles': ['stroke:#ffffff', 'stroke-width:1.5px']}}) utils.cloneCssStyles(svg, { 'default': { 'styles': ['stroke:#ffffff', 'stroke-width:1.5px'] } })
expect(stylesToArray(svg)).toEqual(['#mermaid-01 .node>rect { stroke:#ffffff; stroke-width:1.5px; }', '.node { stroke: #eeeeee;}', '.node-square { stroke: #bbbbbb;}']) expect(stylesToArray(svg)).toEqual(['#mermaid-01 .node>rect { stroke:#ffffff; stroke-width:1.5px; }', '.node { stroke: #eeeeee;}', '.node-square { stroke: #bbbbbb;}'])
}) })
@@ -187,9 +177,9 @@ describe('when cloning CSS ', function () {
var svg = generateSVG() var svg = generateSVG()
addStyleToDocument() addStyleToDocument()
utils.cloneCssStyles(svg, { utils.cloneCssStyles(svg, {
'default': {'styles': ['stroke:#ffffff', 'stroke-width:1.5px']}, 'default': { 'styles': ['stroke:#ffffff', 'stroke-width:1.5px'] },
'node-square': {'styles': ['fill:#eeeeee', 'stroke:#aaaaaa']}, 'node-square': { 'styles': ['fill:#eeeeee', 'stroke:#aaaaaa'] },
'node-circle': {'styles': ['fill:#444444', 'stroke:#111111']} 'node-circle': { 'styles': ['fill:#444444', 'stroke:#111111'] }
}) })
expect(stylesToArray(svg)).toEqual(['#mermaid-01 .node>rect { stroke:#ffffff; stroke-width:1.5px; }', expect(stylesToArray(svg)).toEqual(['#mermaid-01 .node>rect { stroke:#ffffff; stroke-width:1.5px; }',
'.node { stroke: #eeeeee;}', '.node { stroke: #eeeeee;}',

View File

@@ -5,3 +5,8 @@
- default theme doesn't work for class diagram - default theme doesn't work for class diagram
- mermaidAPI.js cannot be required directly - mermaidAPI.js cannot be required directly
- compile less to css - compile less to css
- don't version control generated content, move them to outside project
- editor
- dist
- docs
- what's the correct way to change logLevel as an end user?