UNPKG

6.59 kBJavaScriptView Raw
1import { __assign, __extends } from "tslib";
2import { jsx, Component, computeLayout } from '@antv/f-engine';
3var defaultProps = {
4 offsetX: 0,
5 offsetY: 0,
6 points: [],
7 direct: 'tl',
8 side: '8px',
9 autoAdjust: true
10};
11var defaultStyle = {
12 container: {
13 fill: '#1677FF',
14 radius: '4px',
15 padding: ['4px', '8px']
16 },
17 text: {
18 fontSize: '22px',
19 fill: '#fff'
20 },
21 arrow: {
22 fill: '#1677FF'
23 }
24};
25var Label = function Label(_a) {
26 var content = _a.content,
27 background = _a.background,
28 textStyle = _a.textStyle,
29 _b = _a.animation,
30 animation = _b === void 0 ? {} : _b;
31 return jsx("rect", {
32 style: __assign({
33 display: 'flex',
34 fill: defaultStyle.container.fill,
35 padding: defaultStyle.container.padding,
36 radius: defaultStyle.container.radius
37 }, background),
38 animation: animation
39 }, jsx("text", {
40 style: __assign({
41 text: content,
42 fontSize: defaultStyle.text.fontSize,
43 fill: defaultStyle.text.fill
44 }, textStyle),
45 animation: animation
46 }));
47};
48var Tag = /** @class */function (_super) {
49 __extends(Tag, _super);
50 function Tag() {
51 return _super !== null && _super.apply(this, arguments) || this;
52 }
53 Tag.prototype.render = function () {
54 var _a = this,
55 props = _a.props,
56 context = _a.context;
57 var px2hd = context.px2hd;
58 var cfg = __assign(__assign({}, defaultProps), props);
59 var _b = px2hd(cfg),
60 points = _b.points,
61 content = _b.content,
62 offsetX = _b.offsetX,
63 offsetY = _b.offsetY,
64 direct = _b.direct,
65 side = _b.side,
66 autoAdjust = _b.autoAdjust,
67 canvasWidth = _b.canvasWidth,
68 canvasHeight = _b.canvasHeight,
69 background = _b.background,
70 textStyle = _b.textStyle,
71 animation = _b.animation;
72 var _c = points[0] || {},
73 x = _c.x,
74 y = _c.y;
75 if (isNaN(x) || isNaN(y)) return null;
76 var offsetXNum = context.px2hd(offsetX);
77 var offsetYNum = context.px2hd(offsetY);
78 var posX = x + (offsetXNum || 0);
79 var posY = y + (offsetYNum || 0);
80 var layout = computeLayout(this, jsx(Label, {
81 content: content,
82 background: background,
83 textStyle: textStyle
84 })).layout;
85 var guideWidth = layout.width,
86 guideHeight = layout.height;
87 var _getDirect = function _getDirect(point) {
88 var newDirect = direct;
89 var x = point.x,
90 y = point.y;
91 var vertical = newDirect[0];
92 var horizontal = newDirect[1];
93 // adjust for vertical direction
94 if (vertical === 't' && y - side - guideHeight < 0) {
95 vertical = 'b';
96 } else if (vertical === 'b' && y + side + guideHeight > canvasHeight) {
97 vertical = 't';
98 }
99 // adjust for horizontal direction
100 var diff = vertical === 'c' ? side : 0;
101 if (horizontal === 'l' && x - diff - guideWidth < 0) {
102 horizontal = 'r';
103 } else if (horizontal === 'r' && x + diff + guideWidth > canvasWidth) {
104 horizontal = 'l';
105 } else if (horizontal === 'c') {
106 if (guideWidth / 2 + x + diff > canvasWidth) {
107 horizontal = 'l';
108 } else if (x - guideWidth / 2 - diff < 0) {
109 horizontal = 'r';
110 }
111 }
112 newDirect = vertical + horizontal;
113 return newDirect;
114 };
115 var _getArrowPoints = function _getArrowPoints(direct) {
116 var arrowPoints = [];
117 if (direct === 'tl') {
118 arrowPoints = [{
119 x: guideWidth,
120 y: guideHeight - 1
121 }, {
122 x: guideWidth,
123 y: guideHeight + side
124 }, {
125 x: guideWidth - side,
126 y: guideHeight - 1
127 }];
128 posX -= guideWidth || 0;
129 posY = posY - (guideHeight || 0) - side;
130 } else if (direct === 'cl') {
131 arrowPoints = [{
132 x: guideWidth,
133 y: guideHeight / 2 - side
134 }, {
135 x: guideWidth,
136 y: guideHeight / 2 + side
137 }, {
138 x: guideWidth + side,
139 y: guideHeight / 2
140 }];
141 posX = posX - (guideWidth || 0) - side;
142 posY -= guideHeight / 2 || 0;
143 } else if (direct === 'bl') {
144 arrowPoints = [{
145 x: guideWidth,
146 y: -side
147 }, {
148 x: guideWidth,
149 y: 1
150 }, {
151 x: guideWidth - side,
152 y: 1
153 }];
154 posX = posX - (guideWidth || 0);
155 posY += side;
156 } else if (direct === 'bc') {
157 arrowPoints = [{
158 x: guideWidth / 2,
159 y: -side
160 }, {
161 x: guideWidth / 2 - side,
162 y: 1
163 }, {
164 x: guideWidth / 2 + side,
165 y: 1
166 }];
167 posX = posX - (guideWidth / 2 || 0);
168 posY = posY + side;
169 } else if (direct === 'br') {
170 arrowPoints = [{
171 x: 0,
172 y: -side
173 }, {
174 x: 0,
175 y: 1
176 }, {
177 x: +side,
178 y: 1
179 }];
180 posY += side;
181 } else if (direct === 'cr') {
182 arrowPoints = [{
183 x: -side,
184 y: guideHeight / 2
185 }, {
186 x: 0,
187 y: guideHeight / 2 - side
188 }, {
189 x: 0,
190 y: guideHeight / 2 + side
191 }];
192 posX += side;
193 posY -= guideHeight / 2 || 0;
194 } else if (direct === 'tr') {
195 arrowPoints = [{
196 x: 0,
197 y: guideHeight + side
198 }, {
199 x: 0,
200 y: guideHeight - 1
201 }, {
202 x: side,
203 y: guideHeight - 1
204 }];
205 posY = posY - (guideHeight || 0) - side;
206 } else if (direct === 'tc') {
207 arrowPoints = [{
208 x: guideWidth / 2,
209 y: guideHeight + side
210 }, {
211 x: guideWidth / 2 - side,
212 y: guideHeight - 1
213 }, {
214 x: guideWidth / 2 + side,
215 y: guideHeight - 1
216 }];
217 posX -= guideWidth / 2 || 0;
218 posY = posY - guideHeight - side;
219 }
220 return arrowPoints;
221 };
222 var dr = autoAdjust ? _getDirect(points[0]) : direct;
223 var arrowPoints = _getArrowPoints(dr);
224 return jsx("group", {
225 style: {
226 x: posX,
227 y: posY
228 }
229 }, jsx(Label, {
230 content: content,
231 background: background,
232 textStyle: textStyle,
233 animation: animation
234 }), jsx("polygon", {
235 style: {
236 points: arrowPoints.map(function (d) {
237 return [d.x, d.y];
238 }),
239 fill: (background === null || background === void 0 ? void 0 : background.fill) || defaultStyle.arrow.fill
240 },
241 animation: animation
242 }));
243 };
244 return Tag;
245}(Component);
246export default Tag;
\No newline at end of file