-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathTextTab.ts
More file actions
333 lines (279 loc) · 8.46 KB
/
Copy pathTextTab.ts
File metadata and controls
333 lines (279 loc) · 8.46 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
import { FileTab, TReadOnlyMode } from '/@/components/TabSystem/FileTab'
import TextTabComponent from './TextTab.vue'
import type { editor } from 'monaco-editor'
import { IDisposable } from '/@/types/disposable'
import { App } from '/@/App'
import { TabSystem } from '/@/components/TabSystem/TabSystem'
import { settingsState } from '/@/components/Windows/Settings/SettingsState'
import { debounce } from 'lodash-es'
import { Signal } from '/@/components/Common/Event/Signal'
import { AnyFileHandle } from '/@/components/FileSystem/Types'
import { markRaw } from 'vue'
import { loadMonaco, useMonaco } from '../../../utils/libs/useMonaco'
import { wait } from '/@/utils/wait'
const throttledCacheUpdate = debounce<(tab: TextTab) => Promise<void> | void>(
async (tab) => {
if (!tab.editorModel || tab.editorModel.isDisposed()) return
const fileContent = tab.editorModel?.getValue()
const app = await App.getApp()
app.project.fileChange.dispatch(tab.getPath(), await tab.getFile())
await app.project.packIndexer.updateFile(
tab.getPath(),
fileContent,
tab.isForeignFile,
true
)
await app.project.jsonDefaults.updateDynamicSchemas(tab.getPath())
},
600
)
export class TextTab extends FileTab {
component = TextTabComponent
editorModel: editor.ITextModel | undefined
editorViewState: editor.ICodeEditorViewState | undefined
disposables: (IDisposable | undefined)[] = []
isActive = false
protected modelLoaded = new Signal<void>()
protected initialVersionId: number = 0
get editorInstance() {
return this.parent.monacoEditor
}
constructor(
parent: TabSystem,
fileHandle: AnyFileHandle,
readOnlyMode?: TReadOnlyMode
) {
super(parent, fileHandle, readOnlyMode)
this.fired.then(async () => {
const app = await App.getApp()
await app.projectManager.projectReady.fired
app.project.tabActionProvider.addTabActions(this)
})
}
static from(fileTab: FileTab) {
const tab = new TextTab(
fileTab.tabSystem,
fileTab.getFileHandle(),
fileTab.readOnlyMode
)
tab.isTemporary = fileTab.isTemporary
tab.setReadOnly(fileTab.readOnlyMode)
return tab
}
async getFile() {
if (!this.editorModel || this.editorModel.isDisposed())
return await super.getFile()
return new File([this.editorModel.getValue()], this.name)
}
updateUnsavedStatus() {
if (!this.editorModel || this.editorModel.isDisposed()) return
this.setIsUnsaved(
this.initialVersionId !==
this.editorModel?.getAlternativeVersionId()
)
}
fileDidChange() {
// Updates the isUnsaved status of the tab
this.updateUnsavedStatus()
super.fileDidChange()
}
async onActivate() {
if (this.isActive) return
this.isActive = true
// Load monaco in
if (!loadMonaco.hasFired) {
this.isLoading = true
loadMonaco.dispatch()
// Monaco theme isn't loaded yet
await this.parent.app.themeManager.applyMonacoTheme()
}
const { editor, Uri } = await useMonaco()
await this.parent.fired //Make sure a monaco editor is loaded
await wait(1)
this.isLoading = false
if (!this.editorModel || this.editorModel.isDisposed()) {
const file = await this.fileHandle.getFile()
const fileContent = await file.text()
const uri = Uri.file(this.getPath())
this.editorModel = markRaw(
editor.getModel(uri) ??
editor.createModel(
fileContent,
App.fileType.get(this.getPath())?.meta?.language,
uri
)
)
this.initialVersionId = this.editorModel.getAlternativeVersionId()
this.modelLoaded.dispatch()
await this.loadEditor(false)
} else {
await this.loadEditor()
}
this.disposables.push(
this.editorModel?.onDidChangeContent(() => {
throttledCacheUpdate(this)
this.fileDidChange()
})
)
this.disposables.push(
this.editorInstance?.onDidFocusEditorText(() => {
this.parent.setActive(true)
})
)
this.editorInstance?.layout()
super.onActivate()
}
async onDeactivate() {
await super.onDeactivate()
// MonacoEditor is defined
if (this.tabSystem.hasFired) {
const viewState = this.editorInstance.saveViewState()
if (viewState) this.editorViewState = markRaw(viewState)
}
this.disposables.forEach((disposable) => disposable?.dispose())
this.isActive = false
}
onDestroy() {
this.disposables.forEach((disposable) => disposable?.dispose())
this.editorModel?.dispose()
this.editorModel = undefined
this.editorViewState = undefined
this.isActive = false
this.modelLoaded.resetSignal()
}
updateParent(parent: TabSystem) {
super.updateParent(parent)
}
focus() {
this.editorInstance?.focus()
}
async loadEditor(shouldFocus = true) {
await this.parent.fired //Make sure a monaco editor is loaded
if (this.editorModel && !this.editorModel.isDisposed())
this.editorInstance.setModel(this.editorModel)
if (this.editorViewState)
this.editorInstance.restoreViewState(this.editorViewState)
this.editorInstance?.updateOptions({ readOnly: this.isReadOnly })
if (shouldFocus) setTimeout(() => this.focus(), 10)
}
async _save() {
this.isTemporary = false
const app = await App.getApp()
const action = this.editorInstance?.getAction(
'editor.action.formatDocument'
)
const fileType = App.fileType.get(this.getPath())
const fileContentStr = this.editorModel?.getValue()
if (
// Make sure that there is fileContent to format,
fileContentStr &&
fileContentStr !== '' &&
// ...that we have an action to trigger,
action &&
// ...that the file is a valid fileType,
fileType &&
// ...that formatOnSave is enabled,
(settingsState?.general?.formatOnSave ?? true) &&
// ...and that the current file type supports formatting
(fileType?.formatOnSaveCapable ?? true)
) {
// This is a terrible hack because we need to make sure that the formatter triggers the "onDidChangeContent" event
// The promise returned by action.run() actually resolves before formatting is done so we need the "onDidChangeContent" event to tell when the formatter is done
this.makeFakeEdit('\t')
const editPromise = new Promise<void>((resolve) => {
if (!this.editorModel || this.editorModel.isDisposed())
return resolve()
const disposable = this.editorModel?.onDidChangeContent(() => {
disposable?.dispose()
resolve()
})
})
const actionPromise = action.run()
let didAnyFinish = false
await Promise.race([
// Wait for the action to finish
Promise.all([editPromise, actionPromise]),
// But don't wait longer than 1.5s, action then likely failed for some weird reason
wait(1500).then(() => {
if (didAnyFinish) return
this.makeFakeEdit(null)
}),
])
didAnyFinish = true
}
await this.saveFile()
}
protected makeFakeEdit(text: string | null) {
if (!text) {
this.editorInstance.trigger('automatic', 'undo', null)
} else {
this.editorInstance.pushUndoStop()
this.editorInstance?.executeEdits('automatic', [
{
forceMoveMarkers: false,
range: {
startLineNumber: 1,
endLineNumber: 1,
startColumn: 1,
endColumn: 1,
},
text,
},
])
this.editorInstance.pushUndoStop()
}
}
protected async saveFile() {
if (this.editorModel && !this.editorModel.isDisposed()) {
const writeWorked = await this.writeFile(
this.editorModel.getValue()
)
if (writeWorked) {
this.setIsUnsaved(false)
this.initialVersionId =
this.editorModel.getAlternativeVersionId()
}
} else {
console.error(`Cannot save file content without active editorModel`)
}
}
setReadOnly(val: TReadOnlyMode) {
this.readOnlyMode = val
// Only update options immediately if Monaco is already loaded
if (this.parent.hasFired)
this.editorInstance.updateOptions({ readOnly: val !== 'off' })
}
async paste() {
if (this.isReadOnly) return
this.focus()
this.editorInstance?.trigger('keyboard', 'paste', {
text: await navigator.clipboard.readText(),
})
}
cut() {
if (this.isReadOnly) return
this.focus()
document.execCommand('cut')
}
async close() {
const didClose = await super.close()
// We need to clear the lightning cache store from temporary data if the user doesn't save changes
if (didClose && this.isUnsaved) {
const app = await App.getApp()
if (this.isForeignFile) {
await app.fileSystem.unlink(this.getPath())
} else {
const file = await this.fileHandle.getFile()
const fileContent = await file.text()
await app.project.packIndexer.updateFile(
this.getPath(),
fileContent
)
}
}
return didClose
}
showContextMenu(event: MouseEvent) {
this.parent.showCustomMonacoContextMenu(event, this)
}
}