feat: monitor the requests that open remote folder from the front
This commit is contained in:
		
							
								
								
									
										74
									
								
								src/home.ts
									
									
									
									
									
								
							
							
						
						
									
										74
									
								
								src/home.ts
									
									
									
									
									
								
							@@ -1,35 +1,41 @@
 | 
				
			|||||||
import * as vscode from 'vscode';
 | 
					import * as vscode from 'vscode';
 | 
				
			||||||
 | 
					import RemoteContainer from './remote-container';
 | 
				
			||||||
 | 
					import fetch from './fetch'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default class SIHome {
 | 
					export default class SIHome {
 | 
				
			||||||
  context: vscode.ExtensionContext;
 | 
					  private context: vscode.ExtensionContext;
 | 
				
			||||||
 | 
					  private remoteContainer: RemoteContainer;
 | 
				
			||||||
 | 
					  static defaultUrl = 'http://localhost:8080/tmp/index.html';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor(context: vscode.ExtensionContext) {
 | 
					  constructor(context: vscode.ExtensionContext) {
 | 
				
			||||||
    this.context = context;
 | 
					    this.context = context;
 | 
				
			||||||
 | 
					    this.remoteContainer = new RemoteContainer()
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  toggle(url: string) {
 | 
					  async toggle(url: string = SIHome.defaultUrl) {
 | 
				
			||||||
    console.log('url: ', url)
 | 
					    console.log(url);
 | 
				
			||||||
    const panel = vscode.window.createWebviewPanel(
 | 
					    const panel = vscode.window.createWebviewPanel(
 | 
				
			||||||
      'myWebview',
 | 
					      'myWebview',
 | 
				
			||||||
      'My Webview',
 | 
					      'My Webview',
 | 
				
			||||||
      vscode.ViewColumn.One,
 | 
					      vscode.ViewColumn.One,
 | 
				
			||||||
      {}
 | 
					      {
 | 
				
			||||||
 | 
					        enableScripts: true,
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    panel.webview.options = {
 | 
					    fetch(url).then(async (content) => {
 | 
				
			||||||
      enableScripts: true,
 | 
					      panel.webview.html = await this.getWebviewContent(content);
 | 
				
			||||||
    };
 | 
					    })
 | 
				
			||||||
 | 
					 | 
				
			||||||
    panel.webview.html = this.getWebviewContent();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    panel.webview.onDidReceiveMessage(
 | 
					    panel.webview.onDidReceiveMessage(
 | 
				
			||||||
      (message) => {
 | 
					      async (message) => {
 | 
				
			||||||
        switch (message.command) {
 | 
					        switch (message.command) {
 | 
				
			||||||
          case 'alert':
 | 
					          case 'firstOpenRemoteFolder':
 | 
				
			||||||
            vscode.window.showInformationMessage(message.text);
 | 
					            await this.remoteContainer.firstConnect(message.host, message.username, message.password, message.port);
 | 
				
			||||||
 | 
					            this.remoteContainer.openRemoteFolder(message.host, message.path);
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
          case 'openFolder':
 | 
					          case 'openRemoteFolder':
 | 
				
			||||||
            this.openFolderInVSCode(message.path);
 | 
					            this.remoteContainer.openRemoteFolder(message.host, message.path);
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
@@ -41,43 +47,7 @@ export default class SIHome {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  openFolderInVSCode(path: string): void {
 | 
					  async getWebviewContent(content: string): Promise<string> {
 | 
				
			||||||
    try {
 | 
					    return `${content}`
 | 
				
			||||||
      vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(path))
 | 
					 | 
				
			||||||
      console.log('Opened folder: ', path);
 | 
					 | 
				
			||||||
    } catch (error) {
 | 
					 | 
				
			||||||
      console.error('Error opening folder: ', error);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  getWebviewContent() {
 | 
					 | 
				
			||||||
    return `
 | 
					 | 
				
			||||||
        <!DOCTYPE html>
 | 
					 | 
				
			||||||
        <html lang="en">
 | 
					 | 
				
			||||||
        <head>
 | 
					 | 
				
			||||||
            <meta charset="UTF-8">
 | 
					 | 
				
			||||||
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
 | 
					 | 
				
			||||||
            <title>Cat Coding</title>
 | 
					 | 
				
			||||||
        </head>
 | 
					 | 
				
			||||||
        <body>
 | 
					 | 
				
			||||||
            <h1>Cat Coding</h1>
 | 
					 | 
				
			||||||
            <img src="https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif" width="300" />
 | 
					 | 
				
			||||||
            <button onclick="showAlert()">Show Alert</button>
 | 
					 | 
				
			||||||
            <button onclick="openFolder()">Open Project</button>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            <script>
 | 
					 | 
				
			||||||
                const vscode = acquireVsCodeApi();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                function showAlert() {
 | 
					 | 
				
			||||||
                    vscode.postMessage({ command: 'alert', text: 'Hello from the Webview!' });
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                function openFolder() {
 | 
					 | 
				
			||||||
                    vscode.postMessage({ command: 'openFolder', path: 'D:/Programs/hello' });
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            </script>
 | 
					 | 
				
			||||||
        </body>
 | 
					 | 
				
			||||||
        </html>
 | 
					 | 
				
			||||||
    `;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user