From 22491f0cfa1c950150b7561e08bcc13dcfcba65c Mon Sep 17 00:00:00 2001 From: zhangw Date: Thu, 10 Oct 2024 20:04:57 +0800 Subject: [PATCH 01/15] fix: close https://github.com/dream-num/univer-pro/issues/2780 --- .../editor/editing.render-controller.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts index 923663a4434..89ddd68a9db 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -68,6 +68,7 @@ import { distinctUntilChanged, filter } from 'rxjs'; import { getEditorObject } from '../../basics/editor/get-editor-object'; import { MoveSelectionCommand, MoveSelectionEnterAndTabCommand } from '../../commands/commands/set-selection.command'; import { SetCellEditVisibleArrowOperation, SetCellEditVisibleOperation, SetCellEditVisibleWithF2Operation } from '../../commands/operations/cell-edit.operation'; +import { ScrollToRangeOperation } from '../../commands/operations/scroll-to-range.operation'; import { ICellEditorManagerService } from '../../services/editor/cell-editor-manager.service'; import { IEditorBridgeService } from '../../services/editor-bridge.service'; import styles from '../../views/sheet-container/index.module.less'; @@ -564,9 +565,22 @@ export class EditingRenderController extends Disposable implements IRenderModule ? CursorChange.CursorChange : CursorChange.StartEditor; - this._editorBridgeService.refreshEditCellState(); + let editCellState = this._editorBridgeService.getEditCellState(); + if (editCellState == null) { + return; + } - const editCellState = this._editorBridgeService.getEditCellState(); + this._commandService.syncExecuteCommand(ScrollToRangeOperation.id, { + range: { + startRow: editCellState.row, + startColumn: editCellState.column, + endRow: editCellState.row, + endColumn: editCellState.column, + }, + }); + + this._editorBridgeService.refreshEditCellState(); + editCellState = this._editorBridgeService.getEditCellState(); if (editCellState == null) { return; } From 805ca034335a819678622010fa146b8de876c083 Mon Sep 17 00:00:00 2001 From: zhangw Date: Thu, 10 Oct 2024 23:43:49 +0800 Subject: [PATCH 02/15] feat: update --- .../src/commands/operations/scroll-to-range.operation.ts | 7 +++++-- .../render-controllers/scroll.render-controller.ts | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/sheets-ui/src/commands/operations/scroll-to-range.operation.ts b/packages/sheets-ui/src/commands/operations/scroll-to-range.operation.ts index 161d0d0c23b..a45c1ec4d51 100644 --- a/packages/sheets-ui/src/commands/operations/scroll-to-range.operation.ts +++ b/packages/sheets-ui/src/commands/operations/scroll-to-range.operation.ts @@ -14,21 +14,24 @@ * limitations under the License. */ +import type { IScrollToCellCommandParams } from '../commands/set-scroll.command'; import { CommandType, type ICommand, IUniverInstanceService, UniverInstanceType } from '@univerjs/core'; import { IRenderManagerService } from '@univerjs/engine-render'; -import type { IScrollToCellCommandParams } from '../commands/set-scroll.command'; import { SheetsScrollRenderController } from '../../controllers/render-controllers/scroll.render-controller'; export const ScrollToRangeOperation: ICommand = { id: 'sheet.operation.scroll-to-range', type: CommandType.OPERATION, handler: (accessor, params) => { + if (!params) { + return false; + } const instanceService = accessor.get(IUniverInstanceService); const renderManagerService = accessor.get(IRenderManagerService); const scrollController = renderManagerService .getRenderById(instanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET)!.getUnitId())! .with(SheetsScrollRenderController); - return scrollController.scrollToRange(params!.range); + return scrollController.scrollToRange(params.range); }, }; diff --git a/packages/sheets-ui/src/controllers/render-controllers/scroll.render-controller.ts b/packages/sheets-ui/src/controllers/render-controllers/scroll.render-controller.ts index b8859072422..40e94c56a44 100644 --- a/packages/sheets-ui/src/controllers/render-controllers/scroll.render-controller.ts +++ b/packages/sheets-ui/src/controllers/render-controllers/scroll.render-controller.ts @@ -556,6 +556,10 @@ export class SheetsScrollRenderController extends Disposable implements IRenderM if (startSheetViewRow === undefined && startSheetViewColumn === undefined) return false; const { offsetX, offsetY } = this._scrollManagerService.getCurrentScrollState() || {}; + + startSheetViewRow = startSheetViewRow ? Math.min(startSheetViewRow, row) : startSheetViewRow; + startSheetViewColumn = startSheetViewColumn ? Math.min(startSheetViewColumn, column) : startSheetViewColumn; + return this._commandService.syncExecuteCommand(ScrollCommand.id, { sheetViewStartRow: startSheetViewRow, sheetViewStartColumn: startSheetViewColumn, From 0173437c85e3e6dfdb7c55c7f489eb7d3f4a6c63 Mon Sep 17 00:00:00 2001 From: zhangw Date: Fri, 11 Oct 2024 00:39:33 +0800 Subject: [PATCH 03/15] fix: create unit error show editor --- .../controllers/editor/editing.render-controller.ts | 4 ++++ .../src/views/editor-container/EditorContainer.tsx | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts index 89ddd68a9db..4a553a7c267 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -789,6 +789,10 @@ export class EditingRenderController extends Disposable implements IRenderModule const { position, documentLayoutObject, canvasOffset, scaleX, scaleY } = param; + if (!this._editorBridgeService.isVisible().visible) { + return; + } + this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject, scaleX, scaleY); } })); diff --git a/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx b/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx index e53ced3615f..5a473267877 100644 --- a/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx +++ b/packages/sheets-ui/src/views/editor-container/EditorContainer.tsx @@ -14,13 +14,13 @@ * limitations under the License. */ +import type { IDocumentData } from '@univerjs/core'; import { DEFAULT_EMPTY_DOCUMENT_VALUE, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DocumentFlavor, IContextService, useDependency } from '@univerjs/core'; import { IEditorService, TextEditor } from '@univerjs/docs-ui'; -import { FIX_ONE_PIXEL_BLUR_OFFSET } from '@univerjs/engine-render'; +import { FIX_ONE_PIXEL_BLUR_OFFSET } from '@univerjs/engine-render'; import { DISABLE_AUTO_FOCUS_KEY, useObservable } from '@univerjs/ui'; import React, { useEffect, useState } from 'react'; -import type { IDocumentData } from '@univerjs/core'; import { ICellEditorManagerService } from '../../services/editor/cell-editor-manager.service'; import styles from './index.module.less'; @@ -43,7 +43,6 @@ export const EditorContainer: React.FC = () => { const [state, setState] = useState({ ...EDITOR_DEFAULT_POSITION, }); - const cellEditorManagerService = useDependency(ICellEditorManagerService); const editorService = useDependency(IEditorService); const contextService = useDependency(IContextService); @@ -79,7 +78,7 @@ export const EditorContainer: React.FC = () => { }; useEffect(() => { - cellEditorManagerService.state$.subscribe((param) => { + const sub = cellEditorManagerService.state$.subscribe((param) => { if (param == null) { return; } @@ -115,6 +114,9 @@ export const EditorContainer: React.FC = () => { cellEditorManagerService.setRect({ left, top, width, height }); } }); + return () => { + sub.unsubscribe(); + }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); // Empty dependency array means this effect runs once on mount and clean up on unmount From c50742c5c0aed457d776cd54297f25b9b361bafa Mon Sep 17 00:00:00 2001 From: zhangw Date: Fri, 11 Oct 2024 15:46:15 +0800 Subject: [PATCH 04/15] fix: editor resize --- .../editor/editing.render-controller.ts | 118 +++++++++++++++--- .../editor-bridge.render-controller.ts | 17 ++- .../src/services/editor-bridge.service.ts | 101 ++++++++------- 3 files changed, 161 insertions(+), 75 deletions(-) diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts index 4a553a7c267..fac56661e74 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -23,7 +23,7 @@ import type { DocumentSkeleton, IDocumentLayoutObject, IRenderContext, IRenderMo import type { WorkbookSelections } from '@univerjs/sheets'; import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service'; import { - CellValueType, DEFAULT_EMPTY_DOCUMENT_VALUE, Direction, Disposable, DisposableCollection, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, EDITOR_ACTIVATED, + CellValueType, DEFAULT_EMPTY_DOCUMENT_VALUE, Direction, Disposable, DisposableCollection, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, EDITOR_ACTIVATED, FOCUSING_EDITOR_BUT_HIDDEN, FOCUSING_EDITOR_INPUT_FORMULA, FOCUSING_EDITOR_STANDALONE, @@ -62,7 +62,7 @@ import { ScrollBar, } from '@univerjs/engine-render'; -import { ClearSelectionFormatCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetSelectionsOperation, SetWorksheetActivateCommand, SheetsSelectionsService } from '@univerjs/sheets'; +import { ClearSelectionFormatCommand, COMMAND_LISTENER_SKELETON_CHANGE, SetRangeValuesCommand, SetRangeValuesMutation, SetSelectionsOperation, SetWorksheetActivateCommand, SheetsSelectionsService } from '@univerjs/sheets'; import { ILayoutService, KeyCode, SetEditorResizeOperation } from '@univerjs/ui'; import { distinctUntilChanged, filter } from 'rxjs'; import { getEditorObject } from '../../basics/editor/get-editor-object'; @@ -71,6 +71,7 @@ import { SetCellEditVisibleArrowOperation, SetCellEditVisibleOperation, SetCellE import { ScrollToRangeOperation } from '../../commands/operations/scroll-to-range.operation'; import { ICellEditorManagerService } from '../../services/editor/cell-editor-manager.service'; import { IEditorBridgeService } from '../../services/editor-bridge.service'; +import { SheetSkeletonManagerService } from '../../services/sheet-skeleton-manager.service'; import styles from '../../views/sheet-container/index.module.less'; import { MOVE_SELECTION_KEYCODE_LIST } from '../shortcuts/editor.shortcut'; import { extractStringFromForceString, isForceString } from '../utils/cell-tools'; @@ -122,7 +123,8 @@ export class EditingRenderController extends Disposable implements IRenderModule @Inject(DocSelectionManagerService) private readonly _textSelectionManagerService: DocSelectionManagerService, @ICommandService private readonly _commandService: ICommandService, @Inject(LocaleService) protected readonly _localService: LocaleService, - @IEditorService private readonly _editorService: IEditorService + @IEditorService private readonly _editorService: IEditorService, + @Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService ) { super(); @@ -171,6 +173,7 @@ export class EditingRenderController extends Disposable implements IRenderModule this._initialCursorSync(d); this._listenEditorFocus(d); this._commandExecutedListener(d); + this._initSkeletonListener(d); this.disposeWithMe(this._instanceSrv.unitDisposed$.subscribe((_unit: UnitModel) => { clearTimeout(this._cursorTimeout); @@ -240,6 +243,47 @@ export class EditingRenderController extends Disposable implements IRenderModule })); } + private _initSkeletonListener(d: DisposableCollection) { + const commandList = new Set(COMMAND_LISTENER_SKELETON_CHANGE); + d.add(this._commandService.onCommandExecuted((commandInfo) => { + if (!commandList.has(commandInfo.id)) { + return; + } + this.resizeCellEditor(); + })); + } + + resizeCellEditor() { + const state = this._cellEditorManagerService.getState(); + const skeleton = this._sheetSkeletonManagerService.getCurrent()?.skeleton; + if (!skeleton) return; + if (!state) return; + if (!this._editorBridgeService.isVisible().visible) return; + const latestEditCellState = this._editorBridgeService.getLatestEditCellState(true); + if (!latestEditCellState) return; + + const { row, column, scaleX, scaleY, position, canvasOffset, documentLayoutObject } = latestEditCellState; + const maxSize = this._getEditorMaxSize(position, canvasOffset); + if (!maxSize) return; + const { height: clientHeight, width: clientWidth, scaleAdjust } = maxSize; + + const cell = skeleton.getCellByIndex(row, column); + const height = Math.min((cell.mergeInfo.endY - cell.mergeInfo.startY) * scaleY, clientHeight) * scaleAdjust; + const width = Math.min((cell.mergeInfo.endX - cell.mergeInfo.startX) * scaleX, clientWidth) * scaleAdjust; + const currentHeight = state.endY! - state.startY!; + const currentWidth = state.endX! - state.startX!; + + if (currentHeight !== height || currentWidth !== width) { + this._editorBridgeService.refreshEditCellState(true); + + const skeleton = this._getEditorSkeleton(DOCS_NORMAL_EDITOR_UNIT_ID_KEY); + if (!skeleton) { + return; + } + this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject, scaleX, scaleY); + } + } + /** * Should update current editing cell info when selection is changed. * @param d DisposableCollection @@ -260,6 +304,10 @@ export class EditingRenderController extends Disposable implements IRenderModule return; } + if (this._instanceSrv.getUnit(DOCS_NORMAL_EDITOR_UNIT_ID_KEY) === documentLayoutObject.documentModel) { + return; + } + const { startX, endX } = position; const { textRotation, wrapStrategy, documentModel } = documentLayoutObject; const { vertexAngle: angle } = convertTextRotation(textRotation); @@ -406,6 +454,42 @@ export class EditingRenderController extends Disposable implements IRenderModule }; } + private _getEditorMaxSize(position: IPosition, canvasOffset: ICanvasOffset) { + const editorObject = this._getEditorObject(); + if (editorObject == null) { + return; + } + function pxToNum(width: string): number { + return Number.parseInt(width.replace('px', '')); + } + + const engine = this._context.engine; + const canvasElement = engine.getCanvasElement(); + const canvasClientRect = canvasElement.getBoundingClientRect(); + + // We should take the scale into account when canvas is scaled by CSS. + const widthOfCanvas = pxToNum(canvasElement.style.width); // declared width + const { width } = canvasClientRect; // real width affected by scale + const scaleAdjust = width / widthOfCanvas; + + const { startX, startY } = position; + + const clientHeight = + document.body.clientHeight - + startY - + Number.parseFloat(styles.sheetFooterBarHeight) - + canvasOffset.top - + EDITOR_BORDER_SIZE * 2; + + const clientWidth = document.body.clientWidth - startX - canvasOffset.left; + + return { + height: clientHeight, + width: clientWidth, + scaleAdjust, + }; + } + /** * Mainly used to calculate the volume of scenes and objects, * determine whether a scrollbar appears, @@ -418,38 +502,27 @@ export class EditingRenderController extends Disposable implements IRenderModule canvasOffset: ICanvasOffset, fill: Nullable, scaleX: number = 1, - scaleY: number = 1 + scaleY: number = 1, + callback?: () => void ) { const editorObject = this._getEditorObject(); if (editorObject == null) { return; } - function pxToNum(width: string): number { - return Number.parseInt(width.replace('px', '')); - } - const engine = this._context.engine; const canvasElement = engine.getCanvasElement(); - const canvasClientRect = canvasElement.getBoundingClientRect(); // We should take the scale into account when canvas is scaled by CSS. - const widthOfCanvas = pxToNum(canvasElement.style.width); // declared width - const { top, left, width } = canvasClientRect; // real width affected by scale - const scaleAdjust = width / widthOfCanvas; let { startX, startY } = actualRangeWithCoord; const { document: documentComponent, scene: editorScene, engine: docEngine } = editorObject; const viewportMain = editorScene.getViewport(DOC_VIEWPORT_KEY.VIEW_MAIN); - const clientHeight = - document.body.clientHeight - - startY - - Number.parseFloat(styles.sheetFooterBarHeight) - - canvasOffset.top - - EDITOR_BORDER_SIZE * 2; - const clientWidth = document.body.clientWidth - startX - canvasOffset.left; + const info = this._getEditorMaxSize(actualRangeWithCoord, canvasOffset); + if (!info) return; + const { height: clientHeight, width: clientWidth, scaleAdjust } = info; let physicHeight = editorHeight; @@ -500,6 +573,12 @@ export class EditingRenderController extends Disposable implements IRenderModule fixLineWidthByScale(editorWidth, precisionScaleX), fixLineWidthByScale(physicHeight, precisionScaleY) ); + + this._textSelectionManagerService.refreshSelection({ + unitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY, + subUnitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY, + }); + callback?.(); }, 0); const contentBoundingRect = this._layoutService.getContentElement().getBoundingClientRect(); @@ -990,7 +1069,6 @@ export class EditingRenderController extends Disposable implements IRenderModule this._contextService.setContextValue(EDITOR_ACTIVATED, false); this._contextService.setContextValue(FOCUSING_EDITOR_BUT_HIDDEN, false); this._contextService.setContextValue(FOCUSING_FX_BAR_EDITOR, false); - this._cellEditorManagerService.setState({ show: param.visible, }); diff --git a/packages/sheets-ui/src/controllers/render-controllers/editor-bridge.render-controller.ts b/packages/sheets-ui/src/controllers/render-controllers/editor-bridge.render-controller.ts index f45be730079..5aa5a84d0d1 100644 --- a/packages/sheets-ui/src/controllers/render-controllers/editor-bridge.render-controller.ts +++ b/packages/sheets-ui/src/controllers/render-controllers/editor-bridge.render-controller.ts @@ -14,24 +14,23 @@ * limitations under the License. */ +import type { ICommandInfo, IDisposable, IExecutionOptions, Nullable, Workbook } from '@univerjs/core'; +import type { IRenderContext, IRenderModule } from '@univerjs/engine-render'; +import type { ISelectionWithStyle } from '@univerjs/sheets'; +import type { ICurrentEditCellParam, IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service'; import { DisposableCollection, ICommandService, Inject, IUniverInstanceService, RxDisposable, UniverInstanceType } from '@univerjs/core'; import { IRangeSelectorService } from '@univerjs/docs-ui'; import { DeviceInputEventType } from '@univerjs/engine-render'; import { - COMMAND_LISTENER_SKELETON_CHANGE, SetWorksheetActiveOperation, SheetsSelectionsService, } from '@univerjs/sheets'; import { merge } from 'rxjs'; -import type { ICommandInfo, IDisposable, IExecutionOptions, Nullable, Workbook } from '@univerjs/core'; -import type { IRenderContext, IRenderModule } from '@univerjs/engine-render'; -import type { ISelectionWithStyle } from '@univerjs/sheets'; import { SetZoomRatioCommand } from '../../commands/commands/set-zoom-ratio.command'; import { SetActivateCellEditOperation } from '../../commands/operations/activate-cell-edit.operation'; import { SetCellEditVisibleOperation } from '../../commands/operations/cell-edit.operation'; import { IEditorBridgeService } from '../../services/editor-bridge.service'; import { getSheetObject } from '../utils/component-tools'; -import type { ICurrentEditCellParam, IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service'; // TODO: wzhudev: this should be merged with Edit EditingRenderController. @@ -207,10 +206,10 @@ export class EditorBridgeRenderController extends RxDisposable implements IRende this._editorBridgeService.refreshEditCellState(); } - if (options?.fromCollab) return; - if (command.id !== SetWorksheetActiveOperation.id && COMMAND_LISTENER_SKELETON_CHANGE.includes(command.id)) { - this._hideEditor(); - } + // if (options?.fromCollab) return; + // if (command.id !== SetWorksheetActiveOperation.id && COMMAND_LISTENER_SKELETON_CHANGE.includes(command.id)) { + // this._hideEditor(); + // } })); d.add(this._commandService.beforeCommandExecuted((command: ICommandInfo, options?: IExecutionOptions) => { diff --git a/packages/sheets-ui/src/services/editor-bridge.service.ts b/packages/sheets-ui/src/services/editor-bridge.service.ts index 2c30b3079f1..2381cec931c 100644 --- a/packages/sheets-ui/src/services/editor-bridge.service.ts +++ b/packages/sheets-ui/src/services/editor-bridge.service.ts @@ -14,6 +14,11 @@ * limitations under the License. */ +import type { ICellData, ICellDataForSheetInterceptor, IDisposable, IPosition, ISelectionCell, Nullable, Workbook } from '@univerjs/core'; +import type { Engine, IDocumentLayoutObject, Scene } from '@univerjs/engine-render'; +import type { ISheetLocation, SheetsSelectionsService } from '@univerjs/sheets'; +import type { KeyCode } from '@univerjs/ui'; +import type { Observable } from 'rxjs'; import { CellValueType, createIdentifier, @@ -35,13 +40,8 @@ import { import { getCanvasOffsetByEngine, IEditorService } from '@univerjs/docs-ui'; import { convertTextRotation, DeviceInputEventType, IRenderManagerService } from '@univerjs/engine-render'; import { IRefSelectionsService } from '@univerjs/sheets'; -import { BehaviorSubject } from 'rxjs'; -import type { ICellData, ICellDataForSheetInterceptor, IDisposable, IPosition, ISelectionCell, Nullable, Workbook } from '@univerjs/core'; -import type { Engine, IDocumentLayoutObject, Scene } from '@univerjs/engine-render'; -import type { ISheetLocation, SheetsSelectionsService } from '@univerjs/sheets'; -import type { KeyCode } from '@univerjs/ui'; -import type { Observable } from 'rxjs'; +import { BehaviorSubject } from 'rxjs'; import { ISheetSelectionRenderService } from './selection/base-selection-render.service'; import { attachPrimaryWithCoord } from './selection/util'; import { SheetSkeletonManagerService } from './sheet-skeleton-manager.service'; @@ -92,11 +92,11 @@ export interface IEditorBridgeService { AFTER_CELL_EDIT_ASYNC: typeof AFTER_CELL_EDIT_ASYNC; }>; dispose(): void; - refreshEditCellState(): void; + refreshEditCellState(resetSizeOnly?: boolean): void; setEditCell(param: ICurrentEditCellParam): void; getEditCellState(): Readonly>; // Gets the DocumentDataModel of the latest table cell based on the latest cell contents - getLatestEditCellState(): Readonly>; + getLatestEditCellState(resetSizeOnly?: boolean): Readonly>; changeVisible(param: IEditorBridgeServiceVisibleParam): void; changeEditorDirty(dirtyStatus: boolean): void; getEditorDirty(): boolean; @@ -174,8 +174,8 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ }); } - refreshEditCellState() { - const editCellState = this.getLatestEditCellState(); + refreshEditCellState(resetSizeOnly?: boolean) { + const editCellState = this.getLatestEditCellState(resetSizeOnly); this._currentEditCellState = editCellState; this._currentEditCellState$.next(editCellState); @@ -213,8 +213,8 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ return this._currentEditCellState; } - // eslint-disable-next-line max-lines-per-function - getLatestEditCellState() { + // eslint-disable-next-line max-lines-per-function, complexity + getLatestEditCellState(resetSizeOnly?: boolean) { const currentEditCell = this._currentEditCell; if (currentEditCell == null) { return; @@ -261,49 +261,58 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ origin: worksheet.getCellRaw(startRow, startColumn), }; + let documentLayoutObject: Nullable; const cell = this.interceptor.fetchThroughInterceptors(this.interceptor.getInterceptPoints().BEFORE_CELL_EDIT)( worksheet.getCell(startRow, startColumn), location ); - let documentLayoutObject = cell && skeleton.getCellDocumentModelWithFormula(cell); - - // Rewrite the cellValueType to STRING to avoid render the value on the right side when number type. - const renderConfig = documentLayoutObject?.documentModel?.documentStyle.renderConfig; - if (renderConfig != null) { - renderConfig.cellValueType = CellValueType.STRING; - } - - if (!documentLayoutObject || documentLayoutObject.documentModel == null) { - const blankModel = skeleton.getBlankCellDocumentModel(cell); + if (resetSizeOnly && this._currentEditCellState) { + endX = endX - startX + this._currentEditCellState.position.startX; + endY = endY - startY + this._currentEditCellState.position.startY; + startX = this._currentEditCellState.position.startX; + startY = this._currentEditCellState.position.startY; + documentLayoutObject = this._currentEditCellState.documentLayoutObject; + } else { + documentLayoutObject = cell && skeleton.getCellDocumentModelWithFormula(cell); + + // Rewrite the cellValueType to STRING to avoid render the value on the right side when number type. + const renderConfig = documentLayoutObject?.documentModel?.documentStyle.renderConfig; + if (renderConfig != null) { + renderConfig.cellValueType = CellValueType.STRING; + } - if (documentLayoutObject != null) { - const { verticalAlign, horizontalAlign, wrapStrategy, textRotation, fill } = documentLayoutObject; - const { centerAngle, vertexAngle } = convertTextRotation(textRotation); - blankModel.documentModel!.documentStyle.renderConfig = { - verticalAlign, horizontalAlign, wrapStrategy, background: { rgb: fill }, centerAngle, vertexAngle, - }; + if (!documentLayoutObject || documentLayoutObject.documentModel == null) { + const blankModel = skeleton.getBlankCellDocumentModel(cell); + + if (documentLayoutObject != null) { + const { verticalAlign, horizontalAlign, wrapStrategy, textRotation, fill } = documentLayoutObject; + const { centerAngle, vertexAngle } = convertTextRotation(textRotation); + blankModel.documentModel!.documentStyle.renderConfig = { + verticalAlign, horizontalAlign, wrapStrategy, background: { rgb: fill }, centerAngle, vertexAngle, + }; + } + documentLayoutObject = blankModel; } - documentLayoutObject = blankModel; - } - // background of canvas is set to transparent, so if no bgcolor sepcified in curr cell, set it to white. - documentLayoutObject.fill = documentLayoutObject.fill || '#fff'; - documentLayoutObject.documentModel?.setZoomRatio(Math.max(scaleX, scaleY)); - - if (cell?.isInArrayFormulaRange === true) { - const body = documentLayoutObject.documentModel?.getBody(); - if (body) { - body.textRuns = [ - { - st: 0, - ed: body.dataStream.length - 2, - ts: { - cl: { - rgb: this._themeService.getCurrentTheme().textColorSecondary, + // background of canvas is set to transparent, so if no bgcolor sepcified in curr cell, set it to white. + documentLayoutObject.fill = documentLayoutObject.fill || '#fff'; + documentLayoutObject.documentModel?.setZoomRatio(Math.max(scaleX, scaleY)); + + if (cell?.isInArrayFormulaRange === true) { + const body = documentLayoutObject.documentModel?.getBody(); + if (body) { + body.textRuns = [ + { + st: 0, + ed: body.dataStream.length - 2, + ts: { + cl: { + rgb: this._themeService.getCurrentTheme().textColorSecondary, + }, }, }, - }, - ]; + ]; + } } } From 3fab1a04c372e1d8c661dbb8a85024c646247f71 Mon Sep 17 00:00:00 2001 From: zhangw Date: Fri, 11 Oct 2024 17:37:30 +0800 Subject: [PATCH 05/15] fix: update --- packages/sheets-ui/src/services/editor-bridge.service.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/sheets-ui/src/services/editor-bridge.service.ts b/packages/sheets-ui/src/services/editor-bridge.service.ts index 2381cec931c..52827b2b20d 100644 --- a/packages/sheets-ui/src/services/editor-bridge.service.ts +++ b/packages/sheets-ui/src/services/editor-bridge.service.ts @@ -272,6 +272,14 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ endY = endY - startY + this._currentEditCellState.position.startY; startX = this._currentEditCellState.position.startX; startY = this._currentEditCellState.position.startY; + } + + if (this._currentEditCellState && + this._currentEditCellState.unitId === location.unitId && + this._currentEditCellState.sheetId === location.subUnitId && + this._currentEditCellState.row === location.row && + this._currentEditCellState.column === location.col + ) { documentLayoutObject = this._currentEditCellState.documentLayoutObject; } else { documentLayoutObject = cell && skeleton.getCellDocumentModelWithFormula(cell); From 532cc76deee0f4a08af45cd3bd4479a8008dae39 Mon Sep 17 00:00:00 2001 From: zhangw Date: Fri, 11 Oct 2024 19:35:07 +0800 Subject: [PATCH 06/15] fix: update --- .../src/controllers/editor/editing.render-controller.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts index fac56661e74..39d6846d723 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -574,11 +574,18 @@ export class EditingRenderController extends Disposable implements IRenderModule fixLineWidthByScale(physicHeight, precisionScaleY) ); + callback?.(); + if ( + this._contextService.getContextValue(FOCUSING_EDITOR_STANDALONE) || + this._contextService.getContextValue(FOCUSING_UNIVER_EDITOR_STANDALONE_SINGLE_MODE) + ) { + return; + } + this._textSelectionManagerService.refreshSelection({ unitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY, subUnitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY, }); - callback?.(); }, 0); const contentBoundingRect = this._layoutService.getContentElement().getBoundingClientRect(); From c0b1f41dd764b96a090a5e5e30d011c6e226caa7 Mon Sep 17 00:00:00 2001 From: zhangw Date: Fri, 11 Oct 2024 19:38:35 +0800 Subject: [PATCH 07/15] feat: update --- .../src/controllers/editor/editing.render-controller.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts index 39d6846d723..63706d013a0 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -575,10 +575,7 @@ export class EditingRenderController extends Disposable implements IRenderModule ); callback?.(); - if ( - this._contextService.getContextValue(FOCUSING_EDITOR_STANDALONE) || - this._contextService.getContextValue(FOCUSING_UNIVER_EDITOR_STANDALONE_SINGLE_MODE) - ) { + if (this._contextService.getContextValue(FOCUSING_FX_BAR_EDITOR)) { return; } From 636dbc8f0c35a4f5f2bb3871d86363f0b2c92c76 Mon Sep 17 00:00:00 2001 From: zhangw Date: Fri, 11 Oct 2024 20:00:26 +0800 Subject: [PATCH 08/15] fix: update --- .../editor/editing.render-controller.ts | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts index 63706d013a0..408a43505db 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -280,7 +280,12 @@ export class EditingRenderController extends Disposable implements IRenderModule if (!skeleton) { return; } - this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject, scaleX, scaleY); + this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject, scaleX, scaleY, () => { + this._textSelectionManagerService.refreshSelection({ + unitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY, + subUnitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY, + }); + }); } } @@ -338,7 +343,8 @@ export class EditingRenderController extends Disposable implements IRenderModule documentSkeleton: DocumentSkeleton, documentLayoutObject: IDocumentLayoutObject, scaleX: number = 1, - scaleY: number = 1 + scaleY: number = 1, + callback?: () => void ) { const { startX, startY, endX, endY } = actualRangeWithCoord; const documentDataModel = documentLayoutObject.documentModel; @@ -391,7 +397,7 @@ export class EditingRenderController extends Disposable implements IRenderModule // re-calculate skeleton(viewModel for component) documentSkeleton.calculate(); - this._editAreaProcessing(editorWidth, editorHeight, actualRangeWithCoord, canvasOffset, fill, scaleX, scaleY); + this._editAreaProcessing(editorWidth, editorHeight, actualRangeWithCoord, canvasOffset, fill, scaleX, scaleY, callback); } /** @@ -575,14 +581,6 @@ export class EditingRenderController extends Disposable implements IRenderModule ); callback?.(); - if (this._contextService.getContextValue(FOCUSING_FX_BAR_EDITOR)) { - return; - } - - this._textSelectionManagerService.refreshSelection({ - unitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY, - subUnitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY, - }); }, 0); const contentBoundingRect = this._layoutService.getContentElement().getBoundingClientRect(); From 820ff6893fd40678ff4cd4624cbae80cd8b711c1 Mon Sep 17 00:00:00 2001 From: zhangw Date: Fri, 11 Oct 2024 21:00:53 +0800 Subject: [PATCH 09/15] feat: update --- .../editor/editing.render-controller.ts | 2 +- .../src/services/editor-bridge.service.ts | 22 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts index 408a43505db..0cde36b9fe7 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -660,7 +660,7 @@ export class EditingRenderController extends Disposable implements IRenderModule }, }); - this._editorBridgeService.refreshEditCellState(); + this._editorBridgeService.refreshEditCellState(false, true); editCellState = this._editorBridgeService.getEditCellState(); if (editCellState == null) { return; diff --git a/packages/sheets-ui/src/services/editor-bridge.service.ts b/packages/sheets-ui/src/services/editor-bridge.service.ts index 52827b2b20d..97c0b58e2f0 100644 --- a/packages/sheets-ui/src/services/editor-bridge.service.ts +++ b/packages/sheets-ui/src/services/editor-bridge.service.ts @@ -92,11 +92,11 @@ export interface IEditorBridgeService { AFTER_CELL_EDIT_ASYNC: typeof AFTER_CELL_EDIT_ASYNC; }>; dispose(): void; - refreshEditCellState(resetSizeOnly?: boolean): void; + refreshEditCellState(resetSizeOnly?: boolean, tryKeepLastObject?: boolean): void; setEditCell(param: ICurrentEditCellParam): void; getEditCellState(): Readonly>; // Gets the DocumentDataModel of the latest table cell based on the latest cell contents - getLatestEditCellState(resetSizeOnly?: boolean): Readonly>; + getLatestEditCellState(resetSizeOnly?: boolean, tryKeepLastObject?: boolean): Readonly>; changeVisible(param: IEditorBridgeServiceVisibleParam): void; changeEditorDirty(dirtyStatus: boolean): void; getEditorDirty(): boolean; @@ -122,6 +122,8 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ private _currentEditCell: Nullable = null; private _currentEditCellState: Nullable = null; + + // TODO: @weird94 this should split into to subjects, documentDataModel & position private readonly _currentEditCellState$ = new BehaviorSubject>(null); readonly currentEditCellState$ = this._currentEditCellState$.asObservable(); @@ -174,8 +176,8 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ }); } - refreshEditCellState(resetSizeOnly?: boolean) { - const editCellState = this.getLatestEditCellState(resetSizeOnly); + refreshEditCellState(resetSizeOnly?: boolean, tryKeepLastObject?: boolean) { + const editCellState = this.getLatestEditCellState(resetSizeOnly, tryKeepLastObject); this._currentEditCellState = editCellState; this._currentEditCellState$.next(editCellState); @@ -214,7 +216,7 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ } // eslint-disable-next-line max-lines-per-function, complexity - getLatestEditCellState(resetSizeOnly?: boolean) { + getLatestEditCellState(resetSizeOnly?: boolean, tryKeepLastObject?: boolean) { const currentEditCell = this._currentEditCell; if (currentEditCell == null) { return; @@ -274,11 +276,13 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ startY = this._currentEditCellState.position.startY; } - if (this._currentEditCellState && + if ( + this._currentEditCellState && + tryKeepLastObject && this._currentEditCellState.unitId === location.unitId && - this._currentEditCellState.sheetId === location.subUnitId && - this._currentEditCellState.row === location.row && - this._currentEditCellState.column === location.col + this._currentEditCellState.sheetId === location.subUnitId && + this._currentEditCellState.row === location.row && + this._currentEditCellState.column === location.col ) { documentLayoutObject = this._currentEditCellState.documentLayoutObject; } else { From 8eb1ce54aa298f728c67eb750cd5e228e2a284d8 Mon Sep 17 00:00:00 2001 From: zhangw Date: Fri, 11 Oct 2024 23:22:52 +0800 Subject: [PATCH 10/15] fix: editor size recalc --- .../controllers/editor/editing.render-controller.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts index 0cde36b9fe7..62f59d3cbf0 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -874,7 +874,15 @@ export class EditingRenderController extends Disposable implements IRenderModule return; } - this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject, scaleX, scaleY); + if (commandUnitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) { + // FIXME: this should be fixed on next refactor + // currently, we need to wait util content was synced to cell-editor, than fit size + setTimeout(() => { + this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject, scaleX, scaleY); + }, 0); + } else { + this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject, scaleX, scaleY); + } } })); From 8d44558107e0add11d51c6df538a2b90294f49b6 Mon Sep 17 00:00:00 2001 From: zhangw Date: Sat, 12 Oct 2024 01:29:53 +0800 Subject: [PATCH 11/15] feat: update --- .../editor/editing.render-controller.ts | 15 +- .../src/services/editor-bridge.service.ts | 157 ++++++++++++------ 2 files changed, 113 insertions(+), 59 deletions(-) diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts index 62f59d3cbf0..d2017c34b88 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -255,13 +255,15 @@ export class EditingRenderController extends Disposable implements IRenderModule resizeCellEditor() { const state = this._cellEditorManagerService.getState(); - const skeleton = this._sheetSkeletonManagerService.getCurrent()?.skeleton; - if (!skeleton) return; + if (!state) return; if (!this._editorBridgeService.isVisible().visible) return; - const latestEditCellState = this._editorBridgeService.getLatestEditCellState(true); + this._editorBridgeService.refreshEditCellPosition(true); + const latestEditCellState = this._editorBridgeService.getEditCellState(); if (!latestEditCellState) return; + const skeleton = this._sheetSkeletonManagerService.getWorksheetSkeleton(latestEditCellState.sheetId)?.skeleton; + if (!skeleton) return; const { row, column, scaleX, scaleY, position, canvasOffset, documentLayoutObject } = latestEditCellState; const maxSize = this._getEditorMaxSize(position, canvasOffset); if (!maxSize) return; @@ -274,7 +276,7 @@ export class EditingRenderController extends Disposable implements IRenderModule const currentWidth = state.endX! - state.startX!; if (currentHeight !== height || currentWidth !== width) { - this._editorBridgeService.refreshEditCellState(true); + this._editorBridgeService.refreshEditCellPosition(true); const skeleton = this._getEditorSkeleton(DOCS_NORMAL_EDITOR_UNIT_ID_KEY); if (!skeleton) { @@ -660,7 +662,7 @@ export class EditingRenderController extends Disposable implements IRenderModule }, }); - this._editorBridgeService.refreshEditCellState(false, true); + this._editorBridgeService.refreshEditCellPosition(false); editCellState = this._editorBridgeService.getEditCellState(); if (editCellState == null) { return; @@ -945,6 +947,8 @@ export class EditingRenderController extends Disposable implements IRenderModule } private async _handleEditorInvisible(param: IEditorBridgeServiceVisibleParam) { + const editCellState = this._editorBridgeService.getEditCellState(); + let { keycode } = param; this._setOpenForCurrent(null, null); @@ -952,7 +956,6 @@ export class EditingRenderController extends Disposable implements IRenderModule this._exitInput(param); - const editCellState = this._editorBridgeService.getEditCellState(); if (editCellState == null) { return; } diff --git a/packages/sheets-ui/src/services/editor-bridge.service.ts b/packages/sheets-ui/src/services/editor-bridge.service.ts index 97c0b58e2f0..b31ac7fd646 100644 --- a/packages/sheets-ui/src/services/editor-bridge.service.ts +++ b/packages/sheets-ui/src/services/editor-bridge.service.ts @@ -92,11 +92,12 @@ export interface IEditorBridgeService { AFTER_CELL_EDIT_ASYNC: typeof AFTER_CELL_EDIT_ASYNC; }>; dispose(): void; - refreshEditCellState(resetSizeOnly?: boolean, tryKeepLastObject?: boolean): void; + refreshEditCellState(): void; + refreshEditCellPosition(resetSizeOnly?: boolean): void; setEditCell(param: ICurrentEditCellParam): void; getEditCellState(): Readonly>; // Gets the DocumentDataModel of the latest table cell based on the latest cell contents - getLatestEditCellState(resetSizeOnly?: boolean, tryKeepLastObject?: boolean): Readonly>; + getLatestEditCellState(): Readonly>; changeVisible(param: IEditorBridgeServiceVisibleParam): void; changeEditorDirty(dirtyStatus: boolean): void; getEditorDirty(): boolean; @@ -183,6 +184,74 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ this._currentEditCellState$.next(editCellState); } + refreshEditCellPosition(resetSizeOnly?: boolean) { + const currentEditCell = this._currentEditCell; + if (currentEditCell == null) { + return; + } + + const ru = this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_SHEET); + if (!ru) return; + + const skeleton = ru.with(SheetSkeletonManagerService).getCurrentSkeleton(); + const selectionRenderService = ru.with(ISheetSelectionRenderService); + if (!skeleton) return; + if (!this._currentEditCellState) return; + + const { primary, unitId, sheetId, scene, engine } = currentEditCell; + const { startRow, startColumn } = primary; + const primaryWithCoord = attachPrimaryWithCoord(primary, skeleton); + if (primaryWithCoord == null) { + return; + } + + const actualRangeWithCoord = makeCellToSelection(primaryWithCoord); + const canvasOffset = getCanvasOffsetByEngine(engine); + + let { startX, startY, endX, endY } = actualRangeWithCoord; + + const { scaleX, scaleY } = scene.getAncestorScale(); + + const scrollXY = scene.getViewportScrollXY(selectionRenderService.getViewPort()); + startX = skeleton.convertTransformToOffsetX(startX, scaleX, scrollXY); + startY = skeleton.convertTransformToOffsetY(startY, scaleY, scrollXY); + endX = skeleton.convertTransformToOffsetX(endX, scaleX, scrollXY); + endY = skeleton.convertTransformToOffsetY(endY, scaleY, scrollXY); + + if (resetSizeOnly && this._currentEditCellState) { + endX = endX - startX + this._currentEditCellState.position.startX; + endY = endY - startY + this._currentEditCellState.position.startY; + startX = this._currentEditCellState.position.startX; + startY = this._currentEditCellState.position.startY; + } + + this._editorService.setOperationSheetUnitId(unitId); + + this._editorService.setOperationSheetSubUnitId(sheetId); + + const editCellState = { + position: { + startX, + startY, + endX, + endY, + }, + scaleX, + scaleY, + canvasOffset, + row: startRow, + column: startColumn, + unitId, + sheetId, + documentLayoutObject: this._currentEditCellState.documentLayoutObject, + editorUnitId: this._editorUnitId, + isInArrayFormulaRange: this._currentEditCellState?.isInArrayFormulaRange, + }; + + this._currentEditCellState = editCellState; + this._currentEditCellState$.next(editCellState); + } + setEditCell(param: ICurrentEditCellParam) { this._currentEditCell = param; @@ -215,8 +284,8 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ return this._currentEditCellState; } - // eslint-disable-next-line max-lines-per-function, complexity - getLatestEditCellState(resetSizeOnly?: boolean, tryKeepLastObject?: boolean) { + // eslint-disable-next-line max-lines-per-function + getLatestEditCellState() { const currentEditCell = this._currentEditCell; if (currentEditCell == null) { return; @@ -269,62 +338,44 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ location ); - if (resetSizeOnly && this._currentEditCellState) { - endX = endX - startX + this._currentEditCellState.position.startX; - endY = endY - startY + this._currentEditCellState.position.startY; - startX = this._currentEditCellState.position.startX; - startY = this._currentEditCellState.position.startY; - } - - if ( - this._currentEditCellState && - tryKeepLastObject && - this._currentEditCellState.unitId === location.unitId && - this._currentEditCellState.sheetId === location.subUnitId && - this._currentEditCellState.row === location.row && - this._currentEditCellState.column === location.col - ) { - documentLayoutObject = this._currentEditCellState.documentLayoutObject; - } else { - documentLayoutObject = cell && skeleton.getCellDocumentModelWithFormula(cell); + documentLayoutObject = cell && skeleton.getCellDocumentModelWithFormula(cell); // Rewrite the cellValueType to STRING to avoid render the value on the right side when number type. - const renderConfig = documentLayoutObject?.documentModel?.documentStyle.renderConfig; - if (renderConfig != null) { - renderConfig.cellValueType = CellValueType.STRING; - } + const renderConfig = documentLayoutObject?.documentModel?.documentStyle.renderConfig; + if (renderConfig != null) { + renderConfig.cellValueType = CellValueType.STRING; + } - if (!documentLayoutObject || documentLayoutObject.documentModel == null) { - const blankModel = skeleton.getBlankCellDocumentModel(cell); - - if (documentLayoutObject != null) { - const { verticalAlign, horizontalAlign, wrapStrategy, textRotation, fill } = documentLayoutObject; - const { centerAngle, vertexAngle } = convertTextRotation(textRotation); - blankModel.documentModel!.documentStyle.renderConfig = { - verticalAlign, horizontalAlign, wrapStrategy, background: { rgb: fill }, centerAngle, vertexAngle, - }; - } - documentLayoutObject = blankModel; + if (!documentLayoutObject || documentLayoutObject.documentModel == null) { + const blankModel = skeleton.getBlankCellDocumentModel(cell); + + if (documentLayoutObject != null) { + const { verticalAlign, horizontalAlign, wrapStrategy, textRotation, fill } = documentLayoutObject; + const { centerAngle, vertexAngle } = convertTextRotation(textRotation); + blankModel.documentModel!.documentStyle.renderConfig = { + verticalAlign, horizontalAlign, wrapStrategy, background: { rgb: fill }, centerAngle, vertexAngle, + }; } + documentLayoutObject = blankModel; + } // background of canvas is set to transparent, so if no bgcolor sepcified in curr cell, set it to white. - documentLayoutObject.fill = documentLayoutObject.fill || '#fff'; - documentLayoutObject.documentModel?.setZoomRatio(Math.max(scaleX, scaleY)); - - if (cell?.isInArrayFormulaRange === true) { - const body = documentLayoutObject.documentModel?.getBody(); - if (body) { - body.textRuns = [ - { - st: 0, - ed: body.dataStream.length - 2, - ts: { - cl: { - rgb: this._themeService.getCurrentTheme().textColorSecondary, - }, + documentLayoutObject.fill = documentLayoutObject.fill || '#fff'; + documentLayoutObject.documentModel?.setZoomRatio(Math.max(scaleX, scaleY)); + + if (cell?.isInArrayFormulaRange === true) { + const body = documentLayoutObject.documentModel?.getBody(); + if (body) { + body.textRuns = [ + { + st: 0, + ed: body.dataStream.length - 2, + ts: { + cl: { + rgb: this._themeService.getCurrentTheme().textColorSecondary, }, }, - ]; - } + }, + ]; } } From 5cc2203b7849a1a2d4739212d6e7906da52cf90d Mon Sep 17 00:00:00 2001 From: zhangw Date: Sat, 12 Oct 2024 01:31:24 +0800 Subject: [PATCH 12/15] feat: update --- .../sheets-ui/src/services/editor-bridge.service.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/sheets-ui/src/services/editor-bridge.service.ts b/packages/sheets-ui/src/services/editor-bridge.service.ts index b31ac7fd646..1794838e825 100644 --- a/packages/sheets-ui/src/services/editor-bridge.service.ts +++ b/packages/sheets-ui/src/services/editor-bridge.service.ts @@ -177,8 +177,8 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ }); } - refreshEditCellState(resetSizeOnly?: boolean, tryKeepLastObject?: boolean) { - const editCellState = this.getLatestEditCellState(resetSizeOnly, tryKeepLastObject); + refreshEditCellState() { + const editCellState = this.getLatestEditCellState(); this._currentEditCellState = editCellState; this._currentEditCellState$.next(editCellState); @@ -332,15 +332,14 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ origin: worksheet.getCellRaw(startRow, startColumn), }; - let documentLayoutObject: Nullable; const cell = this.interceptor.fetchThroughInterceptors(this.interceptor.getInterceptPoints().BEFORE_CELL_EDIT)( worksheet.getCell(startRow, startColumn), location ); - documentLayoutObject = cell && skeleton.getCellDocumentModelWithFormula(cell); + let documentLayoutObject = cell && skeleton.getCellDocumentModelWithFormula(cell); - // Rewrite the cellValueType to STRING to avoid render the value on the right side when number type. + // Rewrite the cellValueType to STRING to avoid render the value on the right side when number type. const renderConfig = documentLayoutObject?.documentModel?.documentStyle.renderConfig; if (renderConfig != null) { renderConfig.cellValueType = CellValueType.STRING; @@ -358,7 +357,7 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ } documentLayoutObject = blankModel; } - // background of canvas is set to transparent, so if no bgcolor sepcified in curr cell, set it to white. + // background of canvas is set to transparent, so if no bgcolor sepcified in curr cell, set it to white. documentLayoutObject.fill = documentLayoutObject.fill || '#fff'; documentLayoutObject.documentModel?.setZoomRatio(Math.max(scaleX, scaleY)); From b30fd0b67636420823e48f5bf53832aff7ec964a Mon Sep 17 00:00:00 2001 From: zhangw Date: Sat, 12 Oct 2024 01:47:17 +0800 Subject: [PATCH 13/15] feat: update --- .../docs-ui/src/controllers/doc-move-cursor.controller.ts | 8 ++++---- .../src/commands/operations/text-selection.operation.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/docs-ui/src/controllers/doc-move-cursor.controller.ts b/packages/docs-ui/src/controllers/doc-move-cursor.controller.ts index eccfc2c16e6..1989016b224 100644 --- a/packages/docs-ui/src/controllers/doc-move-cursor.controller.ts +++ b/packages/docs-ui/src/controllers/doc-move-cursor.controller.ts @@ -295,8 +295,8 @@ export class DocMoveCursorController extends Disposable { this._textSelectionManagerService.replaceTextRanges([ { - startOffset: cursor, - endOffset: cursor, + startOffset: Math.max(0, cursor), + endOffset: Math.max(0, cursor), style, }, ], false); @@ -330,8 +330,8 @@ export class DocMoveCursorController extends Disposable { this._textSelectionManagerService.replaceTextRanges([ { - startOffset: cursor, - endOffset: cursor, + startOffset: Math.max(0, cursor), + endOffset: Math.max(0, cursor), style, }, ], false); diff --git a/packages/docs/src/commands/operations/text-selection.operation.ts b/packages/docs/src/commands/operations/text-selection.operation.ts index 7cea2d42347..d05c07a5f90 100644 --- a/packages/docs/src/commands/operations/text-selection.operation.ts +++ b/packages/docs/src/commands/operations/text-selection.operation.ts @@ -15,8 +15,8 @@ */ import type { IOperation } from '@univerjs/core'; -import { CommandType } from '@univerjs/core'; import type { ITextRangeWithStyle, ITextSelectionStyle } from '@univerjs/engine-render'; +import { CommandType } from '@univerjs/core'; export interface ISetTextSelectionsOperationParams { unitId: string; @@ -33,7 +33,7 @@ export const SetTextSelectionsOperation: IOperation { + handler: () => { // for menu highlight use and share cursor. return true; }, From 86530693e13a0e9d5927edf683f0c536f80e0fda Mon Sep 17 00:00:00 2001 From: zhangw Date: Sat, 12 Oct 2024 10:21:34 +0800 Subject: [PATCH 14/15] feat: update --- .../src/controllers/editor/editing.render-controller.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts index d2017c34b88..87a5baf09cf 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -62,7 +62,7 @@ import { ScrollBar, } from '@univerjs/engine-render'; -import { ClearSelectionFormatCommand, COMMAND_LISTENER_SKELETON_CHANGE, SetRangeValuesCommand, SetRangeValuesMutation, SetSelectionsOperation, SetWorksheetActivateCommand, SheetsSelectionsService } from '@univerjs/sheets'; +import { ClearSelectionFormatCommand, COMMAND_LISTENER_SKELETON_CHANGE, SetRangeValuesCommand, SetSelectionsOperation, SetWorksheetActivateCommand, SheetsSelectionsService } from '@univerjs/sheets'; import { ILayoutService, KeyCode, SetEditorResizeOperation } from '@univerjs/ui'; import { distinctUntilChanged, filter } from 'rxjs'; import { getEditorObject } from '../../basics/editor/get-editor-object'; @@ -895,12 +895,6 @@ export class EditingRenderController extends Disposable implements IRenderModule } })); - d.add(this._commandService.onCommandExecuted((command: ICommandInfo) => { - if (command.id === SetRangeValuesMutation.id) { - this._editorBridgeService.refreshEditCellState(); - } - })); - const closeEditorOperation = [SetCellEditVisibleArrowOperation.id]; d.add(this._commandService.onCommandExecuted((command: ICommandInfo) => { if (closeEditorOperation.includes(command.id)) { From 99c61a7d8575312fe8ab90f0341f5f02d0c0f06b Mon Sep 17 00:00:00 2001 From: zhangw Date: Sat, 12 Oct 2024 10:23:36 +0800 Subject: [PATCH 15/15] feat: update --- .../controllers/editor/editing.render-controller.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts index 87a5baf09cf..da8486be62b 100644 --- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts +++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts @@ -62,7 +62,7 @@ import { ScrollBar, } from '@univerjs/engine-render'; -import { ClearSelectionFormatCommand, COMMAND_LISTENER_SKELETON_CHANGE, SetRangeValuesCommand, SetSelectionsOperation, SetWorksheetActivateCommand, SheetsSelectionsService } from '@univerjs/sheets'; +import { ClearSelectionFormatCommand, COMMAND_LISTENER_SKELETON_CHANGE, SetRangeValuesCommand, SetRangeValuesMutation, SetSelectionsOperation, SetWorksheetActivateCommand, SheetsSelectionsService } from '@univerjs/sheets'; import { ILayoutService, KeyCode, SetEditorResizeOperation } from '@univerjs/ui'; import { distinctUntilChanged, filter } from 'rxjs'; import { getEditorObject } from '../../basics/editor/get-editor-object'; @@ -891,6 +891,14 @@ export class EditingRenderController extends Disposable implements IRenderModule // Use fix https://github.com/dream-num/univer/issues/1231. d.add(this._commandService.onCommandExecuted((command: ICommandInfo) => { if (command.id === ClearSelectionFormatCommand.id) { + if (this._editorBridgeService.isVisible().visible) return; + this._editorBridgeService.refreshEditCellState(); + } + })); + + d.add(this._commandService.onCommandExecuted((command: ICommandInfo) => { + if (command.id === SetRangeValuesMutation.id) { + if (this._editorBridgeService.isVisible().visible) return; this._editorBridgeService.refreshEditCellState(); } }));