Start to refactor mermaid.js

This commit is contained in:
Tyler Long 2017-09-09 16:52:09 +08:00
parent c071503f19
commit 626fdfe345
3 changed files with 54 additions and 11 deletions

1
.ackrc
View File

@ -1,3 +1,2 @@
--ignore-dir=dist
--ignore-file=match:/^yarn\.lock$/
--ignore-dir=test

45
src/logger.new.js Normal file
View File

@ -0,0 +1,45 @@
import moment from 'moment'
import chalk from 'chalk'
export const Log = {
debug: () => {},
info: () => {},
warn: () => {},
error: () => {},
fatal: () => {}
}
export const level = {
debug: 1,
info: 2,
warn: 3,
error: 4,
fatal: 5
}
export const setLogLevel = function (level) {
Log.fatal = () => {}
Log.error = () => {}
Log.warn = () => {}
Log.info = () => {}
Log.debug = () => {}
if (level <= level.fatal) {
Log.fatal = (message) => chalk.bgHex('#DDDDDD').red(format('FATAL', message))
}
if (level <= level.error) {
Log.error = (message) => chalk.bgHex('#DDDDDD').red(format('ERROR', message))
}
if (level <= level.warn) {
Log.warn = (message) => chalk.bgHex('#DDDDDD').orange(format('WARN', message))
}
if (level <= level.info) {
Log.info = (message) => chalk.bgHex('#DDDDDD').blue(format('INFO', message))
}
if (level <= level.debug) {
Log.debug = (message) => chalk.bgHex('#DDDDDD').green(format('DEBUG', message))
}
}
const format = (level, message) => {
const time = moment().format('HH:mm:ss (SSS)')
return `${level} : ${time} : ${message}`
}

View File

@ -2,14 +2,13 @@
* 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')
var Logger = require('./logger')
var log = Logger.Log
var mermaidAPI = require('./mermaidAPI')
var nextId = 0
var he = require('he')
module.exports.mermaidAPI = mermaidAPI
/**
* ## init
@ -111,14 +110,14 @@ var init = function () {
}
}
exports.init = init
exports.parse = mermaidAPI.parse
module.exports.init = init
module.exports.parse = mermaidAPI.parse
/**
* ## version
* Function returning version information
* @returns {string} A string containing the version info
*/
exports.version = function () {
module.exports.version = function () {
return 'v' + require('../package.json').version
}
@ -127,7 +126,7 @@ exports.version = function () {
* This function overrides the default configuration.
* @param config
*/
exports.initialize = function (config) {
module.exports.initialize = function (config) {
log.debug('Initializing mermaid')
if (typeof config.mermaid !== 'undefined') {
if (typeof config.mermaid.startOnLoad !== 'undefined') {
@ -165,7 +164,7 @@ global.mermaid = {
init.apply(null, arguments)
},
initialize: function (config) {
exports.initialize(config)
module.exports.initialize(config)
},
version: function () {
return mermaidAPI.version()
@ -187,14 +186,14 @@ global.mermaid = {
* This function overrides the default configuration.
* @param config
*/
exports.parseError = global.mermaid.parseError
module.exports.parseError = global.mermaid.parseError
/**
* ##contentLoaded
* Callback function that is called when page is loaded. This functions fetches configuration for mermaid rendering and
* calls init for rendering the mermaid diagrams on the page.
*/
exports.contentLoaded = function () {
module.exports.contentLoaded = function () {
var config
// Check state of start config mermaid namespace
if (typeof global.mermaid_config !== 'undefined') {
@ -233,6 +232,6 @@ if (typeof document !== 'undefined') {
* Wait for document loaded before starting the execution
*/
window.addEventListener('load', function () {
exports.contentLoaded()
module.exports.contentLoaded()
}, false)
}