Skip to content

Fix local resources loading #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"main": "./out/extension",
"scripts": {
"compile": "tsc -p ./",
"vscode:prepublish": "npm run compile",
"copy-files": "copyfiles -u 1 \"src/result-view/htmlContent/**/*\" out",
"vscode:prepublish": "npm run compile && npm run copy-files",
"watch": "tsc -watch -p ./"
},
"contributes": {
Expand Down Expand Up @@ -285,8 +286,10 @@
"onCommand:firebird.runQuery"
],
"dependencies": {
"copyfiles": "^2.4.1",
"node-firebird": "^0.9.8",
"uuid": "^8.3.0"
"uuid": "^8.3.0",
"vscode-uri": "^3.0.7"
},
"devDependencies": {
"@types/node": "^14.11.2",
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function activate(context: ExtensionContext) {
const firebirdDatabaseWords = new KeywordsDb();
const firebirdTreeDataProvider = new FirebirdTreeDataProvider(context);
const firebirdMockData = new MockData(context.extensionPath);
const firebirdQueryResults = new QueryResultsView(context.extensionPath);
const firebirdQueryResults = new QueryResultsView(context.extensionUri);

context.subscriptions.push(
window.registerTreeDataProvider(Constants.FirebirdExplorerViewId, firebirdTreeDataProvider),
Expand Down
2 changes: 1 addition & 1 deletion src/firebirdTreeDataProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TreeDataProvider, EventEmitter, Event, ExtensionContext, TreeItem } from "vscode";
import * as uuidv1 from "uuid/v1";
import { v1 as uuidv1} from 'uuid';
import { NodeHost } from "./nodes";
import { ConnectionOptions, FirebirdTree } from "./interfaces";
import { connectionWizard } from "./shared/connection-wizard";
Expand Down
7 changes: 3 additions & 4 deletions src/result-view/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Disposable } from "vscode";
import { Disposable, Uri } from "vscode";
import { TextDecoder } from "util";
import { join } from "path";

import { QueryResultsView, Message } from "./queryResultsView";

Expand All @@ -10,7 +9,7 @@ export default class ResultView extends QueryResultsView implements Disposable {
private resultSet?: ResultSet;
private recordsPerPage: string;

constructor(private extensionPath: string) {
constructor(private extensionUri: Uri) {
super("resultview", "Firebird Query Results");
}

Expand All @@ -23,7 +22,7 @@ export default class ResultView extends QueryResultsView implements Disposable {
* DEV: "src",...
* PROD: "out",...
*/
this.show(join(this.extensionPath, "out", "result-view", "htmlContent", "index.html"));
this.show(Uri.joinPath(this.extensionUri, "out", "result-view", "htmlContent", "index.html"));
}

handleMessage(message: Message): void {
Expand Down
30 changes: 13 additions & 17 deletions src/result-view/queryResultsView.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { WebviewPanel, window, ViewColumn, Disposable, Uri, WebviewPanelOptions, WebviewOptions } from "vscode";
import { EventEmitter } from "events";
import { dirname } from "path";
import { readFile } from "fs";
import { logger } from "../logger/logger";
import { Utils as UriUtils } from 'vscode-uri';

export interface Message {
command: string;
Expand All @@ -11,20 +11,18 @@ export interface Message {
}

export class QueryResultsView extends EventEmitter implements Disposable {
private resourceScheme = "vscode-resource";
private disposable?: Disposable;

private resourcesPath: string;
private resourcesPath?: Uri;
private panel: WebviewPanel | undefined;
private htmlCache: { [path: string]: string };
constructor(private type: string, private title: string) {
super();
this.resourcesPath = "";
this.htmlCache = {};
}

show(htmlPath: string) {
this.resourcesPath = dirname(htmlPath);
show(htmlPath: Uri) {
this.resourcesPath = UriUtils.dirname(htmlPath);
if (!this.panel) {
this.init();
}
Expand All @@ -44,7 +42,7 @@ export class QueryResultsView extends EventEmitter implements Disposable {
let options: WebviewPanelOptions & WebviewOptions = {
enableScripts: true,
retainContextWhenHidden: false, // we dont need to keep the state
localResourceRoots: [Uri.parse(this.resourcesPath).with({ scheme: "vscode-resource" })]
localResourceRoots: [this.resourcesPath]
};

this.panel = window.createWebviewPanel(this.type, this.title, ViewColumn.Two, options);
Expand All @@ -62,25 +60,23 @@ export class QueryResultsView extends EventEmitter implements Disposable {
this.disposable = Disposable.from(...subscriptions);
}

private readWithCache(path: string, callback: (html: string) => void) {
private readWithCache(uri: Uri, callback: (html: string) => void) {
let html: string = "";
if (path in this.htmlCache) {
html = this.htmlCache[path];
if (uri.path in this.htmlCache) {
html = this.htmlCache[uri.path];
callback(html);
} else {
readFile(path, "utf8", (err, content) => {
readFile(uri.fsPath, "utf8", (err, content) => {
html = content || "";
html = this.replaceUris(html, path);
this.htmlCache[path] = html;
html = this.replaceUris(html, uri);
this.htmlCache[uri.path] = html;
callback(html);
});
}
}

private replaceUris(html: string, htmlPath: string) {
let basePath = Uri.parse(dirname(htmlPath))
.with({ scheme: this.resourceScheme })
.toString();
private replaceUris(html: string, htmlPath: Uri) {
let basePath = this.panel.webview.asWebviewUri(UriUtils.dirname(htmlPath)).toString();
let regex = /(href|src)\=\"(.+?)\"/g;
html = html.replace(regex, `$1="${basePath + "$2"}"`);
return html;
Expand Down
22 changes: 0 additions & 22 deletions src/test/extension.test.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/test/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
// "strict": true, /* enable all strict type-checking options */
/* Additional Checks */
"noUnusedLocals": true /* Report errors on unused locals. */,
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
// "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
"noUnusedParameters": true /* Report errors on unused parameters. */
// "noUnusedParameters": true /* Report errors on unused parameters. */
},
"exclude": ["node_modules", ".vscode-test"]
}