Skip to content

Commit

Permalink
Content Model: Fix #2230 (#2231)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong authored Dec 1, 2023
1 parent d5271d5 commit 5520f04
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function keyboardInput(editor: IContentModelEditor, rawEvent: KeyboardEve

editor.formatContentModel(
(model, context) => {
const result = deleteSelection(model, [], context).deleteResult;
const result = deleteSelection(model, [], context);

// We have deleted selection then we will let browser to handle the input.
// With this combined operation, we don't wan to mass up the cached model so clear it
Expand All @@ -22,8 +22,15 @@ export function keyboardInput(editor: IContentModelEditor, rawEvent: KeyboardEve
// Skip undo snapshot here and add undo snapshot before the operation so that we don't add another undo snapshot in middle of this replace operation
context.skipUndoSnapshot = true;

// Do not preventDefault since we still want browser to handle the final input for now
return result == 'range';
if (result.deleteResult == 'range') {
// We have deleted something, next input should inherit the segment format from deleted content, so set pending format here
context.newPendingFormat = result.insertPoint?.marker.format;

// Do not preventDefault since we still want browser to handle the final input for now
return true;
} else {
return false;
}
},
{
rawEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ describe('keyboardInput', () => {
newImages: [],
clearModelCache: true,
skipUndoSnapshot: true,
newPendingFormat: undefined,
});
});

Expand Down Expand Up @@ -158,6 +159,7 @@ describe('keyboardInput', () => {
newImages: [],
clearModelCache: true,
skipUndoSnapshot: true,
newPendingFormat: undefined,
});
});

Expand Down Expand Up @@ -186,6 +188,7 @@ describe('keyboardInput', () => {
newImages: [],
clearModelCache: true,
skipUndoSnapshot: true,
newPendingFormat: undefined,
});
});

Expand Down Expand Up @@ -268,6 +271,7 @@ describe('keyboardInput', () => {
newImages: [],
clearModelCache: true,
skipUndoSnapshot: true,
newPendingFormat: undefined,
});
});

Expand Down Expand Up @@ -322,6 +326,45 @@ describe('keyboardInput', () => {
newImages: [],
clearModelCache: true,
skipUndoSnapshot: true,
newPendingFormat: undefined,
});
});

it('Letter input, expanded selection, no modifier key, deleteSelection returns range, has segment format', () => {
const mockedFormat = 'FORMAT' as any;
getDOMSelectionSpy.and.returnValue({
type: 'range',
range: {
collapsed: false,
},
});
deleteSelectionSpy.and.returnValue({
deleteResult: 'range',
insertPoint: {
marker: {
format: mockedFormat,
},
},
});

const rawEvent = {
key: 'A',
} as any;

keyboardInput(editor, rawEvent);

expect(getDOMSelectionSpy).toHaveBeenCalled();
expect(addUndoSnapshotSpy).toHaveBeenCalled();
expect(formatContentModelSpy).toHaveBeenCalled();
expect(deleteSelectionSpy).toHaveBeenCalledWith(mockedModel, [], mockedContext);
expect(formatResult).toBeTrue();
expect(mockedContext).toEqual({
deletedEntities: [],
newEntities: [],
newImages: [],
clearModelCache: true,
skipUndoSnapshot: true,
newPendingFormat: mockedFormat,
});
});
});

0 comments on commit 5520f04

Please sign in to comment.