Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: paste col should keep wider column width #3185

Merged
merged 9 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
153 changes: 66 additions & 87 deletions packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import {
LocaleService,
ObjectMatrix,
OnLifecycle,
Optional, RxDisposable, UniverInstanceType } from '@univerjs/core';
Optional, RxDisposable, UniverInstanceType,
} from '@univerjs/core';
import { MessageType } from '@univerjs/design';
import type {
IInsertColMutationParams,
Expand All @@ -62,9 +63,9 @@ import {
SetWorksheetColWidthMutation,
SetWorksheetRowHeightMutation,
} from '@univerjs/sheets';
import { IMessageService, textTrim } from '@univerjs/ui';
import { IMessageService } from '@univerjs/ui';

import { IRenderManagerService, ITextSelectionRenderManager, ptToPx } from '@univerjs/engine-render';
import { IRenderManagerService, ITextSelectionRenderManager } from '@univerjs/engine-render';
import { takeUntil } from 'rxjs';
import {
SheetCopyCommand,
Expand Down Expand Up @@ -154,7 +155,6 @@ export class SheetClipboardController extends RxDisposable {
this.disposeWithMe({ dispose: () => disposables.forEach((d) => d.dispose()) });
}

//eslint-disable-next-line max-lines-per-function
private _initCopyingHooks(): ISheetClipboardHook {
const self = this;
let currentSheet: Worksheet | null = null;
Expand Down Expand Up @@ -278,7 +278,6 @@ export class SheetClipboardController extends RxDisposable {
};
}

//eslint-disable-next-line max-lines-per-function
private _initPastingHook(): ISheetClipboardHook {
const self = this;

Expand Down Expand Up @@ -324,31 +323,8 @@ export class SheetClipboardController extends RxDisposable {
if (addingRowsCount > 0) {
const rowInfo: IObjectArrayPrimitiveType<IRowData> = {};
rowProperties.slice(existingRowsCount).forEach((property, index) => {
const { style, height: PropertyHeight } = property || {};
if (style) {
const cssTextArray = style.split(';');
let height = DEFAULT_WORKSHEET_ROW_HEIGHT;

cssTextArray.find((css) => {
css = css.toLowerCase();
const key = textTrim(css.substr(0, css.indexOf(':')));
const value = textTrim(css.substr(css.indexOf(':') + 1));
if (key === 'height') {
if (value.endsWith('pt')) {
height = ptToPx(Number.parseFloat(value));
} else {
height = Number.parseFloat(value);
}
return true;
}
return false;
});

rowInfo[index] = {
h: height,
hd: BooleanNumber.FALSE,
};
} else if (PropertyHeight) {
const { height: PropertyHeight } = property || {};
if (PropertyHeight) {
rowInfo[index] = {
h: Number.parseFloat(PropertyHeight),
hd: BooleanNumber.FALSE,
Expand All @@ -363,7 +339,8 @@ export class SheetClipboardController extends RxDisposable {
startColumn: range.cols[0],
endColumn: range.cols[range.cols.length - 1],
endRow: range.rows[range.rows.length - 1],
startRow: maxRow },
startRow: maxRow,
},
rowInfo,
};
redoMutations.push({
Expand All @@ -377,28 +354,8 @@ export class SheetClipboardController extends RxDisposable {
const rowHeight: IObjectArrayPrimitiveType<number> = {};
const originRowHeight: IObjectArrayPrimitiveType<number> = {};
rowProperties.slice(0, existingRowsCount).forEach((property, index) => {
const { style, height: propertyHeight } = property;
if (style) {
const cssTextArray = style.split(';');
let height = DEFAULT_WORKSHEET_ROW_HEIGHT;

cssTextArray.find((css) => {
css = css.toLowerCase();
const key = textTrim(css.substr(0, css.indexOf(':')));
const value = textTrim(css.substr(css.indexOf(':') + 1));
if (key === 'height') {
if (value.endsWith('pt')) {
height = ptToPx(Number.parseFloat(value));
} else {
height = Number.parseFloat(value);
}
return true;
}
return false;
});

rowHeight[index + range.rows[0]] = height;
} else if (propertyHeight) {
const { height: propertyHeight } = property;
if (propertyHeight) {
const rowConfigBeforePaste = rowManager.getRow(range.rows[0] + index);
const willSetHeight = Number.parseFloat(propertyHeight);
if (rowConfigBeforePaste) {
Expand All @@ -416,26 +373,29 @@ export class SheetClipboardController extends RxDisposable {
});

// apply row properties to the existing rows
const setRowPropertyMutation: ISetWorksheetRowHeightMutationParams = {
unitId: unitId!,
subUnitId: subUnitId!,
ranges: [{ startRow: range.rows[0], endRow: Math.min(range.rows[range.rows.length - 1], maxRow),
startColumn: range.cols[0], endColumn: range.cols[range.cols.length - 1],
}],
rowHeight,
};
redoMutations.push({
id: SetWorksheetRowHeightMutation.id,
params: setRowPropertyMutation,
});
if (Object.keys(rowHeight).length) {
const setRowPropertyMutation: ISetWorksheetRowHeightMutationParams = {
unitId: unitId!,
subUnitId: subUnitId!,
ranges: [{
startRow: range.rows[0], endRow: Math.min(range.rows[range.rows.length - 1], maxRow),
startColumn: range.cols[0], endColumn: range.cols[range.cols.length - 1],
}],
rowHeight,
};
redoMutations.push({
id: SetWorksheetRowHeightMutation.id,
params: setRowPropertyMutation,
});

undoMutations.push({
id: SetWorksheetRowHeightMutation.id,
params: {
...setRowPropertyMutation,
rowHeight: 20,
},
});
undoMutations.push({
id: SetWorksheetRowHeightMutation.id,
params: {
...setRowPropertyMutation,
rowHeight: originRowHeight,
},
});
}

return {
redos: redoMutations,
Expand All @@ -454,6 +414,7 @@ export class SheetClipboardController extends RxDisposable {
const existingColsCount = colProperties.length - addingColsCount;

const defaultColumnWidth = self._configService.getConfig<number>(DEFAULT_WORKSHEET_COLUMN_WIDTH_KEY) ?? DEFAULT_WORKSHEET_COLUMN_WIDTH;
const pasteToCols = range.cols;

if (addingColsCount > 0) {
const addColMutation: IInsertColMutationParams = {
Expand All @@ -465,8 +426,8 @@ export class SheetClipboardController extends RxDisposable {
endColumn: range.cols[range.cols.length - 1],
startColumn: maxColumn,
},
colInfo: colProperties.slice(existingColsCount).map((property) => ({
w: property.width ? +property.width : defaultColumnWidth,
colInfo: colProperties.slice(existingColsCount).map((property, index) => ({
w: property.width ? Math.max(+property.width, currentSheet!.getColumnWidth(pasteToCols[index])) : defaultColumnWidth,
hd: BooleanNumber.FALSE,
})),
};
Expand All @@ -475,30 +436,49 @@ export class SheetClipboardController extends RxDisposable {
params: addColMutation,
});
}
// apply col properties to the existing rows
const setColPropertyMutation: ISetWorksheetColWidthMutationParams = {

const targetSetColPropertyParams = {
unitId: unitId!,
subUnitId: subUnitId!,
ranges: [{
startRow: range.rows[0],
endRow: range.rows[range.rows.length - 1],

startColumn: range.cols[0],
endColumn: Math.min(range.cols[range.cols.length - 1], maxColumn) }],
colWidth: colProperties
.slice(0, existingColsCount)
.map((property) => (property.width ? +property.width : defaultColumnWidth)),
endColumn: Math.min(range.cols[range.cols.length - 1], maxColumn),
}],
};
redoMutations.push({
id: SetWorksheetColWidthMutation.id,
params: setColPropertyMutation,
});

// TODO: add undo mutations but we cannot do it now because underlying mechanism is not ready
// apply col properties to the existing rows
if (colProperties.length > 0) {
const setColPropertyMutation: ISetWorksheetColWidthMutationParams = {
...targetSetColPropertyParams,
colWidth: colProperties
.slice(0, existingColsCount)
.map((property, index) => (property.width ? Math.max(+property.width, currentSheet!.getColumnWidth(pasteToCols[index])) : defaultColumnWidth)),
};

const undoSetColPropertyParams: ISetWorksheetColWidthMutationParams = {
...targetSetColPropertyParams,
colWidth: colProperties
.slice(0, existingColsCount)
.map((_property, index) => currentSheet!.getColumnWidth(pasteToCols[index]) ?? defaultColumnWidth),
};

redoMutations.push({
id: SetWorksheetColWidthMutation.id,
params: setColPropertyMutation,
});

undoMutations.push({
id: SetWorksheetColWidthMutation.id,
params: undoSetColPropertyParams,
});
}

return {
redos: redoMutations,
undos: [] || undoMutations,
undos: undoMutations,
};
},

Expand Down Expand Up @@ -604,7 +584,6 @@ export class SheetClipboardController extends RxDisposable {
});
}

//eslint-disable-next-line max-lines-per-function
private _initSpecialPasteHooks() {
const self = this;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,9 @@ describe('Test clipboard', () => {
const styles = getStyles(startRow, startColumn, endRow, endColumn);
const mergedCells = getMergedCells(startRow, startColumn, endRow, endColumn);
const rowManager = get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1')?.getRowManager();
const rowHeight = rowManager?.getRowData()?.[0].h;
const columnManager = get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1')?.getColumnManager();
const columnWidth = columnManager?.getColumnData()?.[0].w;
expect(columnWidth).toBe(73);
expect(rowHeight).toBe(81);
const columnWidth = columnManager?.getColumnData()?.[0]?.w;
expect(columnWidth).toBe(88);
expect(values && values[0][0]?.v).toBe('row1col2');
expect(styles && styles[0][0]).toStrictEqual({
bg: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,23 @@ function parseColGroup(raw: string): IClipboardPropertyItem[] | null {
return null;
}

return Array.from(colMatches).map((colMatch) => parseProperties(colMatch[1]));
const colPropertiesWithSpan = Array.from(colMatches).map((colMatch) => parseProperties(colMatch[1]));

const colProperties: IClipboardPropertyItem[] = [];
colPropertiesWithSpan.forEach((propertiesWithSpan) => {
const span = Number(propertiesWithSpan.span);
if (span) {
for (let i = 0; i < span; i++) {
const propertiesWithoutSpan = { ...propertiesWithSpan };
delete propertiesWithoutSpan.span;
colProperties.push(propertiesWithoutSpan);
}
} else {
colProperties.push(propertiesWithSpan);
}
});

return colProperties;
}

function childNodeToHTML(node: ChildNode) {
Expand Down
Loading