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.

feat(sheet): format painter at toolbar (#307)

YuHong94f5f2f5b42c
diff --git a/packages/base-render/src/BaseObject.ts b/packages/base-render/src/BaseObject.ts
index 407cce7629..5bf0cb88bd 100644
--- a/packages/base-render/src/BaseObject.ts
+++ b/packages/base-render/src/BaseObject.ts
@@ -680,7 +680,7 @@ export abstract class BaseObject {
}
resetCursor() {
- this.setCursor(CURSOR_TYPE.DEFAULT);
+ this.getScene()?.resetCursor();
}
setCursor(val: CURSOR_TYPE) {
diff --git a/packages/base-render/src/Scene.ts b/packages/base-render/src/Scene.ts
index a2d9f68a69..fae52a8493 100644
--- a/packages/base-render/src/Scene.ts
+++ b/packages/base-render/src/Scene.ts
@@ -32,6 +32,8 @@ export class Scene extends ThinScene {
private _cursor: CURSOR_TYPE = CURSOR_TYPE.DEFAULT;
+ private _defaultCursor: CURSOR_TYPE = CURSOR_TYPE.DEFAULT;
+
private _addObject$ = new BehaviorSubject<Scene>(this);
readonly addObject$ = this._addObject$.asObservable();
@@ -145,7 +147,7 @@ export class Scene extends ThinScene {
}
resetCursor() {
- this.setCursor(CURSOR_TYPE.DEFAULT);
+ this.setCursor(this._defaultCursor);
}
setCursor(val: CURSOR_TYPE) {
@@ -157,6 +159,11 @@ export class Scene extends ThinScene {
engine.setCanvasCursor(val);
}
+ setDefaultCursor(val: CURSOR_TYPE) {
+ this._defaultCursor = val;
+ this.resetCursor();
+ }
+
resize(width?: number, height?: number) {
const preWidth = this.width;
if (width !== undefined) {
diff --git a/packages/base-sheets/src/SheetPlugin.ts b/packages/base-sheets/src/SheetPlugin.ts
index 760ab2788c..306c7f8daf 100644
--- a/packages/base-sheets/src/SheetPlugin.ts
+++ b/packages/base-sheets/src/SheetPlugin.ts
@@ -3,7 +3,7 @@ import { ICommandService, LocaleService, Plugin, PLUGIN_NAMES, PluginType } from
import { Dependency, Inject, Injector } from '@wendellhu/redi';
import { SetScrollOperation } from './commands/operations/scroll.operation';
-import { SetSelectionsOperation } from './commands/operations/selection.operation';
+import { SetCopySelectionsOperation, SetSelectionsOperation } from './commands/operations/selection.operation';
import { SetZoomRatioOperation } from './commands/operations/set-zoom-ratio.operation';
import { AutoHeightController } from './Controller/auto-height.controller';
import { BasicWorksheetController } from './Controller/BasicWorksheet.controller';
@@ -87,6 +87,7 @@ export class SheetPlugin extends Plugin {
private _initializeCommands(): void {
this._commandService.registerCommand(SetSelectionsOperation);
+ this._commandService.registerCommand(SetCopySelectionsOperation);
this._commandService.registerCommand(SetScrollOperation);
this._commandService.registerCommand(SetZoomRatioOperation);
}
diff --git a/packages/base-sheets/src/commands/operations/selection.operation.ts b/packages/base-sheets/src/commands/operations/selection.operation.ts
index d1cdfed101..e18bc2ab3b 100644
--- a/packages/base-sheets/src/commands/operations/selection.operation.ts
+++ b/packages/base-sheets/src/commands/operations/selection.operation.ts
@@ -21,3 +21,16 @@ export const SetSelectionsOperation: IOperation<ISetSelectionsOperationParams> =
return true;
},
};
+
+export const SetCopySelectionsOperation: IOperation<ISetSelectionsOperationParams> = {
+ id: 'sheet.operation.set-copy-selection',
+ type: CommandType.OPERATION,
+ handler: async (accessor, params) => {
+ const selectionManagerService = accessor.get(SelectionManagerService);
+ if (!params) {
+ return false;
+ }
+ selectionManagerService.replaceCopySelection(params!);
+ return true;
+ },
+};
diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.ts
index 4ac8677b6e..0419c9ba05 100644
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@ -152,8 +152,8 @@ export {
SetWorksheetRowHeightMutation,
} from './commands/mutations/set-worksheet-row-height.mutation';
export { SetScrollOperation } from './commands/operations/scroll.operation';
-export { SetSelectionsOperation } from './commands/operations/selection.operation';
export { type ISetSelectionsOperationParams } from './commands/operations/selection.operation';
+export { SetCopySelectionsOperation, SetSelectionsOperation } from './commands/operations/selection.operation';
export { SetZoomRatioOperation } from './commands/operations/set-zoom-ratio.operation';
export * from './Locale';
export { SheetSkeletonManagerService } from './services/sheet-skeleton-manager.service';
diff --git a/packages/base-sheets/src/services/selection-manager.service.ts b/packages/base-sheets/src/services/selection-manager.service.ts
index afd072d8c0..2fa474fb9c 100644
--- a/packages/base-sheets/src/services/selection-manager.service.ts
+++ b/packages/base-sheets/src/services/selection-manager.service.ts
@@ -8,7 +8,10 @@ import { IRange, ISelectionCell, makeCellRangeToRangeData, Nullable } from '@uni
import { IDisposable } from '@wendellhu/redi';
import { BehaviorSubject } from 'rxjs';
+import { ISetSelectionsOperationParams } from '../commands/operations/selection.operation';
+
export const NORMAL_SELECTION_PLUGIN_NAME = 'normalSelectionPluginName';
+export const COPY_SELECTION_PLUGIN_NAME = 'copySelectionPluginName';
export interface ISelectionManagerSearchParam {
pluginName: string;
@@ -30,12 +33,14 @@ export class SelectionManagerService implements IDisposable {
private readonly _selectionInfo: ISelectionInfo = new Map();
private _currentSelection: Nullable<ISelectionManagerSearchParam> = null;
+ private _currentCopySelection: Nullable<ISelectionManagerSearchParam> = null;
// private _currentStyle: ISelectionStyle = NORMAL_SELECTION_PLUGIN_STYLE;
// private _isSelectionEnabled: boolean = true;
private readonly _selectionInfo$ = new BehaviorSubject<Nullable<ISelectionWithStyle[]>>(null);
+ private readonly _copySelectionInfo$ = new BehaviorSubject<Nullable<ISelectionWithStyle[]>>(null);
// eslint-disable-next-line @typescript-eslint/member-ordering
readonly selectionInfo$ = this._selectionInfo$.asObservable();
@@ -165,6 +170,32 @@ export class SelectionManagerService implements IDisposable {
this.refresh(this._currentSelection);
}
+ replaceCopySelection(insertParam: ISetSelectionsOperationParams) {
+ let selectionDatas: ISelectionWithStyle[];
+ if (insertParam.selections.length === 0) {
+ selectionDatas = [];
+ } else {
+ selectionDatas = [
+ {
+ ...insertParam.selections[0],
+ style: this.createCopyPasteSelection(),
+ },
+ ];
+ }
+ this._replaceByParam({
+ selectionDatas,
+ pluginName: insertParam.pluginName,
+ unitId: insertParam.workbookId,
+ sheetId: insertParam.worksheetId,
+ });
+ this._currentCopySelection = {
+ pluginName: COPY_SELECTION_PLUGIN_NAME,
+ unitId: insertParam.workbookId,
+ sheetId: insertParam.worksheetId,
+ };
+ this.refresh();
+ }
+
replaceWithNoRefresh(selectionDatas: ISelectionWithStyle[]) {
if (this._currentSelection == null) {
return;
@@ -268,8 +299,12 @@ export class SelectionManagerService implements IDisposable {
return this._selectionInfo.get(pluginName)?.get(unitId)?.get(sheetId);
}
- private refresh(param: ISelectionManagerSearchParam): void {
- this._selectionInfo$.next(this._getSelectionDatas(param));
+ private refresh(param?: ISelectionManagerSearchParam): void {
+ const res = [
+ ...(this._getSelectionDatas(param) || this._getSelectionDatas(this._currentSelection) || []),
+ ...(this._getSelectionDatas(this._currentCopySelection) || []),
+ ];
+ this._selectionInfo$.next(res);
}
private _getFirstByParam(param: Nullable<ISelectionManagerSearchParam>): Readonly<Nullable<ISelectionWithStyle>> {
diff --git a/packages/base-ui/src/Common/ComponentManager.ts b/packages/base-ui/src/Common/ComponentManager.ts
index 5cc86cf817..7f65e0fe84 100644
--- a/packages/base-ui/src/Common/ComponentManager.ts
+++ b/packages/base-ui/src/Common/ComponentManager.ts
@@ -24,6 +24,7 @@ import {
CutIcon,
DeleteLineIcon,
FillColorIcon,
+ FormatPainterIcon,
ForwardIcon,
FullBorderIcon,
FxIcon,
@@ -67,6 +68,7 @@ export class ComponentManager {
const iconList: Record<string, Function> = {
ForwardIcon,
BackIcon,
+ FormatPainterIcon,
BoldIcon,
ItalicIcon,
DeleteLineIcon,
diff --git a/packages/base-ui/src/Components/Icon/Format/index.tsx b/packages/base-ui/src/Components/Icon/Format/index.tsx
index f14b050f6d..b050328d9f 100644
--- a/packages/base-ui/src/Components/Icon/Format/index.tsx
+++ b/packages/base-ui/src/Components/Icon/Format/index.tsx
@@ -1,4 +1,4 @@
-import { Redo24, Undo24 } from '@univerjs/icons';
+import { Painter24, Redo24, Undo24 } from '@univerjs/icons';
import { BaseIconProps, Icon } from '../AddIcon';
@@ -6,6 +6,8 @@ export const BackIcon = (props: BaseIconProps) => <Redo24 />;
export const ForwardIcon = (props: BaseIconProps) => <Undo24 />;
+export const FormatPainterIcon = (props: BaseIconProps) => <Painter24 {...props} />;
+
export const FormatIcon = (props: BaseIconProps) => (
<Icon spin={props.spin} rotate={props.rotate} name="foward" style={props.style}>
<svg
@@ -166,6 +168,7 @@ export default {
BackIcon,
ForwardIcon,
FormatIcon,
+ FormatPainterIcon,
NextIcon,
RightIcon,
CorrectIcon,
diff --git a/packages/base-ui/src/services/menu/menu.ts b/packages/base-ui/src/services/menu/menu.ts
index d5f89629a3..de06883ef6 100644
--- a/packages/base-ui/src/services/menu/menu.ts
+++ b/packages/base-ui/src/services/menu/menu.ts
@@ -23,6 +23,7 @@ export const enum MenuItemType {
interface IMenuItemBase<V> {
/** ID of the menu item. Normally it should be the same as the ID of the command that it would invoke. */
id: string;
+ subId?: string;
title: string;
description?: string;
icon?: string;
diff --git a/packages/base-ui/src/views/components/DocBars/Button/ToolbarButton.tsx b/packages/base-ui/src/views/components/DocBars/Button/ToolbarButton.tsx
index e0901b3a62..9045ca3854 100644
--- a/packages/base-ui/src/views/components/DocBars/Button/ToolbarButton.tsx
+++ b/packages/base-ui/src/views/components/DocBars/Button/ToolbarButton.tsx
@@ -19,6 +19,8 @@ export interface IBaseToolbarButtonProps {
/** Set the handler to handle `click` event */
onClick?: (event: React.MouseEvent<HTMLButtonElement>) => void;
+ onDoubleClick?: (event: React.MouseEvent<HTMLButtonElement>) => void;
+
/**
* Set the button is activated
* @default false
@@ -33,7 +35,16 @@ export interface IBaseToolbarButtonProps {
* Button Component
*/
export function ToolbarButton(props: IBaseToolbarButtonProps) {
- const { children, className, style, disabled = false, active = false, onClick, ...restProps } = props;
+ const {
+ children,
+ className,
+ style,
+ disabled = false,
+ active = false,
+ onClick,
+ onDoubleClick,
+ ...restProps
+ } = props;
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
if (disabled) {
@@ -43,6 +54,13 @@ export function ToolbarButton(props: IBaseToolbarButtonProps) {
onClick && onClick(e);
};
+ const handleDoubleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
+ if (disabled) {
+ e.preventDefault();
+ return;
+ }
+ onDoubleClick && onDoubleClick(e);
+ };
const _className = joinClassNames(
styles.toolbarBtn,
@@ -54,7 +72,14 @@ export function ToolbarButton(props: IBaseToolbarButtonProps) {
);
return (
- <button className={_className} style={style} disabled={disabled} onClick={handleClick} {...restProps}>
+ <button
+ className={_className}
+ style={style}
+ disabled={disabled}
+ onClick={handleClick}
+ onDoubleClick={handleDoubleClick}
+ {...restProps}
+ >
{children}
</button>
);
diff --git a/packages/base-ui/src/views/components/DocBars/ToolbarItem.tsx b/packages/base-ui/src/views/components/DocBars/ToolbarItem.tsx
index 78d361df0b..4bce9b9064 100644
--- a/packages/base-ui/src/views/components/DocBars/ToolbarItem.tsx
+++ b/packages/base-ui/src/views/components/DocBars/ToolbarItem.tsx
@@ -135,6 +135,7 @@ export class ToolbarItem extends Component<IDisplayMenuItem<IMenuItem>, IToolbar
className={styles.textButton}
disabled={disabled}
onClick={() => commandService.executeCommand(props.id)}
+ onDoubleClick={() => props.subId && commandService.executeCommand(props.subId)}
>
<CustomLabel label={icon ? { name: icon } : title} />
</ToolbarButton>
diff --git a/packages/ui-plugin-sheets/src/commands/commands/set-format-painter.command.ts b/packages/ui-plugin-sheets/src/commands/commands/set-format-painter.command.ts
new file mode 100644
index 0000000000..a7dd0ac856
--- /dev/null
+++ b/packages/ui-plugin-sheets/src/commands/commands/set-format-painter.command.ts
@@ -0,0 +1,91 @@
+import { SelectionManagerService, SetCopySelectionsOperation } from '@univerjs/base-sheets';
+import { COPY_SELECTION_PLUGIN_NAME } from '@univerjs/base-sheets/services/selection-manager.service.js';
+import { CommandType, ICommand, ICommandService, IUniverInstanceService } from '@univerjs/core';
+import { IAccessor } from '@wendellhu/redi';
+
+import { FormatPainterStatus, IFormatPainterService } from '../../services/format-painter/format-painter.service';
+import { SetFormatPainterOperation } from '../operations/set-format-painter.operation';
+
+export interface ISetFormatPainterCommandParams {
+ status: FormatPainterStatus;
+}
+
+export const SetInfiniteFormatPainterCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-infinite-format-painter',
+ handler: async (accessor: IAccessor) => {
+ const formatPainterService = accessor.get(IFormatPainterService);
+ const status = formatPainterService.getStatus();
+ let newStatus: FormatPainterStatus;
+ if (status !== FormatPainterStatus.OFF) {
+ newStatus = FormatPainterStatus.OFF;
+ } else {
+ newStatus = FormatPainterStatus.INFINITE;
+ }
+ const commandService = accessor.get(ICommandService);
+ await commandService.executeCommand(SetCopySelectionCommand.id, {
+ show: newStatus === FormatPainterStatus.INFINITE,
+ });
+
+ return commandService.executeCommand(SetFormatPainterOperation.id, { status: newStatus });
+ },
+};
+
+export const SetOnceFormatPainterCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-once-format-painter',
+ handler: async (accessor: IAccessor) => {
+ const formatPainterService = accessor.get(IFormatPainterService);
+ const status = formatPainterService.getStatus();
+ let newStatus: FormatPainterStatus;
+ if (status !== FormatPainterStatus.OFF) {
+ newStatus = FormatPainterStatus.OFF;
+ } else {
+ newStatus = FormatPainterStatus.ONCE;
+ }
+ const commandService = accessor.get(ICommandService);
+ await commandService.executeCommand(SetCopySelectionCommand.id, {
+ show: newStatus === FormatPainterStatus.ONCE,
+ });
+ return commandService.executeCommand(SetFormatPainterOperation.id, { status: newStatus });
+ },
+};
+
+export interface ISetCopySelectionCommandParams {
+ show?: boolean;
+}
+
+export const SetCopySelectionCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-copy-selection',
+ handler: async (accessor: IAccessor, params: ISetCopySelectionCommandParams) => {
+ const commandService = accessor.get(ICommandService);
+ const currentService = accessor.get(IUniverInstanceService);
+ const workbookId = currentService.getCurrentUniverSheetInstance().getUnitId();
+ const worksheetId = currentService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();
+ let setSelectionsParam;
+ if (params.show) {
+ const selectionManagerService = accessor.get(SelectionManagerService);
+ const selections = selectionManagerService.getSelections();
+ if (!selections || selections.length === 0) {
+ return false;
+ }
+ const { range, primary, style } = selections[0];
+ setSelectionsParam = {
+ workbookId,
+ worksheetId,
+ pluginName: COPY_SELECTION_PLUGIN_NAME,
+ selections: [{ range, primary, style: null }],
+ };
+ } else {
+ setSelectionsParam = {
+ workbookId,
+ worksheetId,
+ pluginName: COPY_SELECTION_PLUGIN_NAME,
+ selections: [],
+ };
+ }
+
+ return commandService.executeCommand(SetCopySelectionsOperation.id, setSelectionsParam);
+ },
+};
diff --git a/packages/ui-plugin-sheets/src/commands/operations/set-format-painter.operation.ts b/packages/ui-plugin-sheets/src/commands/operations/set-format-painter.operation.ts
new file mode 100644
index 0000000000..d78b35f7cb
--- /dev/null
+++ b/packages/ui-plugin-sheets/src/commands/operations/set-format-painter.operation.ts
@@ -0,0 +1,19 @@
+import { CommandType, IOperation } from '@univerjs/core';
+
+import { FormatPainterStatus, IFormatPainterService } from '../../services/format-painter/format-painter.service';
+
+export interface ISetFormatPainterOperationParams {
+ status: FormatPainterStatus;
+}
+export const SetFormatPainterOperation: IOperation<ISetFormatPainterOperationParams> = {
+ id: 'sheet.operation.set-format-painter',
+ type: CommandType.OPERATION,
+ handler: async (accessor, params) => {
+ if (!params) {
+ return false;
+ }
+ const formatPainterService = accessor.get(IFormatPainterService);
+ formatPainterService.setStatus(params.status);
+ return true;
+ },
+};
diff --git a/packages/ui-plugin-sheets/src/controller/format-painter/format-painter.controller.ts b/packages/ui-plugin-sheets/src/controller/format-painter/format-painter.controller.ts
new file mode 100644
index 0000000000..931fd6ebfa
--- /dev/null
+++ b/packages/ui-plugin-sheets/src/controller/format-painter/format-painter.controller.ts
@@ -0,0 +1,107 @@
+import { CURSOR_TYPE, IRenderManagerService } from '@univerjs/base-render';
+import {
+ ISetRangeValuesCommandParams,
+ ISetSelectionsOperationParams,
+ NORMAL_SELECTION_PLUGIN_NAME,
+ SelectionManagerService,
+ SetRangeValuesCommand,
+ SetSelectionsOperation,
+} from '@univerjs/base-sheets';
+import {
+ Disposable,
+ ICellData,
+ ICommandInfo,
+ ICommandService,
+ IRange,
+ IUniverInstanceService,
+ LifecycleStages,
+ OnLifecycle,
+} from '@univerjs/core';
+import { Inject } from '@wendellhu/redi';
+
+import { SetOnceFormatPainterCommand } from '../../commands/commands/set-format-painter.command';
+import { FormatPainterStatus, IFormatPainterService } from '../../services/format-painter/format-painter.service';
+
+@OnLifecycle(LifecycleStages.Rendered, FormatPainterController)
+export class FormatPainterController extends Disposable {
+ constructor(
+ @ICommandService private readonly _commandService: ICommandService,
+ @IFormatPainterService private readonly _formatPainterService: IFormatPainterService,
+ @IUniverInstanceService private readonly _currentService: IUniverInstanceService,
+ @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
+ @Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService
+ ) {
+ super();
+
+ this._initialize();
+ }
+
+ private _initialize() {
+ this._commandExecutedListener();
+ this._bindFormatPainterStatus();
+ }
+
+ private _bindFormatPainterStatus() {
+ this._formatPainterService.status$.subscribe((status) => {
+ const scene = this._renderManagerService.getCurrent()?.scene;
+ if (!scene) return;
+ if (status !== FormatPainterStatus.OFF) {
+ scene.setDefaultCursor(CURSOR_TYPE.CELL);
+ } else {
+ scene.setDefaultCursor(CURSOR_TYPE.DEFAULT);
+ }
+ });
+ }
+
+ private _commandExecutedListener() {
+ this.disposeWithMe(
+ this._commandService.onCommandExecuted((command: ICommandInfo) => {
+ if (
+ command.id === SetSelectionsOperation.id &&
+ (command.params as ISetSelectionsOperationParams)?.pluginName === NORMAL_SELECTION_PLUGIN_NAME
+ ) {
+ const isFormatPainterOn = this._formatPainterService.getStatus() !== FormatPainterStatus.OFF;
+ if (!isFormatPainterOn) return;
+ const { selections } = command.params as ISetSelectionsOperationParams;
+ const { range } = selections[selections.length - 1];
+ this._applyFormatPainter(range);
+ }
+ })
+ );
+ }
+
+ private _applyFormatPainter(range: IRange) {
+ const stylesMatrix = this._formatPainterService.getSelectionStyles();
+ const workbookId = this._currentService.getCurrentUniverSheetInstance().getUnitId();
+ const worksheetId = this._currentService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();
+ if (!stylesMatrix) return;
+ const styleRowsNum = stylesMatrix.getLength();
+ const styleColsNum = stylesMatrix.getSizeOf();
+ const styleValues: ICellData[][] = Array.from({ length: range.endRow - range.startRow + 1 }, () =>
+ Array.from({ length: range.endColumn - range.startColumn + 1 }, () => ({}))
+ );
+ styleValues.forEach((row, rowIndex) => {
+ row.forEach((col, colIndex) => {
+ const mappedRowIndex = rowIndex % styleRowsNum;
+ const mappedColIndex = colIndex % styleColsNum;
+ const style = stylesMatrix.getValue(mappedRowIndex, mappedColIndex);
+ if (style) {
+ styleValues[rowIndex][colIndex].s = style;
+ }
+ });
+ });
+
+ const setRangeValuesCommandParams: ISetRangeValuesCommandParams = {
+ worksheetId,
+ workbookId,
+ range,
+ value: styleValues,
+ };
+ this._commandService.executeCommand(SetRangeValuesCommand.id, setRangeValuesCommandParams);
+
+ // if the format painter is once, turn it off
+ if (this._formatPainterService.getStatus() === FormatPainterStatus.ONCE) {
+ this._commandService.executeCommand(SetOnceFormatPainterCommand.id);
+ }
+ }
+}
diff --git a/packages/ui-plugin-sheets/src/controller/menu/menu.ts b/packages/ui-plugin-sheets/src/controller/menu/menu.ts
index e595a2e76e..fa813446a7 100644
--- a/packages/ui-plugin-sheets/src/controller/menu/menu.ts
+++ b/packages/ui-plugin-sheets/src/controller/menu/menu.ts
@@ -78,6 +78,11 @@ import { map } from 'rxjs/operators';
import { SHEET_UI_PLUGIN_NAME } from '../../Basics/Const/PLUGIN_NAME';
import { RenameSheetCommand } from '../../commands/commands/rename.command';
+import {
+ SetInfiniteFormatPainterCommand,
+ SetOnceFormatPainterCommand,
+} from '../../commands/commands/set-format-painter.command';
+import { FormatPainterStatus, IFormatPainterService } from '../../services/format-painter/format-painter.service';
export const CONTEXT_MENU_INPUT_LABEL = 'CONTEXT_MENU_INPUT';
@@ -119,6 +124,33 @@ export function RedoMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
};
}
+export function FormatPainterMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
+ const formatPainterService = accessor.get(IFormatPainterService);
+ return {
+ id: SetOnceFormatPainterCommand.id,
+ subId: SetInfiniteFormatPainterCommand.id,
+ type: MenuItemType.BUTTON,
+ icon: 'FormatPainterIcon',
+ title: 'Format Painter',
+ tooltip: 'toolbar.formatPainter',
+ positions: [MenuPosition.TOOLBAR],
+ activated$: new Observable<boolean>((subscriber) => {
+ let active = false;
+
+ const status$ = formatPainterService.status$.subscribe((s) => {
+ active = s !== FormatPainterStatus.OFF;
+ subscriber.next(active);
+ });
+
+ subscriber.next(active);
+
+ return () => {
+ status$.unsubscribe();
+ };
+ }),
+ };
+}
+
export function BoldMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
const commandService = accessor.get(ICommandService);
const permissionService = accessor.get(IPermissionService);
diff --git a/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts b/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
index e04ba05719..1564728307 100644
--- a/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
+++ b/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
@@ -24,9 +24,15 @@ import { connectInjector } from '@wendellhu/redi/react-bindings';
import { SHEET_UI_PLUGIN_NAME } from '../Basics';
import { RenameSheetCommand } from '../commands/commands/rename.command';
+import {
+ SetCopySelectionCommand,
+ SetInfiniteFormatPainterCommand,
+ SetOnceFormatPainterCommand,
+} from '../commands/commands/set-format-painter.command';
import { ShowMenuListCommand } from '../commands/commands/unhide.command';
import { SetActivateCellEditOperation } from '../commands/operations/activate-cell-edit.operation';
import { SetCellEditOperation } from '../commands/operations/cell-edit.operation';
+import { SetFormatPainterOperation } from '../commands/operations/set-format-painter.operation';
import { RightMenuInput } from '../View/RightMenu/RightMenuInput';
import { RightMenuItem } from '../View/RightMenu/RightMenuItem';
import { RenderSheetContent, RenderSheetFooter, RenderSheetHeader } from '../View/SheetContainer/SheetContainer';
@@ -47,6 +53,7 @@ import {
DeleteSheetMenuItemFactory,
FontFamilySelectorMenuItemFactory,
FontSizeSelectorMenuItemFactory,
+ FormatPainterMenuItemFactory,
FrozenMenuItemFactory,
HideColMenuItemFactory,
HideRowMenuItemFactory,
@@ -162,6 +169,10 @@ export class SheetUIController extends Disposable {
SetCellEditOperation,
SetActivateCellEditOperation,
+ SetOnceFormatPainterCommand,
+ SetCopySelectionCommand,
+ SetInfiniteFormatPainterCommand,
+ SetFormatPainterOperation,
].forEach((c) => {
this.disposeWithMe(this._commandService.registerCommand(c));
});
@@ -199,6 +210,7 @@ export class SheetUIController extends Disposable {
// toolbar
UndoMenuItemFactory,
RedoMenuItemFactory,
+ FormatPainterMenuItemFactory,
BoldMenuItemFactory,
ItalicMenuItemFactory,
UnderlineMenuItemFactory,
diff --git a/packages/ui-plugin-sheets/src/services/format-painter/format-painter.service.ts b/packages/ui-plugin-sheets/src/services/format-painter/format-painter.service.ts
new file mode 100644
index 0000000000..4faed0c2c4
--- /dev/null
+++ b/packages/ui-plugin-sheets/src/services/format-painter/format-painter.service.ts
@@ -0,0 +1,72 @@
+import { SelectionManagerService } from '@univerjs/base-sheets';
+import { ICellData, IStyleData, IUniverInstanceService, ObjectMatrix } from '@univerjs/core';
+import { Disposable } from '@univerjs/core/Shared/lifecycle.js';
+import { createIdentifier, Inject } from '@wendellhu/redi';
+import { BehaviorSubject, Observable } from 'rxjs';
+
+export interface IFormatPainterService {
+ status$: Observable<FormatPainterStatus>;
+ setStatus(status: FormatPainterStatus): void;
+ getStatus(): FormatPainterStatus;
+ getSelectionStyles(): ObjectMatrix<IStyleData>;
+}
+
+export enum FormatPainterStatus {
+ OFF,
+ ONCE,
+ INFINITE,
+}
+
+export const IFormatPainterService = createIdentifier<IFormatPainterService>('univer.format-painter-service');
+
+export class FormatPainterService extends Disposable implements IFormatPainterService {
+ readonly status$: Observable<FormatPainterStatus>;
+ private _selectionStyles: ObjectMatrix<IStyleData>;
+
+ private readonly _status$: BehaviorSubject<FormatPainterStatus>;
+
+ constructor(
+ @Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService,
+ @IUniverInstanceService private readonly _currentService: IUniverInstanceService
+ ) {
+ super();
+
+ this._status$ = new BehaviorSubject<FormatPainterStatus>(FormatPainterStatus.OFF);
+ this.status$ = this._status$.asObservable();
+ this._selectionStyles = new ObjectMatrix<IStyleData>();
+ }
+
+ setStatus(status: FormatPainterStatus) {
+ if (status !== FormatPainterStatus.OFF) {
+ this._getSelectionRangeStyle();
+ }
+ this._status$.next(status);
+ }
+
+ getStatus(): FormatPainterStatus {
+ return this._status$.getValue();
+ }
+
+ private _getSelectionRangeStyle() {
+ const selection = this._selectionManagerService.getLast();
+ const range = selection?.range;
+ if (!range) return;
+ const { startRow, endRow, startColumn, endColumn } = range;
+ const workbook = this._currentService.getCurrentUniverSheetInstance();
+ const worksheet = workbook?.getActiveSheet();
+ const cellData = worksheet.getCellMatrix();
+ const value = cellData.getFragments(startRow, endRow, startColumn, endColumn);
+
+ const styles = workbook.getStyles();
+ const stylesMatrix = new ObjectMatrix<IStyleData>();
+ value.forValue((r, c, cell) => {
+ cell = cell as ICellData;
+ stylesMatrix.setValue(r, c, styles.getStyleByCell(cell) || {});
+ });
+ this._selectionStyles = stylesMatrix;
+ }
+
+ getSelectionStyles() {
+ return this._selectionStyles;
+ }
+}
diff --git a/packages/ui-plugin-sheets/src/sheet-ui-plugin.ts b/packages/ui-plugin-sheets/src/sheet-ui-plugin.ts
index 9c3a0942b9..c61cdf8500 100644
--- a/packages/ui-plugin-sheets/src/sheet-ui-plugin.ts
+++ b/packages/ui-plugin-sheets/src/sheet-ui-plugin.ts
@@ -10,11 +10,13 @@ import { EndEditController } from './controller/editor/end-edit.controller';
import { InitializeEditorController } from './controller/editor/initialize-editor.controller';
import { StartEditController } from './controller/editor/start-edit.controller';
import { EditorBridgeController } from './controller/editor-bridge.controller';
+import { FormatPainterController } from './controller/format-painter/format-painter.controller';
import { SheetUIController } from './controller/sheet-ui.controller';
import { en } from './Locale';
import { ISheetClipboardService, SheetClipboardService } from './services/clipboard/clipboard.service';
import { CellEditorManagerService, ICellEditorManagerService } from './services/editor/cell-editor-manager.service';
import { EditorBridgeService, IEditorBridgeService } from './services/editor-bridge.service';
+import { FormatPainterService, IFormatPainterService } from './services/format-painter/format-painter.service';
import { ISheetBarService, SheetBarService } from './services/sheetbar/sheetbar.service';
export class SheetUIPlugin extends Plugin {
@@ -41,6 +43,7 @@ export class SheetUIPlugin extends Plugin {
[IEditorBridgeService, { useClass: EditorBridgeService }],
[ISheetClipboardService, { useClass: SheetClipboardService }],
[ISheetBarService, { useClass: SheetBarService }],
+ [IFormatPainterService, { useClass: FormatPainterService }],
// [ITextSelectionRenderManager, { useClass: TextSelectionRenderManager }],
// [TextSelectionManagerService],
[ICellEditorManagerService, { useClass: CellEditorManagerService }],
@@ -54,6 +57,7 @@ export class SheetUIPlugin extends Plugin {
[StartEditController],
[EditingController],
[EndEditController],
+ [FormatPainterController],
] as Dependency[]
).forEach((d) => injector.add(d));
}