@setoelkahfi / svara / commits / be61984

feat: unsaved files prompts save dialog on close

mellbacon committed Jun 27, 2023 at 12:23 UTC be6198459316ba175d08e5556f085d95e37cb840
2 files changed +19 -12
src/lib/EditorTabList.svelte
+6 -6
@@ -40,8 +40,8 @@
40 export function addEditorTab(path?: string, label?: string) {
41 editorTab.addEditorTab(path, label);
42 }
43 - export function closeTab(tabid: number) {
44 - editorTab.closeTab(tabid);
43 + export async function closeTab(tabid: number) {
44 + await editorTab.closeTab(tabid);
45 }
46 export function renameTab(tab, label, path) {
47 if (tab) {
@@ -50,8 +50,8 @@
50 editorTab.setActive(tab.id);
51 }
52 }
53 - function closeAllTabs() {
54 - editorTab.closeAllTabs();
53 + async function closeAllTabs() {
54 + await editorTab.closeAllTabs();
55 }
56
57 export let hidden = editorTab.hidden;
@@ -59,10 +59,10 @@
59 export let tabs = editorTab.tabs;
60 </script>
61 <div id="editor-tabs" class:hidden={$hidden}>
62 - <TabList tabs={tabs} on:closetab={(e) => {closeTab(e.detail.tabid)}} on:select={(e) => {editorTab.setActive(e.detail.tabid)}}></TabList>
62 + <TabList tabs={tabs} on:closetab={async (e) => {await closeTab(e.detail.tabid)}} on:select={(e) => {editorTab.setActive(e.detail.tabid)}}></TabList>
63 <div class="tab-toolbar">
64 <Dropdown right menu={{icon: VerticalDots, children: [
65 - {name: "Close All Tabs", action: () => {closeAllTabs()}},
65 + {name: "Close All Tabs", action: async () => {await closeAllTabs()}},
66 ]}}></Dropdown>
67 </div>
68 </div>
src/lib/Tab/Tab.ts
+13 -6
@@ -1,6 +1,7 @@
1 import { writable } from "svelte/store";
2 import Editor from "../Editor.svelte";
3 -import { path as p, fs } from "@tauri-apps/api";
3 +import { path as p, fs, dialog } from "@tauri-apps/api";
4 +import { saveFile } from "../File";
5
6 export class Tab {
7 id = 0;
@@ -100,9 +101,14 @@ export class Tab {
101
102 this.updateTabs();
103 }
103 - closeTab(tabid: number) {
104 + async closeTab(tabid: number) {
105 if (this.activeid === tabid) {
106 for (let i = 0; i <= this.tablist.length - 1; i++) {
107 + if (this.tablist[i].id === tabid && !this.tablist[i].saved) {
108 + if (await dialog.confirm(`Do you want to save ${this.tablist[i].label} before closing?`, {type: "warning"})) {
109 + await saveFile();
110 + }
111 + }
112 // set right tab active
113 if (this.tablist[i].id === tabid && this.tablist[i + 1]) {
114 this.setActive(this.tablist[i + 1].id);
@@ -115,7 +121,7 @@ export class Tab {
121 }
122 }
123 }
118 -
124 +
125 this.tablist.find(t => t.id === tabid).content.$destroy();
126 this.tablist = this.tablist.filter(t => t.id !== tabid);
127 this.tabs.set(this.tablist);
@@ -126,9 +132,10 @@ export class Tab {
132 this.id = 0;
133 }
134 }
129 - closeAllTabs() {
130 - for (const tab of this.tablist) {
131 - this.closeTab(tab.id);
135 + async closeAllTabs() {
136 + await this.closeTab(this.activeid);
137 + for (const tab of this.tablist.reverse()) {
138 + await this.closeTab(tab.id);
139 }
140 }
141 updateView() {