v-regexp
Version:
JavaScript Regular Expression Parser and Visualizer.
212 lines • 5.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.point = exports.smoothLine = exports.hline = exports.textLabel = exports.textRect = void 0;
var tslib_1 = require("tslib");
var Kit_1 = tslib_1.__importDefault(require("../Kit"));
var common_1 = require("./common");
function textRect(_a) {
var str = _a.str, x = _a.x, y = _a.y, bgColor = _a.bgColor, textColor = _a.textColor, theme = _a.theme;
str = Kit_1.default.toPrint(str);
var padding = 6;
var charSize = common_1.getCharSize({
fontSize: theme.nodeFontSize,
templateText: theme.global.templateText,
});
var tw = str.length * charSize.width;
var h = charSize.height + padding * 2;
var w = tw + padding * 2;
var rect = {
type: 'rect',
x: x,
y: y - h / 2,
width: w,
height: h,
stroke: 'none',
fill: bgColor || 'transparent',
};
var text = {
type: 'text',
x: x + w / 2,
y: y,
text: str,
'font-size': theme.nodeFontSize,
'font-family': theme.fontFamily,
fill: textColor || 'black',
};
return {
text: text,
rect: rect,
items: [rect, text],
width: w,
height: h,
x: x,
y: rect.y,
lineInX: x,
lineOutX: x + w,
};
}
exports.textRect = textRect;
function textLabel(_a) {
var x = _a.x, y = _a.y, str = _a.str, color = _a.color, theme = _a.theme;
var charSize = common_1.getCharSize({
fontSize: theme.labelFontSize,
templateText: theme.global.templateText,
});
var lines = str.split('\n');
var textHeight = lines.length * charSize.height;
var textWidth;
if (lines.length > 1) {
textWidth = Math.max.apply(Math, lines.map(function (a) { return a.length; }));
}
else {
textWidth = str.length;
}
textWidth *= charSize.width;
var margin = 4;
var text = {
type: 'text',
x: x,
y: y - textHeight / 2 - margin,
text: str,
'font-size': theme.labelFontSize,
'font-family': theme.fontFamily,
fill: color || '#444',
};
return {
label: text,
x: x - textWidth / 2,
y: y - textHeight - margin,
width: textWidth,
height: textHeight + margin,
};
}
exports.textLabel = textLabel;
// 可视化图形,横轴线
function hline(_a) {
var x = _a.x, y = _a.y, destX = _a.destX, theme = _a.theme;
return {
type: 'path',
x: x,
y: y,
path: ['M', x, y, 'H', destX],
'stroke-linecap': theme.line.strokeLinecap,
'stroke-linejoin': theme.line.strokeLinejoin,
stroke: theme.line.stroke,
'stroke-width': theme.line.strokeWidth,
_translate: function (x, y) {
var p = this.path;
p[1] += x;
p[2] += y;
p[4] += x;
},
};
}
exports.hline = hline;
function smoothLine(_a) {
var fromX = _a.fromX, fromY = _a.fromY, toX = _a.toX, toY = _a.toY, theme = _a.theme;
var radius = 10;
var path;
var translate;
var signX = fromX > toX ? -1 : 1;
var signY = fromY > toY ? -1 : 1;
/* || Math.abs(fromX-toX)<radius*2 */
if (Math.abs(fromY - toY) < radius * 1.5) {
path = [
'M',
fromX,
fromY,
'C',
fromX + Math.min(Math.abs(toX - fromX) / 2, radius) * signX,
fromY,
toX - (toX - fromX) / 2,
toY,
toX,
toY,
];
translate = function (x, y) {
var p = this.path;
p[1] += x;
p[2] += y;
p[4] += x;
p[5] += y;
p[6] += x;
p[7] += y;
p[8] += x;
p[9] += y;
};
}
else {
path = [
'M',
fromX,
fromY,
'Q',
fromX + radius * signX,
fromY,
fromX + radius * signX,
fromY + radius * signY,
'V',
Math.abs(fromY - toY) < radius * 2 ? fromY + radius * signY : toY - radius * signY,
'Q',
fromX + radius * signX,
toY,
fromX + radius * signX * 2,
toY,
'H',
toX,
];
translate = function (x, y) {
var p = this.path;
p[1] += x;
p[2] += y;
p[4] += x;
p[5] += y;
p[6] += x;
p[7] += y;
p[9] += y;
p[11] += x;
p[12] += y;
p[13] += x;
p[14] += y;
p[16] += x;
};
}
return {
type: 'path',
path: path,
'stroke-linecap': theme.line.strokeLinecap,
'stroke-linejoin': theme.line.strokeLinejoin,
stroke: theme.line.stroke,
'stroke-width': theme.line.strokeWidth,
_translate: translate,
};
}
exports.smoothLine = smoothLine;
function point(_a) {
var x = _a.x, y = _a.y, fill = _a.fill;
var r = 10;
return {
items: [
{
type: 'circle',
fill: fill,
cx: x + r,
cy: y,
r: r,
stroke: 'none',
_translate: function (x, y) {
this.cx += x;
this.cy += y;
},
},
],
width: r * 2,
height: r * 2,
x: x,
y: y,
lineInX: x,
lineOutX: x + r * 2,
};
}
exports.point = point;
//# sourceMappingURL=element.js.map