|
| 1 | +/** |
| 2 | + * Copyright (c) 2017-present PlatformIO <contact@platformio.org> |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the license found in the LICENSE file in |
| 6 | + * the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { disposeSubscriptions } from './utils'; |
| 10 | +import { extension } from './main'; |
| 11 | +import { promises as fs } from 'fs'; |
| 12 | +import path from 'path'; |
| 13 | +import vscode from 'vscode'; |
| 14 | + |
| 15 | +export default class PIOReleaseNotes { |
| 16 | + constructor() { |
| 17 | + this.version = extension.context.extension.packageJSON.version; |
| 18 | + this._currentPanel = undefined; |
| 19 | + |
| 20 | + this.subscriptions = [ |
| 21 | + vscode.commands.registerCommand('platformio-ide.showReleaseNotes', () => |
| 22 | + this.toggle() |
| 23 | + ), |
| 24 | + ]; |
| 25 | + |
| 26 | + const stateKey = 'showedReleaseNotesFor'; |
| 27 | + if (extension.context.globalState.get(stateKey) !== this.version) { |
| 28 | + extension.context.globalState.update(stateKey, this.version); |
| 29 | + this.toggle(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + dispose() { |
| 34 | + disposeSubscriptions(this.subscriptions); |
| 35 | + } |
| 36 | + |
| 37 | + async toggle() { |
| 38 | + const column = vscode.window.activeTextEditor |
| 39 | + ? vscode.window.activeTextEditor.viewColumn |
| 40 | + : undefined; |
| 41 | + try { |
| 42 | + if (this._currentPanel) { |
| 43 | + this._currentPanel.webview.html = await this.getWebviewContent(); |
| 44 | + return this._currentPanel.reveal(column); |
| 45 | + } |
| 46 | + } catch (err) { |
| 47 | + console.warn(err); |
| 48 | + } |
| 49 | + this._currentPanel = await this.newPanel(); |
| 50 | + } |
| 51 | + |
| 52 | + async newPanel(startUrl) { |
| 53 | + const panel = vscode.window.createWebviewPanel( |
| 54 | + 'pioReleaseNotes', |
| 55 | + 'PlatformIO IDE: Release Notes', |
| 56 | + vscode.ViewColumn.One, |
| 57 | + { |
| 58 | + enableScripts: true, |
| 59 | + retainContextWhenHidden: true, |
| 60 | + } |
| 61 | + ); |
| 62 | + panel.onDidDispose( |
| 63 | + () => (this._currentPanel = undefined), |
| 64 | + undefined, |
| 65 | + this.subscriptions |
| 66 | + ); |
| 67 | + panel.webview.html = await this.getWebviewContent(startUrl); |
| 68 | + return panel; |
| 69 | + } |
| 70 | + |
| 71 | + async getWebviewContent() { |
| 72 | + const releaseNotes = await this.readReleaseNotes(); |
| 73 | + return ` |
| 74 | +<!DOCTYPE html> |
| 75 | +<html lang="en"> |
| 76 | +<head> |
| 77 | + <meta charset="UTF-8"> |
| 78 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 79 | + <title>PlatformIO IDE: Release Notes</title> |
| 80 | +</head> |
| 81 | +<body> |
| 82 | + <div id="content">Loading...</div> |
| 83 | + <textarea id="pioRNMarkdown" hidden="hidden"> |
| 84 | +# PlatformIO IDE Release Notes |
| 85 | +
|
| 86 | +Welcome to the ${this.version} release of PlatformIO IDE. |
| 87 | +There are many updates in this version that we hope you'll like. |
| 88 | +
|
| 89 | +**PlatformIO Core**: If you would like to read the PlatformIO Core release notes, go to the [Release Notes](https://docs.platformio.org/en/latest/core/history.html) on [docs.platformio.org](https://docs.platformio.org/). |
| 90 | +
|
| 91 | +${releaseNotes} |
| 92 | +
|
| 93 | +## Stay in touch with us |
| 94 | +
|
| 95 | +Please follow us on [LinkedIn](https://www.linkedin.com/company/platformio) and Twitter [@PlatformIO_Org](https://twitter.com/PlatformIO_Org) to keep up to date with the latest news, articles and tips! |
| 96 | +
|
| 97 | +----- |
| 98 | +
|
| 99 | +**Release History**: Want to read release notes for the previous versions? Please visit [PlatformIO IDE Changelog](https://github.com/platformio/platformio-vscode-ide/blob/develop/CHANGELOG.md) for more detailed information. |
| 100 | +
|
| 101 | + </textarea> |
| 102 | + <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> |
| 103 | + <script> |
| 104 | + document.getElementById('content').innerHTML = |
| 105 | + marked.parse(document.getElementById('pioRNMarkdown').value); |
| 106 | + </script> |
| 107 | +</body> |
| 108 | +</html>`; |
| 109 | + } |
| 110 | + |
| 111 | + async readReleaseNotes() { |
| 112 | + const changelogPath = path.join(extension.context.extensionPath, 'CHANGELOG.md'); |
| 113 | + try { |
| 114 | + const contents = await fs.readFile(changelogPath, { encoding: 'utf-8' }); |
| 115 | + const startsAt = contents.indexOf('\n## '); |
| 116 | + return contents.substring(startsAt, contents.indexOf('\n## ', startsAt + 3)); |
| 117 | + } catch (err) { |
| 118 | + return err.toString(); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments