1/* 2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15// The module 'vscode' contains the VS Code extensibility API 16// Import the module and reference it with the alias vscode in your code below 17const vscode = require('vscode'); 18const fs = require('fs'); 19const path = require('path'); 20 21// this method is called when your extension is activated 22// your extension is activated the very first time the command is executed 23 24/** 25 * @param {vscode.ExtensionContext} context 26 */ 27function activate(context) { 28 console.log('Congratulations, your extension "hcseditor" is now active!'); 29 let disposable = vscode.commands.registerCommand('hcs_editor', function (uri) { 30 vscode.window.showInformationMessage('Hello World from HcsEditor!'); 31 if (vscode.ccPanel != undefined){ 32 vscode.ccPanel.dispose(); 33 }else { 34 vscode.workspace.onDidSaveTextDocument((e)=>{ 35 let tt2 = fs.readFileSync(e.uri.fsPath) 36 let tt = new Int8Array(tt2); 37 send("freshfiledata", { 38 fn: e.uri.fsPath, data: tt 39 }) 40 send("parse", e.uri.fsPath)}) 41 } 42 vscode.ccPanel = vscode.window.createWebviewPanel('ccnto', "编辑"+ 43 path.basename(uri.fsPath), vscode.ViewColumn.Two, { 44 enableScripts: true, 45 }); 46 vscode.ccPanel.webview.html = getWebviewContent(context.extensionUri); 47 vscode.ccPanel.webview.onDidReceiveMessage(msg => { 48 switch (msg.type) { 49 case "inited": 50 send("parse", uri.fsPath) 51 break; 52 case 'getfiledata': 53 let tt2 = fs.readFileSync(msg.data); 54 let tt = new Int8Array(tt2); 55 send("filedata", { 56 fn: msg.data, data: tt 57 }) 58 break; 59 case 'writefile': 60 fs.writeFileSync(msg.data.fn,msg.data.data); 61 break; 62 case 'selectchange': 63 vscode.workspace.openTextDocument(msg.data).then((d)=>{ 64 vscode.window.showTextDocument(d,vscode.ViewColumn.One) 65 }) 66 break; 67 case 'open_page': 68 break; 69 case 'error': 70 vscode.window.showErrorMessage(msg.data); 71 break 72 default: 73 vscode.window.showInformationMessage(msg.type); 74 vscode.window.showInformationMessage(msg.data); 75 } 76 }, undefined, context.subscriptions); 77 }); 78 context.subscriptions.push(disposable); 79} 80 81function send(type, data) { 82 vscode.ccPanel.webview.postMessage({ 83 type: type, 84 data: data 85 }); 86} 87 88function getWebviewContent(extpath) { 89 let uri = vscode.Uri.joinPath(extpath, "editor.html") 90 91 let ret = fs.readFileSync(uri.fsPath, { encoding: 'utf8' }); 92 93 return ret; 94} 95 96// this method is called when your extension is deactivated 97function deactivate() { } 98 99module.exports = { 100 activate, 101 deactivate 102} 103