forked from QuantStack/jupyterlab-drawio
/
tokens.ts
501 lines (435 loc) · 11.8 KB
/
tokens.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
Copyright 2023 ipydrawio contributors
Copyright 2020 jupyterlab-drawio contributors
Copyright 2018 Wolf Vollprecht
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { Widget } from '@lumino/widgets';
import { ISignal } from '@lumino/signaling';
import { ReadonlyPartialJSONObject, Token } from '@lumino/coreutils';
import { Contents } from '@jupyterlab/services';
import { LabIcon } from '@jupyterlab/ui-components';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import * as _PACKAGE from '../package.json';
export const PACKAGE = _PACKAGE;
export const NS = PACKAGE.name;
export const VERSION = PACKAGE.version;
export const PLUGIN_ID = `${NS}:plugin`;
import { Diagram } from './editor';
import { DiagramDocument } from './document';
import ICON_SVG from '../style/img/drawio.svg';
/**
* A namespace for commands
*/
export const CMD_NS = 'ipydrawio';
/**
* The name of the factory that creates text-based diagram widgets.
*/
export const TEXT_FACTORY = 'Diagram';
/**
* The name of the factory that creates binary-based diagram widgets.
*/
export const BINARY_FACTORY = 'Diagram Image';
/**
* The name of the factory that creates JSON-based diagram widgets.
*/
export const JSON_FACTORY = 'Diagram Notebook';
/**
* The raw icon SVG
*/
export const DRAWIO_ICON_SVG = ICON_SVG;
/**
* The metadata key for diagrams in notebooks
*/
export const IPYDRAWIO_METADATA = NS;
/**
* The extracted schema type
*/
import SCHEMA from './_schema';
/**
* The concrete schema
*/
import SCHEMA_JSON from '../schema/plugin.json';
/**
* Escape hatch for runtime debugging.
*/
export const DEBUG = window.location.href.indexOf('DRAWIO_DEBUG') > -1;
/**
* A template which will appear in _Custom Diagram..._
*/
export interface ITemplate {
/** A unique URL for template */
url: string;
/** The human-readable name for the template */
label: string;
/** The path to a thumbnail image */
thumbnail?: string;
/** A list of tags to help describe the template */
tags?: string[];
}
/**
* The main manager interface
*/
export interface IDiagramManager {
addFormat(format: IFormat): void;
formats: IFormat[];
formatForModel(contentsModel: Partial<Contents.IModel>): IFormat | null;
activeWidget: DiagramDocument | null;
drawioURL: string;
settings: ISettingRegistry.ISettings;
escapeCurrent(widget: Widget): void;
templates(): Promise<ITemplate[]>;
addTemplates(...template: ITemplate[]): void;
templatesChanged: ISignal<IDiagramManager, void>;
formatsChanged: ISignal<IDiagramManager, void>;
}
/**
* A namespace for diagram manager concerns
*/
export namespace IDiagramManager {
/**
* Initialization options
*/
export interface IOptions {}
}
/**
* A regular expression for rewriting icons
*/
export const DRAWIO_ICON_CLASS_RE = /jp-icon-warn0/;
/**
* A rank for adding to menus
*/
export const DIAGRAM_MENU_RANK = 99;
/**
* A hoisted set definition of theme elements
*/
export type TUIThemes = SCHEMA.UITheme;
/**
* A concrete list of themes
*/
export const UI_THEMES = SCHEMA_JSON.definitions['ui-theme']['enum'] as TUIThemes[];
/**
* The theme color to replace
*/
export const UI_THEME_BASE_COLOR = '#f08705';
/**
* A type for describing how to transform a UI theme branding
*/
export type TUIThemeOverrides = Record<SCHEMA.UITheme, [string, string][]>;
/**
* Override colors for theme icons
*/
export const UI_THEME_COLORS: TUIThemeOverrides = {
sketch: [[UI_THEME_BASE_COLOR, '#fff2a1']],
dark: [[UI_THEME_BASE_COLOR, '#666']],
min: [[UI_THEME_BASE_COLOR, '#eee']],
kennedy: [[UI_THEME_BASE_COLOR, '#0052cc']],
atlas: [[UI_THEME_BASE_COLOR, '#0049B0']],
};
/**
* The token for the main extension, which can be used by other extensions
*/
export const IDiagramManager = new Token<IDiagramManager>(PLUGIN_ID);
/**
* A namespace for capturing extension commands
*/
export namespace CommandIds {
export const createNew = 'drawio:create-new';
export const createNewCustom = 'drawio:create-new-custom';
export const setUrlParams = 'drawio:url-params';
}
/**
* The shape of a setUrlParams command
*/
export interface ISetUrlParamsArgs {
drawioUrlParams: SCHEMA.DrawioURLParams;
}
/**
* The shape of a createNew command
*/
export interface ICreateNewArgs extends ISetUrlParamsArgs {
/**
* The path in which to create a new untitled file.
*/
cwd: string;
/**
* The format to use for a new file
*/
format?: string;
/**
* The base name for a new file (without extension)
*/
name?: string;
}
export interface IFormat<T = string> {
key: string;
name: string;
ext: string;
label: string;
icon: LabIcon;
format: Contents.FileFormat;
mimetype: string;
pattern?: string;
contentType?: Contents.ContentType;
save?: (raw: string) => string;
load?: (raw: string) => string;
toXML?: (model: DocumentRegistry.IModel) => string;
fromXML?: (model: DocumentRegistry.IModel, xml: string) => void;
exporter?: (
drawio: Diagram,
key: string,
settings: ISettingRegistry.ISettings
) => Promise<T | null>;
// factory info
factoryName: string;
modelName: 'base64' | 'notebook' | 'text';
wantsModel?(contentsModel: Partial<Contents.IModel>): boolean;
// behavior switches
isExport?: boolean;
isBinary?: boolean;
isText?: boolean;
isJson?: boolean;
isEditable?: boolean;
isDefault?: boolean;
isTransformed: boolean;
}
export interface IAdapter {
format(): IFormat | null;
// TODO: generate these from schema
/** a dictionary encoding of the drawio JSON API Config Object */
drawioConfig(): ReadonlyPartialJSONObject;
/** a dictionary encoding of the drawio JSON API URL Params */
urlParams(): ReadonlyPartialJSONObject;
/** Use a an exporter to generate non-XML */
saveNeedsExport(): boolean;
/** load the XML from somewhere */
toXML(): string;
/** where to download drawio assets */
drawioUrl(): string;
/** store the XML somewhere */
fromXML(xml: string, hardSave: boolean): void;
}
/*
TODO: really didn't want to go here, but here we are
- to learn more about what's _actually_ in there, load up the iframe in a console
and look at `window.mxEvent`.
- when you have scope on an App, look at `eventListeners`
*/
type TMXAppModelEvent =
| 'backgroundColorChanged'
| 'backgroundImageChanged'
| 'connectionArrowsChanged'
| 'connectionPointsChanged'
| 'copyConnectChanged'
| 'customFontsChanged'
| 'foldingEnabledChanged'
| 'formatWidthChanged'
| 'gridColorChanged'
| 'gridEnabledChanged'
| 'guidesEnabledChanged'
| 'mathEnabledChanged'
| 'pageFormatChanged'
| 'pageScaleChanged'
| 'pageViewChanged'
| 'pageViewChanged'
| 'shadowVisibleChanged'
| 'styleChanged';
export const MX_APP_MODEL_EVENTS: TMXAppModelEvent[] = [
'backgroundColorChanged',
'backgroundImageChanged',
'connectionArrowsChanged',
'connectionPointsChanged',
'copyConnectChanged',
'customFontsChanged',
'foldingEnabledChanged',
'formatWidthChanged',
'gridColorChanged',
'gridEnabledChanged',
'guidesEnabledChanged',
'mathEnabledChanged',
'pageFormatChanged',
'pageScaleChanged',
'pageViewChanged',
'pageViewChanged',
'shadowVisibleChanged',
'styleChanged',
];
type TMXMEditorEvent =
| 'autosaveChanged'
| 'fileLoaded'
| 'fileLoaded'
| 'pageSelected'
| 'resetGraphView'
| 'statusChanged';
export const MX_EDITOR_EVENTS: TMXMEditorEvent[] = [
'autosaveChanged',
'fileLoaded',
'fileLoaded',
'pageSelected',
'resetGraphView',
'statusChanged',
];
type TMXGraphEvent =
| 'cellsAdded'
| 'cellsInserted'
| 'editingStarted'
| 'editingStopped'
| 'escape'
| 'fireMouseEvent'
| 'gesture'
| 'gridSizeChanged'
| 'moveCells'
| 'pan'
| 'removeCells'
| 'resizeCells'
| 'root'
| 'shadowVisibleChanged'
| 'size'
| 'startEditing'
| 'tapAndHold'
| 'textInserted'
| 'viewStateChanged';
export const MX_GRAPH_EVENTS: TMXGraphEvent[] = [
'cellsAdded',
'cellsInserted',
'editingStarted',
'editingStopped',
'escape',
'fireMouseEvent',
'gesture',
'gridSizeChanged',
'moveCells',
'pan',
'resizeCells',
'removeCells',
'root',
'shadowVisibleChanged',
'size',
'startEditing',
'tapAndHold',
'textInserted',
'viewStateChanged',
];
type TMXGraphModelEvent = 'change';
export const MX_GRAPH_MODEL_EVENTS: TMXGraphModelEvent[] = ['change'];
type TMXGraphViewEvent =
| 'down'
| 'scale'
| 'scaleAndTranslate'
| 'translate'
| 'undo'
| 'unitChanged'
| 'up';
export const MX_GRAPH_VIEW_EVENTS: TMXGraphViewEvent[] = [
'down',
'scale',
'scaleAndTranslate',
'translate',
'undo',
'unitChanged',
'up',
];
export type TMXGraphPanningEvent = 'panStart' | 'panEnd' | 'pan';
export const MX_GRAPH_PAN_EVENTS: TMXGraphPanningEvent[] = [
'panStart',
'panEnd',
'pan',
];
export type TMXGraphSelectionEvent = 'change';
export const MX_GRAPH_SELECT_EVENTS: TMXGraphSelectionEvent[] = ['change'];
export type TMXGraphSelectionModelEvent = 'change';
export const MX_GRAPH_SELECT_MODEL_EVENTS: TMXGraphSelectionModelEvent[] = ['change'];
type TMXEvent =
| TMXAppModelEvent
| TMXGraphModelEvent
| TMXGraphEvent
| TMXGraphViewEvent
| TMXGraphModelEvent
| TMXGraphViewEvent
| TMXMEditorEvent
| TMXGraphPanningEvent
| TMXGraphSelectionEvent;
// TODO: use an enum or something to make all these type nicely
export interface IMXEventHandler<T extends IMXEventSource = any> {
(sender: T, evt: IMXEvent): void;
}
export interface IMXEventSource<T = TMXEvent> {
addListener(name: T, handler: IMXEventHandler): void;
}
export interface IMXEvent {
name: TMXEvent;
}
export interface IMXGraphModel extends IMXEventSource<TMXGraphModelEvent> {
dx: number;
dy: number;
cells: IMXCell[];
}
export interface IMXGraphView extends IMXEventSource<TMXGraphViewEvent> {
getScale(): number;
setScale(scale: number): void;
getTranslate(): IMXPoint;
setTranslate(x: number, y: number): void;
gridColor: string;
}
export interface IMXGraphPanningHandler extends IMXEventSource<TMXGraphPanningEvent> {}
export interface IMXGraphSelectionHandler
extends IMXEventSource<TMXGraphSelectionEvent> {}
export interface IMXGraphSelectionModel
extends IMXEventSource<TMXGraphSelectionModelEvent> {
cells: IMXCell[];
setCells(cells: IMXCell[]): void;
}
export interface IMXGraph extends IMXEventSource<TMXGraphEvent> {
model: IMXGraphModel;
view: IMXGraphView;
panningHandler: IMXGraphPanningHandler;
pageFormat: IMXRectangle;
gridEnabled: boolean;
setGridEnabled(gridEnabled: boolean): void;
refresh(): void;
setGridSize(size: number): void;
gridSize: number;
selectionCellsHandler: IMXGraphSelectionHandler;
selectionModel: IMXGraphSelectionModel;
getCellsById(id: string[]): IMXCell[];
}
export interface IMXEditor extends IMXEventSource<TMXMEditorEvent> {
graph: IMXGraph;
}
// TODO: what does this do
export interface IMXPage extends IMXEventSource<any> {
getId(): string;
}
export interface IMXApp extends IMXEventSource<TMXAppModelEvent> {
editor: IMXEditor;
container: HTMLBodyElement;
pages: IMXPage[];
currentPage: IMXPage;
setPageFormat(pageFormat: IMXRectangle): void;
selectPage(page: IMXPage): void;
setGridColor(color: string): void;
// DOM
diagramContainer: HTMLDivElement;
}
export interface IMXPoint {
x: number;
y: number;
}
export interface IMXRectangle {
height: number;
width: number;
x: number;
y: number;
}
export interface IMXCell {
getId(): string;
}