Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
weird94 committed Oct 16, 2024
1 parent d5e6d78 commit af8aa3e
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 7 deletions.
108 changes: 108 additions & 0 deletions mockdata/src/sheets/demo/default-workbook-data-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14287,6 +14287,114 @@ export const DEFAULT_WORKBOOK_DATA_DEMO: IWorkbookData = {
t: 1,
},
},
25: {
10: {
p: {
id: '__INTERNAL_EDITOR__ZEN_EDITOR',
documentStyle: {
pageSize: {
width: 37.2261962890625,
height: undefined,
},
documentFlavor: 1,
marginTop: 0,
marginBottom: 2,
marginRight: 2,
marginLeft: 2,
renderConfig: {
horizontalAlign: 2,
verticalAlign: 0,
centerAngle: 0,
vertexAngle: 0,
wrapStrategy: 0,
},
},
drawings: {},
drawingsOrder: [],
body: {
dataStream: '1\r\n',
textRuns: [],
paragraphs: [
{
startIndex: 1,
paragraphStyle: {
horizontalAlign: 2,
},
bullet: {
nestingLevel: 0,
textStyle: {
fs: 20,
},
listType: 'CHECK_LIST_CHECKED',
listId: 'w6THUh',
},
},
],
sectionBreaks: [
{
startIndex: 2,
},
],
customRanges: [],
customDecorations: [],
},
},
s: 'idtqdi',
},
11: {
p: {
id: '__INTERNAL_EDITOR__ZEN_EDITOR',
documentStyle: {
pageSize: {
width: 37.2261962890625,
height: undefined,
},
documentFlavor: 1,
marginTop: 0,
marginBottom: 2,
marginRight: 2,
marginLeft: 2,
renderConfig: {
horizontalAlign: 2,
verticalAlign: 0,
centerAngle: 0,
vertexAngle: 0,
wrapStrategy: 0,
},
},
drawings: {},
drawingsOrder: [],
body: {
dataStream: '1\r\n',
textRuns: [],
paragraphs: [
{
startIndex: 1,
paragraphStyle: {
horizontalAlign: 2,
},
bullet: {
nestingLevel: 0,
textStyle: {
fs: 20,
},
listType: 'CHECK_LIST',
listId: 'w6THUh',
},
},
],
sectionBreaks: [
{
startIndex: 2,
},
],
customRanges: [],
customDecorations: [],
},
},
s: 'idtqdi',
},
},
},

freeze: {
Expand Down
69 changes: 69 additions & 0 deletions packages/sheets-ui/src/controllers/checkbox.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Disposable, ICommandService, Inject, UniverInstanceType } from '@univerjs/core';
import { CURSOR_TYPE, IRenderManagerService } from '@univerjs/engine-render';
import { ToggleCellCheckboxCommand } from '@univerjs/sheets';
import { HoverManagerService } from '../services/hover-manager.service';

export class SheetCheckboxController extends Disposable {
private _isPointer = false;
constructor(
@Inject(HoverManagerService) private _hoverManagerService: HoverManagerService,
@ICommandService private readonly _commandService: ICommandService,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService
) {
super();
this._initPointerEvent();
this._initHover();
}

private get _mainComponent() {
return this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET)?.mainComponent;
}

private _initHover() {
this.disposeWithMe(this._hoverManagerService.currentRichText$.subscribe((richText) => {
if (richText?.bullet) {
if (!this._isPointer) {
this._mainComponent?.setCursor(CURSOR_TYPE.POINTER);
}
this._isPointer = true;
} else {
if (this._isPointer) {
this._mainComponent?.setCursor(CURSOR_TYPE.AUTO);
}
this._isPointer = false;
}
}));
}

private _initPointerEvent() {
this.disposeWithMe(this._hoverManagerService.currentClickedCell$.subscribe((cell) => {
const { location, bullet } = cell;
if (!bullet) {
return;
}
this._commandService.executeCommand(ToggleCellCheckboxCommand.id, {
unitId: location.unitId,
subUnitId: location.subUnitId,
row: location.row,
col: location.col,
paragraphIndex: bullet.startIndex,
});
}));
}
}
3 changes: 2 additions & 1 deletion packages/sheets-ui/src/services/hover-manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface IHoverRichTextPosition extends IHoverCellPosition {
export class HoverManagerService extends Disposable {
private _currentCell$ = new BehaviorSubject<Nullable<IHoverCellPosition>>(null);
private _currentRichText$ = new BehaviorSubject<Nullable<IHoverRichTextPosition>>(null);
private _currentClickedCell$ = new Subject<IHoverCellPosition>();
private _currentClickedCell$ = new Subject<IHoverRichTextPosition>();

// Notify when hovering over different cells
currentCell$ = this._currentCell$.asObservable().pipe(
Expand Down Expand Up @@ -163,6 +163,7 @@ export class HoverManagerService extends Disposable {
if (font) {
const { paddingLeft, paddingTop } = calcPadding(cell, font);
const rects = calculateDocSkeletonRects(font.documentSkeleton, paddingLeft, paddingTop);

const innerX = offsetX - position.startX - leftOffset;
const innerY = offsetY - position.startY - topOffset;
customRange = rects.links.find((link) => link.rects.some((rect) => rect.left <= innerX && innerX <= rect.right && (rect.top) <= innerY && innerY <= (rect.bottom)));
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-ui/src/services/utils/doc-skeleton-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const calcDocGlyphPosition = (glyph: IDocumentSkeletonGlyph, skeleton: DocumentS
top: rect.top,
bottom: rect.bottom,
left: rect.left,
right: rect.left,
right: rect.right,
};
};

Expand Down
3 changes: 3 additions & 0 deletions packages/sheets-ui/src/sheets-ui-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { AutoFillController } from './controllers/auto-fill.controller';
import { AutoHeightController } from './controllers/auto-height.controller';
import { CellAlertRenderController } from './controllers/cell-alert.controller';
import { CellCustomRenderController } from './controllers/cell-custom-render.controller';
import { SheetCheckboxController } from './controllers/checkbox.controller';
import { SheetClipboardController } from './controllers/clipboard/clipboard.controller';
import { defaultPluginConfig, PLUGIN_CONFIG_KEY } from './controllers/config.schema';
import { SheetsDefinedNameController } from './controllers/defined-name/defined-name.controller';
Expand Down Expand Up @@ -140,6 +141,7 @@ export class UniverSheetsUIPlugin extends Plugin {
[AutoFillController],
[FormatPainterController],
[SheetsDefinedNameController],
[SheetCheckboxController],

// permission
[SheetPermissionPanelModel],
Expand Down Expand Up @@ -181,6 +183,7 @@ export class UniverSheetsUIPlugin extends Plugin {
[SheetsDefinedNameController],
[StatusBarController],
[AutoHeightController],
[SheetCheckboxController],
]);
}

Expand Down
52 changes: 48 additions & 4 deletions packages/sheets/src/commands/commands/toggle-checkbox.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

import type { ICommand, Workbook } from '@univerjs/core';
import { CommandType, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import type { ICommand, IMutationInfo, Workbook } from '@univerjs/core';
import { BuildTextUtils, CellValueType, CommandType, DocumentDataModel, ICommandService, IUndoRedoService, IUniverInstanceService, TextX, Tools, UniverInstanceType } from '@univerjs/core';
import { type ISetRangeValuesMutationParams, SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';

export interface IToggleCellCheckboxCommandParams {
unitId: string;
Expand All @@ -26,7 +27,7 @@ export interface IToggleCellCheckboxCommandParams {
}

export const ToggleCellCheckboxCommand: ICommand<IToggleCellCheckboxCommandParams> = {
id: 'toggle-cell-checkbox',
id: 'sheet.command.toggle-cell-checkbox',
type: CommandType.COMMAND,
handler: (accessor, params) => {
if (!params) {
Expand All @@ -36,14 +37,57 @@ export const ToggleCellCheckboxCommand: ICommand<IToggleCellCheckboxCommandParam
const univerInstanceService = accessor.get(IUniverInstanceService);
const workbook = univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);
const sheet = workbook?.getSheetBySheetId(subUnitId);
const undoRedoService = accessor.get(IUndoRedoService);
const commandService = accessor.get(ICommandService);
if (!sheet) {
return false;
}
const cell = sheet.getCell(row, col);
if (!cell?.p) {
return false;
}
const p = Tools.deepClone(cell.p);
const documentDataModel = new DocumentDataModel(p);
const textX = BuildTextUtils.paragraph.bullet.toggleChecklist({
document: documentDataModel,
paragraphIndex,
});
if (!textX) {
return false;
}
TextX.apply(documentDataModel.getBody()!, textX.serialize());
const redoParams: ISetRangeValuesMutationParams = {
unitId,
subUnitId,
cellValue: {
[row]: {
[col]: {
p,
t: CellValueType.STRING,
},
},
},
};

const redo: IMutationInfo = {
id: SetRangeValuesMutation.id,
params: redoParams,
};
const undoParams = SetRangeValuesUndoMutationFactory(accessor, redoParams);

const undo: IMutationInfo = {
id: SetRangeValuesMutation.id,
params: undoParams,
};
const redos = [redo];
const undos = [undo];

undoRedoService.pushUndoRedo({
redoMutations: redos,
undoMutations: undos,
unitID: unitId,
});

return true;
return commandService.syncExecuteCommand(redo.id, redo.params);
},
};
3 changes: 3 additions & 0 deletions packages/sheets/src/controllers/basic-worksheet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import {
SetWorksheetRowIsAutoHeightCommand,
} from '../commands/commands/set-worksheet-row-height.command';
import { SetWorksheetShowCommand } from '../commands/commands/set-worksheet-show.command';
import { ToggleCellCheckboxCommand } from '../commands/commands/toggle-checkbox.command';
import { AddRangeProtectionMutation } from '../commands/mutations/add-range-protection.mutation';
import { AddWorksheetMergeMutation } from '../commands/mutations/add-worksheet-merge.mutation';
import { AddWorksheetProtectionMutation } from '../commands/mutations/add-worksheet-protection.mutation';
Expand Down Expand Up @@ -266,6 +267,8 @@ export class BasicWorksheetController extends Disposable implements IDisposable
AddRangeProtectionMutation,
DeleteRangeProtectionMutation,
SetRangeProtectionMutation,

ToggleCellCheckboxCommand,
].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sheets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,5 +391,5 @@ export {
export { ScrollToCellOperation } from './commands/operations/scroll-to-cell.operation';
export { type ISetSelectionsOperationParams, SetSelectionsOperation } from './commands/operations/selection.operation';
export { type ISetWorksheetActiveOperationParams, SetWorksheetActiveOperation } from './commands/operations/set-worksheet-active.operation';

export { type IToggleCellCheckboxCommandParams, ToggleCellCheckboxCommand } from './commands/commands/toggle-checkbox.command';
// #endregion

0 comments on commit af8aa3e

Please sign in to comment.