diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a28ea4e..0a54e4a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - [#374](https://github.com/estruyf/vscode-front-matter/issues/374): Hide the front matter section to use the panel instead - [#383](https://github.com/estruyf/vscode-front-matter/issues/383): Add the item menu to the content list view - [#385](https://github.com/estruyf/vscode-front-matter/issues/385): If no default value for the draft field is defined, the field value will be set to `true` +- [#388](https://github.com/estruyf/vscode-front-matter/issues/388): New stop server action has been added to the panel - [#390](https://github.com/estruyf/vscode-front-matter/issues/390): Implement another JSON parser in order to be able to parse the `frontmatter.json` file better ### ⚡️ Optimizations diff --git a/src/listeners/panel/DataListener.ts b/src/listeners/panel/DataListener.ts index aa2ab62e..722f4286 100644 --- a/src/listeners/panel/DataListener.ts +++ b/src/listeners/panel/DataListener.ts @@ -16,6 +16,7 @@ const FILE_LIMIT = 10; export class DataListener extends BaseListener { private static lastMetadataUpdate: any = {}; + private static readonly terminalName: string = 'Local server'; /** * Process the messages for the dashboard views @@ -41,6 +42,9 @@ export class DataListener extends BaseListener { case CommandToCode.frameworkCommand: this.openTerminalWithCommand(msg.data.command); break; + case CommandToCode.stopServer: + this.stopServer(); + break; case CommandToCode.updatePlaceholder: this.updatePlaceholder(msg?.data?.field, msg?.data?.value, msg?.data?.title); break; @@ -305,23 +309,48 @@ export class DataListener extends BaseListener { */ private static openTerminalWithCommand(command: string) { if (command) { - let terminal = window.activeTerminal; + let localServerTerminal = DataListener.findServerTerminal(); + if (localServerTerminal) { + localServerTerminal.dispose(); + } - if (!terminal || (terminal && terminal.state.isInteractedWith === true)) { - terminal = window.createTerminal({ - name: `Starting local server`, + if (!localServerTerminal || (localServerTerminal && localServerTerminal.state.isInteractedWith === true)) { + localServerTerminal = window.createTerminal({ + name: this.terminalName, iconPath: new ThemeIcon('server-environment'), message: `Starting local server`, }); } - if (terminal) { - terminal.sendText(command); - terminal.show(false); + if (localServerTerminal) { + localServerTerminal.sendText(command); + localServerTerminal.show(false); } } } + /** + * Stop the local server + */ + private static stopServer() { + const localServerTerminal = DataListener.findServerTerminal(); + if (localServerTerminal) { + localServerTerminal.dispose(); + } + } + + /** + * Find the server terminal + * @returns + */ + private static findServerTerminal() { + let terminals = window.terminals; + if (terminals) { + const localServerTerminal = terminals.find(t => t.name === DataListener.terminalName); + return localServerTerminal; + } + } + /** * Update the placeholder * @param field diff --git a/src/panelWebView/CommandToCode.ts b/src/panelWebView/CommandToCode.ts index 935b3757..9a94e557 100644 --- a/src/panelWebView/CommandToCode.ts +++ b/src/panelWebView/CommandToCode.ts @@ -39,4 +39,5 @@ export enum CommandToCode { setContentType = "set-content-type", getDataEntries = "get-data-entries", generateSlug = "generate-slug", + stopServer = "stop-server", } \ No newline at end of file diff --git a/src/panelWebView/components/StartServerButton.tsx b/src/panelWebView/components/StartServerButton.tsx index f22e675b..0ef02424 100644 --- a/src/panelWebView/components/StartServerButton.tsx +++ b/src/panelWebView/components/StartServerButton.tsx @@ -14,8 +14,17 @@ export const StartServerButton: React.FunctionComponent const startLocalServer = (command: string) => { Messenger.send(CommandToCode.frameworkCommand, { command }); }; + + const stopLocalServer = () => { + Messenger.send(CommandToCode.stopServer); + }; return ( - startCommand ? : null + startCommand ? ( + <> + + + + ) : null ); }; \ No newline at end of file