Extracted out the commonly used line break regex

This commit extract the commonly used /br\s*\/?>/gi regex to common.js,
in order to keep the code more DRY.
This commit is contained in:
Danny Shemesh 2020-04-23 07:17:09 +03:00
parent 10fdc45dea
commit 22dd50a047
7 changed files with 22 additions and 13 deletions

View File

@ -25,8 +25,10 @@ export const sanitizeText = (text, config) => {
return txt;
};
export const lineBreakRegex = /<br\s*\/?>/gi;
const breakToPlaceholder = s => {
return s.replace(/<br\s*\/?>/gi, '#br#');
return s.replace(lineBreakRegex, '#br#');
};
const placeholderToBreak = s => {
@ -35,5 +37,6 @@ const placeholderToBreak = s => {
export default {
getRows,
sanitizeText
sanitizeText,
lineBreakRegex
};

View File

@ -8,6 +8,7 @@ import { getConfig } from '../../config';
import { render } from '../../dagre-wrapper/index.js';
import addHtmlLabel from 'dagre-d3/lib/label/add-html-label.js';
import { logger } from '../../logger';
import common from '../common/common';
import { interpolateToCurve, getStylesFromArray } from '../../utils';
const conf = {};
@ -61,7 +62,7 @@ export const addVertices = function(vert, g, svgId) {
const svgLabel = document.createElementNS('http://www.w3.org/2000/svg', 'text');
svgLabel.setAttribute('style', styles.labelStyle.replace('color:', 'fill:'));
const rows = vertexText.split(/<br\s*\/?>/gi);
const rows = vertexText.split(common.lineBreakRegex);
for (let j = 0; j < rows.length; j++) {
const tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
@ -243,7 +244,7 @@ export const addEdges = function(edges, g) {
edgeData.label = '<span class="edgeLabel">' + edge.text + '</span>';
} else {
edgeData.labelType = 'text';
edgeData.label = edge.text.replace(/<br\s*\/?>/gi, '\n');
edgeData.label = edge.text.replace(common.lineBreakRegex, '\n');
if (typeof edge.style === 'undefined') {
edgeData.style = edgeData.style || 'stroke: #333; stroke-width: 1.5px;fill:none';

View File

@ -8,6 +8,7 @@ import { getConfig } from '../../config';
import dagreD3 from 'dagre-d3';
import addHtmlLabel from 'dagre-d3/lib/label/add-html-label.js';
import { logger } from '../../logger';
import common from '../common/common';
import { interpolateToCurve, getStylesFromArray } from '../../utils';
import flowChartShapes from './flowChartShapes';
@ -62,7 +63,7 @@ export const addVertices = function(vert, g, svgId) {
const svgLabel = document.createElementNS('http://www.w3.org/2000/svg', 'text');
svgLabel.setAttribute('style', styles.labelStyle.replace('color:', 'fill:'));
const rows = vertexText.split(/<br\s*\/?>/gi);
const rows = vertexText.split(common.lineBreakRegex);
for (let j = 0; j < rows.length; j++) {
const tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
@ -225,7 +226,7 @@ export const addEdges = function(edges, g) {
edgeData.label = '<span class="edgeLabel">' + edge.text + '</span>';
} else {
edgeData.labelType = 'text';
edgeData.label = edge.text.replace(/<br\s*\/?>/gi, '\n');
edgeData.label = edge.text.replace(common.lineBreakRegex, '\n');
if (typeof edge.style === 'undefined') {
edgeData.style = edgeData.style || 'stroke: #333; stroke-width: 1.5px;fill:none';

View File

@ -1,6 +1,7 @@
import * as d3 from 'd3';
import { parser } from './parser/gantt';
import common from '../common/common';
import ganttDb from './ganttDb';
parser.yy = ganttDb;
@ -358,7 +359,7 @@ export const draw = function(text, id) {
.data(numOccurances)
.enter()
.append(function(d) {
const rows = d[0].split(/<br\s*\/?>/gi);
const rows = d[0].split(common.lineBreakRegex);
const dy = -(rows.length - 1) / 2;
const svgLabel = document.createElementNS('http://www.w3.org/2000/svg', 'text');

View File

@ -3,6 +3,7 @@ import * as d3 from 'd3';
import svgDraw from './svgDraw';
import { logger } from '../../logger';
import { parser } from './parser/sequenceDiagram';
import common from '../common/common';
import sequenceDb from './sequenceDb';
parser.yy = sequenceDb;
@ -170,7 +171,7 @@ export const bounds = {
const _drawLongText = (text, x, y, g, width) => {
let textHeight = 0;
const lines = text.split(/<br\s*\/?>/gi);
const lines = text.split(common.lineBreakRegex);
for (const line of lines) {
const textObj = svgDraw.getTextObj();
textObj.x = x;
@ -235,7 +236,7 @@ const drawMessage = function(elem, startx, stopx, verticalPos, msg, sequenceInde
let textElems = [];
let counterBreaklines = 0;
let breaklineOffset = 17;
const breaklines = msg.message.split(/<br\s*\/?>/gi);
const breaklines = msg.message.split(common.lineBreakRegex);
for (const breakline of breaklines) {
textElems.push(
g

View File

@ -1,3 +1,5 @@
import common from '../common/common';
export const drawRect = function(elem, rectData) {
const rectElem = elem.append('rect');
rectElem.attr('x', rectData.x);
@ -18,7 +20,7 @@ export const drawRect = function(elem, rectData) {
export const drawText = function(elem, textData) {
// Remove and ignore br:s
const nText = textData.text.replace(/<br\s*\/?>/gi, ' ');
const nText = textData.text.replace(common.lineBreakRegex, ' ');
const textElem = elem.append('text');
textElem.attr('x', textData.x);
@ -321,7 +323,7 @@ const _drawTextCandidateFunc = (function() {
function byTspan(content, g, x, y, width, height, textAttrs, conf) {
const { actorFontSize, actorFontFamily } = conf;
const lines = content.split(/<br\s*\/?>/gi);
const lines = content.split(common.lineBreakRegex);
for (let i = 0; i < lines.length; i++) {
const dy = i * actorFontSize - (actorFontSize * (lines.length - 1)) / 2;
const text = g

View File

@ -282,7 +282,7 @@ const drawForkJoinState = (g, stateDef) => {
export const drawText = function(elem, textData) {
// Remove and ignore br:s
const nText = textData.text.replace(/<br\s*\/?>/gi, ' ');
const nText = textData.text.replace(common.lineBreakRegex, ' ');
const textElem = elem.append('text');
textElem.attr('x', textData.x);
@ -310,7 +310,7 @@ const _drawLongText = (_text, x, y, g) => {
let text = _text.replace(/\r\n/g, '<br/>');
text = text.replace(/\n/g, '<br/>');
const lines = text.split(/<br\s*\/?>/gi);
const lines = text.split(common.lineBreakRegex);
let tHeight = 1.25 * getConfig().state.noteMargin;
for (const line of lines) {