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 RemoteContainer from './remote-container';
 | 
			
		||||
import fetch from './fetch'
 | 
			
		||||
 | 
			
		||||
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) {
 | 
			
		||||
    this.context = context;
 | 
			
		||||
    this.remoteContainer = new RemoteContainer()
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  toggle(url: string) {
 | 
			
		||||
    console.log('url: ', url)
 | 
			
		||||
  async toggle(url: string = SIHome.defaultUrl) {
 | 
			
		||||
    console.log(url);
 | 
			
		||||
    const panel = vscode.window.createWebviewPanel(
 | 
			
		||||
      'myWebview',
 | 
			
		||||
      'My Webview',
 | 
			
		||||
      vscode.ViewColumn.One,
 | 
			
		||||
      {}
 | 
			
		||||
      {
 | 
			
		||||
        enableScripts: true,
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    panel.webview.options = {
 | 
			
		||||
      enableScripts: true,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    panel.webview.html = this.getWebviewContent();
 | 
			
		||||
    fetch(url).then(async (content) => {
 | 
			
		||||
      panel.webview.html = await this.getWebviewContent(content);
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    panel.webview.onDidReceiveMessage(
 | 
			
		||||
      (message) => {
 | 
			
		||||
      async (message) => {
 | 
			
		||||
        switch (message.command) {
 | 
			
		||||
          case 'alert':
 | 
			
		||||
            vscode.window.showInformationMessage(message.text);
 | 
			
		||||
          case 'firstOpenRemoteFolder':
 | 
			
		||||
            await this.remoteContainer.firstConnect(message.host, message.username, message.password, message.port);
 | 
			
		||||
            this.remoteContainer.openRemoteFolder(message.host, message.path);
 | 
			
		||||
            return;
 | 
			
		||||
          case 'openFolder':
 | 
			
		||||
            this.openFolderInVSCode(message.path);
 | 
			
		||||
          case 'openRemoteFolder':
 | 
			
		||||
            this.remoteContainer.openRemoteFolder(message.host, message.path);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
@@ -41,43 +47,7 @@ export default class SIHome {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  openFolderInVSCode(path: string): void {
 | 
			
		||||
    try {
 | 
			
		||||
      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>
 | 
			
		||||
    `;
 | 
			
		||||
  async getWebviewContent(content: string): Promise<string> {
 | 
			
		||||
    return `${content}`
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user