Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: switch editor action #861

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/components/Editors/Text/TextTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ export class TextTab extends FileTab {
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()
Expand Down Expand Up @@ -271,7 +285,10 @@ export class TextTab extends FileTab {

setReadOnly(val: TReadOnlyMode) {
this.readOnlyMode = val
this.editorInstance?.updateOptions({ readOnly: val !== 'off' })

// Only update options immediately if Monaco is already loaded
if (this.parent.hasFired)
this.editorInstance.updateOptions({ readOnly: val !== 'off' })
}

async paste() {
Expand Down
12 changes: 12 additions & 0 deletions src/components/Editors/TreeEditor/Tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ export class TreeTab extends FileTab {
app.project.tabActionProvider.addTabActions(this)
})
}
static from(fileTab: FileTab) {
const tab = new TreeTab(
fileTab.tabSystem,
fileTab.getFileHandle(),
fileTab.readOnlyMode
)

tab.isTemporary = fileTab.isTemporary
tab.setReadOnly(fileTab.readOnlyMode)

return tab
}

get app() {
return this.parent.app
Expand Down
19 changes: 19 additions & 0 deletions src/components/TabSystem/TabSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ export class TabSystem extends MonacoHolder {

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

Expand Down
23 changes: 23 additions & 0 deletions src/components/Toolbar/Category/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { ViewCompilerOutput } from '../../UIElements/DirectoryViewer/ContextMenu
import { Divider } from '../Divider'
import { platform } from '/@/utils/os'
import { fullScreenAction } from '../../TabSystem/TabContextMenu/Fullscreen'
import { TextTab } from '../../Editors/Text/TextTab'
import { TreeTab } from '../../Editors/TreeEditor/Tab'

export function setupViewCategory(app: App) {
const view = new ToolbarCategory('mdi-eye-outline', 'toolbar.view.name')
Expand Down Expand Up @@ -144,6 +146,27 @@ export function setupViewCategory(app: App) {
},
})
)
view.addItem(
app.actionManager.create({
icon: 'mdi-pencil-outline',
name: 'actions.switchEditorMode.name',
description: 'actions.switchEditorMode.description',
onTrigger: async () => {
const currentTab = app.tabSystem?.selectedTab
if (
!(currentTab instanceof TextTab) &&
!(currentTab instanceof TreeTab)
)
return

const newTab =
currentTab instanceof TextTab
? TreeTab.from(currentTab)
: TextTab.from(currentTab)
currentTab.tabSystem.replaceCurrent(newTab)
},
})
)

App.toolbar.addCategory(view)
}