2018-03-09 13:33:35 +08:00
|
|
|
import * as d3 from 'd3'
|
2015-10-17 10:39:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @function detectType
|
|
|
|
* Detects the type of the graph text.
|
|
|
|
* ```mermaid
|
|
|
|
* graph LR
|
|
|
|
* a-->b
|
|
|
|
* b-->c
|
|
|
|
* c-->d
|
|
|
|
* d-->e
|
|
|
|
* e-->f
|
|
|
|
* f-->g
|
|
|
|
* g-->h
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {string} text The text defining the graph
|
|
|
|
* @returns {string} A graph definition key
|
|
|
|
*/
|
2017-09-09 14:46:58 +08:00
|
|
|
export const detectType = function (text) {
|
2017-04-11 22:14:25 +08:00
|
|
|
text = text.replace(/^\s*%%.*\n/g, '\n')
|
|
|
|
if (text.match(/^\s*sequenceDiagram/)) {
|
2018-03-12 21:16:22 +08:00
|
|
|
return 'sequence'
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2015-10-17 10:39:20 +02:00
|
|
|
|
2017-04-11 22:14:25 +08:00
|
|
|
if (text.match(/^\s*gantt/)) {
|
|
|
|
return 'gantt'
|
|
|
|
}
|
2015-10-17 10:39:20 +02:00
|
|
|
|
2017-04-11 22:14:25 +08:00
|
|
|
if (text.match(/^\s*classDiagram/)) {
|
2018-03-12 21:16:22 +08:00
|
|
|
return 'class'
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2015-10-30 10:47:25 +01:00
|
|
|
|
2017-04-11 22:14:25 +08:00
|
|
|
if (text.match(/^\s*gitGraph/)) {
|
2018-03-12 21:16:22 +08:00
|
|
|
return 'git'
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2018-03-12 21:06:49 +08:00
|
|
|
return 'flowchart'
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2015-10-17 10:39:20 +02:00
|
|
|
|
2017-01-07 12:53:49 -05:00
|
|
|
/**
|
|
|
|
* @function isSubstringInArray
|
|
|
|
* Detects whether a substring in present in a given array
|
|
|
|
* @param {string} str The substring to detect
|
|
|
|
* @param {array} arr The array to search
|
|
|
|
* @returns {number} the array index containing the substring or -1 if not present
|
|
|
|
**/
|
2017-09-09 14:46:58 +08:00
|
|
|
export const isSubstringInArray = function (str, arr) {
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
2017-04-11 22:14:25 +08:00
|
|
|
if (arr[i].match(str)) return i
|
2017-01-07 12:53:49 -05:00
|
|
|
}
|
2017-04-11 22:14:25 +08:00
|
|
|
return -1
|
|
|
|
}
|
2017-09-10 19:41:34 +08:00
|
|
|
|
2018-03-09 13:33:35 +08:00
|
|
|
const interpolates = {
|
|
|
|
basis: d3.curveBasis,
|
|
|
|
linear: d3.curveLinear,
|
|
|
|
cardinal: d3.curveCardinal
|
|
|
|
}
|
|
|
|
export const interpolateToCurve = (interpolate, defaultCurve) => {
|
|
|
|
return interpolates[interpolate] || defaultCurve
|
|
|
|
}
|
|
|
|
|
2017-09-10 19:41:34 +08:00
|
|
|
export default {
|
|
|
|
detectType,
|
2018-03-09 13:33:35 +08:00
|
|
|
isSubstringInArray,
|
|
|
|
interpolateToCurve
|
2017-09-10 19:41:34 +08:00
|
|
|
}
|