Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sheet-ui): make the default font family and font size correct #1827

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/types/const/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ export const DEFAULT_STYLES = {
/**
* fontFamily
*/
ff: 'Times New Roman',
ff: 'Arial',
/**
* fontSize
*/
fs: 14,
fs: 11,
/**
* italic
* 0: false
Expand Down
131 changes: 106 additions & 25 deletions packages/sheets-ui/src/controllers/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ import {
SetTextRotationCommand,
SetTextWrapCommand,
SetVerticalTextAlignCommand,
SetWorksheetActiveOperation,
SetWorksheetRowIsAutoHeightCommand,
} from '@univerjs/sheets';

import type { IMenuButtonItem, IMenuSelectorItem } from '@univerjs/ui';
import {
CopyCommand,
Expand Down Expand Up @@ -157,7 +159,7 @@ export function BoldMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
activated$: new Observable<boolean>((subscriber) => {
const disposable = commandService.onCommandExecuted((c) => {
const id = c.id;
if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id) {
if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) {
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let isBold = FontWeight.NORMAL;
Expand Down Expand Up @@ -187,8 +189,16 @@ export function BoldMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
}
});

subscriber.next(false);
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let isBold = FontWeight.NORMAL;

if (primary != null) {
const range = worksheet.getRange(primary.startRow, primary.startColumn);
isBold = range?.getFontWeight();
}

subscriber.next(isBold === FontWeight.BOLD);
return disposable.dispose;
}),
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.SHEET),
Expand All @@ -213,7 +223,7 @@ export function ItalicMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
activated$: new Observable<boolean>((subscriber) => {
const disposable = commandService.onCommandExecuted((c) => {
const id = c.id;
if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id) {
if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) {
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let isItalic = FontItalic.NORMAL;
Expand Down Expand Up @@ -242,7 +252,15 @@ export function ItalicMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
}
});

subscriber.next(false);
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let isItalic = FontItalic.NORMAL;
if (primary != null) {
const range = worksheet.getRange(primary.startRow, primary.startColumn);
isItalic = range?.getFontStyle();
}

subscriber.next(isItalic === FontItalic.ITALIC);
return disposable.dispose;
}),
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.SHEET),
Expand All @@ -267,7 +285,7 @@ export function UnderlineMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
activated$: new Observable<boolean>((subscriber) => {
const disposable = commandService.onCommandExecuted((c) => {
const id = c.id;
if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id) {
if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) {
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let isUnderline;
Expand Down Expand Up @@ -296,7 +314,15 @@ export function UnderlineMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
}
});

subscriber.next(false);
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let isUnderline;
if (primary != null) {
const range = worksheet.getRange(primary.startRow, primary.startColumn);
isUnderline = range?.getUnderline();
}

subscriber.next(!!(isUnderline && isUnderline.s));
return disposable.dispose;
}),
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.SHEET),
Expand All @@ -321,7 +347,7 @@ export function StrikeThroughMenuItemFactory(accessor: IAccessor): IMenuButtonIt
activated$: new Observable<boolean>((subscriber) => {
const disposable = commandService.onCommandExecuted((c) => {
const id = c.id;
if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id) {
if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) {
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let st;
Expand Down Expand Up @@ -350,7 +376,15 @@ export function StrikeThroughMenuItemFactory(accessor: IAccessor): IMenuButtonIt
}
});

subscriber.next(false);
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let st;
if (primary != null) {
const range = worksheet.getRange(primary.startRow, primary.startColumn);
st = range?.getStrikeThrough();
}

subscriber.next(!!(st && st.s));
return disposable.dispose;
}),
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.SHEET),
Expand All @@ -361,7 +395,6 @@ export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSel
const commandService = accessor.get(ICommandService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const selectionManagerService = accessor.get(SelectionManagerService);

return {
id: SetRangeFontFamilyCommand.id,
tooltip: 'toolbar.font',
Expand All @@ -382,7 +415,7 @@ export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSel

const disposable = commandService.onCommandExecuted((c) => {
const id = c.id;
if (id !== SetRangeValuesMutation.id && id !== SetSelectionsOperation.id) {
if (id !== SetRangeValuesMutation.id && id !== SetSelectionsOperation.id && id !== SetWorksheetActiveOperation.id) {
return;
}

Expand All @@ -397,7 +430,15 @@ export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSel
subscriber.next(ff ?? defaultValue);
});

subscriber.next(defaultValue);
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let ff;
if (primary != null) {
const range = worksheet.getRange(primary.startRow, primary.startColumn);
ff = range?.getFontFamily();
}

subscriber.next(ff ?? defaultValue);
return disposable.dispose;
}),
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.SHEET),
Expand All @@ -409,7 +450,6 @@ export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelec
const univerInstanceService = accessor.get(IUniverInstanceService);
const selectionManagerService = accessor.get(SelectionManagerService);
const contextService = accessor.get(IContextService);

const disabled$ = getCurrentSheetDisabled$(accessor);

return {
Expand All @@ -429,18 +469,17 @@ export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelec
selections: FONT_SIZE_LIST,
disabled$,
value$: new Observable((subscriber) => {
const DEFAULT_SIZE = 14;
const DEFAULT_SIZE = 11;
const disposable = commandService.onCommandExecuted((c) => {
const id = c.id;
if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id) {
if (id === SetRangeValuesMutation.id || id === SetSelectionsOperation.id || id === SetWorksheetActiveOperation.id) {
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let fs;
if (primary != null) {
const range = worksheet.getRange(primary.startRow, primary.startColumn);
fs = range?.getFontSize();
}

subscriber.next(fs ?? DEFAULT_SIZE);
}

Expand All @@ -456,12 +495,18 @@ export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelec
}

const fs = textRun.ts?.fs;

subscriber.next(fs ?? DEFAULT_SIZE);
}
});

subscriber.next(DEFAULT_SIZE);
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let fs;
if (primary != null) {
const range = worksheet.getRange(primary.startRow, primary.startColumn);
fs = range?.getFontSize();
}
subscriber.next(fs ?? DEFAULT_SIZE);

return disposable.dispose;
}),
Expand Down Expand Up @@ -597,7 +642,7 @@ export function HorizontalAlignMenuItemFactory(accessor: IAccessor): IMenuSelect
value$: new Observable<HorizontalAlign>((subscriber) => {
const disposable = accessor.get(ICommandService).onCommandExecuted((c) => {
const id = c.id;
if (id !== SetHorizontalTextAlignCommand.id && id !== SetSelectionsOperation.id) {
if (id !== SetHorizontalTextAlignCommand.id && id !== SetSelectionsOperation.id && id !== SetWorksheetActiveOperation.id) {
return;
}

Expand All @@ -612,7 +657,15 @@ export function HorizontalAlignMenuItemFactory(accessor: IAccessor): IMenuSelect
subscriber.next(ha ?? HorizontalAlign.LEFT);
});

subscriber.next(HorizontalAlign.LEFT);
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let ha;
if (primary != null) {
const range = worksheet.getRange(primary.startRow, primary.startColumn);
ha = range?.getHorizontalAlignment();
}

subscriber.next(ha ?? HorizontalAlign.LEFT);

return disposable.dispose;
}),
Expand Down Expand Up @@ -654,7 +707,7 @@ export function VerticalAlignMenuItemFactory(accessor: IAccessor): IMenuSelector
value$: new Observable<VerticalAlign>((subscriber) => {
const disposable = accessor.get(ICommandService).onCommandExecuted((c) => {
const id = c.id;
if (id !== SetVerticalTextAlignCommand.id && id !== SetSelectionsOperation.id) {
if (id !== SetVerticalTextAlignCommand.id && id !== SetSelectionsOperation.id && id !== SetWorksheetActiveOperation.id) {
return;
}

Expand All @@ -669,7 +722,15 @@ export function VerticalAlignMenuItemFactory(accessor: IAccessor): IMenuSelector
subscriber.next(va ?? VerticalAlign.TOP);
});

subscriber.next(VerticalAlign.TOP);
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let va;
if (primary != null) {
const range = worksheet.getRange(primary.startRow, primary.startColumn);
va = range?.getVerticalAlignment();
}

subscriber.next(va ?? VerticalAlign.TOP);

return disposable.dispose;
}),
Expand Down Expand Up @@ -711,7 +772,7 @@ export function WrapTextMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<
value$: new Observable((subscriber) => {
const disposable = accessor.get(ICommandService).onCommandExecuted((c) => {
const id = c.id;
if (id !== SetTextWrapCommand.id && id !== SetSelectionsOperation.id) {
if (id !== SetTextWrapCommand.id && id !== SetSelectionsOperation.id && id !== SetWorksheetActiveOperation.id) {
return;
}

Expand All @@ -726,7 +787,15 @@ export function WrapTextMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<
subscriber.next(ws ?? WrapStrategy.OVERFLOW);
});

subscriber.next(WrapStrategy.OVERFLOW);
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let ws;
if (primary != null) {
const range = worksheet.getRange(primary.startRow, primary.startColumn);
ws = range?.getWrapStrategy();
}

subscriber.next(ws ?? WrapStrategy.OVERFLOW);

return disposable.dispose;
}),
Expand Down Expand Up @@ -783,7 +852,7 @@ export function TextRotateMenuItemFactory(accessor: IAccessor): IMenuSelectorIte
value$: new Observable<number | string>((subscriber) => {
const disposable = accessor.get(ICommandService).onCommandExecuted((c) => {
const id = c.id;
if (id !== SetTextRotationCommand.id && id !== SetSelectionsOperation.id) {
if (id !== SetTextRotationCommand.id && id !== SetSelectionsOperation.id && id !== SetWorksheetActiveOperation.id) {
return;
}

Expand All @@ -802,7 +871,19 @@ export function TextRotateMenuItemFactory(accessor: IAccessor): IMenuSelectorIte
}
});

subscriber.next(0);
const primary = selectionManagerService.getLast()?.primary;
const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet();
let tr;
if (primary != null) {
const range = worksheet.getRange(primary.startRow, primary.startColumn);
tr = range?.getTextRotation();
}

if (tr?.v === BooleanNumber.TRUE) {
subscriber.next('v');
} else {
subscriber.next((tr && tr.a) ?? 0);
}

return disposable.dispose;
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ describe("Test commands used for updating cells' styles", () => {

// undo
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
expect(getFontSize()).toBe(14);
expect(getFontSize()).toBe(11);

// redo
expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
Expand Down Expand Up @@ -376,16 +376,16 @@ describe("Test commands used for updating cells' styles", () => {
.getFontFamily();
}

expect(await commandService.executeCommand(SetFontFamilyCommand.id, { value: 'Arial' })).toBeTruthy();
expect(getFontFamily()).toBe('Arial');
expect(await commandService.executeCommand(SetFontFamilyCommand.id, { value: 'Times New Roman' })).toBeTruthy();
expect(getFontFamily()).toBe('Times New Roman');

// undo
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
expect(getFontFamily()).toBe('Times New Roman');
expect(getFontFamily()).toBe('Arial');

// redo
expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
expect(getFontFamily()).toBe('Arial');
expect(getFontFamily()).toBe('Times New Roman');
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/components/font-family/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export interface IFontFamilyItemProps extends ICustomComponentProps<string> {

export const FONT_FAMILY_LIST = [
{
value: 'Times New Roman',
value: 'Arial',
},
{
value: 'Arial',
value: 'Times New Roman',
},
{
value: 'Tahoma',
Expand Down