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(doc): the paragraph panel is not updated in real time (#3952)

Gpound.liu2bea4578263a
diff --git a/packages/docs-ui/src/views/paragraph-setting/index.tsx b/packages/docs-ui/src/views/paragraph-setting/index.tsx
index 7e416cf1ec..728a993c62 100644
--- a/packages/docs-ui/src/views/paragraph-setting/index.tsx
+++ b/packages/docs-ui/src/views/paragraph-setting/index.tsx
@@ -15,10 +15,10 @@
*/
import type { ITextRange } from '@univerjs/core';
-import { generateRandomId, ICommandService, LocaleService, useDependency, useObservable } from '@univerjs/core';
-import React, { useEffect, useRef, useState } from 'react';
import type { IRichTextEditingMutationParams, ISetTextSelectionsOperationParams } from '@univerjs/docs';
+import { debounce, generateRandomId, ICommandService, LocaleService, useDependency, useObservable } from '@univerjs/core';
import { RichTextEditingMutation, SetTextSelectionsOperation } from '@univerjs/docs';
+import React, { useEffect, useMemo, useRef, useState } from 'react';
import { DocParagraphSettingCommand } from '../../commands/commands/doc-paragraph-setting.command';
import { ParagraphSetting } from './Setting';
@@ -28,28 +28,18 @@ const isRangesEqual = (oldRanges: ITextRange[], ranges: ITextRange[]) => {
range.startOffset === oldRange.startOffset && range.endOffset === oldRange.endOffset));
};
-const isRangesIntersection = (oldRanges: ITextRange[], ranges: ITextRange[]) => {
- return oldRanges.some((oldRange) => {
- return ranges.some((range) => {
- const { startOffset: activeStart, endOffset: activeEnd } = oldRange;
- const { startOffset: compareStart, endOffset: compareEnd } = range;
-
- if (activeStart == null || activeEnd == null || compareStart == null || compareEnd == null) {
- return false;
- }
-
- return activeStart <= compareEnd && activeEnd >= compareStart;
- });
- });
-};
export function ParagraphSettingIndex() {
const commandService = useDependency(ICommandService);
const localeService = useDependency(LocaleService);
const currentLocale = useObservable(localeService.currentLocale$);
const [key, keySet] = useState('');
+ const debounceReset = useMemo(() => {
+ return debounce(() => keySet(generateRandomId(4)), 300);
+ }, []);
const rangeRef = useRef<ITextRange[]>([]);
+
useEffect(() => {
const dispose = commandService.onCommandExecuted((info) => {
if (SetTextSelectionsOperation.id === info.id) {
@@ -61,19 +51,20 @@ export function ParagraphSettingIndex() {
}
if (RichTextEditingMutation.id === info.id) {
const params = info.params as IRichTextEditingMutationParams;
- const ranges = params.textRanges ?? [];
const trigger = params.trigger;
- if (trigger !== DocParagraphSettingCommand.id && isRangesIntersection(ranges, rangeRef.current)) {
- keySet(generateRandomId(4));
+ if (trigger !== DocParagraphSettingCommand.id) {
+ debounceReset();
}
}
});
return () => dispose.dispose();
- }, []);
+ }, [debounceReset]);
useEffect(() => {
keySet(generateRandomId(4));
}, [currentLocale]);
+ useEffect(() => () => debounceReset.cancel(), [debounceReset]);
+
return <ParagraphSetting key={key} />;
}