UNPKG

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