vscode自定义插件核心代码

发布时间:2024年01月20日

package.json

{
  "name": "gitlab2td",
  "displayName": "gitlab2td",
  "description": "",
  "version": "0.0.1",
  "engines": {
    "vscode": "^1.54.0"
  },
  "categories": [
    "Other"
  ],
  "activationEvents": [],
  "main": "./out/extension.js",
  "contributes": {
    "viewsContainers": {
      "activitybar": [{
        "id": "gitlab2td-custom-view-container",
        "title": "gitlab2td",
        "icon": "$(github-inverted)",
        "activationEvents": [
          "onView:gitlab2td-custom-view-container"
        ]
      }]
    },
    "views": {
      "gitlab2td-custom-view-container": [{
        "id": "gitlab2td-custom-view-container",
        "type": "webview",
        "name": "gitlab2td"
      }]
    }
  },
  "scripts": {
    "vscode:prepublish": "yarn run compile",
    "compile": "tsc -p ./",
    "watch": "tsc -watch -p ./",
    "pretest": "yarn run compile && yarn run lint",
    "lint": "eslint src --ext ts",
    "test": "node ./out/test/runTest.js"
  },
  "devDependencies": {
    "@types/vscode": "^1.54.0",
    "@types/mocha": "^10.0.3",
    "@types/node": "18.x",
    "@typescript-eslint/eslint-plugin": "^6.9.0",
    "@typescript-eslint/parser": "^6.9.0",
    "eslint": "^8.52.0",
    "glob": "^10.3.10",
    "mocha": "^10.2.0",
    "typescript": "^5.2.2",
    "@vscode/test-electron": "^2.3.6"
  }
}

extension.ts

import * as vscode from 'vscode';
import { gitlab2tdCustomView } from './treeViewProvider';

export function activate(context: vscode.ExtensionContext) {
	const sidebarPanel = new gitlab2tdCustomView(context.extensionUri);

	context.subscriptions.push(
		vscode.window.registerWebviewViewProvider("gitlab2td-custom-view-container", sidebarPanel),
	);

}

export function deactivate() { }

treeViewProvider.ts

import * as vscode from "vscode";

export class gitlab2tdCustomView implements vscode.WebviewViewProvider {
    constructor(private readonly _extensionUri: vscode.Uri) { }

    resolveWebviewView(
        webviewView: vscode.WebviewView,
    ): void | Thenable<void> {

        webviewView.webview.options = {
            enableScripts: true,
            localResourceRoots: [this._extensionUri],
        };

        webviewView.webview.html = this._getHtmlForWebview(webviewView.webview);
    }

    private _getHtmlForWebview(webview: vscode.Webview) {
        return `<!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>Your Webview</title>
            </head>
            <body>
                <h1>Hello abu</h1>
                <input type="text" id="input">
            </body>
            </html>`;
    }
}

文章来源:https://blog.csdn.net/weixin_44038355/article/details/135711583
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。