Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve editor actions #234

Merged
merged 4 commits into from
Jul 15, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: rename update actions and create a new update actions function
  • Loading branch information
mortalYoung committed Jul 15, 2021
commit 203472d0551c3a36791fc9238698a68d82cea224
43 changes: 38 additions & 5 deletions src/services/workbench/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
IEditorTab,
EditorEvent,
getEditorInitialActions,
EditorActionsProps,
} from 'mo/model';
import { searchById } from '../helper';
import { editor as monacoEditor, Uri } from 'mo/monaco';
Expand Down Expand Up @@ -71,9 +72,13 @@ export interface IEditorService extends Component<IEditor> {
setActive(groupId: number, tabId: string);
updateGroup(groupId, groupValues: IEditorGroup): void;
/**
* Update actions in group
* Set default actions when create a new group
*/
updateGroupActions(actions: IMenuItemProps[]): void;
setDefaultActions(actions: EditorActionsProps[]): void;
/**
* Update actions in specific group
*/
updateActions(actions: IMenuItemProps[], groupId?: number): void;
updateCurrentGroup(currentValues): void;
/**
* The Instance of Editor
Expand All @@ -85,11 +90,11 @@ export class EditorService
extends Component<IEditor>
implements IEditorService {
protected state: IEditor;
protected groupActions: IMenuItemProps[];
protected defaultActions: EditorActionsProps[];
constructor() {
super();
this.state = container.resolve(EditorModel);
this.groupActions = getEditorInitialActions();
this.defaultActions = getEditorInitialActions();
}

private disposeModel(tabs: IEditorTab | IEditorTab[]) {
Expand All @@ -108,12 +113,40 @@ export class EditorService
this.groupActions = actions;
}

public setDefaultActions(actions: EditorActionsProps[]): void {
this.defaultActions = actions;
}

public setEntry(component: React.ReactNode) {
this.setState({
entry: component,
});
}

public updateActions = (actions: IMenuItemProps[], groupId?: number) => {
const { current, groups: rawGroups } = this.getState();
if (!current) return;

const groups = rawGroups?.concat() || [];
const targetGroup = groups.find(searchById(groupId || current.id));

if (targetGroup) {
const newActions = targetGroup.actions?.concat() || [];
newActions.forEach((action) => {
const target = actions.find((item) => item.id === action.id);
if (target) {
Object.assign(action, target);
}
});
targetGroup.actions = newActions;

this.setState({
current: targetGroup.id === current.id ? targetGroup : current,
groups,
});
}
};

public getTabById<T>(
tabId: string,
group: IEditorGroup
Expand Down Expand Up @@ -378,7 +411,7 @@ export class EditorService
groups.length + 1,
tab,
[tab],
this.groupActions
this.defaultActions
);
groups.push(group);
}
Expand Down