@@ -18,6 +18,7 @@ import { FileEditor } from '@jupyterlab/fileeditor';
1818import { Notebook , NotebookPanel } from '@jupyterlab/notebook' ;
1919import { copyIcon } from '@jupyterlab/ui-components' ;
2020import { LabIcon } from '@jupyterlab/ui-components' ;
21+ import { IEditorServices } from '@jupyterlab/codeeditor' ;
2122
2223import { 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
593633class PreviewHandler extends Widget {
594- constructor ( codeSnippet : ICodeSnippet ) {
595- super ( { node : Private . createPreviewNode ( codeSnippet ) } ) ;
634+ constructor ( ) {
635+ super ( { node : Private . createPreviewNode ( ) } ) ;
596636 }
597637}
598638
599639class 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
0 commit comments