mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-17 06:20:07 +02:00
Update of prettier, eslint and rules
This commit is contained in:
100
src/utils.js
100
src/utils.js
@@ -11,11 +11,10 @@ import {
|
||||
curveStep,
|
||||
curveStepAfter,
|
||||
curveStepBefore,
|
||||
select
|
||||
select,
|
||||
} from 'd3';
|
||||
import common from './diagrams/common/common';
|
||||
import { log } from './logger';
|
||||
// import cryptoRandomString from 'crypto-random-string';
|
||||
|
||||
// Effectively an enum of the supported curve types, accessible by name
|
||||
const d3CurveTypes = {
|
||||
@@ -29,10 +28,12 @@ const d3CurveTypes = {
|
||||
curveNatural: curveNatural,
|
||||
curveStep: curveStep,
|
||||
curveStepAfter: curveStepAfter,
|
||||
curveStepBefore: curveStepBefore
|
||||
curveStepBefore: curveStepBefore,
|
||||
};
|
||||
const directive = /[%]{2}[{]\s*(?:(?:(\w+)\s*:|(\w+))\s*(?:(?:(\w+))|((?:(?![}][%]{2}).|\r?\n)*))?\s*)(?:[}][%]{2})?/gi;
|
||||
const directiveWithoutOpen = /\s*(?:(?:(\w+)(?=:):|(\w+))\s*(?:(?:(\w+))|((?:(?![}][%]{2}).|\r?\n)*))?\s*)(?:[}][%]{2})?/gi;
|
||||
const directive =
|
||||
/[%]{2}[{]\s*(?:(?:(\w+)\s*:|(\w+))\s*(?:(?:(\w+))|((?:(?![}][%]{2}).|\r?\n)*))?\s*)(?:[}][%]{2})?/gi;
|
||||
const directiveWithoutOpen =
|
||||
/\s*(?:(?:(\w+)(?=:):|(\w+))\s*(?:(?:(\w+))|((?:(?![}][%]{2}).|\r?\n)*))?\s*)(?:[}][%]{2})?/gi;
|
||||
const anyComment = /\s*%%.*\n/gm;
|
||||
|
||||
/**
|
||||
@@ -65,18 +66,18 @@ const anyComment = /\s*%%.*\n/gm;
|
||||
* @param {string} text The text defining the graph
|
||||
* @returns {object} the json object representing the init passed to mermaid.initialize()
|
||||
*/
|
||||
export const detectInit = function(text, cnf) {
|
||||
export const detectInit = function (text, cnf) {
|
||||
let inits = detectDirective(text, /(?:init\b)|(?:initialize\b)/);
|
||||
let results = {};
|
||||
if (Array.isArray(inits)) {
|
||||
let args = inits.map(init => init.args);
|
||||
let args = inits.map((init) => init.args);
|
||||
results = assignWithDepth(results, [...args]);
|
||||
} else {
|
||||
results = inits.args;
|
||||
}
|
||||
if (results) {
|
||||
let type = detectType(text, cnf);
|
||||
['config'].forEach(prop => {
|
||||
['config'].forEach((prop) => {
|
||||
if (typeof results[prop] !== 'undefined') {
|
||||
if (type === 'flowchart-v2') {
|
||||
type = 'flowchart';
|
||||
@@ -110,16 +111,13 @@ export const detectInit = function(text, cnf) {
|
||||
* @returns {object | Array} An object or Array representing the directive(s): { type: string, args: object|null } matched by the input type
|
||||
* if a single directive was found, that directive object will be returned.
|
||||
*/
|
||||
export const detectDirective = function(text, type = null) {
|
||||
export const detectDirective = function (text, type = null) {
|
||||
try {
|
||||
const commentWithoutDirectives = new RegExp(
|
||||
`[%]{2}(?![{]${directiveWithoutOpen.source})(?=[}][%]{2}).*\n`,
|
||||
'ig'
|
||||
);
|
||||
text = text
|
||||
.trim()
|
||||
.replace(commentWithoutDirectives, '')
|
||||
.replace(/'/gm, '"');
|
||||
text = text.trim().replace(commentWithoutDirectives, '').replace(/'/gm, '"');
|
||||
log.debug(
|
||||
`Detecting diagram directive${type !== null ? ' type:' + type : ''} based on the text:${text}`
|
||||
);
|
||||
@@ -173,7 +171,7 @@ export const detectDirective = function(text, type = null) {
|
||||
* @param {string} text The text defining the graph
|
||||
* @returns {string} A graph definition key
|
||||
*/
|
||||
export const detectType = function(text, cnf) {
|
||||
export const detectType = function (text, cnf) {
|
||||
text = text.replace(directive, '').replace(anyComment, '\n');
|
||||
log.debug('Detecting diagram type based on the text ' + text);
|
||||
if (text.match(/^\s*sequenceDiagram/)) {
|
||||
@@ -252,7 +250,7 @@ const memoize = (fn, resolver) => {
|
||||
* @param {array} arr The array to search
|
||||
* @returns {number} the array index containing the substring or -1 if not present
|
||||
**/
|
||||
export const isSubstringInArray = function(str, arr) {
|
||||
export const isSubstringInArray = function (str, arr) {
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if (arr[i].match(str)) return i;
|
||||
}
|
||||
@@ -297,11 +295,11 @@ export const runFunc = (functionName, ...params) => {
|
||||
const distance = (p1, p2) =>
|
||||
p1 && p2 ? Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2)) : 0;
|
||||
|
||||
const traverseEdge = points => {
|
||||
const traverseEdge = (points) => {
|
||||
let prevPoint;
|
||||
let totalDistance = 0;
|
||||
|
||||
points.forEach(point => {
|
||||
points.forEach((point) => {
|
||||
totalDistance += distance(point, prevPoint);
|
||||
prevPoint = point;
|
||||
});
|
||||
@@ -310,7 +308,7 @@ const traverseEdge = points => {
|
||||
let remainingDistance = totalDistance / 2;
|
||||
let center = undefined;
|
||||
prevPoint = undefined;
|
||||
points.forEach(point => {
|
||||
points.forEach((point) => {
|
||||
if (prevPoint && !center) {
|
||||
const vectorDistance = distance(point, prevPoint);
|
||||
if (vectorDistance < remainingDistance) {
|
||||
@@ -324,7 +322,7 @@ const traverseEdge = points => {
|
||||
if (distanceRatio > 0 && distanceRatio < 1) {
|
||||
center = {
|
||||
x: (1 - distanceRatio) * prevPoint.x + distanceRatio * point.x,
|
||||
y: (1 - distanceRatio) * prevPoint.y + distanceRatio * point.y
|
||||
y: (1 - distanceRatio) * prevPoint.y + distanceRatio * point.y,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -334,7 +332,7 @@ const traverseEdge = points => {
|
||||
return center;
|
||||
};
|
||||
|
||||
const calcLabelPosition = points => {
|
||||
const calcLabelPosition = (points) => {
|
||||
return traverseEdge(points);
|
||||
};
|
||||
|
||||
@@ -345,7 +343,7 @@ const calcCardinalityPosition = (isRelationTypePresent, points, initialPosition)
|
||||
if (points[0] !== initialPosition) {
|
||||
points = points.reverse();
|
||||
}
|
||||
points.forEach(point => {
|
||||
points.forEach((point) => {
|
||||
totalDistance += distance(point, prevPoint);
|
||||
prevPoint = point;
|
||||
});
|
||||
@@ -356,7 +354,7 @@ const calcCardinalityPosition = (isRelationTypePresent, points, initialPosition)
|
||||
let remainingDistance = distanceToCardinalityPoint;
|
||||
let center;
|
||||
prevPoint = undefined;
|
||||
points.forEach(point => {
|
||||
points.forEach((point) => {
|
||||
if (prevPoint && !center) {
|
||||
const vectorDistance = distance(point, prevPoint);
|
||||
if (vectorDistance < remainingDistance) {
|
||||
@@ -370,7 +368,7 @@ const calcCardinalityPosition = (isRelationTypePresent, points, initialPosition)
|
||||
if (distanceRatio > 0 && distanceRatio < 1) {
|
||||
center = {
|
||||
x: (1 - distanceRatio) * prevPoint.x + distanceRatio * point.x,
|
||||
y: (1 - distanceRatio) * prevPoint.y + distanceRatio * point.y
|
||||
y: (1 - distanceRatio) * prevPoint.y + distanceRatio * point.y,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -401,7 +399,7 @@ const calcTerminalLabelPosition = (terminalMarkerSize, position, _points) => {
|
||||
points = points.reverse();
|
||||
}
|
||||
|
||||
points.forEach(point => {
|
||||
points.forEach((point) => {
|
||||
totalDistance += distance(point, prevPoint);
|
||||
prevPoint = point;
|
||||
});
|
||||
@@ -412,7 +410,7 @@ const calcTerminalLabelPosition = (terminalMarkerSize, position, _points) => {
|
||||
let remainingDistance = distanceToCardinalityPoint;
|
||||
let center;
|
||||
prevPoint = undefined;
|
||||
points.forEach(point => {
|
||||
points.forEach((point) => {
|
||||
if (prevPoint && !center) {
|
||||
const vectorDistance = distance(point, prevPoint);
|
||||
if (vectorDistance < remainingDistance) {
|
||||
@@ -426,7 +424,7 @@ const calcTerminalLabelPosition = (terminalMarkerSize, position, _points) => {
|
||||
if (distanceRatio > 0 && distanceRatio < 1) {
|
||||
center = {
|
||||
x: (1 - distanceRatio) * prevPoint.x + distanceRatio * point.x,
|
||||
y: (1 - distanceRatio) * prevPoint.y + distanceRatio * point.y
|
||||
y: (1 - distanceRatio) * prevPoint.y + distanceRatio * point.y,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -459,7 +457,7 @@ const calcTerminalLabelPosition = (terminalMarkerSize, position, _points) => {
|
||||
return cardinalityPosition;
|
||||
};
|
||||
|
||||
export const getStylesFromArray = arr => {
|
||||
export const getStylesFromArray = (arr) => {
|
||||
let style = '';
|
||||
let labelStyle = '';
|
||||
|
||||
@@ -480,14 +478,7 @@ export const getStylesFromArray = arr => {
|
||||
let cnt = 0;
|
||||
export const generateId = () => {
|
||||
cnt++;
|
||||
return (
|
||||
'id-' +
|
||||
Math.random()
|
||||
.toString(36)
|
||||
.substr(2, 12) +
|
||||
'-' +
|
||||
cnt
|
||||
);
|
||||
return 'id-' + Math.random().toString(36).substr(2, 12) + '-' + cnt;
|
||||
};
|
||||
|
||||
function makeid(length) {
|
||||
@@ -500,7 +491,7 @@ function makeid(length) {
|
||||
return result;
|
||||
}
|
||||
|
||||
export const random = options => {
|
||||
export const random = (options) => {
|
||||
return makeid(options.length);
|
||||
};
|
||||
|
||||
@@ -530,13 +521,13 @@ export const random = options => {
|
||||
* clobber: should dissimilar types clobber (default: { depth: 2, clobber: false })
|
||||
* @returns {*}
|
||||
*/
|
||||
export const assignWithDepth = function(dst, src, config) {
|
||||
export const assignWithDepth = function (dst, src, config) {
|
||||
const { depth, clobber } = Object.assign({ depth: 2, clobber: false }, config);
|
||||
if (Array.isArray(src) && !Array.isArray(dst)) {
|
||||
src.forEach(s => assignWithDepth(dst, s, config));
|
||||
src.forEach((s) => assignWithDepth(dst, s, config));
|
||||
return dst;
|
||||
} else if (Array.isArray(src) && Array.isArray(dst)) {
|
||||
src.forEach(s => {
|
||||
src.forEach((s) => {
|
||||
if (dst.indexOf(s) === -1) {
|
||||
dst.push(s);
|
||||
}
|
||||
@@ -551,7 +542,7 @@ export const assignWithDepth = function(dst, src, config) {
|
||||
}
|
||||
}
|
||||
if (typeof src !== 'undefined' && typeof dst === 'object' && typeof src === 'object') {
|
||||
Object.keys(src).forEach(key => {
|
||||
Object.keys(src).forEach((key) => {
|
||||
if (
|
||||
typeof src[key] === 'object' &&
|
||||
(dst[key] === undefined || typeof dst[key] === 'object')
|
||||
@@ -568,7 +559,7 @@ export const assignWithDepth = function(dst, src, config) {
|
||||
return dst;
|
||||
};
|
||||
|
||||
export const getTextObj = function() {
|
||||
export const getTextObj = function () {
|
||||
return {
|
||||
x: 0,
|
||||
y: 0,
|
||||
@@ -580,11 +571,11 @@ export const getTextObj = function() {
|
||||
textMargin: 0,
|
||||
rx: 0,
|
||||
ry: 0,
|
||||
valign: undefined
|
||||
valign: undefined,
|
||||
};
|
||||
};
|
||||
|
||||
export const drawSimpleText = function(elem, textData) {
|
||||
export const drawSimpleText = function (elem, textData) {
|
||||
// Remove and ignore br:s
|
||||
const nText = textData.text.replace(common.lineBreakRegex, ' ');
|
||||
|
||||
@@ -642,7 +633,7 @@ export const wrapLabel = memoize(
|
||||
completedLines.push(nextLine);
|
||||
}
|
||||
});
|
||||
return completedLines.filter(line => line !== '').join(config.joinWith);
|
||||
return completedLines.filter((line) => line !== '').join(config.joinWith);
|
||||
},
|
||||
(label, maxWidth, config) =>
|
||||
`${label}-${maxWidth}-${config.fontSize}-${config.fontWeight}-${config.fontFamily}-${config.joinWith}`
|
||||
@@ -687,7 +678,7 @@ const breakString = memoize(
|
||||
* @param text the text to measure
|
||||
* @param config - the config for fontSize, fontFamily, and fontWeight all impacting the resulting size
|
||||
*/
|
||||
export const calculateTextHeight = function(text, config) {
|
||||
export const calculateTextHeight = function (text, config) {
|
||||
config = Object.assign(
|
||||
{ fontSize: 12, fontWeight: 400, fontFamily: 'Arial', margin: 15 },
|
||||
config
|
||||
@@ -702,7 +693,7 @@ export const calculateTextHeight = function(text, config) {
|
||||
* @param text - The text to calculate the width of
|
||||
* @param config - the config for fontSize, fontFamily, and fontWeight all impacting the resulting size
|
||||
*/
|
||||
export const calculateTextWidth = function(text, config) {
|
||||
export const calculateTextWidth = function (text, config) {
|
||||
config = Object.assign({ fontSize: 12, fontWeight: 400, fontFamily: 'Arial' }, config);
|
||||
return calculateTextDimensions(text, config).width;
|
||||
};
|
||||
@@ -715,7 +706,7 @@ export const calculateTextWidth = function(text, config) {
|
||||
* @param config - the config for fontSize, fontFamily, fontWeight, and margin all impacting the resulting size
|
||||
*/
|
||||
export const calculateTextDimensions = memoize(
|
||||
function(text, config) {
|
||||
function (text, config) {
|
||||
config = Object.assign({ fontSize: 12, fontWeight: 400, fontFamily: 'Arial' }, config);
|
||||
const { fontSize, fontFamily, fontWeight } = config;
|
||||
if (!text) {
|
||||
@@ -774,13 +765,13 @@ export const calculateTextDimensions = memoize(
|
||||
(text, config) => `${text}-${config.fontSize}-${config.fontWeight}-${config.fontFamily}`
|
||||
);
|
||||
|
||||
const d3Attrs = function(d3Elem, attrs) {
|
||||
const d3Attrs = function (d3Elem, attrs) {
|
||||
for (let attr of attrs) {
|
||||
d3Elem.attr(attr[0], attr[1]);
|
||||
}
|
||||
};
|
||||
|
||||
export const calculateSvgSizeAttrs = function(height, width, useMaxWidth) {
|
||||
export const calculateSvgSizeAttrs = function (height, width, useMaxWidth) {
|
||||
let attrs = new Map();
|
||||
attrs.set('height', height);
|
||||
if (useMaxWidth) {
|
||||
@@ -792,7 +783,7 @@ export const calculateSvgSizeAttrs = function(height, width, useMaxWidth) {
|
||||
return attrs;
|
||||
};
|
||||
|
||||
export const configureSvgSize = function(svgElem, height, width, useMaxWidth) {
|
||||
export const configureSvgSize = function (svgElem, height, width, useMaxWidth) {
|
||||
const attrs = calculateSvgSizeAttrs(height, width, useMaxWidth);
|
||||
d3Attrs(svgElem, attrs);
|
||||
};
|
||||
@@ -814,13 +805,10 @@ export const initIdGeneratior = class iterator {
|
||||
|
||||
// Source https://github.com/shrpne/entity-decode/blob/master/browser.js
|
||||
let decoder;
|
||||
export const entityDecode = function(html) {
|
||||
export const entityDecode = function (html) {
|
||||
decoder = decoder || document.createElement('div');
|
||||
// Escape HTML before decoding for HTML Entities
|
||||
html = escape(html)
|
||||
.replace(/%26/g, '&')
|
||||
.replace(/%23/g, '#')
|
||||
.replace(/%3B/g, ';');
|
||||
html = escape(html).replace(/%26/g, '&').replace(/%23/g, '#').replace(/%3B/g, ';');
|
||||
// decoding
|
||||
decoder.innerHTML = html;
|
||||
return unescape(decoder.textContent);
|
||||
@@ -849,5 +837,5 @@ export default {
|
||||
memoize,
|
||||
runFunc,
|
||||
entityDecode,
|
||||
initIdGeneratior
|
||||
initIdGeneratior,
|
||||
};
|
||||
|
Reference in New Issue
Block a user