Skip to content

Commit 8c88a84

Browse files
committed
Merge branch 'dev' into AL/CR/LK-Jest_testing
2 parents 15ec4c5 + 2da4e37 commit 8c88a84

File tree

4 files changed

+90
-28
lines changed

4 files changed

+90
-28
lines changed

src/extension.ts

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,79 @@
11
import * as vscode from 'vscode';
22
import {createPanel} from './panel';
33
import { Parser } from './parser';
4+
import { Tree } from './types/tree';
5+
6+
let tree: Parser | undefined = undefined;
7+
let panel: vscode.WebviewPanel | undefined = undefined
8+
49

510
// This method is called when your extension is activated
611
// Your extension is activated the very first time the command is executed
7-
812
function activate(context: vscode.ExtensionContext) {
913

10-
let disposable = vscode.commands.registerCommand('react-labyrinth.helloWorld', function () {
11-
vscode.window.showInformationMessage('Hello World from React Labyrinth!');
12-
});
14+
// This is the column where Webview will be revealed to
15+
let columnToShowIn : vscode.ViewColumn | undefined = vscode.window.activeTextEditor
16+
? vscode.window.activeTextEditor.viewColumn
17+
: undefined;
18+
19+
20+
// Command that allows for User to select the root file of their React application.
21+
const pickFile: vscode.Disposable = vscode.commands.registerCommand('myExtension.pickFile', async () => {
22+
23+
// Check if there is an existing webview panel, if so display it.
24+
if(panel) {
25+
panel.reveal(columnToShowIn)
26+
}
1327

14-
// pass in the command we want to register (refer to package.json)
15-
// let result = vscode.commands.registerCommand('myExtension.showPanel', () => {
16-
// // call helper func
17-
// createPanel(context);
18-
// });
1928

20-
vscode.commands.registerCommand('myExtension.pickFile', async () => {
21-
const fileArray = await vscode.window.showOpenDialog({ canSelectFolders: false, canSelectFiles: true, canSelectMany: false });
29+
// Opens window for the User to select the root file of React application
30+
const fileArray: vscode.Uri[] = await vscode.window.showOpenDialog({ canSelectFolders: false, canSelectFiles: true, canSelectMany: false });
2231

32+
33+
// Throw error message if no file was selected
2334
if (!fileArray || fileArray.length === 0) {
2435
vscode.window.showErrorMessage('No file selected');
2536
return;
2637
}
27-
28-
const tree = new Parser(fileArray[0].path);
38+
39+
40+
// Create Tree to be inserted into returned HTML
41+
tree = new Parser(fileArray[0].path);
2942
tree.parse();
30-
const data = tree.getTree();
31-
console.log('Data sent back: \n', data);
32-
createPanel(context, data);
43+
const data: Tree = tree.getTree();
44+
45+
46+
// Check if panel currently has a webview, if it does dispose of it and create another with updated root file selected.
47+
// Otherwise create a new webview to display root file selected.
48+
if(!panel) {
49+
panel = createPanel(context, data, columnToShowIn);
50+
} else {
51+
panel.dispose()
52+
panel = createPanel(context, data, columnToShowIn);
53+
}
54+
55+
// Listens for when webview is closed and disposes of webview resources
56+
panel.onDidDispose(
57+
() => {
58+
console.log("Before: ", panel)
59+
panel.dispose()
60+
panel = undefined;
61+
columnToShowIn = undefined;
62+
console.log("After: ", panel)
63+
},
64+
null,
65+
context.subscriptions
66+
);
3367
});
34-
context.subscriptions.push(disposable);
68+
69+
70+
// Command to show panel if it is hidden
71+
const showPanel: vscode.Disposable = vscode.commands.registerCommand('myExtension.showPanel', () => {
72+
panel.reveal(columnToShowIn)
73+
});
74+
75+
76+
context.subscriptions.push(pickFile, showPanel);
3577
}
3678

3779
// This method is called when your extension is deactivated

src/panel.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,48 @@ import * as vscode from 'vscode';
22
import { getNonce } from './getNonce';
33
import { Tree } from './types/tree';
44

5-
export function createPanel(context: vscode.ExtensionContext, data: Tree) {
6-
// if the current panel exists, then reveal the column, else make one?
5+
let panel: vscode.WebviewPanel | undefined = undefined
6+
7+
export function createPanel(context: vscode.ExtensionContext, data: Tree, columnToShowIn: vscode.ViewColumn) {
78

89
// utilize method on vscode.window object to create webview
9-
const panel = vscode.window.createWebviewPanel(
10+
panel = vscode.window.createWebviewPanel(
1011
'reactLabyrinth',
1112
'React Labyrinth',
13+
1214
// create one new tab
1315
vscode.ViewColumn.One,
1416
{
1517
enableScripts: true,
1618
retainContextWhenHidden: true
1719
}
1820
);
21+
1922

23+
// Set the icon logo of extension webview
2024
panel.iconPath = vscode.Uri.joinPath(context.extensionUri, 'media', 'favicon.ico');
25+
26+
27+
// Set URI to be the path to bundle
28+
const bundlePath: vscode.Uri = vscode.Uri.joinPath(context.extensionUri, 'build', 'bundle.js');
2129

22-
const bundlePath = vscode.Uri.joinPath(context.extensionUri, 'build', 'bundle.js');
2330

2431
// set webview URI to pass into html script
25-
const bundleURI = panel.webview.asWebviewUri(bundlePath);
32+
const bundleURI: vscode.Uri = panel.webview.asWebviewUri(bundlePath);
33+
2634

2735
// render html of webview here
2836
panel.webview.html = createWebviewHTML(bundleURI, data);
2937

30-
// will need to use onDidDispose to clear cached data and reset tree when the webview and/or application is closed
31-
38+
39+
// Sends data to Flow.tsx to be displayed after parsed data is received
3240
panel.webview.onDidReceiveMessage(
3341
async (msg: any) => {
3442
switch (msg.type) {
3543
case 'onData':
3644
if (!msg.value) break;
3745
context.workspaceState.update('reactLabyrinth', msg.value);
38-
// console.log('msg.value from panel.js: ', msg.value);
46+
3947
panel.webview.postMessage(
4048
{
4149
type: 'parsed-data',
@@ -49,13 +57,18 @@ export function createPanel(context: vscode.ExtensionContext, data: Tree) {
4957
undefined,
5058
context.subscriptions
5159
);
60+
61+
return panel
5262
};
5363

64+
65+
5466
// getNonce generates a new random string each time ext is used to prevent external injection of foreign code into the html
55-
const nonce = getNonce();
67+
const nonce: string = getNonce();
68+
5669

5770
// function to create the HTML page for webview
58-
function createWebviewHTML(URI: vscode.Uri, initialData: Tree) {
71+
function createWebviewHTML(URI: vscode.Uri, initialData: Tree) : string {
5972
return (
6073
`
6174
<!DOCTYPE html>

src/webview/Flow.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import ReactFlow, {
99
Node,
1010
Edge
1111
} from "reactflow";
12-
import "reactflow/dist/style.css";
1312
import FlowBuilder from "./flowBuilder";
1413
import { Tree } from "../types/tree";
14+
import "reactflow/dist/style.css";
15+
import "./style.css";
1516

1617

1718

src/webview/style.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
html,
66
body,
77
#root,
8+
89
.App {
910
height: 100%;
1011
}
@@ -13,3 +14,8 @@ body,
1314
font-family: sans-serif;
1415
text-align: center;
1516
}
17+
18+
19+
.react-flow__handle.connectionindicator {
20+
pointer-events: none !important;
21+
}

0 commit comments

Comments
 (0)