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(sheet): maybe get null when get active sheet #2512

Merged
merged 6 commits into from
Jun 15, 2024
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
test(sheet): active sheet test
  • Loading branch information
Dushusir committed Jun 15, 2024
commit bd21733eb5bee69dbe50af8716c8414ead257096
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class SheetsFilterRenderController extends RxDisposable implements IRende
if (!skeletonParams) return of(null);

const { unit: workbook, unitId } = this._context;
const worksheetId = workbook.getActiveSheet().getSheetId();
const worksheetId = workbook.getActiveSheet()?.getSheetId() || '';
const filterModel = this._sheetsFilterService.getFilterModel(unitId, worksheetId) ?? undefined;
const getParams = (): ISheetsFilterRenderParams => ({
unitId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { IWorkbookData, LocaleType } from '@univerjs/core';
import { Direction, ICommandService, Plugin, RANGE_TYPE, Univer, UniverInstanceType } from '@univerjs/core';
import type { IWorkbookData, LocaleType, Workbook } from '@univerjs/core';
import { Direction, ICommandService, IUniverInstanceService, Plugin, RANGE_TYPE, Univer, UniverInstanceType } from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';

Expand All @@ -37,6 +37,12 @@ describe('Test "Filter Interceptor"', () => {
get = testBed.get;
sheetsFilterService = testBed.sheetsFilterService;
commandService = testBed.commandService;
univer.__getInjector();

// active sheet
const currentService = get(IUniverInstanceService);
const workbook = currentService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
workbook.ensureActiveSheet();
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('Test filter model and related utils', () => {
filterColumn = new FilterColumn(
'test',
'sheet1',
get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet()!,
get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.ensureActiveSheet(),
{ colId: 0, customFilters: { customFilters: [{ operator: CustomFilterOperator.LESS_THAN, val: 123 }] } },
{ getAlreadyFilteredOutRows() { return new Set(); } }
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('test "hitCell" method', () => {
beforeEach(() => {
const testBed = createTestBed();
univer = testBed.univer;
worksheet = testBed.sheet.getActiveSheet();
worksheet = testBed.sheet.getActiveSheet()!;
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ export class SheetFindModel extends FindModel {
)
.subscribe(() => {
const activeSheet = this._workbook.getActiveSheet();
if (!activeSheet) {
return;
}

const activeSheetId = activeSheet.getSheetId();
if (!this._matchesByWorksheet.has(activeSheetId)) {
return;
Expand Down Expand Up @@ -280,6 +284,7 @@ export class SheetFindModel extends FindModel {

const checkShouldFindInSelections = (): boolean => {
const currentWorksheet = this._workbook.getActiveSheet();
if (!currentWorksheet) return false;
const currentSelections = this._selectionManagerService.getSelections();
const shouldFindInSelections = currentSelections?.some((selection) => !isSelectionSingleCell(selection, currentWorksheet)) ?? false;
return shouldFindInSelections;
Expand All @@ -291,6 +296,8 @@ export class SheetFindModel extends FindModel {

const performFindInWorksheet = (): IFindComplete => {
const currentWorksheet = this._workbook.getActiveSheet();
if (!currentWorksheet) return { results: [] };

const lastMatch = this.currentMatch; // temporarily store the last match to restore the position after the model changes

findBySelections = checkShouldFindInSelections();
Expand Down Expand Up @@ -476,6 +483,10 @@ export class SheetFindModel extends FindModel {
const searchBackgroundColor = this._themeService.getCurrentTheme().gold400;
const color = new ColorKit(searchBackgroundColor).toRgb();
const worksheet = this._workbook.getActiveSheet();
if (!worksheet) {
return;
}

const activeSheetId = worksheet.getSheetId();
const highlightShapes = matches.filter((match) => match.range.subUnitId === activeSheetId).map((find, index) => {
const { startColumn, startRow, endColumn, endRow } = find.range.range;
Expand Down Expand Up @@ -533,7 +544,7 @@ export class SheetFindModel extends FindModel {

private _focusMatch(match: ISheetCellMatch): void {
const subUnitId = match.range.subUnitId;
if (subUnitId !== this._workbook.getActiveSheet().getSheetId()) {
if (subUnitId !== this._workbook.getActiveSheet()?.getSheetId()) {
this._commandService.syncExecuteCommand(SetWorksheetActivateCommand.id,
{ unitId: this._workbook.getUnitId(), subUnitId } as ISetWorksheetActivateCommandParams,
{ fromFindReplace: true }
Expand Down Expand Up @@ -585,7 +596,7 @@ export class SheetFindModel extends FindModel {
}

if (!noFocus) this._focusMatch(match);
if (this._workbook.getActiveSheet().getSheetId() === match.range.subUnitId) {
if (this._workbook.getActiveSheet()?.getSheetId() === match.range.subUnitId) {
this._updateCurrentHighlightShape(this._activeHighlightIndex);
}

Expand Down Expand Up @@ -618,7 +629,7 @@ export class SheetFindModel extends FindModel {
}

if (!noFocus) this._focusMatch(match);
if (this._workbook.getActiveSheet().getSheetId() === match.range.subUnitId) {
if (this._workbook.getActiveSheet()?.getSheetId() === match.range.subUnitId) {
this._updateCurrentHighlightShape(this._activeHighlightIndex);
}

Expand Down Expand Up @@ -660,7 +671,11 @@ export class SheetFindModel extends FindModel {
return this._findPreviousMatchByRange(this._matches, selections[0].range);
}

const currentSheetId = this._workbook.getActiveSheet().getSheetId();
const currentSheetId = this._workbook.getActiveSheet()?.getSheetId();
if (!currentSheetId) {
return null;
}

const worksheetThatHasMatch = this._findPreviousWorksheetThatHasAMatch(currentSheetId, loop);
if (!worksheetThatHasMatch) {
return null;
Expand Down Expand Up @@ -699,7 +714,11 @@ export class SheetFindModel extends FindModel {
return this._findNextMatchByRange(this._matches, selections[0].range, stayIfOnMatch);
}

const currentSheetId = this._workbook.getActiveSheet().getSheetId();
const currentSheetId = this._workbook.getActiveSheet()?.getSheetId();
if (!currentSheetId) {
return null;
}

const worksheetThatHasMatch = this._findNextWorksheetThatHasAMatch(currentSheetId, loop);
if (!worksheetThatHasMatch) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export class SheetsHyperLinkRemoveSheetController extends Disposable {
return { redos: [], undos: [] };
}
const unitId = workbook.getUnitId();
const subUnitId = params.subUnitId || workbook.getActiveSheet().getSheetId();
const subUnitId = params.subUnitId || workbook.getActiveSheet()?.getSheetId();
if (!subUnitId) {
return { redos: [], undos: [] };
}
const links = this._hyperLinkModel.getSubUnit(unitId, subUnitId);

const redos = links.map((link) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ export class SheetsHyperLinkResolverService {
return false;
}
const worksheet = workbook.getActiveSheet();
if (!worksheet) {
return false;
}

if (worksheet.getSheetId() === subUnitId) {
return worksheet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export const CellLinkEdit = () => {
return;
}
if (!range.sheetName) {
range.sheetName = workbook.getActiveSheet().getName();
range.sheetName = workbook.getActiveSheet()?.getName() || '';
}
const newPayload = serializeRangeToRefString(range);
setPayload(newPayload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('test editor', () => {
testBed.get(NumfmtEditorController);
testBed.get(SheetsNumfmtCellContentController);
workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
worksheet = workbook.getActiveSheet()!;
worksheet = workbook.ensureActiveSheet();
Dushusir marked this conversation as resolved.
Show resolved Hide resolved
});

it('before edit with currency', () => {
Expand Down