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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat: add paste options (#4393)
ybzkyd758b217ec4f
diff --git a/packages/sheets-conditional-formatting-ui/src/controllers/cf.copy-paste.controller.ts b/packages/sheets-conditional-formatting-ui/src/controllers/cf.copy-paste.controller.tsindex ef759c0963..aedf2586d1 100644--- a/packages/sheets-conditional-formatting-ui/src/controllers/cf.copy-paste.controller.ts+++ b/packages/sheets-conditional-formatting-ui/src/controllers/cf.copy-paste.controller.ts@@ -16,7 +16,7 @@import type { IRange, Nullable, Workbook } from '@univerjs/core';import type { IAddConditionalRuleMutationParams, IConditionalFormattingRuleConfig, IConditionFormattingRule, IDeleteConditionalRuleMutationParams, ISetConditionalRuleMutationParams } from '@univerjs/sheets-conditional-formatting';-import type { IDiscreteRange } from '@univerjs/sheets-ui';+import type { IDiscreteRange, IPasteHookValueType } from '@univerjs/sheets-ui';import {Disposable,Inject,@@ -115,7 +115,7 @@ export class ConditionalFormattingCopyPasteController extends Disposable {copyInfo: {copyType: COPY_TYPE;copyRange?: IDiscreteRange;- pasteType: string;+ pasteType: IPasteHookValueType;}) {const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;@@ -135,8 +135,12 @@ export class ConditionalFormattingCopyPasteController extends Disposable {return { redos: [], undos: [] };}+ const specialPastes: IPasteHookValueType[] = [+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT, PREDEFINED_HOOK_NAME.DEFAULT_PASTE, PREDEFINED_HOOK_NAME.SPECIAL_PASTE_BESIDES_BORDER,+ ];+if (- ![PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT, PREDEFINED_HOOK_NAME.DEFAULT_PASTE, PREDEFINED_HOOK_NAME.SPECIAL_PASTE_BESIDES_BORDER].includes(+ !specialPastes.includes(copyInfo.pasteType)) {@@ -202,33 +206,33 @@ export class ConditionalFormattingCopyPasteController extends Disposable {repeatRange.forEach((item) => {matrix &&- matrix.forValue((row, col, copyRangeCfIdList) => {- const range = Rectangle.getPositionRange(- {- startRow: row,- endRow: row,- startColumn: col,- endColumn: col,- },- item.startRange- );+ matrix.forValue((row, col, copyRangeCfIdList) => {+ const range = Rectangle.getPositionRange(+ {+ startRow: row,+ endRow: row,+ startColumn: col,+ endColumn: col,+ },+ item.startRange+ );- const { row: _row, col: _col } = mapFunc(range.startRow, range.startColumn);+ const { row: _row, col: _col } = mapFunc(range.startRow, range.startColumn);- copyRangeCfIdList.forEach((cfId) => {- if (!effectedConditionalFormattingRuleMatrix[cfId]) {- const rule = getCurrentSheetCfRule(cfId);- const ruleMatrix = new ObjectMatrix<1>();- effectedConditionalFormattingRuleMatrix[cfId] = ruleMatrix;- rule.ranges.forEach((range) => {- Range.foreach(range, (row, col) => {- ruleMatrix.setValue(row, col, 1);+ copyRangeCfIdList.forEach((cfId) => {+ if (!effectedConditionalFormattingRuleMatrix[cfId]) {+ const rule = getCurrentSheetCfRule(cfId);+ const ruleMatrix = new ObjectMatrix<1>();+ effectedConditionalFormattingRuleMatrix[cfId] = ruleMatrix;+ rule.ranges.forEach((range) => {+ Range.foreach(range, (row, col) => {+ ruleMatrix.setValue(row, col, 1);+ });});- });- }- effectedConditionalFormattingRuleMatrix[cfId].setValue(_row, _col, 1);+ }+ effectedConditionalFormattingRuleMatrix[cfId].setValue(_row, _col, 1);+ });});- });});const redos = [];const undos = [];diff --git a/packages/sheets-data-validation-ui/src/controllers/dv-copy-paste.controller.ts b/packages/sheets-data-validation-ui/src/controllers/dv-copy-paste.controller.tsindex b059dbdeb2..5c91232e1a 100644--- a/packages/sheets-data-validation-ui/src/controllers/dv-copy-paste.controller.ts+++ b/packages/sheets-data-validation-ui/src/controllers/dv-copy-paste.controller.ts@@ -15,7 +15,7 @@*/import type { IRange, ISheetDataValidationRule, Nullable } from '@univerjs/core';-import type { IDiscreteRange } from '@univerjs/sheets-ui';+import type { IDiscreteRange, IPasteHookValueType } from '@univerjs/sheets-ui';import { Disposable, Inject, Injector, ObjectMatrix, queryObjectMatrix, Rectangle } from '@univerjs/core';import { DATA_VALIDATION_PLUGIN_NAME, getDataValidationDiffMutations, SheetDataValidationModel } from '@univerjs/sheets-data-validation';import { COPY_TYPE, getRepeatRange, ISheetClipboardService, PREDEFINED_HOOK_NAME, rangeToDiscreteRange, virtualizeDiscreteRanges } from '@univerjs/sheets-ui';@@ -78,7 +78,7 @@ export class DataValidationCopyPasteController extends Disposable {copyInfo: {copyType: COPY_TYPE;copyRange?: IDiscreteRange;- pasteType: string;+ pasteType: IPasteHookValueType;unitId: string;subUnitId: string;}@@ -94,16 +94,14 @@ export class DataValidationCopyPasteController extends Disposable {return { redos: [], undos: [] };}- if (- [- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_COL_WIDTH,- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_VALUE,- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT,- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA,- ].includes(- copyInfo.pasteType- )- ) {+ const specialPastes: IPasteHookValueType[] = [+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_COL_WIDTH,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_VALUE,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA,+ ];++ if (specialPastes.includes(copyInfo.pasteType)) {return { redos: [], undos: [] };}diff --git a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-copy-paste.controller.ts b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-copy-paste.controller.tsindex 86b44198e2..cdd930e674 100644--- a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-copy-paste.controller.ts+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-copy-paste.controller.ts@@ -17,7 +17,7 @@import type { IMutationInfo, IRange, Nullable } from '@univerjs/core';import type { IDrawingJsonUndo1 } from '@univerjs/drawing';import type { ISheetDrawing } from '@univerjs/sheets-drawing';-import type { IDiscreteRange, ISheetDiscreteRangeLocation } from '@univerjs/sheets-ui';+import type { IDiscreteRange, IPasteHookValueType, ISheetDiscreteRangeLocation } from '@univerjs/sheets-ui';import { Disposable, Tools } from '@univerjs/core';import { IRenderManagerService } from '@univerjs/engine-render';import { DrawingApplyType, ISheetDrawingService, SetDrawingApplyMutation, SheetDrawingAnchorType } from '@univerjs/sheets-drawing';@@ -107,7 +107,7 @@ export class SheetsDrawingCopyPasteController extends Disposable {copyInfo: {copyType: COPY_TYPE;copyRange?: IDiscreteRange;- pasteType: string;+ pasteType: IPasteHookValueType;unitId: string;subUnitId: string;}@@ -116,16 +116,13 @@ export class SheetsDrawingCopyPasteController extends Disposable {return { redos: [], undos: [] };}- if (- [- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_COL_WIDTH,- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_VALUE,- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT,- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA,- ].includes(- copyInfo.pasteType- )- ) {+ const specialPastes: IPasteHookValueType[] = [+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_COL_WIDTH,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_VALUE,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA,+ ];+ if (specialPastes.includes(copyInfo.pasteType)) {return { redos: [], undos: [] };}diff --git a/packages/sheets-formula-ui/src/controllers/formula-clipboard.controller.ts b/packages/sheets-formula-ui/src/controllers/formula-clipboard.controller.tsindex 438de8bf8d..21f860b95a 100644--- a/packages/sheets-formula-ui/src/controllers/formula-clipboard.controller.ts+++ b/packages/sheets-formula-ui/src/controllers/formula-clipboard.controller.ts@@ -16,7 +16,7 @@import type { IAccessor, ICellData, IMutationInfo, Workbook } from '@univerjs/core';import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';-import type { ICellDataWithSpanInfo, ICopyPastePayload, IDiscreteRange, ISheetClipboardHook, ISheetDiscreteRangeLocation } from '@univerjs/sheets-ui';+import type { ICellDataWithSpanInfo, ICopyPastePayload, IDiscreteRange, IPasteHookValueType, ISheetClipboardHook, ISheetDiscreteRangeLocation } from '@univerjs/sheets-ui';import {DEFAULT_EMPTY_DOCUMENT_VALUE,Disposable,@@ -82,7 +82,11 @@ export class FormulaClipboardController extends Disposable {isSpecialPaste: boolean) {// Intercept scenarios where formulas do not need to be processed, and only process default paste and paste formulas only- if ([PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT, PREDEFINED_HOOK_NAME.SPECIAL_PASTE_COL_WIDTH].includes(payload.pasteType)) {+ const specialPastes: IPasteHookValueType[] = [+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_COL_WIDTH,+ ];+ if (specialPastes.includes(payload.pasteType)) {return {undos: [],redos: [],diff --git a/packages/sheets-hyper-link-ui/src/controllers/copy-paste.controller.ts b/packages/sheets-hyper-link-ui/src/controllers/copy-paste.controller.tsindex c780a8a9c7..5d69b16e6a 100644--- a/packages/sheets-hyper-link-ui/src/controllers/copy-paste.controller.ts+++ b/packages/sheets-hyper-link-ui/src/controllers/copy-paste.controller.ts@@ -15,7 +15,7 @@*/import type { IMutationInfo, IRange, Nullable } from '@univerjs/core';-import type { IDiscreteRange, ISheetDiscreteRangeLocation } from '@univerjs/sheets-ui';+import type { IDiscreteRange, IPasteHookValueType, ISheetDiscreteRangeLocation } from '@univerjs/sheets-ui';import { Disposable, Inject, Injector, ObjectMatrix, Range, Rectangle, Tools } from '@univerjs/core';import { AddHyperLinkMutation, HyperLinkModel, RemoveHyperLinkMutation } from '@univerjs/sheets-hyper-link';import { COPY_TYPE, getRepeatRange, ISheetClipboardService, PREDEFINED_HOOK_NAME, rangeToDiscreteRange, virtualizeDiscreteRanges } from '@univerjs/sheets-ui';@@ -139,7 +139,7 @@ export class SheetsHyperLinkCopyPasteController extends Disposable {copyInfo: {copyType: COPY_TYPE;copyRange?: IDiscreteRange;- pasteType: string;+ pasteType: IPasteHookValueType;unitId: string;subUnitId: string;}@@ -152,16 +152,14 @@ export class SheetsHyperLinkCopyPasteController extends Disposable {return { redos: [], undos: [] };}- if (- [- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_COL_WIDTH,- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_VALUE,- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT,- PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA,- ].includes(- copyInfo.pasteType- )- ) {+ const specialPastes: IPasteHookValueType[] = [+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_COL_WIDTH,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_VALUE,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT,+ PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA,+ ];++ if (specialPastes.includes(copyInfo.pasteType)) {return { redos: [], undos: [] };}diff --git a/packages/sheets-ui/src/commands/commands/clipboard.command.ts b/packages/sheets-ui/src/commands/commands/clipboard.command.tsindex 8b08128d34..0c772a0cc1 100644--- a/packages/sheets-ui/src/commands/commands/clipboard.command.ts+++ b/packages/sheets-ui/src/commands/commands/clipboard.command.ts@@ -136,3 +136,13 @@ export const SheetPasteBesidesBorderCommand: ICommand = {});},};++export const SheetOptionalPasteCommand: ICommand = {+ id: 'sheet.command.optional-paste',+ type: CommandType.COMMAND,+ handler: async (accessor, { type }: { type: keyof typeof PREDEFINED_HOOK_NAME }) => {+ const clipboardService = accessor.get(ISheetClipboardService);++ return clipboardService.rePasteWithPasteType(type);+ },+};diff --git a/packages/sheets-ui/src/components/hook.ts b/packages/sheets-ui/src/components/hook.tsindex 079795e1b8..084e36b977 100644--- a/packages/sheets-ui/src/components/hook.ts+++ b/packages/sheets-ui/src/components/hook.ts@@ -16,8 +16,11 @@import type { Workbook } from '@univerjs/core';import { IUniverInstanceService, UniverInstanceType, useDependency } from '@univerjs/core';+import { IRenderManagerService } from '@univerjs/engine-render';import { useObservable } from '@univerjs/ui';+import { useMemo } from 'react';import { map, merge, of, startWith } from 'rxjs';+import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';export function useActiveWorkbook(): Workbook | null {const univerInstanceService = useDependency(IUniverInstanceService);@@ -42,3 +45,21 @@ export function useWorkbooks(): Workbook[] {);}, [], undefined, [univerInstanceService]);}++export function useSheetSkeleton() {+ const renderManagerService = useDependency(IRenderManagerService);+ const workbook = useActiveWorkbook();++ const { sheetSkeletonManagerService } = useMemo(() => {+ if (workbook) {+ const ru = renderManagerService.getRenderById(workbook.getUnitId());+ return {+ sheetSkeletonManagerService: ru?.with(SheetSkeletonManagerService),+ };+ }++ return { sheetSkeletonManagerService: null };+ }, [workbook, renderManagerService]);++ return sheetSkeletonManagerService;+}diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.tsindex 33b7e2268c..a45005d85a 100644--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts@@ -41,8 +41,11 @@ import type {ISheetClipboardHook,ISheetDiscreteRangeLocation,} from '../../services/clipboard/type';+import type { IUniverSheetsUIConfig } from '../config.schema';+import {BooleanNumber,+ connectInjector,convertBodyToHtml,DEFAULT_WORKSHEET_COLUMN_WIDTH,DEFAULT_WORKSHEET_COLUMN_WIDTH_KEY,@@ -65,10 +68,9 @@ import {} from '@univerjs/core';import { MessageType } from '@univerjs/design';-import { DocSelectionRenderService } from '@univerjs/docs-ui';-import { IRenderManagerService } from '@univerjs/engine-render';+import { IRenderManagerService } from '@univerjs/engine-render';import {InsertColMutation,InsertRowMutation,@@ -83,12 +85,13 @@ import {SetWorksheetColWidthMutation,SetWorksheetRowHeightMutation,} from '@univerjs/sheets';-import { IMessageService } from '@univerjs/ui';+import { BuiltInUIPart, IMessageService, IUIPartsService } from '@univerjs/ui';import { takeUntil } from 'rxjs';import { AddWorksheetMergeCommand } from '../../commands/commands/add-worksheet-merge.command';import {SheetCopyCommand,SheetCutCommand,+ SheetOptionalPasteCommand,SheetPasteBesidesBorderCommand,SheetPasteColWidthCommand,SheetPasteCommand,@@ -98,7 +101,10 @@ import {} from '../../commands/commands/clipboard.command';import { ISheetClipboardService, PREDEFINED_HOOK_NAME } from '../../services/clipboard/clipboard.service';import { SheetSkeletonManagerService } from '../../services/sheet-skeleton-manager.service';+import { ClipboardPopupMenu } from '../../views/clipboard/ClipboardPopupMenu';+import { SHEETS_UI_PLUGIN_CONFIG_KEY } from '../config.schema';import { whenSheetEditorFocused } from '../shortcuts/utils';+import { RemovePasteMenuCommands } from './const';import {generateBody,getClearAndSetMergeMutations,@@ -133,11 +139,13 @@ export class SheetClipboardController extends RxDisposable {@IConfigService private readonly _configService: IConfigService,@ISheetClipboardService private readonly _sheetClipboardService: ISheetClipboardService,@IMessageService private readonly _messageService: IMessageService,- @Inject(LocaleService) private readonly _localService: LocaleService+ @Inject(LocaleService) private readonly _localService: LocaleService,+ @IUIPartsService protected readonly _uiPartsService: IUIPartsService) {super();this._init();this._initCommandListener();+ this._initUIComponents();this._pasteWithDoc();}@@ -189,6 +197,7 @@ export class SheetClipboardController extends RxDisposable {SheetPasteColWidthCommand,SheetPasteBesidesBorderCommand,SheetPasteShortKeyCommand,+ SheetOptionalPasteCommand,].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));// register basic sheet clipboard hooks@@ -867,5 +876,28 @@ export class SheetClipboardController extends RxDisposable {}}));++ const sheetsUIConfig = this._configService.getConfig<IUniverSheetsUIConfig>(SHEETS_UI_PLUGIN_CONFIG_KEY);+ if (sheetsUIConfig?.clipboardConfig?.hidePasteOptions) {+ return;+ }++ this.disposeWithMe(+ this._commandService.onCommandExecuted((command: ICommandInfo) => {+ if (RemovePasteMenuCommands.includes(command.id)) {+ this._sheetClipboardService.disposePasteOptionsCache();+ }+ })+ );+ }++ private _initUIComponents() {+ const sheetsUIConfig = this._configService.getConfig<IUniverSheetsUIConfig>(SHEETS_UI_PLUGIN_CONFIG_KEY);+ if (sheetsUIConfig?.clipboardConfig?.hidePasteOptions) {+ return;+ }+ this.disposeWithMe(+ this._uiPartsService.registerComponent(BuiltInUIPart.CONTENT, () => connectInjector(ClipboardPopupMenu, this._injector))+ );}}diff --git a/packages/sheets-ui/src/controllers/clipboard/const.ts b/packages/sheets-ui/src/controllers/clipboard/const.tsnew file mode 100644index 0000000000..a52fbe72ad--- /dev/null+++ b/packages/sheets-ui/src/controllers/clipboard/const.ts@@ -0,0 +1,37 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { UndoCommandId } from '@univerjs/core';+import { InsertColMutation, InsertRowMutation, MoveColsMutation, MoveRangeMutation, MoveRowsMutation, RemoveColMutation, RemoveRowMutation, SetRangeValuesCommand, SetSelectionsOperation, SetWorksheetColWidthMutation, SetWorksheetRowHeightMutation } from '@univerjs/sheets';+import { SetCellEditVisibleOperation } from '../../commands/operations/cell-edit.operation';+import { SetZoomRatioOperation } from '../../commands/operations/set-zoom-ratio.operation';++export const RemovePasteMenuCommands = [+ SetCellEditVisibleOperation.id,+ SetRangeValuesCommand.id,+ SetZoomRatioOperation.id,+ MoveRangeMutation.id,+ RemoveRowMutation.id,+ RemoveColMutation.id,+ InsertRowMutation.id,+ InsertColMutation.id,+ MoveRowsMutation.id,+ MoveColsMutation.id,+ SetWorksheetColWidthMutation.id,+ SetWorksheetRowHeightMutation.id,+ SetSelectionsOperation.id,+ UndoCommandId,+];diff --git a/packages/sheets-ui/src/controllers/config.schema.ts b/packages/sheets-ui/src/controllers/config.schema.tsindex 96e2a256b7..942ca42a45 100644--- a/packages/sheets-ui/src/controllers/config.schema.ts+++ b/packages/sheets-ui/src/controllers/config.schema.ts@@ -31,6 +31,10 @@ export interface IUniverSheetsUIConfig {* Whether to show the formula bar.*/formulaBar?: boolean;++ clipboardConfig?: {+ hidePasteOptions?: boolean;+ };}export const defaultPluginConfig: IUniverSheetsUIConfig = {diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.tsindex a650eab672..07d1186c13 100644--- a/packages/sheets-ui/src/index.ts+++ b/packages/sheets-ui/src/index.ts@@ -55,7 +55,7 @@ export {SheetClipboardService,} from './services/clipboard/clipboard.service';export { COPY_TYPE } from './services/clipboard/type';-export type { ICellDataWithSpanInfo, ICopyPastePayload, ISheetClipboardHook, ISheetDiscreteRangeLocation } from './services/clipboard/type';+export type { ICellDataWithSpanInfo, ICopyPastePayload, IPasteHookValueType, ISheetClipboardHook, ISheetDiscreteRangeLocation } from './services/clipboard/type';export { getRepeatRange, mergeSetRangeValues } from './services/clipboard/utils';export { DragManagerService, type IDragCellPosition } from './services/drag-manager.service';export {diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.tsindex f0d9fcba86..b66aad935a 100644--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts@@ -21,10 +21,14 @@ import type {Nullable, Workbook, Worksheet,} from '@univerjs/core';import type { ISetSelectionsOperationParams } from '@univerjs/sheets';+import type { Observable } from 'rxjs';import type { IDiscreteRange } from '../../controllers/utils/range-tools';import type {ICellDataWithSpanInfo,IClipboardPropertyItem,+ IPasteHookKeyType,+ IPasteHookValueType,+ IPasteOptionCache,IPasteTarget,ISheetClipboardHook,ISheetDiscreteRangeLocation,@@ -41,13 +45,14 @@ import {IUniverInstanceService,LocaleService,ObjectMatrix,+ sequenceExecute,ThemeService,toDisposable,Tools,UniverInstanceType,} from '@univerjs/core';-import { IRenderManagerService } from '@univerjs/engine-render';+import { IRenderManagerService } from '@univerjs/engine-render';import {getPrimaryForRange,SetSelectionsOperation,@@ -55,7 +60,7 @@ import {SheetsSelectionsService,} from '@univerjs/sheets';import { HTML_CLIPBOARD_MIME_TYPE, IClipboardInterfaceService, INotificationService, IPlatformService, PLAIN_TEXT_CLIPBOARD_MIME_TYPE } from '@univerjs/ui';-import { BehaviorSubject } from 'rxjs';+import { BehaviorSubject, Subject } from 'rxjs';import { rangeToDiscreteRange, virtualizeDiscreteRanges } from '../../controllers/utils/range-tools';import { IMarkSelectionService } from '../mark-selection/mark-selection.service';import { SheetSkeletonManagerService } from '../sheet-skeleton-manager.service';@@ -78,7 +83,7 @@ export const PREDEFINED_HOOK_NAME = {SPECIAL_PASTE_COL_WIDTH: 'special-paste-col-width',SPECIAL_PASTE_BESIDES_BORDER: 'special-paste-besides-border',SPECIAL_PASTE_FORMULA: 'special-paste-formula',-};+} as const;interface ICopyContent {copyId: string;@@ -97,10 +102,18 @@ HtmlToUSMService.use(LarkPastePlugin);HtmlToUSMService.use(UniverPastePlugin);export interface ISheetClipboardService {+ showMenu$: Observable<boolean>;+ setShowMenu: (show: boolean) => void;++ pasteOptionsCache$: Observable<IPasteOptionCache | null>;+ updatePasteOptionsCache(cache: IPasteOptionCache | null): void;+copy(): Promise<boolean>;cut(): Promise<boolean>;paste(item: ClipboardItem, pasteType?: string): Promise<boolean>; // get content from a ClipboardItem and paste it.legacyPaste(html?: string, text?: string): Promise<boolean>; // paste a HTML string or plain text directly.+ rePasteWithPasteType(type: IPasteHookKeyType): boolean;+ disposePasteOptionsCache(): void;generateCopyContent(workbookId: string, worksheetId: string, range: IRange): Nullable<ICopyContent>;copyContentCache(): CopyContentCache; // return the cache content for inner copy/cut/paste.@@ -124,6 +137,14 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardprivate _copyContentCache: CopyContentCache;private _copyMarkId: string | null = null;+ // Record the parsed matrix and row and column attributes+ private _pasteOptionsCache$ = new BehaviorSubject<IPasteOptionCache | null>(null);+ readonly pasteOptionsCache$ = this._pasteOptionsCache$.asObservable();++ //Control the visibility of the Paste Options menu+ private readonly _showMenu$: Subject<boolean> = new Subject<boolean>();+ readonly showMenu$ = this._showMenu$.asObservable();+constructor(@ILogService private readonly _logService: ILogService,@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,@@ -152,6 +173,10 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardthis._initUnitDisposed();}+ setShowMenu(show: boolean) {+ this._showMenu$.next(show);+ }+copyContentCache(): CopyContentCache {return this._copyContentCache;}@@ -259,6 +284,36 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardreturn Promise.resolve(false);}+ rePasteWithPasteType(type: IPasteHookKeyType): boolean {+ const pasteOptionsCache = this._pasteOptionsCache$.getValue();+ if (!pasteOptionsCache) {+ return false;+ }++ const undoRedoService = this._injector.get(IUndoRedoService);+ const element = undoRedoService.pitchTopUndoElement();+ if (element) {+ const result = sequenceExecute(element.undoMutations, this._commandService);+ if (result) {+ undoRedoService.popUndoToRedo();+ }+ }++ const { cellMatrix, rowProperties = [], colProperties = [], source, target } = pasteOptionsCache;++ this._pasteUSM({+ cellMatrix,+ colProperties,+ rowProperties,+ }, target, PREDEFINED_HOOK_NAME[type] as IPasteHookValueType, source);++ return true;+ }++ updatePasteOptionsCache(cache: IPasteOptionCache | null): void {+ this._pasteOptionsCache$.next(cache);+ }+addClipboardHook(hook: ISheetClipboardHook): IDisposable {if (this._clipboardHooks.findIndex((h) => h.id === hook.id) !== -1) {this._logService.error('[SheetClipboardService]', 'hook already exists', hook.id);@@ -364,7 +419,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardthis._clipboardHooks$.next(this._clipboardHooks);}- private async _pastePlainText(text: string, pasteType: string): Promise<boolean> {+ private async _pastePlainText(text: string, pasteType: IPasteHookValueType): Promise<boolean> {const target = this._getPastingTarget();if (!target.subUnitId || !target.selection) {return false;@@ -421,7 +476,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardreturn result;}- private async _pasteHTML(html: string, pasteType: string): Promise<boolean> {+ private async _pasteHTML(html: string, pasteType: IPasteHookValueType): Promise<boolean> {const copyId = extractId(html);if (copyId && this._copyContentCache.get(copyId)) {return this._pasteInternal(copyId, pasteType);@@ -429,7 +484,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardreturn this._pasteExternal(html, pasteType);}- private async _pasteExternal(html: string, pasteType: string): Promise<boolean> {+ private async _pasteExternal(html: string, pasteType: IPasteHookValueType): Promise<boolean> {// this._logService.log('[SheetClipboardService]', 'pasting external content', html);// steps of pasting:@@ -492,7 +547,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard}// eslint-disable-next-line max-lines-per-function, complexity- private async _pasteInternal(copyId: string, pasteType: string): Promise<boolean> {+ private async _pasteInternal(copyId: string, pasteType: IPasteHookValueType): Promise<boolean> {// const target = this._getPastingTarget();// const { selection, unitId, subUnitId } = target;const cachedData = Tools.deepClone(this._copyContentCache.get(copyId));@@ -608,7 +663,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardprivate _pasteUSM(data: IUniverSheetCopyDataModel,target: IPasteTarget,- pasteType: string,+ pasteType: IPasteHookValueType,source?: ISheetDiscreteRangeLocation & { copyId: string; copyType: COPY_TYPE }): boolean {const { rowProperties, colProperties, cellMatrix } = data;@@ -703,6 +758,20 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardundoMutations: undoMutationsInfo,redoMutations: redoMutationsInfo,});++ this.updatePasteOptionsCache({+ target: {+ pastedRange,+ unitId,+ subUnitId,+ },+ cellMatrix,+ rowProperties,+ colProperties,+ pasteType,+ source,+ });+ this.setShowMenu(true);}filteredHooks.forEach((h) => h.onAfterPaste?.(result));@@ -1108,6 +1177,11 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard}));}++ disposePasteOptionsCache() {+ this.setShowMenu(false);+ this.updatePasteOptionsCache(null);+ }}// #region copy generationdiff --git a/packages/sheets-ui/src/services/clipboard/type.ts b/packages/sheets-ui/src/services/clipboard/type.tsindex e99a1cd0a2..497bff42ad 100644--- a/packages/sheets-ui/src/services/clipboard/type.ts+++ b/packages/sheets-ui/src/services/clipboard/type.ts@@ -16,6 +16,7 @@import type { ICellData, IDocumentData, IMutationInfo, IRange, ObjectMatrix } from '@univerjs/core';import type { IDiscreteRange } from '../../controllers/utils/range-tools';+import type { PREDEFINED_HOOK_NAME } from './clipboard.service';export enum COPY_TYPE {COPY = 'COPY',@@ -49,7 +50,7 @@ export interface IParsedCellValueByClipboard {export interface IUniverSheetCopyDataModel {rowProperties?: IClipboardPropertyItem[];- colProperties?: IClipboardPropertyItem[] | null;+ colProperties?: IClipboardPropertyItem[];cellMatrix: ObjectMatrix<ICellDataWithSpanInfo>;}@@ -62,7 +63,7 @@ export interface IPasteTarget {export interface ICopyPastePayload {copyType?: COPY_TYPE;copyId?: string;- pasteType: string;+ pasteType: IPasteHookValueType;}export interface ISheetDiscreteRangeLocation {@@ -180,3 +181,17 @@ export interface ISheetClipboardHook {*/getFilteredOutRows?(range: IRange): number[];}++export interface IPasteOptionCache {+ target: IPasteTarget;+ source?: IPasteSource;+ cellMatrix: ObjectMatrix<ICellDataWithSpanInfo>;+ rowProperties?: IClipboardPropertyItem[];+ colProperties?: IClipboardPropertyItem[];+ pasteType: IPasteHookValueType;+}++export type IPasteSource = ISheetDiscreteRangeLocation & { copyId: string; copyType: COPY_TYPE };+export type IPasteHookKeyType = Exclude<keyof typeof PREDEFINED_HOOK_NAME, 'default-copy'>;+export type IPasteHookValueTypeWithoutDefaultCopy = typeof PREDEFINED_HOOK_NAME[IPasteHookKeyType];+export type IPasteHookValueType = Exclude<IPasteHookValueTypeWithoutDefaultCopy, 'default-copy'>;diff --git a/packages/sheets-ui/src/views/clipboard/ClipboardPopupMenu.tsx b/packages/sheets-ui/src/views/clipboard/ClipboardPopupMenu.tsxnew file mode 100644index 0000000000..111d6151e7--- /dev/null+++ b/packages/sheets-ui/src/views/clipboard/ClipboardPopupMenu.tsx@@ -0,0 +1,223 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { ICommandInfo } from '@univerjs/core';+import type { IDiscreteRange } from '../../controllers/utils/range-tools';+import type { IPasteHookKeyType } from '../../services/clipboard/type';+import { ICommandService, IUniverInstanceService, LocaleService, toDisposable, useDependency, useObservable } from '@univerjs/core';+import { DropdownOverlay, DropdownProvider, DropdownTrigger } from '@univerjs/design';+import { IRenderManagerService } from '@univerjs/engine-render';+import { CheckMarkSingle, MoreDownSingle, PasteSpecial } from '@univerjs/icons';+import clsx from 'clsx';+import React, { useCallback, useEffect, useState } from 'react';+import { SheetOptionalPasteCommand } from '../../commands/commands/clipboard.command';+import { SetScrollOperation } from '../../commands/operations/scroll.operation';+import { useActiveWorkbook, useSheetSkeleton } from '../../components/hook';+import { getSheetObject } from '../../controllers/utils/component-tools';+import { ISheetClipboardService, PREDEFINED_HOOK_NAME } from '../../services/clipboard/clipboard.service';+import { ISheetSelectionRenderService } from '../../services/selection/base-selection-render.service';+import { SheetSkeletonManagerService } from '../../services/sheet-skeleton-manager.service';+import styles from './index.module.less';++const DEFAULT_PADDING = 2;++const SheetPasteOptions = [+ { value: 'DEFAULT_PASTE', label: 'rightClick.paste' },+ { value: 'SPECIAL_PASTE_VALUE', label: 'rightClick.pasteValue' },+ { value: 'SPECIAL_PASTE_FORMAT', label: 'rightClick.pasteFormat' },+ { value: 'SPECIAL_PASTE_COL_WIDTH', label: 'rightClick.pasteColWidth' },+ { value: 'SPECIAL_PASTE_BESIDES_BORDER', label: 'rightClick.pasteBesidesBorder' },+ { value: 'SPECIAL_PASTE_FORMULA', label: 'formula.operation.pasteFormula' },+];++const useMenuPosition = (range?: IDiscreteRange) => {+ const univerInstanceService = useDependency(IUniverInstanceService);+ const renderManagerService = useDependency(IRenderManagerService);++ const workbook = useActiveWorkbook();+ if (!range || !workbook) return null;++ const anchor = {+ startRow: range.rows[0],+ startCol: range.cols[0],+ endRow: range.rows[range.rows.length - 1],+ endCol: range.cols[range.cols.length - 1],+ };++ if (anchor.endRow < 0 || anchor.endCol < 0) {+ return null;+ }++ const ru = renderManagerService.getRenderById(workbook.getUnitId());+ const sheetSkeletonManagerService = ru?.with(SheetSkeletonManagerService);+ const selectionRenderService = ru?.with(ISheetSelectionRenderService);++ const sheetObject = getSheetObject(univerInstanceService, renderManagerService);+ if (!sheetObject || !selectionRenderService) return null;++ const { scene } = sheetObject;+ const skeleton = sheetSkeletonManagerService?.getCurrentSkeleton();+ const viewport = selectionRenderService.getViewPort();+ const scaleX = scene?.scaleX;+ const scaleY = scene?.scaleY;+ const scrollXY = scene?.getViewportScrollXY(viewport);+ const canvas = scene.getEngine()?.getCanvas();+ if (!scaleX || !scene || !scaleX || !scaleY || !scrollXY) return null;++ const endPosition = skeleton?.getNoMergeCellPositionByIndex(anchor.endRow, anchor.endCol);+ const endX = endPosition?.endX ?? 0;+ const endY = endPosition?.endY ?? 0;++ const positionEndX = skeleton?.convertTransformToOffsetX(endX, scaleX, scrollXY) ?? -9999;+ const positionEndY = skeleton?.convertTransformToOffsetY(endY, scaleY, scrollXY) ?? -9999;++ const canvasWidth = canvas?.getWidth();+ const canvasHeight = canvas?.getHeight();++ if (!canvasWidth || !canvasHeight) return null;++ const XInSideView = positionEndX + 50 <= canvasWidth;+ const YInSideView = positionEndY + 50 <= canvasHeight;++ let positionX = positionEndX;+ let positionY = positionEndY;++ if (!XInSideView) {+ positionX = canvasWidth - 100;+ }++ if (!YInSideView) {+ positionY = canvasHeight - 100;+ }++ return {+ positionX,+ positionY,+ };+};++export const ClipboardPopupMenu = () => {+ const clipboardService = useDependency(ISheetClipboardService);+ const showMenu = useObservable(clipboardService.showMenu$, true);+ const pasteOptionsCache = useObservable(clipboardService.pasteOptionsCache$, null);+ const renderManagerService = useDependency(IRenderManagerService);+ const localeService = useDependency(LocaleService);+ const commandService = useDependency(ICommandService);++ const [menuHovered, setMenuHovered] = useState(false);+ const [menuVisible, setMenuVisible] = useState(false);++ const [_, setVersion] = useState(Math.random());++ const range = pasteOptionsCache?.target.pastedRange;++ const relativePosition = useMenuPosition(range);++ const sheetSkeletonManagerService = useSheetSkeleton();++ useEffect(() => {+ if (!showMenu) {+ setMenuHovered(false);+ setMenuVisible(false);+ }+ }, [showMenu]);++ const forceUpdate = useCallback(() => setVersion(Math.random()), []);++ useEffect(() => {+ const disposable = sheetSkeletonManagerService && toDisposable(+ sheetSkeletonManagerService.currentSkeleton$.subscribe((skeleton) => {+ if (skeleton) {+ forceUpdate();+ }+ }));+ return disposable?.dispose;+ }, [sheetSkeletonManagerService, forceUpdate]);++ useEffect(() => {+ const disposable = commandService.onCommandExecuted((command: ICommandInfo) => {+ if (command.id === SetScrollOperation.id) {+ forceUpdate();+ }+ });+ return disposable.dispose;+ }, [forceUpdate, commandService]);++ if (!relativePosition || !showMenu) return null;++ if (relativePosition.positionX < 50 || relativePosition.positionY < 30) return null;++ const iconVisible = menuHovered || menuVisible;++ const handleClick = (type: string) => {+ commandService.executeCommand(SheetOptionalPasteCommand.id, { type });+ };++ return (+ <div+ className={styles.sheetPasteOptionsWrapper}+ style={{+ left: relativePosition.positionX + DEFAULT_PADDING,+ top: relativePosition.positionY + DEFAULT_PADDING,+ }}+ onMouseEnter={() => setMenuHovered(true)}+ onMouseLeave={() => setMenuHovered(false)}+ >+ <DropdownProvider>+ <DropdownTrigger>+ <div+ className={styles.sheetPasteOptionsIconWrapper}+ onClick={() => {+ setMenuVisible(!menuVisible);+ }}+ >+ <PasteSpecial+ style={{ color: '#35322B' }}+ extend={{ colorChannel1: 'rgb(var(--green-700))' }}+ />+ {iconVisible && <MoreDownSingle />}+ </div>+ </DropdownTrigger>+ <DropdownOverlay className={clsx(styles.sheetPasteOptionsMenu, `+ univer-border univer-border-gray-200 univer-border-solid+ `)}+ >+ <ul>+ {SheetPasteOptions.map((item) => {+ const itemType = PREDEFINED_HOOK_NAME[item.value as IPasteHookKeyType];+ const selected = pasteOptionsCache?.pasteType === itemType;+ return (+ <li+ key={item.value}+ onClick={() => handleClick(item.value)}+ className={clsx(styles.sheetPasteOptionsMenuItem, 'hover:univer-bg-neutral-100')}+ >+ <span>+ {selected && (+ <CheckMarkSingle className={styles.sheetPasteOptionsMenuItemIcon} style={{ color: 'rgb(var(--green-700, #409f11))' }} />+ )}+ </span>+ <div className={clsx(styles.sheetPasteOptionsMenuItemTitle, 'univer-text-gray-700')}>{localeService.t(item.label)}</div>+ </li>+ );+ })}+ </ul>+ </DropdownOverlay>+ </DropdownProvider>+ </div>+ );+};+diff --git a/packages/sheets-ui/src/views/clipboard/index.module.less b/packages/sheets-ui/src/views/clipboard/index.module.lessnew file mode 100644index 0000000000..55c13243e4--- /dev/null+++ b/packages/sheets-ui/src/views/clipboard/index.module.less@@ -0,0 +1,51 @@+.sheet-paste-options {+ &-wrapper {+ position: absolute;+ display: flex;+ background-color: #fff;+ z-index: 10;+ border-radius: 4px;+ }++ &-icon-wrapper {+ display: flex;+ align-items: center;+ justify-content: center;+ cursor: pointer;+ border: 1px solid rgb(var(--grey-200, #e5e5e5));+ border-radius: 4px;+ padding: 3px;+ }++ &-menu {+ ul {+ list-style-type: none;+ padding: 0;+ margin: 8px;+ }++ border: 1px solid rgb(var(--grey-200, #e5e5e5));+ box-shadow: var(--box-shadow-base);+ border-radius: 4px;++ &-item {+ padding: 8px 4px;+ cursor: pointer;+ position: relative;++ &:hover {+ background-color: rgb(var(--grey-100, #f5f5f5));+ }++ &-title {+ font-size: 13px;+ color: rgb(var(--black, #35322b));+ margin-left: 20px;+ }++ &-icon {+ position: absolute;+ }+ }+ }+}diff --git a/packages/ui/src/services/clipboard/clipboard.command.ts b/packages/ui/src/services/clipboard/clipboard.command.tsindex 7885450931..f6be3ea337 100644--- a/packages/ui/src/services/clipboard/clipboard.command.ts+++ b/packages/ui/src/services/clipboard/clipboard.command.ts@@ -50,4 +50,4 @@ export const PasteCommand: IMultiCommand = {handler: async () => true,};-export const SheetPasteShortKeyCommandName = 'sheet.command.paste-bu-short-key';+export const SheetPasteShortKeyCommandName = 'sheet.command.paste-by-short-key';