Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep image selection when editing #2049

Merged
merged 3 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ export default class ImageEdit implements EditorPlugin {
this.setEditingImage(null);
}
break;
case PluginEventType.MouseUp:
// When mouse up, if the image and the shadow span exists, the editing mode is on.
// To make sure the selection did not jump to the shadow root, reselect the image.
if (this.image && this.shadowSpan) {
this.editor?.select(this.image);
}
break;
case PluginEventType.KeyDown:
this.setEditingImage(null);
break;
Expand Down
42 changes: 30 additions & 12 deletions packages/roosterjs-editor-plugins/test/imageEdit/imageEditTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as TestHelper from '../TestHelper';
import ImageEditInfo from '../../lib/plugins/ImageEdit/types/ImageEditInfo';
import { IEditor, ImageEditOperation, PluginEvent, PluginEventType } from 'roosterjs-editor-types';
import { ImageEdit } from '../../lib/ImageEdit';
import {
IEditor,
ImageEditOperation,
PluginEvent,
PluginEventType,
SelectionRangeTypes,
} from 'roosterjs-editor-types';
import {
getEditInfoFromImage,
saveEditInfo,
Expand Down Expand Up @@ -225,7 +231,7 @@ describe('ImageEdit | rotate and flip', () => {
});
});

describe('ImageEdit | plugin events | quitting', () => {
describe('ImageEdit | plugin events | ', () => {
let editor: IEditor;
const TEST_ID = 'imageEditTest';
let plugin: ImageEdit;
Expand Down Expand Up @@ -270,22 +276,34 @@ describe('ImageEdit | plugin events | quitting', () => {
target.dispatchEvent(event);
};

it('image selection quit editing', () => {
const IMG_ID = 'IMAGE_ID_QUIT';
const mouseUp = (target: HTMLElement, keyNumber: number) => {
const rect = target.getBoundingClientRect();
const event = new MouseEvent('mouseup', {
view: window,
bubbles: true,
cancelable: true,
clientX: rect.left,
clientY: rect.top,
shiftKey: false,
button: keyNumber,
});
target.dispatchEvent(event);
};

it('mouse up | keep image selected if click in a image', () => {
const IMG_ID = 'IMAGE_ID_MOUSE';
const SPAN_ID = 'SPAN_ID';
const content = `<img id="${IMG_ID}" src='test'/><span id="${SPAN_ID}" ></span>`;
editor.setContent(content);
const image = document.getElementById(IMG_ID) as HTMLImageElement;
editor.focus();
editor.select(image);
expect(setEditingImageSpy).toHaveBeenCalled();
expect(setEditingImageSpy).toHaveBeenCalledWith(
image as any,
ImageEditOperation.ResizeAndRotate as any
);
mouseUp(image, 0);
const selection = editor.getSelectionRangeEx();
expect(selection.type).toBe(SelectionRangeTypes.ImageSelection);
});

it('mousedown quit editing', () => {
it('quitting | mousedown quit editing', () => {
const IMG_ID = 'IMAGE_ID_MOUSE';
const SPAN_ID = 'SPAN_ID';
const content = `<img id="${IMG_ID}" src='test'/><span id="${SPAN_ID}" ></span>`;
Expand All @@ -294,12 +312,12 @@ describe('ImageEdit | plugin events | quitting', () => {
const span = document.getElementById(SPAN_ID) as HTMLImageElement;
editor.focus();
editor.select(image);
mouseDown(span, 0);
mouseDown(span, 2);
expect(setEditingImageSpy).toHaveBeenCalled();
expect(setEditingImageSpy).toHaveBeenCalledWith(null);
});

it('keydown quit editing', () => {
it('quitting | keydown quit editing', () => {
const IMG_ID = 'IMAGE_ID';
const content = `<img id="${IMG_ID}" src='test'/>`;
editor.setContent(content);
Expand Down