forked from DTStack/molecule
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup.tsx
More file actions
154 lines (143 loc) · 5.38 KB
/
group.tsx
File metadata and controls
154 lines (143 loc) · 5.38 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
import { MonacoEditor } from 'mo/components/monaco';
import { Scrollable } from 'mo/components/scrollable';
import { Tabs } from 'mo/components/tabs';
import { IEditorGroup, IEditorOptions } from 'mo/model';
import React, { useRef, useLayoutEffect } from 'react';
import { memo, useEffect } from 'react';
import {
groupClassName,
groupContainerClassName,
groupHeaderClassName,
groupTabsClassName,
} from './base';
import EditorAction from './action';
import EditorBreadcrumb from './breadcrumb';
import { IEditorController } from 'mo/controller/editor';
import { Menu } from 'mo/components/menu';
import { useContextView } from 'mo/components/contextView';
import { getEventPosition } from 'mo/common/dom';
import Scrollbar from 'react-scrollbars-custom';
import { tabItemActiveClassName } from 'mo/components/tabs/tab';
export interface IEditorGroupProps extends IEditorGroup {
currentGroup?: IEditorGroup;
editorOptions?: IEditorOptions;
group?: IEditorGroup;
}
export function EditorGroup(props: IEditorGroupProps & IEditorController) {
const {
id,
data,
tab,
currentGroup,
group,
actions = [],
menu = [],
onMoveTab,
onCloseTab,
onClickContextMenu,
onChangeEditorProps,
onSelectTab,
onClickActions,
editorOptions,
onUpdateEditorIns,
} = props;
const scrollable = useRef<Scrollbar>(null);
const groupTabs = useRef<HTMLDivElement>(null);
const isActiveGroup = id === currentGroup?.id;
const contextView = useContextView();
const handleTabContextMenu = (e: React.MouseEvent, tabItem) => {
const handleOnMenuClick = (e: React.MouseEvent, item) => {
onClickContextMenu?.(e, item, tabItem);
contextView.hide();
};
contextView?.show(getEventPosition(e), () => (
<Menu data={menu} onClick={handleOnMenuClick} />
));
};
useEffect(() => {
return function cleanup() {
contextView?.dispose();
};
});
// scoller into view
useLayoutEffect(() => {
const activeItem = groupTabs.current?.querySelector<HTMLDivElement>(
`.${tabItemActiveClassName}`
);
if (activeItem) {
const width = groupTabs.current?.clientWidth || 0;
const left = activeItem.offsetLeft;
if (left > width) {
scrollable.current?.scrollTo(left, 0);
}
}
}, [currentGroup?.id && currentGroup.tab?.id]);
return (
<div className={groupClassName}>
<div className={groupHeaderClassName}>
<div className={groupTabsClassName} ref={groupTabs}>
<Scrollable
noScrollY
trackStyle={{ height: 3 }}
ref={scrollable}
>
<Tabs
editable={true}
type="card"
data={data}
onMoveTab={onMoveTab}
onSelectTab={onSelectTab}
onContextMenu={handleTabContextMenu}
activeTab={isActiveGroup ? tab?.id : ''}
onCloseTab={onCloseTab}
/>
</Scrollable>
</div>
<EditorAction
isActiveGroup={isActiveGroup}
actions={actions}
menu={menu}
onClickActions={onClickActions}
/>
</div>
<EditorBreadcrumb breadcrumbs={tab?.breadcrumb || []} />
<div className={groupContainerClassName}>
{
// Default we use monaco editor, but also you can customize by renderPanel() function or a react element
tab?.renderPane ? (
typeof tab.renderPane === 'function' ? (
tab.renderPane(
{
...tab.data,
},
tab,
group
)
) : (
tab.renderPane
)
) : (
<MonacoEditor
options={{
...editorOptions,
value: tab?.data?.value,
language: tab?.data?.language,
automaticLayout: true,
}}
path={tab?.id.toString()}
editorInstanceRef={(editorInstance) => {
// This assignment will trigger moleculeCtx update, and subNodes update
onUpdateEditorIns?.(editorInstance, id!);
}}
onChangeEditorProps={(preProps, props) => {
// Listener event for Editor property update
onChangeEditorProps?.(preProps, props);
}}
/>
)
}
</div>
</div>
);
}
export default memo(EditorGroup);