@@ -29,110 +29,128 @@ interface CreateCommandProps {
2929position :any
3030}
3131
32- // React panel webview
33- let webview :any
32+ export const createCommands = ( { context, machine, storage, git, position} :CreateCommandProps ) => {
33+ // React panel webview
34+ let webview :any
3435
35- export const createCommands = ( { context, machine, storage, git, position} :CreateCommandProps ) => ( {
36- // initialize
37- [ COMMANDS . START ] :( ) => {
38- if ( webview ) {
39- console . log ( 'CodeRoad already loaded' )
40- return
41- }
42- // set local storage workspace
43- setStorage ( context . workspaceState )
36+ return {
37+ // initialize
38+ [ COMMANDS . START ] :( ) => {
39+ let webviewState :'INITIALIZING' | 'RESTARTING'
40+ if ( ! webview ) {
41+ webviewState = 'INITIALIZING'
42+ } else if ( webview . loaded ) {
43+ // already loaded
44+ vscode . window . showInformationMessage ( 'CodeRoad already open' )
45+ return
46+ } else {
47+ webviewState = 'RESTARTING'
48+ }
49+
50+ setStorage ( context . workspaceState )
4451
45- // activate machine
46- webview = new ReactWebView ( context . extensionPath )
47- machine . activate ( )
48- } ,
49- // open React webview
50- [ COMMANDS . OPEN_WEBVIEW ] :( column :number = vscode . ViewColumn . Two ) => {
51- // setup 1x1 horizontal layout
52- vscode . commands . executeCommand ( 'vscode.setEditorLayout' , {
53- orientation :0 ,
54- groups :[ { groups :[ { } ] , size :0.6 } , { groups :[ { } ] , size :0.4 } ] ,
55- } )
56- const callback = ( ) => machine . send ( 'WEBVIEW_INITIALIZED' )
57- webview . createOrShow ( column , callback )
58- } ,
59- // launch a new tutorial
60- // NOTE: may be better to move into action as logic is primarily non-vscode
61- [ COMMANDS . TUTORIAL_LAUNCH ] :async ( tutorial :CR . Tutorial ) => {
62- console . log ( 'launch tutorial' )
52+ // activate machine
53+ webview = new ReactWebView ( context . extensionPath )
54+ if ( webviewState === 'INITIALIZING' ) {
55+ machine . activate ( )
56+ } else if ( webviewState === 'RESTARTING' ) {
57+ setTimeout ( ( ) => {
58+ // timeout hack to make data update on new windows
59+ //@ts -ignore
60+ machine . refresh ( )
61+ } , 1000 )
62+ }
63+ } ,
64+ // open React webview
65+ [ COMMANDS . OPEN_WEBVIEW ] :( column :number = vscode . ViewColumn . Two ) => {
66+ // setup 1x1 horizontal layout
67+ vscode . commands . executeCommand ( 'vscode.setEditorLayout' , {
68+ orientation :0 ,
69+ groups :[ { groups :[ { } ] , size :0.6 } , { groups :[ { } ] , size :0.4 } ] ,
70+ } )
71+ const callback = ( ) => {
72+ machine . send ( 'WEBVIEW_INITIALIZED' )
73+ }
74+ webview . createOrShow ( column , callback )
75+ } ,
76+ // launch a new tutorial
77+ // NOTE: may be better to move into action as logic is primarily non-vscode
78+ [ COMMANDS . TUTORIAL_LAUNCH ] :async ( { tutorial, dispatch} :any ) => {
79+ console . log ( 'launch tutorial' )
6380
64- await isEmptyWorkspace ( )
81+ await isEmptyWorkspace ( )
6582
66- await git . gitInitIfNotExists ( )
83+ await git . gitInitIfNotExists ( )
6784
68- // TODO: use actual tutorial repo
69- await Promise . all ( [ git . gitSetupRemote ( tutorial . meta . repo ) , storage . setTutorial ( tutorial ) , storage . resetProgress ( ) ] )
85+ // TODO: use actual tutorial repo
86+ await Promise . all ( [ git . gitSetupRemote ( tutorial . meta . repo ) , storage . setTutorial ( tutorial ) , storage . resetProgress ( ) ] )
7087
71- // TODO: refactor to allow client to call initialization
72- const pos :CR . Position = await position . getInitial ( tutorial )
88+ // TODO: refactor to allow client to call initialization
89+ const pos :CR . Position = await position . getInitial ( tutorial )
7390
74- // eslint-disable-next-line
75- const { steps} = tutorial . data
76- const { setup} = steps [ pos . stepId ] . actions
77- await git . gitLoadCommits ( setup )
78- machine . send ( 'TUTORIAL_LOADED' )
79- } ,
80- [ COMMANDS . TUTORIAL_SETUP ] :async ( tutorial :CR . Tutorial ) => {
81- console . log ( 'tutorial setup' , tutorial )
82- // setup onSave hook
83- const languageIds = tutorial . meta . languages
84- console . log ( `languageIds:${ languageIds . join ( ', ' ) } ` )
85- vscode . workspace . onDidSaveTextDocument ( ( document :vscode . TextDocument ) => {
86- console . log ( 'save document' , document )
87- if ( languageIds . includes ( document . languageId ) && document . uri . scheme === 'file' ) {
88- // do work
89- machine . send ( 'TEST_RUN' )
90- }
91- } )
92- } ,
93- // open a file
94- [ COMMANDS . OPEN_FILE ] :async ( relativeFilePath :string ) => {
95- console . log ( `OPEN_FILE${ JSON . stringify ( relativeFilePath ) } ` )
96- try {
97- const workspaceRoot = vscode . workspace . rootPath
98- if ( ! workspaceRoot ) {
99- throw new Error ( 'No workspace root path' )
91+ // eslint-disable-next-line
92+ const { steps} = tutorial . data
93+ const { setup} = steps [ pos . stepId ] . actions
94+ await git . gitLoadCommits ( setup , dispatch )
95+ machine . send ( 'TUTORIAL_LOADED' )
96+ } ,
97+ [ COMMANDS . TUTORIAL_SETUP ] :async ( tutorial :CR . Tutorial ) => {
98+ console . log ( 'tutorial setup' , tutorial )
99+ // setup onSave hook
100+ const languageIds = tutorial . meta . languages
101+ console . log ( `languageIds:${ languageIds . join ( ', ' ) } ` )
102+ vscode . workspace . onDidSaveTextDocument ( ( document :vscode . TextDocument ) => {
103+ console . log ( 'save document' , document )
104+ if ( languageIds . includes ( document . languageId ) && document . uri . scheme === 'file' ) {
105+ // do work
106+ machine . send ( 'TEST_RUN' )
107+ }
108+ } )
109+ } ,
110+ // open a file
111+ [ COMMANDS . OPEN_FILE ] :async ( relativeFilePath :string ) => {
112+ console . log ( `OPEN_FILE${ JSON . stringify ( relativeFilePath ) } ` )
113+ try {
114+ const workspaceRoot = vscode . workspace . rootPath
115+ if ( ! workspaceRoot ) {
116+ throw new Error ( 'No workspace root path' )
117+ }
118+ const absoluteFilePath = join ( workspaceRoot , relativeFilePath )
119+ const doc = await vscode . workspace . openTextDocument ( absoluteFilePath )
120+ await vscode . window . showTextDocument ( doc , vscode . ViewColumn . One )
121+ } catch ( error ) {
122+ console . log ( `Failed to open file${ relativeFilePath } ` , error )
100123}
101- const absoluteFilePath = join ( workspaceRoot , relativeFilePath )
102- const doc = await vscode . workspace . openTextDocument ( absoluteFilePath )
103- await vscode . window . showTextDocument ( doc , vscode . ViewColumn . One )
104- } catch ( error ) {
105- console . log ( `Failed to open file${ relativeFilePath } ` , error )
106- }
107- } ,
108- // send messages to webview
109- [ COMMANDS . SEND_STATE ] :( payload :{ data :any ; state :any } ) => {
110- webview . postMessage ( { type :'SET_STATE' , payload} )
111- } ,
112- [ COMMANDS . SEND_DATA ] :( payload :{ data :any } ) => {
113- webview . postMessage ( { type :'SET_DATA' , payload} )
114- } ,
115- [ COMMANDS . RECEIVE_ACTION ] :( action :string | CR . Action ) => {
116- // send received actions from web-app into state machine
117- machine . send ( action )
118- } ,
119- [ COMMANDS . RUN_TEST ] :( ) => {
120- runTest ( {
121- onSuccess :( ) => machine . send ( 'TEST_PASS' ) ,
122- onFail :( ) => machine . send ( 'TEST_FAIL' ) ,
123- } )
124- } ,
125- [ COMMANDS . TEST_PASS ] :( ) => {
126- vscode . window . showInformationMessage ( 'PASS' )
127- } ,
128- [ COMMANDS . TEST_FAIL ] :( ) => {
129- vscode . window . showWarningMessage ( 'FAIL' )
130- } ,
131- [ COMMANDS . SET_LAYOUT ] :( ) => {
132- console . log ( 'setLayout' )
133- vscode . commands . executeCommand ( 'vscode.setEditorLayout' , {
134- orientation :0 ,
135- groups :[ { groups :[ { } ] , size :0.6 } , { groups :[ { } ] , size :0.4 } ] ,
136- } )
137- } ,
138- } )
124+ } ,
125+ // send messages to webview
126+ [ COMMANDS . SEND_STATE ] :( payload :{ data :any ; state :any } ) => {
127+ webview . postMessage ( { type :'SET_STATE' , payload} )
128+ } ,
129+ [ COMMANDS . SEND_DATA ] :( payload :{ data :any } ) => {
130+ webview . postMessage ( { type :'SET_DATA' , payload} )
131+ } ,
132+ [ COMMANDS . RECEIVE_ACTION ] :( action :string | CR . Action ) => {
133+ // send received actions from web-app into state machine
134+ machine . send ( action )
135+ } ,
136+ [ COMMANDS . RUN_TEST ] :( ) => {
137+ runTest ( {
138+ onSuccess :( ) => machine . send ( 'TEST_PASS' ) ,
139+ onFail :( ) => machine . send ( 'TEST_FAIL' ) ,
140+ } )
141+ } ,
142+ [ COMMANDS . TEST_PASS ] :( ) => {
143+ vscode . window . showInformationMessage ( 'PASS' )
144+ } ,
145+ [ COMMANDS . TEST_FAIL ] :( ) => {
146+ vscode . window . showWarningMessage ( 'FAIL' )
147+ } ,
148+ [ COMMANDS . SET_LAYOUT ] :( ) => {
149+ console . log ( 'setLayout' )
150+ vscode . commands . executeCommand ( 'vscode.setEditorLayout' , {
151+ orientation :0 ,
152+ groups :[ { groups :[ { } ] , size :0.6 } , { groups :[ { } ] , size :0.4 } ] ,
153+ } )
154+ } ,
155+ }
156+ }