Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit39e3df4

Browse files
committed
fix multiple windows out of sync issue
1 parente02c0e2 commit39e3df4

File tree

7 files changed

+142
-118
lines changed

7 files changed

+142
-118
lines changed

‎src/editor/ReactWebView.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class ReactWebView {
7575
// unfortunately there is no easy way of doing this
7676
constwebPanelListener=setInterval(()=>{
7777
if(this.loaded){
78+
// callback tells editor the webview has loaded
7879
setTimeout(callback)
7980
clearInterval(webPanelListener)
8081
}

‎src/editor/commands/index.ts

Lines changed: 112 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -29,110 +29,123 @@ interface CreateCommandProps {
2929
position:any
3030
}
3131

32-
// React panel webview
33-
letwebview:any
32+
exportconstcreateCommands=({ context, machine, storage, git, position}:CreateCommandProps)=>{
33+
// React panel webview
34+
letwebview:any
3435

35-
exportconstcreateCommands=({ 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=newReactWebView(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-
constcallback=()=>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+
constisInitialized=!!webview
47+
// activate machine
48+
webview=newReactWebView(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+
constcallback=()=>{
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-
awaitisEmptyWorkspace()
76+
awaitisEmptyWorkspace()
6577

66-
awaitgit.gitInitIfNotExists()
78+
awaitgit.gitInitIfNotExists()
6779

68-
// TODO: use actual tutorial repo
69-
awaitPromise.all([git.gitSetupRemote(tutorial.meta.repo),storage.setTutorial(tutorial),storage.resetProgress()])
80+
// TODO: use actual tutorial repo
81+
awaitPromise.all([git.gitSetupRemote(tutorial.meta.repo),storage.setTutorial(tutorial),storage.resetProgress()])
7082

71-
// TODO: refactor to allow client to call initialization
72-
constpos:CR.Position=awaitposition.getInitial(tutorial)
83+
// TODO: refactor to allow client to call initialization
84+
constpos:CR.Position=awaitposition.getInitial(tutorial)
7385

74-
// eslint-disable-next-line
75-
const{ steps}=tutorial.data
76-
const{ setup}=steps[pos.stepId].actions
77-
awaitgit.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-
constlanguageIds=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+
awaitgit.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+
constlanguageIds=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+
constworkspaceRoot=vscode.workspace.rootPath
110+
if(!workspaceRoot){
111+
thrownewError('No workspace root path')
112+
}
113+
constabsoluteFilePath=join(workspaceRoot,relativeFilePath)
114+
constdoc=awaitvscode.workspace.openTextDocument(absoluteFilePath)
115+
awaitvscode.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-
constworkspaceRoot=vscode.workspace.rootPath
98-
if(!workspaceRoot){
99-
thrownewError('No workspace root path')
100-
}
101-
constabsoluteFilePath=join(workspaceRoot,relativeFilePath)
102-
constdoc=awaitvscode.workspace.openTextDocument(absoluteFilePath)
103-
awaitvscode.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+
}

‎src/editor/index.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ class Editor {
2727
}
2828
setWorkspaceRoot(rootPath)
2929
}
30-
31-
privateactivateCommands=():void=>{
32-
constcommands=createCommands({
33-
context:this.context,
34-
machine:this.machine,
35-
storage,
36-
git,
37-
position,
38-
})
39-
for(constcmdincommands){
40-
constcommand:vscode.Disposable=vscode.commands.registerCommand(cmd,commands[cmd])
41-
this.context.subscriptions.push(command)
42-
}
43-
}
4430
publicactivate=(context:vscode.ExtensionContext):void=>{
4531
console.log('ACTIVATE!')
4632
this.context=context
@@ -56,13 +42,28 @@ class Editor {
5642
disposable.dispose()
5743
}
5844
// shut down state machine
45+
console.log('deactivate machine')
5946
this.machine.deactivate()
6047
}
6148

6249
// execute vscode command
6350
publicdispatch=(type:string,payload?:any)=>{
6451
vscode.commands.executeCommand(type,payload)
6552
}
53+
54+
privateactivateCommands=():void=>{
55+
constcommands=createCommands({
56+
context:this.context,
57+
machine:this.machine,
58+
storage,
59+
git,
60+
position,
61+
})
62+
for(constcmdincommands){
63+
constcommand:vscode.Disposable=vscode.commands.registerCommand(cmd,commands[cmd])
64+
this.context.subscriptions.push(command)
65+
}
66+
}
6667
}
6768

6869
exportdefaultEditor

‎src/editor/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ export function get<T>(key: string): T | undefined {
1212
returnstorage.get(key)
1313
}
1414

15-
exportfunctionupdate<T>(key:string,value:string|Object):Thenable<void>{
15+
exportfunctionupdate<T>(key:string,value:string|object):Thenable<void>{
1616
returnstorage.update(key,value)
1717
}

‎src/services/git/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ export async function gitCheckRemoteExists(): Promise<boolean> {
128128
// TODO: improve the specificity of this regex
129129
return!!stdout.match(gitOrigin)
130130
}catch(error){
131-
console.warn(error)
132131
returnfalse
133132
}
134133
}

‎src/state/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface Props {
1010
}
1111

1212
classStateMachine{
13+
privatedispatch:CR.EditorDispatch
1314
privatemachineOptions={
1415
logger:console.log,
1516
devTools:true,
@@ -18,6 +19,7 @@ class StateMachine {
1819
}
1920
privateservice:Interpreter<CR.MachineContext,CR.MachineStateSchema,CR.MachineEvent>
2021
constructor({ dispatch}:Props){
22+
this.dispatch=dispatch
2123
constmachine=createMachine(dispatch)
2224
this.service=interpret(machine,this.machineOptions)
2325
// logging
@@ -32,14 +34,19 @@ class StateMachine {
3234
}
3335
})
3436
}
35-
activate(){
37+
publicactivate(){
3638
// initialize
3739
this.service.start()
3840
}
39-
deactivate(){
41+
publicdeactivate(){
4042
this.service.stop()
4143
}
42-
send(action:string|CR.Action){
44+
publicrefresh(){
45+
console.log('service refresh')
46+
const{ value, context}=this.service.state
47+
this.dispatch('coderoad.send_state',{state:value,data:context})
48+
}
49+
publicsend(action:string|CR.Action){
4350
this.service.send(action)
4451
}
4552
}

‎src/state/machine.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export const machine = (dispatch: CR.EditorDispatch) =>
5656
id:'tutorial',
5757
initial:'Initialize',
5858
onEntry:['tutorialSetup'],
59+
on:{
60+
WEBVIEW_INITIALIZED:'#tutorial-load-currents'
61+
},
5962
states:{
6063
Initialize:{
6164
onEntry:['initializeNewTutorial'],

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp