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 * Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid functionality and to render
* the diagrams to svg code. * the diagrams to svg code.
*/ */
var he = require('he') import he from 'he'
import mermaidAPI from './mermaidAPI'
var Logger = require('./logger') var Logger = require('./logger')
var log = Logger.Log var log = Logger.Log
var mermaidAPI = require('./mermaidAPI')
var nextId = 0 var nextId = 0
/** /**

View File

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

View File

@ -1,11 +1,5 @@
/* eslint-env jasmine */ /* eslint-env jasmine */
/** import mermaidAPI from './mermaidAPI'
* Created by knut on 14-11-26.
*/
/**
* Created by knut on 14-11-23.
*/
var api = require('./mermaidAPI.js')
describe('when using mermaidAPI and ', function () { describe('when using mermaidAPI and ', function () {
describe('doing initialize ', function () { describe('doing initialize ', function () {
@ -15,16 +9,16 @@ describe('when using mermaidAPI and ', function () {
}) })
it('should copy a literal into the configuration', function () { it('should copy a literal into the configuration', function () {
var orgConfig = api.getConfig() var orgConfig = mermaidAPI.getConfig()
expect(orgConfig.testLiteral).toBe(undefined) expect(orgConfig.testLiteral).toBe(undefined)
api.initialize({ 'testLiteral': true }) mermaidAPI.initialize({ 'testLiteral': true })
var config = api.getConfig() var config = mermaidAPI.getConfig()
expect(config.testLiteral).toBe(true) expect(config.testLiteral).toBe(true)
}) })
it('should copy a an object into the configuration', function () { it('should copy a an object into the configuration', function () {
var orgConfig = api.getConfig() var orgConfig = mermaidAPI.getConfig()
expect(orgConfig.testObject).toBe(undefined) expect(orgConfig.testObject).toBe(undefined)
var object = { var object = {
@ -32,9 +26,9 @@ describe('when using mermaidAPI and ', function () {
test2: false test2: false
} }
api.initialize({ 'testObject': object }) mermaidAPI.initialize({ 'testObject': object })
api.initialize({ 'testObject': { 'test3': true } }) mermaidAPI.initialize({ 'testObject': { 'test3': true } })
var config = api.getConfig() var config = mermaidAPI.getConfig()
expect(config.testObject.test1).toBe(1) expect(config.testObject.test1).toBe(1)
expect(config.testObject.test2).toBe(false) expect(config.testObject.test2).toBe(false)
@ -44,10 +38,10 @@ describe('when using mermaidAPI and ', function () {
}) })
describe('checking validity of input ', function () { describe('checking validity of input ', function () {
it('it should throw for an invalid definiton', 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 () { 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 require with import
- Replace module.exports with export default - Replace module.exports with export default
- Replace karma-webpack with karma-babel - Replace karma-webpack with karma-babel
- Webpack generate mermaid.js & mermaid.core.js
- Stops working for mermaid-live-editor since 7.0.9 - Stops working for mermaid-live-editor since 7.0.9
- global.mermaid_config
- rewrite logger