@knotx/decorators 0.2.5 → 0.2.7

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.cjs CHANGED
@@ -312,78 +312,6 @@ function createInjectProxy(...presets) {
312
312
  }
313
313
  const inject = createInjectProxy();
314
314
 
315
- function observable() {
316
- return function(_, context) {
317
- if (context.static || typeof context.name !== "string") {
318
- return;
319
- }
320
- context.addInitializer(function() {
321
- const behaviorKey = getPropertyBehaviorSubjectKey(context.name);
322
- if (createBehaviorSubject(this, behaviorKey, this[context.name])) {
323
- subscribeEngine.call(this, (engine) => {
324
- Reflect.defineProperty(this, context.name, {
325
- get() {
326
- return core.Runtime.getInstance().getValue(engine, {
327
- paths: [],
328
- matcher: () => this[behaviorKey]
329
- });
330
- },
331
- set(value) {
332
- this[behaviorKey].next(value);
333
- }
334
- });
335
- });
336
- }
337
- });
338
- };
339
- }
340
-
341
- function config() {
342
- return function(_, context) {
343
- if (context.static) {
344
- return;
345
- }
346
- observable()(_, context);
347
- context.addInitializer(function() {
348
- const updateConfig = (config2) => {
349
- this[context.name] = config2;
350
- };
351
- mergePluginHookFunction.call(this, "onInit", updateConfig);
352
- mergePluginHookFunction.call(this, "onConfigChange", updateConfig);
353
- });
354
- };
355
- }
356
-
357
- function OnInit(_, context) {
358
- context.addInitializer(function() {
359
- mergePluginHookFunction.call(this, "onInit", this[context.name]);
360
- });
361
- }
362
- function OnDestroy(_, context) {
363
- context.addInitializer(function() {
364
- mergePluginHookFunction.call(this, "onDestroy", this[context.name]);
365
- });
366
- }
367
- function OnConfigChange(_, context) {
368
- context.addInitializer(function() {
369
- mergePluginHookFunction.call(this, "onConfigChange", this[context.name]);
370
- });
371
- }
372
-
373
- function register(property) {
374
- return function(_, context) {
375
- if (context.static) {
376
- return;
377
- }
378
- observable()(_, context);
379
- context.addInitializer(function() {
380
- subscribeEngine.call(this, (engine) => {
381
- engine.registerPluginData(this.name, property, this[getPropertyBehaviorSubjectKey(context.name)]);
382
- });
383
- });
384
- };
385
- }
386
-
387
315
  function layer(layer2, offset) {
388
316
  return function(_, context) {
389
317
  if (context.static) {
@@ -562,6 +490,95 @@ function nodeType(type) {
562
490
  };
563
491
  }
564
492
 
493
+ function observable() {
494
+ return function(_, context) {
495
+ if (context.static || typeof context.name !== "string") {
496
+ return;
497
+ }
498
+ context.addInitializer(function() {
499
+ const behaviorKey = getPropertyBehaviorSubjectKey(context.name);
500
+ if (createBehaviorSubject(this, behaviorKey, this[context.name])) {
501
+ subscribeEngine.call(this, (engine) => {
502
+ Reflect.defineProperty(this, context.name, {
503
+ get() {
504
+ return core.Runtime.getInstance().getValue(engine, {
505
+ paths: [],
506
+ matcher: () => this[behaviorKey]
507
+ });
508
+ },
509
+ set(value) {
510
+ this[behaviorKey].next(value);
511
+ }
512
+ });
513
+ });
514
+ }
515
+ });
516
+ };
517
+ }
518
+
519
+ function config() {
520
+ return function(_, context) {
521
+ if (context.static) {
522
+ return;
523
+ }
524
+ observable()(_, context);
525
+ context.addInitializer(function() {
526
+ const updateConfig = (config2) => {
527
+ this[context.name] = config2;
528
+ };
529
+ mergePluginHookFunction.call(this, "onInit", updateConfig);
530
+ mergePluginHookFunction.call(this, "onConfigChange", updateConfig);
531
+ });
532
+ };
533
+ }
534
+
535
+ function OnInit(_, context) {
536
+ context.addInitializer(function() {
537
+ mergePluginHookFunction.call(this, "onInit", this[context.name]);
538
+ });
539
+ }
540
+ function OnDestroy(_, context) {
541
+ context.addInitializer(function() {
542
+ mergePluginHookFunction.call(this, "onDestroy", this[context.name]);
543
+ });
544
+ }
545
+ function OnConfigChange(_, context) {
546
+ context.addInitializer(function() {
547
+ mergePluginHookFunction.call(this, "onConfigChange", this[context.name]);
548
+ });
549
+ }
550
+
551
+ function register(property) {
552
+ return function(_, context) {
553
+ if (context.static) {
554
+ return;
555
+ }
556
+ observable()(_, context);
557
+ context.addInitializer(function() {
558
+ subscribeEngine.call(this, (engine) => {
559
+ engine.registerPluginData(this.name, property, this[getPropertyBehaviorSubjectKey(context.name)]);
560
+ });
561
+ });
562
+ };
563
+ }
564
+
565
+ function tool(description, params) {
566
+ return function(_, context) {
567
+ if (context.static) {
568
+ return;
569
+ }
570
+ context.addInitializer(function() {
571
+ subscribeEngine.call(this, (engine) => {
572
+ engine.registerPluginTool(this.name, context.name, {
573
+ description,
574
+ params,
575
+ func: this[context.name].bind(this)
576
+ });
577
+ });
578
+ });
579
+ };
580
+ }
581
+
565
582
  exports.OnConfigChange = OnConfigChange;
566
583
  exports.OnDestroy = OnDestroy;
567
584
  exports.OnInit = OnInit;
@@ -578,3 +595,4 @@ exports.nodeType = nodeType;
578
595
  exports.observable = observable;
579
596
  exports.panel = panel;
580
597
  exports.register = register;
598
+ exports.tool = tool;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
- import { BasePlugin, RenderType, IPlugin, EdgeOperatorFunction, EdgeOperationPipe, EdgeRenderType, Engine, PluginData, Layer, VerticalAlignment, HorizontalAlignment, Container, NodeOperatorFunction, NodeOperationPipe, NodeRenderType } from '@knotx/core';
1
+ import { BasePlugin, RenderType, IPlugin, EdgeOperatorFunction, EdgeOperationPipe, EdgeRenderType, Engine, PluginData, Layer, VerticalAlignment, HorizontalAlignment, Container, NodeOperatorFunction, NodeOperationPipe, NodeRenderType, PluginTools } from '@knotx/core';
2
+ import { Schema } from 'jsonschema';
2
3
 
3
4
  interface IInternalPlugin<TPluginName extends string = string, TRenderType extends RenderType = RenderType> extends IPlugin<TPluginName, any, TRenderType> {
4
5
  }
@@ -231,23 +232,6 @@ type InjectDecorator = InjectDecoratorInjectableDataType & InjectDecoratorInject
231
232
  */
232
233
  declare const inject: InjectDecorator;
233
234
 
234
- /**
235
- * 注册插件属性装饰器
236
- *
237
- * 用于将插件的属性注册到引擎中,使其他插件可以通过 inject 访问该属性
238
- * 支持装饰字段和 getter,当装饰字段时,属性值变化会自动通知订阅者
239
- *
240
- * @param property 要注册的属性名
241
- * @returns 类字段或 getter 装饰器函数
242
- * @example
243
- * ```
244
- * // 注册字段
245
- * @register('canUndo')
246
- * canUndo = false
247
- * ```
248
- */
249
- declare function register<K extends string, TN extends keyof PluginData, TP extends keyof PluginData[TN]>(property: TP): (_: any, context: CFDC<K, PluginData[TN][TP], TN>) => void;
250
-
251
235
  declare function layer<K extends string>(layer: Layer, offset?: number): (_: any, context: CMDC<K, () => void>) => void;
252
236
 
253
237
  /**
@@ -374,4 +358,79 @@ declare function OnConfigChange<K extends string, TPluginConfig extends Record<s
374
358
 
375
359
  declare function observable<K extends string, T>(): (_: any, context: CFDC<K, T>) => void;
376
360
 
377
- 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, OnConfigChange, OnDestroy, OnInit, type PanelOffset, type PanelPosition, config, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register };
361
+ /**
362
+ * 注册插件属性装饰器
363
+ *
364
+ * 用于将插件的属性注册到引擎中,使其他插件可以通过 inject 访问该属性
365
+ * 支持装饰字段和 getter,当装饰字段时,属性值变化会自动通知订阅者
366
+ *
367
+ * @param property 要注册的属性名
368
+ * @returns 类字段或 getter 装饰器函数
369
+ * @example
370
+ * ```
371
+ * // 注册字段
372
+ * @register('canUndo')
373
+ * canUndo = false
374
+ * ```
375
+ */
376
+ declare function register<K extends string, TN extends keyof PluginData, TP extends keyof PluginData[TN]>(property: TP): (_: any, context: CFDC<K, PluginData[TN][TP], TN>) => void;
377
+
378
+ /**
379
+ * 工具函数参数定义
380
+ */
381
+ interface ToolParams {
382
+ description: string;
383
+ params: Schema;
384
+ }
385
+ /**
386
+ * 从JSONSchema推断参数类型的辅助类型
387
+ */
388
+ type InferParamsFromSchema<T extends Schema> = T extends {
389
+ type: 'object';
390
+ properties: infer P;
391
+ required?: infer R;
392
+ } ? {
393
+ [K in keyof P as K extends Extract<R extends string[] ? R[number] : never, string> ? K : never]: InferSchemaType<P[K]>;
394
+ } & {
395
+ [K in keyof P as K extends Extract<R extends string[] ? R[number] : never, string> ? never : K]?: InferSchemaType<P[K]>;
396
+ } : T extends {
397
+ type: 'array';
398
+ items: infer I;
399
+ } ? Array<InferSchemaType<I>> : T extends {
400
+ type: 'string';
401
+ } ? string : T extends {
402
+ type: 'number';
403
+ } ? number : T extends {
404
+ type: 'boolean';
405
+ } ? boolean : T extends {
406
+ type: 'null';
407
+ } ? null : T extends {
408
+ type: 'integer';
409
+ } ? number : any;
410
+ /**
411
+ * 从 JSONSchema 推断类型的辅助类型
412
+ */
413
+ type InferSchemaType<T> = T extends Schema ? InferParamsFromSchema<T> : any;
414
+ /**
415
+ * 工具函数装饰器
416
+ *
417
+ * 用于装饰插件中的工具函数,使其能被 engine 和其他 plugin 调用
418
+ *
419
+ * @param description 工具函数描述
420
+ * @param params 工具函数参数
421
+ * @returns 方法装饰器函数
422
+ * @example
423
+ * ```
424
+ * class MyPlugin extends BasePlugin {
425
+ * @tool('calculateDistance')
426
+ * calculateDistance(params: { x1: number, y1: number, x2: number, y2: number }): number {
427
+ * const dx = params.x2 - params.x1;
428
+ * const dy = params.y2 - params.y1;
429
+ * return Math.sqrt(dx * dx + dy * dy);
430
+ * }
431
+ * }
432
+ * ```
433
+ */
434
+ declare function tool<TN extends keyof PluginTools, TP extends keyof PluginTools[TN], TS extends Schema>(description: string, params: TS): (_: any, context: CMDC<Extract<TP, string>, PluginTools[TN][TP], TN>) => void;
435
+
436
+ export { type InferParamsFromSchema, type InferSchemaType, type InjectDecorator, type InjectDecoratorCallableDataType, type InjectDecoratorCallableMethodType, type InjectDecoratorCallablePluginDataType, type InjectDecoratorInjectableDataType, type InjectDecoratorInjectableMethodType, type InjectDecoratorInjectablePluginDataType, type InjectDecoratorInjectableType, type InjectWithContext, type Injectable, type InjectableDataKey, type InjectableMethodKey, OnConfigChange, OnDestroy, OnInit, type PanelOffset, type PanelPosition, type ToolParams, config, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register, tool };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
- import { BasePlugin, RenderType, IPlugin, EdgeOperatorFunction, EdgeOperationPipe, EdgeRenderType, Engine, PluginData, Layer, VerticalAlignment, HorizontalAlignment, Container, NodeOperatorFunction, NodeOperationPipe, NodeRenderType } from '@knotx/core';
1
+ import { BasePlugin, RenderType, IPlugin, EdgeOperatorFunction, EdgeOperationPipe, EdgeRenderType, Engine, PluginData, Layer, VerticalAlignment, HorizontalAlignment, Container, NodeOperatorFunction, NodeOperationPipe, NodeRenderType, PluginTools } from '@knotx/core';
2
+ import { Schema } from 'jsonschema';
2
3
 
3
4
  interface IInternalPlugin<TPluginName extends string = string, TRenderType extends RenderType = RenderType> extends IPlugin<TPluginName, any, TRenderType> {
4
5
  }
@@ -231,23 +232,6 @@ type InjectDecorator = InjectDecoratorInjectableDataType & InjectDecoratorInject
231
232
  */
232
233
  declare const inject: InjectDecorator;
233
234
 
234
- /**
235
- * 注册插件属性装饰器
236
- *
237
- * 用于将插件的属性注册到引擎中,使其他插件可以通过 inject 访问该属性
238
- * 支持装饰字段和 getter,当装饰字段时,属性值变化会自动通知订阅者
239
- *
240
- * @param property 要注册的属性名
241
- * @returns 类字段或 getter 装饰器函数
242
- * @example
243
- * ```
244
- * // 注册字段
245
- * @register('canUndo')
246
- * canUndo = false
247
- * ```
248
- */
249
- declare function register<K extends string, TN extends keyof PluginData, TP extends keyof PluginData[TN]>(property: TP): (_: any, context: CFDC<K, PluginData[TN][TP], TN>) => void;
250
-
251
235
  declare function layer<K extends string>(layer: Layer, offset?: number): (_: any, context: CMDC<K, () => void>) => void;
252
236
 
253
237
  /**
@@ -374,4 +358,79 @@ declare function OnConfigChange<K extends string, TPluginConfig extends Record<s
374
358
 
375
359
  declare function observable<K extends string, T>(): (_: any, context: CFDC<K, T>) => void;
376
360
 
377
- 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, OnConfigChange, OnDestroy, OnInit, type PanelOffset, type PanelPosition, config, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register };
361
+ /**
362
+ * 注册插件属性装饰器
363
+ *
364
+ * 用于将插件的属性注册到引擎中,使其他插件可以通过 inject 访问该属性
365
+ * 支持装饰字段和 getter,当装饰字段时,属性值变化会自动通知订阅者
366
+ *
367
+ * @param property 要注册的属性名
368
+ * @returns 类字段或 getter 装饰器函数
369
+ * @example
370
+ * ```
371
+ * // 注册字段
372
+ * @register('canUndo')
373
+ * canUndo = false
374
+ * ```
375
+ */
376
+ declare function register<K extends string, TN extends keyof PluginData, TP extends keyof PluginData[TN]>(property: TP): (_: any, context: CFDC<K, PluginData[TN][TP], TN>) => void;
377
+
378
+ /**
379
+ * 工具函数参数定义
380
+ */
381
+ interface ToolParams {
382
+ description: string;
383
+ params: Schema;
384
+ }
385
+ /**
386
+ * 从JSONSchema推断参数类型的辅助类型
387
+ */
388
+ type InferParamsFromSchema<T extends Schema> = T extends {
389
+ type: 'object';
390
+ properties: infer P;
391
+ required?: infer R;
392
+ } ? {
393
+ [K in keyof P as K extends Extract<R extends string[] ? R[number] : never, string> ? K : never]: InferSchemaType<P[K]>;
394
+ } & {
395
+ [K in keyof P as K extends Extract<R extends string[] ? R[number] : never, string> ? never : K]?: InferSchemaType<P[K]>;
396
+ } : T extends {
397
+ type: 'array';
398
+ items: infer I;
399
+ } ? Array<InferSchemaType<I>> : T extends {
400
+ type: 'string';
401
+ } ? string : T extends {
402
+ type: 'number';
403
+ } ? number : T extends {
404
+ type: 'boolean';
405
+ } ? boolean : T extends {
406
+ type: 'null';
407
+ } ? null : T extends {
408
+ type: 'integer';
409
+ } ? number : any;
410
+ /**
411
+ * 从 JSONSchema 推断类型的辅助类型
412
+ */
413
+ type InferSchemaType<T> = T extends Schema ? InferParamsFromSchema<T> : any;
414
+ /**
415
+ * 工具函数装饰器
416
+ *
417
+ * 用于装饰插件中的工具函数,使其能被 engine 和其他 plugin 调用
418
+ *
419
+ * @param description 工具函数描述
420
+ * @param params 工具函数参数
421
+ * @returns 方法装饰器函数
422
+ * @example
423
+ * ```
424
+ * class MyPlugin extends BasePlugin {
425
+ * @tool('calculateDistance')
426
+ * calculateDistance(params: { x1: number, y1: number, x2: number, y2: number }): number {
427
+ * const dx = params.x2 - params.x1;
428
+ * const dy = params.y2 - params.y1;
429
+ * return Math.sqrt(dx * dx + dy * dy);
430
+ * }
431
+ * }
432
+ * ```
433
+ */
434
+ declare function tool<TN extends keyof PluginTools, TP extends keyof PluginTools[TN], TS extends Schema>(description: string, params: TS): (_: any, context: CMDC<Extract<TP, string>, PluginTools[TN][TP], TN>) => void;
435
+
436
+ export { type InferParamsFromSchema, type InferSchemaType, type InjectDecorator, type InjectDecoratorCallableDataType, type InjectDecoratorCallableMethodType, type InjectDecoratorCallablePluginDataType, type InjectDecoratorInjectableDataType, type InjectDecoratorInjectableMethodType, type InjectDecoratorInjectablePluginDataType, type InjectDecoratorInjectableType, type InjectWithContext, type Injectable, type InjectableDataKey, type InjectableMethodKey, OnConfigChange, OnDestroy, OnInit, type PanelOffset, type PanelPosition, type ToolParams, config, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register, tool };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { BasePlugin, RenderType, IPlugin, EdgeOperatorFunction, EdgeOperationPipe, EdgeRenderType, Engine, PluginData, Layer, VerticalAlignment, HorizontalAlignment, Container, NodeOperatorFunction, NodeOperationPipe, NodeRenderType } from '@knotx/core';
1
+ import { BasePlugin, RenderType, IPlugin, EdgeOperatorFunction, EdgeOperationPipe, EdgeRenderType, Engine, PluginData, Layer, VerticalAlignment, HorizontalAlignment, Container, NodeOperatorFunction, NodeOperationPipe, NodeRenderType, PluginTools } from '@knotx/core';
2
+ import { Schema } from 'jsonschema';
2
3
 
3
4
  interface IInternalPlugin<TPluginName extends string = string, TRenderType extends RenderType = RenderType> extends IPlugin<TPluginName, any, TRenderType> {
4
5
  }
@@ -231,23 +232,6 @@ type InjectDecorator = InjectDecoratorInjectableDataType & InjectDecoratorInject
231
232
  */
232
233
  declare const inject: InjectDecorator;
233
234
 
234
- /**
235
- * 注册插件属性装饰器
236
- *
237
- * 用于将插件的属性注册到引擎中,使其他插件可以通过 inject 访问该属性
238
- * 支持装饰字段和 getter,当装饰字段时,属性值变化会自动通知订阅者
239
- *
240
- * @param property 要注册的属性名
241
- * @returns 类字段或 getter 装饰器函数
242
- * @example
243
- * ```
244
- * // 注册字段
245
- * @register('canUndo')
246
- * canUndo = false
247
- * ```
248
- */
249
- declare function register<K extends string, TN extends keyof PluginData, TP extends keyof PluginData[TN]>(property: TP): (_: any, context: CFDC<K, PluginData[TN][TP], TN>) => void;
250
-
251
235
  declare function layer<K extends string>(layer: Layer, offset?: number): (_: any, context: CMDC<K, () => void>) => void;
252
236
 
253
237
  /**
@@ -374,4 +358,79 @@ declare function OnConfigChange<K extends string, TPluginConfig extends Record<s
374
358
 
375
359
  declare function observable<K extends string, T>(): (_: any, context: CFDC<K, T>) => void;
376
360
 
377
- 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, OnConfigChange, OnDestroy, OnInit, type PanelOffset, type PanelPosition, config, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register };
361
+ /**
362
+ * 注册插件属性装饰器
363
+ *
364
+ * 用于将插件的属性注册到引擎中,使其他插件可以通过 inject 访问该属性
365
+ * 支持装饰字段和 getter,当装饰字段时,属性值变化会自动通知订阅者
366
+ *
367
+ * @param property 要注册的属性名
368
+ * @returns 类字段或 getter 装饰器函数
369
+ * @example
370
+ * ```
371
+ * // 注册字段
372
+ * @register('canUndo')
373
+ * canUndo = false
374
+ * ```
375
+ */
376
+ declare function register<K extends string, TN extends keyof PluginData, TP extends keyof PluginData[TN]>(property: TP): (_: any, context: CFDC<K, PluginData[TN][TP], TN>) => void;
377
+
378
+ /**
379
+ * 工具函数参数定义
380
+ */
381
+ interface ToolParams {
382
+ description: string;
383
+ params: Schema;
384
+ }
385
+ /**
386
+ * 从JSONSchema推断参数类型的辅助类型
387
+ */
388
+ type InferParamsFromSchema<T extends Schema> = T extends {
389
+ type: 'object';
390
+ properties: infer P;
391
+ required?: infer R;
392
+ } ? {
393
+ [K in keyof P as K extends Extract<R extends string[] ? R[number] : never, string> ? K : never]: InferSchemaType<P[K]>;
394
+ } & {
395
+ [K in keyof P as K extends Extract<R extends string[] ? R[number] : never, string> ? never : K]?: InferSchemaType<P[K]>;
396
+ } : T extends {
397
+ type: 'array';
398
+ items: infer I;
399
+ } ? Array<InferSchemaType<I>> : T extends {
400
+ type: 'string';
401
+ } ? string : T extends {
402
+ type: 'number';
403
+ } ? number : T extends {
404
+ type: 'boolean';
405
+ } ? boolean : T extends {
406
+ type: 'null';
407
+ } ? null : T extends {
408
+ type: 'integer';
409
+ } ? number : any;
410
+ /**
411
+ * 从 JSONSchema 推断类型的辅助类型
412
+ */
413
+ type InferSchemaType<T> = T extends Schema ? InferParamsFromSchema<T> : any;
414
+ /**
415
+ * 工具函数装饰器
416
+ *
417
+ * 用于装饰插件中的工具函数,使其能被 engine 和其他 plugin 调用
418
+ *
419
+ * @param description 工具函数描述
420
+ * @param params 工具函数参数
421
+ * @returns 方法装饰器函数
422
+ * @example
423
+ * ```
424
+ * class MyPlugin extends BasePlugin {
425
+ * @tool('calculateDistance')
426
+ * calculateDistance(params: { x1: number, y1: number, x2: number, y2: number }): number {
427
+ * const dx = params.x2 - params.x1;
428
+ * const dy = params.y2 - params.y1;
429
+ * return Math.sqrt(dx * dx + dy * dy);
430
+ * }
431
+ * }
432
+ * ```
433
+ */
434
+ declare function tool<TN extends keyof PluginTools, TP extends keyof PluginTools[TN], TS extends Schema>(description: string, params: TS): (_: any, context: CMDC<Extract<TP, string>, PluginTools[TN][TP], TN>) => void;
435
+
436
+ export { type InferParamsFromSchema, type InferSchemaType, type InjectDecorator, type InjectDecoratorCallableDataType, type InjectDecoratorCallableMethodType, type InjectDecoratorCallablePluginDataType, type InjectDecoratorInjectableDataType, type InjectDecoratorInjectableMethodType, type InjectDecoratorInjectablePluginDataType, type InjectDecoratorInjectableType, type InjectWithContext, type Injectable, type InjectableDataKey, type InjectableMethodKey, OnConfigChange, OnDestroy, OnInit, type PanelOffset, type PanelPosition, type ToolParams, config, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register, tool };
package/dist/index.js CHANGED
@@ -310,78 +310,6 @@ function createInjectProxy(...presets) {
310
310
  }
311
311
  const inject = createInjectProxy();
312
312
 
313
- function observable() {
314
- return function(_, context) {
315
- if (context.static || typeof context.name !== "string") {
316
- return;
317
- }
318
- context.addInitializer(function() {
319
- const behaviorKey = getPropertyBehaviorSubjectKey(context.name);
320
- if (createBehaviorSubject(this, behaviorKey, this[context.name])) {
321
- subscribeEngine.call(this, (engine) => {
322
- Reflect.defineProperty(this, context.name, {
323
- get() {
324
- return Runtime.getInstance().getValue(engine, {
325
- paths: [],
326
- matcher: () => this[behaviorKey]
327
- });
328
- },
329
- set(value) {
330
- this[behaviorKey].next(value);
331
- }
332
- });
333
- });
334
- }
335
- });
336
- };
337
- }
338
-
339
- function config() {
340
- return function(_, context) {
341
- if (context.static) {
342
- return;
343
- }
344
- observable()(_, context);
345
- context.addInitializer(function() {
346
- const updateConfig = (config2) => {
347
- this[context.name] = config2;
348
- };
349
- mergePluginHookFunction.call(this, "onInit", updateConfig);
350
- mergePluginHookFunction.call(this, "onConfigChange", updateConfig);
351
- });
352
- };
353
- }
354
-
355
- function OnInit(_, context) {
356
- context.addInitializer(function() {
357
- mergePluginHookFunction.call(this, "onInit", this[context.name]);
358
- });
359
- }
360
- function OnDestroy(_, context) {
361
- context.addInitializer(function() {
362
- mergePluginHookFunction.call(this, "onDestroy", this[context.name]);
363
- });
364
- }
365
- function OnConfigChange(_, context) {
366
- context.addInitializer(function() {
367
- mergePluginHookFunction.call(this, "onConfigChange", this[context.name]);
368
- });
369
- }
370
-
371
- function register(property) {
372
- return function(_, context) {
373
- if (context.static) {
374
- return;
375
- }
376
- observable()(_, context);
377
- context.addInitializer(function() {
378
- subscribeEngine.call(this, (engine) => {
379
- engine.registerPluginData(this.name, property, this[getPropertyBehaviorSubjectKey(context.name)]);
380
- });
381
- });
382
- };
383
- }
384
-
385
313
  function layer(layer2, offset) {
386
314
  return function(_, context) {
387
315
  if (context.static) {
@@ -560,4 +488,93 @@ function nodeType(type) {
560
488
  };
561
489
  }
562
490
 
563
- export { OnConfigChange, OnDestroy, OnInit, config, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register };
491
+ function observable() {
492
+ return function(_, context) {
493
+ if (context.static || typeof context.name !== "string") {
494
+ return;
495
+ }
496
+ context.addInitializer(function() {
497
+ const behaviorKey = getPropertyBehaviorSubjectKey(context.name);
498
+ if (createBehaviorSubject(this, behaviorKey, this[context.name])) {
499
+ subscribeEngine.call(this, (engine) => {
500
+ Reflect.defineProperty(this, context.name, {
501
+ get() {
502
+ return Runtime.getInstance().getValue(engine, {
503
+ paths: [],
504
+ matcher: () => this[behaviorKey]
505
+ });
506
+ },
507
+ set(value) {
508
+ this[behaviorKey].next(value);
509
+ }
510
+ });
511
+ });
512
+ }
513
+ });
514
+ };
515
+ }
516
+
517
+ function config() {
518
+ return function(_, context) {
519
+ if (context.static) {
520
+ return;
521
+ }
522
+ observable()(_, context);
523
+ context.addInitializer(function() {
524
+ const updateConfig = (config2) => {
525
+ this[context.name] = config2;
526
+ };
527
+ mergePluginHookFunction.call(this, "onInit", updateConfig);
528
+ mergePluginHookFunction.call(this, "onConfigChange", updateConfig);
529
+ });
530
+ };
531
+ }
532
+
533
+ function OnInit(_, context) {
534
+ context.addInitializer(function() {
535
+ mergePluginHookFunction.call(this, "onInit", this[context.name]);
536
+ });
537
+ }
538
+ function OnDestroy(_, context) {
539
+ context.addInitializer(function() {
540
+ mergePluginHookFunction.call(this, "onDestroy", this[context.name]);
541
+ });
542
+ }
543
+ function OnConfigChange(_, context) {
544
+ context.addInitializer(function() {
545
+ mergePluginHookFunction.call(this, "onConfigChange", this[context.name]);
546
+ });
547
+ }
548
+
549
+ function register(property) {
550
+ return function(_, context) {
551
+ if (context.static) {
552
+ return;
553
+ }
554
+ observable()(_, context);
555
+ context.addInitializer(function() {
556
+ subscribeEngine.call(this, (engine) => {
557
+ engine.registerPluginData(this.name, property, this[getPropertyBehaviorSubjectKey(context.name)]);
558
+ });
559
+ });
560
+ };
561
+ }
562
+
563
+ function tool(description, params) {
564
+ return function(_, context) {
565
+ if (context.static) {
566
+ return;
567
+ }
568
+ context.addInitializer(function() {
569
+ subscribeEngine.call(this, (engine) => {
570
+ engine.registerPluginTool(this.name, context.name, {
571
+ description,
572
+ params,
573
+ func: this[context.name].bind(this)
574
+ });
575
+ });
576
+ });
577
+ };
578
+ }
579
+
580
+ export { OnConfigChange, OnDestroy, OnInit, config, createPanelWrapper, edgeOperator, edgePipe, edgeType, inject, layer, nodeOperator, nodePipe, nodeType, observable, panel, register, tool };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knotx/decorators",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "Decorators for Knotx",
5
5
  "author": "boenfu",
6
6
  "license": "MIT",
@@ -28,19 +28,20 @@
28
28
  "dist"
29
29
  ],
30
30
  "peerDependencies": {
31
- "@knotx/jsx": "0.2.5"
31
+ "@knotx/jsx": "0.2.7"
32
32
  },
33
33
  "dependencies": {
34
+ "jsonschema": "^1.5.0",
34
35
  "lodash-es": "^4.17.21",
35
36
  "rxjs": "^7.8.1",
36
- "@knotx/core": "0.2.5"
37
+ "@knotx/core": "0.2.7"
37
38
  },
38
39
  "devDependencies": {
39
40
  "@types/lodash-es": "^4.14.12",
40
- "@knotx/build-config": "0.2.5",
41
- "@knotx/eslint-config": "0.2.5",
42
- "@knotx/jsx": "0.2.5",
43
- "@knotx/typescript-config": "0.2.5"
41
+ "@knotx/build-config": "0.2.7",
42
+ "@knotx/eslint-config": "0.2.7",
43
+ "@knotx/jsx": "0.2.7",
44
+ "@knotx/typescript-config": "0.2.7"
44
45
  },
45
46
  "scripts": {
46
47
  "build": "unbuild",