Skip to content

Commit

Permalink
fix: paste row height and col width (#1330)
Browse files Browse the repository at this point in the history
* fix: paste row height and col width

* test: add test
  • Loading branch information
yuhongz authored Feb 7, 2024
1 parent a13c450 commit 60bd78a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,28 +282,32 @@ export class SheetClipboardController extends RxDisposable {
if (addingRowsCount > 0) {
const rowInfo: IObjectArrayPrimitiveType<IRowData> = {};
rowProperties.slice(existingRowsCount).forEach((property, index) => {
const style = property?.style;
if (!style) {
return;
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') {
height = Number.parseFloat(value);
return true;
}
return false;
});

rowInfo[index] = {
h: height,
hd: BooleanNumber.FALSE,
};
} else if (PropertyHeight) {
rowInfo[index] = {
h: Number.parseFloat(PropertyHeight),
hd: BooleanNumber.FALSE,
};
}
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') {
height = Number.parseFloat(value);
return true;
}
return false;
});

rowInfo[index] = {
h: height,
hd: BooleanNumber.FALSE,
};
});

const addRowMutation: IInsertRowMutationParams = {
Expand All @@ -322,25 +326,26 @@ export class SheetClipboardController extends RxDisposable {
// TODO When Excel pasted, there was no width height, Do we still need to set the height?
const rowHeight: IObjectArrayPrimitiveType<number> = {};
rowProperties.slice(0, existingRowsCount).forEach((property, index) => {
const style = property.style;
if (!style) {
return;
}
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') {
height = Number.parseFloat(value);
return true;
}
return false;
});
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') {
height = Number.parseFloat(value);
return true;
}
return false;
});

rowHeight[index] = height;
rowHeight[index + range.startRow] = height;
} else if (propertyHeight) {
rowHeight[index + range.startRow] = Number.parseFloat(propertyHeight);
}
});

// apply row properties to the existing rows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ describe('Test clipboard', () => {
const values = getValues(startRow, startColumn, endRow, endColumn);
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);
expect(values && values[0][0]?.v).toBe('row1col2');
expect(styles && styles[0][0]).toStrictEqual({
bg: { rgb: 'rgb(255,0,0)' },
Expand Down
13 changes: 11 additions & 2 deletions packages/sheets-ui/src/services/clipboard/clipboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,20 +409,29 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
if (!worksheet) {
return false;
}
const manager = worksheet.getColumnManager();
const colManager = worksheet.getColumnManager();
const rowManager = worksheet.getRowManager();
const defaultColumnWidth = worksheet.getConfig().defaultColumnWidth;
const defaultRowHeight = worksheet.getConfig().defaultRowHeight;

const colProperties = [];
const rowProperties = [];

for (let i = startColumn; i <= endColumn; i++) {
const column = manager.getColumnOrCreate(i);
const column = colManager.getColumnOrCreate(i);
colProperties.push({ width: `${column.w || defaultColumnWidth}` });
}

for (let j = startRow; j <= endRow; j++) {
const row = rowManager.getRowOrCreate(j);
rowProperties.push({ height: `${row.h || defaultRowHeight}` });
}

const pasteRes = this._pasteUSM(
{
cellMatrix,
colProperties,
rowProperties,
}, // paste data
{
unitId, // paste target
Expand Down
24 changes: 13 additions & 11 deletions packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class HtmlToUSMService {
};

const rowProperties: IClipboardPropertyItem[] = [];
let colProperties: IClipboardPropertyItem[] = [];
const colProperties: IClipboardPropertyItem[] = [];
// pick tables
const tableStrings = html.match(/<table\b[^>]*>([\s\S]*?)<\/table>/gi);
const tables: IParsedTablesInfo[] = [];
Expand Down Expand Up @@ -154,13 +154,13 @@ export class HtmlToUSMService {
rowProperties: tableRowProp,
} = this._parseTable(tableString!);

if (tableColProp) {
colProperties = tableColProp;
}
cellMatrix &&
cellMatrix.forValue((row, col, value) => {
valueMatrix.setValue(curRow + row, col, value);
});
if (tableColProp) {
colProperties.push(...tableColProp);
}
rowProperties.push(...tableRowProp);
}
});
Expand All @@ -187,7 +187,7 @@ export class HtmlToUSMService {
if (tableStrings) {
tableStrings.forEach((t) => {
const curRow = valueMatrix.getDataRange().endRow + 1;
const { cellMatrix } = this._parseTable(t!);
const { cellMatrix, rowProperties: tableRowProp, colProperties: tableColProp } = this._parseTable(t!);
if (cellMatrix) {
cellMatrix.forValue((row, col, value) => {
const { rowSpan = 1, colSpan = 1 } = value;
Expand All @@ -201,14 +201,16 @@ export class HtmlToUSMService {
valueMatrix.setValue(curRow + row, col, value);
});
}

rowProperties.push(...rowProperties);
if (tableColProp) {
colProperties.push(...tableColProp);
}
rowProperties.push(...tableRowProp);
});
}
}
return {
rowProperties: [],
colProperties: [],
rowProperties,
colProperties,
cellMatrix: valueMatrix,
};
}
Expand Down Expand Up @@ -244,8 +246,8 @@ export class HtmlToUSMService {
}
});
return {
rowProperties: [],
colProperties: [],
rowProperties,
colProperties,
cellMatrix: valueMatrix,
};
}
Expand Down

0 comments on commit 60bd78a

Please sign in to comment.