1/* 2 * Copyright (c) 2022-2023 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 */ 15const vscode = require('vscode'); 16const fs = require('fs'); 17const path = require('path'); 18const { dirname } = require('path'); 19const { dir } = require('console'); 20const { getEventListeners } = require('stream'); 21 22function cutImgDict(context, msg) { 23 let imgDir = context.extensionPath + '/images'; 24 let cutImgDict_ = msg.data.data; 25 let whiteCutImg = fs.readFileSync(path.join(imgDir, cutImgDict_['whiteCut'])); 26 send('whiteCutImg', whiteCutImg); 27 let circleImg = fs.readFileSync(path.join(imgDir, cutImgDict_['circleCut'])); 28 send('circleImg', circleImg); 29 let cicleOpenImg = fs.readFileSync(path.join(imgDir, cutImgDict_['circleOpenCut'])); 30 send('cicleOpenImg', cicleOpenImg); 31 let rectangleFocusImg = fs.readFileSync(path.join(imgDir, cutImgDict_['rectangleFocusCut'])); 32 send('rectangleFocusImg', rectangleFocusImg); 33 let nodeIconImg = fs.readFileSync(path.join(imgDir, cutImgDict_['nodeIconCut'])); 34 send('nodeIconImg', nodeIconImg); 35 let attrIconImg = fs.readFileSync(path.join(imgDir, cutImgDict_['attrIconCut'])); 36 send('attrIconImg', attrIconImg); 37 let rootIconImg = fs.readFileSync(path.join(imgDir, cutImgDict_['rootIconCut'])); 38 send('rootIconImg', rootIconImg); 39 let rootIconFocusImg = fs.readFileSync(path.join(imgDir, cutImgDict_['rootIconFocusCut'])); 40 send('rootIconFocusImg', rootIconFocusImg); 41 let backgroundImg = fs.readFileSync(path.join(imgDir, cutImgDict_['backgroundCut'])); 42 send('backgroundImg', backgroundImg); 43 let popItemFocusImg = fs.readFileSync(path.join(imgDir, cutImgDict_['popItemFocusCut'])); 44 send('popItemFocusImg', popItemFocusImg); 45 46 let searchBgImg = fs.readFileSync(path.join(imgDir, cutImgDict_['searchBgCut'])); 47 send('searchBgImg', searchBgImg); 48 let upCutImg = fs.readFileSync(path.join(imgDir, cutImgDict_['upCut'])); 49 send('upCutImg', upCutImg); 50 let downCutImg = fs.readFileSync(path.join(imgDir, cutImgDict_['downCut'])); 51 send('downCut', downCutImg); 52 let closeCutImg = fs.readFileSync(path.join(imgDir, cutImgDict_['closeCut'])); 53 send('closeCutImg', closeCutImg); 54 let searchCutImg = fs.readFileSync(path.join(imgDir, cutImgDict_['searchCut'])); 55 send('searchCutImg', searchCutImg); 56 let searchNoodRectImg = fs.readFileSync(path.join(imgDir, cutImgDict_['searchNoodRectImg'])); 57 send('searchNoodRectImg', searchNoodRectImg); 58 let searchAttrRectImg = fs.readFileSync(path.join(imgDir, cutImgDict_['searchAttrRectImg'])); 59 send('searchAttrRectImg', searchAttrRectImg); 60} 61 62/** 63 * @param {vscode.ExtensionContext} context 64 */ 65function activate(context) { 66 console.log('Congratulations, your extension "HCS View" is now active!'); 67 let disposable = vscode.commands.registerCommand('hcs_editor', function (uri) { 68 vscode.window.showInformationMessage('Hello World from HCS View!'); 69 const color = new vscode.ThemeColor('badge.colorTheme'); 70 if (vscode.ccPanel != undefined) { 71 vscode.ccPanel.dispose(); 72 } else { 73 sendMsgFunc(); 74 } 75 vscode.ccPanel = vscode.window.createWebviewPanel('ccnto','编辑' + 76 path.basename(uri.fsPath), vscode.ViewColumn.Two, { enableScripts: true }); 77 vscode.ccPanel.webview.html = getWebviewContent(context, context.extensionUri); 78 vscode.ccPanel.webview.onDidReceiveMessage((msg) => { 79 switch (msg.type) { 80 case 'inited': 81 send('parse', uri.fsPath); 82 break; 83 case 'getfiledata': 84 getFileData(msg); 85 break; 86 case 'writefile': 87 fs.writeFileSync(msg.data.fn, msg.data.data); 88 break; 89 case 'selectchange': 90 vscode.workspace.openTextDocument(msg.data).then((d) => { 91 vscode.window.showTextDocument(d, vscode.ViewColumn.One);}); 92 break; 93 case 'open_page': 94 break; 95 case 'error': 96 vscode.window.showErrorMessage(msg.data); 97 break; 98 case 'cutImgDict': 99 cutImgDict(context, msg); 100 break; 101 case 'reloadMenuBg': 102 reloadMenuBgFunc(context, msg); 103 break; 104 default: 105 vscode.window.showInformationMessage(msg.type); 106 vscode.window.showInformationMessage(msg.data); 107 } 108 }, undefined, context.subscriptions); 109 vscode.window.onDidChangeActiveColorTheme((colorTheme) => { 110 send('colorThemeChanged', null); 111 }); 112 }); 113 context.subscriptions.push(disposable); 114} 115 116function sendMsgFunc() { 117 vscode.workspace.onDidSaveTextDocument((e) => { 118 let tt2 = fs.readFileSync(e.uri.fsPath); 119 let tt = new Int8Array(tt2); 120 send('freshfiledata', { 121 fn: e.uri.fsPath, 122 data: tt, 123 }); 124 send('parse', e.uri.fsPath); 125 }); 126} 127 128function reloadMenuBgFunc(context, msg) { 129 let picDir = context.extensionPath + '/images'; 130 let menuBgPic = msg.data.data; 131 let menuBgImg = fs.readFileSync(path.join(picDir, menuBgPic)); 132 send('backgroundImg', menuBgImg); 133} 134 135function getFileData(msg) { 136 let tt2 = fs.readFileSync(msg.data); 137 let tt = new Int8Array(tt2); 138 send('filedata', { 139 fn: msg.data, data: tt, 140 }); 141} 142 143function send(type, data) { 144 vscode.ccPanel.webview.postMessage({ 145 type: type, 146 data: data, 147 }); 148} 149 150function getWebviewContent(context, extpath) { 151 let uri = vscode.Uri.joinPath(extpath, 'editor.html'); 152 let ret = fs.readFileSync(uri.fsPath, { encoding: 'utf8' }); 153 ret = getWebViewContent(context, '/editor.html'); 154 return ret; 155} 156 157function getWebViewContent(context, templatePath) { 158 const resourcePath = path.join(context.extensionPath, templatePath); 159 const dirPath = path.dirname(resourcePath); 160 let html = fs.readFileSync(resourcePath, 'utf-8'); 161 html = html.replace( 162 /(<link.+?href="|<script.+?src="|<iframe.+?src="|<img.+?src=")(.+?)"/g, 163 (m, $1, $2) => { 164 if ($2.indexOf('https://') < 0) 165 return ( 166 $1 + 167 vscode.Uri.file(path.resolve(dirPath, $2)) 168 .with({ scheme: 'vscode-resource' }) 169 .toString() + 170 '"' 171 ); 172 else return $1 + $2 + '"'; 173 } 174 ); 175 return html; 176} 177 178function deactivate() {} 179 180module.exports = { 181 activate, 182 deactivate, 183}; 184