From 7c94a10d78f6e4231ddac998d23baffc2e379e55 Mon Sep 17 00:00:00 2001 From: mahaoyuan Date: Fri, 5 Aug 2022 11:18:31 +0800 Subject: [PATCH 1/5] fix: adjust the ui Signed-off-by: mahaoyuan --- napi_vs_plugin/src/extension.js | 77 +++++++++++++++++++++---- napi_vs_plugin/src/images/file.png | Bin 0 -> 311 bytes napi_vs_plugin/src/images/path.png | Bin 0 -> 608 bytes napi_vs_plugin/src/vs_plugin_view.html | 71 +++++++++++++++++------ 4 files changed, 119 insertions(+), 29 deletions(-) create mode 100644 napi_vs_plugin/src/images/file.png create mode 100644 napi_vs_plugin/src/images/path.png diff --git a/napi_vs_plugin/src/extension.js b/napi_vs_plugin/src/extension.js index 55db25b6..fde05327 100644 --- a/napi_vs_plugin/src/extension.js +++ b/napi_vs_plugin/src/extension.js @@ -20,6 +20,7 @@ const fs = require('fs'); const re = require("./gen/tools/VsPluginRe"); const { VsPluginLog } = require("./gen/tools/VsPluginLog"); const { detectPlatform, readFile } = require('./gen/tools/VsPluginTool'); +const path = require('path'); var exeFilePath = null; // this method is called when your extension is activated // your extension is activated the very first time the command is executed @@ -59,7 +60,7 @@ function executor(name, genDir, mode) { function genCommand(name, genDir, mode) { var genFileMode = mode == 0 ? " -f " : " -d "; - if (genDir == "") { + if (genDir == ""){ return exeFilePath + genFileMode + name; } return exeFilePath + genFileMode + name + " -o " + genDir; @@ -79,30 +80,73 @@ function register(context, command) { const panel = vscode.window.createWebviewPanel( 'generate', // Identifies the type of WebView 'Generate Napi Frame', // Title of the panel displayed to the user - vscode.ViewColumn.One, // Display the WebView panel in the form of new columns in the editor + vscode.ViewColumn.Two, // Display the WebView panel in the form of new columns in the editor { enableScripts: true, // Enable or disable JS, default is Enable retainContextWhenHidden: true, // Keep the WebView state when it is hidden to avoid being reset } ); - panel.webview.html = getWebviewContent(); + panel.webview.html = getWebviewContent(context); panel.webview.onDidReceiveMessage(message => { - if (message == "cancel") { + let msg = message.msg; + if (msg == "cancel") { panel.dispose(); - } else { + } else if(msg == "param") { let mode = message.mode; let name = message.fileNames; let genDir = message.genDir; checkMode(name, genDir, mode); + } else { + selectPath(panel, message); } }, undefined, context.subscriptions); let fn = re.getFileInPath(uri.fsPath); let tt = re.match("@ohos.[a-zA-Z_0-9]+.d.ts", fn); - panel.webview.postMessage(tt ? uri.fsPath : ""); + var result = { + msg: "selectInterPath", + path: tt ? uri.fsPath : "" + } + panel.webview.postMessage(result); }); return disposable; } +/** +* 选择本地目录/文件夹 +*/ + function selectPath(panel, message) { + let mode = 1; + if (message.mode != undefined) { + mode = message.mode; + } + const options = { + canSelectMany: mode == 0 ? true : false,//是否可以选择多个 + openLabel: mode == 0 ? '选择文件' : '选择文件夹',//打开选择的右下角按钮label + canSelectFiles: mode == 0 ? true : false,//是否选择文件 + canSelectFolders: mode == 0 ? false : true,//是否选择文件夹 + defaultUri:vscode.Uri.file(''),//默认打开本地路径 + filters: { + 'Text files': ['d.ts'] + } + }; + + return vscode.window.showOpenDialog(options).then(fileUri => { + if (fileUri && fileUri[0]) { + console.log('Selected file: ' + fileUri[0].fsPath); + let filePath = ""; + for (let index = 0; index < fileUri.length; index++) { + filePath += fileUri[index].fsPath.concat(","); + } + var result = { + msg: message.msg, + path: filePath.length > 0 ? filePath.substring(0, filePath.length - 1) : filePath + } + panel.webview.postMessage(result); + return fileUri[0].fsPath + } + }); +} + function checkMode(name, genDir, mode) { name = re.replaceAll(name, " ", ""); if ("" == name) { @@ -110,13 +154,13 @@ function checkMode(name, genDir, mode) { return; } if (mode == 0) { - if (name.indexOf(".") < 0) { + if (name.indexOf(".") < 0 || !fs.lstatSync(name).isFile()) { vscode.window.showErrorMessage("Please enter the correct file path!"); return; } } else { - if (name.indexOf(".") > 0) { - vscode.window.showErrorMessage("Please enter the correct folder path!"); + if (name.indexOf(".") > 0 || !fs.lstatSync(name).isDirectory()) { + vscode.window.showErrorMessage("Please enter the correct folder folder!"); return; } } @@ -130,11 +174,24 @@ function checkMode(name, genDir, mode) { // this method is called when your extension is deactivated function deactivate() { } -function getWebviewContent() { +function getWebviewContent(context) { let data = readFile(__dirname + '/vs_plugin_view.html'); + data = getWebViewContent(context, '/vs_plugin_view.html'); return data.toString(); } +function getWebViewContent(context, templatePath) { + const resourcePath = path.join(context.extensionPath, templatePath); + const dirPath = path.dirname(resourcePath); + let html = fs.readFileSync(resourcePath, 'utf-8'); + html = html.replace(/( { + if($2.indexOf("https://")<0)return $1 + vscode.Uri.file(path.resolve(dirPath, $2)) + .with({ scheme: 'vscode-resource' }).toString() + '"'; + else return $1 + $2+'"'; + }); + return html; +} + module.exports = { activate, deactivate diff --git a/napi_vs_plugin/src/images/file.png b/napi_vs_plugin/src/images/file.png new file mode 100644 index 0000000000000000000000000000000000000000..f5c365a02e99414acb29c643fc2d64d150cff9f0 GIT binary patch literal 311 zcmV-70m%M|P)Px#@kvBMR9Hvtmr)ABFc3uFqQ?;Y67(=Ccni_v=tTt2q9BNR4_!$~rCQr&n}Q(8 zZ+CXyPSVf{KVI@K>M5`WhHXGv0eIsXuxe|Z!&U+qrn&n2U?(E?P@SsQZ$Ut#n}7dJ#+{yl0LaRc z#sos{fdr5eNC~6_QUd>-K+JR&Z5Q7I7Qa$$2_ULlggE^hJ^@2>!y!FdR_nk literal 0 HcmV?d00001 diff --git a/napi_vs_plugin/src/images/path.png b/napi_vs_plugin/src/images/path.png new file mode 100644 index 0000000000000000000000000000000000000000..56f408a584f243956fab1916d9152f34364cdf78 GIT binary patch literal 608 zcmV-m0-ybfP)Px%8c9S!R9HvtmphD2Q51&1Kk=%(;t>^%3bkjn6iB8Kl|)BWBGI5iBN-~9lc<J56Lj|zC5_nvcf|A>muCcug*q%%nX8gAa^Mh@#08XbS zv=vxdD%`Se{xZxA6V-4B*s?&;su4vA-<-D*WdOHaWa!wR@* zzW69QWT>C!h~yUoMG%R;q2Jg>xW5J uhP!}h&974#&WiUAP?E}CBd`ci5`o_-qRb&!3c5Z30000