| 1 | import { writable } from "svelte/store"; |
| 2 | import Editor from "../Editor.svelte"; |
| 3 | import { invoke } from "@tauri-apps/api/core"; |
| 4 | import { saveFile } from "../File"; |
| 5 | import { warn } from "tauri-plugin-log-api"; |
| 6 | import { appSettings } from "../../config/config"; |
| 7 | import * as dialog from "@tauri-apps/plugin-dialog" |
| 8 | |
| 9 | export class Tab { |
| 10 | id = 0; |
| 11 | activeid = this.id; |
| 12 | tablist = []; |
| 13 | Tab; // Tab class |
| 14 | activeTab = null; |
| 15 | hidden = writable(true); |
| 16 | isfile = writable(false); |
| 17 | tabs = writable([]); |
| 18 | constructor (Tab) { |
| 19 | this.Tab = Tab; |
| 20 | } |
| 21 | |
| 22 | setActive(id: number) { |
| 23 | for (let tab of this.tablist) { |
| 24 | if (tab.id === id) { |
| 25 | this.activeid = id; |
| 26 | tab.active = true; |
| 27 | if (tab.isfile) { |
| 28 | this.isfile.set(true); |
| 29 | tab.content.focus(); |
| 30 | } |
| 31 | else { |
| 32 | this.isfile.set(false); |
| 33 | } |
| 34 | this.activeTab = tab; |
| 35 | } |
| 36 | else { |
| 37 | tab.active = false; |
| 38 | } |
| 39 | } |
| 40 | this.tabs.set(this.tablist); |
| 41 | this.updateView(); |
| 42 | } |
| 43 | updateTabs() { |
| 44 | if (this.tablist.length > 0) { |
| 45 | this.hidden.set(false); |
| 46 | } |
| 47 | |
| 48 | this.tabs.set(this.tablist); |
| 49 | this.setActive(this.id); |
| 50 | this.id++; |
| 51 | } |
| 52 | tabOpen(path: string) { |
| 53 | for (const tab of this.tablist) { |
| 54 | if (tab.path === path) { |
| 55 | this.setActive(tab.id); |
| 56 | return true; |
| 57 | } |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | addTab(path: string = "", label: string = "", content = null) { |
| 62 | // dont add tabs that are already open |
| 63 | if (this.tabOpen(path)) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | let tab = new this.Tab(this.id, label, content, path); |
| 68 | this.tablist = [...this.tablist, tab]; |
| 69 | |
| 70 | this.updateTabs(); |
| 71 | } |
| 72 | async addEditorTab(path: string, label: string = "") { |
| 73 | if (this.tabOpen(path)) { |
| 74 | return; |
| 75 | } |
| 76 | let fileData = {text: "", encoding: "UTF-8", extension: "", bom: false, spaces: await appSettings.get("editor.tabSize")}; |
| 77 | try { |
| 78 | fileData = await invoke("read_file", {path: path}); |
| 79 | if (fileData.spaces === 0) { |
| 80 | fileData.spaces = await appSettings.get("editor.tabSize") |
| 81 | } |
| 82 | } catch (error) { |
| 83 | warn(`Can't read file content in ${path}. Setting to empty string. Error: ${error}`, {file: "Tab.ts", line: 79}); |
| 84 | } |
| 85 | let content = new Editor({target: document.getElementById("tabview"), props: {content: fileData.text}}); |
| 86 | let tab = new this.Tab(this.id, label, content, path); |
| 87 | tab.isfile = true; |
| 88 | tab.saved = true; |
| 89 | |
| 90 | content.updateFileInfo({ |
| 91 | "filename": tab.label, |
| 92 | "path": tab.path, |
| 93 | "fileType": fileData.extension, |
| 94 | "language": await content.getLang(fileData.extension), |
| 95 | "encoding": fileData.encoding, |
| 96 | "hasBom": fileData.bom, |
| 97 | "spaces": fileData.spaces, |
| 98 | "readonly": false, |
| 99 | }); |
| 100 | this.tablist = [...this.tablist, tab]; |
| 101 | |
| 102 | this.updateView(); |
| 103 | |
| 104 | this.updateTabs(); |
| 105 | } |
| 106 | async closeTab(tabid: number) { |
| 107 | if (this.activeid === tabid) { |
| 108 | for (let i = 0; i <= this.tablist.length - 1; i++) { |
| 109 | if (this.tablist[i].id === tabid && !this.tablist[i].saved) { |
| 110 | if (await dialog.ask(`Do you want to save ${this.tablist[i].label} before closing?`, {type: "warning"})) { |
| 111 | await saveFile(); |
| 112 | } |
| 113 | } |
| 114 | // set right tab active |
| 115 | if (this.tablist[i].id === tabid && this.tablist[i + 1]) { |
| 116 | this.setActive(this.tablist[i + 1].id); |
| 117 | break; |
| 118 | } |
| 119 | // set left tab active |
| 120 | else if (this.tablist[i].id === tabid && this.tablist[i - 1]) { |
| 121 | this.setActive(this.tablist[i - 1].id); |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | this.tablist.find(t => t.id === tabid).content.$destroy(); |
| 128 | this.tablist = this.tablist.filter(t => t.id !== tabid); |
| 129 | this.tabs.set(this.tablist); |
| 130 | |
| 131 | if (this.tablist.length === 0) { |
| 132 | this.hidden.set(true); |
| 133 | this.isfile.set(false); |
| 134 | this.id = 0; |
| 135 | } |
| 136 | } |
| 137 | async closeAllTabs() { |
| 138 | await this.closeTab(this.activeid); |
| 139 | const temp = [...this.tablist].reverse(); // js just loves to be inconsistent with its array functions innit |
| 140 | for (const tab of temp) { |
| 141 | await this.closeTab(tab.id); |
| 142 | } |
| 143 | } |
| 144 | updateView() { |
| 145 | for (let tab of this.tablist) { |
| 146 | tab.updateView(this.activeid); |
| 147 | } |
| 148 | } |
| 149 | refreshTabList() { |
| 150 | this.tabs.set(this.tablist); |
| 151 | } |
| 152 | } |