Skip to content

Commit 5de680f

Browse files
committed
feat: activityBar selected
1 parent 606f73c commit 5de680f

File tree

7 files changed

+57
-35
lines changed

7 files changed

+57
-35
lines changed

src/extensions/activityBar/index.tsx

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
IExtensionService, activityBarService,
3-
ActivityBarEvent, IActivityBarItem,
3+
IActivityBarItem,
44
} from 'mo';
55
import { IExtension } from 'mo/model/extension';
66

@@ -10,26 +10,27 @@ function initActivityBar(extensionCtx: IExtensionService) {
1010
name: 'Settings',
1111
iconName: 'codicon-settings-gear',
1212
type: 'global',
13-
onClick: function(e, options) {
14-
15-
},
1613
};
1714

1815
const globalUserAccount: IActivityBarItem = {
1916
id: 'global-Account',
2017
name: 'Account',
2118
iconName: 'codicon-account',
2219
type: 'global',
23-
onClick: function(e, options) {
24-
25-
},
2620
};
2721

2822
activityBarService.push(globalUserAccount);
2923
activityBarService.push(globalSettings);
3024

31-
activityBarService.subscribe(ActivityBarEvent.OnClick, (data) => {
32-
console.log('Explore activityBar subscribe onClick:', data);
25+
activityBarService.onClick((data) => {
26+
const target = data[0].target;
27+
// activityBarService.updateState({ selected: 'search' });
28+
console.log('activityBar onClick:', data, target);
29+
});
30+
31+
activityBarService.onSelect((data) => {
32+
const target = data[0].target;
33+
console.log('activityBar onSelect:', data, target);
3334
});
3435
}
3536

src/extensions/explore/explore.tsx

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import Collapse, { Panel } from 'mo/components/collapse';
33
import { prefixClaName } from 'mo/common/className';
4-
import { activityBarService, editorService, ActivityBarEvent } from 'mo/index';
4+
import { activityBarService, editorService } from 'mo';
55

66
interface IExplorerProps {
77
}
@@ -30,11 +30,6 @@ export const Explorer: React.FunctionComponent<IExplorerProps> = (IExplorerProps
3030
const OpenCommand = function() {
3131
// MonacoEditor.editor.getModel().
3232
};
33-
34-
activityBarService.subscribe(ActivityBarEvent.OnClick, (data) => {
35-
console.log('Explore activityBar subscribe onClick:', data);
36-
});
37-
3833
return (
3934
<div className={prefixClaName('explorer', 'sidebar')}>
4035
<Collapse className="dee">

src/extensions/explore/index.tsx

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ import { ExtensionService } from 'mo/services/extensionService';
66
import { IExtension } from 'mo/model/extension';
77

88
function initActivityBar(extensionCtx: ExtensionService) {
9+
const activeId = 'active-explorer';
10+
const state = activityBarService.getState();
911
const folderFeat: IActivityBarItem = {
10-
id: 'active-explorer',
12+
id: activeId,
1113
name: 'Explore',
1214
iconName: 'codicon-files',
13-
onClick: function(e, options) {
14-
15-
},
1615
};
17-
activityBarService.push(folderFeat);
16+
// activityBarService.push(folderFeat);
17+
// state.data?.push(folderFeat);
18+
// state.selected = activeId;
19+
activityBarService.updateState({
20+
selected: activeId, data: [...state.data, folderFeat],
21+
});
1822

1923
activityBarService.subscribe(ActivityBarEvent.OnClick, (data) => {
2024
console.log('Explore activityBar subscribe onClick:', data);

src/model/workbench/activityBar.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface IActivityBarItem {
3232
}
3333

3434
export interface IActivityBar {
35-
data: IActivityBarItem[];
35+
data?: IActivityBarItem[];
3636
selected?: string;
3737
onSelect?: (key: string, item?: IActivityBarItem) => void;
3838
onClick?: (event: React.MouseEvent, item: IActivityBarItem) => void;
@@ -56,7 +56,6 @@ export class ActivityBarModel implements IActivityBar {
5656
public render!: () => React.ReactNode;
5757

5858
public readonly onSelect = (key: string, item?: IActivityBarItem | undefined) => {
59-
this.selected = key;
6059
EventBus.emit(ActivityBarEvent.Selected, key, item);
6160
}
6261

src/react/component.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { GlobalEvent } from 'mo/common/event';
2+
import logger from 'mo/common/logger';
23

34
export abstract class Component<S> extends GlobalEvent {
45
protected abstract state: S;
56

67
public updateState(nextState: S) {
78
Object.assign(this.state, nextState);
9+
logger.info(`Component.updateState()`, nextState, this.state);
810
}
911

1012
public getState(): S {

src/services/workbench/activityBarService.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ export interface IActivityBarService extends Component<IActivityBar> {
66
reset(): void;
77
push(data: IActivityBarItem): void;
88
remove(index: number) : void;
9+
/**
10+
* Add click event listener
11+
* @param callback
12+
*/
13+
onClick(callback: Function);
14+
onSelect(callback: Function);
915
}
1016

1117
@singleton()
@@ -26,7 +32,7 @@ export class ActivityBarService extends Component<IActivityBar> implements IActi
2632

2733
public push(data: IActivityBarItem) {
2834
const original = this.state.data;
29-
original.push(data);
35+
original?.push(data);
3036
console.log('ac push:', original);
3137
}
3238

@@ -38,14 +44,24 @@ export class ActivityBarService extends Component<IActivityBar> implements IActi
3844
}
3945

4046
public setRenderer(renderer: () => React.ReactNode) {
41-
// this.updateState(Object.assign(this.state, {
42-
// render: renderer,
43-
// }));
4447
// eslint-disable-next-line react/no-direct-mutation-state
4548
this.state.render = renderer;
4649
}
4750

4851
public onClick(callback: Function) {
4952
this.subscribe(ActivityBarEvent.OnClick, callback);
5053
}
54+
55+
public onSelect(callback: Function) {
56+
this.subscribe(ActivityBarEvent.Selected, (args) => {
57+
const key = args[0];
58+
const item: IActivityBarItem = args[1];
59+
if (item.type !== 'global') {
60+
this.updateState({
61+
selected: key,
62+
});
63+
}
64+
if (callback) callback(key, item);
65+
});
66+
}
5167
}

src/workbench/activityBar/activityBar.tsx

+14-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { IActivityBar, IActivityBarItem } from 'mo/model/workbench/activityBar';
77
import ActivityBarItem from './activityBarItem';
88

99
export function ActivityBar(props: IActivityBar) {
10-
const { data = [], render, selected, onClick } = props;
10+
const { data = [], render, selected, onClick, onSelect } = props;
1111

1212
if (render) {
1313
return (
@@ -17,20 +17,25 @@ export function ActivityBar(props: IActivityBar) {
1717
);
1818
}
1919

20+
const onClickBar = (e: React.MouseEvent, item: IActivityBarItem) => {
21+
console.log('ActivityBar onClick:', e);
22+
if (onClick) onClick(e, item);
23+
if (onSelect) {
24+
onSelect(item.id || '', item);
25+
}
26+
};
27+
2028
const normalBarItems = data?.filter((item: IActivityBarItem) => !item.type || item.type === 'normal') || [];
2129
const globalBarItems = data?.filter((item: IActivityBarItem) => item.type && item.type === 'global') || [];
2230

23-
const renderItems = (item: IActivityBarItem, index: number) => (
24-
<ActivityBarItem key={item.id} {...item} data-index={index} checked={selected === item.id}/>
25-
);
26-
27-
const click = (e: React.MouseEvent) => {
28-
console.log('ActivityBar onClick:', e);
29-
if (onClick) onClick(e, {} as any );
31+
const renderItems = (item: IActivityBarItem, index: number) => {
32+
return (
33+
<ActivityBarItem key={item.id} {...item} onClick={onClickBar} data-index={index} checked={selected === item.id}/>
34+
);
3035
};
3136

3237
return (
33-
<div className={prefixClaName(ID_ACTIVITY_BAR)} id={ID_ACTIVITY_BAR} onClick={click}>
38+
<div className={prefixClaName(ID_ACTIVITY_BAR)} id={ID_ACTIVITY_BAR}>
3439
<div className={prefixClaName('container', ID_ACTIVITY_BAR)}>
3540
<ul className={'normal-items'}>
3641
{normalBarItems.map(renderItems)}

0 commit comments

Comments
 (0)