@antv/g-svg 2.0.45 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.esm.js +2036 -14
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +2037 -28
- package/dist/index.js.map +1 -1
- package/dist/index.umd.min.js +4 -28
- package/dist/index.umd.min.js.map +1 -1
- package/package.json +6 -8
- package/types/index.d.ts +6 -5
- package/types/index.d.ts.map +1 -1
- package/types/plugins/picker/SVGPickerPlugin.d.ts +13 -0
- package/types/plugins/picker/SVGPickerPlugin.d.ts.map +1 -0
- package/types/plugins/picker/index.d.ts +7 -0
- package/types/plugins/picker/index.d.ts.map +1 -0
- package/types/plugins/svg-renderer/DefaultElementLifeCycleContribution.d.ts +15 -0
- package/types/plugins/svg-renderer/DefaultElementLifeCycleContribution.d.ts.map +1 -0
- package/types/plugins/svg-renderer/SVGRendererPlugin.d.ts +64 -0
- package/types/plugins/svg-renderer/SVGRendererPlugin.d.ts.map +1 -0
- package/types/plugins/svg-renderer/components/ElementSVG.d.ts +19 -0
- package/types/plugins/svg-renderer/components/ElementSVG.d.ts.map +1 -0
- package/types/plugins/svg-renderer/index.d.ts +17 -0
- package/types/plugins/svg-renderer/index.d.ts.map +1 -0
- package/types/plugins/svg-renderer/interfaces.d.ts +12 -0
- package/types/plugins/svg-renderer/interfaces.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/defs/DefElementManager.d.ts +19 -0
- package/types/plugins/svg-renderer/shapes/defs/DefElementManager.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/defs/Filter.d.ts +8 -0
- package/types/plugins/svg-renderer/shapes/defs/Filter.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/defs/Pattern.d.ts +7 -0
- package/types/plugins/svg-renderer/shapes/defs/Pattern.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/defs/Shadow.d.ts +7 -0
- package/types/plugins/svg-renderer/shapes/defs/Shadow.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/defs/index.d.ts +5 -0
- package/types/plugins/svg-renderer/shapes/defs/index.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/paths/Image.d.ts +3 -0
- package/types/plugins/svg-renderer/shapes/paths/Image.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/paths/Line.d.ts +3 -0
- package/types/plugins/svg-renderer/shapes/paths/Line.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/paths/Path.d.ts +3 -0
- package/types/plugins/svg-renderer/shapes/paths/Path.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/paths/Polyline.d.ts +3 -0
- package/types/plugins/svg-renderer/shapes/paths/Polyline.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/paths/Rect.d.ts +3 -0
- package/types/plugins/svg-renderer/shapes/paths/Rect.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/paths/Text.d.ts +3 -0
- package/types/plugins/svg-renderer/shapes/paths/Text.d.ts.map +1 -0
- package/types/plugins/svg-renderer/shapes/paths/index.d.ts +7 -0
- package/types/plugins/svg-renderer/shapes/paths/index.d.ts.map +1 -0
- package/types/plugins/svg-renderer/utils/dom.d.ts +2 -0
- package/types/plugins/svg-renderer/utils/dom.d.ts.map +1 -0
- package/types/plugins/svg-renderer/utils/format.d.ts +3 -0
- package/types/plugins/svg-renderer/utils/format.d.ts.map +1 -0
package/dist/index.esm.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @antv/g-svg
|
|
3
3
|
* @description A renderer implemented by SVG
|
|
4
|
-
* @version 2.0
|
|
5
|
-
* @date 12/
|
|
4
|
+
* @version 2.1.0
|
|
5
|
+
* @date 12/22/2025, 3:04:35 AM
|
|
6
6
|
* @author AntVis
|
|
7
7
|
* @docs https://g.antv.antgroup.com/
|
|
8
8
|
*/
|
|
@@ -10,18 +10,2040 @@ import _createClass from '@babel/runtime/helpers/createClass';
|
|
|
10
10
|
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
|
|
11
11
|
import _callSuper from '@babel/runtime/helpers/callSuper';
|
|
12
12
|
import _inherits from '@babel/runtime/helpers/inherits';
|
|
13
|
-
import { propertyMetadataCache, AbstractRendererPlugin, AbstractRenderer } from '@antv/g-lite';
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
import
|
|
17
|
-
|
|
18
|
-
import
|
|
19
|
-
import
|
|
20
|
-
|
|
13
|
+
import { isDisplayObject, translatePathToString, UnitType, isCSSRGB, isPattern, isBrowser, GradientType, computeLinearGradient, computeRadialGradient, isCSSGradientValue, parseTransform, ElementEvent, RenderReason, Shape, propertyMetadataCache, AbstractRendererPlugin, AbstractRenderer, DomInteraction } from '@antv/g-lite';
|
|
14
|
+
export { DomInteraction } from '@antv/g-lite';
|
|
15
|
+
import { isString, clamp, isNil } from '@antv/util';
|
|
16
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
17
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
18
|
+
import { mat4 } from 'gl-matrix';
|
|
19
|
+
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
20
|
+
import _createForOfIteratorHelper from '@babel/runtime/helpers/createForOfIteratorHelper';
|
|
21
21
|
import _regeneratorRuntime from '@babel/runtime/helpers/regeneratorRuntime';
|
|
22
22
|
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
|
|
23
23
|
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
|
|
24
|
-
|
|
24
|
+
|
|
25
|
+
var ElementSVG = /*#__PURE__*/_createClass(function ElementSVG() {
|
|
26
|
+
_classCallCheck(this, ElementSVG);
|
|
27
|
+
});
|
|
28
|
+
ElementSVG.tag = 'c-svg-element';
|
|
29
|
+
|
|
30
|
+
function updateImageElementAttribute($el, parsedStyle) {
|
|
31
|
+
var _parsedStyle$src = parsedStyle.src,
|
|
32
|
+
src = _parsedStyle$src === void 0 ? '' : _parsedStyle$src,
|
|
33
|
+
_parsedStyle$x = parsedStyle.x,
|
|
34
|
+
x = _parsedStyle$x === void 0 ? 0 : _parsedStyle$x,
|
|
35
|
+
_parsedStyle$y = parsedStyle.y,
|
|
36
|
+
y = _parsedStyle$y === void 0 ? 0 : _parsedStyle$y,
|
|
37
|
+
width = parsedStyle.width,
|
|
38
|
+
height = parsedStyle.height;
|
|
39
|
+
$el.setAttribute('x', "".concat(x));
|
|
40
|
+
$el.setAttribute('y', "".concat(y));
|
|
41
|
+
if (isString(src)) {
|
|
42
|
+
$el.setAttribute('href', src);
|
|
43
|
+
} else if (src instanceof Image) {
|
|
44
|
+
if (!width) {
|
|
45
|
+
$el.setAttribute('width', "".concat(src.width));
|
|
46
|
+
}
|
|
47
|
+
if (!height) {
|
|
48
|
+
$el.setAttribute('height', "".concat(src.height));
|
|
49
|
+
}
|
|
50
|
+
$el.setAttribute('href', src.src);
|
|
51
|
+
} else if (
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
src instanceof HTMLElement && isString(src.nodeName) && src.nodeName.toUpperCase() === 'CANVAS') {
|
|
54
|
+
$el.setAttribute('href', src.toDataURL());
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
} else if (src instanceof ImageData) {
|
|
57
|
+
var canvas = document.createElement('canvas');
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
canvas.setAttribute('width', "".concat(src.width));
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
canvas.setAttribute('height', "".concat(src.height));
|
|
62
|
+
var context = canvas.getContext('2d');
|
|
63
|
+
if (context) {
|
|
64
|
+
context.putImageData(src, 0, 0);
|
|
65
|
+
if (!width) {
|
|
66
|
+
// @ts-ignore
|
|
67
|
+
$el.setAttribute('width', "".concat(src.width));
|
|
68
|
+
}
|
|
69
|
+
if (!height) {
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
$el.setAttribute('height', "".concat(src.height));
|
|
72
|
+
}
|
|
73
|
+
$el.setAttribute('href', canvas.toDataURL());
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function updateLineElementAttribute($el, parsedStyle) {
|
|
79
|
+
var x1 = parsedStyle.x1,
|
|
80
|
+
y1 = parsedStyle.y1,
|
|
81
|
+
x2 = parsedStyle.x2,
|
|
82
|
+
y2 = parsedStyle.y2,
|
|
83
|
+
markerStart = parsedStyle.markerStart,
|
|
84
|
+
markerEnd = parsedStyle.markerEnd,
|
|
85
|
+
markerStartOffset = parsedStyle.markerStartOffset,
|
|
86
|
+
markerEndOffset = parsedStyle.markerEndOffset;
|
|
87
|
+
var startOffsetX = 0;
|
|
88
|
+
var startOffsetY = 0;
|
|
89
|
+
var endOffsetX = 0;
|
|
90
|
+
var endOffsetY = 0;
|
|
91
|
+
var rad = 0;
|
|
92
|
+
var x;
|
|
93
|
+
var y;
|
|
94
|
+
if (markerStart && isDisplayObject(markerStart) && markerStartOffset) {
|
|
95
|
+
x = x2 - x1;
|
|
96
|
+
y = y2 - y1;
|
|
97
|
+
rad = Math.atan2(y, x);
|
|
98
|
+
startOffsetX = Math.cos(rad) * (markerStartOffset || 0);
|
|
99
|
+
startOffsetY = Math.sin(rad) * (markerStartOffset || 0);
|
|
100
|
+
}
|
|
101
|
+
if (markerEnd && isDisplayObject(markerEnd) && markerEndOffset) {
|
|
102
|
+
x = x1 - x2;
|
|
103
|
+
y = y1 - y2;
|
|
104
|
+
rad = Math.atan2(y, x);
|
|
105
|
+
endOffsetX = Math.cos(rad) * (markerEndOffset || 0);
|
|
106
|
+
endOffsetY = Math.sin(rad) * (markerEndOffset || 0);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// @see https://github.com/antvis/g/issues/1038
|
|
110
|
+
$el.setAttribute('x1', "".concat(x1 + startOffsetX));
|
|
111
|
+
$el.setAttribute('y1', "".concat(y1 + startOffsetY));
|
|
112
|
+
$el.setAttribute('x2', "".concat(x2 + endOffsetX));
|
|
113
|
+
$el.setAttribute('y2', "".concat(y2 + endOffsetY));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function updatePathElementAttribute($el, parsedStyle) {
|
|
117
|
+
var d = parsedStyle.d,
|
|
118
|
+
markerStart = parsedStyle.markerStart,
|
|
119
|
+
markerEnd = parsedStyle.markerEnd,
|
|
120
|
+
markerStartOffset = parsedStyle.markerStartOffset,
|
|
121
|
+
markerEndOffset = parsedStyle.markerEndOffset;
|
|
122
|
+
var startOffsetX = 0;
|
|
123
|
+
var startOffsetY = 0;
|
|
124
|
+
var endOffsetX = 0;
|
|
125
|
+
var endOffsetY = 0;
|
|
126
|
+
var rad = 0;
|
|
127
|
+
var x;
|
|
128
|
+
var y;
|
|
129
|
+
if (markerStart && isDisplayObject(markerStart) && markerStartOffset) {
|
|
130
|
+
var _getStartTangent = markerStart.parentNode.getStartTangent(),
|
|
131
|
+
_getStartTangent2 = _slicedToArray(_getStartTangent, 2),
|
|
132
|
+
p1 = _getStartTangent2[0],
|
|
133
|
+
p2 = _getStartTangent2[1];
|
|
134
|
+
x = p1[0] - p2[0];
|
|
135
|
+
y = p1[1] - p2[1];
|
|
136
|
+
rad = Math.atan2(y, x);
|
|
137
|
+
startOffsetX = Math.cos(rad) * (markerStartOffset || 0);
|
|
138
|
+
startOffsetY = Math.sin(rad) * (markerStartOffset || 0);
|
|
139
|
+
}
|
|
140
|
+
if (markerEnd && isDisplayObject(markerEnd) && markerEndOffset) {
|
|
141
|
+
var _getEndTangent = markerEnd.parentNode.getEndTangent(),
|
|
142
|
+
_getEndTangent2 = _slicedToArray(_getEndTangent, 2),
|
|
143
|
+
_p = _getEndTangent2[0],
|
|
144
|
+
_p2 = _getEndTangent2[1];
|
|
145
|
+
x = _p[0] - _p2[0];
|
|
146
|
+
y = _p[1] - _p2[1];
|
|
147
|
+
rad = Math.atan2(y, x);
|
|
148
|
+
endOffsetX = Math.cos(rad) * (markerEndOffset || 0);
|
|
149
|
+
endOffsetY = Math.sin(rad) * (markerEndOffset || 0);
|
|
150
|
+
}
|
|
151
|
+
$el.setAttribute('d', translatePathToString(d.absolutePath, startOffsetX, startOffsetY, endOffsetX, endOffsetY));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function updatePolylineElementAttribute($el, parsedStyle) {
|
|
155
|
+
var points = parsedStyle.points.points,
|
|
156
|
+
markerStart = parsedStyle.markerStart,
|
|
157
|
+
markerStartOffset = parsedStyle.markerStartOffset,
|
|
158
|
+
markerEnd = parsedStyle.markerEnd,
|
|
159
|
+
markerEndOffset = parsedStyle.markerEndOffset;
|
|
160
|
+
var length = points.length;
|
|
161
|
+
if (points && length >= 2) {
|
|
162
|
+
var startOffsetX = 0;
|
|
163
|
+
var startOffsetY = 0;
|
|
164
|
+
var endOffsetX = 0;
|
|
165
|
+
var endOffsetY = 0;
|
|
166
|
+
var rad = 0;
|
|
167
|
+
var x;
|
|
168
|
+
var y;
|
|
169
|
+
if (markerStart && isDisplayObject(markerStart) && markerStartOffset) {
|
|
170
|
+
x = points[1][0] - points[0][0];
|
|
171
|
+
y = points[1][1] - points[0][1];
|
|
172
|
+
rad = Math.atan2(y, x);
|
|
173
|
+
startOffsetX = Math.cos(rad) * (markerStartOffset || 0);
|
|
174
|
+
startOffsetY = Math.sin(rad) * (markerStartOffset || 0);
|
|
175
|
+
}
|
|
176
|
+
if (markerEnd && isDisplayObject(markerEnd) && markerEndOffset) {
|
|
177
|
+
x = points[length - 2][0] - points[length - 1][0];
|
|
178
|
+
y = points[length - 2][1] - points[length - 1][1];
|
|
179
|
+
rad = Math.atan2(y, x);
|
|
180
|
+
endOffsetX = Math.cos(rad) * (markerEndOffset || 0);
|
|
181
|
+
endOffsetY = Math.sin(rad) * (markerEndOffset || 0);
|
|
182
|
+
}
|
|
183
|
+
$el.setAttribute('points', points.map(function (point, i) {
|
|
184
|
+
var offsetX = 0;
|
|
185
|
+
var offsetY = 0;
|
|
186
|
+
if (i === 0) {
|
|
187
|
+
offsetX = startOffsetX;
|
|
188
|
+
offsetY = startOffsetY;
|
|
189
|
+
} else if (i === length - 1) {
|
|
190
|
+
offsetX = endOffsetX;
|
|
191
|
+
offsetY = endOffsetY;
|
|
192
|
+
}
|
|
193
|
+
return "".concat(point[0] + offsetX, ",").concat(point[1] + offsetY);
|
|
194
|
+
}).join(' '));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function updateRectElementAttribute($el, parsedStyle) {
|
|
199
|
+
var radius = parsedStyle.radius,
|
|
200
|
+
_parsedStyle$x = parsedStyle.x,
|
|
201
|
+
x = _parsedStyle$x === void 0 ? 0 : _parsedStyle$x,
|
|
202
|
+
_parsedStyle$y = parsedStyle.y,
|
|
203
|
+
y = _parsedStyle$y === void 0 ? 0 : _parsedStyle$y,
|
|
204
|
+
width = parsedStyle.width,
|
|
205
|
+
height = parsedStyle.height;
|
|
206
|
+
|
|
207
|
+
// CSSKeyword: auto
|
|
208
|
+
if (!isFinite(width) || !isFinite(height)) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
var hasRadius = radius && radius.some(function (r) {
|
|
212
|
+
return r !== 0;
|
|
213
|
+
});
|
|
214
|
+
var d = '';
|
|
215
|
+
if (!hasRadius) {
|
|
216
|
+
d = "M ".concat(x, ",").concat(y, " l ").concat(width, ",0 l 0,").concat(height, " l").concat(-width, " 0 z");
|
|
217
|
+
} else {
|
|
218
|
+
var _radius$map = radius.map(function (r) {
|
|
219
|
+
return clamp(r, 0, Math.min(Math.abs(width) / 2, Math.abs(height) / 2));
|
|
220
|
+
}),
|
|
221
|
+
_radius$map2 = _slicedToArray(_radius$map, 4),
|
|
222
|
+
tlr = _radius$map2[0],
|
|
223
|
+
trr = _radius$map2[1],
|
|
224
|
+
brr = _radius$map2[2],
|
|
225
|
+
blr = _radius$map2[3];
|
|
226
|
+
var signX = width > 0 ? 1 : -1;
|
|
227
|
+
var signY = height > 0 ? 1 : -1;
|
|
228
|
+
// sweep-flag @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Paths#arcs
|
|
229
|
+
var sweepFlag = signX + signY !== 0 ? 1 : 0;
|
|
230
|
+
d = [["M ".concat(signX * tlr + x, ",").concat(y)], ["l ".concat(width - signX * (tlr + trr), ",0")], ["a ".concat(trr, ",").concat(trr, ",0,0,").concat(sweepFlag, ",").concat(signX * trr, ",").concat(signY * trr)], ["l 0,".concat(height - signY * (trr + brr))], ["a ".concat(brr, ",").concat(brr, ",0,0,").concat(sweepFlag, ",").concat(-signX * brr, ",").concat(signY * brr)], ["l ".concat(signX * (brr + blr) - width, ",0")], ["a ".concat(blr, ",").concat(blr, ",0,0,").concat(sweepFlag, ",").concat(-signX * blr, ",").concat(-signY * blr)], ["l 0,".concat(signY * (blr + tlr) - height)], ["a ".concat(tlr, ",").concat(tlr, ",0,0,").concat(sweepFlag, ",").concat(signX * tlr, ",").concat(-signY * tlr)], ['z']].join(' ');
|
|
231
|
+
}
|
|
232
|
+
$el.setAttribute('d', d);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function createSVGElement(type, doc) {
|
|
236
|
+
return (doc || document).createElementNS('http://www.w3.org/2000/svg', type);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
var FILTER_PREFIX = 'g-filter-';
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* use SVG filters, eg. blur, brightness, contrast...
|
|
243
|
+
* @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/filter
|
|
244
|
+
*/
|
|
245
|
+
function createOrUpdateFilter(document, $def, object, $el, filters) {
|
|
246
|
+
// eg. filter="url(#f1) url(#f2)"
|
|
247
|
+
var filterName = FILTER_PREFIX + object.entity;
|
|
248
|
+
var $existedFilters = $def.querySelectorAll("[name=".concat(filterName, "]"));
|
|
249
|
+
if ($existedFilters.length) {
|
|
250
|
+
$existedFilters.forEach(function ($filter) {
|
|
251
|
+
$def.removeChild($filter);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
if (filters.length === 0) {
|
|
255
|
+
// 'none'
|
|
256
|
+
$el === null || $el === void 0 || $el.removeAttribute('filter');
|
|
257
|
+
} else {
|
|
258
|
+
var filterIds = filters.map(function (_ref, i) {
|
|
259
|
+
var name = _ref.name,
|
|
260
|
+
params = _ref.params;
|
|
261
|
+
var $filter = createSVGElement('filter', document);
|
|
262
|
+
// @see https://github.com/antvis/g/issues/1025
|
|
263
|
+
$filter.setAttribute('filterUnits', 'userSpaceOnUse');
|
|
264
|
+
if (name === 'blur') {
|
|
265
|
+
createBlur(document, $filter, params);
|
|
266
|
+
} else if (name === 'brightness') {
|
|
267
|
+
createBrightness(document, $filter, params);
|
|
268
|
+
} else if (name === 'drop-shadow') {
|
|
269
|
+
createDropShadow(document, $filter, params);
|
|
270
|
+
} else if (name === 'contrast') {
|
|
271
|
+
createContrast(document, $filter, params);
|
|
272
|
+
} else if (name === 'grayscale') {
|
|
273
|
+
createGrayscale(document, $filter, params);
|
|
274
|
+
} else if (name === 'sepia') {
|
|
275
|
+
createSepia(document, $filter, params);
|
|
276
|
+
} else if (name === 'saturate') {
|
|
277
|
+
createSaturate(document, $filter, params);
|
|
278
|
+
} else if (name === 'hue-rotate') {
|
|
279
|
+
createHueRotate(document, $filter, params);
|
|
280
|
+
} else if (name === 'invert') {
|
|
281
|
+
createInvert(document, $filter, params);
|
|
282
|
+
}
|
|
283
|
+
$filter.id = "".concat(filterName, "-").concat(i);
|
|
284
|
+
$filter.setAttribute('name', filterName);
|
|
285
|
+
$def.appendChild($filter);
|
|
286
|
+
return $filter.id;
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
// @see https://github.com/antvis/G/issues/1114
|
|
290
|
+
setTimeout(function () {
|
|
291
|
+
$el === null || $el === void 0 || $el.setAttribute('filter', filterIds.map(function (filterId) {
|
|
292
|
+
return "url(#".concat(filterId, ")");
|
|
293
|
+
}).join(' '));
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function convertToAbsoluteValue(param) {
|
|
298
|
+
return param.unit === UnitType.kPercentage ? param.value / 100 : param.value;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* @see https://drafts.fxtf.org/filter-effects/#blurEquivalent
|
|
303
|
+
*/
|
|
304
|
+
function createBlur(document, $filter, params) {
|
|
305
|
+
var $feGaussianBlur = createSVGElement('feGaussianBlur', document);
|
|
306
|
+
$feGaussianBlur.setAttribute('in', 'SourceGraphic');
|
|
307
|
+
$feGaussianBlur.setAttribute('stdDeviation', "".concat(params[0].value));
|
|
308
|
+
$filter.appendChild($feGaussianBlur);
|
|
309
|
+
}
|
|
310
|
+
function createFeComponentTransfer(document, $filter, _ref2) {
|
|
311
|
+
var type = _ref2.type,
|
|
312
|
+
slope = _ref2.slope,
|
|
313
|
+
intercept = _ref2.intercept,
|
|
314
|
+
tableValues = _ref2.tableValues;
|
|
315
|
+
var $feComponentTransfer = createSVGElement('feComponentTransfer', document);
|
|
316
|
+
[createSVGElement('feFuncR', document), createSVGElement('feFuncG', document), createSVGElement('feFuncB', document)].forEach(function ($feFunc) {
|
|
317
|
+
$feFunc.setAttribute('type', type);
|
|
318
|
+
if (type === 'table') {
|
|
319
|
+
$feFunc.setAttribute('tableValues', "".concat(tableValues));
|
|
320
|
+
} else {
|
|
321
|
+
$feFunc.setAttribute('slope', "".concat(slope));
|
|
322
|
+
$feFunc.setAttribute('intercept', "".concat(intercept));
|
|
323
|
+
}
|
|
324
|
+
$feComponentTransfer.appendChild($feFunc);
|
|
325
|
+
});
|
|
326
|
+
$filter.appendChild($feComponentTransfer);
|
|
327
|
+
}
|
|
328
|
+
function createContrast(document, $filter, params) {
|
|
329
|
+
var slope = convertToAbsoluteValue(params[0]);
|
|
330
|
+
createFeComponentTransfer(document, $filter, {
|
|
331
|
+
type: 'linear',
|
|
332
|
+
slope: slope,
|
|
333
|
+
intercept: -(0.5 * slope) + 0.5
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
function createInvert(document, $filter, params) {
|
|
337
|
+
var amount = convertToAbsoluteValue(params[0]);
|
|
338
|
+
createFeComponentTransfer(document, $filter, {
|
|
339
|
+
type: 'table',
|
|
340
|
+
tableValues: "".concat(amount, " ").concat(1 - amount)
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
function createBrightness(document, $filter, params) {
|
|
344
|
+
var slope = convertToAbsoluteValue(params[0]);
|
|
345
|
+
createFeComponentTransfer(document, $filter, {
|
|
346
|
+
type: 'linear',
|
|
347
|
+
slope: slope,
|
|
348
|
+
intercept: 0
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
function createSaturate(document, $filter, params) {
|
|
352
|
+
var amount = convertToAbsoluteValue(params[0]);
|
|
353
|
+
var $feColorMatrix = createSVGElement('feColorMatrix', document);
|
|
354
|
+
$feColorMatrix.setAttribute('type', 'saturate');
|
|
355
|
+
$feColorMatrix.setAttribute('values', "".concat(amount));
|
|
356
|
+
$filter.appendChild($feColorMatrix);
|
|
357
|
+
}
|
|
358
|
+
function createHueRotate(document, $filter, params) {
|
|
359
|
+
var $feColorMatrix = createSVGElement('feColorMatrix', document);
|
|
360
|
+
$feColorMatrix.setAttribute('type', 'hueRotate');
|
|
361
|
+
// $feColorMatrix.setAttribute('values', `${params[0].to(UnitType.kDegrees).value}`);
|
|
362
|
+
// FIXME: convert to degrees
|
|
363
|
+
$feColorMatrix.setAttribute('values', "".concat(params[0].value));
|
|
364
|
+
$filter.appendChild($feColorMatrix);
|
|
365
|
+
}
|
|
366
|
+
function createDropShadow(document, $filter, params) {
|
|
367
|
+
var shadowOffsetX = params[0].value;
|
|
368
|
+
var shadowOffsetY = params[1].value;
|
|
369
|
+
var shadowBlur = params[2].value;
|
|
370
|
+
// @ts-ignore
|
|
371
|
+
var shadowColor = params[3].formatted;
|
|
372
|
+
var $feGaussianBlur = createSVGElement('feGaussianBlur', document);
|
|
373
|
+
$feGaussianBlur.setAttribute('in', 'SourceAlpha');
|
|
374
|
+
$feGaussianBlur.setAttribute('stdDeviation', "".concat(shadowBlur));
|
|
375
|
+
$filter.appendChild($feGaussianBlur);
|
|
376
|
+
var $feOffset = createSVGElement('feOffset', document);
|
|
377
|
+
$feOffset.setAttribute('dx', "".concat(shadowOffsetX));
|
|
378
|
+
$feOffset.setAttribute('dy', "".concat(shadowOffsetY));
|
|
379
|
+
$feOffset.setAttribute('result', 'offsetblur');
|
|
380
|
+
$filter.appendChild($feOffset);
|
|
381
|
+
var $feFlood = createSVGElement('feFlood', document);
|
|
382
|
+
$feFlood.setAttribute('flood-color', shadowColor);
|
|
383
|
+
$filter.appendChild($feFlood);
|
|
384
|
+
var $feComposite = createSVGElement('feComposite', document);
|
|
385
|
+
$feComposite.setAttribute('in2', 'offsetblur');
|
|
386
|
+
$feComposite.setAttribute('operator', 'in');
|
|
387
|
+
$filter.appendChild($feComposite);
|
|
388
|
+
var $feMerge = createSVGElement('feMerge', document);
|
|
389
|
+
$filter.appendChild($feMerge);
|
|
390
|
+
var $feMergeNode1 = createSVGElement('feMergeNode', document);
|
|
391
|
+
var $feMergeNode2 = createSVGElement('feMergeNode', document);
|
|
392
|
+
$feMergeNode2.setAttribute('in', 'SourceGraphic');
|
|
393
|
+
$feMerge.appendChild($feMergeNode1);
|
|
394
|
+
$feMerge.appendChild($feMergeNode2);
|
|
395
|
+
}
|
|
396
|
+
function createFeColorMatrix(document, $filter, matrix) {
|
|
397
|
+
var $feColorMatrix = createSVGElement('feColorMatrix', document);
|
|
398
|
+
$feColorMatrix.setAttribute('type', 'matrix');
|
|
399
|
+
$feColorMatrix.setAttribute('values', matrix.join(' '));
|
|
400
|
+
$filter.appendChild($feColorMatrix);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* @see https://drafts.fxtf.org/filter-effects/#grayscaleEquivalent
|
|
405
|
+
*/
|
|
406
|
+
function createGrayscale(document, $filter, params) {
|
|
407
|
+
var amount = convertToAbsoluteValue(params[0]);
|
|
408
|
+
createFeColorMatrix(document, $filter, [0.2126 + 0.7874 * (1 - amount), 0.7152 - 0.7152 * (1 - amount), 0.0722 - 0.0722 * (1 - amount), 0, 0, 0.2126 - 0.2126 * (1 - amount), 0.7152 + 0.2848 * (1 - amount), 0.0722 - 0.0722 * (1 - amount), 0, 0, 0.2126 - 0.2126 * (1 - amount), 0.7152 - 0.7152 * (1 - amount), 0.0722 + 0.9278 * (1 - amount), 0, 0, 0, 0, 0, 1, 0]);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* @see https://drafts.fxtf.org/filter-effects/#sepiaEquivalent
|
|
413
|
+
*/
|
|
414
|
+
function createSepia(document, $filter, params) {
|
|
415
|
+
var amount = convertToAbsoluteValue(params[0]);
|
|
416
|
+
createFeColorMatrix(document, $filter, [0.393 + 0.607 * (1 - amount), 0.769 - 0.769 * (1 - amount), 0.189 - 0.189 * (1 - amount), 0, 0, 0.349 - 0.349 * (1 - amount), 0.686 + 0.314 * (1 - amount), 0.168 - 0.168 * (1 - amount), 0, 0, 0.272 - 0.272 * (1 - amount), 0.534 - 0.534 * (1 - amount), 0.131 + 0.869 * (1 - amount), 0, 0, 0, 0, 0, 1, 0]);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
var PATTERN_PREFIX = 'g-pattern-';
|
|
420
|
+
var cacheKey2IDMap = {};
|
|
421
|
+
var counter = 0;
|
|
422
|
+
function resetPatternCounter() {
|
|
423
|
+
counter = 0;
|
|
424
|
+
cacheKey2IDMap = {};
|
|
425
|
+
}
|
|
426
|
+
function createOrUpdateGradientAndPattern(document, $def, object, $el, parsedColor, name, createImage, plugin) {
|
|
427
|
+
// eg. clipPath don't have fill/stroke
|
|
428
|
+
if (!parsedColor) {
|
|
429
|
+
return '';
|
|
430
|
+
}
|
|
431
|
+
if (isCSSRGB(parsedColor)) {
|
|
432
|
+
// keep using currentColor @see https://github.com/d3/d3-axis/issues/49
|
|
433
|
+
if (object.style[name] === 'currentColor') {
|
|
434
|
+
$el === null || $el === void 0 || $el.setAttribute(name, 'currentColor');
|
|
435
|
+
} else {
|
|
436
|
+
// constant value, eg. '#fff'
|
|
437
|
+
$el === null || $el === void 0 || $el.setAttribute(name, parsedColor.isNone ? 'none' : parsedColor.toString());
|
|
438
|
+
}
|
|
439
|
+
} else if (isPattern(parsedColor)) {
|
|
440
|
+
var patternId = createOrUpdatePattern(document, $def, object, parsedColor, createImage, plugin);
|
|
441
|
+
// use style instead of attribute when applying <pattern>
|
|
442
|
+
// @see https://stackoverflow.com/a/7723115
|
|
443
|
+
$el.style[name] = "url(#".concat(patternId, ")");
|
|
444
|
+
return patternId;
|
|
445
|
+
} else {
|
|
446
|
+
if (parsedColor.length === 1) {
|
|
447
|
+
var gradientId = createOrUpdateGradient(document, object, $def, $el, parsedColor[0]);
|
|
448
|
+
$el === null || $el === void 0 || $el.setAttribute(name, "url(#".concat(gradientId, ")"));
|
|
449
|
+
return gradientId;
|
|
450
|
+
}
|
|
451
|
+
// @see https://stackoverflow.com/questions/20671502/can-i-blend-gradients-in-svg
|
|
452
|
+
var filterId = createOrUpdateMultiGradient(document, object, $def, $el, parsedColor);
|
|
453
|
+
$el === null || $el === void 0 || $el.setAttribute('filter', "url(#".concat(filterId, ")"));
|
|
454
|
+
$el === null || $el === void 0 || $el.setAttribute('fill', 'black');
|
|
455
|
+
return filterId;
|
|
456
|
+
}
|
|
457
|
+
return '';
|
|
458
|
+
}
|
|
459
|
+
function generateCacheKey(src) {
|
|
460
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
461
|
+
var cacheKey = '';
|
|
462
|
+
if (isCSSGradientValue(src)) {
|
|
463
|
+
var type = src.type,
|
|
464
|
+
value = src.value;
|
|
465
|
+
if (type === GradientType.LinearGradient || type === GradientType.RadialGradient) {
|
|
466
|
+
// @ts-ignore
|
|
467
|
+
var _value$options = _objectSpread(_objectSpread({}, value), options),
|
|
468
|
+
_type = _value$options.type,
|
|
469
|
+
x = _value$options.x,
|
|
470
|
+
y = _value$options.y,
|
|
471
|
+
width = _value$options.width,
|
|
472
|
+
height = _value$options.height,
|
|
473
|
+
steps = _value$options.steps,
|
|
474
|
+
angle = _value$options.angle,
|
|
475
|
+
cx = _value$options.cx,
|
|
476
|
+
cy = _value$options.cy,
|
|
477
|
+
size = _value$options.size;
|
|
478
|
+
cacheKey = "gradient-".concat(_type, "-").concat((x === null || x === void 0 ? void 0 : x.toString()) || 0, "-").concat((y === null || y === void 0 ? void 0 : y.toString()) || 0, "-").concat((angle === null || angle === void 0 ? void 0 : angle.toString()) || 0, "-").concat((cx === null || cx === void 0 ? void 0 : cx.toString()) || 0, "-").concat((cy === null || cy === void 0 ? void 0 : cy.toString()) || 0, "-").concat((size === null || size === void 0 ? void 0 : size.toString()) || 0, "-").concat(width, "-").concat(height, "-").concat(steps.map(function (_ref) {
|
|
479
|
+
var offset = _ref.offset,
|
|
480
|
+
color = _ref.color;
|
|
481
|
+
return "".concat(offset).concat(color);
|
|
482
|
+
}).join('-'));
|
|
483
|
+
}
|
|
484
|
+
} else if (isPattern(src)) {
|
|
485
|
+
if (isString(src.image)) {
|
|
486
|
+
cacheKey = "pattern-".concat(src.image, "-").concat(src.repetition);
|
|
487
|
+
} else if (src.image.nodeName === 'rect') {
|
|
488
|
+
// use rect's entity as key
|
|
489
|
+
cacheKey = "pattern-rect-".concat(src.image.entity);
|
|
490
|
+
} else {
|
|
491
|
+
cacheKey = "pattern-".concat(counter);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
if (cacheKey) {
|
|
495
|
+
if (!cacheKey2IDMap[cacheKey]) {
|
|
496
|
+
cacheKey2IDMap[cacheKey] = "".concat(PATTERN_PREFIX).concat(counter++);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return cacheKey2IDMap[cacheKey];
|
|
500
|
+
}
|
|
501
|
+
function formatTransform(transform) {
|
|
502
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/patternTransform
|
|
503
|
+
// should remove unit: rotate(20deg) -> rotate(20)
|
|
504
|
+
return parseTransform(transform).map(function (parsed) {
|
|
505
|
+
var t = parsed.t,
|
|
506
|
+
d = parsed.d;
|
|
507
|
+
if (t === 'translate') {
|
|
508
|
+
return "translate(".concat(d[0].value, " ").concat(d[1].value, ")");
|
|
509
|
+
}
|
|
510
|
+
if (t === 'translateX') {
|
|
511
|
+
return "translate(".concat(d[0].value, " 0)");
|
|
512
|
+
}
|
|
513
|
+
if (t === 'translateY') {
|
|
514
|
+
return "translate(0 ".concat(d[0].value, ")");
|
|
515
|
+
}
|
|
516
|
+
if (t === 'rotate') {
|
|
517
|
+
return "rotate(".concat(d[0].value, ")");
|
|
518
|
+
}
|
|
519
|
+
if (t === 'scale') {
|
|
520
|
+
// scale(1) scale(1, 1)
|
|
521
|
+
var newScale = (d === null || d === void 0 ? void 0 : d.map(function (s) {
|
|
522
|
+
return s.value;
|
|
523
|
+
})) || [1, 1];
|
|
524
|
+
return "scale(".concat(newScale[0], ", ").concat(newScale[1], ")");
|
|
525
|
+
}
|
|
526
|
+
if (t === 'scaleX') {
|
|
527
|
+
var _newScale = (d === null || d === void 0 ? void 0 : d.map(function (s) {
|
|
528
|
+
return s.value;
|
|
529
|
+
})) || [1];
|
|
530
|
+
return "scale(".concat(_newScale[0], ", 1)");
|
|
531
|
+
}
|
|
532
|
+
if (t === 'scaleY') {
|
|
533
|
+
var _newScale2 = (d === null || d === void 0 ? void 0 : d.map(function (s) {
|
|
534
|
+
return s.value;
|
|
535
|
+
})) || [1];
|
|
536
|
+
return "scale(1, ".concat(_newScale2[0], ")");
|
|
537
|
+
}
|
|
538
|
+
if (t === 'skew') {
|
|
539
|
+
var newSkew = (d === null || d === void 0 ? void 0 : d.map(function (s) {
|
|
540
|
+
return s.value;
|
|
541
|
+
})) || [0, 0];
|
|
542
|
+
return "skewX(".concat(newSkew[0], ") skewY(").concat(newSkew[1], ")");
|
|
543
|
+
}
|
|
544
|
+
if (t === 'skewZ') {
|
|
545
|
+
var _newSkew = (d === null || d === void 0 ? void 0 : d.map(function (s) {
|
|
546
|
+
return s.value;
|
|
547
|
+
})) || [0];
|
|
548
|
+
return "skewX(".concat(_newSkew[0], ")");
|
|
549
|
+
}
|
|
550
|
+
if (t === 'skewY') {
|
|
551
|
+
var _newSkew2 = (d === null || d === void 0 ? void 0 : d.map(function (s) {
|
|
552
|
+
return s.value;
|
|
553
|
+
})) || [0];
|
|
554
|
+
return "skewY(".concat(_newSkew2[0], ")");
|
|
555
|
+
}
|
|
556
|
+
if (t === 'matrix') {
|
|
557
|
+
var _d$map = d.map(function (s) {
|
|
558
|
+
return s.value;
|
|
559
|
+
}),
|
|
560
|
+
_d$map2 = _slicedToArray(_d$map, 6),
|
|
561
|
+
a = _d$map2[0],
|
|
562
|
+
b = _d$map2[1],
|
|
563
|
+
c = _d$map2[2],
|
|
564
|
+
dd = _d$map2[3],
|
|
565
|
+
tx = _d$map2[4],
|
|
566
|
+
ty = _d$map2[5];
|
|
567
|
+
return "matrix(".concat(a, " ").concat(b, " ").concat(c, " ").concat(dd, " ").concat(tx, " ").concat(ty, ")");
|
|
568
|
+
}
|
|
569
|
+
return null;
|
|
570
|
+
}).filter(function (item) {
|
|
571
|
+
return item !== null;
|
|
572
|
+
}).join(' ');
|
|
573
|
+
}
|
|
574
|
+
function create$Pattern(document, $def, object, pattern, patternId, width, height) {
|
|
575
|
+
var repetition = pattern.repetition,
|
|
576
|
+
transform = pattern.transform;
|
|
577
|
+
|
|
578
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/pattern
|
|
579
|
+
var $pattern = createSVGElement('pattern', document);
|
|
580
|
+
if (transform) {
|
|
581
|
+
$pattern.setAttribute('patternTransform', formatTransform(transform));
|
|
582
|
+
}
|
|
583
|
+
$pattern.setAttribute('patternUnits', 'userSpaceOnUse');
|
|
584
|
+
$pattern.id = patternId;
|
|
585
|
+
$def.appendChild($pattern);
|
|
586
|
+
var _object$getGeometryBo = object.getGeometryBounds(),
|
|
587
|
+
halfExtents = _object$getGeometryBo.halfExtents,
|
|
588
|
+
min = _object$getGeometryBo.min;
|
|
589
|
+
$pattern.setAttribute('x', "".concat(min[0]));
|
|
590
|
+
$pattern.setAttribute('y', "".concat(min[1]));
|
|
591
|
+
|
|
592
|
+
// There is no equivalent to CSS no-repeat for SVG patterns
|
|
593
|
+
// @see https://stackoverflow.com/a/33481956
|
|
594
|
+
var patternWidth = width;
|
|
595
|
+
var patternHeight = height;
|
|
596
|
+
if (repetition === 'repeat-x') {
|
|
597
|
+
patternHeight = halfExtents[1] * 2;
|
|
598
|
+
} else if (repetition === 'repeat-y') {
|
|
599
|
+
patternWidth = halfExtents[0] * 2;
|
|
600
|
+
} else if (repetition === 'no-repeat') {
|
|
601
|
+
patternWidth = halfExtents[0] * 2;
|
|
602
|
+
patternHeight = halfExtents[1] * 2;
|
|
603
|
+
}
|
|
604
|
+
$pattern.setAttribute('width', "".concat(patternWidth));
|
|
605
|
+
$pattern.setAttribute('height', "".concat(patternHeight));
|
|
606
|
+
return $pattern;
|
|
607
|
+
}
|
|
608
|
+
function createOrUpdatePattern(document, $def, object, pattern, createImage, plugin) {
|
|
609
|
+
var patternId = generateCacheKey(pattern);
|
|
610
|
+
var $existed = $def.querySelector("#".concat(patternId));
|
|
611
|
+
if (!$existed) {
|
|
612
|
+
var image = pattern.image;
|
|
613
|
+
var imageURL = '';
|
|
614
|
+
if (isString(image)) {
|
|
615
|
+
imageURL = image;
|
|
616
|
+
} else if (isBrowser) {
|
|
617
|
+
if (image instanceof HTMLImageElement) {
|
|
618
|
+
imageURL = image.src;
|
|
619
|
+
} else if (image instanceof HTMLCanvasElement) {
|
|
620
|
+
imageURL = image.toDataURL();
|
|
621
|
+
} else ;
|
|
622
|
+
}
|
|
623
|
+
if (imageURL) {
|
|
624
|
+
var $image = createSVGElement('image', document);
|
|
625
|
+
// use href instead of xlink:href
|
|
626
|
+
// @see https://stackoverflow.com/a/13379007
|
|
627
|
+
$image.setAttribute('href', imageURL);
|
|
628
|
+
var img = createImage();
|
|
629
|
+
if (!imageURL.match(/^data:/i)) {
|
|
630
|
+
img.crossOrigin = 'Anonymous';
|
|
631
|
+
$image.setAttribute('crossorigin', 'anonymous');
|
|
632
|
+
}
|
|
633
|
+
img.src = imageURL;
|
|
634
|
+
var onload = function onload() {
|
|
635
|
+
var $pattern = create$Pattern(document, $def, object, pattern, patternId, img.width, img.height);
|
|
636
|
+
$def.appendChild($pattern);
|
|
637
|
+
$pattern.appendChild($image);
|
|
638
|
+
$image.setAttribute('x', '0');
|
|
639
|
+
$image.setAttribute('y', '0');
|
|
640
|
+
$image.setAttribute('width', "".concat(img.width));
|
|
641
|
+
$image.setAttribute('height', "".concat(img.height));
|
|
642
|
+
};
|
|
643
|
+
if (img.complete) {
|
|
644
|
+
onload();
|
|
645
|
+
} else {
|
|
646
|
+
img.onload = onload;
|
|
647
|
+
// Fix onload() bug in IE9
|
|
648
|
+
// img.src = img.src;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
if (image.nodeName === 'rect') {
|
|
652
|
+
var _parsedStyle = image.parsedStyle,
|
|
653
|
+
width = _parsedStyle.width,
|
|
654
|
+
height = _parsedStyle.height;
|
|
655
|
+
var $pattern = create$Pattern(document, $def, image, pattern, patternId, width, height);
|
|
656
|
+
|
|
657
|
+
// traverse subtree of pattern
|
|
658
|
+
image.forEach(function (object) {
|
|
659
|
+
plugin.createSVGDom(document, object, null);
|
|
660
|
+
|
|
661
|
+
// @ts-ignore
|
|
662
|
+
var svgElement = object.elementSVG;
|
|
663
|
+
|
|
664
|
+
// apply local RTS transformation to <group> wrapper
|
|
665
|
+
var localTransform = object.getLocalTransform();
|
|
666
|
+
plugin.applyTransform(svgElement.$groupEl, localTransform);
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
// @ts-ignore
|
|
670
|
+
var svgElement = image.elementSVG;
|
|
671
|
+
$pattern.appendChild(svgElement.$groupEl);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
return patternId;
|
|
675
|
+
}
|
|
676
|
+
function createOrUpdateGradient(document, object, $def, $el, parsedColor) {
|
|
677
|
+
var bounds = object.getGeometryBounds();
|
|
678
|
+
var width = bounds && bounds.halfExtents[0] * 2 || 0;
|
|
679
|
+
var height = bounds && bounds.halfExtents[1] * 2 || 0;
|
|
680
|
+
var min = bounds && bounds.min || [0, 0];
|
|
681
|
+
var gradientId = generateCacheKey(parsedColor, {
|
|
682
|
+
x: min[0],
|
|
683
|
+
y: min[1],
|
|
684
|
+
width: width,
|
|
685
|
+
height: height
|
|
686
|
+
});
|
|
687
|
+
var $existed = $def.querySelector("#".concat(gradientId));
|
|
688
|
+
if (!$existed) {
|
|
689
|
+
// <linearGradient> <radialGradient>
|
|
690
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/linearGradient
|
|
691
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/radialGradient
|
|
692
|
+
$existed = createSVGElement(parsedColor.type === GradientType.LinearGradient ? 'linearGradient' : 'radialGradient', document);
|
|
693
|
+
// @see https://github.com/antvis/g/issues/1025
|
|
694
|
+
$existed.setAttribute('gradientUnits', 'userSpaceOnUse');
|
|
695
|
+
// add stops
|
|
696
|
+
var innerHTML = '';
|
|
697
|
+
parsedColor.value.steps
|
|
698
|
+
// sort by offset @see https://github.com/antvis/G/issues/1171
|
|
699
|
+
.sort(function (a, b) {
|
|
700
|
+
return a.offset.value - b.offset.value;
|
|
701
|
+
}).forEach(function (_ref2) {
|
|
702
|
+
var offset = _ref2.offset,
|
|
703
|
+
color = _ref2.color;
|
|
704
|
+
// TODO: support absolute unit like `px`
|
|
705
|
+
innerHTML += "<stop offset=\"".concat(offset.value / 100, "\" stop-color=\"").concat(color, "\"></stop>");
|
|
706
|
+
});
|
|
707
|
+
$existed.innerHTML = innerHTML;
|
|
708
|
+
$existed.id = gradientId;
|
|
709
|
+
$def.appendChild($existed);
|
|
710
|
+
}
|
|
711
|
+
if (parsedColor.type === GradientType.LinearGradient) {
|
|
712
|
+
var _ref3 = parsedColor.value,
|
|
713
|
+
angle = _ref3.angle;
|
|
714
|
+
var _computeLinearGradien = computeLinearGradient([min[0], min[1]], width, height, angle),
|
|
715
|
+
x1 = _computeLinearGradien.x1,
|
|
716
|
+
y1 = _computeLinearGradien.y1,
|
|
717
|
+
x2 = _computeLinearGradien.x2,
|
|
718
|
+
y2 = _computeLinearGradien.y2;
|
|
719
|
+
$existed.setAttribute('x1', "".concat(x1));
|
|
720
|
+
$existed.setAttribute('y1', "".concat(y1));
|
|
721
|
+
$existed.setAttribute('x2', "".concat(x2));
|
|
722
|
+
$existed.setAttribute('y2', "".concat(y2));
|
|
723
|
+
|
|
724
|
+
// $existed.setAttribute('gradientTransform', `rotate(${angle})`);
|
|
725
|
+
} else {
|
|
726
|
+
var _ref4 = parsedColor.value,
|
|
727
|
+
cx = _ref4.cx,
|
|
728
|
+
cy = _ref4.cy,
|
|
729
|
+
size = _ref4.size;
|
|
730
|
+
var _computeRadialGradien = computeRadialGradient([min[0], min[1]], width, height, cx, cy, size),
|
|
731
|
+
x = _computeRadialGradien.x,
|
|
732
|
+
y = _computeRadialGradien.y,
|
|
733
|
+
r = _computeRadialGradien.r;
|
|
734
|
+
$existed.setAttribute('cx', "".concat(x));
|
|
735
|
+
$existed.setAttribute('cy', "".concat(y));
|
|
736
|
+
$existed.setAttribute('r', "".concat(r));
|
|
737
|
+
}
|
|
738
|
+
return gradientId;
|
|
739
|
+
}
|
|
740
|
+
function createOrUpdateMultiGradient(document, object, $def, $el, gradients) {
|
|
741
|
+
var filterId = "".concat(FILTER_PREFIX + object.entity, "-gradient");
|
|
742
|
+
var $existed = $def.querySelector("#".concat(filterId));
|
|
743
|
+
if (!$existed) {
|
|
744
|
+
$existed = createSVGElement('filter', document);
|
|
745
|
+
$existed.setAttribute('filterUnits', 'userSpaceOnUse');
|
|
746
|
+
// @see https://github.com/antvis/g/issues/1025
|
|
747
|
+
$existed.setAttribute('x', '0%');
|
|
748
|
+
$existed.setAttribute('y', '0%');
|
|
749
|
+
$existed.setAttribute('width', '100%');
|
|
750
|
+
$existed.setAttribute('height', '100%');
|
|
751
|
+
$existed.id = filterId;
|
|
752
|
+
$def.appendChild($existed);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* <rect id="wave-rect" x="0" y="0" width="100%" height="100%" fill="url(#wave)"></rect>
|
|
757
|
+
* <filter id="blend-it" x="0%" y="0%" width="100%" height="100%">
|
|
758
|
+
<feImage xlink:href="#wave-rect" result="myWave" x="100" y="100"/>
|
|
759
|
+
<feImage xlink:href="#ry-rect" result="myRY" x="100" y="100"/>
|
|
760
|
+
<feBlend in="myWave" in2="myRY" mode="multiply" result="blendedGrad"/>
|
|
761
|
+
<feComposite in="blendedGrad" in2="SourceGraphic" operator="in"/>
|
|
762
|
+
</filter>
|
|
763
|
+
*/
|
|
764
|
+
|
|
765
|
+
var blended = 0;
|
|
766
|
+
gradients.forEach(function (gradient, i) {
|
|
767
|
+
var gradientId = createOrUpdateGradient(document, object, $def, $el, gradient);
|
|
768
|
+
var rectId = "".concat(gradientId, "_rect");
|
|
769
|
+
var $rect = createSVGElement('rect', document);
|
|
770
|
+
$rect.setAttribute('x', '0');
|
|
771
|
+
$rect.setAttribute('y', '0');
|
|
772
|
+
$rect.setAttribute('width', '100%');
|
|
773
|
+
$rect.setAttribute('height', '100%');
|
|
774
|
+
$rect.setAttribute('fill', "url(#".concat(gradientId, ")"));
|
|
775
|
+
$rect.id = rectId;
|
|
776
|
+
$def.appendChild($rect);
|
|
777
|
+
var $feImage = createSVGElement('feImage', document);
|
|
778
|
+
$feImage.setAttribute('href', "#".concat(rectId));
|
|
779
|
+
$feImage.setAttribute('result', "".concat(filterId, "-").concat(i));
|
|
780
|
+
$existed.appendChild($feImage);
|
|
781
|
+
if (i > 0) {
|
|
782
|
+
var $feBlend = createSVGElement('feBlend', document);
|
|
783
|
+
$feBlend.setAttribute('in', i === 1 ? "".concat(filterId, "-").concat(i - 1) : "".concat(filterId, "-blended-").concat(blended - 1));
|
|
784
|
+
$feBlend.setAttribute('in2', "".concat(filterId, "-").concat(i));
|
|
785
|
+
$feBlend.setAttribute('result', "".concat(filterId, "-blended-").concat(blended++));
|
|
786
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/CSS/blend-mode
|
|
787
|
+
$feBlend.setAttribute('mode', 'multiply');
|
|
788
|
+
$existed.appendChild($feBlend);
|
|
789
|
+
}
|
|
790
|
+
});
|
|
791
|
+
var $feComposite = createSVGElement('feComposite', document);
|
|
792
|
+
$feComposite.setAttribute('in', "".concat(filterId, "-blended-").concat(blended));
|
|
793
|
+
$feComposite.setAttribute('in2', 'SourceGraphic');
|
|
794
|
+
$feComposite.setAttribute('operator', 'in');
|
|
795
|
+
$existed.appendChild($feComposite);
|
|
796
|
+
return filterId;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
var FILTER_DROPSHADOW_PREFIX = 'g-filter-dropshadow-';
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* use SVG filters
|
|
803
|
+
* @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/filter
|
|
804
|
+
*/
|
|
805
|
+
function createOrUpdateShadow(document, $def, object, $el, name) {
|
|
806
|
+
var _object$parsedStyle = object.parsedStyle,
|
|
807
|
+
_object$parsedStyle$s = _object$parsedStyle.shadowType,
|
|
808
|
+
shadowType = _object$parsedStyle$s === void 0 ? 'outer' : _object$parsedStyle$s,
|
|
809
|
+
shadowBlur = _object$parsedStyle.shadowBlur,
|
|
810
|
+
shadowColor = _object$parsedStyle.shadowColor,
|
|
811
|
+
shadowOffsetX = _object$parsedStyle.shadowOffsetX,
|
|
812
|
+
shadowOffsetY = _object$parsedStyle.shadowOffsetY;
|
|
813
|
+
var hasShadow = !isNil(shadowColor) && shadowBlur > 0;
|
|
814
|
+
var shadowId = FILTER_DROPSHADOW_PREFIX + object.entity;
|
|
815
|
+
var $existedFilter = $def.querySelector("#".concat(shadowId));
|
|
816
|
+
if ($existedFilter) {
|
|
817
|
+
var existedShadowType = $existedFilter.getAttribute('data-type');
|
|
818
|
+
if (existedShadowType !== shadowType || !hasShadow) {
|
|
819
|
+
// remove existed shadow
|
|
820
|
+
$existedFilter.remove();
|
|
821
|
+
$existedFilter = null;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// <Group> also has shadowType as its default value
|
|
826
|
+
// only apply shadow when blur > 0
|
|
827
|
+
if (hasShadow) {
|
|
828
|
+
// use filter <feDropShadow>
|
|
829
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feDropShadow
|
|
830
|
+
$el === null || $el === void 0 || $el.setAttribute('filter', "url(#".concat(shadowId, ")"));
|
|
831
|
+
} else {
|
|
832
|
+
$el === null || $el === void 0 || $el.removeAttribute('filter');
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
if (!$existedFilter) {
|
|
836
|
+
$existedFilter = createSVGElement('filter', document);
|
|
837
|
+
$existedFilter.setAttribute('data-type', shadowType);
|
|
838
|
+
if (shadowType === 'outer') {
|
|
839
|
+
var $feDropShadow = createSVGElement('feDropShadow', document);
|
|
840
|
+
$feDropShadow.setAttribute('dx', "".concat((shadowOffsetX || 0) / 2));
|
|
841
|
+
$feDropShadow.setAttribute('dy', "".concat((shadowOffsetY || 0) / 2));
|
|
842
|
+
$feDropShadow.setAttribute('stdDeviation', "".concat((shadowBlur || 0) / 4));
|
|
843
|
+
$feDropShadow.setAttribute('flood-color', shadowColor.toString());
|
|
844
|
+
$existedFilter.appendChild($feDropShadow);
|
|
845
|
+
} else if (shadowType === 'inner') {
|
|
846
|
+
var $feComponentTransfer = createSVGElement('feComponentTransfer', document);
|
|
847
|
+
$feComponentTransfer.setAttribute('in', 'SourceAlpha');
|
|
848
|
+
var $feFuncA = createSVGElement('feFuncA', document);
|
|
849
|
+
$feFuncA.setAttribute('type', 'table');
|
|
850
|
+
$feFuncA.setAttribute('tableValues', '1 0');
|
|
851
|
+
$feComponentTransfer.appendChild($feFuncA);
|
|
852
|
+
$existedFilter.appendChild($feComponentTransfer);
|
|
853
|
+
var $feGaussianBlur = createSVGElement('feGaussianBlur', document);
|
|
854
|
+
$feGaussianBlur.setAttribute('stdDeviation', "".concat((shadowBlur || 0) / 4));
|
|
855
|
+
$existedFilter.appendChild($feGaussianBlur);
|
|
856
|
+
var $feOffset = createSVGElement('feOffset', document);
|
|
857
|
+
$feOffset.setAttribute('dx', "".concat((shadowOffsetX || 0) / 2));
|
|
858
|
+
$feOffset.setAttribute('dy', "".concat((shadowOffsetY || 0) / 2));
|
|
859
|
+
$feOffset.setAttribute('result', 'offsetblur');
|
|
860
|
+
$existedFilter.appendChild($feOffset);
|
|
861
|
+
var $feFlood = createSVGElement('feFlood', document);
|
|
862
|
+
$feFlood.setAttribute('flood-color', shadowColor.toString());
|
|
863
|
+
$feFlood.setAttribute('result', 'color');
|
|
864
|
+
$existedFilter.appendChild($feFlood);
|
|
865
|
+
var $feComposite = createSVGElement('feComposite', document);
|
|
866
|
+
$feComposite.setAttribute('in2', 'offsetblur');
|
|
867
|
+
$feComposite.setAttribute('operator', 'in');
|
|
868
|
+
$existedFilter.appendChild($feComposite);
|
|
869
|
+
var $feComposite2 = createSVGElement('feComposite', document);
|
|
870
|
+
$feComposite2.setAttribute('in2', 'SourceAlpha');
|
|
871
|
+
$feComposite2.setAttribute('operator', 'in');
|
|
872
|
+
$existedFilter.appendChild($feComposite2);
|
|
873
|
+
var $feMerge = createSVGElement('feMerge', document);
|
|
874
|
+
$existedFilter.appendChild($feMerge);
|
|
875
|
+
var $feMergeNode = createSVGElement('feMergeNode', document);
|
|
876
|
+
$feMergeNode.setAttribute('in', 'SourceGraphic');
|
|
877
|
+
var $feMergeNode2 = createSVGElement('feMergeNode', document);
|
|
878
|
+
$feMerge.appendChild($feMergeNode);
|
|
879
|
+
$feMerge.appendChild($feMergeNode2);
|
|
880
|
+
}
|
|
881
|
+
$existedFilter.id = shadowId;
|
|
882
|
+
// @see https://github.com/antvis/g/issues/1025
|
|
883
|
+
$existedFilter.setAttribute('filterUnits', 'userSpaceOnUse');
|
|
884
|
+
$def.appendChild($existedFilter);
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
if (shadowType === 'inner') {
|
|
888
|
+
var _$feGaussianBlur = $existedFilter.children[1];
|
|
889
|
+
var _$feOffset = $existedFilter.children[2];
|
|
890
|
+
var _$feFlood = $existedFilter.children[3];
|
|
891
|
+
if (name === 'shadowColor') {
|
|
892
|
+
_$feFlood.setAttribute('flood-color', shadowColor.toString());
|
|
893
|
+
} else if (name === 'shadowBlur') {
|
|
894
|
+
// half the blur radius
|
|
895
|
+
// @see https://drafts.csswg.org/css-backgrounds/#shadow-blur
|
|
896
|
+
// @see https://css-tricks.com/breaking-css-box-shadow-vs-drop-shadow/
|
|
897
|
+
_$feGaussianBlur.setAttribute('stdDeviation', "".concat((shadowBlur || 0) / 4));
|
|
898
|
+
} else if (name === 'shadowOffsetX') {
|
|
899
|
+
_$feOffset.setAttribute('dx', "".concat((shadowOffsetX || 0) / 2));
|
|
900
|
+
} else if (name === 'shadowOffsetY') {
|
|
901
|
+
_$feOffset.setAttribute('dy', "".concat((shadowOffsetY || 0) / 2));
|
|
902
|
+
}
|
|
903
|
+
} else if (shadowType === 'outer') {
|
|
904
|
+
var _$feDropShadow = $existedFilter.children[0];
|
|
905
|
+
if (name === 'shadowColor') {
|
|
906
|
+
_$feDropShadow.setAttribute('flood-color', shadowColor.toString());
|
|
907
|
+
} else if (name === 'shadowBlur') {
|
|
908
|
+
// half the blur radius
|
|
909
|
+
// @see https://drafts.csswg.org/css-backgrounds/#shadow-blur
|
|
910
|
+
// @see https://css-tricks.com/breaking-css-box-shadow-vs-drop-shadow/
|
|
911
|
+
_$feDropShadow.setAttribute('stdDeviation', "".concat((shadowBlur || 0) / 4));
|
|
912
|
+
} else if (name === 'shadowOffsetX') {
|
|
913
|
+
_$feDropShadow.setAttribute('dx', "".concat((shadowOffsetX || 0) / 2));
|
|
914
|
+
} else if (name === 'shadowOffsetY') {
|
|
915
|
+
_$feDropShadow.setAttribute('dy', "".concat((shadowOffsetY || 0) / 2));
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
var urlRegexp = /url\("?#(.*)\)/;
|
|
921
|
+
var DefElementManager = /*#__PURE__*/function () {
|
|
922
|
+
function DefElementManager(context) {
|
|
923
|
+
_classCallCheck(this, DefElementManager);
|
|
924
|
+
this.gradientCache = {};
|
|
925
|
+
this.context = context;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* container for <gradient> <clipPath>...
|
|
930
|
+
*/
|
|
931
|
+
return _createClass(DefElementManager, [{
|
|
932
|
+
key: "getDefElement",
|
|
933
|
+
value: function getDefElement() {
|
|
934
|
+
return this.$def;
|
|
935
|
+
}
|
|
936
|
+
}, {
|
|
937
|
+
key: "init",
|
|
938
|
+
value: function init() {
|
|
939
|
+
var document = this.context.config.document;
|
|
940
|
+
var $svg = this.context.contextService.getContext();
|
|
941
|
+
this.$def = createSVGElement('defs', document);
|
|
942
|
+
$svg.appendChild(this.$def);
|
|
943
|
+
}
|
|
944
|
+
}, {
|
|
945
|
+
key: "clear",
|
|
946
|
+
value: function clear(entity) {
|
|
947
|
+
var _this = this;
|
|
948
|
+
Object.keys(this.gradientCache).forEach(function (id) {
|
|
949
|
+
_this.clearUnusedDefElement(_this.gradientCache, id, entity);
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
}, {
|
|
953
|
+
key: "clearUnusedDefElement",
|
|
954
|
+
value: function clearUnusedDefElement(cache, id, entity) {
|
|
955
|
+
if (cache[id] && cache[id].size === 1 && cache[id].has(entity)) {
|
|
956
|
+
var targetElement = this.$def.querySelector("#".concat(id));
|
|
957
|
+
if (targetElement) {
|
|
958
|
+
this.$def.removeChild(targetElement);
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
}, {
|
|
963
|
+
key: "createOrUpdateGradientAndPattern",
|
|
964
|
+
value: function createOrUpdateGradientAndPattern$1(object, $el, parsedColor, name, plugin) {
|
|
965
|
+
var _this$context$config = this.context.config,
|
|
966
|
+
doc = _this$context$config.document,
|
|
967
|
+
createImage = _this$context$config.createImage;
|
|
968
|
+
if ($el) {
|
|
969
|
+
var attributeValue = '';
|
|
970
|
+
if (isPattern(parsedColor)) {
|
|
971
|
+
// `fill: url(#${patternId})`
|
|
972
|
+
attributeValue = $el.style[name];
|
|
973
|
+
} else {
|
|
974
|
+
// `url(#${gradientId})`
|
|
975
|
+
attributeValue = $el.getAttribute(name) || '';
|
|
976
|
+
}
|
|
977
|
+
var matches = attributeValue.match(urlRegexp);
|
|
978
|
+
if (matches && matches.length > 1) {
|
|
979
|
+
this.clearUnusedDefElement(this.gradientCache, matches[1].replace('"', ''), object.entity);
|
|
980
|
+
}
|
|
981
|
+
var newDefElementId = createOrUpdateGradientAndPattern(doc || document, this.$def, object, $el, parsedColor, name, createImage, plugin);
|
|
982
|
+
if (newDefElementId) {
|
|
983
|
+
if (!this.gradientCache[newDefElementId]) {
|
|
984
|
+
this.gradientCache[newDefElementId] = new Set();
|
|
985
|
+
}
|
|
986
|
+
this.gradientCache[newDefElementId].add(object.entity);
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
}, {
|
|
991
|
+
key: "createOrUpdateShadow",
|
|
992
|
+
value: function createOrUpdateShadow$1(object, $el, name) {
|
|
993
|
+
var doc = this.context.config.document;
|
|
994
|
+
createOrUpdateShadow(doc || document, this.$def, object, $el, name);
|
|
995
|
+
}
|
|
996
|
+
}, {
|
|
997
|
+
key: "createOrUpdateFilter",
|
|
998
|
+
value: function createOrUpdateFilter$1(object, $el, filters) {
|
|
999
|
+
var doc = this.context.config.document;
|
|
1000
|
+
createOrUpdateFilter(doc || document, this.$def, object, $el, filters);
|
|
1001
|
+
}
|
|
1002
|
+
}]);
|
|
1003
|
+
}();
|
|
1004
|
+
|
|
1005
|
+
function numberToLongString(x) {
|
|
1006
|
+
return x.toFixed(6).replace('.000000', '');
|
|
1007
|
+
}
|
|
1008
|
+
function convertHTML(str) {
|
|
1009
|
+
var regex = /[&|<|>|"|']/g;
|
|
1010
|
+
return str.replace(regex, function (match) {
|
|
1011
|
+
if (match === '&') {
|
|
1012
|
+
return '&';
|
|
1013
|
+
}
|
|
1014
|
+
if (match === '<') {
|
|
1015
|
+
return '<';
|
|
1016
|
+
}
|
|
1017
|
+
if (match === '>') {
|
|
1018
|
+
return '>';
|
|
1019
|
+
}
|
|
1020
|
+
if (match === '"') {
|
|
1021
|
+
return '"';
|
|
1022
|
+
}
|
|
1023
|
+
return ''';
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
var SVG_ATTR_MAP = {
|
|
1028
|
+
opacity: 'opacity',
|
|
1029
|
+
fillStyle: 'fill',
|
|
1030
|
+
fill: 'fill',
|
|
1031
|
+
fillRule: 'fill-rule',
|
|
1032
|
+
fillOpacity: 'fill-opacity',
|
|
1033
|
+
strokeStyle: 'stroke',
|
|
1034
|
+
strokeOpacity: 'stroke-opacity',
|
|
1035
|
+
stroke: 'stroke',
|
|
1036
|
+
clipPath: 'clip-path',
|
|
1037
|
+
textPath: 'text-path',
|
|
1038
|
+
r: 'r',
|
|
1039
|
+
cx: 'cx',
|
|
1040
|
+
cy: 'cy',
|
|
1041
|
+
rx: 'rx',
|
|
1042
|
+
ry: 'ry',
|
|
1043
|
+
x: 'x',
|
|
1044
|
+
y: 'y',
|
|
1045
|
+
width: 'width',
|
|
1046
|
+
height: 'height',
|
|
1047
|
+
lineCap: 'stroke-linecap',
|
|
1048
|
+
lineJoin: 'stroke-linejoin',
|
|
1049
|
+
lineWidth: 'stroke-width',
|
|
1050
|
+
lineDash: 'stroke-dasharray',
|
|
1051
|
+
lineDashOffset: 'stroke-dashoffset',
|
|
1052
|
+
miterLimit: 'stroke-miterlimit',
|
|
1053
|
+
font: 'font',
|
|
1054
|
+
fontSize: 'font-size',
|
|
1055
|
+
fontStyle: 'font-style',
|
|
1056
|
+
fontVariant: 'font-variant',
|
|
1057
|
+
fontWeight: 'font-weight',
|
|
1058
|
+
fontFamily: 'font-family',
|
|
1059
|
+
letterSpacing: 'letter-spacing',
|
|
1060
|
+
startArrow: 'marker-start',
|
|
1061
|
+
endArrow: 'marker-end',
|
|
1062
|
+
"class": 'class',
|
|
1063
|
+
id: 'id',
|
|
1064
|
+
// style: 'style',
|
|
1065
|
+
preserveAspectRatio: 'preserveAspectRatio',
|
|
1066
|
+
visibility: 'visibility',
|
|
1067
|
+
shadowColor: 'flood-color',
|
|
1068
|
+
shadowBlur: 'stdDeviation',
|
|
1069
|
+
shadowOffsetX: 'dx',
|
|
1070
|
+
shadowOffsetY: 'dy',
|
|
1071
|
+
filter: 'filter',
|
|
1072
|
+
innerHTML: 'innerHTML',
|
|
1073
|
+
textAlign: 'text-anchor',
|
|
1074
|
+
pointerEvents: 'pointer-events'
|
|
1075
|
+
};
|
|
1076
|
+
var FORMAT_VALUE_MAP = {
|
|
1077
|
+
textAlign: {
|
|
1078
|
+
inherit: 'inherit',
|
|
1079
|
+
left: 'left',
|
|
1080
|
+
start: 'left',
|
|
1081
|
+
center: 'middle',
|
|
1082
|
+
right: 'end',
|
|
1083
|
+
end: 'end'
|
|
1084
|
+
}
|
|
1085
|
+
};
|
|
1086
|
+
var DEFAULT_VALUE_MAP = {
|
|
1087
|
+
textAlign: 'inherit',
|
|
1088
|
+
// textBaseline: 'alphabetic',
|
|
1089
|
+
// @see https://www.w3.org/TR/SVG/painting.html#LineCaps
|
|
1090
|
+
lineCap: 'butt',
|
|
1091
|
+
// @see https://www.w3.org/TR/SVG/painting.html#LineJoin
|
|
1092
|
+
lineJoin: 'miter',
|
|
1093
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/stroke-width
|
|
1094
|
+
lineWidth: '1px',
|
|
1095
|
+
opacity: '1',
|
|
1096
|
+
fillOpacity: '1',
|
|
1097
|
+
fillRule: 'nonzero',
|
|
1098
|
+
strokeOpacity: '1',
|
|
1099
|
+
strokeWidth: '0',
|
|
1100
|
+
strokeMiterLimit: '4',
|
|
1101
|
+
letterSpacing: '0',
|
|
1102
|
+
fontSize: 'inherit',
|
|
1103
|
+
fontFamily: 'inherit',
|
|
1104
|
+
pointerEvents: 'auto',
|
|
1105
|
+
transform: 'matrix(1,0,0,1,0,0)'
|
|
1106
|
+
};
|
|
1107
|
+
/**
|
|
1108
|
+
* G_SVG_PREFIX + nodeName + entity
|
|
1109
|
+
*
|
|
1110
|
+
* eg. g_svg_circle_345
|
|
1111
|
+
*/
|
|
1112
|
+
var G_SVG_PREFIX = 'g-svg';
|
|
1113
|
+
var CLIP_PATH_PREFIX = 'clip-path-';
|
|
1114
|
+
var TEXT_PATH_PREFIX = 'text-path-';
|
|
1115
|
+
var SVGRendererPlugin = /*#__PURE__*/function () {
|
|
1116
|
+
function SVGRendererPlugin(pluginOptions, defElementManager, context) {
|
|
1117
|
+
_classCallCheck(this, SVGRendererPlugin);
|
|
1118
|
+
/**
|
|
1119
|
+
* Will be used in svg-picker for finding relative SVG element of current DisplayObject.
|
|
1120
|
+
*/
|
|
1121
|
+
this.svgElementMap = new WeakMap();
|
|
1122
|
+
/**
|
|
1123
|
+
* render at the end of frame
|
|
1124
|
+
*/
|
|
1125
|
+
this.renderQueue = [];
|
|
1126
|
+
/**
|
|
1127
|
+
* dirty attributes at the end of frame
|
|
1128
|
+
*/
|
|
1129
|
+
this.dirtyAttributes = new WeakMap();
|
|
1130
|
+
/**
|
|
1131
|
+
* reorder after mounted
|
|
1132
|
+
*/
|
|
1133
|
+
this.pendingReorderQueue = new Set();
|
|
1134
|
+
/**
|
|
1135
|
+
* <use> elements in <clipPath>, which should be sync with clipPath
|
|
1136
|
+
*
|
|
1137
|
+
* @example
|
|
1138
|
+
* <clipPath transform="matrix(1,0,0,1,-100,-155)" id="clip-path-0-2">
|
|
1139
|
+
* <use href="#g_svg_circle_0" transform="matrix(1.477115,0,0,1.477115,150,150)">
|
|
1140
|
+
* </use>
|
|
1141
|
+
* </clipPath>
|
|
1142
|
+
*/
|
|
1143
|
+
this.clipPathUseMap = new WeakMap();
|
|
1144
|
+
this.pluginOptions = pluginOptions;
|
|
1145
|
+
this.defElementManager = defElementManager;
|
|
1146
|
+
this.context = context;
|
|
1147
|
+
}
|
|
1148
|
+
return _createClass(SVGRendererPlugin, [{
|
|
1149
|
+
key: "apply",
|
|
1150
|
+
value: function apply(context) {
|
|
1151
|
+
var _this = this;
|
|
1152
|
+
var renderingService = context.renderingService,
|
|
1153
|
+
renderingContext = context.renderingContext;
|
|
1154
|
+
this.context = context;
|
|
1155
|
+
// @ts-ignore
|
|
1156
|
+
this.context.svgElementMap = this.svgElementMap;
|
|
1157
|
+
var canvas = renderingContext.root.ownerDocument.defaultView;
|
|
1158
|
+
var document = this.context.config.document;
|
|
1159
|
+
var handleMounted = function handleMounted(e) {
|
|
1160
|
+
var object = e.target;
|
|
1161
|
+
|
|
1162
|
+
// should remove clipPath already existed in <defs>
|
|
1163
|
+
var $useRefs = _this.clipPathUseMap.get(object);
|
|
1164
|
+
if ($useRefs) {
|
|
1165
|
+
var $def = _this.defElementManager.getDefElement();
|
|
1166
|
+
var existed = $def.querySelector("#".concat(_this.getId(object)));
|
|
1167
|
+
if (existed) {
|
|
1168
|
+
existed.remove();
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
// create SVG DOM Node
|
|
1173
|
+
_this.createSVGDom(document, object, _this.$camera);
|
|
1174
|
+
};
|
|
1175
|
+
var handleUnmounted = function handleUnmounted(e) {
|
|
1176
|
+
var object = e.target;
|
|
1177
|
+
_this.defElementManager.clear(object.entity);
|
|
1178
|
+
_this.clipPathUseMap["delete"](object);
|
|
1179
|
+
_this.removeSVGDom(object);
|
|
1180
|
+
};
|
|
1181
|
+
var reorderChildren = function reorderChildren(object) {
|
|
1182
|
+
var _object$parentNode;
|
|
1183
|
+
var parent = object.parentNode;
|
|
1184
|
+
// @ts-ignore
|
|
1185
|
+
var $groupEl = (_object$parentNode = object.parentNode) === null || _object$parentNode === void 0 || (_object$parentNode = _object$parentNode.elementSVG) === null || _object$parentNode === void 0 ? void 0 : _object$parentNode.$groupEl;
|
|
1186
|
+
var children = ((parent === null || parent === void 0 ? void 0 : parent.children) || []).slice();
|
|
1187
|
+
if ($groupEl) {
|
|
1188
|
+
_this.reorderChildren(document, $groupEl, children);
|
|
1189
|
+
}
|
|
1190
|
+
};
|
|
1191
|
+
var handleReparent = function handleReparent(e) {
|
|
1192
|
+
var object = e.target;
|
|
1193
|
+
reorderChildren(object);
|
|
1194
|
+
};
|
|
1195
|
+
var handleAttributeChanged = function handleAttributeChanged(e) {
|
|
1196
|
+
var object = e.target;
|
|
1197
|
+
|
|
1198
|
+
// @see https://github.com/antvis/g/issues/994
|
|
1199
|
+
// @ts-ignore
|
|
1200
|
+
if (!object.elementSVG) {
|
|
1201
|
+
return;
|
|
1202
|
+
}
|
|
1203
|
+
var attrName = e.attrName;
|
|
1204
|
+
var attribtues = _this.dirtyAttributes.get(object);
|
|
1205
|
+
if (!attribtues) {
|
|
1206
|
+
_this.dirtyAttributes.set(object, []);
|
|
1207
|
+
attribtues = _this.dirtyAttributes.get(object);
|
|
1208
|
+
}
|
|
1209
|
+
attribtues.push(attrName);
|
|
1210
|
+
};
|
|
1211
|
+
var handleGeometryBoundsChanged = function handleGeometryBoundsChanged(e) {
|
|
1212
|
+
var records = e.detail;
|
|
1213
|
+
var _loop = function _loop() {
|
|
1214
|
+
var record = records[i];
|
|
1215
|
+
var object = record.target;
|
|
1216
|
+
var nodes = object.nodeName === Shape.FRAGMENT ? object.childNodes : [object];
|
|
1217
|
+
nodes.forEach(function (node) {
|
|
1218
|
+
var _object$elementSVG;
|
|
1219
|
+
// @ts-ignore
|
|
1220
|
+
var $el = (_object$elementSVG = object.elementSVG) === null || _object$elementSVG === void 0 ? void 0 : _object$elementSVG.$el;
|
|
1221
|
+
var _object$parsedStyle = object.parsedStyle,
|
|
1222
|
+
fill = _object$parsedStyle.fill,
|
|
1223
|
+
stroke = _object$parsedStyle.stroke,
|
|
1224
|
+
clipPath = _object$parsedStyle.clipPath;
|
|
1225
|
+
if (fill && !isCSSRGB(fill)) {
|
|
1226
|
+
_this.defElementManager.createOrUpdateGradientAndPattern(object, $el, fill, 'fill', _this);
|
|
1227
|
+
}
|
|
1228
|
+
if (stroke && !isCSSRGB(stroke)) {
|
|
1229
|
+
_this.defElementManager.createOrUpdateGradientAndPattern(object, $el, stroke, 'stroke', _this);
|
|
1230
|
+
}
|
|
1231
|
+
if (clipPath) {
|
|
1232
|
+
var parentInvert = mat4.invert(mat4.create(), object.getWorldTransform());
|
|
1233
|
+
var clipPathId = "".concat(CLIP_PATH_PREFIX + clipPath.entity, "-").concat(object.entity);
|
|
1234
|
+
var $def = _this.defElementManager.getDefElement();
|
|
1235
|
+
var $existed = $def.querySelector("#".concat(clipPathId));
|
|
1236
|
+
if ($existed) {
|
|
1237
|
+
_this.applyTransform($existed, parentInvert);
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
});
|
|
1241
|
+
};
|
|
1242
|
+
for (var i = 0; i < records.length; i++) {
|
|
1243
|
+
_loop();
|
|
1244
|
+
}
|
|
1245
|
+
};
|
|
1246
|
+
renderingService.hooks.init.tap(SVGRendererPlugin.tag, function () {
|
|
1247
|
+
var _this$context$config = _this.context.config,
|
|
1248
|
+
background = _this$context$config.background,
|
|
1249
|
+
document = _this$context$config.document;
|
|
1250
|
+
|
|
1251
|
+
// <defs>
|
|
1252
|
+
_this.defElementManager.init();
|
|
1253
|
+
var $svg = _this.context.contextService.getContext();
|
|
1254
|
+
if (background) {
|
|
1255
|
+
$svg.style.background = background;
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/color-interpolation-filters
|
|
1259
|
+
$svg.setAttribute('color-interpolation-filters', 'sRGB');
|
|
1260
|
+
_this.$camera = createSVGElement('g', document);
|
|
1261
|
+
_this.$camera.id = "".concat(G_SVG_PREFIX, "-camera");
|
|
1262
|
+
_this.applyTransform(_this.$camera, _this.context.camera.getOrthoMatrix());
|
|
1263
|
+
$svg.appendChild(_this.$camera);
|
|
1264
|
+
canvas.addEventListener(ElementEvent.MOUNTED, handleMounted);
|
|
1265
|
+
canvas.addEventListener(ElementEvent.UNMOUNTED, handleUnmounted);
|
|
1266
|
+
canvas.addEventListener(ElementEvent.REPARENT, handleReparent);
|
|
1267
|
+
canvas.addEventListener(ElementEvent.ATTR_MODIFIED, handleAttributeChanged);
|
|
1268
|
+
canvas.addEventListener(ElementEvent.BOUNDS_CHANGED, handleGeometryBoundsChanged);
|
|
1269
|
+
});
|
|
1270
|
+
renderingService.hooks.destroy.tap(SVGRendererPlugin.tag, function () {
|
|
1271
|
+
canvas.removeEventListener(ElementEvent.MOUNTED, handleMounted);
|
|
1272
|
+
canvas.removeEventListener(ElementEvent.UNMOUNTED, handleUnmounted);
|
|
1273
|
+
canvas.removeEventListener(ElementEvent.REPARENT, handleReparent);
|
|
1274
|
+
canvas.removeEventListener(ElementEvent.ATTR_MODIFIED, handleAttributeChanged);
|
|
1275
|
+
canvas.removeEventListener(ElementEvent.BOUNDS_CHANGED, handleGeometryBoundsChanged);
|
|
1276
|
+
resetPatternCounter();
|
|
1277
|
+
});
|
|
1278
|
+
renderingService.hooks.render.tap(SVGRendererPlugin.tag, function (object) {
|
|
1279
|
+
_this.renderQueue.push(object);
|
|
1280
|
+
});
|
|
1281
|
+
renderingService.hooks.beginFrame.tap(SVGRendererPlugin.tag, function () {
|
|
1282
|
+
var doc = _this.context.config.document;
|
|
1283
|
+
if (_this.pendingReorderQueue.size) {
|
|
1284
|
+
_this.pendingReorderQueue.forEach(function (object) {
|
|
1285
|
+
var _object$elementSVG2;
|
|
1286
|
+
var children = ((object === null || object === void 0 ? void 0 : object.children) || []).slice();
|
|
1287
|
+
var $parentGroupEl = // @ts-ignore
|
|
1288
|
+
object === null || object === void 0 || (_object$elementSVG2 = object.elementSVG) === null || _object$elementSVG2 === void 0 ? void 0 : _object$elementSVG2.$groupEl;
|
|
1289
|
+
if ($parentGroupEl) {
|
|
1290
|
+
_this.reorderChildren(doc || document, $parentGroupEl, children || []);
|
|
1291
|
+
}
|
|
1292
|
+
});
|
|
1293
|
+
_this.pendingReorderQueue.clear();
|
|
1294
|
+
}
|
|
1295
|
+
});
|
|
1296
|
+
renderingService.hooks.endFrame.tap(SVGRendererPlugin.tag, function () {
|
|
1297
|
+
if (renderingContext.renderReasons.has(RenderReason.CAMERA_CHANGED)) {
|
|
1298
|
+
_this.applyTransform(_this.$camera, _this.context.camera.getOrthoMatrix());
|
|
1299
|
+
}
|
|
1300
|
+
_this.renderQueue.forEach(function (object) {
|
|
1301
|
+
var _elementSVG, _elementSVG2;
|
|
1302
|
+
var $el = (_elementSVG = object.elementSVG) === null || _elementSVG === void 0 ? void 0 : _elementSVG.$el;
|
|
1303
|
+
var $groupEl = (_elementSVG2 = object.elementSVG) === null || _elementSVG2 === void 0 ? void 0 : _elementSVG2.$groupEl;
|
|
1304
|
+
if ($el && $groupEl) {
|
|
1305
|
+
// apply local RTS transformation to <group> wrapper
|
|
1306
|
+
// account for anchor
|
|
1307
|
+
var localTransform = object.getLocalTransform();
|
|
1308
|
+
_this.applyTransform($groupEl, localTransform);
|
|
1309
|
+
|
|
1310
|
+
// clipped shapes should also be informed
|
|
1311
|
+
|
|
1312
|
+
var $useRefs = _this.clipPathUseMap.get(object);
|
|
1313
|
+
if ($useRefs && $useRefs.length) {
|
|
1314
|
+
$useRefs.forEach(function ($use) {
|
|
1315
|
+
// <clipPath transform="matrix()"><circle /></clipPath>
|
|
1316
|
+
_this.applyTransform($use, object.getWorldTransform());
|
|
1317
|
+
// const parentInvert = mat4.invert(
|
|
1318
|
+
// mat4.create(),
|
|
1319
|
+
// (object as DisplayObject).getWorldTransform(),
|
|
1320
|
+
// );
|
|
1321
|
+
// this.applyTransform($clipPath, parentInvert);
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
// finish rendering, clear dirty flag
|
|
1326
|
+
object.renderable.dirty = false;
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
// update dirty attributes
|
|
1330
|
+
var attributes = _this.dirtyAttributes.get(object);
|
|
1331
|
+
if (attributes) {
|
|
1332
|
+
attributes.forEach(function (attrName) {
|
|
1333
|
+
if (attrName === 'zIndex') {
|
|
1334
|
+
reorderChildren(object);
|
|
1335
|
+
} else if (attrName === 'increasedLineWidthForHitTesting') {
|
|
1336
|
+
_this.createOrUpdateHitArea(object, $el, $groupEl);
|
|
1337
|
+
}
|
|
1338
|
+
_this.updateAttribute(object, [attrName]);
|
|
1339
|
+
});
|
|
1340
|
+
_this.dirtyAttributes["delete"](object);
|
|
1341
|
+
}
|
|
1342
|
+
});
|
|
1343
|
+
_this.renderQueue = [];
|
|
1344
|
+
});
|
|
1345
|
+
}
|
|
1346
|
+
}, {
|
|
1347
|
+
key: "getId",
|
|
1348
|
+
value: function getId(object) {
|
|
1349
|
+
return object.id || "".concat(G_SVG_PREFIX, "-").concat(object.entity);
|
|
1350
|
+
}
|
|
1351
|
+
}, {
|
|
1352
|
+
key: "reorderChildren",
|
|
1353
|
+
value: function reorderChildren(doc, $groupEl, children) {
|
|
1354
|
+
// need to reorder parent's children
|
|
1355
|
+
children.sort(function (a, b) {
|
|
1356
|
+
return a.sortable.renderOrder - b.sortable.renderOrder;
|
|
1357
|
+
});
|
|
1358
|
+
if (children.length) {
|
|
1359
|
+
// create empty fragment
|
|
1360
|
+
var fragment = (doc || document).createDocumentFragment();
|
|
1361
|
+
children.forEach(function (child) {
|
|
1362
|
+
if (child.isConnected) {
|
|
1363
|
+
var $el = child.elementSVG.$groupEl;
|
|
1364
|
+
if ($el) {
|
|
1365
|
+
fragment.appendChild($el);
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
});
|
|
1369
|
+
$groupEl.appendChild(fragment);
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
}, {
|
|
1373
|
+
key: "applyTransform",
|
|
1374
|
+
value: function applyTransform($el, rts) {
|
|
1375
|
+
var matrix = "matrix(".concat(numberToLongString(rts[0]), ",").concat(numberToLongString(rts[1]), ",").concat(numberToLongString(rts[4]), ",").concat(numberToLongString(rts[5]), ",").concat(numberToLongString(rts[12]), ",").concat(numberToLongString(rts[13]), ")");
|
|
1376
|
+
if (matrix !== $el.getAttribute('transform')) {
|
|
1377
|
+
// use proper precision avoiding too long string in `transform`
|
|
1378
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Transformations
|
|
1379
|
+
$el.setAttribute('transform', matrix);
|
|
1380
|
+
}
|
|
1381
|
+
if (matrix === DEFAULT_VALUE_MAP.transform) {
|
|
1382
|
+
$el.removeAttribute('transform');
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
}, {
|
|
1386
|
+
key: "applyAttributes",
|
|
1387
|
+
value: function applyAttributes(object) {
|
|
1388
|
+
var elementSVG = object.elementSVG;
|
|
1389
|
+
var $el = elementSVG === null || elementSVG === void 0 ? void 0 : elementSVG.$el;
|
|
1390
|
+
var $groupEl = elementSVG === null || elementSVG === void 0 ? void 0 : elementSVG.$groupEl;
|
|
1391
|
+
if ($el && $groupEl) {
|
|
1392
|
+
var nodeName = object.nodeName,
|
|
1393
|
+
attributes = object.attributes;
|
|
1394
|
+
if (nodeName !== Shape.HTML) {
|
|
1395
|
+
$el.setAttribute('fill', 'none');
|
|
1396
|
+
}
|
|
1397
|
+
if (nodeName === Shape.IMAGE) {
|
|
1398
|
+
$el.setAttribute('preserveAspectRatio', 'none');
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
// apply attributes
|
|
1402
|
+
this.updateAttribute(object, Object.keys(attributes));
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
}, {
|
|
1406
|
+
key: "updateAttribute",
|
|
1407
|
+
value: function updateAttribute(object, attributes) {
|
|
1408
|
+
var _this2 = this;
|
|
1409
|
+
var document = this.context.config.document;
|
|
1410
|
+
var _ref = object.elementSVG,
|
|
1411
|
+
$el = _ref.$el,
|
|
1412
|
+
$hitTestingEl = _ref.$hitTestingEl;
|
|
1413
|
+
var parsedStyle = object.parsedStyle,
|
|
1414
|
+
nodeName = object.nodeName;
|
|
1415
|
+
var shouldUpdateElementAttribute = attributes.some(function (name) {
|
|
1416
|
+
return (
|
|
1417
|
+
// @ts-ignore
|
|
1418
|
+
_this2.context.SVGElementLifeCycleContribution.shouldUpdateElementAttribute(object, name)
|
|
1419
|
+
);
|
|
1420
|
+
});
|
|
1421
|
+
|
|
1422
|
+
// need re-generate path
|
|
1423
|
+
if (shouldUpdateElementAttribute && $el) {
|
|
1424
|
+
[$el, $hitTestingEl].forEach(function ($el) {
|
|
1425
|
+
if ($el) {
|
|
1426
|
+
// @ts-ignore
|
|
1427
|
+
_this2.context.SVGElementLifeCycleContribution.updateElementAttribute(object, $el, _this2.svgElementMap);
|
|
1428
|
+
}
|
|
1429
|
+
});
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
// update common attributes
|
|
1433
|
+
attributes.forEach(function (name) {
|
|
1434
|
+
var _FORMAT_VALUE_MAP$nam, _propertyMetadataCach;
|
|
1435
|
+
var usedName = SVG_ATTR_MAP[name];
|
|
1436
|
+
var computedValue = parsedStyle[name];
|
|
1437
|
+
var computedValueStr = !isNil(computedValue) && computedValue.toString();
|
|
1438
|
+
var formattedValueStr = ((_FORMAT_VALUE_MAP$nam = FORMAT_VALUE_MAP[name]) === null || _FORMAT_VALUE_MAP$nam === void 0 ? void 0 : _FORMAT_VALUE_MAP$nam[computedValueStr]) || computedValueStr;
|
|
1439
|
+
var usedValue = parsedStyle[name];
|
|
1440
|
+
var inherited = usedName && !!((_propertyMetadataCach = propertyMetadataCache[name]) !== null && _propertyMetadataCach !== void 0 && _propertyMetadataCach.inh);
|
|
1441
|
+
|
|
1442
|
+
// <foreignObject>
|
|
1443
|
+
if (nodeName === Shape.HTML) {
|
|
1444
|
+
if (name === 'fill') {
|
|
1445
|
+
$el.style.background = usedValue.toString();
|
|
1446
|
+
} else if (name === 'stroke') {
|
|
1447
|
+
$el.style['border-color'] = usedValue.toString();
|
|
1448
|
+
$el.style['border-style'] = 'solid';
|
|
1449
|
+
} else if (name === 'lineWidth') {
|
|
1450
|
+
$el.style['border-width'] = "".concat(usedValue || 0, "px");
|
|
1451
|
+
} else if (name === 'lineDash') {
|
|
1452
|
+
$el.style['border-style'] = 'dashed';
|
|
1453
|
+
} else if (name === 'innerHTML') {
|
|
1454
|
+
_this2.createOrUpdateInnerHTML(document, $el, usedValue);
|
|
1455
|
+
} else if (name === 'width' || name === 'height' || name === 'class' || name === 'x' || name === 'y') {
|
|
1456
|
+
// width & height are both required for <foreignObject> and cannot be used as style.
|
|
1457
|
+
$el.setAttribute(name, usedValue.toString());
|
|
1458
|
+
} else if (!isNil(object.style[name]) && object.style[name] !== '') {
|
|
1459
|
+
$el.style[name] = object.style[name];
|
|
1460
|
+
}
|
|
1461
|
+
} else {
|
|
1462
|
+
if (!usedName || (nodeName === Shape.GROUP || object.isCustomElement) && (inherited || usedName === 'fill' || usedName === 'stroke')) {
|
|
1463
|
+
return;
|
|
1464
|
+
}
|
|
1465
|
+
if (name === 'fill') {
|
|
1466
|
+
_this2.defElementManager.createOrUpdateGradientAndPattern(object, $el, usedValue, usedName, _this2);
|
|
1467
|
+
} else if (name === 'stroke') {
|
|
1468
|
+
_this2.defElementManager.createOrUpdateGradientAndPattern(object, $el, usedValue, usedName, _this2);
|
|
1469
|
+
} else if (name === 'clipPath') {
|
|
1470
|
+
_this2.createOrUpdateClipOrTextPath(document, usedValue, object);
|
|
1471
|
+
} else if (name === 'textPath') {
|
|
1472
|
+
_this2.createOrUpdateClipOrTextPath(document, usedValue, object, true);
|
|
1473
|
+
} else if (name === 'shadowType' || name === 'shadowColor' || name === 'shadowBlur' || name === 'shadowOffsetX' || name === 'shadowOffsetY') {
|
|
1474
|
+
_this2.defElementManager.createOrUpdateShadow(object, $el, name);
|
|
1475
|
+
} else if (name === 'filter') {
|
|
1476
|
+
_this2.defElementManager.createOrUpdateFilter(object, $el, usedValue);
|
|
1477
|
+
} else if (!isNil(computedValue)) {
|
|
1478
|
+
// use computed value so that we can use cascaded effect in SVG
|
|
1479
|
+
// ignore 'unset' and default value
|
|
1480
|
+
[$el, $hitTestingEl].forEach(function ($el) {
|
|
1481
|
+
if ($el && usedName) {
|
|
1482
|
+
if (computedValueStr !== 'unset' && computedValueStr !== DEFAULT_VALUE_MAP[name]) {
|
|
1483
|
+
$el.setAttribute(usedName, formattedValueStr);
|
|
1484
|
+
} else {
|
|
1485
|
+
$el.removeAttribute(usedName);
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
});
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
});
|
|
1492
|
+
}
|
|
1493
|
+
}, {
|
|
1494
|
+
key: "createSVGDom",
|
|
1495
|
+
value: function createSVGDom(document, object, root) {
|
|
1496
|
+
var noWrapWithGroup = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
1497
|
+
// create svg element
|
|
1498
|
+
// @ts-ignore
|
|
1499
|
+
object.elementSVG = new ElementSVG();
|
|
1500
|
+
// @ts-ignore
|
|
1501
|
+
var svgElement = object.elementSVG;
|
|
1502
|
+
|
|
1503
|
+
// use <group> as default, eg. CustomElement
|
|
1504
|
+
var $el =
|
|
1505
|
+
// @ts-ignore
|
|
1506
|
+
this.context.SVGElementLifeCycleContribution.createElement(object, this.svgElementMap);
|
|
1507
|
+
if ($el) {
|
|
1508
|
+
var _object$parentNode$el;
|
|
1509
|
+
var $groupEl;
|
|
1510
|
+
|
|
1511
|
+
// save $el on parsedStyle, which will be returned in getDomElement()
|
|
1512
|
+
if (object.nodeName === Shape.HTML) {
|
|
1513
|
+
object.parsedStyle.$el = $el;
|
|
1514
|
+
}
|
|
1515
|
+
if (this.pluginOptions.outputSVGElementId) {
|
|
1516
|
+
// use user-defined id first.
|
|
1517
|
+
$el.id = this.getId(object);
|
|
1518
|
+
}
|
|
1519
|
+
if (this.pluginOptions.outputSVGElementName && object.name) {
|
|
1520
|
+
$el.setAttribute('name', object.name);
|
|
1521
|
+
}
|
|
1522
|
+
if (($el.hasAttribute('data-wrapgroup') || $el.nodeName !== 'g') && !noWrapWithGroup) {
|
|
1523
|
+
$groupEl = createSVGElement('g', document);
|
|
1524
|
+
// if (this.pluginOptions.outputSVGElementId) {
|
|
1525
|
+
// $groupEl.id = $el.id + '-g';
|
|
1526
|
+
// }
|
|
1527
|
+
$groupEl.appendChild($el);
|
|
1528
|
+
} else {
|
|
1529
|
+
$groupEl = $el;
|
|
1530
|
+
}
|
|
1531
|
+
svgElement.$el = $el;
|
|
1532
|
+
svgElement.$groupEl = $groupEl;
|
|
1533
|
+
|
|
1534
|
+
// apply attributes at first time
|
|
1535
|
+
this.applyAttributes(object);
|
|
1536
|
+
|
|
1537
|
+
// create hitArea if necessary
|
|
1538
|
+
this.createOrUpdateHitArea(object, $el, $groupEl);
|
|
1539
|
+
var $parentGroupEl = root ||
|
|
1540
|
+
// @ts-ignore
|
|
1541
|
+
object.parentNode && ((_object$parentNode$el = object.parentNode.elementSVG) === null || _object$parentNode$el === void 0 ? void 0 : _object$parentNode$el.$groupEl);
|
|
1542
|
+
if ($parentGroupEl) {
|
|
1543
|
+
$parentGroupEl.appendChild($groupEl);
|
|
1544
|
+
|
|
1545
|
+
// need reorder children later
|
|
1546
|
+
this.pendingReorderQueue.add(object.parentNode);
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
}, {
|
|
1551
|
+
key: "removeSVGDom",
|
|
1552
|
+
value: function removeSVGDom(object) {
|
|
1553
|
+
var _object$elementSVG3;
|
|
1554
|
+
// @ts-ignore
|
|
1555
|
+
var $groupEl = (_object$elementSVG3 = object.elementSVG) === null || _object$elementSVG3 === void 0 ? void 0 : _object$elementSVG3.$groupEl;
|
|
1556
|
+
if ($groupEl && $groupEl.parentNode) {
|
|
1557
|
+
$groupEl.parentNode.removeChild($groupEl);
|
|
1558
|
+
|
|
1559
|
+
// @ts-ignore
|
|
1560
|
+
this.context.SVGElementLifeCycleContribution.destroyElement(object, $groupEl);
|
|
1561
|
+
// object.entity.removeComponent(ElementSVG, true);
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
}, {
|
|
1565
|
+
key: "createOrUpdateHitArea",
|
|
1566
|
+
value: function createOrUpdateHitArea(object, $el, $groupEl) {
|
|
1567
|
+
var svgElement = object.elementSVG;
|
|
1568
|
+
var $hitTestingEl = svgElement.$hitTestingEl;
|
|
1569
|
+
var increasedLineWidthForHitTesting = Number(object.parsedStyle.increasedLineWidthForHitTesting);
|
|
1570
|
+
|
|
1571
|
+
// account for hitArea
|
|
1572
|
+
if (increasedLineWidthForHitTesting) {
|
|
1573
|
+
if (!$hitTestingEl) {
|
|
1574
|
+
$hitTestingEl = $el.cloneNode();
|
|
1575
|
+
// clear attributes like `filter` `font-size`
|
|
1576
|
+
['filter'].forEach(function (attribute) {
|
|
1577
|
+
$hitTestingEl.removeAttribute(attribute);
|
|
1578
|
+
});
|
|
1579
|
+
// hitArea should be 'transparent' but not 'none'
|
|
1580
|
+
var hasFill = $el.getAttribute('fill') !== 'none';
|
|
1581
|
+
$hitTestingEl.setAttribute('fill', hasFill ? 'transparent' : 'none');
|
|
1582
|
+
$hitTestingEl.setAttribute('stroke', 'transparent');
|
|
1583
|
+
$groupEl.appendChild($hitTestingEl);
|
|
1584
|
+
svgElement.$hitTestingEl = $hitTestingEl;
|
|
1585
|
+
|
|
1586
|
+
// svg-picker will use this map to find target object
|
|
1587
|
+
this.svgElementMap.set($hitTestingEl, object);
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
// increase interactive line width
|
|
1591
|
+
$hitTestingEl.setAttribute('stroke-width', "".concat(increasedLineWidthForHitTesting + object.parsedStyle.lineWidth));
|
|
1592
|
+
} else if ($hitTestingEl) {
|
|
1593
|
+
$groupEl.removeChild($hitTestingEl);
|
|
1594
|
+
svgElement.$hitTestingEl = null;
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
}, {
|
|
1598
|
+
key: "createOrUpdateInnerHTML",
|
|
1599
|
+
value: function createOrUpdateInnerHTML(doc, $el, usedValue) {
|
|
1600
|
+
var $div = (doc || document).createElement('div');
|
|
1601
|
+
if (typeof usedValue === 'string') {
|
|
1602
|
+
$div.innerHTML = usedValue;
|
|
1603
|
+
} else {
|
|
1604
|
+
$div.appendChild(usedValue);
|
|
1605
|
+
}
|
|
1606
|
+
$el.innerHTML = '';
|
|
1607
|
+
$el.appendChild($div);
|
|
1608
|
+
}
|
|
1609
|
+
}, {
|
|
1610
|
+
key: "createOrUpdateClipOrTextPath",
|
|
1611
|
+
value: function createOrUpdateClipOrTextPath(document, clipPath, object) {
|
|
1612
|
+
var isTextPath = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
1613
|
+
var _ref2 = object.elementSVG,
|
|
1614
|
+
$groupEl = _ref2.$groupEl;
|
|
1615
|
+
var PREFIX = isTextPath ? TEXT_PATH_PREFIX : CLIP_PATH_PREFIX;
|
|
1616
|
+
var attributeNameCamel = isTextPath ? 'g' : 'clipPath';
|
|
1617
|
+
var attributeNameHyphen = isTextPath ? 'text-path' : 'clip-path';
|
|
1618
|
+
if (clipPath) {
|
|
1619
|
+
var clipPathId = "".concat(PREFIX + clipPath.entity, "-").concat(object.entity);
|
|
1620
|
+
var $def = this.defElementManager.getDefElement();
|
|
1621
|
+
var existed = $def.querySelector("#".concat(clipPathId));
|
|
1622
|
+
if (!existed) {
|
|
1623
|
+
var $clipPath;
|
|
1624
|
+
if (isTextPath) {
|
|
1625
|
+
// use <path> directly instead of wrapping with <g>
|
|
1626
|
+
this.createSVGDom(document, clipPath, null, true);
|
|
1627
|
+
// @ts-ignore
|
|
1628
|
+
$clipPath = clipPath.elementSVG.$el;
|
|
1629
|
+
} else {
|
|
1630
|
+
// the clipPath is allowed to be detached from canvas
|
|
1631
|
+
if (!clipPath.isConnected) {
|
|
1632
|
+
var $existedClipPath = $def.querySelector("#".concat(this.getId(clipPath)));
|
|
1633
|
+
if (!$existedClipPath) {
|
|
1634
|
+
this.createSVGDom(document, clipPath, $def, true);
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
// create <clipPath> dom node
|
|
1639
|
+
$clipPath = createSVGElement(attributeNameCamel, document);
|
|
1640
|
+
var $use = createSVGElement('use', document);
|
|
1641
|
+
// @ts-ignore
|
|
1642
|
+
$use.setAttribute('href', "#".concat(clipPath.elementSVG.$el.id));
|
|
1643
|
+
$clipPath.appendChild($use);
|
|
1644
|
+
var $useRefs = this.clipPathUseMap.get(clipPath);
|
|
1645
|
+
if (!$useRefs) {
|
|
1646
|
+
this.clipPathUseMap.set(clipPath, []);
|
|
1647
|
+
$useRefs = this.clipPathUseMap.get(clipPath);
|
|
1648
|
+
}
|
|
1649
|
+
$useRefs.push($use);
|
|
1650
|
+
|
|
1651
|
+
// <clipPath transform="matrix()"><circle /></clipPath>
|
|
1652
|
+
this.applyTransform($use, clipPath.getWorldTransform());
|
|
1653
|
+
var parentInvert = mat4.invert(mat4.create(), object.getWorldTransform());
|
|
1654
|
+
this.applyTransform($clipPath, parentInvert);
|
|
1655
|
+
}
|
|
1656
|
+
if (this.pluginOptions.outputSVGElementId) {
|
|
1657
|
+
$clipPath.id = clipPathId;
|
|
1658
|
+
}
|
|
1659
|
+
// append it to <defs>
|
|
1660
|
+
$def.appendChild($clipPath);
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
// apply attributes
|
|
1664
|
+
this.applyAttributes(clipPath);
|
|
1665
|
+
if (!isTextPath) {
|
|
1666
|
+
// apply clipPath to $group
|
|
1667
|
+
// @see https://github.com/antvis/g/issues/961
|
|
1668
|
+
$groupEl.setAttribute(attributeNameHyphen, "url(#".concat(clipPathId, ")"));
|
|
1669
|
+
}
|
|
1670
|
+
} else if (!isTextPath) {
|
|
1671
|
+
// remove clip path
|
|
1672
|
+
$groupEl.removeAttribute(attributeNameHyphen);
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
}]);
|
|
1676
|
+
}();
|
|
1677
|
+
SVGRendererPlugin.tag = 'SVGRenderer';
|
|
1678
|
+
|
|
1679
|
+
// @see https://github.com/plouc/nivo/issues/164
|
|
1680
|
+
var BASELINE_MAP = {
|
|
1681
|
+
top: 'hanging',
|
|
1682
|
+
// Use hanging here.
|
|
1683
|
+
middle: 'central',
|
|
1684
|
+
bottom: 'text-after-edge',
|
|
1685
|
+
// FIXME: It is not a standard property.
|
|
1686
|
+
alphabetic: 'alphabetic',
|
|
1687
|
+
ideographic: 'ideographic',
|
|
1688
|
+
hanging: 'hanging'
|
|
1689
|
+
};
|
|
1690
|
+
function updateTextElementAttribute($el, parsedStyle, text, runtime) {
|
|
1691
|
+
// Trigger text geometry calculation.
|
|
1692
|
+
text.getBounds();
|
|
1693
|
+
var _parsedStyle$lineWidt = parsedStyle.lineWidth,
|
|
1694
|
+
lineWidth = _parsedStyle$lineWidt === void 0 ? 1 : _parsedStyle$lineWidt,
|
|
1695
|
+
_parsedStyle$x = parsedStyle.x,
|
|
1696
|
+
x = _parsedStyle$x === void 0 ? 0 : _parsedStyle$x,
|
|
1697
|
+
_parsedStyle$y = parsedStyle.y,
|
|
1698
|
+
y = _parsedStyle$y === void 0 ? 0 : _parsedStyle$y,
|
|
1699
|
+
_parsedStyle$dx = parsedStyle.dx,
|
|
1700
|
+
dx = _parsedStyle$dx === void 0 ? 0 : _parsedStyle$dx,
|
|
1701
|
+
_parsedStyle$dy = parsedStyle.dy,
|
|
1702
|
+
dy = _parsedStyle$dy === void 0 ? 0 : _parsedStyle$dy,
|
|
1703
|
+
textPath = parsedStyle.textPath,
|
|
1704
|
+
_parsedStyle$textPath = parsedStyle.textPathSide,
|
|
1705
|
+
textPathSide = _parsedStyle$textPath === void 0 ? 'left' : _parsedStyle$textPath,
|
|
1706
|
+
_parsedStyle$textPath2 = parsedStyle.textPathStartOffset,
|
|
1707
|
+
textPathStartOffset = _parsedStyle$textPath2 === void 0 ? 0 : _parsedStyle$textPath2,
|
|
1708
|
+
_parsedStyle$textDeco = parsedStyle.textDecorationLine,
|
|
1709
|
+
textDecorationLine = _parsedStyle$textDeco === void 0 ? '' : _parsedStyle$textDeco,
|
|
1710
|
+
_parsedStyle$textDeco2 = parsedStyle.textDecorationColor,
|
|
1711
|
+
textDecorationColor = _parsedStyle$textDeco2 === void 0 ? '' : _parsedStyle$textDeco2,
|
|
1712
|
+
_parsedStyle$textDeco3 = parsedStyle.textDecorationStyle,
|
|
1713
|
+
textDecorationStyle = _parsedStyle$textDeco3 === void 0 ? '' : _parsedStyle$textDeco3,
|
|
1714
|
+
_parsedStyle$textDeco4 = parsedStyle.textDecorationThickness,
|
|
1715
|
+
textDecorationThickness = _parsedStyle$textDeco4 === void 0 ? 1 : _parsedStyle$textDeco4,
|
|
1716
|
+
metrics = parsedStyle.metrics;
|
|
1717
|
+
var _parsedStyle$textBase = parsedStyle.textBaseline,
|
|
1718
|
+
textBaseline = _parsedStyle$textBase === void 0 ? 'alphabetic' : _parsedStyle$textBase;
|
|
1719
|
+
if (textBaseline === 'alphabetic') {
|
|
1720
|
+
textBaseline = 'bottom';
|
|
1721
|
+
}
|
|
1722
|
+
$el.setAttribute('dominant-baseline', BASELINE_MAP[textBaseline]);
|
|
1723
|
+
$el.setAttribute('paint-order', 'stroke');
|
|
1724
|
+
var lines = metrics.lines,
|
|
1725
|
+
lineHeight = metrics.lineHeight,
|
|
1726
|
+
height = metrics.height;
|
|
1727
|
+
var lineNum = lines.length;
|
|
1728
|
+
var styleCSSText = "transform:translate(".concat(dx, "px, ").concat(dy, "px);");
|
|
1729
|
+
if (textDecorationLine && textDecorationLine !== 'none') {
|
|
1730
|
+
// Use CSS text-decoration for basic support
|
|
1731
|
+
styleCSSText += "text-decoration:".concat(textDecorationLine, " ").concat(textDecorationStyle, " ").concat(textDecorationColor, ";text-decoration-thickness:").concat(textDecorationThickness, ";");
|
|
1732
|
+
}
|
|
1733
|
+
if (styleCSSText) {
|
|
1734
|
+
$el.setAttribute('style', styleCSSText);
|
|
1735
|
+
}
|
|
1736
|
+
if (x !== 0) {
|
|
1737
|
+
$el.setAttribute('x', "".concat(x));
|
|
1738
|
+
}
|
|
1739
|
+
if (y !== 0) {
|
|
1740
|
+
$el.setAttribute('y', "".concat(y));
|
|
1741
|
+
}
|
|
1742
|
+
if (lineNum === 1) {
|
|
1743
|
+
var textContent = convertHTML(lines[0]);
|
|
1744
|
+
$el.setAttribute('dx', "".concat(lineWidth / 2));
|
|
1745
|
+
|
|
1746
|
+
// Since `text-after-edge` is not a standard property value, we use `dy` instead.
|
|
1747
|
+
if (textBaseline === 'bottom' || textBaseline === 'top') {
|
|
1748
|
+
$el.setAttribute('dominant-baseline', BASELINE_MAP.middle);
|
|
1749
|
+
$el.setAttribute('dy', textBaseline === 'bottom' ? "-".concat(height / 2, "px") : "".concat(height / 2, "px"));
|
|
1750
|
+
} else {
|
|
1751
|
+
$el.setAttribute('dy', '0px');
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
// <textPath> only support one line
|
|
1755
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/textPath
|
|
1756
|
+
if (textPath) {
|
|
1757
|
+
// clear existed text content first
|
|
1758
|
+
$el.innerHTML = '';
|
|
1759
|
+
|
|
1760
|
+
// append <textPath href="#MyPath">text</textPath>
|
|
1761
|
+
var $textPath = createSVGElement('textPath', $el.ownerDocument);
|
|
1762
|
+
$textPath.setAttribute('href', "#".concat(TEXT_PATH_PREFIX + textPath.entity));
|
|
1763
|
+
if (textPathSide !== 'left') {
|
|
1764
|
+
$textPath.setAttribute('side', textPathSide);
|
|
1765
|
+
}
|
|
1766
|
+
if (textPathStartOffset !== 0) {
|
|
1767
|
+
$textPath.setAttribute('startOffset', "".concat(textPathStartOffset));
|
|
1768
|
+
}
|
|
1769
|
+
$textPath.innerHTML = textContent;
|
|
1770
|
+
$el.appendChild($textPath);
|
|
1771
|
+
} else {
|
|
1772
|
+
$el.innerHTML = textContent;
|
|
1773
|
+
}
|
|
1774
|
+
} else {
|
|
1775
|
+
$el.innerHTML = lines.map(function (line, i) {
|
|
1776
|
+
var dx = lineWidth / 2;
|
|
1777
|
+
var dy = 0;
|
|
1778
|
+
if (i === 0) {
|
|
1779
|
+
// TODO: handle other textBaseline values
|
|
1780
|
+
if (textBaseline === 'middle') {
|
|
1781
|
+
dy = lineHeight / 2 - height / 2;
|
|
1782
|
+
} else if (textBaseline === 'top' || textBaseline === 'hanging') {
|
|
1783
|
+
dy = 0;
|
|
1784
|
+
} else if (textBaseline === 'bottom' || textBaseline === 'ideographic') {
|
|
1785
|
+
dy = -lineHeight * (lineNum - 1);
|
|
1786
|
+
}
|
|
1787
|
+
} else {
|
|
1788
|
+
dy = lineHeight;
|
|
1789
|
+
}
|
|
1790
|
+
return "<tspan x=".concat(x, " dx=\"").concat(dx, "\" dy=\"").concat(dy, "\">").concat(convertHTML(line), "</tspan>");
|
|
1791
|
+
}).join('');
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
var _SHAPE2TAGS;
|
|
1796
|
+
var SHAPE2TAGS = (_SHAPE2TAGS = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_SHAPE2TAGS, Shape.RECT, 'path'), Shape.CIRCLE, 'circle'), Shape.ELLIPSE, 'ellipse'), Shape.IMAGE, 'image'), Shape.GROUP, 'g'), Shape.LINE, 'line'), Shape.POLYLINE, 'polyline'), Shape.POLYGON, 'polygon'), Shape.TEXT, 'text'), Shape.PATH, 'path'), _defineProperty(_SHAPE2TAGS, Shape.HTML, 'foreignObject'));
|
|
1797
|
+
var SHAPE_UPDATE_DEPS = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, Shape.CIRCLE, ['cx', 'cy', 'r']), Shape.ELLIPSE, ['cx', 'cy', 'rx', 'ry']), Shape.RECT, ['x', 'y', 'width', 'height', 'radius']), Shape.IMAGE, ['x', 'y', 'src', 'width', 'height']), Shape.LINE, ['x1', 'y1', 'x2', 'y2', 'markerStart', 'markerEnd', 'markerStartOffset', 'markerEndOffset']), Shape.POLYLINE, ['points', 'markerStart', 'markerEnd', 'markerMid', 'markerStartOffset', 'markerEndOffset']), Shape.POLYGON, ['points', 'markerStart', 'markerEnd', 'markerMid', 'markerStartOffset', 'markerEndOffset']), Shape.PATH, ['d', 'markerStart', 'markerEnd', 'markerMid', 'markerStartOffset', 'markerEndOffset']), Shape.TEXT, ['text', 'font', 'fontSize', 'fontFamily', 'fontStyle', 'fontWeight', 'fontVariant', 'lineHeight', 'letterSpacing', 'wordWrap', 'wordWrapWidth', 'maxLines', 'leading', 'textBaseline', 'textAlign', 'textTransform', 'textOverflow', 'textPath', 'textPathSide', 'textPathStartOffset', 'textDecorationLine', 'textDecorationColor', 'textDecorationStyle',
|
|
1798
|
+
// 'whiteSpace',
|
|
1799
|
+
'dx', 'dy']);
|
|
1800
|
+
var DefaultElementLifeCycleContribution = /*#__PURE__*/function () {
|
|
1801
|
+
function DefaultElementLifeCycleContribution(context, runtime) {
|
|
1802
|
+
_classCallCheck(this, DefaultElementLifeCycleContribution);
|
|
1803
|
+
this.context = context;
|
|
1804
|
+
this.runtime = runtime;
|
|
1805
|
+
}
|
|
1806
|
+
return _createClass(DefaultElementLifeCycleContribution, [{
|
|
1807
|
+
key: "createElement",
|
|
1808
|
+
value: function createElement(object, svgElementMap) {
|
|
1809
|
+
var doc = this.context.config.document;
|
|
1810
|
+
var type = SHAPE2TAGS[object.nodeName] || 'g';
|
|
1811
|
+
var $el = createSVGElement(type, doc || document);
|
|
1812
|
+
svgElementMap.set($el, object);
|
|
1813
|
+
return $el;
|
|
1814
|
+
}
|
|
1815
|
+
}, {
|
|
1816
|
+
key: "destroyElement",
|
|
1817
|
+
value: function destroyElement(object, $el) {}
|
|
1818
|
+
}, {
|
|
1819
|
+
key: "shouldUpdateElementAttribute",
|
|
1820
|
+
value: function shouldUpdateElementAttribute(object, attributeName) {
|
|
1821
|
+
var nodeName = object.nodeName;
|
|
1822
|
+
return (SHAPE_UPDATE_DEPS[nodeName] || []).indexOf(attributeName) > -1;
|
|
1823
|
+
}
|
|
1824
|
+
}, {
|
|
1825
|
+
key: "updateElementAttribute",
|
|
1826
|
+
value: function updateElementAttribute(object) {
|
|
1827
|
+
// @ts-ignore
|
|
1828
|
+
var _ref = object.elementSVG,
|
|
1829
|
+
$el = _ref.$el;
|
|
1830
|
+
var nodeName = object.nodeName,
|
|
1831
|
+
parsedStyle = object.parsedStyle;
|
|
1832
|
+
switch (nodeName) {
|
|
1833
|
+
case Shape.IMAGE:
|
|
1834
|
+
{
|
|
1835
|
+
updateImageElementAttribute($el, parsedStyle);
|
|
1836
|
+
break;
|
|
1837
|
+
}
|
|
1838
|
+
case Shape.RECT:
|
|
1839
|
+
{
|
|
1840
|
+
updateRectElementAttribute($el, parsedStyle);
|
|
1841
|
+
break;
|
|
1842
|
+
}
|
|
1843
|
+
case Shape.LINE:
|
|
1844
|
+
{
|
|
1845
|
+
updateLineElementAttribute($el, parsedStyle);
|
|
1846
|
+
break;
|
|
1847
|
+
}
|
|
1848
|
+
case Shape.POLYGON:
|
|
1849
|
+
case Shape.POLYLINE:
|
|
1850
|
+
{
|
|
1851
|
+
updatePolylineElementAttribute($el, parsedStyle);
|
|
1852
|
+
break;
|
|
1853
|
+
}
|
|
1854
|
+
case Shape.PATH:
|
|
1855
|
+
{
|
|
1856
|
+
updatePathElementAttribute($el, parsedStyle);
|
|
1857
|
+
break;
|
|
1858
|
+
}
|
|
1859
|
+
case Shape.TEXT:
|
|
1860
|
+
{
|
|
1861
|
+
updateTextElementAttribute($el, parsedStyle, object, this.runtime);
|
|
1862
|
+
break;
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
}]);
|
|
1867
|
+
}();
|
|
1868
|
+
|
|
1869
|
+
var Plugin$1 = /*#__PURE__*/function (_AbstractRendererPlug) {
|
|
1870
|
+
function Plugin() {
|
|
1871
|
+
var _this;
|
|
1872
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1873
|
+
_classCallCheck(this, Plugin);
|
|
1874
|
+
_this = _callSuper(this, Plugin);
|
|
1875
|
+
_this.name = 'svg-renderer';
|
|
1876
|
+
_this.options = options;
|
|
1877
|
+
return _this;
|
|
1878
|
+
}
|
|
1879
|
+
_inherits(Plugin, _AbstractRendererPlug);
|
|
1880
|
+
return _createClass(Plugin, [{
|
|
1881
|
+
key: "init",
|
|
1882
|
+
value: function init(runtime) {
|
|
1883
|
+
var _this$options = this.options,
|
|
1884
|
+
outputSVGElementId = _this$options.outputSVGElementId,
|
|
1885
|
+
outputSVGElementName = _this$options.outputSVGElementName;
|
|
1886
|
+
var defElementManager = new DefElementManager(this.context);
|
|
1887
|
+
|
|
1888
|
+
// default implementation
|
|
1889
|
+
var defaultElementLifeCycleContribution = new DefaultElementLifeCycleContribution(this.context, runtime);
|
|
1890
|
+
// @ts-ignore
|
|
1891
|
+
this.context.defaultElementLifeCycleContribution = defaultElementLifeCycleContribution;
|
|
1892
|
+
|
|
1893
|
+
// @ts-ignore
|
|
1894
|
+
this.context.SVGElementLifeCycleContribution = defaultElementLifeCycleContribution;
|
|
1895
|
+
var SVGRendererPluginOptions = {
|
|
1896
|
+
outputSVGElementId: !isNil(outputSVGElementId) ? !!outputSVGElementId : true,
|
|
1897
|
+
outputSVGElementName: !isNil(outputSVGElementName) ? !!outputSVGElementName : true
|
|
1898
|
+
};
|
|
1899
|
+
this.addRenderingPlugin(
|
|
1900
|
+
// @ts-ignore
|
|
1901
|
+
new SVGRendererPlugin(SVGRendererPluginOptions, defElementManager, this.context));
|
|
1902
|
+
}
|
|
1903
|
+
}, {
|
|
1904
|
+
key: "destroy",
|
|
1905
|
+
value: function destroy() {
|
|
1906
|
+
this.removeAllRenderingPlugins();
|
|
1907
|
+
|
|
1908
|
+
// @ts-ignore
|
|
1909
|
+
delete this.context.defaultElementLifeCycleContribution;
|
|
1910
|
+
// @ts-ignore
|
|
1911
|
+
delete this.context.SVGElementLifeCycleContribution;
|
|
1912
|
+
}
|
|
1913
|
+
}]);
|
|
1914
|
+
}(AbstractRendererPlugin);
|
|
1915
|
+
|
|
1916
|
+
var index$1 = /*#__PURE__*/Object.freeze({
|
|
1917
|
+
__proto__: null,
|
|
1918
|
+
CLIP_PATH_PREFIX: CLIP_PATH_PREFIX,
|
|
1919
|
+
DEFAULT_VALUE_MAP: DEFAULT_VALUE_MAP,
|
|
1920
|
+
DefaultElementLifeCycleContribution: DefaultElementLifeCycleContribution,
|
|
1921
|
+
ElementSVG: ElementSVG,
|
|
1922
|
+
G_SVG_PREFIX: G_SVG_PREFIX,
|
|
1923
|
+
Plugin: Plugin$1,
|
|
1924
|
+
SHAPE2TAGS: SHAPE2TAGS,
|
|
1925
|
+
SHAPE_UPDATE_DEPS: SHAPE_UPDATE_DEPS,
|
|
1926
|
+
SVGRendererPlugin: SVGRendererPlugin,
|
|
1927
|
+
SVG_ATTR_MAP: SVG_ATTR_MAP,
|
|
1928
|
+
TEXT_PATH_PREFIX: TEXT_PATH_PREFIX,
|
|
1929
|
+
createSVGElement: createSVGElement,
|
|
1930
|
+
updateImageElementAttribute: updateImageElementAttribute,
|
|
1931
|
+
updateLineElementAttribute: updateLineElementAttribute,
|
|
1932
|
+
updatePathElementAttribute: updatePathElementAttribute,
|
|
1933
|
+
updatePolylineElementAttribute: updatePolylineElementAttribute,
|
|
1934
|
+
updateRectElementAttribute: updateRectElementAttribute,
|
|
1935
|
+
updateTextElementAttribute: updateTextElementAttribute
|
|
1936
|
+
});
|
|
1937
|
+
|
|
1938
|
+
/**
|
|
1939
|
+
* pick shape(s) with Mouse/Touch event
|
|
1940
|
+
*
|
|
1941
|
+
* 1. find AABB with r-tree
|
|
1942
|
+
* 2. use elementFromPoint
|
|
1943
|
+
*/
|
|
1944
|
+
var SVGPickerPlugin = /*#__PURE__*/function () {
|
|
1945
|
+
function SVGPickerPlugin() {
|
|
1946
|
+
_classCallCheck(this, SVGPickerPlugin);
|
|
1947
|
+
}
|
|
1948
|
+
return _createClass(SVGPickerPlugin, [{
|
|
1949
|
+
key: "apply",
|
|
1950
|
+
value: function apply(context, runtime) {
|
|
1951
|
+
var _this = this;
|
|
1952
|
+
var doc = context.config.document,
|
|
1953
|
+
renderingService = context.renderingService,
|
|
1954
|
+
svgElementMap = context.svgElementMap;
|
|
1955
|
+
renderingService.hooks.pick.tapPromise(SVGPickerPlugin.tag, /*#__PURE__*/function () {
|
|
1956
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(result) {
|
|
1957
|
+
return _regeneratorRuntime().wrap(function (_context) {
|
|
1958
|
+
while (1) switch (_context.prev = _context.next) {
|
|
1959
|
+
case 0:
|
|
1960
|
+
return _context.abrupt("return", _this.pick(svgElementMap, doc, result));
|
|
1961
|
+
case 1:
|
|
1962
|
+
case "end":
|
|
1963
|
+
return _context.stop();
|
|
1964
|
+
}
|
|
1965
|
+
}, _callee);
|
|
1966
|
+
}));
|
|
1967
|
+
return function (_x) {
|
|
1968
|
+
return _ref.apply(this, arguments);
|
|
1969
|
+
};
|
|
1970
|
+
}());
|
|
1971
|
+
renderingService.hooks.pickSync.tap(SVGPickerPlugin.tag, function (result) {
|
|
1972
|
+
return _this.pick(svgElementMap, doc, result);
|
|
1973
|
+
});
|
|
1974
|
+
}
|
|
1975
|
+
}, {
|
|
1976
|
+
key: "pick",
|
|
1977
|
+
value: function pick(svgElementMap, doc, result) {
|
|
1978
|
+
var topmost = result.topmost,
|
|
1979
|
+
_result$position = result.position,
|
|
1980
|
+
clientX = _result$position.clientX,
|
|
1981
|
+
clientY = _result$position.clientY;
|
|
1982
|
+
try {
|
|
1983
|
+
var targets = [];
|
|
1984
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/Document/elementsFromPoint
|
|
1985
|
+
var _iterator = _createForOfIteratorHelper((doc || document).elementsFromPoint(clientX, clientY)),
|
|
1986
|
+
_step;
|
|
1987
|
+
try {
|
|
1988
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
1989
|
+
var element = _step.value;
|
|
1990
|
+
if (element.shadowRoot && element.shadowRoot !== doc) {
|
|
1991
|
+
return this.pick(svgElementMap, element.shadowRoot, result);
|
|
1992
|
+
}
|
|
1993
|
+
var target = svgElementMap.get(element);
|
|
1994
|
+
// don't need to account for `visibility` since DOM API already does
|
|
1995
|
+
if (target && target.isInteractive()) {
|
|
1996
|
+
targets.push(target);
|
|
1997
|
+
if (topmost) {
|
|
1998
|
+
result.picked = targets;
|
|
1999
|
+
return result;
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
} catch (err) {
|
|
2004
|
+
_iterator.e(err);
|
|
2005
|
+
} finally {
|
|
2006
|
+
_iterator.f();
|
|
2007
|
+
}
|
|
2008
|
+
result.picked = targets;
|
|
2009
|
+
} catch (_unused) {
|
|
2010
|
+
result.picked = [];
|
|
2011
|
+
}
|
|
2012
|
+
return result;
|
|
2013
|
+
}
|
|
2014
|
+
}]);
|
|
2015
|
+
}();
|
|
2016
|
+
SVGPickerPlugin.tag = 'SVGPicker';
|
|
2017
|
+
|
|
2018
|
+
var Plugin = /*#__PURE__*/function (_AbstractRendererPlug) {
|
|
2019
|
+
function Plugin() {
|
|
2020
|
+
var _this;
|
|
2021
|
+
_classCallCheck(this, Plugin);
|
|
2022
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2023
|
+
args[_key] = arguments[_key];
|
|
2024
|
+
}
|
|
2025
|
+
_this = _callSuper(this, Plugin, [].concat(args));
|
|
2026
|
+
_this.name = 'svg-picker';
|
|
2027
|
+
return _this;
|
|
2028
|
+
}
|
|
2029
|
+
_inherits(Plugin, _AbstractRendererPlug);
|
|
2030
|
+
return _createClass(Plugin, [{
|
|
2031
|
+
key: "init",
|
|
2032
|
+
value: function init() {
|
|
2033
|
+
this.addRenderingPlugin(new SVGPickerPlugin());
|
|
2034
|
+
}
|
|
2035
|
+
}, {
|
|
2036
|
+
key: "destroy",
|
|
2037
|
+
value: function destroy() {
|
|
2038
|
+
this.removeAllRenderingPlugins();
|
|
2039
|
+
}
|
|
2040
|
+
}]);
|
|
2041
|
+
}(AbstractRendererPlugin);
|
|
2042
|
+
|
|
2043
|
+
var index = /*#__PURE__*/Object.freeze({
|
|
2044
|
+
__proto__: null,
|
|
2045
|
+
Plugin: Plugin
|
|
2046
|
+
});
|
|
25
2047
|
|
|
26
2048
|
var _excluded = ["offset", "composite", "computedOffset", "easing"],
|
|
27
2049
|
_excluded2 = ["offset", "composite", "computedOffset", "easing", "transformOrigin"],
|
|
@@ -287,16 +2309,16 @@ var Renderer = /*#__PURE__*/function (_AbstractRenderer) {
|
|
|
287
2309
|
_classCallCheck(this, Renderer);
|
|
288
2310
|
_this = _callSuper(this, Renderer, [config]);
|
|
289
2311
|
_this.registerPlugin(new ContextRegisterPlugin());
|
|
290
|
-
_this.registerPlugin(new
|
|
2312
|
+
_this.registerPlugin(new Plugin$1({
|
|
291
2313
|
outputSVGElementId: config.outputSVGElementId
|
|
292
2314
|
}));
|
|
293
2315
|
_this.registerPlugin(new DomInteraction.Plugin());
|
|
294
|
-
_this.registerPlugin(new
|
|
2316
|
+
_this.registerPlugin(new Plugin());
|
|
295
2317
|
return _this;
|
|
296
2318
|
}
|
|
297
2319
|
_inherits(Renderer, _AbstractRenderer);
|
|
298
2320
|
return _createClass(Renderer);
|
|
299
2321
|
}(AbstractRenderer);
|
|
300
2322
|
|
|
301
|
-
export { Renderer };
|
|
2323
|
+
export { G_SVG_PREFIX, Renderer, index as SVGPicker, index$1 as SVGRenderer, createSVGElement };
|
|
302
2324
|
//# sourceMappingURL=index.esm.js.map
|