Skip to content

Commit

Permalink
feat: add model layer and extract view interfaces to model file
Browse files Browse the repository at this point in the history
  • Loading branch information
wewoor committed Oct 31, 2020
1 parent 4fc75cf commit 85eed7e
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 30 deletions.
53 changes: 53 additions & 0 deletions src/model/activityBar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-disable no-invalid-this */
import { observable } from 'mo/common/observable';
import { ActivityBarEvent, EventService } from 'mo/services';
import { container, inject, injectable } from 'tsyringe';


export interface IActivityBarItem {
id?: string;
name?: string;
data?: any;
iconName?: string;
checked?: boolean;
type?: 'normal' | 'global';
render?: () => React.ReactNode | JSX.Element;
onClick?: (event: React.MouseEvent, item: IActivityBarItem) => void;
}

export interface IActivityBar {
data: IActivityBarItem[];
selected?: string;
onSelect?: (key: string, item?: IActivityBarItem) => void;
onClick?: (event: React.MouseEvent, item: IActivityBarItem) => void;
render?: () => React.ReactNode;
}

@observable()
@injectable()
export class ActivityBarModel implements IActivityBar {
public data: IActivityBarItem[];
public selected: string;

constructor(
@inject('ActivityBarData') data: IActivityBarItem[] = [],
@inject('ActivityBarSelected') selected: string = '',
) {
this.data = data;
this.selected = selected;
}

public render!: () => React.ReactNode;

public readonly onSelect = (key: string, item?: IActivityBarItem | undefined) => {
this.selected = key;
EventService.emit(ActivityBarEvent.Selected, key, item);
}

public readonly onClick = (event: React.MouseEvent, item: IActivityBarItem) => {
EventService.emit(ActivityBarEvent.OnClick, event, item);
}
}

container.register('ActivityBarData', { useValue: [] });
container.register('ActivityBarSelected', { useValue: '' });
71 changes: 71 additions & 0 deletions src/model/editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* eslint-disable no-invalid-this */
import { observable } from 'mo/common/observable';
import { ITab } from 'mo/components/tabs';
import { container, inject, injectable } from 'tsyringe';


export interface IEditor {
current: IEditorGroup | undefined;
groups: IEditorGroup [];
closeAll?: () => void;
onClose?: () => void;
render?:() => React.ReactNode;
}

export interface IEditorGroup<E = any> {
id: number;
activeTab: ITab;
tabs: ITab[];
breadcrumb: any[];
actions: any[];
menu: any[];
editorInstance?: E | null;
}

export class EditorGroupModel implements IEditorGroup {
id: number;
activeTab: ITab;
tabs: ITab[];
breadcrumb: any[];
actions: any[];
menu: any[];
editorInstance: any;

constructor(
id: number,
activeTab: ITab,
tabs: ITab[],
breadcrumb: any[] = [],
actions: any[] = [],
menu: any[] = [],
editorInstance?: any,
) {
this.id = id;
this.tabs = tabs;
this.menu = menu;
this.actions = actions;
this.activeTab = activeTab;
this.breadcrumb = breadcrumb;
this.editorInstance = editorInstance;
}
}

@observable()
@injectable()
export class EditorModel implements IEditor {
public current: IEditorGroup | undefined;
public groups!: IEditorGroup[];

constructor(
@inject('CurrentEditorGroup') current?: IEditorGroup,
@inject('EditorGroup') groups: IEditorGroup[] = [],
) {
this.current = current;
this.groups = groups;
}

public render!: () => React.ReactNode;
}

container.register('CurrentEditorGroup', { useValue: '' });
container.register('EditorGroup', { useValue: [] });
30 changes: 0 additions & 30 deletions src/model/editorGroup.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './activityBar';
export * from './editor';
export * from './menuBar';
export * from './sidebar';
export * from './statusBar';
35 changes: 35 additions & 0 deletions src/model/menuBar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { observable } from 'mo/common/observable';
import { container, injectable, inject } from 'tsyringe';

export interface IMenuBarItem {
id?: string;
name?: string;
data?: any;
iconName?: string;
render?: () => React.ReactNode | JSX.Element;
onClick?:(e: React.MouseEvent, option: IMenuBarItem) => any;
}

export interface IMenuBar {
data: IMenuBarItem[];
onClick:(event: React.MouseEvent<any, any>, item: IMenuBarItem) => void;
render?: () => React.ReactNode | JSX.Element;
}

@observable()
@injectable()
export class MenuBarModel implements IMenuBar {
public data: IMenuBarItem[];

constructor(@inject('MenuBarData') data: IMenuBarItem[] = []) {
this.data = data;
}

public render!: () => React.ReactNode;

public readonly onClick = (event: React.MouseEvent, item: IMenuBarItem) => {
console.log('onClick:', event);
}
}

container.register('MenuBarData', { useValue: [] });
34 changes: 34 additions & 0 deletions src/model/sidebar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { observable } from 'mo/common/observable';
import { container, inject, injectable } from 'tsyringe';

export interface ISidebarPane {
id?: string;
name?: string;
render?: () => React.ReactElement | undefined;
}

export interface ISidebar {
selected: string;
panes: ISidebarPane[];
render?: () => React.ReactNode;
}

@observable()
@injectable()
export class SidebarModel implements ISidebar {
public selected: string;
public panes: ISidebarPane[];

constructor(
@inject('SidebarPane') panes: ISidebarPane[] = [],
@inject('Selected') selected: string = '',
) {
this.panes = panes;
this.selected = selected;
}

public render!: () => React.ReactNode;
}

container.register('SidebarPane', { useValue: [] });
container.register('Selected', { useValue: '' });
29 changes: 29 additions & 0 deletions src/model/statusBar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { observable } from 'mo/common/observable';
import { container, inject, injectable } from 'tsyringe';

export interface IStatusBarItem {
}

export interface IStatusBar {
data: IStatusBarItem[];
onClick:(event: React.MouseEvent<any, any>) => void;
render?: () => React.ReactNode | JSX.Element;
}

@observable()
@injectable()
export class StatusBarModel implements IStatusBar {
public data: IStatusBarItem[] = [];

constructor(@inject('StatusBarData') data: IStatusBarItem[] = []) {
this.data = data;
}

public render!: () => React.ReactNode;

public onClick = (event: React.MouseEvent) => {
console.log('onClick:', event);
}
}

container.register('StatusBarData', { useValue: [] });

0 comments on commit 85eed7e

Please sign in to comment.