-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcreateShape.ts
More file actions
140 lines (128 loc) · 3.24 KB
/
Copy pathcreateShape.ts
File metadata and controls
140 lines (128 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {
Text,
Circle,
Ellipse,
Rect,
Path,
Image,
Line,
Polygon,
DisplayObject,
CSS,
PropertySyntax,
runtime,
} from '@antv/g-lite';
import { Arc, Marker, Sector, SmoothPolyline } from '../../shape';
import Gesture from '../../gesture';
import { getTag, registerTag } from '../../jsx/tag';
const EVENT_LIST = [
['click', 'onClick'],
['touchstart', 'onTouchStart'],
['touchmove', 'onTouchMove'],
['touchend', 'onTouchEnd'],
['touchendoutside', 'onTouchEndOutside'],
// drage 相关
['dragenter', 'onDragEnter'],
['dragleave', 'onDragLeave'],
['dragover', 'onDragOver'],
['drop', 'onDrop'],
['dragstart', 'onDragStart'],
['drag', 'onDrag'],
['dragend', 'onDragEnd'],
// pan
['panstart', 'onPanStart'],
['pan', 'onPan'],
['panend', 'onPanEnd'],
// press
['pressstart', 'onPressStart'],
['press', 'onPress'],
['pressend', 'onPressEnd'],
// swipe
['swipe', 'onSwipe'],
// pinch
['pinchstart', 'onPinchStart'],
['pinch', 'onPinch'],
['pinchend', 'onPinchEnd'],
];
// 默认标签
const TagElements = [
['group', Rect],
['text', Text],
['circle', Circle],
['path', Path],
['ellipse', Ellipse],
['rect', Rect],
['image', Image],
['line', Line],
['polyline', SmoothPolyline],
['polygon', Polygon],
['arc', Arc],
['marker', Marker],
['sector', Sector],
];
TagElements.map(([type, ShapeClass]) => {
registerTag(type as string, ShapeClass as typeof DisplayObject);
});
// 注册 css 属性,不能注册已有属性,比如 r width等
const SECTOR_CSS_PROPERTY = [
{
name: 'r0',
inherits: false,
interpolable: true,
syntax: PropertySyntax.LENGTH_PERCENTAGE,
},
{
name: 'startAngle',
inherits: false,
interpolable: true,
syntax: PropertySyntax.ANGLE,
},
{
name: 'endAngle',
inherits: false,
interpolable: true,
syntax: PropertySyntax.ANGLE,
},
];
SECTOR_CSS_PROPERTY.forEach((property) => {
CSS.registerProperty(property);
});
// https://github.com/antvis/G/releases/tag/%40antv%2Fg%406.0.0
runtime.enableCSSParsing = true;
function createShape(type: string, props) {
if (!type) return null;
const ShapeClass = getTag(type);
if (!ShapeClass) return null;
// const result = checkCSSRule(type, originStyle);
const shape = new ShapeClass(props);
// @ts-ignore
shape.gesture = addEvent(shape, props);
return shape;
}
function updateShape(shape: DisplayObject, props, lastProps) {
// @ts-ignore
const gesture = shape.gesture;
// 如果 shape 上存在 gesture,说明是 jsx 标签创建的元素,才需要更新事件
if (gesture) {
// 先清除上次 props 绑定的事件
EVENT_LIST.forEach(([eventName, handlerName]) => {
if (!lastProps[handlerName]) return;
gesture.off(eventName, lastProps[handlerName]);
});
// 绑定最新的事件
EVENT_LIST.forEach(([eventName, handlerName]) => {
if (!props[handlerName]) return;
gesture.on(eventName, props[handlerName]);
});
}
return shape;
}
function addEvent(shape, props) {
const gesture = new Gesture(shape);
EVENT_LIST.forEach(([eventName, handlerName]) => {
if (!props[handlerName]) return;
gesture.on(eventName, props[handlerName]);
});
return gesture;
}
export { createShape, updateShape, addEvent };