-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcore.ts
More file actions
166 lines (138 loc) · 4.95 KB
/
core.ts
File metadata and controls
166 lines (138 loc) · 4.95 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { Injector } from '@angular/core';
import { BaseSchemes, CanAssignSignal, Scope } from 'rete'
import { createCustomElement } from '@angular/elements';
import { NgElement, NodeProps, Position, RenderSignal } from './types'
import { RenderPreset } from './presets/types';
import { reflect } from './reflect';
type Item = { key: string, ngElement: NgElement }
type Renderer = {
get(element: HTMLElement): Item | undefined
mount(element: HTMLElement, key: string, component: any, injector: Injector, props: Record<string, unknown>): void
update(item: Item, props: Record<string, unknown>): void
unmount(element: HTMLElement): void
}
function getRenderer(): Renderer {
const elements = new WeakMap<HTMLElement, Item>()
return {
get(element) {
return elements.get(element)
},
mount(element, key, component, injector, props) {
// LIMITATION: If an element is remounted with the same identifier, the component cannot be replaced
const exists = customElements.get(key)
if (!exists) {
customElements.define(key, createCustomElement(component, { injector }))
}
const ngElement = document.createElement(key) as NodeProps & NgElement & typeof props
Object.keys(props).forEach(key => {
ngElement[key] = props[key]
})
element.appendChild(ngElement)
elements.set(element, { key, ngElement })
},
update({ ngElement }, props) {
Object.keys(props).forEach(key => {
ngElement.ngElementStrategy.setInputValue(key, reflect(props[key]))
})
},
unmount(element) {
const existing = elements.get(element)
if (existing) {
existing.ngElement.remove()
elements.delete(element)
}
}
}
}
/**
* Signals that can be emitted by the plugin
* @priority 10
*/
export type Produces<Schemes extends BaseSchemes> =
| { type: 'connectionpath', data: { payload: Schemes['Connection'], path?: string, points: Position[] } }
type Requires<Schemes extends BaseSchemes> =
| RenderSignal<'node', { payload: Schemes['Node'] }>
| RenderSignal<'connection', { payload: Schemes['Connection'], start?: Position, end?: Position }>
| { type: 'unmount', data: { element: HTMLElement } }
/**
* Angular plugin. Renders nodes, connections and other elements using React.
* @priority 9
* @emits connectionpath
* @listens render
* @listens unmount
*/
export class AngularPlugin<Schemes extends BaseSchemes, T = Requires<Schemes>> extends Scope<Produces<Schemes>, [Requires<Schemes> | T]> {
presets: RenderPreset<Schemes, T>[] = []
renderer: Renderer
owners = new WeakMap<HTMLElement, RenderPreset<Schemes, T>>()
/**
* @constructor
* @param params Plugin properties
* @param params.injector Angular's Injector instance
*/
constructor(private params: { injector: Injector }) {
super('angular-render')
this.renderer = getRenderer()
this.addPipe(context => {
if (!context || typeof context !== 'object' || !('type' in context)) return context
if (context.type === 'unmount') {
this.unmount(context.data.element)
} else if (context.type === 'render') {
if ('filled' in context.data && context.data.filled) {
return context
}
if (this.mount(context.data.element, context)) {
return {
...context,
data: {
...context.data,
filled: true
}
} as typeof context
}
}
return context
})
}
setParent(scope: Scope<Requires<Schemes> | T>): void {
super.setParent(scope)
this.presets.forEach(preset => {
if (preset.attach) preset.attach(this)
})
}
private unmount(element: HTMLElement) {
this.owners.delete(element)
this.renderer.unmount(element)
}
private mount(element: HTMLElement, context: Requires<Schemes>) {
const existing = this.renderer.get(element)
if (existing) {
this.presets.forEach(preset => {
if (this.owners.get(element) !== preset) return
const result = preset.update(context as unknown as Extract<T, { type: 'render' }>, this)
if (result) {
this.renderer.update(existing, result)
}
})
return true
}
for (const preset of this.presets) {
const result = preset.mount(context as unknown as Extract<T, { type: 'render' }>, this)
if (!result) continue
const { key, component, props } = result
this.renderer.mount(element, key, component, this.params.injector, props)
this.owners.set(element, preset)
return true
}
return
}
/**
* Adds a preset to the plugin.
* @param preset Preset that can render nodes, connections and other elements.
*/
public addPreset<K>(preset: RenderPreset<Schemes, CanAssignSignal<T, K> extends true ? K : 'Cannot apply preset. Provided signals are not compatible'>) {
const local = preset as unknown as RenderPreset<Schemes, T>
if (local.attach) local.attach(this)
this.presets.push(local)
}
}