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

Recreate Content Model for table and image selection #2128

Merged
merged 2 commits into from
Oct 6, 2023
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 @@ -41,12 +41,6 @@ function isIndexedSegment(node: Node): node is IndexedSegmentNode {
);
}

function isIndexedTable(element: HTMLTableElement): element is IndexedTableElement {
const { tableRows } = (element as IndexedTableElement).__roosterjsContentModel ?? {};

return Array.isArray(tableRows) && tableRows.every(row => Array.isArray(row.cells));
}

function onSegment(
segmentNode: Node,
paragraph: ContentModelParagraph,
Expand Down Expand Up @@ -112,34 +106,9 @@ function reconcileSelection(

switch (newSelection.type) {
case 'image':
const imageModel = isIndexedSegment(newSelection.image)
? newSelection.image.__roosterjsContentModel.segments[0]
: null;

if (imageModel?.segmentType == 'Image') {
imageModel.isSelected = true;
imageModel.isSelectedAsImageSelection = true;

return true;
}

break;

case 'table':
const rows = isIndexedTable(newSelection.table)
? newSelection.table.__roosterjsContentModel.tableRows
: null;
rows?.forEach((row, rowIndex) => {
row.cells.forEach((cell, colIndex) => {
cell.isSelected =
rowIndex >= newSelection.firstRow &&
rowIndex <= newSelection.lastRow &&
colIndex >= newSelection.firstColumn &&
colIndex <= newSelection.lastColumn;
});
});

return true;
// For image and table selection, we just clear the cached model since during selecting the element id might be changed
return false;

case 'range':
const newRange = newSelection.range;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ describe('contentModelDomIndexer.reconcileSelection', () => {

const result = contentModelDomIndexer.reconcileSelection(model, newRangeEx);

expect(result).toBeTrue();
expect(result).toBeFalse();
expect(node1.__roosterjsContentModel).toEqual({
paragraph,
segments: [oldSegment1],
Expand All @@ -465,8 +465,6 @@ describe('contentModelDomIndexer.reconcileSelection', () => {
src: 'test',
format: {},
dataset: {},
isSelected: true,
isSelectedAsImageSelection: true,
});
});

Expand Down Expand Up @@ -503,7 +501,7 @@ describe('contentModelDomIndexer.reconcileSelection', () => {

const result = contentModelDomIndexer.reconcileSelection(model, newRangeEx);

expect(result).toBeTrue();
expect(result).toBeFalse();
expect(node1.__roosterjsContentModel).toEqual({
tableRows: tableModel.rows,
});
Expand All @@ -512,15 +510,6 @@ describe('contentModelDomIndexer.reconcileSelection', () => {
blockGroupType: 'Document',
blocks: [tableModel],
});
expect(cell00.isSelected).toBeFalse();
expect(cell01.isSelected).toBeFalse();
expect(cell02.isSelected).toBeFalse();
expect(cell10.isSelected).toBeTrue();
expect(cell11.isSelected).toBeTrue();
expect(cell12.isSelected).toBeFalse();
expect(cell20.isSelected).toBeTrue();
expect(cell21.isSelected).toBeTrue();
expect(cell22.isSelected).toBeFalse();
});

it('no old range, collapsed range after last node', () => {
Expand Down