-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
373 lines (320 loc) · 9.99 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
jupyterlab-dagitty
Copyright (C) 2022 Michal Krassowski
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
import { Widget } from '@lumino/widgets';
import type { Message } from '@lumino/messaging';
import { DAGittyController, GraphParser } from './_dagitty';
import type { Graph } from './_dagitty';
import { dagIcon } from './icons';
/**
* The default mime type for the extension.
*/
const MIME_TYPE = 'application/x.dagitty.dag';
/**
* The class name added to the extension.
*/
const CLASS_NAME = 'mimerenderer-dagitty-dag';
/**
* A widget for rendering Dagitty DAG.
*/
export class OutputWidget extends Widget implements IRenderMime.IRenderer {
dagController?: DAGittyController;
private _arePositionsOutdated: boolean;
private _resizeObserver: ResizeObserver;
private _offsetLeft: number;
private _offsetTop: number;
private _offsetWidth: number;
private _offsetHeight: number;
private _inDrag: boolean;
/**
* Construct a new output widget.
*/
constructor(options: IRenderMime.IRendererOptions) {
super();
this._mimeType = options.mimeType;
this.addClass(CLASS_NAME);
this._updatePositions();
this._resizeObserver = new ResizeObserver((entries: any) => {
this._resize();
});
this._resizeObserver.observe(this.node);
this._inDrag = false;
}
/**
* Render Dagitty DAG into this widget's node.
*/
renderModel(model: IRenderMime.IMimeModel): Promise<void> {
const data = model.data[this._mimeType] as string;
const metadata = model.metadata as any;
for (const argument of ['width', 'height']) {
const value = metadata[argument] as string | undefined;
if (value) {
this.node.style.setProperty(argument, value);
}
}
const isMutable = (metadata['mutable'] as boolean | undefined) || false;
const graph = GraphParser.parseGuess(data);
this.dagController = new DAGittyController({
canvas: this.node,
graph: graph,
autofocus: true,
interactive: true,
// we set mutable=false to prevent adding new nodes
// but we still alllow view mutations, see setListeners()
mutable: isMutable,
});
this.adjustPointerPositioning();
if (!isMutable) {
this.setDragListeners();
}
return Promise.resolve();
}
private _maybeUpdatePositions() {
if (this._arePositionsOutdated) {
this._updatePositions();
}
}
private _updatePositions() {
this._offsetLeft = this.node.offsetLeft;
this._offsetTop = this.node.offsetTop;
this._offsetWidth = this.node.offsetWidth;
this._offsetHeight = this.node.offsetHeight;
this._arePositionsOutdated = false;
}
protected adjustPointerPositioning(): void {
const view = this.dagController.getView();
const impl = view.impl;
// dagitty uses offsetLeft and offsetTop to calculate mouse position,
// which is only correct if the container is a direct descendant of body
// (or nested in elements which do not have paddings/border/position)
// so here we override position getters to return correct values.
const offsetX = (e: MouseEvent) => {
this._maybeUpdatePositions();
return e.offsetX + this._offsetLeft;
};
const offsetY = (e: MouseEvent) => {
this._maybeUpdatePositions();
return e.offsetY + this._offsetTop;
};
view.pointerX = offsetX;
view.pointerY = offsetY;
impl.pointerX = offsetX;
impl.pointerY = offsetY;
}
onUpdateRequest(message: Message): void {
this._arePositionsOutdated = true;
}
/**
* Preserve the information about moved edges in vertices,
* so that they remain in place when we resize the view.
*/
setDragListeners(): void {
const view = this.dagController.getView();
const impl = view.impl;
impl.setEventListener('vertex_drag', (vs: any) => {
const [x, y] = view.toGraphCoordinate(vs.x, vs.y);
vs.v.layout_pos_x = x;
vs.v.layout_pos_y = y;
if (view.getViewMode() !== 'normal') {
const v = view.getGraph().getVertex(vs.v.id);
v.layout_pos_x = x;
v.layout_pos_y = y;
}
});
impl.setEventListener('edge_drag', (es: any) => {
const [x, y] = view.toGraphCoordinate(es.cx, es.cy);
es.e.layout_pos_x = x;
es.e.layout_pos_y = y;
});
impl.setEventListener('drag_end', () =>
this.dagController.graphLayoutChanged()
);
}
private _resize(): void {
if (this.dagController) {
this.dagController.getView().resize();
}
}
protected onResize(msg: Widget.ResizeMessage): void {
this._resize();
this.update();
}
/**
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the widgets's DOM node.
*
* This should not be called directly by user code.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'wheel':
this._evtMouseWheel(event as WheelEvent);
event.preventDefault();
break;
case 'pointerdown':
this._evtMouseDown(event as MouseEvent);
break;
case 'pointermove':
this._evtMouseMove(event as MouseEvent);
break;
case 'pointerleave':
this._evtMouseLeave(event as MouseEvent);
break;
case 'pointerup':
this._evtMouseUp(event as MouseEvent);
break;
}
}
private _evtMouseDown(event: MouseEvent): void {
this._inDrag = true;
}
private _getBoundingBox(graph: Graph): number[] {
let box = graph.getBoundingBox();
if (box === null) {
const box2 = this.dagController.getView().bounds;
box = [box2[0], box2[2], box2[1], box2[3]];
}
return box;
}
private _evtMouseMove(event: MouseEvent): void {
if (!(this._inDrag && event.ctrlKey)) {
return;
}
this._maybeUpdatePositions();
const graph = this.dagController.getGraph();
const box = this._getBoundingBox(graph);
const w = box[2] - box[0];
const h = box[3] - box[1];
const dx = (event.movementX / this._offsetWidth) * w;
const dy = (event.movementY / this._offsetHeight) * h;
box[0] -= dx;
box[1] -= dy;
box[2] -= dx;
box[3] -= dy;
graph.setBoundingBox(box);
this.dagController.getView().drawGraph();
this.update();
}
private _evtMouseUp(event: MouseEvent): void {
this._inDrag = false;
}
private _evtMouseLeave(event: MouseEvent): void {
this._inDrag = false;
}
private _evtMouseWheel(event: WheelEvent): void {
if (!(this.dagController && event.ctrlKey)) {
return;
}
const graph = this.dagController.getGraph();
let box = this._getBoundingBox(graph);
if (box === null) {
box = [0, 0, 1, 1];
}
const scale = 1 + event.deltaY / window.screen.height;
this._maybeUpdatePositions();
let w = box[2] - box[0];
let h = box[3] - box[1];
const xmin = box[0];
const ymin = box[1];
box[0] -= xmin;
box[1] -= ymin;
box[2] -= xmin;
box[3] -= ymin;
box[0] -= w / 2;
box[1] -= h / 2;
box[2] -= w / 2;
box[3] -= h / 2;
box = box.map((x) => x * scale);
const dx = (event.offsetX / this._offsetWidth) * (1 - scale) * w;
const dy = (event.offsetY / this._offsetHeight) * (1 - scale) * h;
box[0] += dx;
box[1] += dy;
box[2] += dx;
box[3] += dy;
w = box[2] - box[0];
h = box[3] - box[1];
box[0] += w / 2;
box[1] += h / 2;
box[2] += w / 2;
box[3] += h / 2;
box[0] += xmin;
box[1] += ymin;
box[2] += xmin;
box[3] += ymin;
graph.setBoundingBox(box);
this.dagController.getView().drawGraph();
this.update();
}
/**
* A message handler invoked on a `'before-attach'` message.
*/
protected onBeforeAttach(msg: Message): void {
this.node.addEventListener('pointerdown', this);
this.node.addEventListener('pointerup', this);
this.node.addEventListener('pointermove', this);
this.node.addEventListener('pointerleave', this);
this.node.addEventListener('wheel', this);
}
/**
* A message handler invoked on an `'after-detach'` message.
*/
protected onAfterDetach(msg: Message): void {
this.node.removeEventListener('pointerdown', this);
this.node.removeEventListener('pointerup', this);
this.node.removeEventListener('pointermove', this);
this.node.removeEventListener('pointerleave', this);
this.node.removeEventListener('wheel', this);
}
dispose(): void {
if (this._resizeObserver) {
this._resizeObserver.unobserve(this.node);
this._resizeObserver = null;
}
super.dispose();
}
private _mimeType: string;
}
/**
* A mime renderer factory for Dagitty DAG data.
*/
export const rendererFactory: IRenderMime.IRendererFactory = {
safe: true,
mimeTypes: [MIME_TYPE],
createRenderer: (options) => new OutputWidget(options),
};
/**
* Extension definition.
*/
const extension: IRenderMime.IExtension = {
id: 'jupyterlab-dagitty:plugin',
rendererFactory,
rank: 100,
dataType: 'string',
fileTypes: [
{
name: 'dag',
mimeTypes: [MIME_TYPE],
extensions: ['.dag'],
icon: dagIcon.name,
},
{
name: 'dagitty',
mimeTypes: [MIME_TYPE],
extensions: ['.dagitty'],
icon: dagIcon.name,
},
],
documentWidgetFactoryOptions: {
name: 'Dagitty DAG',
primaryFileType: 'dag',
fileTypes: ['dag'],
defaultFor: ['dag'],
},
};
export default extension;