@knotx/decorators 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,370 @@
1
+ import { RenderType, IPlugin, EdgeOperatorFunction, EdgeOperationPipe, EdgeRenderType, Engine, PluginData, Layer, VerticalAlignment, HorizontalAlignment, Container, NodeOperatorFunction, NodeOperationPipe, NodeRenderType } from '@knotx/core';
2
+
3
+ interface IInternalPlugin<TPluginName extends string = string, TRenderType extends RenderType = RenderType> extends IPlugin<TPluginName, TRenderType> {
4
+ }
5
+
6
+ declare const edgeOperatorSymbol: unique symbol;
7
+ declare module '../internal' {
8
+ interface IInternalPlugin {
9
+ [edgeOperatorSymbol]?: Record<string | symbol, EdgeOperatorFunction>;
10
+ }
11
+ }
12
+ /**
13
+ * 边操作装饰器
14
+ * 用于声明一个方法作为边操作函数
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * class MyPlugin extends BasePlugin {
19
+ * @edgeOperator()
20
+ * addEdge(source: string, target: string) {
21
+ * return [{
22
+ * type: 'add',
23
+ * edge: {
24
+ * id: generateId(),
25
+ * source,
26
+ * target,
27
+ * }
28
+ * }]
29
+ * }
30
+ * }
31
+ * ```
32
+ */
33
+ declare function edgeOperator(): <TRenderType extends RenderType, Args extends any[]>(_target: any, { name, static: isStatic, private: isPrivate, addInitializer }: ClassMethodDecoratorContext<IInternalPlugin<string, TRenderType>, EdgeOperatorFunction<any, Args>>) => void;
34
+
35
+ declare const edgePipeSymbol: unique symbol;
36
+ declare module '../internal' {
37
+ interface IInternalPlugin {
38
+ [edgePipeSymbol]?: Record<string | symbol, EdgeOperationPipe<any>>;
39
+ }
40
+ }
41
+ /**
42
+ * 边操作管道装饰器
43
+ * 用于声明一个方法作为边操作管道,在边操作执行前后进行处理
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * class MyPlugin extends BasePlugin {
48
+ * @edgePipe()
49
+ * validateEdgeOperation(): EdgeOperationPipe {
50
+ * return {
51
+ * // 在操作执行前进行验证
52
+ * preOperation: () => pipe(
53
+ * filter(operation => {
54
+ * if (operation.type === 'add') {
55
+ * return this.validateEdge(operation.data)
56
+ * }
57
+ * return true
58
+ * })
59
+ * )
60
+ * }
61
+ * }
62
+ * }
63
+ * ```
64
+ */
65
+ declare function edgePipe(): <TRenderType extends RenderType, T = any>(_target: any, { name, static: isStatic, private: isPrivate, addInitializer }: ClassMethodDecoratorContext<IInternalPlugin<string, TRenderType>, () => EdgeOperationPipe<T>>) => void;
66
+
67
+ declare const edgeRendererSymbol: unique symbol;
68
+ declare module '../internal' {
69
+ interface IInternalPlugin {
70
+ [edgeRendererSymbol]?: Record<string, EdgeRenderType>;
71
+ }
72
+ }
73
+ declare function edgeType(type: string): <TRenderType extends RenderType>(_target: any, { name, static: isStatic, private: isPrivate, addInitializer }: ClassMethodDecoratorContext<IInternalPlugin<string, TRenderType>, EdgeRenderType> | ClassFieldDecoratorContext<IInternalPlugin<string, TRenderType>, EdgeRenderType>) => void;
74
+
75
+ type InjectableDataKey = keyof {
76
+ [K in keyof Engine as Engine[K] extends (...args: any[]) => any ? never : K]: Engine[K];
77
+ };
78
+ type InjectableMethodKey = Exclude<keyof Engine, InjectableDataKey>;
79
+ interface InjectWithContext<T, TContext> {
80
+ __contextRef__: T;
81
+ __contextValue__: TContext;
82
+ }
83
+ type InjectDecoratorInjectableDataType = {
84
+ [K in InjectableDataKey]: {
85
+ (): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, Engine[K]>) => void;
86
+ <R>(selector: (data: Engine[K]) => R): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, R>) => void;
87
+ <R, TContext>(selector: (data: Engine[K], context: TContext) => R): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, InjectWithContext<R, TContext>>) => void;
88
+ };
89
+ };
90
+ type InjectDecoratorInjectablePluginDataType = {
91
+ [K in keyof PluginData]: {
92
+ [P in keyof PluginData[K]]: {
93
+ (): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, PluginData[K][P]>) => void;
94
+ <R>(selector: (data: PluginData[K][P]) => R): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, R>) => void;
95
+ <R, TContext>(selector: (data: PluginData[K][P], context: TContext) => R): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, InjectWithContext<R, TContext>>) => void;
96
+ };
97
+ };
98
+ };
99
+ type InjectDecoratorInjectableMethodType = {
100
+ [K in InjectableMethodKey]: () => (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, Engine[K]>) => void;
101
+ };
102
+ interface InjectDecoratorCallableDataType {
103
+ /**
104
+ * 注入引擎属性装饰器
105
+ * 用于将引擎的数据属性注入到插件类的字段中
106
+ * @param engineKey 引擎数据的键名
107
+ * @example
108
+ * ```
109
+ * @inject('nodes')
110
+ * nodes!: NodeData[]
111
+ * ```
112
+ */
113
+ <T extends InjectableDataKey>(engineKey: T): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, Engine[T]>) => void;
114
+ /**
115
+ * 注入引擎属性装饰器
116
+ * 用于将引擎的数据属性注入到插件类的字段中
117
+ * @param engineKey 引擎数据的键名
118
+ * @param selector 可选的选择器函数,用于转换注入的数据
119
+ * @example
120
+ * ```
121
+ * @inject('nodes', nodes => nodes.length)
122
+ * nodesCount!: number
123
+ * ```
124
+ */
125
+ <T extends InjectableDataKey, R = Engine[T]>(engineKey: T, selector: (data: Engine[T]) => R): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, R>) => void;
126
+ /**
127
+ * 注入引擎属性装饰器
128
+ * 用于将引擎的数据属性注入到插件类的字段中
129
+ * @param engineKey 引擎数据的键名
130
+ * @param selector 可选的选择器函数,用于转换注入的数据
131
+ * @param context 可选的上下文对象,用于选择器函数
132
+ * @example
133
+ * ```
134
+ * @inject('nodes', (nodes, context) => nodes.find(node => node.id === context.node))
135
+ * ```
136
+ */
137
+ <T extends InjectableDataKey, TContext, R = Engine[T]>(engineKey: T, selector: (data: Engine[T], context: TContext) => R): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, InjectWithContext<R, TContext>>) => void;
138
+ }
139
+ interface InjectDecoratorCallablePluginDataType {
140
+ /**
141
+ * 注入插件属性装饰器
142
+ * 用于将其他插件的数据属性注入到当前插件类的字段中
143
+ * @param pluginName 插件名称
144
+ * @param property 插件属性名
145
+ * @example
146
+ * ```
147
+ * @inject('history', 'canUndo')
148
+ * canUndo!: boolean
149
+ * ```
150
+ */
151
+ <T extends keyof PluginData, TP extends keyof PluginData[T]>(pluginName: T, property: TP): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, PluginData[T][TP]>) => void;
152
+ /**
153
+ * 注入插件属性装饰器
154
+ * 用于将其他插件的数据属性注入到当前插件类的字段中
155
+ * @param pluginName 插件名称
156
+ * @param property 插件属性名
157
+ * @param selector 可选的选择器函数,用于转换注入的数据
158
+ * @example
159
+ * ```
160
+ * @inject('drag', 'options', options => options.inertia)
161
+ * dragInertia!: boolean
162
+ * ```
163
+ */
164
+ <T extends keyof PluginData, TP extends keyof PluginData[T], R = PluginData[T][TP]>(pluginName: T, property: TP, selector: (data: PluginData[T][TP]) => R): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, R>) => void;
165
+ /**
166
+ * 注入插件属性装饰器
167
+ * 用于将其他插件的数据属性注入到当前插件类的字段中
168
+ * @param pluginName 插件名称
169
+ * @param property 插件属性名
170
+ * @param selector 可选的选择器函数,用于转换注入的数据
171
+ * @param context 可选的上下文对象,用于选择器函数
172
+ * @example
173
+ * ```
174
+ * @inject('history', 'canUndo', (canUndo, context) => canUndo === context.xx)
175
+ * ```
176
+ */
177
+ <T extends keyof PluginData, TP extends keyof PluginData[T], TContext, R = PluginData[T][TP]>(pluginName: T, property: TP, selector: (data: PluginData[T][TP], context: TContext) => R): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, InjectWithContext<R, TContext>>) => void;
178
+ }
179
+ interface InjectDecoratorCallableMethodType {
180
+ /**
181
+ * 注入引擎方法装饰器
182
+ * 用于将引擎的方法注入到插件类的字段或方法中
183
+ * @param methodKey 引擎方法的键名
184
+ * @example
185
+ * ```
186
+ * @inject('getNode')
187
+ * getNode!: (id: string) => NodeData | undefined
188
+ *
189
+ * @inject('dispatchNodeOperation')
190
+ * dispatchNodeOperation!: (operation: NodeOperation) => void
191
+ * ```
192
+ */
193
+ <T extends InjectableMethodKey>(methodKey: T): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, Engine[T]>) => void;
194
+ }
195
+ type Injectable = Pick<Engine, InjectableDataKey> & PluginData;
196
+ interface InjectDecoratorInjectableType {
197
+ /**
198
+ * 使用 injectable 风格的注入
199
+ * 用于将多个数据源注入到插件类的字段中
200
+ * @param selector 使用 injectable 对象访问多个数据源的选择器函数
201
+ * @example
202
+ * ```
203
+ * @inject((injectable, context) => injectable.nodes.filter(node => node.id === context.id))
204
+ * filteredNodes!: NodeData[]
205
+ * ```
206
+ */
207
+ <R>(selector: (injectable: Injectable) => R): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, R>) => void;
208
+ <R, TContext>(selector: (injectable: Injectable, context: TContext) => R): (_: undefined, context: ClassFieldDecoratorContext<IInternalPlugin, InjectWithContext<R, TContext>>) => void;
209
+ }
210
+ type InjectDecorator = InjectDecoratorInjectableDataType & InjectDecoratorInjectablePluginDataType & InjectDecoratorInjectableMethodType & InjectDecoratorCallableDataType & InjectDecoratorCallablePluginDataType & InjectDecoratorCallableMethodType & InjectDecoratorInjectableType;
211
+
212
+ /**
213
+ * 注入引擎变量
214
+ * \@inject.property()
215
+ *
216
+ * 注入引擎变量并使用选择器
217
+ * \@inject.property(selector)
218
+ *
219
+ * 注入插件变量
220
+ * \@inject.pluginName.property()
221
+ * \@inject.pluginName.property(selector)
222
+ *
223
+ * 注入引擎方法
224
+ * \@inject.methodName()
225
+ */
226
+ declare const inject: InjectDecorator;
227
+
228
+ /**
229
+ * 注册插件属性装饰器
230
+ *
231
+ * 用于将插件的属性注册到引擎中,使其他插件可以通过 inject 访问该属性
232
+ * 支持装饰字段和 getter,当装饰字段时,属性值变化会自动通知订阅者
233
+ *
234
+ * @param property 要注册的属性名
235
+ * @returns 类字段或 getter 装饰器函数
236
+ * @example
237
+ * ```
238
+ * // 注册普通字段
239
+ * @register('canUndo')
240
+ * canUndo = false
241
+ *
242
+ * // 注册计算属性
243
+ * @register('ref')
244
+ * get ref() {
245
+ * return this
246
+ * }
247
+ * ```
248
+ */
249
+ declare function register<T extends keyof PluginData, TP extends keyof PluginData[T]>(property: TP): <TRenderType extends RenderType>(_: any, context: ClassFieldDecoratorContext<IInternalPlugin<T, TRenderType>, PluginData[T][TP]> | ClassGetterDecoratorContext<IInternalPlugin<T, TRenderType>, PluginData[T][TP]>) => void;
250
+
251
+ declare function layer<This extends IPlugin>(layer: Layer, offset?: number): (_target: any, { name, kind, static: isStatic, private: isPrivate, addInitializer }: ClassMethodDecoratorContext<This>) => void;
252
+
253
+ /**
254
+ * 面板位置
255
+ * @example
256
+ * ```
257
+ * @panel('top')
258
+ * ```
259
+ */
260
+ type PanelPosition = VerticalAlignment | HorizontalAlignment | `${HorizontalAlignment}-${VerticalAlignment}` | 'center';
261
+ /**
262
+ * 面板偏移
263
+ * @example
264
+ * ```
265
+ * @panel('top', { x: 10, y: 10 })
266
+ * ```
267
+ */
268
+ interface PanelOffset {
269
+ x?: number;
270
+ y?: number;
271
+ }
272
+ /**
273
+ * 创建面板包装组件
274
+ * @param position 面板位置
275
+ * @param offset 面板偏移
276
+ */
277
+ declare function createPanelWrapper(position: PanelPosition, offset?: PanelOffset): ({ children, container }: {
278
+ children: React.ReactNode;
279
+ container: Container;
280
+ }) => JSX.Element;
281
+ /**
282
+ * 面板装饰器
283
+ * @param position 面板位置
284
+ * @param offset 面板偏移
285
+ */
286
+ declare function panel<This extends IPlugin>(position: PanelPosition, offset?: PanelOffset): (_target: any, { name, kind, static: isStatic, private: isPrivate, addInitializer }: ClassMethodDecoratorContext<This>) => void;
287
+
288
+ declare const nodeOperatorSymbol: unique symbol;
289
+ declare module '../internal' {
290
+ interface IInternalPlugin {
291
+ [nodeOperatorSymbol]?: Record<string | symbol, NodeOperatorFunction>;
292
+ }
293
+ }
294
+ /**
295
+ * 节点操作装饰器
296
+ * 用于声明一个方法作为节点操作函数
297
+ *
298
+ * @example
299
+ * ```typescript
300
+ * class MyPlugin extends BasePlugin {
301
+ * @nodeOperator()
302
+ * addNode(x: number, y: number) {
303
+ * return [{
304
+ * type: 'add',
305
+ * node: {
306
+ * id: generateId(),
307
+ * type: 'default',
308
+ * position: { x, y }
309
+ * }
310
+ * }]
311
+ * }
312
+ * }
313
+ * ```
314
+ */
315
+ declare function nodeOperator(): <TRenderType extends RenderType, Args extends any[]>(_target: any, { name, static: isStatic, private: isPrivate, addInitializer }: ClassMethodDecoratorContext<IInternalPlugin<string, TRenderType>, NodeOperatorFunction<any, Args>>) => void;
316
+
317
+ declare const nodePipeSymbol: unique symbol;
318
+ declare module '../internal' {
319
+ interface IInternalPlugin {
320
+ [nodePipeSymbol]?: Record<string | symbol, NodeOperationPipe<any>>;
321
+ }
322
+ }
323
+ /**
324
+ * 节点操作管道装饰器
325
+ * 用于声明一个方法作为节点操作管道,在节点操作执行前后进行处理
326
+ *
327
+ * @example
328
+ * ```typescript
329
+ * class MyPlugin extends BasePlugin {
330
+ * @nodePipe()
331
+ * validateNodeOperation(): NodeOperationPipe {
332
+ * return {
333
+ * // 在操作执行前进行验证
334
+ * preOperation: () => pipe(
335
+ * filter(operation => {
336
+ * if (operation.type === 'add') {
337
+ * return this.validateNode(operation.data)
338
+ * }
339
+ * return true
340
+ * })
341
+ * )
342
+ * }
343
+ * }
344
+ * }
345
+ * ```
346
+ */
347
+ declare function nodePipe(): <TRenderType extends RenderType, T = any>(_target: any, { name, static: isStatic, private: isPrivate, addInitializer }: ClassMethodDecoratorContext<IInternalPlugin<string, TRenderType>, () => NodeOperationPipe<T>>) => void;
348
+
349
+ declare const nodeRendererSymbol: unique symbol;
350
+ declare module '../internal' {
351
+ interface IInternalPlugin {
352
+ [nodeRendererSymbol]?: Record<string, NodeRenderType>;
353
+ }
354
+ }
355
+ declare function nodeType(type: string): <TRenderType extends RenderType>(_target: any, { name, static: isStatic, private: isPrivate, addInitializer }: ClassMethodDecoratorContext<IInternalPlugin<string, TRenderType>, NodeRenderType> | ClassFieldDecoratorContext<IInternalPlugin<string, TRenderType>, NodeRenderType>) => void;
356
+
357
+ /**
358
+ * 生命周期装饰器
359
+ * 在插件初始化时调用
360
+ */
361
+ declare function OnInit<This extends IPlugin>(originalMethod: (this: This) => void, context: ClassMethodDecoratorContext<This>): void;
362
+ /**
363
+ * 销毁装饰器
364
+ * 在插件销毁时调用
365
+ */
366
+ declare function OnDestroy<This extends IPlugin>(originalMethod: (this: This) => void, context: ClassMethodDecoratorContext<This>): void;
367
+
368
+ declare function observable<T>(): (_: any, context: ClassFieldDecoratorContext<IPlugin, T>) => void;
369
+
370
+ export { type InjectDecorator, type InjectDecoratorCallableDataType, type InjectDecoratorCallableMethodType, type InjectDecoratorCallablePluginDataType, type InjectDecoratorInjectableDataType, type InjectDecoratorInjectableMethodType, type InjectDecoratorInjectablePluginDataType, type InjectDecoratorInjectableType, type InjectWithContext, type Injectable, type InjectableDataKey, type InjectableMethodKey, OnDestroy, OnInit, type PanelOffset, type PanelPosition, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register };