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.

fix(ui): progress bar animation (#4136)

Dushusirf578296f9e6a
diff --git a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts b/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts
index 120f91fd3d..7b2fb49b7a 100644
--- a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts
+++ b/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts
@@ -22,7 +22,8 @@ import type {
IExecutionInProgressParams,
IFormulaDirtyData,
ISetFormulaCalculationNotificationMutation,
- ISetFormulaCalculationStartMutation } from '@univerjs/engine-formula';
+ ISetFormulaCalculationStartMutation,
+} from '@univerjs/engine-formula';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import type { IUniverSheetsFormulaBaseConfig } from './config.schema';
import { Disposable, ICommandService, IConfigService, ILogService, Inject, LocaleService } from '@univerjs/core';
@@ -33,7 +34,8 @@ import {
IActiveDirtyManagerService,
SetFormulaCalculationNotificationMutation,
SetFormulaCalculationStartMutation,
- SetFormulaCalculationStopMutation } from '@univerjs/engine-formula';
+ SetFormulaCalculationStopMutation,
+} from '@univerjs/engine-formula';
import {
ClearSelectionFormatCommand,
SetBorderCommand,
@@ -301,9 +303,9 @@ export class TriggerCalculationController extends Disposable {
const { startRow: existingStartRow, startColumn: existingStartColumn, endRow: existingEndRow, endColumn: existingEndColumn } = existingRange.range;
if (
startRow === existingStartRow &&
- startColumn === existingStartColumn &&
- endRow === existingEndRow &&
- endColumn === existingEndColumn
+ startColumn === existingStartColumn &&
+ endRow === existingEndRow &&
+ endColumn === existingEndColumn
) {
isDuplicate = true;
break;
@@ -366,31 +368,6 @@ export class TriggerCalculationController extends Disposable {
if (forceCalculation) {
this._forceCalculating = true;
}
-
- // In NO_CALCULATION mode, the following processes will not be triggered, so there is no need to start
- if (this._calculationMode === CalculationMode.NO_CALCULATION) {
- return;
- }
-
- // When calculations are started multiple times in succession, only the first time is recognized
- if (calculationProcessCount === 0) {
- this._startExecutionTime = performance.now();
- }
-
- // Increment the calculation process count and assign a new ID
- calculationProcessCount++;
-
- // Clear any existing timer to prevent duplicate executions
- if (startDependencyTimer !== null) {
- clearTimeout(startDependencyTimer);
- startDependencyTimer = null;
- }
-
- // If the total calculation time exceeds 1s, a progress bar is displayed.
- startDependencyTimer = setTimeout(() => {
- startDependencyTimer = null;
- this._startProgress();
- }, 1000);
} else if (command.id === SetFormulaCalculationStopMutation.id) {
this.clearProgress();
}
@@ -406,7 +383,32 @@ export class TriggerCalculationController extends Disposable {
stage,
} = params.stageInfo;
- if (stage === FormulaExecuteStageType.CURRENTLY_CALCULATING) {
+ if (stage === FormulaExecuteStageType.START) {
+ // In NO_CALCULATION mode, the following processes will not be triggered, so there is no need to start
+ if (this._calculationMode === CalculationMode.NO_CALCULATION) {
+ return;
+ }
+
+ // When calculations are started multiple times in succession, only the first time is recognized
+ if (calculationProcessCount === 0) {
+ this._startExecutionTime = performance.now();
+ }
+
+ // Increment the calculation process count and assign a new ID
+ calculationProcessCount++;
+
+ // Clear any existing timer to prevent duplicate executions
+ if (startDependencyTimer !== null) {
+ clearTimeout(startDependencyTimer);
+ startDependencyTimer = null;
+ }
+
+ // If the total calculation time exceeds 1s, a progress bar is displayed.
+ startDependencyTimer = setTimeout(() => {
+ startDependencyTimer = null;
+ this._startProgress();
+ }, 1000);
+ } else if (stage === FormulaExecuteStageType.CURRENTLY_CALCULATING) {
this._executionInProgressParams = params.stageInfo;
if (startDependencyTimer === null) {
diff --git a/packages/ui/src/components/progress-bar/ProgressBar.tsx b/packages/ui/src/components/progress-bar/ProgressBar.tsx
index 5960538460..3d7db0248c 100644
--- a/packages/ui/src/components/progress-bar/ProgressBar.tsx
+++ b/packages/ui/src/components/progress-bar/ProgressBar.tsx
@@ -17,7 +17,7 @@
import { ThemeService, useDependency } from '@univerjs/core';
import { Tooltip } from '@univerjs/design';
import { CloseSingle } from '@univerjs/icons';
-import React, { useEffect, useRef, useState } from 'react';
+import React, { useEffect, useRef } from 'react';
import styles from './index.module.less';
export interface IProgressBarProps {
@@ -35,30 +35,36 @@ export function ProgressBar(props: IProgressBarProps) {
const color = barColor ?? themeService.getCurrentTheme().primaryColor; ;
const progressBarInnerRef = useRef<HTMLDivElement>(null);
- // Introduce a state variable for visibility
- const [visible, setVisible] = useState(count > 0);
+ const progressBarContainerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
- if (!progressBarInnerRef.current) return;
-
const progressBarInner = progressBarInnerRef.current;
+ const progressBarContainer = progressBarContainerRef.current;
+
+ if (!progressBarInner || !progressBarContainer) return;
// Hide immediately if both count and done are zero
if (count === 0 && done === 0) {
- setVisible(false);
+ progressBarContainer.style.display = 'none';
progressBarInner.style.width = '0%';
+ return;
}
// Update the width of the progress bar
else if (count > 0) {
- setVisible(true);
+ progressBarContainer.style.display = 'flex';
+
+ const width = Math.floor((done / count) * 100);
// Trigger the animation to prevent the progress bar from not being closed due to reaching 100% too quickly without animation
if (done === count) {
requestAnimationFrame(() => {
- progressBarInner.style.width = `${Math.floor((done / count) * 100)}%`;
+ progressBarInner.style.width = `${width - 1}%`; // Set a width slightly smaller than the target
+ requestAnimationFrame(() => {
+ progressBarInner.style.width = `${width}%`; // Then set the target width
+ });
});
} else {
- progressBarInner.style.width = `${Math.floor((done / count) * 100)}%`;
+ progressBarInner.style.width = `${width}%`;
}
}
// Else, wait for the transition to end before hiding
@@ -67,7 +73,8 @@ export function ProgressBar(props: IProgressBarProps) {
const handleTransitionEnd = () => {
if (done === count) {
// Hide the progress bar after the animation finishes
- setVisible(false);
+ progressBarContainer.style.display = 'none';
+ progressBarInner.style.width = '0%';
// Notify the parent component to reset the progress after the animation ends
// After the progress bar is completed 100%, the upper props data source may not be reset, resulting in count and done still being the previous values (displaying 100%) when the progress bar is triggered next time, so a message is reported here to trigger clearing.
@@ -84,7 +91,7 @@ export function ProgressBar(props: IProgressBarProps) {
}, [count, done]);
return (
- <div className={styles.progressBarContainer} style={{ display: visible ? 'flex' : 'none' }}>
+ <div ref={progressBarContainerRef} className={styles.progressBarContainer} style={{ display: 'none' }}>
<Tooltip showIfEllipsis title={label}>
<span className={styles.progressBarLabel}>{label}</span>