refactor: abstract and simplify the communication between home page and vscode
* refactor the infertace in index.html * adapt the change of interface in home page and vscode
This commit is contained in:
@@ -52,7 +52,7 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
|
||||
}
|
||||
|
||||
|
||||
async function biCommunication2Vscode(command, data) {
|
||||
async function communicateVSCode(command, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// request to vscode
|
||||
vscode.postMessage({ command: command, data: data })
|
||||
@@ -60,8 +60,9 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
|
||||
function handleResponse(event) {
|
||||
const jsonData = event.data;
|
||||
if (jsonData.command === command) {
|
||||
// console.log("communicateVSCode", jsonData.data)
|
||||
|
||||
// return vscode response
|
||||
console.log("biCommunication2Vscode", jsonData.data)
|
||||
window.removeEventListener('message', handleResponse) // 清理监听器
|
||||
resolve(jsonData.data)
|
||||
}
|
||||
@@ -84,40 +85,15 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
|
||||
// }
|
||||
try {
|
||||
const jsonData = event.data;
|
||||
if (jsonData.action === 'firstOpenRemoteFolder') {
|
||||
console.log('Received message:', jsonData);
|
||||
firstOpenRemoteFolderWithData(jsonData.host, jsonData.username, jsonData.password, jsonData.port, jsonData.path);
|
||||
|
||||
} else if (jsonData.action === 'openRemoteFolder') {
|
||||
console.log('Received message:', jsonData);
|
||||
openRemoteFolderWithData(jsonData.host, jsonData.path);
|
||||
|
||||
} else if (jsonData.action === 'getUserToken') {
|
||||
// request user token from vscode global state
|
||||
const data = await biCommunication2Vscode('getUserToken', {})
|
||||
if (jsonData.target === 'vscode') {
|
||||
const actionFromHome = jsonData.action
|
||||
const dataFromHome = jsonData.data
|
||||
const dataFromVSCodeResponse = await communicateVSCode(actionFromHome, dataFromHome)
|
||||
|
||||
var iframe = document.getElementById('embedded-devstar');
|
||||
if (iframe && iframe.contentWindow) {
|
||||
iframe.contentWindow.postMessage({ action: 'getUserToken', data: data}, '*');
|
||||
}
|
||||
} else if (jsonData.action === 'setUserToken') {
|
||||
// set user token to vscode global state
|
||||
await biCommunication2Vscode('setUserToken', jsonData.data)
|
||||
.then((data) => {
|
||||
var iframe = document.getElementById('embedded-devstar');
|
||||
if (iframe && iframe.contentWindow) {
|
||||
iframe.contentWindow.postMessage({ action: 'setUserToken', data: data}, '*');
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (iframe && iframe.contentWindow) {
|
||||
iframe.contentWindow.postMessage({ action: 'setUserToken', data: {ok: false}}, '*');
|
||||
}
|
||||
});
|
||||
} else if (jsonData.action === 'getDefaultPublicKey') {
|
||||
const data = await biCommunication2Vscode('getDefaultPublicKey', {})
|
||||
var iframe = document.getElementById('embedded-devstar')
|
||||
if (iframe && iframe.contentWindow) {
|
||||
iframe.contentWindow.postMessage({ action: 'getDefaultPublicKey', data: data}, "*")
|
||||
if(iframe && iframe.contentWindow) {
|
||||
iframe.contentWindow.postMessage({action: actionFromHome, data: dataFromVSCodeResponse}, '*')
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
|
Reference in New Issue
Block a user