diff --git a/packages/core/src/sheets/styles.ts b/packages/core/src/sheets/styles.ts index b3e1087c59..76ad0d38ba 100644 --- a/packages/core/src/sheets/styles.ts +++ b/packages/core/src/sheets/styles.ts @@ -90,6 +90,7 @@ export class Styles { return this._styles; } + // TODO: this should be `deReuseStyle` to be more accurate. getStyleByCell(cell: Nullable): Nullable { let style; if (cell && Tools.isObject(cell.s)) { diff --git a/packages/core/src/sheets/worksheet.ts b/packages/core/src/sheets/worksheet.ts index ab54e9b9a6..a0bda5673e 100644 --- a/packages/core/src/sheets/worksheet.ts +++ b/packages/core/src/sheets/worksheet.ts @@ -139,6 +139,8 @@ export class Worksheet { * @param {boolean} [keepRaw] If true, return the raw style data, otherwise return the style data object * @returns {Nullable|string} The style of the column */ + getColumnStyle(column: number, keepRaw: true): string | Nullable; + getColumnStyle(column: number): Nullable; getColumnStyle(column: number, keepRaw = false): string | Nullable { if (keepRaw) { return this._columnManager.getColumnStyle(column); @@ -161,6 +163,8 @@ export class Worksheet { * @param {boolean} [keepRaw] If true, return the raw style data, otherwise return the style data object * @returns {Nullable} The style of the row */ + getRowStyle(row: number, keepRaw: true): string | Nullable; + getRowStyle(row: number): Nullable; getRowStyle(row: number, keepRaw = false): string | Nullable { if (keepRaw) { return this._rowManager.getRowStyle(row); @@ -223,6 +227,19 @@ export class Worksheet { this._snapshot.defaultStyle = style; } + getCellStyle(row: number, col: number): Nullable { + const cell = this.getCell(row, col); + if (cell) { + const style = cell.s; + if (typeof style === 'string') { + return this._styles.get(style); + } + return style; + } + + return null; + } + /** * Returns WorkSheet Cell Data Matrix * @returns WorkSheet Cell Data Matrix diff --git a/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts b/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts index 2a361d6ae6..270ea54913 100644 --- a/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts +++ b/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts @@ -1310,8 +1310,8 @@ export class SpreadsheetSkeleton extends SheetSkeleton { const cell = this.worksheet.getCell(row, col) || this.worksheet.getCellRaw(row, col); const cellStyle = this._styles.getStyleByCell(cell); - const columnStyle = this.worksheet.getColumnStyle(col) as IStyleData; - const rowStyle = this.worksheet.getRowStyle(row) as IStyleData; + const columnStyle = this.worksheet.getColumnStyle(col); + const rowStyle = this.worksheet.getRowStyle(row); const defaultStyle = this.worksheet.getDefaultCellStyleInternal(); const style = this._isRowStylePrecedeColumnStyle diff --git a/packages/sheets-ui/src/controllers/menu/menu.ts b/packages/sheets-ui/src/controllers/menu/menu.ts index 46d913be5c..d4467bb271 100644 --- a/packages/sheets-ui/src/controllers/menu/menu.ts +++ b/packages/sheets-ui/src/controllers/menu/menu.ts @@ -18,6 +18,7 @@ import type { IAccessor } from '@univerjs/core'; import type { IMenuButtonItem, IMenuSelectorItem } from '@univerjs/ui'; import { BooleanNumber, + composeStyles, DEFAULT_STYLES, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, EDITOR_ACTIVATED, @@ -411,30 +412,32 @@ export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSel disabled$: getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellStylePermission], rangeTypes: [RangeProtectionPermissionEditPoint] }, true), value$: deriveStateFromActiveSheet$(univerInstanceService, defaultValue, ({ worksheet }) => new Observable((subscriber) => { - const disposable = commandService.onCommandExecuted((c) => { - const id = c.id; - if (id !== SetRangeValuesMutation.id && id !== SetSelectionsOperation.id && id !== SetWorksheetActiveOperation.id) { - return; - } + const updateSheet = () => { + let ff = defaultValue; const primary = selectionManagerService.getCurrentLastSelection()?.primary; - let ff; if (primary != null) { - const range = worksheet.getRange(primary.startRow, primary.startColumn); - ff = range?.getFontFamily(); + const cell = worksheet.getCellStyle(primary.startRow, primary.startColumn); + const defaultStyle = worksheet.getDefaultCellStyleInternal(); + const rowStyle = worksheet.getRowStyle(primary.startRow); + const colStyle = worksheet.getColumnStyle(primary.startColumn); + const style = composeStyles(defaultStyle, rowStyle, colStyle, cell); + if (style.ff) { + ff = style.ff; + } } - subscriber.next(ff ?? defaultValue); + subscriber.next(ff); + }; + + const disposable = commandService.onCommandExecuted((c) => { + const id = c.id; + if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) { + updateSheet(); + } }); - const primary = selectionManagerService.getCurrentLastSelection()?.primary; - let ff; - if (primary != null) { - const range = worksheet.getRange(primary.startRow, primary.startColumn); - ff = range?.getFontFamily(); - } - - subscriber.next(ff ?? defaultValue); + updateSheet(); return disposable.dispose; })), hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET), @@ -465,16 +468,34 @@ export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelec selections: FONT_SIZE_LIST, disabled$, value$: deriveStateFromActiveSheet$(univerInstanceService, defaultValue, ({ worksheet }) => new Observable((subscriber) => { + const updateSheet = () => { + let fs = defaultValue; + const primary = selectionManagerService.getCurrentLastSelection()?.primary; + if (primary != null) { + const cell = worksheet.getCellStyle(primary.startRow, primary.startColumn); + const defaultStyle = worksheet.getDefaultCellStyleInternal(); + const rowStyle = worksheet.getRowStyle(primary.startRow); + const colStyle = worksheet.getColumnStyle(primary.startColumn); + const style = composeStyles(defaultStyle, rowStyle, colStyle, cell); + if (style.fs) { + fs = style.fs; + } + } + subscriber.next(fs); + }; + + const updateSheetEditor = () => { + const textRun = getFontStyleAtCursor(accessor); + if (textRun != null) { + const fs = textRun.ts?.fs ?? defaultValue; + subscriber.next(fs); + } + }; + const disposable = commandService.onCommandExecuted((c) => { const id = c.id; if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) { - const primary = selectionManagerService.getCurrentLastSelection()?.primary; - let fs; - if (primary != null) { - const range = worksheet.getRange(primary.startRow, primary.startColumn); - fs = range?.getFontSize(); - } - subscriber.next(fs ?? defaultValue); + updateSheet(); } if ( @@ -482,25 +503,11 @@ export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelec contextService.getContextValue(EDITOR_ACTIVATED) && contextService.getContextValue(FOCUSING_SHEET) ) { - const textRun = getFontStyleAtCursor(accessor); - - if (textRun == null) { - return; - } - - const fs = textRun.ts?.fs; - subscriber.next(fs ?? defaultValue); + updateSheetEditor(); } }); - const primary = selectionManagerService.getCurrentLastSelection()?.primary; - let fs; - if (primary != null) { - const range = worksheet.getRange(primary.startRow, primary.startColumn); - fs = range?.getFontSize(); - } - subscriber.next(fs ?? defaultValue); - + updateSheet(); return disposable.dispose; })), hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET), diff --git a/packages/sheets/src/facade/f-worksheet.ts b/packages/sheets/src/facade/f-worksheet.ts index deea8c2dfb..1cdcef7c53 100644 --- a/packages/sheets/src/facade/f-worksheet.ts +++ b/packages/sheets/src/facade/f-worksheet.ts @@ -183,7 +183,8 @@ export class FWorksheet extends FBaseInitialable { * ``` */ getRowDefaultStyle(index: number, keepRaw: boolean = false): Nullable | string { - return this._worksheet.getRowStyle(index, keepRaw); + // TODO@VicKun4937: should use function overload here + return keepRaw ? this._worksheet.getRowStyle(index, keepRaw) : this._worksheet.getRowStyle(index); } /** @@ -203,7 +204,8 @@ export class FWorksheet extends FBaseInitialable { * ``` */ getColumnDefaultStyle(index: number, keepRaw: boolean = false): Nullable | string { - return this._worksheet.getColumnStyle(index, keepRaw); + // TODO@VicKun4937: should use function overload here + return keepRaw ? this._worksheet.getColumnStyle(index, keepRaw) : this._worksheet.getColumnStyle(index); } /**