forked from getcursor/cursor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabs.tsx
286 lines (265 loc) · 10.5 KB
/
tabs.tsx
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
import * as React from 'react'
import { useEffect, useRef } from 'react'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faClose } from '@fortawesome/free-solid-svg-icons'
import { useAppDispatch, useAppSelector } from '../app/hooks'
import { getFile, getRelativeFilePath, getTab } from '../features/selectors'
import { setDraggingTab, stopDraggingTab } from '../features/globalSlice'
import { getIconElement } from './filetree'
import * as gs from '../features/globalSlice'
import * as gt from '../features/globalThunks'
import * as ts from '../features/tools/toolSlice'
import * as gsel from '../features/selectors'
import * as tsel from '../features/tools/toolSelectors'
import { faTableColumns, faTableRows } from '@fortawesome/pro-regular-svg-icons'
import { HoverState } from '../features/window/state'
// Actions needed for this component
// selectTab(tid: number) - select a tab
// closeTab(tid: number) - close a tab
// Selectors needed for this component
// getActiveTabs() - get the list of active tabs
// Objects needed for this component
// Tab - {id: number, name: string, path: string, is_active: boolean}
function Tab({ tid }: { tid: number }) {
const dispatch = useAppDispatch()
const tab = useAppSelector(getTab(tid))
const file = useAppSelector(getFile(tab.fileId))
const tabDiv = React.useRef<HTMLDivElement>(null)
let name = tab.isMulti ? 'multifile' : file.name
if (!tab.isMulti && !file.saved) name += ' *'
function revertTabsChildrenEvents() {
if (tabDiv.current) {
tabDiv.current.style.background = ''
const tabs =
tabDiv.current.parentElement?.getElementsByClassName('tab')
for (const tab of tabs || []) {
tab.childNodes.forEach((child) => {
const childElement = child as HTMLElement
childElement.style.pointerEvents = ''
})
}
}
}
return (
<div
draggable="true"
onDragStart={(e) => {
dispatch(setDraggingTab(tid))
}}
onDragEnd={(e) => {
revertTabsChildrenEvents() // revert for current pane
dispatch(stopDraggingTab())
}}
className={`tab ${tab.isActive ? 'tab__is_active' : ''} ${
file.deleted == true ? 'tab__is_deleted' : ''
}`}
onClick={() => {
dispatch(gs.selectTab(tid))
}}
onDragOver={(event) => {
event.preventDefault()
if (tabDiv.current) {
tabDiv.current.style.background = 'rgba(255, 255, 255, 0.3)'
tabDiv.current.childNodes.forEach((child) => {
const childElement = child as HTMLElement
childElement.style.pointerEvents = 'none' // we don't want onDragLeave event for tab children while reordering
})
}
}}
onDragLeave={(event) => {
event.preventDefault()
if (tabDiv.current) {
tabDiv.current.style.background = ''
}
}}
onDrop={(event) => {
event.preventDefault()
revertTabsChildrenEvents() // revert for new pane
}}
ref={tabDiv}
onContextMenu={() => dispatch(gs.rightClickTab(tid))}
>
<div
onMouseDown={(e) => {
if (e.button == 1) {
// middle click
e.stopPropagation()
dispatch(gt.closeTab(tid))
}
}}
>
<div className="tab__icon">{getIconElement(file.name)}</div>
<div className="tab__name">{name}</div>
<div
className="tab__close"
onClick={(e) => {
e.stopPropagation()
dispatch(gt.closeTab(tid))
}}
>
<FontAwesomeIcon icon={faClose} />
</div>
</div>
</div>
)
}
function TabPath({ tid }: { tid: number }) {
const tab = useAppSelector(getTab(tid))
const filePath = useAppSelector(getRelativeFilePath(tab.fileId))
const splitPaths = filePath.split(connector.PLATFORM_DELIMITER)
const delimeter = '〉'
return (
<>
{!tab.isMulti && (
<div className="tab__path">
{splitPaths.map((path, i) => (
<div key={i} className="whitespace-nowrap">
<span>{path}</span>
{i < splitPaths.length - 1 ? (
<span
className="ml-4 mr-4"
style={{ width: '5px' }}
>
{delimeter}
</span>
) : null}
</div>
))}
</div>
)}
</>
)
}
function TabRemainder({ children }: { children: React.ReactNode }) {
const containerDiv = useRef<HTMLDivElement>(null)
function revertTabsChildrenEvents() {
if (containerDiv.current) {
containerDiv.current.style.background = ''
const tabs =
containerDiv.current.parentElement?.getElementsByClassName(
'tab'
)
for (const tab of tabs || []) {
tab.childNodes.forEach((child) => {
const childElement = child as HTMLElement
childElement.style.pointerEvents = ''
})
}
}
}
return (
<div
className="w-full"
ref={containerDiv}
onDragOver={(event) => {
event.preventDefault()
if (containerDiv.current) {
containerDiv.current.style.background =
'rgba(255, 255, 255, 0.3)'
containerDiv.current.childNodes.forEach((child) => {
const childElement = child as HTMLElement
childElement.style.pointerEvents = 'none' // we don't want onDragLeave event for tab children while reordering
})
}
}}
onDragLeave={(event) => {
event.preventDefault()
if (containerDiv.current) {
containerDiv.current.style.background = ''
}
}}
onDrop={(event) => {
event.preventDefault()
revertTabsChildrenEvents() // revert for new pane
}}
>
{children}
</div>
)
}
export function TabBar({
tabIds,
activeTabId = null,
}: {
tabIds: number[]
activeTabId?: number | null
}) {
// Add event listener to translate vertical scroll to horizontal scroll
const dispatch = useAppDispatch()
const tabBarRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const tabBar = tabBarRef.current
if (tabBar) {
tabBar.addEventListener('wheel', (e) => {
if (e.deltaY !== 0) {
e.preventDefault()
tabBar.scrollLeft += e.deltaY
}
})
}
}, [tabBarRef])
const currentPane = useAppSelector(gsel.getCurrentPane)
const currentTab = useAppSelector(gsel.getCurrentTab(currentPane!))
const leftSideExpanded = useAppSelector(tsel.getLeftSideExpanded)
const handleExpandLeftSideClick = () => {
dispatch(ts.expandLeftSide())
}
return (
<div className="window__tabbarcontainer">
<div className="tabbar" ref={tabBarRef}>
<div className="w-full flex" ref={tabBarRef}>
{!leftSideExpanded && (
<div className=" h-full flex items-center justify-center">
<button
className={`leftside__tab opacity-75`}
onClick={() => handleExpandLeftSideClick()}
>
<div>
<i className="fas fa-chevrons-right"></i>
</div>
</button>
</div>
)}
{tabIds.map((tabId) => (
<Tab key={tabId} tid={tabId} />
))}
<TabRemainder>
{currentPane != null && currentTab != null && (
<div className="tabbar__hoverbuttons">
<div
className="tabbar__hoverbutton"
onClick={(e) => {
e.stopPropagation()
dispatch(
gs.splitPaneAndOpenFile({
paneId: currentPane,
hoverState: HoverState.Right,
})
)
}}
>
<FontAwesomeIcon icon={faTableColumns} />
</div>
<div
className="tabbar__hoverbutton"
onClick={(e) => {
e.stopPropagation()
dispatch(
gs.splitPaneAndOpenFile({
paneId: currentPane,
hoverState: HoverState.Bottom,
})
)
}}
>
<FontAwesomeIcon icon={faTableRows} />
</div>
</div>
)}
</TabRemainder>
</div>
</div>
{activeTabId != null ? <TabPath tid={activeTabId} /> : null}
</div>
)
}