Skip to content

Commit 4616869

Browse files
liangdrimeLiang MengBryanValverdeU
authored
Fix: keep image selection when selectionchange range is collapsed (#3396)
Co-authored-by: Liang Meng <liangmeng@microsoft.com> Co-authored-by: Bryan Valverde U <bvalverde@microsoft.com>
1 parent 6c008ba commit 4616869

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

packages/roosterjs-content-model-core/lib/corePlugin/selection/SelectionPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,11 +738,11 @@ class SelectionPlugin implements PluginWithState<SelectionPluginState> {
738738
const newSelection = this.editor.getDOMSelection();
739739
const domHelper = this.editor.getDOMHelper();
740740

741-
//If am image selection changed to a wider range due a keyboard event, we should update the selection
741+
// If an image selection changed to a wider range due to a keyboard event, we should update the selection
742742
const range = domHelper.getSelectionRange();
743743
if (range) {
744744
const image = isSingleImageInSelection(range);
745-
if (newSelection?.type == 'image' && !image) {
745+
if (newSelection?.type == 'image' && !image && !range.collapsed) {
746746
const sel = this.editor.getDocument().defaultView?.getSelection();
747747
const isReverted = sel
748748
? sel.focusNode != range.endContainer || sel.focusOffset != range.endOffset

packages/roosterjs-content-model-core/test/corePlugin/selection/SelectionPluginTest.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3698,6 +3698,38 @@ describe('SelectionPlugin selectionChange on image selected', () => {
36983698
});
36993699
expect(getDOMSelectionSpy).toHaveBeenCalledTimes(1);
37003700
});
3701+
3702+
it('onSelectionChange on image | 5, keeps image selection when range is collapsed', () => {
3703+
spyOn(isSingleImageInSelection, 'isSingleImageInSelection').and.returnValue(null);
3704+
3705+
mockedRange = { startContainer: {}, collapsed: true } as any;
3706+
3707+
const plugin = createSelectionPlugin({});
3708+
const state = plugin.getState();
3709+
const mockedOldSelection = {
3710+
type: 'image',
3711+
image: {} as any,
3712+
} as DOMSelection;
3713+
3714+
state.selection = mockedOldSelection;
3715+
3716+
plugin.initialize(editor);
3717+
3718+
const onSelectionChange = addEventListenerSpy.calls.argsFor(0)[1] as Function;
3719+
const mockedNewSelection = {
3720+
type: 'image',
3721+
image: {} as any,
3722+
} as any;
3723+
3724+
hasFocusSpy.and.returnValue(true);
3725+
isInShadowEditSpy.and.returnValue(false);
3726+
getDOMSelectionSpy.and.returnValue(mockedNewSelection);
3727+
3728+
onSelectionChange();
3729+
3730+
expect(setDOMSelectionSpy).not.toHaveBeenCalled();
3731+
expect(getDOMSelectionSpy).toHaveBeenCalledTimes(1);
3732+
});
37013733
});
37023734

37033735
describe('SelectionPlugin handle logical root change', () => {

0 commit comments

Comments
 (0)