Skip to content

Commit

Permalink
feat(sheet): add custom for row and column
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Oct 10, 2024
1 parent 9e98122 commit 84dc7e0
Show file tree
Hide file tree
Showing 14 changed files with 561 additions and 143 deletions.
16 changes: 14 additions & 2 deletions packages/core/src/sheets/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

import { type BooleanNumber, CellValueType } from '../types/enum';
import type { IResources } from '../services/resource-manager/type';
import type { IObjectArrayPrimitiveType, IObjectMatrixPrimitiveType, Nullable } from '../shared';
import type { LocaleType } from '../types/enum/locale-type';
import type { IDocumentData } from '../types/interfaces';
import type { ICellCustomRender } from '../types/interfaces/i-cell-custom-render';
import type { ICellValidationData } from '../types/interfaces/i-cell-validation-data';
import type { IStyleData } from '../types/interfaces/i-style-data';
import { type BooleanNumber, CellValueType } from '../types/enum';

/**
* Snapshot of a workbook.
Expand Down Expand Up @@ -131,6 +131,8 @@ export interface IWorksheetData {
rightToLeft: BooleanNumber;
}

export type CustomData = Nullable<Record<string, any>>;

/**
* Properties of row data
*/
Expand All @@ -154,6 +156,11 @@ export interface IRowData {
* hidden
*/
hd?: BooleanNumber;

/**
* User stored custom fields
*/
custom?: CustomData;
}

export interface IRowAutoHeightInfo {
Expand All @@ -174,6 +181,11 @@ export interface IColumnData {
* hidden
*/
hd?: BooleanNumber;

/**
* User stored custom fields
*/
custom?: CustomData;
}

/**
Expand Down Expand Up @@ -215,7 +227,7 @@ export interface ICellData {
/**
* User stored custom fields
*/
custom?: Nullable<Record<string, any>>;
custom?: CustomData;
}

export interface ICellMarksStyle {
Expand Down
46 changes: 40 additions & 6 deletions packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

import type { ICellData, Injector, IStyleData, Nullable, Workbook } from '@univerjs/core';
import type { FUniver } from '../../facade';
import { ICommandService, IUniverInstanceService, RANGE_TYPE, UniverInstanceType } from '@univerjs/core';
import { AddWorksheetMergeCommand, AddWorksheetMergeMutation, InsertColCommand, InsertColMutation, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, RemoveWorksheetMergeCommand, RemoveWorksheetMergeMutation, SetColHiddenCommand, SetColHiddenMutation, SetColVisibleMutation, SetColWidthCommand, SetFrozenCommand, SetFrozenMutation, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetRowHeightCommand, SetRowHiddenCommand, SetRowHiddenMutation, SetRowVisibleMutation, SetSelectionsOperation, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SetWorksheetColWidthMutation, SetWorksheetRowHeightMutation, SetWorksheetRowIsAutoHeightCommand, SetWorksheetRowIsAutoHeightMutation, SheetsSelectionsService } from '@univerjs/sheets';
import { AddWorksheetMergeCommand, AddWorksheetMergeMutation, InsertColCommand, InsertColMutation, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, RemoveWorksheetMergeCommand, RemoveWorksheetMergeMutation, SetColHiddenCommand, SetColHiddenMutation, SetColumnCustomCommand, SetColumnCustomMutation, SetColVisibleMutation, SetColWidthCommand, SetFrozenCommand, SetFrozenMutation, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetRowCustomCommand, SetRowCustomMutation, SetRowHeightCommand, SetRowHiddenCommand, SetRowHiddenMutation, SetRowVisibleMutation, SetSelectionsOperation, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SetWorksheetColWidthMutation, SetWorksheetRowHeightMutation, SetWorksheetRowIsAutoHeightCommand, SetWorksheetRowIsAutoHeightMutation, SheetsSelectionsService } from '@univerjs/sheets';

import { CancelFrozenCommand } from '@univerjs/sheets-ui';
import { beforeEach, describe, expect, it } from 'vitest';

import type { ICellData, Injector, IStyleData, Nullable, Workbook } from '@univerjs/core';
import { createWorksheetTestBed } from './create-worksheet-test-bed';
import type { FUniver } from '../../facade';

describe('Test FWorksheet', () => {
let get: Injector['get'];
Expand Down Expand Up @@ -70,6 +70,8 @@ describe('Test FWorksheet', () => {
commandService.registerCommand(SetWorksheetRowHeightMutation);
commandService.registerCommand(SetWorksheetRowIsAutoHeightCommand);
commandService.registerCommand(SetWorksheetRowIsAutoHeightMutation);
commandService.registerCommand(SetRowCustomCommand);
commandService.registerCommand(SetRowCustomMutation);

// column
commandService.registerCommand(InsertColCommand);
Expand All @@ -84,6 +86,8 @@ describe('Test FWorksheet', () => {
commandService.registerCommand(SetColVisibleMutation);
commandService.registerCommand(SetColWidthCommand);
commandService.registerCommand(SetWorksheetColWidthMutation);
commandService.registerCommand(SetColumnCustomCommand);
commandService.registerCommand(SetColumnCustomMutation);

// merge cells
commandService.registerCommand(AddWorksheetMergeCommand);
Expand Down Expand Up @@ -349,6 +353,21 @@ describe('Test FWorksheet', () => {
expect(currentRowHeight).toBe(200);
});

it('Worksheet setRowCustom', async () => {
const activeSheet = univerAPI.getActiveWorkbook()?.getSheetByName('sheet1');

const sheet = await activeSheet?.setRowCustom({
0: {
color: 'red',
},
});
expect(sheet).toBeDefined();

const currentWorksheet = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)?.getActiveSheet();
const currentRowCustom = currentWorksheet?.getRowManager().getRow(0)?.custom;
expect(currentRowCustom).toEqual({ color: 'red' });
});

// #endregion

// #region Column
Expand Down Expand Up @@ -493,6 +512,23 @@ describe('Test FWorksheet', () => {
expect(currentColWidth2).toBe(100);
});

it('Worksheet setColumnCustom', async () => {
const activeSheet = univerAPI.getActiveWorkbook()?.getSheetByName('sheet1');

const sheet = await activeSheet?.setColumnCustom({
0: {
color: 'red',
},
});
expect(sheet).toBeDefined();

const currentWorksheet = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)?.getActiveSheet();
const currentColCustom = currentWorksheet?.getColumnManager().getColumn(0)?.custom;
expect(currentColCustom).toEqual({ color: 'red' });
});

// #endregion

it('Worksheet freeze', async () => {
const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();

Expand Down Expand Up @@ -544,6 +580,4 @@ describe('Test FWorksheet', () => {
expect(activeSheet?.getFrozenRows()).toBe(21);
expect(activeSheet?.getFrozenColumns()).toBe(9);
});

// #endregion
});
40 changes: 38 additions & 2 deletions packages/facade/src/apis/sheets/f-worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

import type { IFreeze, IRange, Nullable, ObjectMatrix, Workbook, Worksheet } from '@univerjs/core';
import type { CustomData, IFreeze, IObjectArrayPrimitiveType, IRange, Nullable, ObjectMatrix, Workbook, Worksheet } from '@univerjs/core';
import type { IDataValidationResCache } from '@univerjs/sheets-data-validation';
import type { FilterModel } from '@univerjs/sheets-filter';

import type { FWorkbook, IFICanvasFloatDom } from './f-workbook';
import { Direction, ICommandService, Inject, Injector, RANGE_TYPE } from '@univerjs/core';
import { deserializeRangeWithSheet } from '@univerjs/engine-formula';
import { copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService } from '@univerjs/sheets';
import { copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColHiddenCommand, SetColumnCustomCommand, SetColWidthCommand, SetFrozenCommand, SetRowCustomCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService } from '@univerjs/sheets';
import { DataValidationModel, SheetsDataValidationValidatorService } from '@univerjs/sheets-data-validation';
import { SheetCanvasFloatDomManagerService } from '@univerjs/sheets-drawing-ui';
import { SheetsFilterService } from '@univerjs/sheets-filter';
Expand Down Expand Up @@ -602,6 +602,24 @@ export class FWorksheet {
return this;
}

/**
* Set custom properties for given rows.
* @param custom The custom properties to set.
* @returns This sheet, for chaining.
*/
async setRowCustom(custom: IObjectArrayPrimitiveType<CustomData>): Promise<FWorksheet> {
const unitId = this._workbook.getUnitId();
const subUnitId = this._worksheet.getSheetId();

await this._commandService.executeCommand(SetRowCustomCommand.id, {
unitId,
subUnitId,
custom,
});

return this;
}

// #endregion

// #region Column
Expand Down Expand Up @@ -892,6 +910,24 @@ export class FWorksheet {
return this;
}

/**
* Set custom properties for given columns.
* @param custom The custom properties to set.
* @returns This sheet, for chaining.
*/
async setColumnCustom(custom: IObjectArrayPrimitiveType<CustomData>): Promise<FWorksheet> {
const unitId = this._workbook.getUnitId();
const subUnitId = this._worksheet.getSheetId();

await this._commandService.executeCommand(SetColumnCustomCommand.id, {
unitId,
subUnitId,
custom,
});

return this;
}

// #endregion

// #region merge cells
Expand Down
50 changes: 0 additions & 50 deletions packages/sheets/src/basics/__tests__/cell-custom.spec.ts

This file was deleted.

78 changes: 0 additions & 78 deletions packages/sheets/src/basics/cell-custom.ts

This file was deleted.

Loading

0 comments on commit 84dc7e0

Please sign in to comment.