2020-05-27 19:38:30 +02:00
|
|
|
import { select } from 'd3';
|
2021-02-06 15:56:05 +05:30
|
|
|
import { log } from '../logger'; // eslint-disable-line
|
2020-05-27 19:38:30 +02:00
|
|
|
// let vertexNode;
|
|
|
|
// if (getConfig().flowchart.htmlLabels) {
|
|
|
|
// // TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?
|
|
|
|
// const node = {
|
|
|
|
// label: vertexText.replace(/fa[lrsb]?:fa-[\w-]+/g, s => `<i class='${s.replace(':', ' ')}'></i>`)
|
|
|
|
// };
|
|
|
|
// vertexNode = addHtmlLabel(svg, node).node();
|
|
|
|
// vertexNode.parentNode.removeChild(vertexNode);
|
|
|
|
// } else {
|
|
|
|
// const svgLabel = document.createElementNS('http://www.w3.org/2000/svg', 'text');
|
|
|
|
// svgLabel.setAttribute('style', styles.labelStyle.replace('color:', 'fill:'));
|
|
|
|
|
|
|
|
// 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');
|
|
|
|
// tspan.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve');
|
|
|
|
// tspan.setAttribute('dy', '1em');
|
|
|
|
// tspan.setAttribute('x', '1');
|
|
|
|
// tspan.textContent = rows[j];
|
|
|
|
// svgLabel.appendChild(tspan);
|
|
|
|
// }
|
|
|
|
// vertexNode = svgLabel;
|
|
|
|
// }
|
|
|
|
import { getConfig } from '../config';
|
|
|
|
|
|
|
|
function applyStyle(dom, styleFn) {
|
|
|
|
if (styleFn) {
|
|
|
|
dom.attr('style', styleFn);
|
2020-04-02 19:35:12 +02:00
|
|
|
}
|
2020-05-27 19:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function addHtmlLabel(node) {
|
|
|
|
// var fo = root.append('foreignObject').attr('width', '100000');
|
|
|
|
|
|
|
|
// var div = fo.append('xhtml:div');
|
|
|
|
// div.attr('xmlns', 'http://www.w3.org/1999/xhtml');
|
|
|
|
|
|
|
|
// var label = node.label;
|
|
|
|
// switch (typeof label) {
|
|
|
|
// case 'function':
|
|
|
|
// div.insert(label);
|
|
|
|
// break;
|
|
|
|
// case 'object':
|
|
|
|
// // Currently we assume this is a DOM object.
|
|
|
|
// div.insert(function() {
|
|
|
|
// return label;
|
|
|
|
// });
|
|
|
|
// break;
|
|
|
|
// default:
|
|
|
|
// div.html(label);
|
|
|
|
// }
|
2020-03-08 09:49:41 +01:00
|
|
|
|
2020-05-27 19:38:30 +02:00
|
|
|
// applyStyle(div, node.labelStyle);
|
|
|
|
// div.style('display', 'inline-block');
|
|
|
|
// // Fix for firefox
|
|
|
|
// div.style('white-space', 'nowrap');
|
|
|
|
|
|
|
|
// var client = div.node().getBoundingClientRect();
|
|
|
|
// fo.attr('width', client.width).attr('height', client.height);
|
|
|
|
const fo = select(document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject'));
|
|
|
|
const div = fo.append('xhtml:div');
|
|
|
|
|
|
|
|
const label = node.label;
|
2020-05-27 20:41:59 +02:00
|
|
|
const labelClass = node.isNode ? 'nodeLabel' : 'edgeLabel';
|
2021-01-28 20:51:11 +01:00
|
|
|
div.html(
|
|
|
|
'<span class="' +
|
|
|
|
labelClass +
|
|
|
|
'" ' +
|
|
|
|
(node.labelStyle ? 'style="' + node.labelStyle + '"' : '') +
|
|
|
|
'>' +
|
|
|
|
label +
|
|
|
|
'</span>'
|
|
|
|
);
|
2020-05-27 19:38:30 +02:00
|
|
|
|
|
|
|
applyStyle(div, node.labelStyle);
|
|
|
|
div.style('display', 'inline-block');
|
|
|
|
// Fix for firefox
|
|
|
|
div.style('white-space', 'nowrap');
|
|
|
|
div.attr('xmlns', 'http://www.w3.org/1999/xhtml');
|
|
|
|
return fo.node();
|
|
|
|
}
|
|
|
|
|
2020-05-27 20:41:59 +02:00
|
|
|
const createLabel = (_vertexText, style, isTitle, isNode) => {
|
|
|
|
let vertexText = _vertexText || '';
|
2021-04-30 19:25:45 +02:00
|
|
|
if (typeof vertexText === 'object') vertexText = vertexText[0];
|
2020-05-27 19:38:30 +02:00
|
|
|
if (getConfig().flowchart.htmlLabels) {
|
|
|
|
// TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?
|
2020-05-27 20:41:59 +02:00
|
|
|
vertexText = vertexText.replace(/\\n|\n/g, '<br />');
|
2021-02-06 15:56:05 +05:30
|
|
|
log.info('vertexText' + vertexText);
|
2020-05-27 19:38:30 +02:00
|
|
|
const node = {
|
2020-05-27 20:41:59 +02:00
|
|
|
isNode,
|
2020-05-27 19:38:30 +02:00
|
|
|
label: vertexText.replace(
|
|
|
|
/fa[lrsb]?:fa-[\w-]+/g,
|
|
|
|
s => `<i class='${s.replace(':', ' ')}'></i>`
|
2020-10-15 17:37:16 +02:00
|
|
|
),
|
|
|
|
labelStyle: style.replace('fill:', 'color:')
|
2020-05-27 19:38:30 +02:00
|
|
|
};
|
|
|
|
let vertexNode = addHtmlLabel(node);
|
|
|
|
// vertexNode.parentNode.removeChild(vertexNode);
|
|
|
|
return vertexNode;
|
|
|
|
} else {
|
|
|
|
const svgLabel = document.createElementNS('http://www.w3.org/2000/svg', 'text');
|
|
|
|
svgLabel.setAttribute('style', style.replace('color:', 'fill:'));
|
|
|
|
let rows = [];
|
|
|
|
if (typeof vertexText === 'string') {
|
|
|
|
rows = vertexText.split(/\\n|\n|<br\s*\/?>/gi);
|
|
|
|
} else if (Array.isArray(vertexText)) {
|
|
|
|
rows = vertexText;
|
2020-04-25 17:01:20 +02:00
|
|
|
} else {
|
2020-05-27 19:38:30 +02:00
|
|
|
rows = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let j = 0; j < rows.length; j++) {
|
|
|
|
const tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
|
|
|
|
tspan.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve');
|
|
|
|
tspan.setAttribute('dy', '1em');
|
|
|
|
tspan.setAttribute('x', '0');
|
|
|
|
if (isTitle) {
|
|
|
|
tspan.setAttribute('class', 'title-row');
|
|
|
|
} else {
|
|
|
|
tspan.setAttribute('class', 'row');
|
|
|
|
}
|
|
|
|
tspan.textContent = rows[j].trim();
|
|
|
|
svgLabel.appendChild(tspan);
|
2020-04-25 17:01:20 +02:00
|
|
|
}
|
2020-05-27 19:38:30 +02:00
|
|
|
return svgLabel;
|
2020-03-08 09:49:41 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default createLabel;
|