Skip to content

Commit d969511

Browse files
committed
Remove the restrictions in name and description of a snippet and fixed a bug in codeSnippetEditor
1 parent 500a9c8 commit d969511

File tree

6 files changed

+26
-71
lines changed

6 files changed

+26
-71
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ import {
9494
qsharpIcon,
9595
sasIcon,
9696
} from './CodeSnippetLanguages';
97+
import { ICodeSnippetEditorMetadata } from './CodeSnippetEditor';
9798

9899
/**
99100
* The CSS class added to code snippet widget.
@@ -153,7 +154,7 @@ interface ICodeSnippetDisplayProps {
153154
codeSnippetManager: CodeSnippetService;
154155
app: JupyterFrontEnd;
155156
getCurrentWidget: () => Widget;
156-
openCodeSnippetEditor: (args: any) => void;
157+
openCodeSnippetEditor: (args: ICodeSnippetEditorMetadata) => void;
157158
editorServices: IEditorServices;
158159
updateCodeSnippetWidget: () => void;
159160
}
@@ -631,7 +632,6 @@ export class CodeSnippetDisplay extends React.Component<
631632
): void {
632633
const target = event.target as HTMLElement;
633634
let top: number;
634-
console.log(target.tagName);
635635
if (target.tagName === 'path') {
636636
top = target.getBoundingClientRect().top + 10;
637637
} else {
@@ -1476,7 +1476,8 @@ export class CodeSnippetDisplay extends React.Component<
14761476
description: '',
14771477
language: 'Python',
14781478
code: [],
1479-
id: 0,
1479+
id: this.state.codeSnippets.length,
1480+
selectedTags: [],
14801481
allTags: this.getActiveTags(),
14811482
fromScratch: true,
14821483
});

src/CodeSnippetEditor.tsx

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ const CODE_SNIPPET_EDITOR_MIRROR = 'jp-codeSnippetInput-editor';
4949
const CODE_SNIPPET_EDITOR_INPUTAREA = 'jp-codeSnippetInputArea';
5050
const CODE_SNIPPET_EDITOR_INPUTAREA_MIRROR = 'jp-codeSnippetInputArea-editor';
5151

52-
const CODE_SNIPPET_EDITOR_INPUTNAME_VALIDITY =
53-
'jp-codeSnippet-inputName-validity';
54-
const CODE_SNIPPET_EDITOR_INPUTDESC_VALIDITY =
55-
'jp-codeSnippet-inputDesc-validity';
56-
5752
const EDITOR_DIRTY_CLASS = 'jp-mod-dirty';
5853

5954
export interface ICodeSnippetEditorMetadata {
@@ -85,7 +80,6 @@ export class CodeSnippetEditor extends ReactWidget {
8580
args: ICodeSnippetEditorMetadata
8681
) {
8782
super();
88-
8983
this.addClass(CODE_SNIPPET_EDITOR);
9084
this.contentsService = CodeSnippetService.getCodeSnippetService();
9185
this.editorServices = editorServices;
@@ -364,18 +358,6 @@ export class CodeSnippetEditor extends ReactWidget {
364358
message += 'Name must be filled out\n';
365359
status = false;
366360
}
367-
if (name.match(/[^a-zA-Z0-9_]+/)) {
368-
message += 'Wrong format of the name\n';
369-
status = false;
370-
}
371-
// if (description === '') {
372-
// message += 'Description must be filled out\n';
373-
// status = false;
374-
// }
375-
if (description.match(/[^a-zA-Z0-9_ ,.?!]+/)) {
376-
message += 'Wrong format of the description\n';
377-
status = false;
378-
}
379361
if (language === '') {
380362
message += 'Language must be filled out';
381363
status = false;
@@ -579,10 +561,9 @@ export class CodeSnippetEditor extends ReactWidget {
579561
<input
580562
className={CODE_SNIPPET_EDITOR_NAME_INPUT}
581563
defaultValue={this._codeSnippetEditorMetaData.name}
582-
placeholder={'Ex. starter_code'}
564+
placeholder={'Ex. starter code'}
583565
type="text"
584566
required
585-
pattern={'[a-zA-Z0-9_]+'}
586567
onMouseDown={(
587568
event: React.MouseEvent<HTMLInputElement, MouseEvent>
588569
): void => this.activeFieldState(event)}
@@ -591,11 +572,6 @@ export class CodeSnippetEditor extends ReactWidget {
591572
}}
592573
onBlur={this.handleOnBlur}
593574
></input>
594-
<p className={CODE_SNIPPET_EDITOR_INPUTNAME_VALIDITY}>
595-
{
596-
'Name of the code snippet MUST be alphanumeric or composed of underscore(_)'
597-
}
598-
</p>
599575
<label className={CODE_SNIPPET_EDITOR_LABEL}>
600576
Description (optional)
601577
</label>
@@ -604,7 +580,6 @@ export class CodeSnippetEditor extends ReactWidget {
604580
defaultValue={this._codeSnippetEditorMetaData.description}
605581
placeholder={'Description'}
606582
type="text"
607-
pattern={'[a-zA-Z0-9_ ,.?!]+'}
608583
onMouseDown={(
609584
event: React.MouseEvent<HTMLInputElement, MouseEvent>
610585
): void => this.activeFieldState(event)}
@@ -613,11 +588,6 @@ export class CodeSnippetEditor extends ReactWidget {
613588
}}
614589
onBlur={this.handleOnBlur}
615590
></input>
616-
<p className={CODE_SNIPPET_EDITOR_INPUTDESC_VALIDITY}>
617-
{
618-
'Description of the code snippet MUST be alphanumeric but can include space or punctuation'
619-
}
620-
</p>
621591
<label className={CODE_SNIPPET_EDITOR_LABEL}>
622592
Language (required)
623593
</label>

src/CodeSnippetInputDialog.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ const FILE_DIALOG_CLASS = 'jp-codeSnippet-fileDialog';
2727
*/
2828
const CODE_SNIPPET_DIALOG_INPUT = 'jp-codeSnippet-dialog-input';
2929
const CODE_SNIPPET_INPUTTAG_PLUS_ICON = 'jp-codeSnippet-inputTag-plusIcon';
30-
const CODE_SNIPPET_INPUTNAME_VALIDITY = 'jp-codeSnippet-inputName-validity';
31-
const CODE_SNIPPET_INPUTDESC_VALIDITY = 'jp-codeSnippet-inputDesc-validity';
3230
const CODE_SNIPPET_INPUTTAG_LIST = 'jp-codeSnippet-inputTagList';
3331
const CODE_SNIPPET_INPUT_TAG = 'jp-codeSnippet-inputTag';
3432
const CODE_SNIPPET_INPUT_TAG_CHECK = 'jp-codeSnippet-inputTag-check';
@@ -229,23 +227,12 @@ export function validateForm(
229227
let status = true;
230228
let message = '';
231229
const name = input.value[0];
232-
const description = input.value[1];
233230
const language = input.value[2];
234231

235232
if (name === '') {
236233
message += 'Name must be filled out\n';
237234
status = false;
238235
}
239-
if (name.match(/[^a-zA-Z0-9_]+/)) {
240-
//allow lowercase, uppercase, alphanumeric, and underscore
241-
message += 'Wrong format of the name\n';
242-
status = false;
243-
}
244-
if (description.match(/[^a-zA-Z0-9_ ,.?!]+/)) {
245-
//alphanumeric but can include space or punctuation
246-
message += 'Wrong format of the description\n';
247-
status = false;
248-
}
249236
if (language === '') {
250237
message += 'Language must be filled out\n';
251238
status = false;
@@ -254,6 +241,7 @@ export function validateForm(
254241
message += 'Language must be one of the options';
255242
status = false;
256243
}
244+
// TODO: change it to a better UI
257245
if (status === false) {
258246
alert(message);
259247
}
@@ -315,29 +303,20 @@ class Private {
315303
static createInputNode(tags: string[]): HTMLElement {
316304
Private.allTags = tags;
317305
const body = document.createElement('form');
318-
const nameValidity = document.createElement('p');
319-
nameValidity.textContent =
320-
'Name of the code snippet MUST be alphanumeric, or composed of underscore(_)';
321-
nameValidity.className = CODE_SNIPPET_INPUTNAME_VALIDITY;
322-
323-
const descriptionValidity = document.createElement('p');
324-
descriptionValidity.textContent =
325-
'Description of the code snippet MUST be alphanumeric but can include space or punctuation';
326-
descriptionValidity.className = CODE_SNIPPET_INPUTDESC_VALIDITY;
327306

328307
const nameTitle = document.createElement('label');
329308
nameTitle.textContent = 'Snippet Name (required)';
330309
const name = document.createElement('input');
331310
name.className = CODE_SNIPPET_DIALOG_INPUT;
332311
name.required = true;
333-
name.pattern = '[a-zA-Z0-9_]+';
312+
name.placeholder = 'Ex. starter code';
334313
name.onblur = Private.handleOnBlur;
335314

336315
const descriptionTitle = document.createElement('label');
337316
descriptionTitle.textContent = 'Description (optional)';
338317
const description = document.createElement('input');
339318
description.className = CODE_SNIPPET_DIALOG_INPUT;
340-
description.pattern = '[a-zA-Z0-9_ ,.?!]+';
319+
description.placeholder = 'Description';
341320
description.onblur = Private.handleOnBlur;
342321

343322
const languageTitle = document.createElement('label');
@@ -391,10 +370,8 @@ class Private {
391370

392371
body.appendChild(nameTitle);
393372
body.appendChild(name);
394-
body.appendChild(nameValidity);
395373
body.appendChild(descriptionTitle);
396374
body.appendChild(description);
397-
body.appendChild(descriptionValidity);
398375
body.appendChild(languageTitle);
399376
body.appendChild(languageInput);
400377
body.appendChild(languageOption);

src/CodeSnippetWidget.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export class CodeSnippetWidget extends ReactWidget {
336336
codeSnippetManager={this.codeSnippetManager}
337337
app={this.app}
338338
getCurrentWidget={this.getCurrentWidget}
339-
openCodeSnippetEditor={this.openCodeSnippetEditor.bind(this)}
339+
openCodeSnippetEditor={this.openCodeSnippetEditor}
340340
editorServices={this.editorServices}
341341
updateCodeSnippetWidget={this.updateCodeSnippetWidget}
342342
/>

src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ import {
4040
ICodeSnippetEditorMetadata,
4141
} from './CodeSnippetEditor';
4242
import { CodeSnippetService } from './CodeSnippetService';
43-
// import { ServerConnection, SettingManager } from '@jupyterlab/services';
44-
// import { URLExt } from '@jupyterlab/coreutils';
4543

4644
const CODE_SNIPPET_EXTENSION_ID = 'code-snippet-extension';
4745

@@ -104,12 +102,14 @@ function activateCodeSnippet(
104102
// codeSnippetEditors are in the main area
105103
const widgetId = `jp-codeSnippet-editor-${args.id}`;
106104

105+
// when the editor is already open
107106
const openEditor = find(
108107
app.shell.widgets('main'),
109108
(widget: Widget, _: number) => {
110109
return widget.id === widgetId;
111110
}
112111
);
112+
113113
if (openEditor) {
114114
app.shell.activateById(widgetId);
115115
return;
@@ -199,15 +199,18 @@ function activateCodeSnippet(
199199
resultArray.push(indexedInput[i]); //push cell code lines into result
200200
}
201201
}
202-
CodeSnippetInputDialog(codeSnippetWidget, resultArray, 0);
202+
CodeSnippetInputDialog(
203+
codeSnippetWidget,
204+
resultArray,
205+
codeSnippetWidget.codeSnippetManager.snippets.length
206+
);
203207
} else {
204208
CodeSnippetInputDialog(
205209
codeSnippetWidget,
206210
highlightedCode.split('\n'),
207-
0
211+
codeSnippetWidget.codeSnippetManager.snippets.length
208212
);
209213
}
210-
// if highlightedCode is empty, check the code of the entire cell.
211214
},
212215
});
213216

@@ -266,6 +269,7 @@ function activateCodeSnippet(
266269
id: editorMetadata.id,
267270
selectedTags: editorMetadata.selectedTags,
268271
allTags: editorMetadata.allTags,
272+
fromScratch: editorMetadata.fromScratch,
269273
};
270274
},
271275
name: (widget) => {

style/base.css

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@
245245
margin-left: 2%;
246246
margin-bottom: 5px;
247247
font-size: var(--jp-ui-font-size1);
248-
color: var(--jp-ui-font-color2);
248+
color: var(--jp-ui-font-color0);
249249
}
250250

251251
.jp-codeSnippet-editor-name,
@@ -270,6 +270,7 @@
270270
-webkit-appearance: none;
271271
-moz-appearance: none;
272272
margin-top: 5px;
273+
margin-bottom: 15px;
273274
}
274275

275276
.jp-codeSnippet-editor-language {
@@ -376,7 +377,7 @@
376377
}
377378

378379
.jp-codeSnippetInputArea .saveBtn {
379-
width: 8%;
380+
width: 10%;
380381
background: var(--jp-brand-color1);
381382
color: var(--jp-ui-inverse-font-color0);
382383
align-self: flex-end;
@@ -513,9 +514,6 @@ mark.jp-codeSnippet-search-bolding {
513514
width: 50px;
514515
}
515516

516-
/* Cell tag styling */
517-
/* @import url('~@jupyterlab/celltags/style/base.css'); */
518-
519517
.jp-codeSnippet-tags {
520518
margin-top: 8px;
521519
}
@@ -555,6 +553,11 @@ mark.jp-codeSnippet-search-bolding {
555553
border: var(--jp-border-width) solid var(--jp-error-color1);
556554
}
557555

556+
.jp-codeSnippet-dialog-input.jp-mod-styled {
557+
margin-bottom: 10px;
558+
font-size: var(--jp-ui-font-size1);
559+
}
560+
558561
.jp-codeSnippet-inputName-validity,
559562
.jp-codeSnippet-inputDesc-validity {
560563
font-size: var(--jp-ui-font-size0);

0 commit comments

Comments
 (0)