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

feat(pivot): fix scroll height & add hooks to format paint #3224

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion packages/design/src/components/scrollbar/Scrollbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function Scrollbar(props: IScrollbarProps) {

const { scrollHeight } = contentRef.current!;
setThumbHeight((containerHeight / scrollHeight) * 100);
containerRef.current!.style.height = `${containerHeight}px`;
containerRef.current!.style.height = `${Math.floor(containerHeight)}px`;
}

function handleScroll(e: Event) {
Expand Down
3 changes: 3 additions & 0 deletions packages/sheets-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export { UniverSheetsMobileUIPlugin } from './mobile-sheets-ui-plugin';
export { MobileSheetBar } from './views/mobile/sheet-bar/MobileSheetBar';
export { SheetPermissionInitController } from './controllers/permission/sheet-permission-init.controller';

export { IFormatPainterService } from './services/format-painter/format-painter.service';
export type { IFormatPainterBeforeApplyHookParams, IFormatPainterHook } from './services/format-painter/format-painter.service';

// #region - all commands

export { AddWorksheetMergeCommand, AddWorksheetMergeAllCommand, AddWorksheetMergeVerticalCommand, AddWorksheetMergeHorizontalCommand } from './commands/commands/add-worksheet-merge.command';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,29 @@ export interface IFormatPainterHook {
isDefaultHook?: boolean;
priority?: number;
onStatusChange(status: FormatPainterStatus): void;
onApply(
onApply?(
unitId: string,
subUnitId: string,
range: IRange,
format: ISelectionFormatInfo): {
undos: IMutationInfo[];
redos: IMutationInfo[];
};
onBeforeApply?(ctx: IFormatPainterBeforeApplyHookParams): boolean;
}
export enum FormatPainterStatus {
OFF,
ONCE,
INFINITE,
}
export interface IFormatPainterBeforeApplyHookParams {
unitId: string;
subUnitId: string;
range: IRange;
redoMutationsInfo: IMutationInfo[];
undoMutationsInfo: IMutationInfo[];
format: ISelectionFormatInfo;
}

export const IFormatPainterService = createIdentifier<IFormatPainterService>('univer.format-painter-service');

Expand All @@ -84,7 +93,7 @@ export class FormatPainterService extends Disposable implements IFormatPainterSe
this._selectionFormat = { styles: new ObjectMatrix<IStyleData>(), merges: [] };
}

addHook(hook: IFormatPainterHook) {
addHook(hook: IFormatPainterHook): void {
if (hook.isDefaultHook && (hook.priority ?? 0) > (this._defaultHook?.priority ?? -1)) {
this._defaultHook = hook;
} else {
Expand All @@ -93,11 +102,11 @@ export class FormatPainterService extends Disposable implements IFormatPainterSe
}
}

getHooks() {
getHooks(): IFormatPainterHook[] {
return this._defaultHook ? [this._defaultHook, ...this._extendHooks] : this._extendHooks;
}

setStatus(status: FormatPainterStatus) {
setStatus(status: FormatPainterStatus): void {
this._updateRangeMark(status);
this._status$.next(status);
const hooks = this.getHooks();
Expand All @@ -110,31 +119,51 @@ export class FormatPainterService extends Disposable implements IFormatPainterSe
return this._status$.getValue();
}

setSelectionFormat(format: ISelectionFormatInfo) {
setSelectionFormat(format: ISelectionFormatInfo): void {
this._selectionFormat = format;
}

getSelectionFormat() {
getSelectionFormat(): ISelectionFormatInfo {
return this._selectionFormat;
}

applyFormatPainter(unitId: string, subUnitId: string, range: IRange) {
applyFormatPainter(unitId: string, subUnitId: string, range: IRange): boolean {
const hooks = this.getHooks();
const redoMutationsInfo: IMutationInfo[] = [];
const undoMutationsInfo: IMutationInfo[] = [];
hooks.forEach((h) => {
const applyReturn = h.onApply(
unitId,
subUnitId,
range,
this._selectionFormat
);
if (applyReturn) {
redoMutationsInfo.push(...applyReturn.redos);
undoMutationsInfo.push(...applyReturn.undos);
if (h.onApply !== undefined) {
const applyReturn = h.onApply(
unitId,
subUnitId,
range,
this._selectionFormat
);
if (applyReturn) {
redoMutationsInfo.push(...applyReturn.redos);
undoMutationsInfo.push(...applyReturn.undos);
}
}
});

for (const beforeHook of hooks) {
if (beforeHook.onBeforeApply !== undefined) {
// check before apply hook, it can cancel the apply action
const result = beforeHook.onBeforeApply({
unitId,
subUnitId,
range,
redoMutationsInfo,
format: this._selectionFormat,
undoMutationsInfo,
});

if (!result) {
return false;
}
}
}

this._logService.log('[FormatPainterService]', 'apply mutations', {
undoMutationsInfo,
redoMutationsInfo,
Expand All @@ -153,7 +182,7 @@ export class FormatPainterService extends Disposable implements IFormatPainterSe
return result;
}

private _updateRangeMark(status: FormatPainterStatus) {
private _updateRangeMark(status: FormatPainterStatus): void {
this._markSelectionService.removeAllShapes();

if (status !== FormatPainterStatus.OFF) {
Expand Down
Loading