Skip to content

Commit 2382248

Browse files
kumilingusclaude
andcommitted
feat: add typed EventMap and DefinedEventMap for Paper and Graph
- Add LiteralUnion<T> and ExcludeIndexSignature<T> utility types - Add mvc.EventCallbackMap<T> generic utility for partial event maps - Add Paper.DefinedEventMap (EventMap without index signature) for IDE autocomplete - Add paper:mouseenter and paper:mouseleave to Paper.EventMap - Add typed on() overloads to Paper using DefinedEventMap - Add Graph.EventMap with cell, change:*, layer, and batch events - Add Graph.DefinedEventMap and typed on() overloads to Graph - Preserve backwards compatibility: EventMap keeps index signature Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5ca4008 commit 2382248

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

packages/joint-core/test/ts/index.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,6 @@ cellView.listenTo(paper, {
211211
}
212212
} as joint.dia.Paper.EventMap);
213213

214-
215-
216-
217214
class AttributeHighlighterView extends joint.dia.HighlighterView<{ attribute: string; }> {
218215
preinitialize() {
219216
this.UPDATE_ATTRIBUTES = function() { return [this.options.attribute]; };

packages/joint-core/types/joint.d.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ export namespace dia {
360360
'move': (cell: Cell, options: Options) => void;
361361
'changeId': (cell: Cell, previousId: Cell.ID, options: Options) => void;
362362
// layer events
363-
'layer:add': (layer: GraphLayer, options: Options) => void;
364-
'layer:remove': (layer: GraphLayer, options: Options) => void;
363+
'layer:add': (layer: GraphLayer, collection: GraphLayerCollection, options: Options) => void;
364+
'layer:remove': (layer: GraphLayer, collection: GraphLayerCollection, options: Options) => void;
365365
'layer:change': (layer: GraphLayer, options: Options) => void;
366366
[layerChangeEvent: `layer:change:${string}`]: (layer: GraphLayer, newValue: any, options: Options) => void;
367367
'layer:default': (layer: GraphLayer, options: Options) => void;
@@ -374,6 +374,7 @@ export namespace dia {
374374
}
375375

376376
type DefinedEventMap = ExcludeIndexSignature<EventMap>;
377+
377378
}
378379

379380
class Graph<A extends ObjectHash = Graph.Attributes, S = dia.ModelSetOptions> extends mvc.Model<A, S> {
@@ -409,7 +410,7 @@ export namespace dia {
409410
context?: any
410411
): this;
411412

412-
on<E extends Partial<Graph.DefinedEventMap> & { [key: string]: mvc.EventHandler }>(
413+
on<E extends mvc.EventCallbackMap<Graph.DefinedEventMap>>(
413414
events: E,
414415
context?: any
415416
): this;
@@ -2222,7 +2223,7 @@ export namespace dia {
22222223
context?: any
22232224
): this;
22242225

2225-
on<E extends Partial<Paper.DefinedEventMap> & { [key: string]: mvc.EventHandler }>(
2226+
on<E extends mvc.EventCallbackMap<Paper.DefinedEventMap>>(
22262227
events: E,
22272228
context?: any
22282229
): this;
@@ -3641,6 +3642,12 @@ export namespace mvc {
36413642
[event: string]: EventHandler;
36423643
}
36433644

3645+
/**
3646+
* A partial map of known event callbacks, plus any custom string events.
3647+
* Use with a DefinedEventMap to get autocomplete for known events.
3648+
*/
3649+
type EventCallbackMap<T> = Partial<T> & { [key: string]: EventHandler };
3650+
36443651
const Events: Events;
36453652
interface Events extends EventsMixin {}
36463653

0 commit comments

Comments
 (0)