Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

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

VicKun84e8c89d1793
diff --git a/packages/design/src/components/scrollbar/Scrollbar.tsx b/packages/design/src/components/scrollbar/Scrollbar.tsx
index 4e9d99324f..bbf011cc35 100644
--- a/packages/design/src/components/scrollbar/Scrollbar.tsx
+++ b/packages/design/src/components/scrollbar/Scrollbar.tsx
@@ -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) {
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index d7c2afd3ad..c30b777322 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -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';
diff --git a/packages/sheets-ui/src/services/format-painter/format-painter.service.ts b/packages/sheets-ui/src/services/format-painter/format-painter.service.ts
index d5789f0cdb..e24307d281 100644
--- a/packages/sheets-ui/src/services/format-painter/format-painter.service.ts
+++ b/packages/sheets-ui/src/services/format-painter/format-painter.service.ts
@@ -43,8 +43,8 @@ export interface IFormatPainterHook {
id: string;
isDefaultHook?: boolean;
priority?: number;
- onStatusChange(status: FormatPainterStatus): void;
- onApply(
+ onStatusChange?(status: FormatPainterStatus): void;
+ onApply?(
unitId: string,
subUnitId: string,
range: IRange,
@@ -52,12 +52,21 @@ export interface IFormatPainterHook {
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');
@@ -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 {
@@ -93,16 +102,18 @@ 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();
hooks.forEach((hook) => {
- hook.onStatusChange(status);
+ if (hook.onStatusChange !== undefined) {
+ hook.onStatusChange(status);
+ }
});
}
@@ -110,31 +121,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,
@@ -153,7 +184,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) {