No global mermaidAPI

This commit is contained in:
Tyler Long 2017-09-10 10:45:38 +08:00
parent ee912c2b29
commit a5b7145527
4 changed files with 27 additions and 29 deletions

View File

@ -2,11 +2,12 @@
* Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid functionality and to render
* the diagrams to svg code.
*/
var he = require('he')
import he from 'he'
import mermaidAPI from './mermaidAPI'
var Logger = require('./logger')
var log = Logger.Log
var mermaidAPI = require('./mermaidAPI')
var nextId = 0
/**

View File

@ -242,7 +242,7 @@ var config = {
Logger.setLogLevel(config.logLevel)
var parse = function (text) {
function parse (text) {
var graphType = utils.detectType(text)
var parser
@ -284,7 +284,6 @@ var parse = function (text) {
parser.parse(text)
}
module.exports.parse = parse
/**
* ## version
@ -476,7 +475,7 @@ var render = function (id, txt, cb, container) {
return svgCode
}
module.exports.render = function (id, text, cb, containerElement) {
function render2 (id, text, cb, containerElement) {
try {
if (arguments.length === 1) {
text = id
@ -517,7 +516,7 @@ var setConf = function (cnf) {
}
}
module.exports.initialize = function (options) {
function initialize (options) {
log.debug('Initializing mermaidAPI')
// Update default config with options supplied at initialization
if (typeof options === 'object') {
@ -525,14 +524,17 @@ module.exports.initialize = function (options) {
}
Logger.setLogLevel(config.logLevel)
}
module.exports.getConfig = function () {
function getConfig () {
return config
}
global.mermaidAPI = {
render: module.exports.render,
parse: module.exports.parse,
initialize: module.exports.initialize,
const mermaidAPI = {
render: render2,
parse,
initialize,
detectType: utils.detectType,
getConfig: module.exports.getConfig
getConfig
}
export default mermaidAPI

View File

@ -1,11 +1,5 @@
/* eslint-env jasmine */
/**
* Created by knut on 14-11-26.
*/
/**
* Created by knut on 14-11-23.
*/
var api = require('./mermaidAPI.js')
import mermaidAPI from './mermaidAPI'
describe('when using mermaidAPI and ', function () {
describe('doing initialize ', function () {
@ -15,16 +9,16 @@ describe('when using mermaidAPI and ', function () {
})
it('should copy a literal into the configuration', function () {
var orgConfig = api.getConfig()
var orgConfig = mermaidAPI.getConfig()
expect(orgConfig.testLiteral).toBe(undefined)
api.initialize({ 'testLiteral': true })
var config = api.getConfig()
mermaidAPI.initialize({ 'testLiteral': true })
var config = mermaidAPI.getConfig()
expect(config.testLiteral).toBe(true)
})
it('should copy a an object into the configuration', function () {
var orgConfig = api.getConfig()
var orgConfig = mermaidAPI.getConfig()
expect(orgConfig.testObject).toBe(undefined)
var object = {
@ -32,9 +26,9 @@ describe('when using mermaidAPI and ', function () {
test2: false
}
api.initialize({ 'testObject': object })
api.initialize({ 'testObject': { 'test3': true } })
var config = api.getConfig()
mermaidAPI.initialize({ 'testObject': object })
mermaidAPI.initialize({ 'testObject': { 'test3': true } })
var config = mermaidAPI.getConfig()
expect(config.testObject.test1).toBe(1)
expect(config.testObject.test2).toBe(false)
@ -44,10 +38,10 @@ describe('when using mermaidAPI and ', function () {
})
describe('checking validity of input ', function () {
it('it should throw for an invalid definiton', function () {
expect(() => global.mermaidAPI.parse('this is not a mermaid diagram definition')).toThrow()
expect(() => mermaidAPI.parse('this is not a mermaid diagram definition')).toThrow()
})
it('it should not throw for a valid definiton', function () {
expect(() => global.mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).not.toThrow()
expect(() => mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).not.toThrow()
})
})
})

View File

@ -16,5 +16,6 @@
- Replace require with import
- Replace module.exports with export default
- Replace karma-webpack with karma-babel
- Webpack generate mermaid.js & mermaid.core.js
- Stops working for mermaid-live-editor since 7.0.9
- global.mermaid_config
- rewrite logger