1 | import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
2 | import { extendMap, px2hd as defaultPx2hd } from '../util';
|
3 | import { isFunction, omit } from '@antv/util';
|
4 | import computeLayout from './css-layout';
|
5 | import getShapeAttrs from './shape';
|
6 | import getAnimation from './animation';
|
7 | import { ELEMENT_DELETE } from './elementStatus';
|
8 | import createClipElement from './createClipElement';
|
9 |
|
10 | function 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 |
|
22 | var style = px2hd(props.style);
|
23 | var attrs = px2hd(props.attrs);
|
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 |
|
55 | style: style,
|
56 | attrs: attrs
|
57 | };
|
58 | }
|
59 |
|
60 | function 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 |
|
72 | function 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);
|
86 |
|
87 | var lastAttrs = _cache.attrs;
|
88 |
|
89 | var elementAttrs = _objectSpread(_objectSpread(_objectSpread({}, getShapeAttrs(type, layout)), status === ELEMENT_DELETE ? lastAttrs : null), 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 | }));
|
107 |
|
108 | var children = renderChildren ? renderChildren : nodeChildren;
|
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 |
|
134 | function 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 |
|
155 | function 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 |
|
168 | export { render };
|
169 | export default (function (element, container, animate) {
|
170 | return render(element, container, animate);
|
171 | }); |
\ | No newline at end of file |