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(richtext): fix richtext editor formula input

Dushusir47579b27680c
diff --git a/packages/base-sheets/src/Controller/CellEditorController.ts b/packages/base-sheets/src/Controller/CellEditorController.ts
index 219afdd3f6..27ff12a717 100644
--- a/packages/base-sheets/src/Controller/CellEditorController.ts
+++ b/packages/base-sheets/src/Controller/CellEditorController.ts
@@ -269,9 +269,18 @@ export class CellEditorController {
*/
enterEditMode() {
this.focusEditEle();
+ // setTimeout(() => {
+ // this.richTextEditEle.focus();
+
+ // }, 1);
if (this.isEditMode) return;
+ // set focus to last position
+ setTimeout(() => {
+ this.richText.cellInputHandler.setLastCaretPosition();
+ }, 1);
+
this.isEditMode = true;
const currentCell = this._plugin.getSelectionManager().getCurrentModel();
@@ -325,7 +334,7 @@ export class CellEditorController {
});
if (cell) {
- cellValue = cell.value;
+ cellValue = this.richText.cellInputHandler.functionHTMLGenerate(cell.value);
}
this.richText.setValue(cellValue);
@@ -380,7 +389,7 @@ export class CellEditorController {
// If there is no settimeout, the first letter will be intercepted in Chinese state
setTimeout(() => {
this.richTextEditEle.focus();
- }, 1);
+ }, 100);
}
setCurrentEditRangeData() {
diff --git a/packages/base-sheets/src/View/UI/RichText/CellInputHandler.ts b/packages/base-sheets/src/View/UI/RichText/CellInputHandler.ts
index 2e12a29a23..11082361e6 100644
--- a/packages/base-sheets/src/View/UI/RichText/CellInputHandler.ts
+++ b/packages/base-sheets/src/View/UI/RichText/CellInputHandler.ts
@@ -372,6 +372,12 @@ export class CellInputHandler {
}
}
+ setLastCaretPosition() {
+ const range = window.getSelection();
+ range && range.selectAllChildren(this.editor);
+ range && range.collapseToEnd();
+ }
+
functionRange(obj: HTMLElement, v: string, vp: string) {
let _this = this;
diff --git a/packages/base-sheets/src/View/UI/RichText/RichText.tsx b/packages/base-sheets/src/View/UI/RichText/RichText.tsx
index e0f04d5047..cb3dcc5394 100644
--- a/packages/base-sheets/src/View/UI/RichText/RichText.tsx
+++ b/packages/base-sheets/src/View/UI/RichText/RichText.tsx
@@ -2,7 +2,6 @@ import { Component, createRef } from '@univer/base-component';
import { KeyCode } from '@univer/core';
import { CellInputHandler } from './CellInputHandler';
import { CellTextStyle } from './CellTextStyle';
-import { HelpFunction, SearchFunction } from './FormulaPrompt';
import styles from './index.module.less';
// interface IProps {
// style?: {};
@@ -282,9 +281,8 @@ export class RichText extends Component<BaseRichTextProps, IState> {
dangerouslySetInnerHTML={{ __html: text || '' }}
contentEditable
></div>
- {this.state.searchActive && !this.state.helpFormulaActive ? <SearchFunction value={this.state.formula} active={this.state.funIndex} /> : null}
- {!this.state.searchActive && this.state.helpFormulaActive ? <HelpFunction funName={this.state.formulaName} paramIndex={this.state.paramIndex} /> : null}
- {/* <HelpFunction funName={this.state.formulaName} paramIndex={this.state.paramIndex} /> */}
+ {/* {this.state.searchActive && !this.state.helpFormulaActive ? <SearchFunction value={this.state.formula} active={this.state.funIndex} /> : null}
+ {!this.state.searchActive && this.state.helpFormulaActive ? <HelpFunction funName={this.state.formulaName} paramIndex={this.state.paramIndex} /> : null} */}
</div>
);
}
diff --git a/packages/core/src/Shared/Common.ts b/packages/core/src/Shared/Common.ts
index d858c67daf..78099e4673 100644
--- a/packages/core/src/Shared/Common.ts
+++ b/packages/core/src/Shared/Common.ts
@@ -76,5 +76,5 @@ export function getColorStyle(color: Nullable<IColorStyle>): Nullable<string> {
}
export function isFormulaString(value: any): boolean {
- return Tools.isString(value) && value.indexOf('=') === 0;
+ return Tools.isString(value) && value.indexOf('=') === 0 && value.length > 1;
}
diff --git a/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts b/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts
index 32ee27a747..4d111c2269 100644
--- a/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts
+++ b/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts
@@ -32,7 +32,7 @@ export class FormulaActionExtension extends BaseActionExtension<ISetRangeDataAct
const cellData = new ObjectMatrix(formulaData[unitId][sheetId]);
- // a range
+ // a range TODO api will trigger here
if (!isNaN(parseInt(Object.keys(cellValue)[0]))) {
const rangeMatrix = new ObjectMatrix(cellValue as ObjectMatrixPrimitiveType<ICellData>);
@@ -74,13 +74,17 @@ export class FormulaActionExtension extends BaseActionExtension<ISetRangeDataAct
// cell.t = 1;
engine.execute(unitId, formulaData, formulaController.toInterpreterCalculateProps(), true, unitRange).then((sheetData) => {
- const cellCalculate = sheetData[sheetId].getValue(r, c);
+ const cellCalculate = sheetData[sheetId].getValue(r, c) as ICellData;
// cell.v = cellCalculate?.v;
// cell.t = cellCalculate?.t;
if (cellCalculate && cellCalculate.p) {
delete cellCalculate.p;
}
+ if (cellCalculate && Number.isNaN(cellCalculate.v)) {
+ cellCalculate.v = 0;
+ }
+
const action: ISetRangeDataActionData = {
actionName: ACTION_NAMES.SET_RANGE_DATA_ACTION,
sheetId,
@@ -90,11 +94,7 @@ export class FormulaActionExtension extends BaseActionExtension<ISetRangeDataAct
startRow: r,
endRow: r,
},
- cellValue: {
- [r]: {
- [c]: cellCalculate,
- },
- },
+ cellValue: cellCalculate,
};
const setFormulaDataAction = {