-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathTabSystem.ts
More file actions
372 lines (303 loc) · 9.16 KB
/
Copy pathTabSystem.ts
File metadata and controls
372 lines (303 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { Tab } from './CommonTab'
import WelcomeScreen from './WelcomeScreen.vue'
import { TextTab } from '../Editors/Text/TextTab'
import Vue, { computed, Ref, ref } from 'vue'
import { App } from '/@/App'
import { UnsavedFileWindow } from '../Windows/UnsavedFile/UnsavedFile'
import { Project } from '../Projects/Project/Project'
import { OpenedFiles } from './OpenedFiles'
import { v4 as uuid } from 'uuid'
import { MonacoHolder } from './MonacoHolder'
import { FileTab, TReadOnlyMode } from './FileTab'
import { TabProvider } from './TabProvider'
import { AnyFileHandle } from '../FileSystem/Types'
import { IframeTab } from '../Editors/IframeTab/IframeTab'
export interface IOpenTabOptions {
selectTab?: boolean
isTemporary?: boolean
readOnlyMode?: TReadOnlyMode
}
export class TabSystem extends MonacoHolder {
protected uuid = uuid()
public tabs = <Ref<Tab[]>>ref([])
protected _selectedTab = <Ref<Tab | undefined>>ref(undefined)
protected get tabTypes() {
return TabProvider.tabs
}
protected _isActive = ref(true)
public readonly openedFiles: OpenedFiles
get isActive() {
return this._isActive
}
public readonly shouldRender = computed(() => this.tabs.value.length > 0)
public readonly isSharingScreen = computed(() => {
const other = this.project.tabSystems.find(
(tabSystem) => tabSystem !== this
)
return other?.shouldRender.value ?? false
})
get app() {
return this._app
}
get project() {
return this._project
}
get hasUnsavedTabs() {
return this.tabs.value.some((tab) => tab.isUnsaved)
}
constructor(protected _project: Project, id = 0) {
super(_project.app)
this.openedFiles = new OpenedFiles(
this,
_project.app,
`${_project.projectPath}/.bridge/openedFiles_${id}.json`
)
}
get selectedTab() {
return this._selectedTab.value
}
get currentComponent() {
return this._selectedTab.value?.component ?? WelcomeScreen
}
get projectRoot() {
return this.project.baseDirectory
}
get projectName() {
return this.project.name
}
async open(
fileHandle: AnyFileHandle,
{
selectTab = true,
readOnlyMode = 'off',
isTemporary = true,
}: IOpenTabOptions = {}
) {
const tab = await this.getTabFor(fileHandle, readOnlyMode)
// Default value is true so we only need to update if the caller wants to create a permanent tab
if (!isTemporary) tab.isTemporary = false
await this.add(tab, selectTab)
return tab
}
async openPath(path: string, options: IOpenTabOptions = {}) {
const fileHandle = await this.project.app.fileSystem.getFileHandle(path)
return await this.open(fileHandle, options)
}
protected async getTabFor(
fileHandle: AnyFileHandle,
readOnlyMode: TReadOnlyMode = 'off'
) {
let tab: Tab | undefined = undefined
for (const CurrentTab of this.tabTypes) {
if (await CurrentTab.is(fileHandle)) {
// @ts-ignore
tab = new CurrentTab(this, fileHandle, readOnlyMode)
break
}
}
// Default tab type: Text editor
if (!tab) tab = new TextTab(this, fileHandle, readOnlyMode)
return await tab.fired
}
async hasTab(tab: Tab) {
for (const currentTab of this.tabs.value) {
if (await currentTab.is(tab)) return true
}
return false
}
async add(tab: Tab, selectTab = true, noTabExistanceCheck = false) {
this.closeAllTemporary()
if (!noTabExistanceCheck) {
for (const currentTab of this.tabs.value) {
if (await currentTab.is(tab)) {
// Trigger openWith event again for iframe tabs
if (
tab instanceof IframeTab &&
currentTab instanceof IframeTab
) {
currentTab.setOpenWithPayload(
tab.getOptions().openWithPayload
)
}
tab.onDeactivate()
return selectTab ? currentTab.select() : currentTab
}
}
}
if (!tab.hasFired) await tab.fired
this.tabs.value = [...this.tabs.value, tab]
if (!tab.isForeignFile && !(tab instanceof FileTab && tab.isReadOnly))
await this.openedFiles.add(tab.getPath())
if (selectTab) tab.select()
this.project.updateTabFolders()
return tab
}
remove(tab: Tab, destroyEditor = true, selectNewTab = true) {
tab.onDeactivate()
const tabIndex = this.tabs.value.findIndex((current) => current === tab)
if (tabIndex === -1) return
this.tabs.value.splice(tabIndex, 1)
if (destroyEditor) tab.onDestroy()
if (selectNewTab && tab === this.selectedTab)
this.select(this.tabs.value[tabIndex === 0 ? 0 : tabIndex - 1])
if (!tab.isForeignFile) this.openedFiles.remove(tab.getPath())
this.project.updateTabFolders()
return tab
}
async replaceCurrent(newTab: Tab) {
const currentTab = this.selectedTab
if (!currentTab) return
currentTab.onDeactivate()
currentTab.onDestroy()
const tabIndex = this.tabs.value.findIndex(
(current) => current === currentTab
)
if (tabIndex === -1) throw new Error('Tab not found')
this.tabs.value.splice(tabIndex, 1, newTab)
if (!newTab.hasFired) await newTab.fired
newTab.select()
}
async close(tab = this.selectedTab, checkUnsaved = true) {
if (!tab) return false
if (checkUnsaved && tab.isUnsaved) {
const unsavedWin = new UnsavedFileWindow(tab)
return (await unsavedWin.fired) !== 'cancel'
} else {
this.remove(tab)
return true
}
}
async closeTabWithHandle(fileHandle: AnyFileHandle) {
const tab = await this.getTab(fileHandle)
if (tab) this.close(tab)
}
/**
* Select next tab
*/
async selectNextTab() {
const tabs = this.tabs.value
if (tabs.length === 0) return
const selectedTab = this.selectedTab
if (!selectedTab) return
const index = tabs.indexOf(selectedTab)
const nextTab = tabs[index + 1] ?? tabs[0]
await nextTab.select()
}
/**
* Select previous tab
*/
async selectPreviousTab() {
const tabs = this.tabs.value
if (tabs.length === 0) return
const selectedTab = this.selectedTab
if (!selectedTab) return
const index = tabs.indexOf(selectedTab)
const previousTab = tabs[index - 1] ?? tabs[tabs.length - 1]
await previousTab.select()
}
async select(tab?: Tab) {
if (this.isActive.value !== !!tab) this.setActive(!!tab)
if (this.app.mobile.isCurrentDevice()) App.sidebar.hide()
if (tab?.isSelected) return
this.selectedTab?.onDeactivate()
if (tab && tab !== this.selectedTab && this.project.isActiveProject) {
App.eventSystem.dispatch('currentTabSwitched', tab)
}
this._selectedTab.value = tab
// Next steps don't need to be done if we simply unselect tab
if (!tab) return
await this.selectedTab?.onActivate()
Vue.nextTick(async () => {
this._monacoEditor?.layout()
})
}
async save(tab = this.selectedTab) {
if (!tab || (tab instanceof FileTab && tab.isReadOnly)) return
tab?.setIsLoading(true)
// Save whether the tab was selected previously for use later
const tabWasActive = this.selectedTab === tab
// We need to select the tab before saving to format it correctly
const selectedTab = this.selectedTab
if (selectedTab !== tab) await this.select(tab)
if (tab instanceof FileTab) await tab.save()
// Select the previously selected tab again
if (selectedTab !== tab) await this.select(selectedTab)
if (!tab.isForeignFile && tab instanceof FileTab) {
await this.project.updateFile(tab.getPath())
this.project.fileSave.dispatch(tab.getPath(), await tab.getFile())
// Only refresh auto-completion content if tab is active
if (tabWasActive)
App.eventSystem.dispatch('refreshCurrentContext', tab.getPath())
}
tab.focus()
tab?.setIsLoading(false)
}
async saveAs() {
if (this.selectedTab instanceof FileTab) await this.selectedTab.saveAs()
}
async saveAll() {
const app = await App.getApp()
app.windows.loadingWindow.open()
for (const tab of this.tabs.value) {
if (tab.isUnsaved) await this.save(tab)
}
app.windows.loadingWindow.close()
}
closeAllTemporary() {
for (const tab of [...this.tabs.value]) {
if (!tab.isTemporary) continue
this.remove(tab, true, false)
}
}
async activate() {
await this.openedFiles.restoreTabs()
if (this.tabs.value.length > 0) this.setActive(true)
if (!this.selectedTab && this.tabs.value.length > 0)
this.tabs.value[0].select()
await this.selectedTab?.onActivate()
}
deactivate() {
this.selectedTab?.onDeactivate()
this.dispose()
}
setActive(isActive: boolean, updateProject = true) {
if (updateProject) this.project.setActiveTabSystem(this, !isActive)
if (isActive === this._isActive.value) return
this._isActive.value = isActive
if (isActive && this._selectedTab && this.project.isActiveProject) {
App.eventSystem.dispatch('currentTabSwitched', this._selectedTab)
}
}
async getTab(fileHandle: AnyFileHandle) {
for (const tab of this.tabs.value) {
if (
tab instanceof FileTab &&
(await tab.isForFileHandle(fileHandle))
)
return tab
}
}
closeTabs(predicate: (tab: Tab) => boolean) {
const tabs = [...this.tabs.value].reverse()
for (const tab of tabs) {
if (predicate(tab)) tab.close()
}
}
forceCloseTabs(predicate: (tab: Tab) => boolean) {
const tabs = [...this.tabs.value].reverse()
for (const tab of tabs) {
if (predicate(tab)) this.remove(tab)
}
}
has(predicate: (tab: Tab) => boolean) {
for (const tab of this.tabs.value) {
if (predicate(tab)) return true
}
return true
}
get(predicate: (tab: Tab) => boolean) {
for (const tab of this.tabs.value) {
if (predicate(tab)) return tab
}
}
}