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.

refactor(*): refactor toolbar implementation

wzhudev827439e1d4fc
diff --git a/packages/base-sheets/src/Commands/Commands/set-style.command.ts b/packages/base-sheets/src/Commands/Commands/set-style.command.ts
index 5a8529a375..197142941f 100644
--- a/packages/base-sheets/src/Commands/Commands/set-style.command.ts
+++ b/packages/base-sheets/src/Commands/Commands/set-style.command.ts
@@ -232,7 +232,7 @@ export interface ISetFontSizeCommandParams {
export const SetFontSizeCommand: ICommand<ISetFontSizeCommandParams> = {
type: CommandType.COMMAND,
- id: 'sheet.command.set-font-family',
+ id: 'sheet.command.set-font-size',
handler: async (accessor, params) => {
if (!params) {
return false;
@@ -256,7 +256,7 @@ export interface ISetColorCommandParams {
export const SetTextColorCommand: ICommand<ISetColorCommandParams> = {
type: CommandType.COMMAND,
- id: 'sheet.command.set-font-family',
+ id: 'sheet.command.set-text-color',
handler: async (accessor, params) => {
if (!params) {
return false;
diff --git a/packages/base-ui/src/BaseComponent.ts b/packages/base-ui/src/BaseComponent.ts
index 27d6b5c8bc..18b63c4e16 100644
--- a/packages/base-ui/src/BaseComponent.ts
+++ b/packages/base-ui/src/BaseComponent.ts
@@ -2,7 +2,6 @@ import { ComponentClass } from 'preact';
import { ForwardFn } from 'preact/compat';
import { BasePlugin } from '@univerjs/core';
import { Context } from '@univerjs/core/src/Basics/Context';
-import { Injector } from '@wendellhu/redi';
import { Description } from './Interfaces';
// TODO Button const enum; BaseComponentSheet => BaseComponentXXX
@@ -13,8 +12,6 @@ export interface BaseComponentSheet extends BasePlugin {
getComponentFactory(): BaseComponentFactory;
}
export interface BaseComponentProps {
- injector: Injector;
-
getComponent?: (ref: any) => void; //获取自身组件
id?: string; // 组件id
diff --git a/packages/base-ui/src/Components/Select/Select.tsx b/packages/base-ui/src/Components/Select/Select.tsx
index 57ab282bf9..25b23abd1b 100644
--- a/packages/base-ui/src/Components/Select/Select.tsx
+++ b/packages/base-ui/src/Components/Select/Select.tsx
@@ -1,6 +1,5 @@
import { ComponentChildren, createRef, RefObject } from 'preact';
import { PureComponent } from 'preact/compat';
-import { Icon } from '..';
import { ICustomComponent } from '../../Common';
import { BaseMenuItem } from '../../Interfaces';
import { Dropdown } from '../Dropdown';
@@ -8,6 +7,9 @@ import { Input } from '../Input';
import { BaseItemProps, Item } from '../Item/Item';
import { CustomLabel } from '../CustomLabel';
import styles from './index.module.less';
+import { Icon } from '../..'; // FIXME: strange import
+
+// TODO: these type definitions should be moved out of components to menu service
export enum SelectTypes {
SINGLE, // 普通下拉
@@ -18,7 +20,7 @@ export enum SelectTypes {
DOUBLEFIX,
}
-enum DisplayTypes {
+export enum DisplayTypes {
LABEL,
SUFFIX,
}
diff --git a/packages/base-ui/src/index.ts b/packages/base-ui/src/index.ts
index f05aa731de..438b64c07b 100644
--- a/packages/base-ui/src/index.ts
+++ b/packages/base-ui/src/index.ts
@@ -8,7 +8,7 @@ export * from './Components';
export * from './BaseComponent';
export * from './UIPlugin';
-export { IMenuService, IMenuItem, MenuPosition, DesktopMenuService, IDisplayMenuItem } from './services/menu/menu.service';
+export { IMenuService, IMenuItem, IMenuButtonItem, IMenuSelectorItem, MenuPosition, DesktopMenuService, IDisplayMenuItem, MenuItemType } from './services/menu/menu.service';
export { KeyCode, MetaKeys } from './services/shortcut/keycode';
export { IShortcutService, IShortcutItem, DesktopShortcutService } from './services/shortcut/shorcut.service';
export { IPlatformService, DesktopPlatformService } from './services/platform/platform.service';
diff --git a/packages/base-ui/src/services/menu/menu.service.ts b/packages/base-ui/src/services/menu/menu.service.ts
index bc549ef204..6659110480 100644
--- a/packages/base-ui/src/services/menu/menu.service.ts
+++ b/packages/base-ui/src/services/menu/menu.service.ts
@@ -1,9 +1,10 @@
import { Disposable, toDisposable } from '@univerjs/core';
import { createIdentifier, IDisposable } from '@wendellhu/redi';
import { Observable } from 'rxjs';
+import { ComponentChildren } from 'preact';
import { IShortcutService } from '../shortcut/shorcut.service';
import { ICustomLabelType } from '../../Interfaces/CustomLabel';
-import { ComponentChildren } from 'preact';
+import { BaseSelectProps, DisplayTypes, SelectTypes } from '../../Components/Select/Select';
export type OneOrMany<T> = T | T[];
@@ -12,16 +13,13 @@ export const enum MenuPosition {
CONTEXT_MENU,
}
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
-export interface IMenuItemState {
- id: string;
-
- disabled?: boolean;
- hidden?: boolean;
- checked?: boolean;
+export const enum MenuItemType {
+ /** default */
+ BUTTON,
+ SELECTOR,
}
-export interface IMenuItem {
+interface IMenuItemBase {
id: string;
menu: OneOrMany<MenuPosition>;
subMenus?: string[]; // submenu id list
@@ -29,6 +27,9 @@ export interface IMenuItem {
label?: string | ICustomLabelType | ComponentChildren;
title: string;
+
+ className?: string;
+
icon?: string;
tooltip?: string;
description?: string;
@@ -36,13 +37,35 @@ export interface IMenuItem {
hidden$?: Observable<boolean>;
activated$?: Observable<boolean>;
disabled$?: Observable<boolean>;
+
+ type?: MenuItemType;
+}
+
+export interface IMenuButtonItem extends IMenuItemBase {}
+
+export interface IMenuSelectorItem extends IMenuItemBase {
+ type: MenuItemType.SELECTOR;
+
+ display?: DisplayTypes;
+
+ label?: {
+ name: string;
+ props: any; // TODO: fix later
+ };
+
+ selectType: SelectTypes;
+ selections?: Array<number | BaseSelectProps>;
}
-export interface IDisplayMenuItem extends IMenuItem {
+export type IMenuItem = IMenuButtonItem | IMenuSelectorItem;
+
+// TODO@wzhudev: maybe we should separate `IMenuItemState` to different types of menu items.
+
+export type IDisplayMenuItem<T extends IMenuItem> = T & {
/** MenuService should get responsible shortcut and display on the UI. */
shortcut?: string;
- subMenuItems?: IDisplayMenuItem[];
-}
+ subMenuItems?: Array<IDisplayMenuItem<any>>;
+};
export const IMenuService = createIdentifier<IMenuService>('univer.menu-service');
@@ -88,12 +111,12 @@ export class DesktopMenuService extends Disposable implements IMenuService {
});
}
- getMenuItems(positions: MenuPosition): IDisplayMenuItem[] {
+ getMenuItems(positions: MenuPosition): Array<IDisplayMenuItem<any>> {
if (this._menuByPositions.has(positions)) {
return [...this._menuByPositions.get(positions)!.values()].map((menu) => this.getDisplayMenuItems(menu));
}
- return [] as IDisplayMenuItem[];
+ return [] as Array<IDisplayMenuItem<any>>;
}
getMenuItem(id: string): IMenuItem | null {
@@ -104,7 +127,7 @@ export class DesktopMenuService extends Disposable implements IMenuService {
return null;
}
- private getDisplayMenuItems(menuItem: IMenuItem): IDisplayMenuItem {
+ private getDisplayMenuItems(menuItem: IMenuItem): IDisplayMenuItem<any> {
const shortcut = this._shortcutService.getCommandShortcut(menuItem.id);
if (!shortcut) {
return menuItem;
diff --git a/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts b/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts
index 5c9486c8f0..5f89aa41aa 100644
--- a/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts
@@ -1,41 +1,24 @@
import { Inject, Injector, SkipSelf } from '@wendellhu/redi';
-import { BorderInfo, ISelectionManager, SelectionManager } from '@univerjs/base-sheets';
-import { BaseSelectChildrenProps, BaseSelectProps, ColorPicker, ComponentManager, ICustomComponent, IMenuService, MenuPosition } from '@univerjs/base-ui';
-import {
- BorderType,
- CommandManager,
- DEFAULT_STYLES,
- HorizontalAlign,
- IKeyValue,
- ISheetActionData,
- SheetActionBase,
- Tools,
- UIObserver,
- VerticalAlign,
- WrapStrategy,
- Range,
- ITextRotation,
- ICurrentUniverService,
- ObserverManager,
- Disposable,
-} from '@univerjs/core';
+import { ISelectionManager, SelectionManager } from '@univerjs/base-sheets';
+import { BaseSelectChildrenProps, BaseSelectProps, ColorPicker, ComponentManager, IMenuService, MenuPosition } from '@univerjs/base-ui';
+import { UIObserver, ICurrentUniverService, ObserverManager, Disposable } from '@univerjs/core';
import { ComponentChildren } from 'preact';
-import { DefaultToolbarConfig, SheetToolbarConfig, SHEET_UI_PLUGIN_NAME } from '../Basics';
+import { SheetToolbarConfig, SHEET_UI_PLUGIN_NAME } from '../Basics';
import { ColorSelect, LineBold, LineColor, Toolbar } from '../View';
-import {
- FONT_FAMILY_CHILDREN,
- FONT_SIZE_CHILDREN,
- BORDER_LINE_CHILDREN,
- BORDER_SIZE_CHILDREN,
- MERGE_CHILDREN,
- HORIZONTAL_ALIGN_CHILDREN,
- VERTICAL_ALIGN_CHILDREN,
- TEXT_WRAP_CHILDREN,
- TEXT_ROTATE_CHILDREN,
-} from '../View/Toolbar/Const';
-import styles from '../View/Toolbar/index.module.less';
-import { BoldMenuItemFactory, ItalicMenuItemFactory, RedoMenuItemFactory, StrikeThroughMenuItemFactory, UnderlineMenuItemFactory, UndoMenuItemFactory } from './menu';
+import {
+ BackgroundColorSelectorMenuItemFactory,
+ BoldMenuItemFactory,
+ CellBorderSelectorMenuItemFactory,
+ FontColorSelectorMenuItemFactory,
+ FontFamilySelectorMenuItemFactory,
+ FontSizeSelectorMenuItemFactory,
+ ItalicMenuItemFactory,
+ RedoMenuItemFactory,
+ StrikeThroughMenuItemFactory,
+ UnderlineMenuItemFactory,
+ UndoMenuItemFactory,
+} from './menu';
export interface BaseToolbarSelectProps extends BaseSelectProps {
children?: BaseSelectChildrenProps[];
@@ -62,29 +45,8 @@ export class ToolbarUIController extends Disposable {
/**
* @deprecated
*/
- private _toolList: IToolbarItemProps[];
-
- private _config: SheetToolbarConfig;
-
- private _lineColor: LineColor;
-
- private _lineBold: LineBold;
-
- private _colorSelect1: ColorSelect;
-
- private _textColor: string = '#000';
+ private _toolList: IToolbarItemProps[] = [];
- private _colorSelect2: ColorSelect;
-
- private _background: string = '#fff';
-
- private _borderInfo: BorderInfo = {
- type: BorderType.ALL,
- color: '#000',
- style: 1,
- }; //存储边框信息
-
- // eslint-disable-next-line max-lines-per-function
constructor(
config: SheetToolbarConfig | undefined,
@Inject(Injector) private readonly _injector: Injector,
@@ -96,277 +58,7 @@ export class ToolbarUIController extends Disposable {
) {
super();
- this._config = Tools.deepMerge({}, DefaultToolbarConfig, config);
-
- // TODO: @wzhudev: toolbar configurations should be moved to command system
- this._toolList = [
- {
- type: 0,
- tooltip: 'toolbar.font',
- className: styles.selectLabelString,
- name: 'font',
- show: this._config.font,
- border: true,
- onMainClick: () => {
- this.hideTooltip();
- },
- onClick: (fontFamily: string) => {
- this.setFontFamily(fontFamily);
- },
- children: FONT_FAMILY_CHILDREN,
- },
- {
- type: 1,
- tooltip: 'toolbar.fontSize',
- label: String(DEFAULT_STYLES.fs),
- name: 'fontSize',
- show: this._config.fontSize,
- onClick: (fontSize: number) => {
- this.setFontSize(fontSize);
- },
- onMainClick: () => {
- this.hideTooltip();
- },
- onPressEnter: (fontSize: number) => {
- this.setFontSize(fontSize);
- this.hideTooltip();
- },
- children: FONT_SIZE_CHILDREN,
- },
-
- {
- type: 5,
- tooltip: 'toolbar.textColor.main',
- label: {
- name: SHEET_UI_PLUGIN_NAME + ColorSelect.name,
- props: {
- getComponent: (ref: ColorSelect) => {
- this._colorSelect1 = ref;
- },
- color: '#000',
- label: {
- name: 'TextColorIcon',
- },
- },
- },
- onClick: () => {
- this.hideTooltip();
- const textColor = this._toolList.find((item) => item.name === 'textColor');
- if (!textColor || !textColor.label) return;
- if (!(textColor.label as ICustomComponent).props?.color) return;
- (textColor.label as ICustomComponent).props!.color = this._textColor;
- this.changeColor(this._textColor);
- },
- hideSelectedIcon: true,
- className: styles.selectColorPickerParent,
- children: [
- {
- label: 'toolbar.resetColor',
- },
- {
- label: {
- name: SHEET_UI_PLUGIN_NAME + ColorPicker.name,
- props: {
- onClick: (color: string, e: MouseEvent) => {
- this._colorSelect1.setColor(color);
- this._textColor = color;
- },
- },
- },
- className: styles.selectColorPicker,
- },
- ],
- name: 'textColor',
- show: this._config.textColor,
- },
- {
- type: 5,
- tooltip: 'toolbar.fillColor.main',
- label: {
- name: SHEET_UI_PLUGIN_NAME + ColorSelect.name,
- props: {
- getComponent: (ref: ColorSelect) => {
- this._colorSelect2 = ref;
- },
- color: '#fff',
- label: {
- name: 'FillColorIcon',
- },
- },
- },
- onClick: () => {
- this.hideTooltip();
- const fillColor = this._toolList.find((item) => item.name === 'fillColor');
- if (!fillColor || !fillColor.label) return;
- if (!(fillColor.label as ICustomComponent).props?.color) return;
- (fillColor.label as ICustomComponent).props!.color = this._background;
- this.setBackground(this._background);
- },
- hideSelectedIcon: true,
- className: styles.selectColorPickerParent,
- children: [
- {
- label: 'toolbar.resetColor',
- },
- {
- label: {
- name: SHEET_UI_PLUGIN_NAME + ColorPicker.name,
- props: {
- onClick: (color: string, e: MouseEvent) => {
- this._colorSelect2.setColor(color);
- this._background = color;
- },
- },
- },
- className: styles.selectColorPicker,
- },
- ],
- name: 'fillColor',
- show: this._config.fillColor,
- },
- {
- type: 3,
- display: 1,
- show: this._config.border,
- tooltip: 'toolbar.border.main',
- className: styles.selectDoubleString,
- onClick: (value: string) => {
- if (value) {
- this._borderInfo.type = value as BorderType;
- }
- this.hideTooltip();
- this.setBorder();
- },
- name: 'border',
- children: [
- ...BORDER_LINE_CHILDREN,
- {
- name: 'borderColor',
- label: {
- name: SHEET_UI_PLUGIN_NAME + LineColor.name,
- props: {
- color: '#000',
- label: 'borderLine.borderColor',
- getComponent: (ref: LineColor) => (this._lineColor = ref),
- },
- },
- unSelectable: true,
- className: styles.selectColorPickerParent,
- children: [
- {
- label: {
- name: SHEET_UI_PLUGIN_NAME + ColorPicker.name,
- props: {
- onClick: (color: string, e: MouseEvent) => {
- this._lineColor.setColor(color);
- this._borderInfo.color = color;
- const borderItem = this._toolList.find((item) => item.name === 'border');
- const lineColor = borderItem?.children?.find((item) => item.name === 'borderColor');
- if (!lineColor || !lineColor.label) return;
- if (!(lineColor.label as ICustomComponent).props?.color) return;
- (lineColor.label as ICustomComponent).props!.color = color;
- },
- },
- },
- className: styles.selectColorPicker,
- onClick: (...arg) => {
- arg[0].stopPropagation();
- },
- },
- ],
- },
- {
- label: {
- name: SHEET_UI_PLUGIN_NAME + LineBold.name,
- props: {
- img: 0,
- label: 'borderLine.borderSize',
- getComponent: (ref: LineBold) => (this._lineBold = ref),
- },
- },
- onClick: (...arg) => {
- arg[0].stopPropagation();
- const label = BORDER_SIZE_CHILDREN[arg[2]].label;
- if (typeof label === 'string') {
- this._lineBold.setImg(label);
- } else {
- this._lineBold.setImg(label.name);
- }
- this._borderInfo.style = arg[1];
- },
- className: styles.selectLineBoldParent,
- unSelectable: true,
- children: BORDER_SIZE_CHILDREN,
- },
- ],
- },
- {
- type: 5,
- tooltip: 'toolbar.mergeCell.main',
- label: {
- name: 'MergeIcon',
- },
- show: this._config.mergeCell,
- onClick: (value: string) => {
- this.setMerge(value ?? 'all');
- this.hideTooltip();
- },
- name: 'mergeCell',
- children: MERGE_CHILDREN,
- },
- {
- type: 3,
- tooltip: 'toolbar.horizontalAlignMode.main',
- className: styles.selectDoubleString,
- display: 1,
- name: 'horizontalAlignMode',
- show: this._config.horizontalAlignMode,
- onClick: (value: HorizontalAlign) => {
- this.setHorizontalAlignment(value);
- this.hideTooltip();
- },
- children: HORIZONTAL_ALIGN_CHILDREN,
- },
- {
- type: 3,
- tooltip: 'toolbar.verticalAlignMode.main',
- className: styles.selectDoubleString,
- display: 1,
- name: 'verticalAlignMode',
- show: this._config.verticalAlignMode,
- onClick: (value: VerticalAlign) => {
- this.setVerticalAlignment(value);
- this.hideTooltip();
- },
- children: VERTICAL_ALIGN_CHILDREN,
- },
- {
- type: 3,
- className: styles.selectDoubleString,
- tooltip: 'toolbar.textWrapMode.main',
- display: 1,
- name: 'textWrapMode',
- show: this._config.textWrapMode,
- onClick: (value: WrapStrategy) => {
- this.setWrapStrategy(value);
- this.hideTooltip();
- },
- children: TEXT_WRAP_CHILDREN,
- },
- {
- type: 3,
- className: styles.selectDoubleString,
- name: 'textRotateMode',
- tooltip: 'toolbar.textRotateMode.main',
- display: 1,
- show: this._config.textRotateMode,
- onClick: (value: number | string) => {
- this.setTextRotation(value);
- this.hideTooltip();
- },
- children: TEXT_ROTATE_CHILDREN,
- },
- ];
+ // this._config = Tools.deepMerge({}, DefaultToolbarConfig, config);
this._initialize();
}
@@ -409,202 +101,32 @@ export class ToolbarUIController extends Disposable {
this._globalObserverManager.requiredObserver<UIObserver<T>>('onUIChangeObservable', 'core').notifyObservers(msg);
}
- changeColor(color: string) {
- this.setFontColor(color);
- }
-
- setFontFamily(fontFamily: string) {
- const msg = {
- name: 'fontFamily',
- value: fontFamily,
- };
- this.setUIObserve(msg);
- }
-
- setFontSize(fontSize: number) {
- const msg = {
- name: 'fontSize',
- value: fontSize,
- };
- this.setUIObserve<number>(msg);
- }
-
- setFontColor(color: string) {
- const msg = {
- name: 'fontColor',
- value: color,
- };
- this.setUIObserve(msg);
- }
-
- setBackground(color: string) {
- const msg = {
- name: 'background',
- value: color,
- };
- this.setUIObserve(msg);
- }
-
- setMerge(value: string) {
- const msg = {
- name: 'merge',
- value,
- };
- this.setUIObserve(msg);
- }
-
- setTextRotation(value: string | number) {
- const msg = {
- name: 'textRotation',
- value,
- };
- this.setUIObserve(msg);
- }
-
- setWrapStrategy(value: WrapStrategy) {
- const msg = {
- name: 'wrapStrategy',
- value,
- };
- this.setUIObserve<number>(msg);
- }
-
- setVerticalAlignment(value: VerticalAlign) {
- const msg = {
- name: 'verticalAlignment',
- value,
- };
- this.setUIObserve<number>(msg);
- }
-
- setHorizontalAlignment(value: HorizontalAlign) {
- const msg = {
- name: 'horizontalAlignment',
- value,
- };
- this.setUIObserve<number>(msg);
- }
-
- setBorder() {
- const msg = {
- name: 'borderInfo',
- value: this._borderInfo,
- };
- this.setUIObserve<IKeyValue>(msg);
- }
-
- hideTooltip() {
- const dom = this._toolbar.base as HTMLDivElement;
- const tooltip = dom.querySelectorAll(`.${styles.tooltipTitle}.${styles.bottom}`);
- tooltip.forEach((item) => {
- (item as HTMLSpanElement).style.display = 'none';
- });
- }
-
- // eslint-disable-next-line max-lines-per-function
- private _changeToolbarState(range: Range): void {
- // FIXME@wzhudev: this is anti-pattern too, it should not compose all the toolbar items here
- const workbook = this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook();
- const worksheet = workbook.getActiveSheet();
- if (worksheet) {
- const fontSize = range.getFontSize();
- const fontWeight = range.getFontWeight();
- const fontName = range.getFontFamily();
- const fontItalic = range.getFontStyle();
- const horizontalAlign = range.getHorizontalAlignment() ?? HorizontalAlign.LEFT;
- const verticalAlign = range.getVerticalAlignment() ?? VerticalAlign.BOTTOM;
- const rotation = range.getTextRotation() as ITextRotation;
- const warp = range.getWrapStrategy() ?? WrapStrategy.CLIP;
-
- const textRotateModeItem = this._toolList.find((item) => item.name === 'textRotateMode');
- const fontSizeItem = this._toolList.find((item) => item.name === 'fontSize');
- const fontNameItem = this._toolList.find((item) => item.name === 'font');
- const fontBoldItem = this._toolList.find((item) => item.name === 'bold');
- const fontItalicItem = this._toolList.find((item) => item.name === 'italic');
- const horizontalAlignModeItem = this._toolList.find((item) => item.name === 'horizontalAlignMode');
- const verticalAlignModeItem = this._toolList.find((item) => item.name === 'verticalAlignMode');
- const textWrapMode = this._toolList.find((item) => item.name === 'textWrapMode');
-
- if (fontNameItem) {
- fontNameItem.children?.forEach((item) => {
- item.selected = fontName === item.value;
- });
- }
- if (fontSizeItem) {
- fontSizeItem.label = fontSize.toString();
- }
- if (fontBoldItem) {
- fontBoldItem.active = !!fontWeight;
- }
- if (fontItalicItem) {
- fontItalicItem.active = !!fontItalic;
- }
- if (horizontalAlignModeItem) {
- horizontalAlignModeItem.children?.forEach((item) => {
- item.selected = horizontalAlign === item.value;
- });
- }
- if (textRotateModeItem) {
- textRotateModeItem.children?.forEach((item) => {
- if ((rotation as ITextRotation).v) {
- item.selected = item.value === 'v';
- } else {
- item.selected = (rotation as ITextRotation).a === item.value;
- }
- });
- }
- if (verticalAlignModeItem) {
- verticalAlignModeItem.children?.forEach((item) => {
- item.selected = verticalAlign === item.value;
- });
- }
- if (textWrapMode) {
- textWrapMode.children?.forEach((item) => {
- item.selected = warp === item.value;
- });
- }
-
- this.setToolbar();
- }
- }
-
- private _initializeToolbar(): void {
- // NOTE@wzhudev: now we register menu items that only display in the toolbar here. In fact we should register all commands and menu items and shortcuts
- // in a single controller. I will do that layer.
- [UndoMenuItemFactory, RedoMenuItemFactory, BoldMenuItemFactory, ItalicMenuItemFactory, UnderlineMenuItemFactory, StrikeThroughMenuItemFactory].forEach((factory) => {
- this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory)));
- });
- }
-
private _initialize() {
+ // TODO@wzhudev: this should be builtin and requires no registration process.
const componentManager = this._componentManager;
componentManager.register(SHEET_UI_PLUGIN_NAME + ColorSelect.name, ColorSelect);
componentManager.register(SHEET_UI_PLUGIN_NAME + ColorPicker.name, ColorPicker);
componentManager.register(SHEET_UI_PLUGIN_NAME + LineColor.name, LineColor);
componentManager.register(SHEET_UI_PLUGIN_NAME + LineBold.name, LineBold);
+ }
- CommandManager.getCommandObservers().add(({ actions }) => {
- if (!actions || actions.length === 0) return;
- const action = actions[0] as SheetActionBase<ISheetActionData, ISheetActionData, void>;
-
- const currentUnitId = this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook().getUnitId();
-
- // TODO not use try catch
- try {
- action.getWorkBook();
- } catch (error) {
- return;
- }
-
- const actionUnitId = action.getWorkBook().getUnitId();
-
- if (currentUnitId !== actionUnitId) return;
-
- const manager = this._selectionManager;
- const range = manager.getCurrentCell();
- if (range) {
- this._changeToolbarState(range);
- }
+ private _initializeToolbar(): void {
+ // TODO@wzhudev: now we register menu items that only display in the toolbar here. In fact we should register all commands and menu items and shortcuts
+ // in a single controller. I will do that layer.
+ [
+ UndoMenuItemFactory,
+ RedoMenuItemFactory,
+ FontFamilySelectorMenuItemFactory,
+ FontSizeSelectorMenuItemFactory,
+ BoldMenuItemFactory,
+ ItalicMenuItemFactory,
+ UnderlineMenuItemFactory,
+ StrikeThroughMenuItemFactory,
+ FontColorSelectorMenuItemFactory,
+ BackgroundColorSelectorMenuItemFactory,
+ CellBorderSelectorMenuItemFactory,
+ ].forEach((factory) => {
+ this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory)));
});
}
}
diff --git a/packages/ui-plugin-sheets/src/Controller/menu.ts b/packages/ui-plugin-sheets/src/Controller/menu.ts
index 2fb3fea045..8d4ffded86 100644
--- a/packages/ui-plugin-sheets/src/Controller/menu.ts
+++ b/packages/ui-plugin-sheets/src/Controller/menu.ts
@@ -14,13 +14,23 @@ import {
SetWorksheetRowHeightCommand,
RemoveRowCommand,
RemoveColCommand,
+ SetFontFamilyCommand,
+ SetFontSizeCommand,
+ SetTextColorCommand,
+ SetBackgroundColorCommand,
+ SetCellBorderCommand,
} from '@univerjs/base-sheets';
-import { IMenuItem, MenuPosition } from '@univerjs/base-ui';
+import { ColorPicker, DisplayTypes, IMenuItem, IMenuSelectorItem, MenuItemType, MenuPosition, SelectTypes } from '@univerjs/base-ui';
import { FontItalic, FontWeight, ICommandService, IPermissionService, IUndoRedoService, RedoCommand, UndoCommand } from '@univerjs/core';
import { IAccessor } from '@wendellhu/redi';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { RightMenuInput } from '../View';
+import { BORDER_LINE_CHILDREN, BORDER_SIZE_CHILDREN, FONT_FAMILY_CHILDREN, FONT_SIZE_CHILDREN } from '../View/Toolbar/Const';
+import { ColorSelect, LineBold, LineColor } from '../View/Common';
+import { SHEET_UI_PLUGIN_NAME } from '../Basics/Const/PLUGIN_NAME';
+
+import styles from '../View/Toolbar/index.module.less';
export function UndoMenuItemFactory(accessor: IAccessor): IMenuItem {
const undoRedoService = accessor.get(IUndoRedoService);
@@ -224,15 +234,234 @@ export function StrikeThroughMenuItemFactory(accessor: IAccessor): IMenuItem {
};
}
-export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuItem {}
+export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {
+ // NOTE: we could get more font options from (a) font service, so user can provide their own fonts
+
+ return {
+ id: SetFontFamilyCommand.id,
+ title: 'font',
+ tooltip: 'toolbar.font',
+ selectType: SelectTypes.SINGLE,
+ type: MenuItemType.SELECTOR,
+ menu: [MenuPosition.TOOLBAR],
+ selections: FONT_FAMILY_CHILDREN,
+ };
+}
-export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuItem {}
+export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {
+ return {
+ id: SetFontSizeCommand.id,
+ title: 'fontSize',
+ tooltip: 'toolbar.fontSize',
+ type: MenuItemType.SELECTOR,
+ selectType: SelectTypes.INPUT,
+ menu: [MenuPosition.TOOLBAR],
+ selections: FONT_SIZE_CHILDREN,
+ };
+}
+
+export function FontColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {
+ return {
+ id: SetTextColorCommand.id,
+ title: 'textColor',
+ type: MenuItemType.SELECTOR,
+ selectType: SelectTypes.DOUBLEFIX,
+ menu: [MenuPosition.TOOLBAR],
+ label: {
+ name: SHEET_UI_PLUGIN_NAME + ColorSelect.name,
+ props: {
+ getComponent: (ref: ColorSelect) => {},
+ color: '#000',
+ label: {
+ name: 'TextColorIcon',
+ },
+ },
+ },
+ className: styles.selectColorPickerParent,
+ selections: [
+ {
+ label: 'toolbar.resetColor',
+ },
+ {
+ label: {
+ name: SHEET_UI_PLUGIN_NAME + ColorPicker.name,
+ props: {
+ onClick: (color: string, e: MouseEvent) => {},
+ },
+ },
+ className: styles.selectColorPicker,
+ },
+ ],
+ };
+}
-export function FontColorSelectorMenuItemFactory(accessor: IAccessor): IMenuItem {}
+export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {
+ return {
+ id: SetBackgroundColorCommand.id,
+ title: 'fillColor',
+ type: MenuItemType.SELECTOR,
+ selectType: SelectTypes.DOUBLEFIX,
+ menu: [MenuPosition.TOOLBAR],
+ label: {
+ name: SHEET_UI_PLUGIN_NAME + ColorSelect.name,
+ props: {
+ getComponent: (ref: ColorSelect) => {},
+ color: '#fff',
+ label: {
+ name: 'FillColorIcon',
+ },
+ },
+ },
+ className: styles.selectColorPickerParent,
+ selections: [
+ {
+ label: 'toolbar.resetColor',
+ },
+ {
+ label: {
+ name: SHEET_UI_PLUGIN_NAME + ColorPicker.name,
+ props: {
+ onClick: (color: string, e: MouseEvent) => {},
+ },
+ },
+ className: styles.selectColorPicker,
+ },
+ ],
+ };
+}
-export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMenuItem {}
+export function CellBorderSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {
+ return {
+ id: SetCellBorderCommand.id,
+ title: 'border',
+ display: DisplayTypes.SUFFIX,
+ menu: [MenuPosition.TOOLBAR],
+ type: MenuItemType.SELECTOR,
+ selectType: SelectTypes.DOUBLEFIX,
+ selections: [
+ ...BORDER_LINE_CHILDREN,
+ {
+ name: 'borderColor',
+ label: {
+ name: SHEET_UI_PLUGIN_NAME + LineColor.name,
+ props: {
+ color: '#000',
+ label: 'borderLine.borderColor',
+ },
+ },
+ className: styles.selectColorPickerParent,
+ children: [
+ {
+ label: {
+ name: SHEET_UI_PLUGIN_NAME + ColorPicker.name,
+ props: {},
+ },
+ className: styles.selectColorPicker,
+ onClick: (...arg) => {
+ arg[0].stopPropagation();
+ },
+ },
+ ],
+ },
+ {
+ label: {
+ name: SHEET_UI_PLUGIN_NAME + LineBold.name,
+ props: {
+ img: 0,
+ label: 'borderLine.borderSize',
+ },
+ },
+ onClick: (...arg) => {},
+ className: styles.selectLineBoldParent,
+ children: BORDER_SIZE_CHILDREN,
+ },
+ ],
+ };
+}
-export function CellBorderSelectorMenuItemFactory(accessor: IAccessor): IMenuItem {}
+// this._toolList = [
+// {
+// type: 3,
+// display: 1,
+// show: this._config.border,
+// tooltip: 'toolbar.border.main',
+// className: styles.selectDoubleString,
+// onClick: (value: string) => {
+// if (value) {
+// this._borderInfo.type = value as BorderType;
+// }
+// this.hideTooltip();
+// this.setBorder();
+// },
+// name: 'border',
+// },
+// {
+// type: 5,
+// tooltip: 'toolbar.mergeCell.main',
+// label: {
+// name: 'MergeIcon',
+// },
+// show: this._config.mergeCell,
+// onClick: (value: string) => {
+// this.setMerge(value ?? 'all');
+// this.hideTooltip();
+// },
+// name: 'mergeCell',
+// children: MERGE_CHILDREN,
+// },
+// {
+// type: 3,
+// tooltip: 'toolbar.horizontalAlignMode.main',
+// className: styles.selectDoubleString,
+// display: 1,
+// name: 'horizontalAlignMode',
+// show: this._config.horizontalAlignMode,
+// onClick: (value: HorizontalAlign) => {
+// this.setHorizontalAlignment(value);
+// this.hideTooltip();
+// },
+// children: HORIZONTAL_ALIGN_CHILDREN,
+// },
+// {
+// type: 3,
+// tooltip: 'toolbar.verticalAlignMode.main',
+// className: styles.selectDoubleString,
+// display: 1,
+// name: 'verticalAlignMode',
+// show: this._config.verticalAlignMode,
+// onClick: (value: VerticalAlign) => {
+// this.setVerticalAlignment(value);
+// this.hideTooltip();
+// },
+// children: VERTICAL_ALIGN_CHILDREN,
+// },
+// {
+// type: 3,
+// className: styles.selectDoubleString,
+// tooltip: 'toolbar.textWrapMode.main',
+// display: 1,
+// name: 'textWrapMode',
+// show: this._config.textWrapMode,
+// onClick: (value: WrapStrategy) => {
+// this.setWrapStrategy(value);
+// this.hideTooltip();
+// },
+// children: TEXT_WRAP_CHILDREN,
+// },
+// {
+// type: 3,
+// className: styles.selectDoubleString,
+// name: 'textRotateMode',
+// tooltip: 'toolbar.textRotateMode.main',
+// display: 1,
+// show: this._config.textRotateMode,
+// onClick: (value: number | string) => {
+// this.setTextRotation(value);
+// this.hideTooltip();
+// },
+// children: TEXT_ROTATE_CHILDREN,
+// },
+// ];
export function MergeCellMenuItemFactory(accessor: IAccessor): IMenuItem {}
@@ -345,4 +574,4 @@ export function DeleteRangeMoveUpMenuItemFactory(accessor: IAccessor): IMenuItem
title: 'rightClick.moveUp',
parentId: `${DeleteRangeCommand.id}.parent`,
};
-}
\ No newline at end of file
+}
diff --git a/packages/ui-plugin-sheets/src/Controller/shortcut.controller.ts b/packages/ui-plugin-sheets/src/Controller/shortcut.controller.ts
index 9754d107d3..049d4af331 100644
--- a/packages/ui-plugin-sheets/src/Controller/shortcut.controller.ts
+++ b/packages/ui-plugin-sheets/src/Controller/shortcut.controller.ts
@@ -1,4 +1,13 @@
-import { ChangeSelectionCommand, ExpandSelectionCommand, SetBoldCommand, SetItalicCommand, SetStrikeThroughCommand, SetUnderlineCommand } from '@univerjs/base-sheets';
+import {
+ ChangeSelectionCommand,
+ ExpandSelectionCommand,
+ SetBoldCommand,
+ SetFontFamilyCommand,
+ SetFontSizeCommand,
+ SetItalicCommand,
+ SetStrikeThroughCommand,
+ SetUnderlineCommand,
+} from '@univerjs/base-sheets';
import { IShortcutService } from '@univerjs/base-ui';
import { Disposable, ICommandService } from '@univerjs/core';
import {
@@ -25,7 +34,17 @@ export class DesktopSheetShortcutController extends Disposable {
constructor(@IShortcutService private readonly _shortcutService: IShortcutService, @ICommandService private readonly _commandService: ICommandService) {
super();
- [ChangeSelectionCommand, ExpandSelectionCommand, SetBoldCommand, SetItalicCommand, SetStrikeThroughCommand, SetUnderlineCommand].forEach((c) => {
+ // NOTE@wzhudev: these commands are just here temporarily
+ [
+ ChangeSelectionCommand,
+ ExpandSelectionCommand,
+ SetBoldCommand,
+ SetItalicCommand,
+ SetStrikeThroughCommand,
+ SetUnderlineCommand,
+ SetFontFamilyCommand,
+ SetFontSizeCommand,
+ ].forEach((c) => {
this.disposeWithMe(this._commandService.registerCommand(c));
});
diff --git a/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx b/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
index 0e10bffaa2..382cbf211a 100644
--- a/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
+++ b/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
@@ -1,7 +1,7 @@
import { ComponentChild, Component } from 'preact';
import { Subscription } from 'rxjs';
-import { AppContext, Button, Tooltip, CustomLabel } from '@univerjs/base-ui';
+import { AppContext, Button, Tooltip, CustomLabel, IDisplayMenuItem, MenuItemType, Select, IMenuSelectorItem, IMenuItem } from '@univerjs/base-ui';
import { ICommandService } from '@univerjs/core';
import styles from './index.module.less';
@@ -11,7 +11,7 @@ export interface IToolbarItemStatus {
activated: boolean;
}
-export class ToolbarItem extends Component<IDisplayMenuItem, IToolbarItemStatus> {
+export class ToolbarItem extends Component<IDisplayMenuItem<IMenuItem>, IToolbarItemStatus> {
static override contextType = AppContext;
private disabledSubscription: Subscription | undefined;
@@ -39,12 +39,45 @@ export class ToolbarItem extends Component<IDisplayMenuItem, IToolbarItemStatus>
override componentWillMount() {
this.disabledSubscription?.unsubscribe();
+ this.activatedSubscription?.unsubscribe();
}
render(): ComponentChild {
- const { props, context } = this;
+ // here we render different types of toolbar item according to their type
+ switch (this.props.type) {
+ case MenuItemType.SELECTOR:
+ return this.renderSelectorType();
+ default:
+ return this.renderButtonType();
+ }
+ }
+
+ private renderSelectorType(): ComponentChild {
+ const { context, state } = this;
+ const commandService: ICommandService = context.injector.get(ICommandService);
+ const { disabled, activated } = state;
+
+ const props = this.props as IDisplayMenuItem<IMenuSelectorItem>;
+
+ return (
+ <Tooltip title={props.title + (props.shortcut ? ` (${props.shortcut})` : '')} placement="bottom">
+ <Select
+ className={props.className}
+ tooltip={props.tooltip}
+ label={props.label}
+ display={props.display}
+ type={props.selectType!}
+ children={props.selections!}
+ onClick={(value) => commandService.executeCommand(props.id, value)}
+ ></Select>
+ </Tooltip>
+ );
+ }
+
+ private renderButtonType(): ComponentChild {
+ const { props, context, state } = this;
const commandService: ICommandService = context.injector.get(ICommandService);
- const { disabled, activated } = this.state;
+ const { disabled, activated } = state;
return (
<Tooltip title={props.title + (props.shortcut ? ` (${props.shortcut})` : '')} placement="bottom">