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

Copy table cell styles #1431

Merged
merged 4 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 12 additions & 3 deletions packages/roosterjs-editor-dom/lib/table/pasteTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@ export default function pasteTable(
for (let j = cursorCol; j < columns; j++) {
let cell = currentTable.getCell(i, j);
let newCell = newTable.getTd(i - cursorRow, j - cursorCol);
if (cell.td) {
moveChildNodes(cell.td, newCell!);
if (cell.td && newCell) {
moveChildNodes(cell.td, newCell);
copyCellStyle(cell.td, newCell);
} else {
cell.td = document.createElement('td');
}
}
}
currentTable.writeBack();

currentTable.writeBack(true /* skipApplyFormat*/);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I apply format later, say I pasted a table into a table, then I insert a row or column, what will happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using saveMetadata with bgColorOverride: true, then write applyFormat will not apply format to this cells.

}

function copyCellStyle(cell: HTMLTableCellElement, styledCell: HTMLTableCellElement) {
const styles = styledCell.getAttribute('style');
if (styles) {
cell.setAttribute('style', styles);
}
}
10 changes: 8 additions & 2 deletions packages/roosterjs-editor-dom/test/table/pasteTableTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const ID = 'id1';
const TABLE123 =
'<table id=' +
ID +
'><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>4</td><td>5</td><td>6</td></tr><tr><td>7</td><td>8</td><td>9</td></tr></table>';
"><tr><td id='pivotCell' >1</td><td>2</td><td>3</td></tr><tr><td>4</td><td >5</td><td>6</td></tr><tr><td>7</td><td>8</td><td>9</td></tr></table>";
/*
|1|2|3|
|4|5|6|
|7|8|9|
*/
const TABLEABC =
'<table id=id2><tr><td>a</td><td>b</td><td>c</td></tr><tr><td>d</td><td>e</td><td>f</td></tr><tr><td>g</td><td>h</td><td>i</td></tr></table>';
"<table id=id2><tr><td style='color:red' >a</td><td>b</td><td>c</td></tr><tr><td>d</td><td >e</td><td>f</td></tr><tr><td>g</td><td>h</td><td>i</td></tr></table>";
/*
|a|b|c|
|d|e|f|
Expand Down Expand Up @@ -61,4 +61,10 @@ describe('PasteTable', () => {
expect(node.childNodes.length).toEqual(5);
expect(node.childNodes[0].childNodes.length).toEqual(5);
});

it('Paste table | copy styles', () => {
runTest(TABLE123, TABLEABC, 0, 0);
const pivotCell = document.getElementById('pivotCell') as HTMLTableCellElement;
expect(pivotCell.style.color).toEqual('red');
});
});