fix(sheets-ui): update sheet font menus (#4828)

This commit is contained in:
Wenzhao Hu
2025-03-18 07:58:22 +08:00
committed by GitHub
parent 2611159b05
commit c8a4d2641e
5 changed files with 71 additions and 44 deletions
+1
View File
@@ -90,6 +90,7 @@ export class Styles {
return this._styles;
}
// TODO: this should be `deReuseStyle` to be more accurate.
getStyleByCell(cell: Nullable<ICellDataForSheetInterceptor>): Nullable<IStyleData> {
let style;
if (cell && Tools.isObject(cell.s)) {
+17
View File
@@ -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<IStyleData>|string} The style of the column
*/
getColumnStyle(column: number, keepRaw: true): string | Nullable<IStyleData>;
getColumnStyle(column: number): Nullable<IStyleData>;
getColumnStyle(column: number, keepRaw = false): string | Nullable<IStyleData> {
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<IStyleData>} The style of the row
*/
getRowStyle(row: number, keepRaw: true): string | Nullable<IStyleData>;
getRowStyle(row: number): Nullable<IStyleData>;
getRowStyle(row: number, keepRaw = false): string | Nullable<IStyleData> {
if (keepRaw) {
return this._rowManager.getRowStyle(row);
@@ -223,6 +227,19 @@ export class Worksheet {
this._snapshot.defaultStyle = style;
}
getCellStyle(row: number, col: number): Nullable<IStyleData> {
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
@@ -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
+47 -40
View File
@@ -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),
+4 -2
View File
@@ -183,7 +183,8 @@ export class FWorksheet extends FBaseInitialable {
* ```
*/
getRowDefaultStyle(index: number, keepRaw: boolean = false): Nullable<IStyleData> | 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<IStyleData> | 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);
}
/**