@visactor/vgrammar-core
Version:
VGrammar is a visual grammar library
115 lines (107 loc) • 8.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.transformAttributes = exports.cloneTransformAttributes = exports.transformsByType = void 0;
const vutils_1 = require("@visactor/vutils"), enums_1 = require("../enums"), helpers_1 = require("./helpers"), common_1 = require("./common");
function storeOriginAttributes(name, channels, graphicAttributes, nextAttrs, element, markName) {
const prevStoredAttrs = element.getGraphicAttribute(name, !1, markName);
if (prevStoredAttrs) return channels.forEach((channel => {
channel in nextAttrs && (prevStoredAttrs[channel] = nextAttrs[channel]);
})), prevStoredAttrs;
const storedAttrs = {};
return channels.forEach((channel => {
storedAttrs[channel] = nextAttrs[channel];
})), graphicAttributes[name] = storedAttrs, storedAttrs;
}
function cloneTransformAttributes(markType, attributes) {
var _a;
return (null !== (_a = exports.transformsByType[markType]) && void 0 !== _a ? _a : []).reduce(((clonedAttributes, transform) => (transform.channels.forEach((channel => {
(0, vutils_1.has)(attributes, channel) && (clonedAttributes[channel] = attributes[channel]);
})), clonedAttributes)), {});
}
exports.transformsByType = {
rect3d: [ {
channels: [ "x", "y", "z", "x1", "y1", "width", "height", "length" ],
transform: (graphicAttributes, nextAttrs, storedAttrs) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
(0, vutils_1.isValidNumber)(nextAttrs.width) || !(0, vutils_1.isValidNumber)(nextAttrs.x1) && (0,
vutils_1.isValidNumber)(storedAttrs.width) ? (graphicAttributes.x = Math.min(null !== (_a = storedAttrs.x) && void 0 !== _a ? _a : 0, null !== (_b = storedAttrs.x1) && void 0 !== _b ? _b : 1 / 0),
graphicAttributes.width = storedAttrs.width) : (0, vutils_1.isValidNumber)(nextAttrs.x1) || !(0,
vutils_1.isValidNumber)(nextAttrs.width) && (0, vutils_1.isValidNumber)(storedAttrs.x1) ? (graphicAttributes.x = Math.min(storedAttrs.x, storedAttrs.x1),
graphicAttributes.width = Math.abs(storedAttrs.x1 - storedAttrs.x)) : (graphicAttributes.x = Math.min(null !== (_c = storedAttrs.x) && void 0 !== _c ? _c : 0, null !== (_d = storedAttrs.x1) && void 0 !== _d ? _d : 1 / 0),
graphicAttributes.width = storedAttrs.width), (0, vutils_1.isValidNumber)(nextAttrs.height) || !(0,
vutils_1.isValidNumber)(nextAttrs.y1) && (0, vutils_1.isValidNumber)(storedAttrs.height) ? (graphicAttributes.y = Math.min(null !== (_e = storedAttrs.y) && void 0 !== _e ? _e : 0, null !== (_f = storedAttrs.y1) && void 0 !== _f ? _f : 1 / 0),
graphicAttributes.height = storedAttrs.height) : (0, vutils_1.isValidNumber)(nextAttrs.y1) || !(0,
vutils_1.isValidNumber)(nextAttrs.height) && (0, vutils_1.isValidNumber)(storedAttrs.y1) ? (graphicAttributes.y = Math.min(storedAttrs.y, storedAttrs.y1),
graphicAttributes.height = Math.abs(storedAttrs.y1 - storedAttrs.y)) : (graphicAttributes.y = Math.min(null !== (_g = storedAttrs.y) && void 0 !== _g ? _g : 0, null !== (_h = storedAttrs.y1) && void 0 !== _h ? _h : 1 / 0),
graphicAttributes.height = storedAttrs.height), (0, vutils_1.isValidNumber)(nextAttrs.length) || !(0,
vutils_1.isValidNumber)(nextAttrs.z1) && (0, vutils_1.isValidNumber)(storedAttrs.length) ? (graphicAttributes.z = Math.min(null !== (_j = storedAttrs.z) && void 0 !== _j ? _j : 0, null !== (_k = storedAttrs.z1) && void 0 !== _k ? _k : 1 / 0),
graphicAttributes.length = storedAttrs.length) : (0, vutils_1.isValidNumber)(nextAttrs.z1) || !(0,
vutils_1.isValidNumber)(nextAttrs.length) && (0, vutils_1.isValidNumber)(storedAttrs.z1) ? (graphicAttributes.z = Math.min(storedAttrs.z, storedAttrs.z1),
graphicAttributes.length = Math.abs(storedAttrs.z1 - storedAttrs.z)) : (graphicAttributes.z = Math.min(null !== (_l = storedAttrs.z) && void 0 !== _l ? _l : 0, null !== (_m = storedAttrs.z1) && void 0 !== _m ? _m : 1 / 0),
graphicAttributes.length = storedAttrs.length);
},
storedAttrs: "sizeAttrs"
} ],
[enums_1.GrammarMarkType.text]: [ {
channels: [ "text", "limit", "autoLimit", "maxLineWidth", "textType", "_originText" ],
transform: (graphicAttributes, nextAttrs, storedAttrs) => {
var _a, _b, _c, _d;
const limit = null !== (_a = storedAttrs.limit) && void 0 !== _a ? _a : 1 / 0, autoLimit = null !== (_b = storedAttrs.autoLimit) && void 0 !== _b ? _b : 1 / 0, maxWidth = Math.min(limit, autoLimit), isTextConfig = (0,
vutils_1.isPlainObject)(storedAttrs.text) && !(0, vutils_1.isNil)(storedAttrs.text.text), text = isTextConfig ? storedAttrs.text.text : storedAttrs.text;
Array.isArray(text), graphicAttributes.maxLineWidth = maxWidth === 1 / 0 ? storedAttrs.maxLineWidth : maxWidth,
isTextConfig || storedAttrs.textType ? "rich" === storedAttrs.text.type || "rich" === storedAttrs.textType ? graphicAttributes.textConfig = text : "html" === storedAttrs.text.type ? (graphicAttributes.html = text,
graphicAttributes.text = null !== (_c = storedAttrs._originText) && void 0 !== _c ? _c : "",
graphicAttributes.renderable = !1) : "react" === storedAttrs.text.type ? (graphicAttributes.react = text,
graphicAttributes.text = null !== (_d = storedAttrs._originText) && void 0 !== _d ? _d : "",
graphicAttributes.renderable = !1) : graphicAttributes.text = text : graphicAttributes.text = text;
},
storedAttrs: "limitAttrs"
} ],
[enums_1.GrammarMarkType.rule]: [ {
channels: [ "x", "y", "x1", "y1" ],
transform: (graphicAttributes, nextAttrs, storedAttrs) => {
const points = (0, helpers_1.getRulePoints)(storedAttrs);
graphicAttributes.points = points, graphicAttributes.x = 0, graphicAttributes.y = 0;
},
storedAttrs: "pointAttrs"
} ],
[enums_1.GrammarMarkType.symbol]: [ {
channels: [ "shape", "symbolType" ],
transform: (graphicAttributes, nextAttrs, storedAttrs) => {
var _a;
graphicAttributes.symbolType = null !== (_a = nextAttrs.shape) && void 0 !== _a ? _a : nextAttrs.symbolType;
}
}, {
channels: [ "image", "fill", "background" ],
transform: (graphicAttributes, nextAttrs, storedAttrs) => {
nextAttrs.image ? (graphicAttributes.background = nextAttrs.image, graphicAttributes.fill = !1) : storedAttrs.image ? (graphicAttributes.background = storedAttrs.image,
graphicAttributes.fill = !1) : (graphicAttributes.fill = storedAttrs.fill, graphicAttributes.background = storedAttrs.background);
},
storedAttrs: "imageAttrs"
} ]
}, exports.cloneTransformAttributes = cloneTransformAttributes;
const transformAttributes = (markType, nextAttrs, element, markName) => {
const graphicAttributes = {}, changedKeys = nextAttrs ? Object.keys(nextAttrs) : [], transforms = (0,
vutils_1.isString)(markType) ? exports.transformsByType[markType] : markType;
if (transforms && transforms.length) {
const tags = [];
changedKeys.forEach((key => {
let isTransformed = !1;
transforms.forEach(((transform, index) => {
if (transform.channels.includes(key)) {
if (!tags[index]) if (transform.storedAttrs) {
const storedAttrs = storeOriginAttributes(transform.storedAttrs, transform.channels, graphicAttributes, nextAttrs, element, markName);
transform.transform(graphicAttributes, nextAttrs, storedAttrs);
} else transform.transform(graphicAttributes, nextAttrs, null);
tags[index] = !0, isTransformed = !0;
}
})), isTransformed || (common_1.commonAttributes.includes(key) ? (0, common_1.transformCommonAttribute)(graphicAttributes, key, nextAttrs) : graphicAttributes[key] = nextAttrs[key]);
}));
} else changedKeys.forEach((key => {
common_1.commonAttributes.includes(key) ? (0, common_1.transformCommonAttribute)(graphicAttributes, key, nextAttrs) : graphicAttributes[key] = nextAttrs[key];
}));
return graphicAttributes;
};
exports.transformAttributes = transformAttributes;
//# sourceMappingURL=transform.js.map