UNPKG

4.78 kBJavaScriptView Raw
1import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2import { extendMap, px2hd as defaultPx2hd } from '../util';
3import { isFunction, omit } from '@antv/util';
4import computeLayout from './css-layout';
5import getShapeAttrs from './shape';
6import getAnimation from './animation';
7import { ELEMENT_DELETE } from './elementStatus';
8import createClipElement from './createClipElement';
9// 转换成布局所需要的布局树
10function createNodeTree(element, container, px2hd) {
11 var key = element.key,
12 ref = element.ref,
13 _cache = element._cache,
14 type = element.type,
15 props = element.props,
16 status = element.status,
17 animation = element.animation;
18 var children = extendMap(props.children, function (child) {
19 return createNodeTree(child, container, px2hd);
20 });
21 // const { style, attrs } = props;
22 var style = px2hd(props.style);
23 var attrs = px2hd(props.attrs);
24 // 文本要自动计算文本的宽高, TODO, 后面再优化
25 if (type === 'text') {
26 var shape = container.addShape(type, {
27 attrs: _objectSpread({
28 x: 0,
29 y: 0
30 }, attrs)
31 });
32 var _shape$getBBox = shape.getBBox(),
33 width = _shape$getBBox.width,
34 height = _shape$getBBox.height;
35 style = _objectSpread({
36 width: width,
37 height: height
38 }, style);
39 // 无用,销毁掉
40 shape.remove(true);
41 }
42 return {
43 key: key,
44 ref: ref,
45 _cache: _cache,
46 type: type,
47 props: props,
48 children: children,
49 status: status,
50 animation: animation,
51 // 处理px2hd之后的配置
52 style: style,
53 attrs: attrs
54 };
55}
56function mergeLayout(parent, layout) {
57 if (!parent || !layout) return layout;
58 var parentLeft = parent.left,
59 parentTop = parent.top;
60 var left = layout.left,
61 top = layout.top;
62 return _objectSpread(_objectSpread({}, layout), {}, {
63 left: parentLeft + left,
64 top: parentTop + top
65 });
66}
67function createElement(node, container, parentLayout, animate) {
68 var _node$_cache = node._cache,
69 _cache = _node$_cache === void 0 ? {} : _node$_cache,
70 ref = node.ref,
71 type = node.type,
72 props = node.props,
73 attrs = node.attrs,
74 originLayout = node.layout,
75 renderChildren = node.renderChildren,
76 nodeChildren = node.children,
77 status = node.status,
78 animation = node.animation;
79 var layout = mergeLayout(parentLayout, originLayout);
80 // 该元素上一次的attrs
81 var lastAttrs = _cache.attrs;
82 var elementAttrs = _objectSpread(_objectSpread(_objectSpread({}, getShapeAttrs(type, layout)), status === ELEMENT_DELETE ? lastAttrs : null), attrs);
83 // 缓存这次新的attrs
84 _cache.attrs = elementAttrs;
85 if (elementAttrs.clip) {
86 var clip = elementAttrs.clip;
87 var clipConfig = isFunction(clip) ? clip(elementAttrs) : clip;
88 elementAttrs.clip = createClipElement(clipConfig.type, clipConfig);
89 }
90 var element;
91 if (type === 'group') {
92 element = container.addGroup(_objectSpread(_objectSpread({}, omit(props, ['children'])), {}, {
93 status: status,
94 attrs: elementAttrs
95 }));
96 // 如果元素被删除了,就不会有renderChildren, 直接拿node.children渲染
97 var children = renderChildren ? renderChildren : nodeChildren;
98 // 只有group才需要处理children
99 if (children && children.length) {
100 for (var i = 0, len = children.length; i < len; i++) {
101 createElement(children[i], element, layout, animate);
102 }
103 }
104 } else {
105 element = container.addShape(type, _objectSpread(_objectSpread({}, props), {}, {
106 status: status,
107 attrs: elementAttrs
108 }));
109 }
110 if (animate !== false) {
111 element.set('animation', getAnimation(element, animation, elementAttrs, lastAttrs));
112 }
113 if (ref) {
114 ref.current = element;
115 }
116 return element;
117}
118// 过滤删除的元素,让其不参与布局计算
119function filterDeleteElement(node) {
120 var status = node.status,
121 children = node.children;
122 if (status === ELEMENT_DELETE) {
123 return null;
124 }
125 if (!children || !children.length) {
126 return node;
127 }
128 var newChildren = children.filter(function (child) {
129 return !!filterDeleteElement(child);
130 });
131 // 要保留引用
132 node.children = newChildren;
133 node.renderChildren = children;
134 return node;
135}
136function render(element, container, animate) {
137 var px2hd = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultPx2hd;
138 if (!element) {
139 return;
140 }
141 var nodeTree = createNodeTree(element, container, px2hd);
142 var computeLayoutTree = filterDeleteElement(nodeTree);
143 computeLayout(computeLayoutTree);
144 return createElement(nodeTree, container, null, animate);
145}
146export { render };
147export default (function (element, container, animate) {
148 return render(element, container, animate);
149});
\No newline at end of file