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: sync actions between formula editor and cell editor (#2380)

Ran Luobdf033c2ed5b
diff --git a/packages/core/src/types/interfaces/i-document-data.ts b/packages/core/src/types/interfaces/i-document-data.ts
index 96fa8381c0..a98ac4f520 100644
--- a/packages/core/src/types/interfaces/i-document-data.ts
+++ b/packages/core/src/types/interfaces/i-document-data.ts
@@ -378,6 +378,7 @@ export interface IDocumentRenderConfig {
background?: IColorStyle; // background
wrapStrategy?: WrapStrategy; // wrap to the next line, for sheet cell
cellValueType?: CellValueType; // sheet cell type, In a spreadsheet cell, without any alignment settings applied, text should be left-aligned, numbers should be right-aligned, and Boolean values should be center-aligned.
+ isRenderStyle?: BooleanNumber; // Whether to render the style(textRuns), used in formula bar editor. the default value is TRUE.
}
export interface ISectionBreakBase {
diff --git a/packages/docs/src/commands/commands/core-editing.command.ts b/packages/docs/src/commands/commands/core-editing.command.ts
index 48957398cf..55ff22fa0c 100644
--- a/packages/docs/src/commands/commands/core-editing.command.ts
+++ b/packages/docs/src/commands/commands/core-editing.command.ts
@@ -37,11 +37,13 @@ export interface IInsertCommandParams {
segmentId?: string;
}
+export const EditorInsertTextCommandId = 'doc.command.insert-text';
+
/**
* The command to insert text. The changed range could be non-collapsed, mainly use in line break and normal input.
*/
export const InsertCommand: ICommand<IInsertCommandParams> = {
- id: 'doc.command.insert-text',
+ id: EditorInsertTextCommandId,
type: CommandType.COMMAND,
diff --git a/packages/docs/src/commands/commands/inline-format.command.ts b/packages/docs/src/commands/commands/inline-format.command.ts
index 4f899630a3..2337630224 100644
--- a/packages/docs/src/commands/commands/inline-format.command.ts
+++ b/packages/docs/src/commands/commands/inline-format.command.ts
@@ -21,8 +21,6 @@ import {
BaselineOffset,
BooleanNumber,
CommandType,
- DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
- DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
ICommandService,
IUniverInstanceService,
JSONX,
@@ -278,19 +276,12 @@ export const SetInlineFormatCommand: ICommand<ISetInlineFormatCommandParams> = {
return false;
}
- let documentDataModel = univerInstanceService.getCurrentUniverDocInstance();
+ const documentDataModel = univerInstanceService.getCurrentUniverDocInstance();
if (!documentDataModel) {
return false;
}
- let unitId = documentDataModel.getUnitId();
-
- // When setting the formula bar style, the effect will be displayed in the cell editor,
- // and the formula bar only displays plain text.
- if (unitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) {
- documentDataModel = univerInstanceService.getUniverDocInstance(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)!;
- unitId = documentDataModel.getUnitId();
- }
+ const unitId = documentDataModel.getUnitId();
let formatValue;
diff --git a/packages/docs/src/index.ts b/packages/docs/src/index.ts
index 8901e66dcc..b11c2329aa 100644
--- a/packages/docs/src/index.ts
+++ b/packages/docs/src/index.ts
@@ -26,6 +26,7 @@ export {
InsertCommand,
type IUpdateCommandParams,
UpdateCommand,
+ EditorInsertTextCommandId,
} from './commands/commands/core-editing.command';
export { DeleteLeftCommand, DeleteRightCommand } from './commands/commands/delete.command';
export { type IIMEInputCommandParams, IMEInputCommand } from './commands/commands/ime-input.command';
@@ -43,7 +44,13 @@ export {
ResetInlineFormatTextBackgroundColorCommand,
SetInlineFormatTextBackgroundColorCommand,
} from './commands/commands/inline-format.command';
-export { AlignOperationCommand, AlignLeftCommand, AlignCenterCommand, AlignRightCommand, AlignJustifyCommand } from './commands/commands/paragraph-align.command';
+export {
+ AlignOperationCommand,
+ AlignLeftCommand,
+ AlignCenterCommand,
+ AlignRightCommand,
+ AlignJustifyCommand,
+} from './commands/commands/paragraph-align.command';
export { BulletListCommand, OrderListCommand } from './commands/commands/list.command';
export { CoverContentCommand, ReplaceContentCommand } from './commands/commands/replace-content.command';
export {
diff --git a/packages/engine-render/src/components/docs/layout/tools.ts b/packages/engine-render/src/components/docs/layout/tools.ts
index 3496d920a6..22ba36a59d 100644
--- a/packages/engine-render/src/components/docs/layout/tools.ts
+++ b/packages/engine-render/src/components/docs/layout/tools.ts
@@ -741,15 +741,6 @@ export function getFontCreateConfig(
sectionBreakConfig: ISectionBreakConfig,
paragraphStyle: IParagraphStyle
) {
- const { startIndex } = paragraphNode;
- const textRun = bodyModel.getTextRun(index + startIndex) || { ts: {}, st: 0, ed: 0 };
- const { st, ed } = textRun;
- let { ts: textStyle = {} } = textRun;
- const cache = fontCreateConfigCache.getValue(st, ed);
- if (cache) {
- return cache;
- }
-
const {
gridType = GridType.LINES,
charSpace = 0,
@@ -762,7 +753,19 @@ export function getFontCreateConfig(
marginRight = 0,
marginLeft = 0,
localeService,
+ renderConfig = {},
} = sectionBreakConfig;
+ const { isRenderStyle } = renderConfig;
+ const { startIndex } = paragraphNode;
+ const textRun = isRenderStyle === BooleanNumber.FALSE
+ ? { ts: {}, st: 0, ed: 0 }
+ : bodyModel.getTextRun(index + startIndex) || { ts: {}, st: 0, ed: 0 };
+ const { st, ed } = textRun;
+ let { ts: textStyle = {} } = textRun;
+ const cache = fontCreateConfigCache.getValue(st, ed);
+ if (cache) {
+ return cache;
+ }
const { snapToGrid = BooleanNumber.TRUE } = paragraphStyle;
diff --git a/packages/sheets-ui/src/controllers/editor/formula-editor.controller.ts b/packages/sheets-ui/src/controllers/editor/formula-editor.controller.ts
index 21f5440efc..a2efb1fb1f 100644
--- a/packages/sheets-ui/src/controllers/editor/formula-editor.controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/formula-editor.controller.ts
@@ -14,8 +14,9 @@
* limitations under the License.
*/
-import type { ICommandInfo, IParagraph, ITextRun, Nullable } from '@univerjs/core';
+import type { DocumentDataModel, ICommandInfo, IParagraph, ITextRun, JSONXActions, Nullable } from '@univerjs/core';
import {
+ BooleanNumber,
createInterceptorKey,
DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
@@ -280,12 +281,13 @@ export class FormulaEditorController extends RxDisposable {
});
}
+ // Sync cell content to formula editor bar when sheet selection changed or visible changed.
private _editorSyncHandler(param: IEditorBridgeServiceParam) {
const body = param.documentLayoutObject.documentModel?.getBody();
let dataStream = body?.dataStream;
let paragraphs = body?.paragraphs;
- let textRuns: ITextRun[] = [];
+ let textRuns = body?.textRuns;
if (dataStream == null || paragraphs == null) {
return;
@@ -301,8 +303,7 @@ export class FormulaEditorController extends RxDisposable {
startIndex: 0,
},
];
- } else if (param.isInArrayFormulaRange === true) {
- textRuns = body?.textRuns || [];
+ textRuns = [];
}
this._syncContentAndRender(DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, dataStream, paragraphs, textRuns);
@@ -311,37 +312,76 @@ export class FormulaEditorController extends RxDisposable {
this._autoScroll();
}
+ // eslint-disable-next-line max-lines-per-function
private _commandExecutedListener() {
- const updateCommandList = [RichTextEditingMutation.id, SetEditorResizeOperation.id];
-
const INCLUDE_LIST = [DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];
this.disposeWithMe(
this._commandService.onCommandExecuted((command: ICommandInfo) => {
- if (updateCommandList.includes(command.id)) {
- const params = command.params as IRichTextEditingMutationParams;
- const { unitId } = params;
-
- if (INCLUDE_LIST.includes(unitId)) {
- // sync cell content to formula editor bar when edit cell editor and vice verse.
- const editorDocDataModel = this._univerInstanceService.getUniverDocInstance(unitId);
- const dataStream = editorDocDataModel?.getBody()?.dataStream;
- const paragraphs = editorDocDataModel?.getBody()?.paragraphs;
-
- const syncId =
- unitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY
- ? DOCS_NORMAL_EDITOR_UNIT_ID_KEY
- : DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY;
-
- if (dataStream == null || paragraphs == null) {
- return;
- }
+ if (command.id !== RichTextEditingMutation.id) {
+ return;
+ }
+ const params = command.params as IRichTextEditingMutationParams;
+ const { unitId, actions } = params;
+
+ if (INCLUDE_LIST.includes(unitId)) {
+ // sync cell content to formula editor bar when edit cell editor and vice verse.
+ const editorDocDataModel = this._univerInstanceService.getUniverDocInstance(unitId);
+ const dataStream = editorDocDataModel?.getBody()?.dataStream;
+ const paragraphs = editorDocDataModel?.getBody()?.paragraphs;
+ const textRuns = editorDocDataModel?.getBody()?.textRuns;
+
+ const syncId =
+ unitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY
+ ? DOCS_NORMAL_EDITOR_UNIT_ID_KEY
+ : DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY;
+
+ if (dataStream == null || paragraphs == null) {
+ return;
+ }
- this._syncContentAndRender(syncId, dataStream, paragraphs);
+ if (syncId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY) {
+ this._checkAndSetRenderStyleConfig(editorDocDataModel!);
+ this._syncActionsAndRender(syncId, actions);
+ } else {
+ this._syncContentAndRender(syncId, dataStream, paragraphs, textRuns);
+ }
- // handle weather need to show scroll bar.
- this._autoScroll();
+ // handle weather need to show scroll bar.
+ this._autoScroll();
+ }
+ })
+ );
+
+ this.disposeWithMe(
+ this._commandService.onCommandExecuted((command: ICommandInfo) => {
+ if (command.id !== SetEditorResizeOperation.id) {
+ return;
+ }
+
+ const params = command.params as IRichTextEditingMutationParams;
+ const { unitId } = params;
+
+ if (INCLUDE_LIST.includes(unitId)) {
+ // sync cell content to formula editor bar when edit cell editor and vice verse.
+ const editorDocDataModel = this._univerInstanceService.getUniverDocInstance(unitId);
+ const dataStream = editorDocDataModel?.getBody()?.dataStream;
+ const paragraphs = editorDocDataModel?.getBody()?.paragraphs;
+ const textRuns = editorDocDataModel?.getBody()?.textRuns;
+
+ const syncId =
+ unitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY
+ ? DOCS_NORMAL_EDITOR_UNIT_ID_KEY
+ : DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY;
+
+ if (dataStream == null || paragraphs == null) {
+ return;
}
+
+ this._syncContentAndRender(syncId, dataStream, paragraphs, textRuns);
+
+ // handle weather need to show scroll bar.
+ this._autoScroll();
}
})
);
@@ -389,6 +429,43 @@ export class FormulaEditorController extends RxDisposable {
);
}
+ // Sync actions between cell editor and formula editor, and make `dataStream` and `paragraph` is the same.
+ private _syncActionsAndRender(
+ unitId: string,
+ actions: JSONXActions
+ ) {
+ const INCLUDE_LIST = [DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY];
+
+ const docsSkeletonObject = this._docSkeletonManagerService.getSkeletonByUnitId(unitId);
+ const docDataModel = this._univerInstanceService.getUniverDocInstance(unitId);
+ const docViewModel = this._docViewModelManagerService.getViewModel(unitId);
+
+ if (docDataModel == null || docViewModel == null || docsSkeletonObject == null) {
+ return;
+ }
+
+ // Update document data model.
+ docDataModel.apply(actions);
+
+ this._checkAndSetRenderStyleConfig(docDataModel);
+
+ docViewModel.reset(docDataModel);
+
+ const { skeleton } = docsSkeletonObject;
+
+ const currentRender = this._renderManagerService.getRenderById(unitId);
+
+ if (currentRender == null) {
+ return;
+ }
+
+ skeleton.calculate();
+
+ if (INCLUDE_LIST.includes(unitId)) {
+ currentRender.mainComponent?.makeDirty();
+ }
+ }
+
private _syncContentAndRender(
unitId: string,
dataStream: string,
@@ -408,15 +485,12 @@ export class FormulaEditorController extends RxDisposable {
docDataModel.getBody()!.dataStream = dataStream;
docDataModel.getBody()!.paragraphs = this._clearParagraph(paragraphs);
- // Need to empty textRuns(previous formula highlight) every time when sync content(change selection or edit cell or edit formula bar).
- if (unitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) {
- docDataModel.getBody()!.textRuns = [];
- }
-
if (textRuns.length > 0) {
docDataModel.getBody()!.textRuns = textRuns;
}
+ this._checkAndSetRenderStyleConfig(docDataModel);
+
docViewModel.reset(docDataModel);
const { skeleton } = docsSkeletonObject;
@@ -434,6 +508,28 @@ export class FormulaEditorController extends RxDisposable {
}
}
+ private _checkAndSetRenderStyleConfig(documentDataModel: DocumentDataModel) {
+ const snapshot = documentDataModel.getSnapshot();
+ const { body } = snapshot;
+
+ if (snapshot.id !== DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) {
+ return;
+ }
+
+ let renderConfig = snapshot.documentStyle.renderConfig;
+
+ if (renderConfig == null) {
+ renderConfig = {};
+ snapshot.documentStyle.renderConfig = renderConfig;
+ }
+
+ if ((body?.dataStream ?? '').startsWith('=')) {
+ renderConfig.isRenderStyle = BooleanNumber.TRUE;
+ } else {
+ renderConfig.isRenderStyle = BooleanNumber.FALSE;
+ }
+ }
+
private _clearParagraph(paragraphs: IParagraph[]) {
const newParagraphs = Tools.deepClone(paragraphs);
for (const paragraph of newParagraphs) {
diff --git a/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx b/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
index c05fa7aed9..c580770261 100644
--- a/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
+++ b/packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
@@ -15,7 +15,7 @@
*/
import type { Nullable, Workbook } from '@univerjs/core';
-import { DEFAULT_EMPTY_DOCUMENT_VALUE, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, HorizontalAlign, IPermissionService, IUniverInstanceService, Rectangle, ThemeService, UniverInstanceType, VerticalAlign, WrapStrategy } from '@univerjs/core';
+import { BooleanNumber, DEFAULT_EMPTY_DOCUMENT_VALUE, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, HorizontalAlign, IPermissionService, IUniverInstanceService, Rectangle, ThemeService, UniverInstanceType, VerticalAlign, WrapStrategy } from '@univerjs/core';
import { DeviceInputEventType } from '@univerjs/engine-render';
import { CheckMarkSingle, CloseSingle, DropdownSingle, FxSingle } from '@univerjs/icons';
import { KeyCode, ProgressBar, TextEditor } from '@univerjs/ui';
@@ -124,6 +124,7 @@ export function FormulaBar() {
centerAngle: 0,
vertexAngle: 0,
wrapStrategy: WrapStrategy.WRAP,
+ isRenderStyle: BooleanNumber.FALSE,
},
},
};