Skip to content

Commit 7882d05

Browse files
committed
Clean and refactor code base
1 parent 016a2ab commit 7882d05

6 files changed

+392
-57
lines changed

src/CodeSnippetConfirmMessage.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) 2020, jupytercalpoly
2+
// Distributed under the terms of the BSD-3 Clause License.
3+
4+
// import { WidgetTracker, ReactWidget } from '@jupyterlab/apputils';
5+
import { Dialog } from '@jupyterlab/apputils';
6+
7+
import { Widget } from '@lumino/widgets';
8+
import { Message } from '@lumino/messaging';
9+
// import { PromiseDelegate } from '@lumino/coreutils';
10+
// import { ArrayExt } from '@lumino/algorithm';
11+
12+
import checkSVGstr from '../style/icon/jupyter_checkmark.svg';
13+
14+
/**
15+
* The class name for confirmation box
16+
*/
17+
// const CONFIRM_CLASS = 'jp-codeSnippet-confirm';
18+
const CONFIRM_CONTENT = 'jp-codeSnippet-Message-content';
19+
const CONFIRM_BODY = 'jp-codeSnippet-Message-body';
20+
const CODE_SNIPPET_CONFIRM_TEXT = 'jp-codeSnippet-confirm-text';
21+
22+
/**
23+
* Create and show a dialog.
24+
*
25+
* @param options - The dialog setup options.
26+
*
27+
* @returns A promise that resolves with whether the dialog was accepted.
28+
*/
29+
export function showMessage<T>(): Promise<Dialog.IResult<T>> {
30+
const confirmMessage = new ConfirmMessage({
31+
body: new MessageHandler(),
32+
buttons: [],
33+
});
34+
return confirmMessage.launch();
35+
}
36+
37+
/**
38+
* A widget used to show confirmation message.
39+
*/
40+
export class ConfirmMessage<T> extends Dialog<any> {
41+
constructor(options: Partial<Dialog.IOptions<T>> = {}) {
42+
super(options);
43+
this.children().next().addClass(CONFIRM_CONTENT);
44+
}
45+
46+
protected onAfterAttach(msg: Message): void {
47+
const node = this.node;
48+
node.addEventListener('click', this, true);
49+
}
50+
51+
/**
52+
* A message handler invoked on an `'after-detach'` message.
53+
*/
54+
protected onAfterDetach(msg: Message): void {
55+
const node = this.node;
56+
node.removeEventListener('click', this, true);
57+
}
58+
}
59+
60+
class MessageHandler extends Widget {
61+
constructor() {
62+
super({ node: Private.createConfirmMessageNode() });
63+
this.addClass(CONFIRM_BODY);
64+
}
65+
}
66+
67+
/**
68+
* The namespace for module private data.
69+
*/
70+
namespace Private {
71+
// create a confirm message when new snippet is created successfully
72+
export function createConfirmMessageNode(): HTMLElement {
73+
const body = document.createElement('div');
74+
body.innerHTML = checkSVGstr;
75+
76+
const messageContainer = document.createElement('div');
77+
messageContainer.className = CODE_SNIPPET_CONFIRM_TEXT;
78+
const message = document.createElement('text');
79+
message.textContent = 'Saved as Snippet!';
80+
messageContainer.appendChild(message);
81+
body.append(messageContainer);
82+
return body;
83+
}
84+
}

src/CodeSnippetDisplay.tsx

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ import {
3636
import { CodeEditor, IEditorServices } from '@jupyterlab/codeeditor';
3737
import * as nbformat from '@jupyterlab/nbformat';
3838
import { JupyterFrontEnd } from '@jupyterlab/application';
39-
import {
40-
Cell,
41-
CodeCellModel,
42-
ICodeCellModel,
43-
MarkdownCell,
44-
CodeCell,
45-
} from '@jupyterlab/cells';
39+
import { CodeCellModel, MarkdownCell, CodeCell } from '@jupyterlab/cells';
4640

4741
import { Widget } from '@lumino/widgets';
4842
import { find, StringExt } from '@lumino/algorithm';
@@ -51,8 +45,8 @@ import { MimeData } from '@lumino/coreutils';
5145

5246
import React from 'react';
5347
import { CodeSnippetService, ICodeSnippet } from './CodeSnippetService';
54-
import { FilterTools } from './FilterTools';
55-
import { showPreview } from './PreviewSnippet';
48+
import { FilterTools } from './CodeSnippetFilterTools';
49+
import { showPreview } from './CodeSnippetPreview';
5650
import { showMoreOptions } from './CodeSnippetMenu';
5751

5852
import { CodeSnippetContentsService } from './CodeSnippetContentsService';
@@ -127,6 +121,7 @@ const CODE_SNIPPET_MORE_OTPIONS_DOWNLOAD =
127121
'jp-codeSnippet-more-options-download';
128122
const CODE_SNIPPET_CREATE_NEW_BTN = 'jp-createSnippetBtn';
129123
const CODE_SNIPPET_NAME = 'jp-codeSnippet-name';
124+
const OPTIONS_BODY = 'jp-codeSnippet-options-body';
130125

131126
/**
132127
* The threshold in pixels to start a drag event.
@@ -695,8 +690,7 @@ export class CodeSnippetDisplay extends React.Component<
695690
): Promise<void> {
696691
const target = event.target as HTMLElement;
697692

698-
const modelFactory = new ModelFactory();
699-
const model = modelFactory.createCodeCell({});
693+
const model = new CodeCellModel({});
700694
model.value.text = codeSnippet.code.join('\n');
701695
model.metadata;
702696

@@ -1295,7 +1289,11 @@ export class CodeSnippetDisplay extends React.Component<
12951289
title: 'Insert, copy, edit, and delete',
12961290
icon: moreOptionsIcon,
12971291
onClick: (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
1298-
showMoreOptions({ body: new OptionsHandler(this, codeSnippet) });
1292+
console.log('three dots clicked');
1293+
showMoreOptions({
1294+
body: new OptionsHandler(this, codeSnippet),
1295+
// buttons: [],
1296+
});
12991297
this._setOptionsPosition(event);
13001298
},
13011299
},
@@ -1316,7 +1314,10 @@ export class CodeSnippetDisplay extends React.Component<
13161314
event: React.MouseEvent<HTMLElement, MouseEvent>
13171315
): void => {
13181316
event.preventDefault();
1319-
showMoreOptions({ body: new OptionsHandler(this, codeSnippet) });
1317+
showMoreOptions({
1318+
body: new OptionsHandler(this, codeSnippet),
1319+
// buttons: [],
1320+
});
13201321
this._setOptionsPosition(event);
13211322
}}
13221323
>
@@ -1456,11 +1457,10 @@ export class CodeSnippetDisplay extends React.Component<
14561457
title: 'Delete snippet?',
14571458
body: 'Are you sure you want to delete "' + codeSnippet.name + '"? ',
14581459
buttons: [
1459-
Dialog.okButton({
1460+
Dialog.cancelButton(),
1461+
Dialog.warnButton({
14601462
label: 'Delete',
1461-
displayType: 'warn',
14621463
}),
1463-
Dialog.cancelButton(),
14641464
],
14651465
}).then((response: any): void => {
14661466
if (response.button.accept) {
@@ -1533,6 +1533,7 @@ export class CodeSnippetDisplay extends React.Component<
15331533
// create dropdown menu
15341534
public createOptionsNode(codeSnippet: ICodeSnippet): HTMLElement {
15351535
const body = document.createElement('div');
1536+
body.className = OPTIONS_BODY;
15361537

15371538
const optionsContainer = document.createElement('div');
15381539
optionsContainer.className = CODE_SNIPPET_MORE_OTPIONS_CONTENT;
@@ -1677,40 +1678,3 @@ class Private {
16771678
return this.createPreviewContent();
16781679
}
16791680
}
1680-
1681-
/**
1682-
* A content factory for console children.
1683-
*/
1684-
export interface IContentFactory extends Cell.IContentFactory {
1685-
/**
1686-
* Create a new code cell widget.
1687-
*/
1688-
createCodeCell(options: CodeCell.IOptions): CodeCell;
1689-
}
1690-
1691-
/**
1692-
* The default implementation of an `IModelFactory`.
1693-
*/
1694-
export class ModelFactory {
1695-
/**
1696-
* The factory for output area models.
1697-
*/
1698-
readonly codeCellContentFactory: CodeCellModel.IContentFactory;
1699-
1700-
/**
1701-
* Create a new code cell.
1702-
*
1703-
* @param source - The data to use for the original source data.
1704-
*
1705-
* @returns A new code cell. If a source cell is provided, the
1706-
* new cell will be initialized with the data from the source.
1707-
* If the contentFactory is not provided, the instance
1708-
* `codeCellContentFactory` will be used.
1709-
*/
1710-
createCodeCell(options: CodeCellModel.IOptions): ICodeCellModel {
1711-
if (!options.contentFactory) {
1712-
options.contentFactory = this.codeCellContentFactory;
1713-
}
1714-
return new CodeCellModel(options);
1715-
}
1716-
}

src/CodeSnippetEditor.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import { CodeSnippetService } from './CodeSnippetService';
3232
import { CodeSnippetWidget } from './CodeSnippetWidget';
3333
import { SUPPORTED_LANGUAGES } from './CodeSnippetLanguages';
3434
import { CodeSnippetEditorTags } from './CodeSnippetEditorTags';
35-
import { saveOverWriteFile } from './CodeSnippetInputDialog';
36-
import { validateInputs } from './CodeSnippetUtilities';
35+
import { showMessage } from './CodeSnippetConfirmMessage';
36+
import { validateInputs, saveOverWriteFile } from './CodeSnippetUtilities';
3737

3838
/**
3939
* CSS style classes
@@ -393,6 +393,7 @@ export class CodeSnippetEditor extends ReactWidget {
393393
return false;
394394
}
395395
});
396+
showMessage();
396397
}
397398
}
398399
// modify existing snippet

src/CodeSnippetEditorTags.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class CodeSnippetEditorTags extends React.Component<
112112
addTagOnKeyDown(event: React.KeyboardEvent<HTMLInputElement>): void {
113113
const inputElement = event.target as HTMLInputElement;
114114

115-
if (inputElement.value !== '' && event.keyCode === 13) {
115+
if (inputElement.value !== '' && event.key === 'Enter') {
116116
if (this.state.tags.includes(inputElement.value)) {
117117
alert('Duplicate Tag Name!');
118118
return;
@@ -190,7 +190,6 @@ export class CodeSnippetEditorTags extends React.Component<
190190
{hasTags
191191
? this.state.tags.map((tag: string, index: number) =>
192192
((): JSX.Element => {
193-
console.log(this.state.tags);
194193
if (!this.state.selectedTags) {
195194
return (
196195
<ul

0 commit comments

Comments
 (0)