Skip to content

Commit

Permalink
Fix insert entity issue (#2694)
Browse files Browse the repository at this point in the history
* Fix insert entity issue

* fix comment
  • Loading branch information
JiuqingSong committed Jun 12, 2024
1 parent 00bb508 commit 08c948c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,25 @@ function getInsertPoint(
context?: FormatContentModelContext
): InsertPoint | null {
if (insertPointOverride) {
return insertPointOverride;
const { paragraph, marker, tableContext, path } = insertPointOverride;
const index = paragraph.segments.indexOf(marker);
const previousSegment = index > 0 ? paragraph.segments[index - 1] : null;

// It is possible that the real selection is right before the override selection marker.
// This happens when:
// [Override marker][Entity node to wrap][Real marker]
// Then we will move the entity node into entity wrapper, causes the override marker and real marker are at the same place
// And recreating content model causes real marker to appear before override marker.
// Once that happens, we need to use the real marker instead so that after insert entity, real marker can be placed
// after new entity (if insertPointOverride==true)
return previousSegment?.segmentType == 'SelectionMarker' && previousSegment.isSelected
? {
marker: previousSegment,
paragraph,
tableContext,
path,
}
: insertPointOverride;
} else {
const deleteResult = deleteSelection(model, [], context);
const insertPoint = deleteResult.insertPoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2620,4 +2620,49 @@ describe('insertEntityModel, use insert point', () => {
],
});
});

it('Block entity, Has insert point override which is right after real marker', () => {
const model = createContentModelDocument();
const para1 = createParagraph();
const para2 = createParagraph();
const text1 = createText('test');
const markerReal = createSelectionMarker();
const markerOverride = createSelectionMarker();

text1.isSelected = true;
para1.segments.push(text1);

markerOverride.isSelected = false;
para2.segments.push(markerReal, markerOverride);

model.blocks.push(para1, para2);

const ip: InsertPoint = {
path: [model],
paragraph: para2,
marker: markerOverride,
};

insertEntityModel(model, Entity, 'focus', false, true, undefined, ip);

expect(model).toEqual({
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
segments: [{ segmentType: 'Text', text: 'test', format: {}, isSelected: true }],
format: {},
},
{
blockType: 'Paragraph',
segments: [
Entity,
{ segmentType: 'SelectionMarker', isSelected: true, format: {} },
{ segmentType: 'SelectionMarker', isSelected: false, format: {} },
],
format: {},
},
],
});
});
});

0 comments on commit 08c948c

Please sign in to comment.