From 69f65665da9b029036012bc88af7a36bde23be49 Mon Sep 17 00:00:00 2001 From: Roman Bruckner Date: Wed, 14 Aug 2024 16:36:49 +0200 Subject: [PATCH] add typescript definition --- packages/joint-core/test/ts/index.test.ts | 24 +++++++++++++ packages/joint-core/types/joint.d.ts | 44 +++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/packages/joint-core/test/ts/index.test.ts b/packages/joint-core/test/ts/index.test.ts index e458c7ad6..621a715ed 100644 --- a/packages/joint-core/test/ts/index.test.ts +++ b/packages/joint-core/test/ts/index.test.ts @@ -198,3 +198,27 @@ rectangle.prop({ size: { width: 100 }}); new joint.shapes.standard.Rectangle({ position: { x: 100 }, }); + +class MyElement extends joint.dia.Element { + + static attributes = { + 'empty-attribute': {}, + 'set1-attribute': { + set: 'alias', + unset: 'alias' + }, + 'set2-attribute': { + set: () => ({ 'alias': 21 }), + unset: ['alias'] + }, + 'set3-attribute': { + set: function() { return 21; }, + }, + 'position-attribute': { + position: () => ({ x: 5, y: 7 }) + }, + 'offset-attribute': { + offset: () => ({ x: 11, y: 13 }) + }, + }; +} diff --git a/packages/joint-core/types/joint.d.ts b/packages/joint-core/types/joint.d.ts index 2ef0c9547..1dadb7be4 100644 --- a/packages/joint-core/types/joint.d.ts +++ b/packages/joint-core/types/joint.d.ts @@ -334,6 +334,46 @@ export namespace dia { interface ConstructorOptions extends Graph.Options { mergeArrays?: boolean; } + + type UnsetCallback = ( + this: V, + node: Element, + nodeAttributes: { [name: string]: any }, + cellView: V + ) => string | Array | null | void; + + type SetCallback = ( + this: V, + attributeValue: any, + refBBox: g.Rect, + nodeAttributes: { [name: string]: any }, + cellView: V + ) => { [key: string]: any } | string | number; + + type PositionCallback = ( + this: V, + attributeValue: any, + refBBox: g.Rect, + node: Element, + nodeAttributes: { [name: string]: any }, + cellView: V + ) => dia.Point | null | void; + + type OffsetCallback = ( + this: V, + attributeValue: any, + nodeBBox: g.Rect, + node: Element, + nodeAttributes: { [name: string]: any }, + cellView: V + ) => dia.Point | null | void; + + interface SpecialAttribute { + set?: SetCallback | string; + unset?: UnsetCallback | string | Array; + position?: PositionCallback; + offset?: OffsetCallback; + } } class Cell extends mvc.Model { @@ -568,6 +608,8 @@ export namespace dia { protected generatePortId(): string | number; static define(type: string, defaults?: any, protoProps?: any, staticProps?: any): Cell.Constructor; + + static attributes: { [attributeName: string]: Cell.SpecialAttribute }; } // dia.Link @@ -716,6 +758,8 @@ export namespace dia { translate(tx: number, ty: number, opt?: S): this; static define(type: string, defaults?: any, protoProps?: any, staticProps?: any): Cell.Constructor; + + static attributes: { [attributeName: string]: Cell.SpecialAttribute }; } // dia.CellView