Skip to content

Commit

Permalink
Merge pull request #2224 from microsoft/u/juliaroldi/click-image
Browse files Browse the repository at this point in the history
Only select image on click
  • Loading branch information
juliaroldi authored Nov 22, 2023
2 parents 2699cf9 + 006fcae commit 3f7cba1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export default class ImageSelection implements EditorPlugin {
if (
safeInstanceOf(target, 'HTMLImageElement') &&
target.isContentEditable &&
event.rawEvent.button != mouseMiddleButton
event.rawEvent.button != mouseMiddleButton &&
event.isClicking
) {
this.editor.select(target);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
ImageSelectionRange,
PluginEvent,
PluginEventType,
PluginMouseUpEvent,
PluginMouseDownEvent,
} from 'roosterjs-editor-types';
export * from 'roosterjs-editor-dom/test/DomTestHelper';

Expand Down Expand Up @@ -58,15 +60,28 @@ describe('ImageSelectionPlugin |', () => {
editor.setContent(`<img id=${imageId}></img>`);
const target = document.getElementById(imageId);
editorIsFeatureEnabled.and.returnValue(true);
simulateMouseEvent('mousedown', target!, 0);
simulateMouseEvent('mouseup', target!, 0);
imageSelection.onPluginEvent(mouseDown(target!, 0));
imageSelection.onPluginEvent(mouseup(target!, 0, true));
editor.focus();

const selection = editor.getSelectionRangeEx();
expect(selection.type).toBe(SelectionRangeTypes.ImageSelection);
expect(selection.areAllCollapsed).toBe(false);
});

it('should not be triggered in mouse up left click', () => {
editor.setContent(`<img id=${imageId}></img>`);
const target = document.getElementById(imageId);
editorIsFeatureEnabled.and.returnValue(true);
imageSelection.onPluginEvent(mouseDown(target!, 0));
imageSelection.onPluginEvent(mouseup(target!, 0, false));
editor.focus();

const selection = editor.getSelectionRangeEx();
expect(selection.type).toBe(SelectionRangeTypes.Normal);
expect(selection.areAllCollapsed).toBe(true);
});

it('should handle a ESCAPE KEY in a image', () => {
editor.setContent(`<img id=${imageId}></img>`);
const target = document.getElementById(imageId);
Expand Down Expand Up @@ -204,17 +219,42 @@ describe('ImageSelectionPlugin |', () => {
};
};

function simulateMouseEvent(mouseEvent: string, target: HTMLElement, keyNumber: number) {
const mouseup = (
target: HTMLElement,
keyNumber: number,
isClicking: boolean
): PluginMouseUpEvent => {
const rect = target.getBoundingClientRect();
return {
eventType: PluginEventType.MouseUp,
rawEvent: <any>{
target: target,
view: window,
bubbles: true,
cancelable: true,
clientX: rect.left,
clientY: rect.top,
shiftKey: false,
button: keyNumber,
},
isClicking,
};
};

const mouseDown = (target: HTMLElement, keyNumber: number): PluginMouseDownEvent => {
const rect = target.getBoundingClientRect();
var event = new MouseEvent(mouseEvent, {
view: window,
bubbles: true,
cancelable: true,
clientX: rect.left,
clientY: rect.top,
shiftKey: false,
button: keyNumber,
});
target.dispatchEvent(event);
}
return {
eventType: PluginEventType.MouseDown,
rawEvent: <any>{
target: target,
view: window,
bubbles: true,
cancelable: true,
clientX: rect.left,
clientY: rect.top,
shiftKey: false,
button: keyNumber,
},
};
};
});

0 comments on commit 3f7cba1

Please sign in to comment.