Skip to content

Commit

Permalink
add typescript definition
Browse files Browse the repository at this point in the history
  • Loading branch information
kumilingus committed Aug 14, 2024
1 parent 654a65e commit 69f6566
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/joint-core/test/ts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
},
};
}
44 changes: 44 additions & 0 deletions packages/joint-core/types/joint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,46 @@ export namespace dia {
interface ConstructorOptions extends Graph.Options {
mergeArrays?: boolean;
}

type UnsetCallback<V> = (
this: V,
node: Element,
nodeAttributes: { [name: string]: any },
cellView: V
) => string | Array<string> | null | void;

type SetCallback<V> = (
this: V,
attributeValue: any,
refBBox: g.Rect,
nodeAttributes: { [name: string]: any },
cellView: V
) => { [key: string]: any } | string | number;

type PositionCallback<V> = (
this: V,
attributeValue: any,
refBBox: g.Rect,
node: Element,
nodeAttributes: { [name: string]: any },
cellView: V
) => dia.Point | null | void;

type OffsetCallback<V> = (
this: V,
attributeValue: any,
nodeBBox: g.Rect,
node: Element,
nodeAttributes: { [name: string]: any },
cellView: V
) => dia.Point | null | void;

interface SpecialAttribute<V = dia.CellView> {
set?: SetCallback<V> | string;
unset?: UnsetCallback<V> | string | Array<string>;
position?: PositionCallback<V>;
offset?: OffsetCallback<V>;
}
}

class Cell<A extends ObjectHash = Cell.Attributes, S extends mvc.ModelSetOptions = dia.ModelSetOptions> extends mvc.Model<A, S> {
Expand Down Expand Up @@ -568,6 +608,8 @@ export namespace dia {
protected generatePortId(): string | number;

static define(type: string, defaults?: any, protoProps?: any, staticProps?: any): Cell.Constructor<Element>;

static attributes: { [attributeName: string]: Cell.SpecialAttribute<ElementView> };
}

// dia.Link
Expand Down Expand Up @@ -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<Link>;

static attributes: { [attributeName: string]: Cell.SpecialAttribute<LinkView> };
}

// dia.CellView
Expand Down

0 comments on commit 69f6566

Please sign in to comment.