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