Skip to content

Commit 2219d43

Browse files
authored
Merge pull request #55 from jahn96/editorRestore
Render code mirror to code snippet editor and restore state when the …
2 parents b7269ab + ae890dc commit 2219d43

File tree

8 files changed

+293
-279
lines changed

8 files changed

+293
-279
lines changed

src/CodeSnippetContentsService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export interface ICodeSnippet {
77
language: string;
88
// code separated by new line
99
code: string[];
10-
bookmarked: boolean;
1110
id: number;
1211
tags?: string[];
1312
}

src/CodeSnippetDisplay.tsx

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ const CODE_SNIPPET_ITEM = 'elyra-codeSnippet-item';
6363
*/
6464
const JUPYTER_CELL_MIME = 'application/vnd.jupyter.cells';
6565

66+
/**
67+
* Icons used for snippet
68+
*/
69+
const insertIcon = new LabIcon({
70+
name: 'custom-ui-compnents:insert',
71+
svgstr: insertSVGstr
72+
});
73+
74+
const previewIcon = new LabIcon({
75+
name: 'custom-ui-compnents:preview',
76+
svgstr: carrotSVGstr
77+
});
78+
6679
/**
6780
* CodeSnippetDisplay props.
6881
*/
@@ -313,9 +326,6 @@ export class CodeSnippetDisplay extends React.Component<
313326

314327
// add CSS style
315328
this._dragData.dragImage.classList.add('jp-codesnippet-drag-image');
316-
317-
console.log('draggin');
318-
console.log(event);
319329
target.addEventListener('mouseup', this._evtMouseUp, true);
320330
target.addEventListener('mousemove', this.handleDragMove, true);
321331

@@ -327,8 +337,6 @@ export class CodeSnippetDisplay extends React.Component<
327337
event.preventDefault();
328338
event.stopPropagation();
329339

330-
console.log('cancelled');
331-
332340
const target = event.target as HTMLElement;
333341

334342
target.removeEventListener('mousemove', this.handleDragMove, true);
@@ -341,18 +349,6 @@ export class CodeSnippetDisplay extends React.Component<
341349
// event.stopPropagation();
342350
const data = this._dragData;
343351

344-
// console.log(data);
345-
// console.log(event);
346-
// console.log(this);
347-
// console.log(
348-
// this.shouldStartDrag(
349-
// data.pressX,
350-
// data.pressY,
351-
// event.clientX,
352-
// event.clientY
353-
// )
354-
// );
355-
356352
if (
357353
data &&
358354
this.shouldStartDrag(
@@ -362,8 +358,6 @@ export class CodeSnippetDisplay extends React.Component<
362358
event.clientY
363359
)
364360
) {
365-
console.log('moving start');
366-
367361
const idx = (event.target as HTMLElement).id;
368362
const codeSnippet = this.state.codeSnippets[parseInt(idx)];
369363

@@ -397,36 +391,8 @@ export class CodeSnippetDisplay extends React.Component<
397391
clientX: number,
398392
clientY: number
399393
): Promise<void> {
400-
/**
401-
* TODO: check what the current widget is
402-
*/
403-
// const widget = this.props.getCurrentWidget();
404394
const target = event.target as HTMLElement;
405395

406-
// if (widget instanceof NotebookPanel) {
407-
// const notebookWidget = widget as NotebookPanel;
408-
409-
// const kernelInfo = await notebookWidget.sessionContext.session?.kernel
410-
// ?.info;
411-
// const kernelLanguage: string = kernelInfo?.language_info.name || '';
412-
413-
// if (
414-
// kernelLanguage &&
415-
// codeSnippet.language.toLowerCase() !== kernelLanguage.toLowerCase()
416-
// ) {
417-
// const result = await this.showWarnDialog(
418-
// kernelLanguage,
419-
// codeSnippet.displayName
420-
// );
421-
// if (!result.button.accept) {
422-
// target.removeEventListener('mousemove', this.handleDragMove, true);
423-
// target.removeEventListener('mouseup', this._evtMouseUp, true);
424-
// this._dragData = null;
425-
// return;
426-
// }
427-
// }
428-
// }
429-
430396
const modelFactory = new ModelFactory();
431397
const model = modelFactory.createCodeCell({});
432398
model.value.text = codeSnippet.code.join('\n');
@@ -446,8 +412,6 @@ export class CodeSnippetDisplay extends React.Component<
446412
const textContent = codeSnippet.code.join('\n');
447413
this._drag.mimeData.setData('text/plain', textContent);
448414

449-
console.log('removing events');
450-
451415
// Remove mousemove and mouseup listeners and start the drag.
452416
target.removeEventListener('mousemove', this.handleDragMove, true);
453417
target.removeEventListener('mouseup', this._evtMouseUp, true);
@@ -469,17 +433,6 @@ export class CodeSnippetDisplay extends React.Component<
469433
const buttonClasses = [ELYRA_BUTTON_CLASS, BUTTON_CLASS].join(' ');
470434
const displayName =
471435
'[' + codeSnippet.language + '] ' + codeSnippet.displayName;
472-
//this.boldNameOnSearch(this.state.filterValue,displayName,parseInt(id,10));
473-
474-
const insertIcon = new LabIcon({
475-
name: 'custom-ui-compnents:insert',
476-
svgstr: insertSVGstr
477-
});
478-
479-
const previewIcon = new LabIcon({
480-
name: 'custom-ui-compnents:preview',
481-
svgstr: carrotSVGstr
482-
});
483436

484437
const actionButtons = [
485438
{

src/CodeSnippetEditor.tsx

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import { ReactWidget, showDialog, Dialog } from '@jupyterlab/apputils';
44
import React from 'react';
55
import { Message } from '@lumino/messaging';
66
import { Button } from '@jupyterlab/ui-components';
7+
// import { DocumentWidget } from '@jupyterlab/docregistry';
78
// import { Widget } from '@lumino/widgets';
89

9-
interface ICodeSnippetEditor {
10-
namespace: string;
11-
codeSnippet: ICodeSnippet;
12-
// onSave: () => void;
13-
}
10+
// const CODE_SNIPPET_EDITOR_ID = 'jp-codeSnippet-editor';
1411

1512
export class CodeSnippetEditor extends ReactWidget {
1613
editorServices: IEditorServices;
@@ -25,17 +22,17 @@ export class CodeSnippetEditor extends ReactWidget {
2522
this._deactivateField(e, target, label)*/
2623
editor: CodeEditor.IEditor;
2724
saved: boolean;
28-
namespace: string;
2925
codeSnippet: ICodeSnippet;
26+
_hasRefreshedSinceAttach: boolean;
3027

31-
constructor(editorServices: IEditorServices, args: ICodeSnippetEditor) {
28+
constructor(editorServices: IEditorServices, args: ICodeSnippet) {
3229
super();
3330
// this.id = 'Code-Snippet-Edit';
3431

3532
this.editorServices = editorServices;
36-
this.namespace = args.namespace;
37-
this.codeSnippet = args.codeSnippet;
33+
this.codeSnippet = args;
3834
this.saved = false;
35+
this._hasRefreshedSinceAttach = false;
3936

4037
this.renderCodeInput = this.renderCodeInput.bind(this);
4138
}
@@ -45,7 +42,6 @@ export class CodeSnippetEditor extends ReactWidget {
4542
target: HTMLElement,
4643
label: HTMLElement
4744
): void {
48-
console.log(e.target);
4945
const clickedElement = e.target as HTMLElement;
5046
if (clickedElement.className !== target.className) {
5147
target.classList.remove('jp-snippet-editor-active');
@@ -66,12 +62,9 @@ export class CodeSnippetEditor extends ReactWidget {
6662
const label = document.getElementsByClassName(
6763
labelClassName
6864
)[0] as HTMLElement;
69-
console.log(target.classList);
7065
if (!target.classList.contains('jp-snippet-editor-active')) {
71-
console.log('YES REAChed.');
7266
target.classList.add('jp-snippet-editor-active');
7367
label.classList.add('jp-snippet-editor-label-active');
74-
console.log(labelClassName);
7568
}
7669
window.addEventListener(
7770
'click',
@@ -80,31 +73,27 @@ export class CodeSnippetEditor extends ReactWidget {
8073
);
8174
}
8275

83-
/**
84-
* Gets called by update() call or when first rendered
85-
* @param msg
86-
*/
87-
onActivateRequest(msg: Message): void {
88-
super.onActivateRequest(msg);
89-
console.log('updating');
76+
// updateCodeSnippetCode(): void {
77+
78+
// }
79+
80+
// /**
81+
// * Gets called by update() call or when first rendered
82+
// * @param msg
83+
// */
84+
onUpdateRequest(msg: Message): void {
85+
super.onUpdateRequest(msg);
86+
9087
if (
9188
!this.editor &&
9289
document.getElementById('code-' + this.codeSnippet.id)
9390
) {
9491
const editorFactory = this.editorServices.factoryService.newInlineEditor;
95-
9692
const getMimeTypeByLanguage = this.editorServices.mimeTypeService
9793
.getMimeTypeByLanguage;
98-
if (typeof this.codeSnippet === 'string') {
99-
this.codeSnippet = JSON.parse(this.codeSnippet);
100-
}
101-
console.log(this);
102-
// console.log(this.args);
103-
const editor = editorFactory({
104-
// config: { readOnly: false, rulers: [1, 2, 3, 4] },
105-
host: document.querySelector(
106-
`#code-${this.codeSnippet.id}`
107-
) as HTMLElement,
94+
95+
this.editor = editorFactory({
96+
host: document.getElementById('code-' + this.codeSnippet.id),
10897
model: new CodeEditor.Model({
10998
value: this.codeSnippet.code.join('\n'),
11099
mimeType: getMimeTypeByLanguage({
@@ -113,10 +102,40 @@ export class CodeSnippetEditor extends ReactWidget {
113102
})
114103
})
115104
});
116-
this.editor = editor;
105+
// this.editor.model.value.changed.connect((args: any) => {
106+
// this.
107+
// })
108+
// console.log(document.querySelector(`#code-${this.codeSnippet.id}`));
109+
}
110+
if (this.isVisible) {
111+
this._hasRefreshedSinceAttach = true;
112+
this.editor.refresh();
117113
}
118114
}
119115

116+
onAfterAttach(msg: Message): void {
117+
super.onAfterAttach(msg);
118+
119+
this._hasRefreshedSinceAttach = false;
120+
if (this.isVisible) {
121+
this.update();
122+
}
123+
}
124+
125+
onAfterShow(msg: Message): void {
126+
if (!this._hasRefreshedSinceAttach) {
127+
this.update();
128+
}
129+
}
130+
131+
/**
132+
* Initial focus on the editor when it gets activated!
133+
* @param msg
134+
*/
135+
onActivateRequest(msg: Message): void {
136+
this.editor.focus();
137+
}
138+
120139
onCloseRequest(msg: Message): void {
121140
if (this.saved) {
122141
showDialog({
@@ -150,7 +169,6 @@ export class CodeSnippetEditor extends ReactWidget {
150169
let target = event.target as HTMLElement;
151170
while (target && target.parentElement) {
152171
if (target.classList.contains('jp-codeSnippetInput-editor')) {
153-
// console.log('hello');
154172
break;
155173
}
156174
target = target.parentElement;
@@ -244,15 +262,19 @@ export class CodeSnippetEditor extends ReactWidget {
244262
<input
245263
className="jp-snippet-editor-name"
246264
defaultValue={this.codeSnippet.displayName}
247-
onClick={(event): void => this.activeFieldState(event)}
265+
onClick={(
266+
event: React.MouseEvent<HTMLInputElement, MouseEvent>
267+
): void => this.activeFieldState(event)}
248268
></input>
249269
<label className="jp-snippet-editor-description-label">
250270
Description
251271
</label>
252272
<input
253273
className="jp-snippet-editor-description"
254274
defaultValue={this.codeSnippet.description}
255-
onClick={(event): void => this.activeFieldState(event)}
275+
onClick={(
276+
event: React.MouseEvent<HTMLInputElement, MouseEvent>
277+
): void => this.activeFieldState(event)}
256278
></input>
257279
{/* <input
258280
className="jp-snippet-editor-language"

src/CodeSnippetForm.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ export function inputDialog(
7777
description: result.value[1],
7878
language: result.value[2],
7979
code: code,
80-
id: idx,
81-
bookmarked: false
80+
id: idx
8281
};
8382
/**
8483
* TODO: NEED in memory data store instead of making request every time.

src/CodeSnippetWidget.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class CodeSnippetWidget extends ReactWidget {
104104
const paths: string[] = [];
105105

106106
// Clear the current snippets
107-
// this._codeSnippetWidgetModel.clearSnippets();
107+
this._codeSnippetWidgetModel.clearSnippets();
108108

109109
// const data: ICodeSnippet[] = [];
110110
if (this._codeSnippets.length === 0) {
@@ -132,6 +132,7 @@ export class CodeSnippetWidget extends ReactWidget {
132132

133133
updateCodeSnippets(): void {
134134
this.fetchData().then((codeSnippets: ICodeSnippet[]) => {
135+
console.log(codeSnippets);
135136
if (codeSnippets !== null) {
136137
this.renderCodeSnippetsSignal.emit(codeSnippets);
137138
}

src/CodeSnippetWidgetModel.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ export class CodeSnippetWidgetModel implements ICodeSnippetWidgetModel {
6666
}
6767
}
6868

69+
clearSnippets(): void {
70+
this._snippets = [];
71+
}
72+
6973
updateSnippetContents(): void {
7074
console.log('updating snippets!');
7175
this._snippets.forEach(snippet => {

src/PreviewSnippet.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,7 @@ export namespace Preview {
425425
editButton.removeClass('jp-Preview-button-hover');
426426
};
427427
editButton.node.onclick = (): void => {
428-
openCodeSnippetEditor({
429-
namespace: codeSnippet.name,
430-
codeSnippet: codeSnippet
431-
});
428+
openCodeSnippetEditor(codeSnippet);
432429
document
433430
.getElementsByClassName('drag-hover')
434431
[prev._id].classList.remove('drag-hover-clicked');

0 commit comments

Comments
 (0)