forked from jupytercalpoly/jupyterlab-code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeSnippetForm.tsx
814 lines (739 loc) · 21.2 KB
/
CodeSnippetForm.tsx
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
// Copyright (c) 2020, jupytercalpoly
// Distributed under the terms of the BSD-3 Clause License.
import { closeIcon, Button, LabIcon } from '@jupyterlab/ui-components';
import { WidgetTracker, ReactWidget, Styling } from '@jupyterlab/apputils';
import { Widget, PanelLayout, Panel } from '@lumino/widgets';
import { Message, MessageLoop } from '@lumino/messaging';
import { PromiseDelegate } from '@lumino/coreutils';
import { ArrayExt, each, map, toArray } from '@lumino/algorithm';
import * as React from 'react';
/**
* CSS styling
*/
const CODE_SNIPPET_FORM = 'jp-codeSnippet-form';
const DIALOG_CONTENT = 'jp-Dialog-content';
/**
* Create and show a code snippet form.
*
* @param options - The code snippet form setup options.
*
* @returns A promise that resolves with whether the form was accepted.
*/
export function showCodeSnippetForm<T>(
options: Partial<CodeSnippetForm.IOptions<T>> = {}
): Promise<CodeSnippetForm.IResult<T>> {
const codeSnippetForm = new CodeSnippetForm(options);
return codeSnippetForm.launch();
}
/**
* A widget used to show code snippet form
*/
export class CodeSnippetForm<T> extends Widget {
constructor(options: Partial<CodeSnippetForm.IOptions<T>> = {}) {
super();
this.addClass(CODE_SNIPPET_FORM);
const normalized = Private.handleOptions(options);
const renderer = normalized.renderer;
this._host = normalized.host;
this._defaultButton = normalized.defaultButton;
this._buttons = normalized.buttons;
this._buttonNodes = toArray(
map(this._buttons, (button) => {
return renderer.createButtonNode(button);
})
);
const layout = (this.layout = new PanelLayout());
const content = new Panel();
content.addClass(DIALOG_CONTENT);
layout.addWidget(content);
this._body = normalized.body;
const header = renderer.createHeader(
normalized.title,
() => this.reject(),
options
);
const body = renderer.createBody(normalized.body);
const footer = renderer.createFooter(this._buttonNodes);
content.addWidget(header);
content.addWidget(body);
content.addWidget(footer);
this._primary = this._buttonNodes[this._defaultButton];
this._focusNodeSelector = options.focusNodeSelector;
// Add new code snippet form to the tracker.
void CodeSnippetForm.tracker.add(this);
}
/**
* Dispose of the resources used by the code snippet form.
*/
dispose(): void {
const promise = this._promise;
if (promise) {
this._promise = null;
promise.reject(void 0);
ArrayExt.removeFirstOf(Private.launchQueue, promise.promise);
}
super.dispose();
}
/**
* Launch the code snippet form as a modal window.
*
* @returns a promise that resolves with the result of the code snippet form.
*/
launch(): Promise<CodeSnippetForm.IResult<T>> {
// Return the existing code snippet form if already open.
if (this._promise) {
return this._promise.promise;
}
const promise = (this._promise = new PromiseDelegate<
CodeSnippetForm.IResult<T>
>());
const promises = Promise.all(Private.launchQueue);
Private.launchQueue.push(this._promise.promise);
return promises.then(() => {
Widget.attach(this, this._host);
return promise.promise;
});
}
/**
* Resolve the current code snippet form.
*
* @param index - An optional index to the button to resolve.
*
* #### Notes
* Will default to the defaultIndex.
* Will resolve the current `show()` with the button value.
* Will be a no-op if the code snippet form is not shown.
*/
resolve(index?: number): void {
if (!this._promise) {
return;
}
if (index === undefined) {
index = this._defaultButton;
}
this._resolve(this._buttons[index]);
}
/**
* Reject the current code snippet form with a default reject value.
*
* #### Notes
* Will be a no-op if the code snippet form is not shown.
*/
reject(): void {
if (!this._promise) {
return;
}
this._resolve(CodeSnippetForm.cancelButton());
}
/**
* Handle the DOM events for the directory listing.
*
* @param event - The DOM event sent to the widget.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the panel's DOM node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'click':
this._evtClick(event as MouseEvent);
break;
case 'focus':
this._evtFocus(event as FocusEvent);
break;
default:
break;
}
}
/**
* A message handler invoked on an `'after-attach'` message.
*/
protected onAfterAttach(msg: Message): void {
const node = this.node;
node.addEventListener('keydown', this, true);
node.addEventListener('click', this, true);
document.addEventListener('focus', this, true);
this._first = Private.findFirstFocusable(this.node);
this._original = document.activeElement as HTMLElement;
if (this._focusNodeSelector) {
const body = this.node.querySelector('.jp-Dialog-body');
const el = body?.querySelector(this._focusNodeSelector);
if (el) {
this._primary = el as HTMLElement;
}
}
this._primary.focus();
}
/**
* A message handler invoked on an `'after-detach'` message.
*/
protected onAfterDetach(msg: Message): void {
const node = this.node;
node.removeEventListener('keydown', this, true);
node.removeEventListener('click', this, true);
document.removeEventListener('focus', this, true);
this._original.focus();
}
/**
* A message handler invoked on a `'close-request'` message.
*/
protected onCloseRequest(msg: Message): void {
if (this._promise) {
this.reject();
}
super.onCloseRequest(msg);
}
/**
* Handle the `'click'` event for a code snippet form button.
*
* @param event - The DOM event sent to the widget
*/
protected _evtClick(event: MouseEvent): void {
const content = this.node.getElementsByClassName(
'jp-Dialog-content'
)[0] as HTMLElement;
if (!content.contains(event.target as HTMLElement)) {
event.stopPropagation();
event.preventDefault();
this.reject();
return;
}
for (const buttonNode of this._buttonNodes) {
if (buttonNode.contains(event.target as HTMLElement)) {
const index = this._buttonNodes.indexOf(buttonNode);
this.resolve(index);
}
}
}
/**
* Handle the `'keydown'` event for the widget.
*
* @param event - The DOM event sent to the widget
*/
protected _evtKeydown(event: KeyboardEvent): void {
// Check for escape key
switch (event.keyCode) {
case 27: // Escape.
event.stopPropagation();
event.preventDefault();
this.reject();
break;
case 9: {
// Tab.
// Handle a tab on the last button.
const node = this._buttonNodes[this._buttons.length - 1];
if (document.activeElement === node && !event.shiftKey) {
event.stopPropagation();
event.preventDefault();
this._first.focus();
}
break;
}
default:
break;
}
}
/**
* Handle the `'focus'` event for the widget.
*
* @param event - The DOM event sent to the widget
*/
protected _evtFocus(event: FocusEvent): void {
const target = event.target as HTMLElement;
if (!this.node.contains(target as HTMLElement)) {
event.stopPropagation();
this._buttonNodes[this._defaultButton].focus();
}
}
/**
* Resolve a button item.
*/
private _resolve(button: CodeSnippetForm.IButton): void {
// Prevent loopback.
const promise = this._promise;
if (!promise) {
this.dispose();
return;
}
this._promise = null;
ArrayExt.removeFirstOf(Private.launchQueue, promise.promise);
const body = this._body;
let value: T | null = null;
if (
button.accept &&
body instanceof Widget &&
typeof body.getValue === 'function'
) {
value = body.getValue();
}
this.dispose();
promise.resolve({ button, value });
}
private _buttonNodes: ReadonlyArray<HTMLElement>;
private _buttons: ReadonlyArray<CodeSnippetForm.IButton>;
private _original: HTMLElement;
private _first: HTMLElement;
private _primary: HTMLElement;
private _promise: PromiseDelegate<CodeSnippetForm.IResult<T>> | null;
private _defaultButton: number;
private _host: HTMLElement;
private _body: CodeSnippetForm.Body<T>;
private _focusNodeSelector: string | undefined = '';
}
export namespace CodeSnippetForm {
/**
* The body input types.
*/
export type Body<T> = IBodyWidget<T> | React.ReactElement<any> | string;
/**
* The header input types.
*/
export type Header = React.ReactElement<any> | string;
/**
* A widget used as a code snippet form body.
*/
export interface IBodyWidget<T = string> extends Widget {
/**
* Get the serialized value of the widget.
*/
getValue?(): T;
}
/**
* The options used to make a button item.
*/
export interface IButton {
/**
* The label for the button.
*/
label: string;
/**
* The icon class for the button.
*/
iconClass: string;
/**
* The icon label for the button.
*/
iconLabel: string;
/**
* The caption for the button.
*/
caption: string;
/**
* The extra class name for the button.
*/
className: string;
/**
* The code snippet form action to perform when the button is clicked.
*/
accept: boolean;
/**
* The additional code snippet form actions to perform when the button is clicked.
*/
actions: Array<string>;
/**
* The button display type.
*/
displayType: 'default' | 'warn';
}
export interface IOptions<T> {
/**
* The top level text for the code snippet form. Defaults to an empty string.
*/
title: Header;
/**
* The main body element for the code snippet form to display.
* Defaults to an empty string.
*
* #### Notes
* If a widget is given as the body, it will be disposed after the
* code snippet form is resolved. If the widget has a `getValue()` method,
* the method will be called prior to disposal and the value
* will be provided as part of the code snippet form result.
* A string argument will be used as raw `textContent`.
* All `input` and `select` nodes will be wrapped and styled.
*/
body: Body<T>;
/**
* The host element for the code snippet form. Defaults to `document.body`.
*/
host: HTMLElement;
/**
* The buttons to display. Defaults to cancel and accept buttons.
*/
buttons: ReadonlyArray<IButton>;
/**
* The index of the default button. Defaults to the last button.
*/
defaultButton: number;
/**
* A selector for the primary element that should take focus in the code snippet form.
* Defaults to an empty string, causing the [[defaultButton]] to take
* focus.
*/
focusNodeSelector: string;
/**
* When "true", renders a close button for the code snippet form
*/
hasClose: boolean;
/**
* An optional renderer for code snippet form items. Defaults to a shared
* default renderer.
*/
renderer: IRenderer;
}
/**
* A code snippet form renderer.
*/
export interface IRenderer {
/**
* Create the header of the code snippet form.
*
* @param title - The title of the code snippet form.
*
* @returns A widget for the code snippet form header.
*/
createHeader<T>(
title: Header,
reject: () => void,
options: Partial<CodeSnippetForm.IOptions<T>>
): Widget;
/**
* Create the body of the code snippet form.
*
* @param value - The input value for the body.
*
* @returns A widget for the body.
*/
createBody(body: Body<any>): Widget;
/**
* Create the footer of the code snippet form.
*
* @param buttons - The button nodes to add to the footer.
*
* @returns A widget for the footer.
*/
createFooter(buttons: ReadonlyArray<HTMLElement>): Widget;
/**
* Create a button node for the code snippet form.
*
* @param button - The button data.
*
* @returns A node for the button.
*/
createButtonNode(button: IButton): HTMLElement;
}
/**
* The result of a code snippet form.
*/
export interface IResult<T> {
/**
* The button that was pressed.
*/
button: IButton;
/**
* The value retrieved from `.getValue()` if given on the widget.
*/
value: T | null;
}
/**
* Create a button item.
*/
export function createButton(value: Partial<IButton>): Readonly<IButton> {
value.accept = value.accept !== false;
const defaultLabel = value.accept ? 'OK' : 'Cancel';
return {
label: value.label || defaultLabel,
iconClass: value.iconClass || '',
iconLabel: value.iconLabel || '',
caption: value.caption || '',
className: value.className || '',
accept: value.accept,
actions: value.actions || [],
displayType: value.displayType || 'default',
};
}
/**
* Create a reject button.
*/
export function cancelButton(
options: Partial<IButton> = {}
): Readonly<IButton> {
options.accept = false;
return createButton(options);
}
/**
* Create an accept button.
*/
export function okButton(options: Partial<IButton> = {}): Readonly<IButton> {
options.accept = true;
return createButton(options);
}
/**
* Create a warn button.
*/
export function warnButton(
options: Partial<IButton> = {}
): Readonly<IButton> {
options.displayType = 'warn';
return createButton(options);
}
/**
* Disposes all code snippet form instances.
*
* #### Notes
* This function should only be used in tests or cases where application state
* may be discarded.
*/
export function flush(): void {
tracker.forEach((form) => {
form.dispose();
});
}
/**
* The default implementation of a code snippet form renderer.
*/
export class Renderer {
/**
* Create the header of the code snippet form.
*
* @param title - The title of the code snippet form.
*
* @returns A widget for the code snippet form header.
*/
createHeader<T>(
title: Header,
reject: () => void = (): void => {
/* empty */
},
options: Partial<CodeSnippetForm.IOptions<T>> = {}
): Widget {
let header: Widget;
const handleMouseDown = (event: React.MouseEvent): void => {
// Fire action only when left button is pressed.
if (event.button === 0) {
event.preventDefault();
reject();
}
};
const handleKeyDown = (event: React.KeyboardEvent): void => {
const { key } = event;
if (key === 'Enter' || key === ' ') {
reject();
}
};
if (typeof title === 'string') {
header = ReactWidget.create(
<>
{title}
{options.hasClose && (
<Button
className="jp-Dialog-close-button"
onMouseDown={handleMouseDown}
onKeyDown={handleKeyDown}
title="Cancel"
minimal
>
<LabIcon.resolveReact
icon={closeIcon}
iconClass="jp-Icon"
className="jp-ToolbarButtonComponent-icon"
tag="span"
/>
</Button>
)}
</>
);
} else {
header = ReactWidget.create(title);
}
header.addClass('jp-Dialog-header');
Styling.styleNode(header.node);
return header;
}
/**
* Create the body of the code snippet form.
*
* @param value - The input value for the body.
*
* @returns A widget for the body.
*/
createBody(value: Body<any>): Widget {
let body: Widget;
if (typeof value === 'string') {
body = new Widget({ node: document.createElement('span') });
body.node.textContent = value;
} else if (value instanceof Widget) {
body = value;
} else {
body = ReactWidget.create(value);
// Immediately update the body even though it has not yet attached in
// order to trigger a render of the DOM nodes from the React element.
MessageLoop.sendMessage(body, Widget.Msg.UpdateRequest);
}
body.addClass('jp-Dialog-body');
Styling.styleNode(body.node);
return body;
}
/**
* Create the footer of the code snippet form.
*
* @param buttonNodes - The buttons nodes to add to the footer.
*
* @returns A widget for the footer.
*/
createFooter(buttons: ReadonlyArray<HTMLElement>): Widget {
const footer = new Widget();
footer.addClass('jp-Dialog-footer');
each(buttons, (button) => {
footer.node.appendChild(button);
});
Styling.styleNode(footer.node);
return footer;
}
/**
* Create a button node for the code snippet form.
*
* @param button - The button data.
*
* @returns A node for the button.
*/
createButtonNode(button: IButton): HTMLElement {
const e = document.createElement('button');
e.className = this.createItemClass(button);
e.appendChild(this.renderIcon(button));
e.appendChild(this.renderLabel(button));
return e;
}
/**
* Create the class name for the button.
*
* @param data - The data to use for the class name.
*
* @returns The full class name for the button.
*/
createItemClass(data: IButton): string {
// Setup the initial class name.
let name = 'jp-Dialog-button';
// Add the other state classes.
if (data.accept) {
name += ' jp-mod-accept';
} else {
name += ' jp-mod-reject';
}
if (data.displayType === 'warn') {
name += ' jp-mod-warn';
}
// Add the extra class.
const extra = data.className;
if (extra) {
name += ` ${extra}`;
}
// Return the complete class name.
return name;
}
/**
* Render an icon element for a code snippet form item.
*
* @param data - The data to use for rendering the icon.
*
* @returns An HTML element representing the icon.
*/
renderIcon(data: IButton): HTMLElement {
const e = document.createElement('div');
e.className = this.createIconClass(data);
e.appendChild(document.createTextNode(data.iconLabel));
return e;
}
/**
* Create the class name for the button icon.
*
* @param data - The data to use for the class name.
*
* @returns The full class name for the item icon.
*/
createIconClass(data: IButton): string {
const name = 'jp-Dialog-buttonIcon';
const extra = data.iconClass;
return extra ? `${name} ${extra}` : name;
}
/**
* Render the label element for a button.
*
* @param data - The data to use for rendering the label.
*
* @returns An HTML element representing the item label.
*/
renderLabel(data: IButton): HTMLElement {
const e = document.createElement('div');
e.className = 'jp-Dialog-buttonLabel';
e.title = data.caption;
e.appendChild(document.createTextNode(data.label));
return e;
}
}
/**
* The default renderer instance.
*/
export const defaultRenderer = new Renderer();
/**
* The code snippet form widget tracker.
*/
export const tracker = new WidgetTracker<CodeSnippetForm<any>>({
namespace: '@jupyterlab/apputils:CodeSnippetForm',
});
}
/**
* The namespace for module private data.
*/
namespace Private {
/**
* The queue for launching code snippet forms.
*/
export const launchQueue: Promise<CodeSnippetForm.IResult<any>>[] = [];
export const errorMessagePromiseCache: Map<string, Promise<void>> = new Map();
/**
* Handle the input options for a code snippet form.
*
* @param options - The input options.
*
* @returns A new options object with defaults applied.
*/
export function handleOptions<T>(
options: Partial<CodeSnippetForm.IOptions<T>> = {}
): CodeSnippetForm.IOptions<T> {
const buttons = options.buttons || [
CodeSnippetForm.cancelButton(),
CodeSnippetForm.okButton(),
];
let defaultButton = buttons.length - 1;
if (options.defaultButton !== undefined) {
defaultButton = options.defaultButton;
}
return {
title: options.title || '',
body: options.body || '',
host: options.host || document.body,
buttons,
defaultButton,
renderer: options.renderer || CodeSnippetForm.defaultRenderer,
focusNodeSelector: options.focusNodeSelector || '',
hasClose: options.hasClose || false,
};
}
/**
* Find the first focusable item in the code snippet form.
*/
export function findFirstFocusable(node: HTMLElement): HTMLElement {
const candidateSelectors = [
'input',
'select',
'a[href]',
'textarea',
'button',
'[tabindex]',
].join(',');
return node.querySelectorAll(candidateSelectors)[0] as HTMLElement;
}
}