Refactor utils.js

This commit is contained in:
Tyler Long
2017-09-09 14:46:58 +08:00
parent e0f5f10215
commit 873f7a591b
2 changed files with 21 additions and 30 deletions

View File

@@ -1,8 +1,4 @@
/** import { Log } from './logger'
* Created by knut on 14-11-23.
*/
var Logger = require('./logger')
var log = Logger.Log
/** /**
* @function detectType * @function detectType
@@ -21,7 +17,7 @@ var log = Logger.Log
* @param {string} text The text defining the graph * @param {string} text The text defining the graph
* @returns {string} A graph definition key * @returns {string} A graph definition key
*/ */
var detectType = function (text) { export const detectType = function (text) {
text = text.replace(/^\s*%%.*\n/g, '\n') text = text.replace(/^\s*%%.*\n/g, '\n')
if (text.match(/^\s*sequenceDiagram/)) { if (text.match(/^\s*sequenceDiagram/)) {
return 'sequenceDiagram' return 'sequenceDiagram'
@@ -40,17 +36,16 @@ var detectType = function (text) {
} }
if (text.match(/^\s*classDiagram/)) { if (text.match(/^\s*classDiagram/)) {
log.debug('Detected classDiagram syntax') Log.debug('Detected classDiagram syntax')
return 'classDiagram' return 'classDiagram'
} }
if (text.match(/^\s*gitGraph/)) { if (text.match(/^\s*gitGraph/)) {
log.debug('Detected gitGraph syntax') Log.debug('Detected gitGraph syntax')
return 'gitGraph' return 'gitGraph'
} }
return 'graph' return 'graph'
} }
exports.detectType = detectType
/** /**
* Copies all relevant CSS content into the graph SVG. * Copies all relevant CSS content into the graph SVG.
@@ -58,21 +53,20 @@ exports.detectType = detectType
* @param {element} svg The root element of the SVG * @param {element} svg The root element of the SVG
* @param {object} Hash table of class definitions from the graph definition * @param {object} Hash table of class definitions from the graph definition
*/ */
var cloneCssStyles = function (svg, classes) { export const cloneCssStyles = function (svg, classes) {
var usedStyles = '' let usedStyles = ''
var sheets = document.styleSheets const sheets = document.styleSheets
var rule let rule
for (var i = 0; i < sheets.length; i++) { for (let i = 0; i < sheets.length; i++) {
// Avoid multiple inclusion on pages with multiple graphs // Avoid multiple inclusion on pages with multiple graphs
if (sheets[i].title !== 'mermaid-svg-internal-css') { if (sheets[i].title !== 'mermaid-svg-internal-css') {
try { try {
var rules = sheets[i].cssRules const rules = sheets[i].cssRules
if (rules !== null) { if (rules !== null) {
for (var j = 0; j < rules.length; j++) { for (let j = 0; j < rules.length; j++) {
rule = rules[j] rule = rules[j]
if (typeof (rule.style) !== 'undefined') { if (typeof (rule.style) !== 'undefined') {
var elems const elems = svg.querySelectorAll(rule.selectorText)
elems = svg.querySelectorAll(rule.selectorText)
if (elems.length > 0) { if (elems.length > 0) {
usedStyles += rule.selectorText + ' { ' + rule.style.cssText + '}\n' usedStyles += rule.selectorText + ' { ' + rule.style.cssText + '}\n'
} }
@@ -81,16 +75,15 @@ var cloneCssStyles = function (svg, classes) {
} }
} catch (err) { } catch (err) {
if (typeof (rule) !== 'undefined') { if (typeof (rule) !== 'undefined') {
log.warn('Invalid CSS selector "' + rule.selectorText + '"', err) Log.warn('Invalid CSS selector "' + rule.selectorText + '"', err)
} }
} }
} }
} }
var defaultStyles = '' let defaultStyles = ''
var embeddedStyles = '' let embeddedStyles = ''
for (const className in classes) {
for (var className in classes) {
if (classes.hasOwnProperty(className) && typeof (className) !== 'undefined') { if (classes.hasOwnProperty(className) && typeof (className) !== 'undefined') {
if (className === 'default') { if (className === 'default') {
if (classes.default.styles instanceof Array) { if (classes.default.styles instanceof Array) {
@@ -114,7 +107,7 @@ var cloneCssStyles = function (svg, classes) {
} }
if (usedStyles !== '' || defaultStyles !== '' || embeddedStyles !== '') { if (usedStyles !== '' || defaultStyles !== '' || embeddedStyles !== '') {
var s = document.createElement('style') const s = document.createElement('style')
s.setAttribute('type', 'text/css') s.setAttribute('type', 'text/css')
s.setAttribute('title', 'mermaid-svg-internal-css') s.setAttribute('title', 'mermaid-svg-internal-css')
s.innerHTML = '/* <![CDATA[ */\n' s.innerHTML = '/* <![CDATA[ */\n'
@@ -133,8 +126,6 @@ var cloneCssStyles = function (svg, classes) {
} }
} }
exports.cloneCssStyles = cloneCssStyles
/** /**
* @function isSubstringInArray * @function isSubstringInArray
* Detects whether a substring in present in a given array * Detects whether a substring in present in a given array
@@ -142,11 +133,9 @@ exports.cloneCssStyles = cloneCssStyles
* @param {array} arr The array to search * @param {array} arr The array to search
* @returns {number} the array index containing the substring or -1 if not present * @returns {number} the array index containing the substring or -1 if not present
**/ **/
var isSubstringInArray = function (str, arr) { export const isSubstringInArray = function (str, arr) {
for (var i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) {
if (arr[i].match(str)) return i if (arr[i].match(str)) return i
} }
return -1 return -1
} }
exports.isSubstringInArray = isSubstringInArray

View File

@@ -16,3 +16,5 @@
- 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