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

refactor: refactor core package to DI pattern and remove Context #45

Merged
merged 6 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
refactor: remove dependency on context for more modules
  • Loading branch information
Wenzhao Hu committed Aug 10, 2023
commit 2aa046957cf31a04a601caf4aa71209684979832
5 changes: 2 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ module.exports = {
'guard-for-in': 'off',
'no-prototype-builtins': 'off',
'no-lonely-if': 'off',
'prefer-const': 'off',
radix: 'off',
'no-nested-ternary': 'off',
'no-new': 'off',
Expand Down Expand Up @@ -109,7 +108,7 @@ module.exports = {
'grouped-accessor-pairs': 'off',
'@typescript-eslint/member-ordering': 'error',
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/no-unsafe-return': 'error'
'@typescript-eslint/no-unsafe-return': 'error',
},
// https://www.npmjs.com/package/eslint-import-resolver-typescript
settings: {
Expand All @@ -126,4 +125,4 @@ module.exports = {
},
},
},
};
};
37 changes: 28 additions & 9 deletions packages/base-sheets/src/Controller/Selection/SelectionManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { IMouseEvent, IPointerEvent, Rect, ScrollTimer, Spreadsheet, SpreadsheetColumnTitle, SpreadsheetRowTitle } from '@univerjs/base-render';
import {
ActionOperation, Command, DEFAULT_CELL, DEFAULT_SELECTION, Direction, ICellInfo, ICurrentUniverService, IGridRange, IRangeCellData, IRangeData, ISelection, ISelectionData, makeCellToSelection, Nullable,
Observer, ObserverManager, Range, RangeList, Worksheet
ActionOperation,
Command,
CommandManager,
DEFAULT_CELL,
DEFAULT_SELECTION,
Direction,
ICellInfo,
ICurrentUniverService,
IGridRange,
IRangeCellData,
IRangeData,
ISelection,
ISelectionData,
makeCellToSelection,
Nullable,
Observer,
ObserverManager,
Range,
RangeList,
Worksheet,
} from '@univerjs/core';
import { Inject, Injector } from '@wendellhu/redi';

Expand Down Expand Up @@ -53,6 +71,7 @@ export class SelectionManager {
private readonly _sheetView: SheetView,
private readonly _config: ISheetPluginConfig,
@ICurrentUniverService private readonly _currentUniverService: ICurrentUniverService,
@Inject(CommandManager) private readonly _commandManager: CommandManager,
@Inject(Injector) private readonly _injector: Injector,
@Inject(ObserverManager) private readonly _observerManager: ObserverManager,
/** @deprecated this should be divided into smaller pieces */
Expand Down Expand Up @@ -283,15 +302,16 @@ export class SelectionManager {
models = this._selectionControls.map((control) => control.model.getValue());
}

const workbook = this._worksheet.getContext().getWorkBook();
const commandManager = workbook.getCommandManager();
const workbook = this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook();

let action: ISetSelectionValueActionData = {
sheetId: this._worksheet.getSheetId(),
actionName: SetSelectionValueAction.NAME,
selections: models,
injector: this._injector,
};

// TODO@wzhudev: this design is bad, leaking implementation details to its users. Will redesign command system and refactor this.
action = ActionOperation.make<ISetSelectionValueActionData>(action).removeUndo().getAction();

const command = new Command(
Expand All @@ -300,7 +320,7 @@ export class SelectionManager {
},
action
);
commandManager.invoke(command);
this._commandManager.invoke(command);
}

/**
Expand All @@ -322,8 +342,7 @@ export class SelectionManager {
}
});

const workbook = this._worksheet.getContext().getWorkBook();
const commandManager = workbook.getCommandManager();
const workbook = this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook();

let action: ISetSelectionValueActionData = {
sheetId: this._worksheet.getSheetId(),
Expand All @@ -339,7 +358,7 @@ export class SelectionManager {
},
action
);
commandManager.invoke(command);
this._commandManager.invoke(command);
}

setModels(selections: ISelectionModelValue[]) {
Expand Down Expand Up @@ -1045,4 +1064,4 @@ export class SelectionManager {
private getWorksheetId(): string {
return this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook().getActiveSheet().getSheetId();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SheetActionBase, ActionObservers, ActionType, ISheetActionData, ICellIn

import { ACTION_NAMES } from '../../Basics/Enum/ACTION_NAMES';
import { SetSelectionValue } from '../Apply/SetSelectionValue';
import { ISelectionManager } from '../../Services/tokens';

export interface ISelectionModelValue {
selection: ISelection;
Expand Down Expand Up @@ -33,10 +34,10 @@ export class SetSelectionValueAction extends SheetActionBase<ISetSelectionValueA
}

do(): ISelectionModelValue[] {
const { selections } = this._doActionData;
const { selections, injector } = this._doActionData;
const worksheet = this.getWorkSheet();

const result = SetSelectionValue(worksheet, selections);
const result = SetSelectionValue(worksheet, selections, injector!.get(ISelectionManager));

this._observers.notifyObservers({
type: ActionType.REDO,
Expand All @@ -59,14 +60,14 @@ export class SetSelectionValueAction extends SheetActionBase<ISetSelectionValueA
}

undo(): void {
const { selections, sheetId } = this._oldActionData;
const { selections, sheetId, injector } = this._oldActionData;
const worksheet = this.getWorkSheet();

// update current data
this._doActionData = {
actionName: ACTION_NAMES.SET_SELECTION_VALUE_ACTION,
sheetId,
selections: SetSelectionValue(worksheet, selections),
selections: SetSelectionValue(worksheet, selections, injector!.get(ISelectionManager)),
};

this._observers.notifyObservers({
Expand Down
14 changes: 5 additions & 9 deletions packages/base-sheets/src/Model/Apply/SetSelectionValue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PLUGIN_NAMES, Worksheet } from '@univerjs/core';
import { SheetPlugin } from '../..';
import { Worksheet } from '@univerjs/core';
import { SelectionManager } from '../..';
import { ISelectionModelValue } from '../Action/SetSelectionValueAction';

/**
Expand All @@ -12,12 +12,8 @@ import { ISelectionModelValue } from '../Action/SetSelectionValueAction';
*
* @internal
*/
export function SetSelectionValue(worksheet: Worksheet, selections: ISelectionModelValue[]): ISelectionModelValue[] {
const selectionManager = worksheet.getContext().getPluginManager().getRequirePluginByName<SheetPlugin>(PLUGIN_NAMES.SPREADSHEET).getSelectionManager();

const result = selectionManager.getCurrentModelsValue();

selectionManager.setModels(selections);

export function SetSelectionValue(_worksheet: Worksheet, selections: ISelectionModelValue[], _selectionManager: SelectionManager): ISelectionModelValue[] {
const result = _selectionManager.getCurrentModelsValue();
_selectionManager.setModels(selections);
return result;
}
2 changes: 2 additions & 0 deletions packages/core/src/Command/SheetActionBase.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Injector } from '@wendellhu/redi';
import { Workbook, Worksheet } from '../Sheets/Domain';
import { ActionBase, IActionData, ActionObservers, CommandUnit } from './index';

Expand All @@ -7,6 +8,7 @@ import { ActionBase, IActionData, ActionObservers, CommandUnit } from './index';
export interface ISheetActionData extends IActionData {
sheetId: string;
rangeRef?: string;
injector?: Injector;
}

/**
Expand Down
Loading