@@ -4,7 +4,10 @@ import {
4
4
ILayoutRestorer
5
5
} from '@jupyterlab/application' ;
6
6
import { ICommandPalette , WidgetTracker } from '@jupyterlab/apputils' ;
7
- import { IDocumentManager } from '@jupyterlab/docmanager' ;
7
+ import {
8
+ IDocumentManager ,
9
+ IDocumentWidgetOpener
10
+ } from '@jupyterlab/docmanager' ;
8
11
import { NotebookPanel } from '@jupyterlab/notebook' ;
9
12
10
13
import { LeetCodeMainWidget , LeetCodeHeaderWidget } from './widget' ;
@@ -18,12 +21,13 @@ const plugin: JupyterFrontEndPlugin<void> = {
18
21
id : PLUGIN_ID ,
19
22
description : 'Integrate LeetCode into beloved Jupyter.' ,
20
23
autoStart : true ,
21
- requires : [ ICommandPalette , IDocumentManager ] ,
24
+ requires : [ ICommandPalette , IDocumentManager , IDocumentWidgetOpener ] ,
22
25
optional : [ ILayoutRestorer ] ,
23
26
activate : (
24
27
app : JupyterFrontEnd ,
25
28
palette : ICommandPalette ,
26
29
docManager : IDocumentManager ,
30
+ docWidgetOpener : IDocumentWidgetOpener ,
27
31
restorer : ILayoutRestorer | null
28
32
) => {
29
33
let leetcodeWidget : LeetCodeMainWidget ;
@@ -45,28 +49,26 @@ const plugin: JupyterFrontEndPlugin<void> = {
45
49
}
46
50
} ) ;
47
51
palette . addItem ( { command, category : 'LeetCode' } ) ;
48
-
49
- const addHeaderCommand = 'leetcode-widget:add-header' ;
50
- app . commands . addCommand ( addHeaderCommand , {
51
- label : 'Add Header to LeetCode Widget' ,
52
- caption : 'Add Header to LeetCode Widget' ,
53
- execute : ( ) => {
54
- const main = app . shell . currentWidget ;
55
- if ( main instanceof NotebookPanel ) {
56
- const widget = new LeetCodeHeaderWidget ( main ) ;
57
- widget . node . style . minHeight = '20px' ;
58
- main . contentHeader . addWidget ( widget ) ;
59
- }
60
- }
61
- } ) ;
62
- palette . addItem ( { command : addHeaderCommand , category : 'LeetCode' } ) ;
63
-
64
52
const tracker = new WidgetTracker < LeetCodeMainWidget > ( {
65
53
namespace : 'leetcode-widget'
66
54
} ) ;
67
55
if ( restorer ) {
68
56
restorer . restore ( tracker , { command, name : ( ) => 'leetcode' } ) ;
69
57
}
58
+
59
+ docWidgetOpener . opened . connect ( ( sender , widget ) => {
60
+ if ( widget instanceof NotebookPanel ) {
61
+ widget . revealed . then ( ( ) => {
62
+ if ( widget . model ?. metadata ?. leetcode_question_info ) {
63
+ const header = new LeetCodeHeaderWidget ( widget ) ;
64
+ header . node . style . minHeight = '20px' ;
65
+ if ( widget . contentHeader . widgets . every ( w => w . id !== header . id ) ) {
66
+ widget . contentHeader . addWidget ( header ) ;
67
+ }
68
+ }
69
+ } ) ;
70
+ }
71
+ } ) ;
70
72
}
71
73
} ;
72
74
0 commit comments