-
Notifications
You must be signed in to change notification settings - Fork 2
Add UI elements to fetch the Examples Repo #317
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0c20ec9
Add UI elements to fetch the Examples Repo
esarver 1d4603d
Merge branch 'release/v1.5.2' into task/fetch_examples_button
esarver a7289bd
fix lint
esarver cc3b661
Merge branch 'release/v1.5.2' into task/fetch_examples_button
esarver 7df43ca
Change "Fetch" to "Import"
esarver 875e9e5
Merge branch 'release/v1.5.2' into task/fetch_examples_button
esarver c36917b
Fix lambda return value
esarver 57d1a53
Merge branch 'release/v1.5.2' into task/fetch_examples_button
esarver 7956a45
Add error notification if script fetch fails
esarver b29c3c2
Add error notification for when the fetch fails
esarver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import * as vscode from "vscode" | ||
| import { Uri, Webview, WebviewView, WebviewViewProvider } from "vscode" | ||
|
|
||
| export class ExtraActionsWebView implements WebviewViewProvider { | ||
| public static readonly viewType = "extraActions" | ||
| private _webviewView: vscode.WebviewView | undefined | ||
| constructor(private readonly _extensionUri: Uri) {} | ||
|
|
||
| resolveWebviewView( | ||
| webviewView: vscode.WebviewView, | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| _context: vscode.WebviewViewResolveContext, | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| _token: vscode.CancellationToken, | ||
| ) { | ||
| this._webviewView = webviewView | ||
| // Allow scripts in the webview | ||
| webviewView.webview.options = { | ||
| // Enable JavaScript in the webview | ||
| enableScripts: true, | ||
| // Restrict the webview to only load resources from the `out` directory | ||
| localResourceRoots: [Uri.joinPath(this._extensionUri)], | ||
| } | ||
|
|
||
| // Set the HTML content that will fill the webview view | ||
| webviewView.webview.html = this._getWebviewContent(webviewView.webview) | ||
|
|
||
| // Sets up an event listener to listen for messages passed from the webview view context | ||
| // and executes code based on the message that is recieved | ||
| this._setWebviewMessageListener(webviewView) | ||
| } | ||
| private _getWebviewContent(webview: Webview) { | ||
| const webviewScriptUri = this.getUri(webview, this._extensionUri, [ | ||
| "out", | ||
| "extraActions.js", | ||
| ]) | ||
| const stylesUri = this.getUri(webview, this._extensionUri, [ | ||
| "out", | ||
| "styles.css", | ||
| ]) | ||
| const codiconsUri = this.getUri(webview, this._extensionUri, [ | ||
| "node_modules", | ||
| "@vscode/codicons", | ||
| "dist", | ||
| "codicon.css", | ||
| ]) | ||
| const nonce = this.getNonce() | ||
| return /*html*/ ` | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="Content-Security-Policy" | ||
| content="default-src 'none'; style-src ${webview.cspSource}; script-src 'nonce-${nonce}'; font-src ${webview.cspSource};"> | ||
| <link rel="stylesheet" href="${stylesUri.toString()}"> | ||
| <link rel="stylesheet" href="${codiconsUri.toString()}"> | ||
| <title>Extra Actions</title> | ||
| </head> | ||
|
|
||
| <body> | ||
| <div id="action-container"></div> | ||
| <script type="module" nonce="${nonce}" src="${webviewScriptUri.toString()}"></script> | ||
| <button class="vscode-style-button" data-id="fetchExamples" id="fetchExamples" type="button">Import TSP Examples</button> | ||
| </div> | ||
| </body> | ||
| </html> | ||
| ` | ||
| } | ||
|
|
||
| private _setWebviewMessageListener(webviewView: WebviewView) { | ||
| webviewView.webview.onDidReceiveMessage(async (message) => { | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
| switch (message.command) { | ||
| case "fetchExamples": { | ||
| await vscode.commands.executeCommand( | ||
| "tsp.fetchExampleScripts", | ||
| ) | ||
| this._webviewView?.webview.postMessage({ | ||
| command: "fetchComplete", | ||
| }) | ||
| break | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| private getNonce() { | ||
| let text = "" | ||
| const possible = | ||
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" | ||
| for (let i = 0; i < 32; i++) { | ||
| text += possible.charAt(Math.floor(Math.random() * possible.length)) | ||
| } | ||
| return text | ||
| } | ||
|
|
||
| private getUri(webview: Webview, extensionUri: Uri, pathList: string[]) { | ||
| return webview.asWebviewUri(Uri.joinPath(extensionUri, ...pathList)) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Get access to the VS Code API from within the webview context | ||
| const vscode = acquireVsCodeApi(); | ||
|
|
||
| window.addEventListener("load", main) | ||
|
|
||
| // Set up an event listener to listen for messages passed from the extension context | ||
| async function waitForEventWith(predicate) { | ||
| return new Promise((res, rej) => { | ||
| const event_listener = (event) => { | ||
| if (predicate(event.data)) { | ||
| window.removeEventListener("message", event_listener) | ||
| res() | ||
|
|
||
| } | ||
| } | ||
| window.addEventListener("message", event_listener); | ||
| }) | ||
| } | ||
|
|
||
| async function fetchExamples() { | ||
| return new Promise(async (resolve, reject) => { | ||
| vscode.postMessage({command: "fetchExamples"}) | ||
| await waitForEventWith((data) => {return data.command === "fetchComplete"}) | ||
| resolve() | ||
| }) | ||
| } | ||
|
|
||
| async function main() { | ||
| const fetchExamplesButton = document.getElementById("fetchExamples") | ||
|
|
||
| fetchExamplesButton.addEventListener("click", async (event) => { | ||
| fetchExamplesButton.disabled = true | ||
| await fetchExamples() | ||
| fetchExamplesButton.disabled = false | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.