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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(editor): the rendering is not consistent with the result (#3966)
Gpound.liu125cfa950b1b
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/index.tsx b/packages/sheets-formula-ui/src/views/formula-editor/index.tsxindex aab5342c4b..fa77c6d9fd 100644--- a/packages/sheets-formula-ui/src/views/formula-editor/index.tsx+++ b/packages/sheets-formula-ui/src/views/formula-editor/index.tsx@@ -23,6 +23,7 @@ import { operatorToken } from '@univerjs/engine-formula';import { EMBEDDING_FORMULA_EDITOR } from '@univerjs/sheets-ui';import clsx from 'clsx';import React, { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';+import { useEmitChange } from '../range-selector/hooks/useEmitChange';import { useFocus } from '../range-selector/hooks/useFocus';import { useFormulaToken } from '../range-selector/hooks/useFormulaToken';import { useLeftAndRightArrow } from '../range-selector/hooks/useLeftAndRightArrow';@@ -98,6 +99,11 @@ export function FormulaEditor(props: IFormulaEditorProps) {const isError = useMemo(() => errorText !== undefined, [errorText]);const { sequenceNodes, sequenceNodesSet } = useFormulaToken(formulaWithoutEqualSymbol);++ const needEmit = useEmitChange(sequenceNodes, (text: string) => {+ onChange(`=${text}`);+ }, editor);+const refSelections = useDocHight(editorId, sequenceNodes);useVerify(isFocus, onVerify, formulaText);const focus = useFocus(editor);@@ -124,8 +130,8 @@ export function FormulaEditor(props: IFormulaEditorProps) {const handleSelectionChange = (refString: string, offset: number) => {const result = `=${refString}`;+ needEmit();formulaTextSet(result);- onChange(result);if (offset > -1) {setTimeout(() => {editor?.setSelectionRanges([{ startOffset: offset + 1, endOffset: offset + 1 }]);@@ -148,8 +154,8 @@ export function FormulaEditor(props: IFormulaEditorProps) {if (editor) {const d = editor.input$.subscribe((e) => {const text = (e.data.body?.dataStream ?? '').replaceAll(/\n|\r/g, '');+ needEmit();formulaTextSet(text);- onChange(text);});return () => {d.unsubscribe();diff --git a/packages/sheets-formula-ui/src/views/range-selector/hooks/useEmitChange.ts b/packages/sheets-formula-ui/src/views/range-selector/hooks/useEmitChange.tsnew file mode 100644index 0000000000..491fb63c7f--- /dev/null+++ b/packages/sheets-formula-ui/src/views/range-selector/hooks/useEmitChange.ts@@ -0,0 +1,47 @@+/**+ * 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 type { Editor } from '@univerjs/docs-ui';+import type { INode } from './useFormulaToken';+import { useEffect, useRef } from 'react';+import { sequenceNodeToText } from '../utils/sequenceNodeToText';++export const useEmitChange = (sequenceNodes: INode[], onChange: (v: string) => void, editor?: Editor) => {+ const isNeedEmit = useRef(false);++ useEffect(() => {+ if (editor) {+ const d = editor.input$.subscribe(() => {+ isNeedEmit.current = true;+ });+ return () => {+ d.unsubscribe();+ };+ }+ }, [editor]);++ useEffect(() => {+ if (isNeedEmit.current && sequenceNodes) {+ isNeedEmit.current = false;+ const result = sequenceNodeToText(sequenceNodes);+ onChange(result);+ }+ }, [sequenceNodes]);++ const needEmit = () => isNeedEmit.current = true;++ return needEmit;+};diff --git a/packages/sheets-formula-ui/src/views/range-selector/index.tsx b/packages/sheets-formula-ui/src/views/range-selector/index.tsxindex c6fb67a816..3c3f616127 100644--- a/packages/sheets-formula-ui/src/views/range-selector/index.tsx+++ b/packages/sheets-formula-ui/src/views/range-selector/index.tsx@@ -32,6 +32,7 @@ import { filter, noop } from 'rxjs';import { RefSelectionsRenderService } from '../../services/render-services/ref-selections.render-service';import { useEditorInput } from './hooks/useEditorInput';+import { useEmitChange } from './hooks/useEmitChange';import { useFocus } from './hooks/useFocus';import { useFormulaToken } from './hooks/useFormulaToken';import { buildTextRuns, useColor, useDocHight, useSheetHighlight } from './hooks/useHighlight';@@ -161,33 +162,6 @@ export function RangeSelector(props: IRangeSelectorProps) {}}, 30), [isSupportAcrossSheet]);- const handleConfirm = (ranges: IUnitRangeName[]) => {- const text = unitRangesToText(ranges, isSupportAcrossSheet).join(matchToken.COMMA);- rangeStringSet(text);- onChange(text);- rangeDialogVisibleSet(false);- onRangeSelectorDialogVisibleChange(false);- setTimeout(() => {- editor?.setSelectionRanges([{ startOffset: text.length, endOffset: text.length }]);- }, 30);- };-- const handleClose = () => {- rangeDialogVisibleSet(false);- onRangeSelectorDialogVisibleChange(false);- };-- const handleOpenModal = () => {- if (!isError) {- focus();- // 从另一个 editor 直接打开的时候,调整下事件监听顺序,确保打开面板的逻辑在 editor 切换之后- setTimeout(() => {- rangeDialogVisibleSet(true);- onRangeSelectorDialogVisibleChange(true);- }, 30);- }- };-const focus = useFocus(editor);useLayoutEffect(() => {@@ -211,10 +185,15 @@ export function RangeSelector(props: IRangeSelectorProps) {const { checkScrollBar } = useResize(editor);+ const { sequenceNodes, sequenceNodesSet } = useFormulaToken(rangeString);+ const sheetHighlightRanges = useDocHight(editorId, sequenceNodes);++ const needEmit = useEmitChange(sequenceNodes, handleInputDebounce, editor);+const handleSheetSelectionChange = useMemo(() => {return (text: string, offset: number) => {rangeStringSet(text);- onChange(text);+ needEmit();focus();if (offset !== -1) {// 在渲染结束之后再设置选区@@ -229,8 +208,6 @@ export function RangeSelector(props: IRangeSelectorProps) {};}, [editor]);- const { sequenceNodes, sequenceNodesSet } = useFormulaToken(rangeString);- const sheetHighlightRanges = useDocHight(editorId, sequenceNodes);useSheetHighlight(!rangeDialogVisible && isFocus, unitId, subUnitId, sheetHighlightRanges);useSheetSelectionChange(!rangeDialogVisible && isFocus, unitId, subUnitId, sequenceNodes, isSupportAcrossSheet, isOnlyOneRange, handleSheetSelectionChange);@@ -253,8 +230,8 @@ export function RangeSelector(props: IRangeSelectorProps) {if (editor) {const dispose = editor.input$.subscribe((e) => {const text = (e.data.body?.dataStream ?? '').replaceAll(/\n|\r/g, '').replaceAll(/,{2,}/g, ',').replaceAll(/(^,)/g, '');+ needEmit();rangeStringSet(text);- handleInputDebounce(text);});return () => {dispose.unsubscribe();@@ -309,6 +286,33 @@ export function RangeSelector(props: IRangeSelectorProps) {}, 30);};+ const handleConfirm = (ranges: IUnitRangeName[]) => {+ const text = unitRangesToText(ranges, isSupportAcrossSheet).join(matchToken.COMMA);+ needEmit();+ rangeStringSet(text);+ rangeDialogVisibleSet(false);+ onRangeSelectorDialogVisibleChange(false);+ setTimeout(() => {+ editor?.setSelectionRanges([{ startOffset: text.length, endOffset: text.length }]);+ }, 30);+ };++ const handleClose = () => {+ rangeDialogVisibleSet(false);+ onRangeSelectorDialogVisibleChange(false);+ };++ const handleOpenModal = () => {+ if (!isError) {+ focus();+ // 从另一个 editor 直接打开的时候,调整下事件监听顺序,确保打开面板的逻辑在 editor 切换之后+ setTimeout(() => {+ rangeDialogVisibleSet(true);+ onRangeSelectorDialogVisibleChange(true);+ }, 30);+ }+ };+return (<div className={styles.sheetRangeSelector} ref={rangeSelectorWrapRef}><div className={cl(styles.sheetRangeSelectorTextWrap, {