Skip to content

Commit

Permalink
Merge pull request #2131 from microsoft/u/juliaroldi/image-select-copy
Browse files Browse the repository at this point in the history
Fix copy/cut images
  • Loading branch information
juliaroldi authored Oct 6, 2023
2 parents 1ba8d4a + 32f3c2f commit b6f6961
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
11 changes: 9 additions & 2 deletions packages/roosterjs-editor-core/lib/corePlugins/ImageSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ export default class ImageSelection implements EditorPlugin {
}
break;
case PluginEventType.KeyDown:
const key = event.rawEvent.key;
const rawEvent = event.rawEvent;
const key = rawEvent.key;
const keyDownSelection = this.editor.getSelectionRangeEx();
if (keyDownSelection.type === SelectionRangeTypes.ImageSelection) {
if (
!rawEvent.ctrlKey &&
!rawEvent.altKey &&
!rawEvent.shiftKey &&
!rawEvent.metaKey &&
keyDownSelection.type === SelectionRangeTypes.ImageSelection
) {
if (key === Escape) {
this.editor.select(keyDownSelection.image, PositionType.Before);
this.editor.getSelectionRange()?.collapse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ describe('ImageSelectionPlugin |', () => {
expect(selection.areAllCollapsed).toBe(true);
});

it('should not handle any key in a image in ctrl', () => {
editor.setContent(`<img id=${imageId}></img>`);
const target = document.getElementById(imageId);
editorIsFeatureEnabled.and.returnValue(true);
editor.focus();
editor.select(target);
const range = document.createRange();
range.selectNode(target!);
imageSelection.onPluginEvent(keyDown(Space, true));
imageSelection.onPluginEvent(keyUp(Space, true));
const selection = editor.getSelectionRangeEx();
expect(selection.type).toBe(SelectionRangeTypes.ImageSelection);
expect(selection.areAllCollapsed).toBe(false);
});

it('should handle contextMenu', () => {
editor.setContent(`<img id=${imageId}></img>`);
const target = document.getElementById(imageId);
Expand Down Expand Up @@ -149,24 +164,32 @@ describe('ImageSelectionPlugin |', () => {
expect(editor.select).not.toHaveBeenCalled();
});

const keyDown = (key: string): PluginEvent => {
const keyDown = (key: string, ctrlKey: boolean = false): PluginEvent => {
return {
eventType: PluginEventType.KeyDown,
rawEvent: <KeyboardEvent>{
key: key,
preventDefault: () => {},
stopPropagation: () => {},
shiftKey: false,
ctrlKey: ctrlKey,
altKey: false,
metaKey: false,
},
};
};

const keyUp = (key: string): PluginEvent => {
const keyUp = (key: string, ctrlKey: boolean = false): PluginEvent => {
return {
eventType: PluginEventType.KeyUp,
rawEvent: <KeyboardEvent>{
key: key,
preventDefault: () => {},
stopPropagation: () => {},
shiftKey: false,
ctrlKey: ctrlKey,
altKey: false,
metaKey: false,
},
};
};
Expand Down

0 comments on commit b6f6961

Please sign in to comment.