Skip to content

Commit

Permalink
Trim code.
Browse files Browse the repository at this point in the history
  • Loading branch information
cweijan committed Mar 29, 2024
1 parent a0fe755 commit 2b6339e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 44 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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'],
Expand Down
50 changes: 10 additions & 40 deletions src/provider/officeViewerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -39,45 +38,39 @@ 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),
path: handler.panel.webview.asWebviewUri(uri).with({ query: `nonce=${Date.now().toString()}` }).toString(),
})
}

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":
case ".xls":
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":
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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"))
Expand All @@ -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"))
});

}

}
2 changes: 1 addition & 1 deletion src/react/util/vscode.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/react/view/excel/excel_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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
Expand Down

0 comments on commit 2b6339e

Please sign in to comment.