@vyr/service-graph 0.0.1

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.
@@ -0,0 +1,172 @@
1
+ import { Generate } from '@vyr/engine'
2
+ import { Cell, Shape } from '@antv/x6'
3
+ import { GraphDrawer } from '../GraphDrawer'
4
+
5
+ interface ClickEvent {
6
+ cell: Unit
7
+ x: number
8
+ y: number
9
+ }
10
+
11
+ interface UnitMeta {
12
+ drawer: GraphDrawer
13
+ id: string
14
+ icon: string
15
+ label: string
16
+ maxOutCount: number
17
+ maxInCount: number
18
+ status: '' | 'loading' | 'success' | 'warning'
19
+ position?: { x: number; y: number },
20
+ }
21
+
22
+ type UnitPartial<T extends UnitMeta = UnitMeta> = { drawer: GraphDrawer } & Partial<Omit<T, 'drawer'>>
23
+
24
+ class Unit extends Shape.Rect {
25
+ static width = 180
26
+ static height = 45
27
+ static edge = { line: { stroke: '#A2B1C3', strokeWidth: 2, }, highlight: { stroke: '#feb663', } }
28
+ static attrs = {
29
+ card: { refWidth: '100%', refHeight: '100%', fill: 'transparent', rx: 4, ry: 4, },
30
+ html: { refWidth: '100%', refHeight: '100%', x: 0, y: 0, },
31
+ }
32
+ static instanceOf(instance: Cell, list: Array<typeof Unit>) {
33
+ for (const item of list) {
34
+ if (instance instanceof item) return true
35
+ }
36
+ return false
37
+ }
38
+ static getPortMeta(position: string) {
39
+ return {
40
+ position,
41
+ attrs: {
42
+ circle: {
43
+ r: 4,
44
+ magnet: true,
45
+ stroke: '#31d0c6',
46
+ strokeWidth: 1,
47
+ fill: '#fff',
48
+ },
49
+ },
50
+ }
51
+ }
52
+ static formatName(name: string) {
53
+ const len = 10
54
+ if (!name || name.length <= len) return name
55
+
56
+ return name.substring(0, len) + '...'
57
+ }
58
+
59
+ vMeta: UnitMeta
60
+
61
+ constructor(vMeta: UnitPartial<UnitMeta>) {
62
+ super({ id: vMeta.id ?? Generate.uuid(), label: vMeta.label, position: vMeta.position })
63
+ this.vMeta = {
64
+ drawer: vMeta.drawer,
65
+ icon: vMeta.icon ?? '',
66
+ label: vMeta.label ?? '',
67
+ status: vMeta.status ?? '',
68
+ maxOutCount: vMeta.maxOutCount ?? 1,
69
+ maxInCount: vMeta.maxInCount ?? 1,
70
+ id: this.id,
71
+ }
72
+ this.htmlFactory()
73
+ }
74
+
75
+ clone() {
76
+ const CLass = this.constructor as typeof Unit
77
+
78
+ return new CLass({ ...this.vMeta, id: undefined }) as any
79
+ }
80
+
81
+ htmlFactory() {
82
+ if (!this.attrs) return
83
+ const div = `
84
+ <div class="graph-service-unit">
85
+ <i class="${this.vMeta.icon} unit-icon" ></i>
86
+ <div class="unit-wrapper">${this.label}</div>
87
+ <i class="graphfont graph-${this.vMeta.status} unit-icon" ></i>
88
+ </div>
89
+ `
90
+ this.setAttrs({ html: { html: div } })
91
+ }
92
+
93
+ setVMeta(meta: Partial<UnitMeta>) {
94
+ const keys = Object.keys(meta) as Array<keyof UnitMeta>
95
+ for (const key of keys) {
96
+ const value = meta[key]
97
+ if (value === undefined) continue
98
+ //@ts-ignore
99
+ this.vMeta[key] = value
100
+ }
101
+ this.setLabel(meta.label)
102
+ if (meta.position) this.setPosition(meta.position)
103
+ this.htmlFactory()
104
+ }
105
+
106
+ /**点击节点 */
107
+ click(e: ClickEvent) {
108
+ //
109
+ }
110
+ /**右键点击节点 */
111
+ rightClick(e: ClickEvent) {
112
+ //
113
+ }
114
+ /**节点是否可移动 */
115
+ isMove() {
116
+ return true
117
+ }
118
+ /**节点是否可生成链接边 */
119
+ isCreateEdge(e: Element) {
120
+ if (e.getAttribute('port-group') !== 'out') return false
121
+ const edges = this.vMeta.drawer.graph.getConnectedEdges(this, { outgoing: true })
122
+ return edges.length < this.vMeta.maxOutCount
123
+ }
124
+ /**是否可以连接该节点 */
125
+ isConnection(e: Element, source: Cell) {
126
+ if (e.getAttribute('port-group') !== 'in') return false
127
+ const edges = this.vMeta.drawer.graph.getConnectedEdges(this, { incoming: true })
128
+ return edges.length < this.vMeta.maxInCount
129
+ }
130
+ }
131
+ Unit.config({
132
+ width: Unit.width,
133
+ height: Unit.height,
134
+ position: { x: 0, y: 0 },
135
+ attrs: {
136
+ card: Unit.attrs.card,
137
+ html: Unit.attrs.html,
138
+ },
139
+ markup: [
140
+ {
141
+ tagName: 'rect',
142
+ selector: 'card',
143
+ },
144
+ {
145
+ tagName: 'foreignObject',
146
+ selector: 'html',
147
+ }
148
+ ],
149
+ ports: {
150
+ groups: {
151
+ in: Unit.getPortMeta('left'),
152
+ out: Unit.getPortMeta('right'),
153
+ },
154
+ items: [
155
+ {
156
+ id: 'in',
157
+ group: 'in',
158
+ },
159
+ {
160
+ id: 'out',
161
+ group: 'out',
162
+ },
163
+ ],
164
+ },
165
+ })
166
+
167
+ export {
168
+ ClickEvent,
169
+ UnitMeta,
170
+ UnitPartial,
171
+ Unit,
172
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ import './asset/font/iconfont.css'
2
+ import './asset/less/index.less'
3
+ export * from './locale'
4
+ export * from './common/Unit'
5
+ export * from './common/RoutineUnit'
6
+ export * from './GraphDrawer'
7
+ export * from './GraphService'
@@ -0,0 +1,10 @@
1
+ import { Locale } from "@vyr/locale";
2
+ import { zhCnLanguageProvider, ZhCNLanguageProvider } from "./LanguageProvider";
3
+
4
+ Locale.register(zhCnLanguageProvider)
5
+
6
+ const language = Locale.getLanguage<ZhCNLanguageProvider>(zhCnLanguageProvider.name)
7
+
8
+ export {
9
+ language
10
+ }
@@ -0,0 +1,16 @@
1
+ import { LanguageProvider } from '@vyr/locale'
2
+
3
+ interface ZhCNLanguageProvider extends LanguageProvider {
4
+ 'graphDrawer.stencil.title': string
5
+ }
6
+
7
+ const zhCnLanguageProvider: ZhCNLanguageProvider = {
8
+ id: 'zh_CN',
9
+ name: '@vyr/service-graph',
10
+ 'graphDrawer.stencil.title': '例程节点',
11
+ }
12
+
13
+ export {
14
+ ZhCNLanguageProvider,
15
+ zhCnLanguageProvider,
16
+ }
@@ -0,0 +1,2 @@
1
+ export * from './LanguageProvider'
2
+ export * from './Language'