-
-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathTabBar.tsx
More file actions
198 lines (180 loc) · 6.42 KB
/
Copy pathTabBar.tsx
File metadata and controls
198 lines (180 loc) · 6.42 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
import React, { useState, useEffect, useRef } from 'react'
import { WebApplication } from '@/Application/WebApplication'
import { NoteViewController } from '../NoteView/Controller/NoteViewController'
import { FileViewController } from '../NoteView/Controller/FileViewController'
type TabBarProps = {
application: WebApplication
activeControllerIndex: number
splitControllerIndex: number | undefined
focusedPane: 'primary' | 'secondary'
controllers: (NoteViewController | FileViewController)[]
}
export const TabBar: React.FC<TabBarProps> = ({
application,
activeControllerIndex,
splitControllerIndex,
focusedPane,
controllers,
}) => {
const itemControllerGroup = application.itemControllerGroup
const [contextMenu, setContextMenu] = useState<{
x: number
y: number
index: number
} | null>(null)
const menuRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (menuRef.current && !menuRef.current.contains(event.target as Node)) {
setContextMenu(null)
}
}
document.addEventListener('mousedown', handleClickOutside)
return () => document.removeEventListener('mousedown', handleClickOutside)
}, [])
if (controllers.length === 0) {
return null
}
const handleTabClick = (index: number) => {
// If split screen is active, determine which pane we are clicking in
// For simplicity, clicking a tab will focus that index as the primary tab
if (splitControllerIndex !== undefined && index === splitControllerIndex) {
itemControllerGroup.focusedPane = 'secondary'
} else {
itemControllerGroup.activeControllerIndex = index
itemControllerGroup.focusedPane = 'primary'
}
itemControllerGroup.notifyObservers()
}
const handleCloseClick = (e: React.MouseEvent, index: number) => {
e.stopPropagation()
itemControllerGroup.closeTab(index)
}
const handleContextMenu = (e: React.MouseEvent, index: number) => {
e.preventDefault()
setContextMenu({
x: e.clientX,
y: e.clientY,
index,
})
}
const handleCloseTab = () => {
if (contextMenu !== null) {
itemControllerGroup.closeTab(contextMenu.index)
setContextMenu(null)
}
}
const handleCloseOthers = () => {
if (contextMenu !== null) {
const targetController = controllers[contextMenu.index]
for (const controller of [...controllers]) {
if (controller !== targetController) {
itemControllerGroup.closeItemController(controller, { notify: false })
}
}
itemControllerGroup.activeControllerIndex = 0
itemControllerGroup.splitControllerIndex = undefined
itemControllerGroup.focusedPane = 'primary'
itemControllerGroup.notifyObservers()
setContextMenu(null)
}
}
const handleSplitTab = () => {
if (contextMenu !== null) {
itemControllerGroup.splitTab(contextMenu.index)
setContextMenu(null)
}
}
const getTabTitle = (controller: NoteViewController | FileViewController) => {
if (controller instanceof NoteViewController) {
return controller.item?.title || 'Untitled note'
} else {
return (controller.item as any)?.title || (controller.item as any)?.name || 'Untitled file'
}
}
return (
<div className="tab-bar-container">
<div className="tab-bar-tabs">
{controllers.map((controller, index) => {
const isPrimaryActive = activeControllerIndex === index
const isSecondaryActive = splitControllerIndex === index
const isFocused = (focusedPane === 'primary' && isPrimaryActive) ||
(focusedPane === 'secondary' && isSecondaryActive)
const isOpened = isPrimaryActive || isSecondaryActive
let tabClassName = 'tab-item'
if (isOpened) {
tabClassName += ' tab-item-active'
}
if (isFocused) {
tabClassName += ' tab-item-focused'
}
return (
<div
key={controller.runtimeId}
className={tabClassName}
onClick={() => handleTabClick(index)}
onContextMenu={(e) => handleContextMenu(e, index)}
>
<span className="tab-title-text">{getTabTitle(controller)}</span>
{controllers.length > 1 && (
<button
className="tab-close-btn"
onClick={(e) => handleCloseClick(e, index)}
title="Close tab"
>
✕
</button>
)}
</div>
)
})}
<button
className="tab-add-btn"
onClick={() => itemControllerGroup.openNewTemplateTab()}
title="New tab"
>
+
</button>
</div>
<div className="tab-bar-actions">
<button
className={`tab-action-btn ${splitControllerIndex !== undefined ? 'tab-action-btn-active' : ''}`}
onClick={() => itemControllerGroup.toggleSplitScreen()}
title={splitControllerIndex !== undefined ? "Merge Editor Panes" : "Split Editor Panes"}
>
{splitControllerIndex !== undefined ? (
// Merge/Close Split icon
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
<rect x="3" y="3" width="18" height="18" rx="2" />
<line x1="12" y1="3" x2="12" y2="21" />
<path d="M17 12h-3M7 12h3" />
</svg>
) : (
// Split icon
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
<rect x="3" y="3" width="18" height="18" rx="2" />
<line x1="12" y1="3" x2="12" y2="21" />
</svg>
)}
</button>
</div>
{contextMenu && (
<div
ref={menuRef}
className="tab-context-menu"
style={{ top: contextMenu.y, left: contextMenu.x }}
>
<div className="context-menu-item" onClick={handleCloseTab}>
Close Tab
</div>
<div className="context-menu-item" onClick={handleCloseOthers}>
Close Other Tabs
</div>
<div className="context-menu-item" onClick={handleSplitTab}>
Split Screen
</div>
</div>
)}
</div>
)
}