Skip to content

Commit 877fa2c

Browse files
authored
Merge pull request #72 from kpinnipa/ui-patch
New preview made with mini codeeditor on hover.
2 parents 6463681 + 16efd6a commit 877fa2c

File tree

4 files changed

+213
-126
lines changed

4 files changed

+213
-126
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { FileEditor } from '@jupyterlab/fileeditor';
1818
import { Notebook, NotebookPanel } from '@jupyterlab/notebook';
1919
import { copyIcon } from '@jupyterlab/ui-components';
2020
import { LabIcon } from '@jupyterlab/ui-components';
21+
import { IEditorServices } from '@jupyterlab/codeeditor';
2122

2223
import { IExpandableActionButton } from '@elyra/ui-components';
2324

@@ -83,6 +84,7 @@ interface ICodeSnippetDisplayProps {
8384
codeSnippets: ICodeSnippet[];
8485
getCurrentWidget: () => Widget;
8586
openCodeSnippetEditor: (args: any) => void;
87+
editorServices: IEditorServices;
8688
}
8789

8890
/**
@@ -421,6 +423,47 @@ export class CodeSnippetDisplay extends React.Component<
421423
});
422424
}
423425

426+
private _evtMouseLeave(): void {
427+
//get rid of preview by clicking anything
428+
const preview = document.querySelector('.jp-preview');
429+
if (preview) {
430+
// if target is not the code snippet name area, then add inactive
431+
// if target area is the code snippet name area, previewSnippet widget will handle preview.
432+
if (!preview.classList.contains('inactive')) {
433+
preview.classList.add('inactive');
434+
for (const elem of document.getElementsByClassName('drag-hover')) {
435+
if (elem.classList.contains('drag-hover-clicked')) {
436+
elem.classList.remove('drag-hover-clicked');
437+
}
438+
}
439+
for (const item of document.getElementsByClassName(
440+
'codeSnippet-item'
441+
)) {
442+
if (item.classList.contains('codeSnippet-item-clicked')) {
443+
item.classList.remove('codeSnippet-item-clicked');
444+
}
445+
}
446+
}
447+
}
448+
}
449+
450+
//Set the position of the preview to be next to the snippet title.
451+
private _setPreviewPosition(id: string): void {
452+
const intID = parseInt(id, 10);
453+
const target = event.target as HTMLElement;
454+
console.log(target);
455+
const realTarget = document.getElementsByClassName(
456+
'expandableContainer-title'
457+
)[intID];
458+
// distDown is the number of pixels to shift the preview down
459+
let distDown: number = realTarget.getBoundingClientRect().top - 40;
460+
if (realTarget.getBoundingClientRect().top > window.screen.height / 2) {
461+
distDown = distDown - 66;
462+
}
463+
const final = distDown.toString(10) + 'px';
464+
document.documentElement.style.setProperty('--preview-distance', final);
465+
}
466+
424467
// Render display of code snippet list
425468
// To get the variety of color based on code length just append -long to CODE_SNIPPET_ITEM
426469
private renderCodeSnippet = (
@@ -453,10 +496,11 @@ export class CodeSnippetDisplay extends React.Component<
453496
{
454497
id: parseInt(id, 10),
455498
title: displayName,
456-
body: new PreviewHandler(codeSnippet),
499+
body: new PreviewHandler(),
457500
codeSnippet: codeSnippet
458501
},
459-
this.props.openCodeSnippetEditor
502+
this.props.openCodeSnippetEditor,
503+
this.props.editorServices
460504
);
461505
this.snippetClicked(id);
462506
}
@@ -485,31 +529,27 @@ export class CodeSnippetDisplay extends React.Component<
485529
}}
486530
></div>
487531
<div>
488-
<div
489-
key={displayName}
490-
className={TITLE_CLASS}
491-
// onMouseOver={() => {
492-
// this.dragHoverStyle(id);
493-
// }}
494-
// onMouseOut={() => {
495-
// this.dragHoverStyleRemove(id);
496-
// }}
497-
>
532+
<div key={displayName} className={TITLE_CLASS}>
498533
<span
499534
id={id}
500535
title={codeSnippet.name}
501536
className={DISPLAY_NAME_CLASS}
502-
onClick={(): void => {
537+
onMouseEnter={(event): void => {
503538
showPreview(
504539
{
505540
id: parseInt(id, 10),
506541
title: displayName,
507-
body: new PreviewHandler(codeSnippet),
542+
body: new PreviewHandler(),
508543
codeSnippet: codeSnippet
509544
},
510-
this.props.openCodeSnippetEditor
545+
this.props.openCodeSnippetEditor,
546+
this.props.editorServices
511547
);
512548
this.snippetClicked(id);
549+
this._setPreviewPosition(id);
550+
}}
551+
onMouseLeave={(): void => {
552+
this._evtMouseLeave();
513553
}}
514554
>
515555
{this.boldNameOnSearch(this.state.filterValue, displayName)}
@@ -591,44 +631,21 @@ export class CodeSnippetDisplay extends React.Component<
591631
}
592632

593633
class PreviewHandler extends Widget {
594-
constructor(codeSnippet: ICodeSnippet) {
595-
super({ node: Private.createPreviewNode(codeSnippet) });
634+
constructor() {
635+
super({ node: Private.createPreviewNode() });
596636
}
597637
}
598638

599639
class Private {
600-
static createPreviewContent(codeSnippet: ICodeSnippet): HTMLElement {
640+
static createPreviewContent(): HTMLElement {
601641
const body = document.createElement('div');
602-
const previewContainer = document.createElement('div');
603-
const descriptionContainer = document.createElement('div');
604-
const descriptionTitle = document.createElement('h4');
605-
const description = document.createElement('text');
606-
const preview = document.createElement('text');
607-
608-
previewContainer.className = 'jp-preview-text';
609-
descriptionContainer.className = 'jp-preview-description-container';
610-
descriptionTitle.className = 'jp-preview-description-title';
611-
description.className = 'jp-preview-description';
612-
preview.className = 'jp-preview-textarea';
613-
614-
descriptionTitle.textContent = 'DESCRIPTION';
615-
description.textContent = codeSnippet.description;
616-
preview.textContent = codeSnippet.code.join('\n');
617-
618-
descriptionContainer.appendChild(descriptionTitle);
619-
descriptionContainer.appendChild(description);
620-
previewContainer.appendChild(descriptionContainer);
621-
622-
previewContainer.appendChild(preview);
623-
body.append(previewContainer);
624-
625642
return body;
626643
}
627644
/**
628645
* Create structure for preview of snippet data.
629646
*/
630-
static createPreviewNode(codeSnippet: ICodeSnippet): HTMLElement {
631-
return this.createPreviewContent(codeSnippet);
647+
static createPreviewNode(): HTMLElement {
648+
return this.createPreviewContent();
632649
}
633650
}
634651

src/CodeSnippetWidget.tsx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class CodeSnippetWidget extends ReactWidget {
7676
renderCodeSnippetsSignal: Signal<this, ICodeSnippet[]>;
7777
app: JupyterFrontEnd;
7878
codeSnippetManager: CodeSnippetContentsService;
79-
// private editorServices: IEditorServices;
79+
private editorServices: IEditorServices;
8080

8181
constructor(
8282
// codeSnippets: ICodeSnippet[],
@@ -86,7 +86,7 @@ export class CodeSnippetWidget extends ReactWidget {
8686
) {
8787
super();
8888
this.app = app;
89-
// this.editorServices = editorServices;
89+
this.editorServices = editorServices;
9090
this.getCurrentWidget = getCurrentWidget;
9191
this._codeSnippetWidgetModel = new CodeSnippetWidgetModel([]);
9292
this._codeSnippets = this._codeSnippetWidgetModel.snippets;
@@ -176,6 +176,9 @@ export class CodeSnippetWidget extends ReactWidget {
176176
case 'mousedown':
177177
this._evtMouseDown(event as IDragEvent);
178178
break;
179+
// case 'mouseleave':
180+
// this._evtMouseLeave(event as MouseEvent);
181+
// break;
179182
case 'lm-dragenter':
180183
this._evtDragEnter(event as IDragEvent);
181184
break;
@@ -242,6 +245,35 @@ export class CodeSnippetWidget extends ReactWidget {
242245
return undefined;
243246
}
244247

248+
// private _evtMouseLeave(event: MouseEvent): void {
249+
// //get rid of preview by clicking anything
250+
// const target = event.target as HTMLElement;
251+
252+
// const preview = document.querySelector('.jp-preview');
253+
// if (preview) {
254+
// // if target is not the code snippet name area, then add inactive
255+
// // if target area is the code snippet name area, previewSnippet widget will handle preview.
256+
// if (
257+
// !preview.classList.contains('inactive') &&
258+
// !target.classList.contains('expandableContainer-name')
259+
// ) {
260+
// preview.classList.add('inactive');
261+
// for (const elem of document.getElementsByClassName('drag-hover')) {
262+
// if (elem.classList.contains('drag-hover-clicked')) {
263+
// elem.classList.remove('drag-hover-clicked');
264+
// }
265+
// }
266+
// for (const item of document.getElementsByClassName(
267+
// 'codeSnippet-item'
268+
// )) {
269+
// if (item.classList.contains('codeSnippet-item-clicked')) {
270+
// item.classList.remove('codeSnippet-item-clicked');
271+
// }
272+
// }
273+
// }
274+
// }
275+
// }
276+
245277
private _evtMouseDown(event: MouseEvent): void {
246278
//get rid of preview by clicking anything
247279
const target = event.target as HTMLElement;
@@ -427,6 +459,7 @@ export class CodeSnippetWidget extends ReactWidget {
427459
codeSnippets={codeSnippets}
428460
getCurrentWidget={this.getCurrentWidget}
429461
openCodeSnippetEditor={this.openCodeSnippetEditor.bind(this)}
462+
editorServices={this.editorServices}
430463
/>
431464
</div>
432465
)}

0 commit comments

Comments
 (0)