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
Next Next commit
WIP: remove context in worksheet and workbook
  • Loading branch information
Wenzhao Hu committed Aug 10, 2023
commit 8328d058ffcab17a99e1801a268f0e6afce0b75f
4 changes: 3 additions & 1 deletion packages/core/src/Basics/ContextBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export abstract class ContextBase {
onUniver(univer: Univer): void {
const globalContext = univer.getGlobalContext();
this._univer = univer;

// TODO: huwenzhao: 这里的意思其实就是业务 Context 里面要复用上层的 undoManager 和 CommandManager
this._undoManager = globalContext.getUndoManager();
this._commandManager = globalContext.getCommandManager();
}
Expand Down Expand Up @@ -77,4 +79,4 @@ export abstract class ContextBase {
}

protected abstract _setObserver(): void;
}
}
62 changes: 14 additions & 48 deletions packages/core/src/Sheets/Domain/Workbook.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import {
DEFAULT_RANGE_ARRAY,
DEFAULT_WORKBOOK,
DEFAULT_WORKSHEET,
} from '../../Types/Const';
import { DEFAULT_RANGE_ARRAY, DEFAULT_WORKBOOK, DEFAULT_WORKSHEET } from '../../Types/Const';

import { BooleanNumber } from '../../Types/Enum';
import { SheetContext, Univer } from '../../Basics';

import {
InsertSheetAction,
ISetSheetOrderActionData,
RemoveSheetAction,
SetSheetOrderAction,
IInsertSheetActionData,
IRemoveSheetActionData,
} from '../Action';
import { InsertSheetAction, ISetSheetOrderActionData, RemoveSheetAction, SetSheetOrderAction, IInsertSheetActionData, IRemoveSheetActionData } from '../Action';

import {
IColumnStartEndData,
Expand Down Expand Up @@ -92,16 +81,14 @@ export class Workbook {
static rangeDataToRangeStringData(rangeData: IRangeData) {
const { startRow, endRow, startColumn, endColumn } = rangeData;

return `${Tools.chatAtABC(startColumn) + (startRow + 1)}:${Tools.chatAtABC(
endColumn
)}${endRow + 1}`;
return `${Tools.chatAtABC(startColumn) + (startRow + 1)}:${Tools.chatAtABC(endColumn)}${endRow + 1}`;
}

static isIRangeType(range: IRangeType | IRangeType[]): Boolean {
return typeof range === 'string' || 'startRow' in range || 'row' in range;
}

onUniver(univer: Univer) {
onUniver() {
this._getDefaultWorkSheet();
}

Expand Down Expand Up @@ -136,9 +123,7 @@ export class Workbook {
}
}
}
const worksheet = this._worksheets.get(
sheetOrder[sheetOrder.length - 1]
);
const worksheet = this._worksheets.get(sheetOrder[sheetOrder.length - 1]);
if (worksheet) {
worksheet.activate();
}
Expand Down Expand Up @@ -448,10 +433,7 @@ export class Workbook {
const { _context } = this;
const name = argument[0];
const conf = { ...DEFAULT_WORKSHEET, name };
const worksheet = new Worksheet(
_context,
conf as Partial<IWorksheetConfig>
);
const worksheet = new Worksheet(_context, conf as Partial<IWorksheetConfig>);
this.insertSheet(worksheet);
return worksheet;
}
Expand All @@ -461,10 +443,7 @@ export class Workbook {
const rowCount = argument[1];
const columnCount = argument[2];
const conf = { ...DEFAULT_WORKSHEET, name, rowCount, columnCount };
const worksheet = new Worksheet(
_context,
conf as Partial<IWorksheetConfig>
);
const worksheet = new Worksheet(_context, conf as Partial<IWorksheetConfig>);
this.insertSheet(worksheet);
return worksheet;
}
Expand Down Expand Up @@ -599,9 +578,7 @@ export class Workbook {

getSheets(): Worksheet[] {
const { sheetOrder } = this._config;
return sheetOrder.map((sheetId) =>
this._worksheets.get(sheetId)
) as Worksheet[];
return sheetOrder.map((sheetId) => this._worksheets.get(sheetId)) as Worksheet[];
}

getSheetIndex(sheet: Worksheet): number {
Expand All @@ -622,12 +599,8 @@ export class Workbook {

if (sheetOrder.length > 1 && sheet != null) {
const index = this.getSheetIndex(sheet);
const before = this.getContext().getContextObserver(
'onBeforeRemoveSheetObservable'
);
const aftert = this.getContext().getContextObserver(
'onAfterRemoveSheetObservable'
);
const before = this.getContext().getContextObserver('onBeforeRemoveSheetObservable');
const aftert = this.getContext().getContextObserver('onAfterRemoveSheetObservable');
before.notifyObservers({
index,
});
Expand Down Expand Up @@ -865,10 +838,7 @@ export class Workbook {
sheetTxt = val[0];
rangeTxt = val[1];
sheetTxt = sheetTxt.replace(/\\'/g, "'").replace(/''/g, "'");
if (
sheetTxt.substring(0, 1) === "'" &&
sheetTxt.substring(sheetTxt.length - 1, 1) === "'"
) {
if (sheetTxt.substring(0, 1) === "'" && sheetTxt.substring(sheetTxt.length - 1, 1) === "'") {
sheetTxt = sheetTxt.substring(1, sheetTxt.length - 1);
}
} else {
Expand Down Expand Up @@ -896,12 +866,8 @@ export class Workbook {

const row: IRowStartEndData = [0, 0];
const col: IColumnStartEndData = [0, 0];
const maxRow =
this.getSheetBySheetName(sheetTxt)?.getMaxRows() ||
this.getActiveSheet()?.getMaxRows();
const maxCol =
this.getSheetBySheetName(sheetTxt)?.getMaxColumns() ||
this.getActiveSheet()?.getMaxColumns();
const maxRow = this.getSheetBySheetName(sheetTxt)?.getMaxRows() || this.getActiveSheet()?.getMaxRows();
const maxCol = this.getSheetBySheetName(sheetTxt)?.getMaxColumns() || this.getActiveSheet()?.getMaxColumns();
row[0] = parseInt(rangeTxt[0].replace(/[^0-9]/g, ''), 10) - 1;
row[1] = parseInt(rangeTxt[1].replace(/[^0-9]/g, ''), 10) - 1;

Expand Down Expand Up @@ -939,4 +905,4 @@ export class Workbook {
};
return item;
}
}
}
Loading