From 1fb936197ddd04a34d547c9c379e6ffa0a5df5a8 Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Wed, 14 Jul 2021 04:05:17 -0700 Subject: [PATCH] Implement closing the current, and all, documents from the menu bar Closes #261 Additional cleanup and refactoring with the way the backend relays the list of open documents to the frontend and prompts for confirmation. --- .../widgets/inputs/MenuBarInput.vue | 5 +- client/web/src/components/workspace/Panel.vue | 41 ++++++---- .../src/components/workspace/Workspace.vue | 17 ++--- client/web/src/utilities/response-handler.ts | 38 +++++----- client/web/wasm/src/document.rs | 25 +++++-- core/editor/src/document/document_file.rs | 8 +- .../src/document/document_message_handler.rs | 74 +++++++++++++++---- .../src/frontend/frontend_message_handler.rs | 7 +- core/editor/src/input/input_mapper.rs | 8 +- 9 files changed, 148 insertions(+), 75 deletions(-) diff --git a/client/web/src/components/widgets/inputs/MenuBarInput.vue b/client/web/src/components/widgets/inputs/MenuBarInput.vue index 0bc2731beb..9c00b4ed3b 100644 --- a/client/web/src/components/widgets/inputs/MenuBarInput.vue +++ b/client/web/src/components/widgets/inputs/MenuBarInput.vue @@ -83,8 +83,8 @@ const menuEntries: MenuListEntries = [ }, ], [ - { label: "Close", shortcut: ["Ctrl", "W"] }, - { label: "Close All", shortcut: ["Ctrl", "Alt", "W"] }, + { label: "Close", shortcut: ["Ctrl", "W"], action: async () => (await wasm).close_active_document_with_confirmation() }, + { label: "Close All", shortcut: ["Ctrl", "Alt", "W"], action: async () => (await wasm).close_all_documents_with_confirmation() }, ], [ { label: "Save", shortcut: ["Ctrl", "S"] }, @@ -154,6 +154,7 @@ export default defineComponent({ window.open("https://www.graphite.design", "_blank"); }, actionNotImplemented() { + // eslint-disable-next-line no-alert alert("This action is not yet implemented"); }, }, diff --git a/client/web/src/components/workspace/Panel.vue b/client/web/src/components/workspace/Panel.vue index 874c4d20f1..771a87ff27 100644 --- a/client/web/src/components/workspace/Panel.vue +++ b/client/web/src/components/workspace/Panel.vue @@ -7,11 +7,11 @@ :class="{ active: tabIndex === tabActiveIndex }" v-for="(tabLabel, tabIndex) in tabLabels" :key="tabLabel" - @click.middle="closeTab(tabIndex)" + @click.middle="handleTabClose(tabIndex)" @click="handleTabClick(tabIndex)" > {{ tabLabel }} - + @@ -150,7 +150,7 @@ import Minimap from "../panels/Minimap.vue"; import IconButton from "../widgets/buttons/IconButton.vue"; import PopoverButton, { PopoverButtonIcon } from "../widgets/buttons/PopoverButton.vue"; import { MenuDirection } from "../widgets/floating-menus/FloatingMenu.vue"; -import { ResponseType, registerResponseHandler, Response } from "../../utilities/response-handler"; +import { ResponseType, registerResponseHandler, Response, PromptConfirmationToCloseDocument } from "../../utilities/response-handler"; const wasm = import("../../../wasm/pkg"); @@ -164,24 +164,37 @@ export default defineComponent({ PopoverButton, }, methods: { - async handleTabClick(tabIndex: number) { - if (this.panelType !== "Document") return; - + handleTabClick(tabIndex: number) { + if (this.panelType === "Document") this.selectDocument(tabIndex); + }, + handleTabClose(tabIndex: number) { + if (this.panelType === "Document") this.closeDocumentWithConfirmation(tabIndex); + }, + async selectDocument(tabIndex: number) { const { select_document } = await wasm; select_document(tabIndex); }, - async closeTab(tabIndex: number) { - if (this.panelType !== "Document") return; - - const { close_document } = await wasm; + async closeDocumentWithConfirmation(tabIndex: number) { // eslint-disable-next-line no-alert - const result = window.confirm("Closing this document will permanently discard all work. Continue?"); - if (result) close_document(tabIndex); + const userConfirmation = window.confirm("Closing this document will permanently discard all work. Continue?"); + if (userConfirmation) (await wasm).close_document(tabIndex); + }, + async closeAllDocumentsWithConfirmation() { + // eslint-disable-next-line no-alert + const userConfirmation = window.confirm("Closing all documents will permanently discard all work in each of them. Continue?"); + if (userConfirmation) (await wasm).close_all_documents(); }, }, mounted() { - registerResponseHandler(ResponseType.PromptCloseConfirmationModal, (_responseData: Response) => { - this.closeTab(this.tabActiveIndex); + // TODO: Move these somewhere more appropriate to act upon all panels + + registerResponseHandler(ResponseType.PromptConfirmationToCloseDocument, (responseData: Response) => { + const promptData = responseData as PromptConfirmationToCloseDocument; + this.closeDocumentWithConfirmation(promptData.document_index); + }); + + registerResponseHandler(ResponseType.PromptConfirmationToCloseAllDocuments, (_responseData: Response) => { + this.closeAllDocumentsWithConfirmation(); }); }, props: { diff --git a/client/web/src/components/workspace/Workspace.vue b/client/web/src/components/workspace/Workspace.vue index 326d7e3cf2..10583a5282 100644 --- a/client/web/src/components/workspace/Workspace.vue +++ b/client/web/src/components/workspace/Workspace.vue @@ -46,7 +46,7 @@