|
1 | 1 | import * as vscode from 'vscode'; |
2 | 2 | import {createPanel} from './panel'; |
3 | 3 | 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 | + |
4 | 9 |
|
5 | 10 | // This method is called when your extension is activated |
6 | 11 | // Your extension is activated the very first time the command is executed |
7 | | - |
8 | 12 | function activate(context: vscode.ExtensionContext) { |
9 | 13 |
|
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 | + const 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 | + |
13 | 23 |
|
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 | | - // }); |
| 24 | + // Check if there is an existing webview panel, if so display it. |
| 25 | + if(panel) { |
| 26 | + panel.reveal(columnToShowIn) |
| 27 | + } |
19 | 28 |
|
20 | | - vscode.commands.registerCommand('myExtension.pickFile', async () => { |
21 | | - const fileArray = await vscode.window.showOpenDialog({ canSelectFolders: false, canSelectFiles: true, canSelectMany: false }); |
| 29 | + |
| 30 | + // Opens window for the User to select the root file of React application |
| 31 | + const fileArray: vscode.Uri[] = await vscode.window.showOpenDialog({ canSelectFolders: false, canSelectFiles: true, canSelectMany: false }); |
22 | 32 |
|
| 33 | + |
| 34 | + // Throw error message if no file was selected |
23 | 35 | if (!fileArray || fileArray.length === 0) { |
24 | 36 | vscode.window.showErrorMessage('No file selected'); |
25 | 37 | return; |
26 | 38 | } |
27 | | - |
28 | | - const tree = new Parser(fileArray[0].path); |
| 39 | + |
| 40 | + |
| 41 | + // Create Tree to be inserted into returned HTML |
| 42 | + tree = new Parser(fileArray[0].path); |
29 | 43 | tree.parse(); |
30 | | - const data = tree.getTree(); |
31 | | - console.log('Data sent back: \n', data); |
32 | | - createPanel(context, data); |
| 44 | + const data: Tree = tree.getTree(); |
| 45 | + |
| 46 | + |
| 47 | + // Check if panel currently has a webview, if it does dispose of it and create another with updated root file selected. |
| 48 | + // Otherwise create a new webview to display root file selected. |
| 49 | + if(!panel) { |
| 50 | + panel = createPanel(context, data, columnToShowIn); |
| 51 | + } else { |
| 52 | + panel.dispose() |
| 53 | + panel = createPanel(context, data, columnToShowIn); |
| 54 | + } |
33 | 55 | }); |
34 | | - context.subscriptions.push(disposable); |
| 56 | + |
| 57 | + |
| 58 | + // Command to show panel if it is hidden |
| 59 | + const showPanel: vscode.Disposable = vscode.commands.registerCommand('myExtension.showPanel', () => { |
| 60 | + panel.reveal(columnToShowIn) |
| 61 | + }); |
| 62 | + |
| 63 | + |
| 64 | + context.subscriptions.push(pickFile, showPanel); |
35 | 65 | } |
36 | 66 |
|
37 | 67 | // This method is called when your extension is deactivated |
|
0 commit comments