Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: add paste options #4393

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)!;
Expand All @@ -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
)
) {
Expand Down Expand Up @@ -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 = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -78,7 +78,7 @@ export class DataValidationCopyPasteController extends Disposable {
copyInfo: {
copyType: COPY_TYPE;
copyRange?: IDiscreteRange;
pasteType: string;
pasteType: IPasteHookValueType;
unitId: string;
subUnitId: string;
}
Expand All @@ -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: [] };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -107,7 +107,7 @@ export class SheetsDrawingCopyPasteController extends Disposable {
copyInfo: {
copyType: COPY_TYPE;
copyRange?: IDiscreteRange;
pasteType: string;
pasteType: IPasteHookValueType;
unitId: string;
subUnitId: string;
}
Expand All @@ -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: [] };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -139,7 +139,7 @@ export class SheetsHyperLinkCopyPasteController extends Disposable {
copyInfo: {
copyType: COPY_TYPE;
copyRange?: IDiscreteRange;
pasteType: string;
pasteType: IPasteHookValueType;
unitId: string;
subUnitId: string;
}
Expand All @@ -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: [] };
}

Expand Down
10 changes: 10 additions & 0 deletions packages/sheets-ui/src/commands/commands/clipboard.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};
21 changes: 21 additions & 0 deletions packages/sheets-ui/src/components/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -896,5 +905,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))
);
}
}
Loading
Loading