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 insert entity issue #2694

Merged
merged 2 commits into from
Jun 12, 2024
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 @@ -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: {},
},
],
});
});
});
Loading