Skip to content

Commit

Permalink
Address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanValverdeU committed Apr 3, 2024
1 parent ebd6e5e commit 673f750
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,21 +272,13 @@ class SelectionPlugin implements PluginWithState<SelectionPluginState> {
this.selectBeforeOrAfterElement(editor, selection.image);
}
} else if ((key == Left || key == Right) && rawEvent.shiftKey) {
const parent = selection.image.parentNode;
const index = parent && toArray(parent.childNodes).indexOf(selection.image);
if (index != null && parent) {
const endIndex = index + (selection.isReverted ? 1 : 0);
const newRange = doc.createRange();

newRange.setStart(parent, index);
newRange.setEnd(parent, endIndex);

this.editor?.setDOMSelection({
type: 'range',
range: newRange,
isReverted: !!selection.isReverted,
});
}
const newRange = doc.createRange();
newRange.selectNode(selection.image);
this.editor?.setDOMSelection({
type: 'range',
range: newRange,
isReverted: !!selection.isReverted,
});
}
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1045,59 +1045,29 @@ describe('SelectionPlugin handle image selection', () => {
});

const mockedRange = {
setStart: jasmine.createSpy('setStart'),
collapse: jasmine.createSpy('collapse'),
selectNode: jasmine.createSpy('selectNode'),
};

createRangeSpy.and.returnValue(mockedRange);

const rawEvent = {
const rawEvent = (<Partial<KeyboardEvent>>{
key: 'ArrowRight',
} as any;
shiftKey: true,
}) as any;
plugin.onPluginEvent!({
eventType: 'keyDown',
rawEvent,
});

expect(setDOMSelectionSpy).toHaveBeenCalledTimes(1);
expect(mockedRange.selectNode).toHaveBeenCalledTimes(1);
expect(mockedRange.selectNode).toHaveBeenCalledWith(mockedImage);
expect(setDOMSelectionSpy).toHaveBeenCalledWith({
type: 'range',
range: mockedRange,
isReverted: false,
});
});

it('Key down + Shift and Right/Left Key, do not handle the image was not found', () => {
const mockedImage = {} as any;
mockedImage.parentNode = { childNodes: [] };

getDOMSelectionSpy.and.returnValue({
type: 'image',
image: mockedImage,
});

const mockedRange = {
setStart: jasmine.createSpy('setStart'),
collapse: jasmine.createSpy('collapse'),
};

createRangeSpy.and.returnValue(mockedRange);

const rawEvent = {
key: 'ArrowRight',
} as any;
plugin.onPluginEvent!({
eventType: 'keyDown',
rawEvent,
});

expect(setDOMSelectionSpy).toHaveBeenCalledTimes(0);
expect(setDOMSelectionSpy).not.toHaveBeenCalledWith({
type: 'range',
range: mockedRange,
isReverted: false,
});
});
});

describe('SelectionPlugin handle table selection', () => {
Expand Down

0 comments on commit 673f750

Please sign in to comment.