Skip to content

Commit fff52cd

Browse files
author
Loïc Mangeonjean
committed
fix: improve VSCode part handling
1 parent a5b50e8 commit fff52cd

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/vscodeParts.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Fragment, ReactNode, useEffect, useRef, useState } from 'react'
22
import * as monaco from 'monaco-editor'
3-
import { renderSidebarPart, renderActivitybarPar, renderEditorPart, renderPanelPart, renderStatusBarPart, registerCustomView, ViewContainerLocation, CustomViewOption } from '@codingame/monaco-vscode-views-service-override/views'
3+
import { attachPart, Parts, onPartVisibilityChange, registerCustomView, ViewContainerLocation, CustomViewOption, isPartVisibile } from '@codingame/monaco-vscode-views-service-override/views'
44
import { createPortal } from 'react-dom'
55
import { initializePromise } from '@codingame/monaco-editor-wrapper'
66
import { DisposableStore } from 'vscode/monaco'
@@ -12,19 +12,19 @@ interface CustomView extends Omit<CustomViewOption, 'renderBody' | 'location' |
1212
})[]
1313
}
1414

15-
interface VscodePartRendererProps {
15+
export interface VscodePartRendererProps {
1616
views?: CustomView[]
1717
className?: string
1818
style?: React.CSSProperties
1919
}
2020

21-
function createPart (location: ViewContainerLocation | null, renderPart: (container: HTMLElement) => monaco.IDisposable) {
21+
function createPart (part: Parts, location: ViewContainerLocation | null) {
2222
const element = document.createElement('div')
2323
element.style.flex = '1'
2424
element.style.minWidth = '0'
2525

2626
initializePromise.then(() => {
27-
renderPart(element)
27+
attachPart(part, element)
2828
}, console.error)
2929

3030
let counter = 0
@@ -46,6 +46,15 @@ function createPart (location: ViewContainerLocation | null, renderPart: (contai
4646

4747
const [portalComponents, setPortalComponents] = useState({} as Record<string, HTMLElement>)
4848

49+
const [visible, setVisible] = useState(isPartVisibile(part))
50+
51+
useEffect(() => {
52+
const disposable = onPartVisibilityChange(part, setVisible)
53+
return () => {
54+
disposable.dispose()
55+
}
56+
}, [])
57+
4958
useEffect(() => {
5059
if (location == null) {
5160
return
@@ -126,7 +135,7 @@ function createPart (location: ViewContainerLocation | null, renderPart: (contai
126135
})}
127136
<div
128137
ref={ref} className={className} style={{
129-
display: 'flex',
138+
display: visible ? 'flex' : 'none',
130139
alignItems: 'stretch',
131140
justifyContent: 'stretch',
132141
...style
@@ -137,10 +146,11 @@ function createPart (location: ViewContainerLocation | null, renderPart: (contai
137146
}
138147
}
139148

140-
export const SidebarPart = createPart(ViewContainerLocation.Sidebar, renderSidebarPart)
141-
export const ActivitybarPart = createPart(null, renderActivitybarPar)
142-
export const EditorPart = createPart(null, renderEditorPart)
143-
export const StatusBarPart = createPart(null, renderStatusBarPart)
144-
export const PanelPart = createPart(ViewContainerLocation.Panel, renderPanelPart)
149+
export const SidebarPart = createPart(Parts.SIDEBAR_PART, ViewContainerLocation.Sidebar)
150+
export const ActivitybarPart = createPart(Parts.ACTIVITYBAR_PART, null)
151+
export const EditorPart = createPart(Parts.EDITOR_PART, null)
152+
export const StatusBarPart = createPart(Parts.STATUSBAR_PART, null)
153+
export const PanelPart = createPart(Parts.PANEL_PART, ViewContainerLocation.Panel)
154+
export const AuxiliaryPart = createPart(Parts.AUXILIARYBAR_PART, ViewContainerLocation.AuxiliaryBar)
145155

146156
export type { CustomView }

0 commit comments

Comments
 (0)