-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCodeSnippetMessage.ts
113 lines (99 loc) · 3.1 KB
/
CodeSnippetMessage.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
// Copyright (c) 2020, jupytercalpoly
// Distributed under the terms of the BSD-3 Clause License.
// import { WidgetTracker, ReactWidget } from '@jupyterlab/apputils';
import { Dialog } from '@jupyterlab/apputils';
import { Widget } from '@lumino/widgets';
import { Message } from '@lumino/messaging';
import checkSVGstr from '../style/icon/jupyter_checkmark.svg';
/**
* The class name for message
*/
// const CONFIRM_CLASS = 'jp-codeSnippet-confirm';
const CONFIRM_CONTENT = 'jp-codeSnippet-Message-content';
const CONFIRM_BODY = 'jp-codeSnippet-Message-body';
const CODE_SNIPPET_CONFIRM_TEXT = 'jp-codeSnippet-confirm-text';
/**
* Create and show a dialog.
*
* @param options - The dialog setup options.
*
* @returns A promise that resolves with whether the dialog was accepted.
*/
export function showMessage<T>(type: string): Promise<Dialog.IResult<T>> {
const confirmMessage = new CodeSnippetMessage({
body: new MessageHandler(type),
buttons: [],
});
return confirmMessage.launch();
}
/**
* A widget used to show message.
*/
export class CodeSnippetMessage<T> extends Dialog<any> {
constructor(options: Partial<Dialog.IOptions<T>> = {}) {
super(options);
this.children().next().addClass(CONFIRM_CONTENT);
}
protected onAfterAttach(msg: Message): void {
const node = this.node;
node.addEventListener('click', this, true);
document.addEventListener('keydown', this, false);
}
/**
* A message handler invoked on an `'after-detach'` message.
*/
protected onAfterDetach(msg: Message): void {
const node = this.node;
node.removeEventListener('click', this, true);
document.removeEventListener('keydown', this, false);
}
handleEvent(event: Event): void {
switch (event.type) {
case 'keydown':
this._evtKeydown(event as KeyboardEvent);
break;
case 'click':
this._evtClick(event as MouseEvent);
break;
}
}
protected _evtKeydown(event: KeyboardEvent): void {
// Check for escape key
switch (event.key) {
case 'Escape':
event.stopPropagation();
event.preventDefault();
this.reject();
break;
}
}
}
class MessageHandler extends Widget {
constructor(type: string) {
super({ node: Private.createMessageNode(type) });
this.addClass(CONFIRM_BODY);
}
}
/**
* The namespace for module private data.
*/
namespace Private {
// create a confirm message when new snippet is created successfully
export function createMessageNode(type: string): HTMLElement {
const body = document.createElement('div');
body.innerHTML = checkSVGstr;
const messageContainer = document.createElement('div');
messageContainer.className = CODE_SNIPPET_CONFIRM_TEXT;
const message = document.createElement('text');
if (type === 'confirm') {
message.textContent = 'Saved as Snippet!';
} else if (type === 'copy') {
message.textContent = 'Saved to Clipboard!';
} else if (type === 'export') {
message.textContent = 'Exported the Snippet!';
}
messageContainer.appendChild(message);
body.append(messageContainer);
return body;
}
}