From 2b6339ecb819a75c0ad52c4e28e7b3d728c28d95 Mon Sep 17 00:00:00 2001 From: cweijan Date: Fri, 29 Mar 2024 22:41:44 +0800 Subject: [PATCH] Trim code. --- .eslintrc.js | 1 + src/provider/officeViewerProvider.ts | 50 ++++++---------------------- src/react/util/vscode.ts | 2 +- src/react/view/excel/excel_reader.ts | 6 ++-- 4 files changed, 15 insertions(+), 44 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 1caa99b..b78c5c7 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,5 +1,6 @@ const typeScriptExtensions = ['.ts']; module.exports = { + ignorePatterns: ['**/*.d.ts'], parser: '@typescript-eslint/parser', extends: ['plugin:@typescript-eslint/recommended'], plugins: ['unused-imports','eslint-plugin-import'], diff --git a/src/provider/officeViewerProvider.ts b/src/provider/officeViewerProvider.ts index e17938c..8f87d7e 100644 --- a/src/provider/officeViewerProvider.ts +++ b/src/provider/officeViewerProvider.ts @@ -3,7 +3,6 @@ import { spawn } from 'child_process'; import { existsSync, mkdirSync, readFileSync } from 'fs'; import { tmpdir } from 'os'; import { extname, parse } from 'path'; -import { TextEncoder } from 'util'; import * as vscode from 'vscode'; import { Output } from '../common/Output'; import { Handler } from '../common/handler'; @@ -39,10 +38,6 @@ export class OfficeViewerProvider implements vscode.CustomReadonlyEditorProvider localResourceRoots: [vscode.Uri.file(this.extensionPath), folderPath] } - const ext = extname(uri.fsPath).toLowerCase() - let htmlPath: string | null = null; - - const send = () => { handler.emit("open", { ext: extname(uri.fsPath), @@ -50,21 +45,21 @@ export class OfficeViewerProvider implements vscode.CustomReadonlyEditorProvider }) } - let route: string; - const handler = Handler.bind(webviewPanel, uri); - handler + const handler = Handler.bind(webviewPanel, uri) .on("editInVSCode", (full: boolean) => { const side = full ? vscode.ViewColumn.Active : vscode.ViewColumn.Beside; vscode.commands.executeCommand('vscode.openWith', uri, "default", side); }) .on('developerTool', () => vscode.commands.executeCommand('workbench.action.toggleDevTools')) .on("init", send) + .on("fileChange", send) + let route: string; + const ext = extname(uri.fsPath).toLowerCase() if (isImage(ext)) { handleImage(handler, uri, webview) - return ReactApp.view(webview, { route: 'image' }) + route = 'image' } - switch (ext) { case ".xlsx": case ".xlsm": @@ -72,12 +67,10 @@ export class OfficeViewerProvider implements vscode.CustomReadonlyEditorProvider case ".csv": case ".ods": route = 'excel'; - handler.on("fileChange", send) break; case ".docx": case ".dotx": route = 'word' - handler.on("fileChange", send) break; case ".jar": case ".zip": @@ -87,14 +80,15 @@ export class OfficeViewerProvider implements vscode.CustomReadonlyEditorProvider handleZip(uri, handler); break; case ".pdf": - this.handlePdf(webview); - handler.on("fileChange", send) + webview.html = readFileSync(this.extensionPath + "/resource/pdf/viewer.html", 'utf8') + .replace("{{baseUrl}}", this.getBaseUrl(webview, 'pdf')) break; case ".ttf": case ".woff": case ".woff2": case ".otf": - this.handleFont(handler) + webview.html = readFileSync(`${this.extensionPath}/resource/font/index.html`, 'utf8') + .replace('{{baseUrl}}', this.getBaseUrl(webview, 'font')) break; case ".class": this.handleClass(uri, webviewPanel); @@ -107,26 +101,10 @@ export class OfficeViewerProvider implements vscode.CustomReadonlyEditorProvider }) break; default: + if (route) break; vscode.commands.executeCommand('vscode.openWith', uri, "default"); } if (route) return ReactApp.view(webview, { route }) - - if (htmlPath != null) { - webview.html = Util.buildPath(readFileSync(this.extensionPath + "/resource/" + htmlPath, 'utf8'), webview, this.extensionPath + "/resource") - } - - } - - private handlePdf(webview: vscode.Webview) { - const baseUrl = this.getBaseUrl(webview, 'pdf') - webview.html = readFileSync(this.extensionPath + "/resource/pdf/viewer.html", 'utf8').replace("{{baseUrl}}", baseUrl) - } - - private handleFont(handler: Handler) { - const webview = handler.panel.webview; - const baseUrl = this.getBaseUrl(webview, 'font') - webview.html = readFileSync(`${this.extensionPath}/resource/font/index.html`, 'utf8') - .replace('{{baseUrl}}', baseUrl) } private getBaseUrl(webview: vscode.Webview, path: string) { @@ -136,16 +114,10 @@ export class OfficeViewerProvider implements vscode.CustomReadonlyEditorProvider } private async handleClass(uri: vscode.Uri, panel: vscode.WebviewPanel) { - if (uri.scheme != "file") { - vscode.commands.executeCommand('vscode.openWith', uri, "default"); - return; - } - const tempPath = `${tmpdir()}/office_temp_java` if (!existsSync(tempPath)) { mkdirSync(tempPath) } - const java = spawn("java", ['-cp', '../resource/java-decompiler.jar', 'org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler', uri.fsPath, tempPath], { cwd: __dirname }) java.stdout.on('data', (data) => { console.log(data.toString("utf8")) @@ -157,11 +129,9 @@ export class OfficeViewerProvider implements vscode.CustomReadonlyEditorProvider vscode.window.showTextDocument(vscode.Uri.file(fileName).with({ scheme: "decompile_java", query: new Date().getTime().toString() })); }, 10); }); - java.stderr.on('data', (data) => { Output.log(data.toString("utf8")) }); - } } \ No newline at end of file diff --git a/src/react/util/vscode.ts b/src/react/util/vscode.ts index f646956..47e3fa5 100644 --- a/src/react/util/vscode.ts +++ b/src/react/util/vscode.ts @@ -1,7 +1,7 @@ const vscode = window['acquireVsCodeApi']?.(); const postMessage = (message) => { if (vscode) { vscode.postMessage(message) } } -let events = {} +const events = {} function receive({ data }) { if (!data) return; diff --git a/src/react/view/excel/excel_reader.ts b/src/react/view/excel/excel_reader.ts index c677acd..363a598 100644 --- a/src/react/view/excel/excel_reader.ts +++ b/src/react/view/excel/excel_reader.ts @@ -13,9 +13,9 @@ export interface ExcelData { } export function loadSheets(buffer: ArrayBuffer, ext: string): S2DataConfig[] { - let start = new Date().getTime(); + const start = new Date().getTime(); const ab = new Uint8Array(buffer).buffer - var { sheets, maxCols } = ext.toLowerCase() == ".csv" ? readCSV(ab) : readXLSX(ab); + const { sheets, maxCols } = ext.toLowerCase() == ".csv" ? readCSV(ab) : readXLSX(ab); if (import.meta.env.DEV) { console.log('Load time:', new Date().getTime() - start, 'ms'); } @@ -37,7 +37,7 @@ export function loadSheets(buffer: ArrayBuffer, ext: string): S2DataConfig[] { export function readCSV(buffer: ArrayBuffer): ExcelData { const csvStr = new TextDecoder("utf-8").decode(buffer); let maxCols = 26; - let schema = inferSchema(csvStr, { header: () => [] }); + const schema = inferSchema(csvStr, { header: () => [] }); const rows = initParser(schema).stringArrs(csvStr).map(row => { return row.reduce((colMap, column, j) => { colMap[String.fromCharCode(65 + j)] = column