Skip to content

Commit b65542c

Browse files
authored
Merge pull request #186 from jahn96/codeRefactor
Refactor and clean codebase
2 parents 7b30354 + 3896154 commit b65542c

14 files changed

+327
-1576
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 17 additions & 54 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';
@@ -98,6 +92,7 @@ import {
9892
sasIcon,
9993
} from './CodeSnippetLanguages';
10094
import { ICodeSnippetEditorMetadata } from './CodeSnippetEditor';
95+
import { showMessage } from './CodeSnippetMessage';
10196

10297
/**
10398
* The CSS class added to code snippet widget.
@@ -127,6 +122,7 @@ const CODE_SNIPPET_MORE_OTPIONS_DOWNLOAD =
127122
'jp-codeSnippet-more-options-download';
128123
const CODE_SNIPPET_CREATE_NEW_BTN = 'jp-createSnippetBtn';
129124
const CODE_SNIPPET_NAME = 'jp-codeSnippet-name';
125+
const OPTIONS_BODY = 'jp-codeSnippet-options-body';
130126

131127
/**
132128
* The threshold in pixels to start a drag event.
@@ -695,8 +691,7 @@ export class CodeSnippetDisplay extends React.Component<
695691
): Promise<void> {
696692
const target = event.target as HTMLElement;
697693

698-
const modelFactory = new ModelFactory();
699-
const model = modelFactory.createCodeCell({});
694+
const model = new CodeCellModel({});
700695
model.value.text = codeSnippet.code.join('\n');
701696
model.metadata;
702697

@@ -1295,7 +1290,9 @@ export class CodeSnippetDisplay extends React.Component<
12951290
title: 'Insert, copy, edit, and delete',
12961291
icon: moreOptionsIcon,
12971292
onClick: (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
1298-
showMoreOptions({ body: new OptionsHandler(this, codeSnippet) });
1293+
showMoreOptions({
1294+
body: new OptionsHandler(this, codeSnippet),
1295+
});
12991296
this._setOptionsPosition(event);
13001297
},
13011298
},
@@ -1316,7 +1313,9 @@ export class CodeSnippetDisplay extends React.Component<
13161313
event: React.MouseEvent<HTMLElement, MouseEvent>
13171314
): void => {
13181315
event.preventDefault();
1319-
showMoreOptions({ body: new OptionsHandler(this, codeSnippet) });
1316+
showMoreOptions({
1317+
body: new OptionsHandler(this, codeSnippet),
1318+
});
13201319
this._setOptionsPosition(event);
13211320
}}
13221321
>
@@ -1456,11 +1455,10 @@ export class CodeSnippetDisplay extends React.Component<
14561455
title: 'Delete snippet?',
14571456
body: 'Are you sure you want to delete "' + codeSnippet.name + '"? ',
14581457
buttons: [
1459-
Dialog.okButton({
1458+
Dialog.cancelButton(),
1459+
Dialog.warnButton({
14601460
label: 'Delete',
1461-
displayType: 'warn',
14621461
}),
1463-
Dialog.cancelButton(),
14641462
],
14651463
}).then((response: any): void => {
14661464
if (response.button.accept) {
@@ -1518,6 +1516,7 @@ export class CodeSnippetDisplay extends React.Component<
15181516
format: 'text',
15191517
content: JSON.stringify(codeSnippet),
15201518
});
1519+
showMessage('download');
15211520
}
15221521
});
15231522
}
@@ -1533,6 +1532,7 @@ export class CodeSnippetDisplay extends React.Component<
15331532
// create dropdown menu
15341533
public createOptionsNode(codeSnippet: ICodeSnippet): HTMLElement {
15351534
const body = document.createElement('div');
1535+
body.className = OPTIONS_BODY;
15361536

15371537
const optionsContainer = document.createElement('div');
15381538
optionsContainer.className = CODE_SNIPPET_MORE_OTPIONS_CONTENT;
@@ -1548,7 +1548,7 @@ export class CodeSnippetDisplay extends React.Component<
15481548
copySnip.textContent = 'Copy snippet to clipboard';
15491549
copySnip.onclick = (): void => {
15501550
Clipboard.copyToSystem(codeSnippet.code.join('\n'));
1551-
alert('saved to clipboard');
1551+
showMessage('copy');
15521552
this.removeOptionsNode();
15531553
};
15541554
const editSnip = document.createElement('div');
@@ -1677,40 +1677,3 @@ class Private {
16771677
return this.createPreviewContent();
16781678
}
16791679
}
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: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +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';
35+
import { showMessage } from './CodeSnippetMessage';
36+
import { validateInputs, saveOverWriteFile } from './CodeSnippetUtilities';
3637

3738
/**
3839
* CSS style classes
@@ -266,7 +267,7 @@ export class CodeSnippetEditor extends ReactWidget {
266267
`.${CODE_SNIPPET_EDITOR}-${this._codeSnippetEditorMetaData.id} .${CODE_SNIPPET_EDITOR_LANG_INPUT}`
267268
) as HTMLSelectElement).value;
268269

269-
const validity = this.validateInputs(name, description, language);
270+
const validity = validateInputs(name, description, language);
270271
if (validity) {
271272
this.updateSnippet().then((value) => {
272273
if (value) {
@@ -342,37 +343,12 @@ export class CodeSnippetEditor extends ReactWidget {
342343
`.${CODE_SNIPPET_EDITOR}-${this._codeSnippetEditorMetaData.id} .${CODE_SNIPPET_EDITOR_LANG_INPUT}`
343344
) as HTMLSelectElement).value;
344345

345-
const validity = this.validateInputs(name, description, language);
346+
const validity = validateInputs(name, description, language);
346347
if (validity) {
347348
this.updateSnippet();
348349
}
349350
}
350351

351-
private validateInputs(
352-
name: string,
353-
description: string,
354-
language: string
355-
): boolean {
356-
let status = true;
357-
let message = '';
358-
if (name === '') {
359-
message += 'Name must be filled out\n';
360-
status = false;
361-
}
362-
if (language === '') {
363-
message += 'Language must be filled out';
364-
status = false;
365-
}
366-
if (!SUPPORTED_LANGUAGES.includes(language)) {
367-
message += 'Language must be one of the options';
368-
status = false;
369-
}
370-
if (status === false) {
371-
alert(message);
372-
}
373-
return status;
374-
}
375-
376352
async updateSnippet(): Promise<boolean> {
377353
const name = (document.querySelector(
378354
`.${CODE_SNIPPET_EDITOR}-${this._codeSnippetEditorMetaData.id} .${CODE_SNIPPET_EDITOR_NAME_INPUT}`
@@ -417,6 +393,7 @@ export class CodeSnippetEditor extends ReactWidget {
417393
return false;
418394
}
419395
});
396+
showMessage('confirm');
420397
}
421398
}
422399
// 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
File renamed without changes.

0 commit comments

Comments
 (0)