Skip to content

Commit

Permalink
Merge pull request #2931 from microsoft/u/juliaroldi/undo-images
Browse files Browse the repository at this point in the history
Stop editing when content changed
  • Loading branch information
juliaroldi authored Jan 28, 2025
2 parents f960478 + f969062 commit 555c239
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type { DragAndDropContext } from './types/DragAndDropContext';
import type { ImageHtmlOptions } from './types/ImageHtmlOptions';
import type { ImageEditOptions } from './types/ImageEditOptions';
import type {
ContentChangedEvent,
ContentModelImage,
EditorPlugin,
IEditor,
Expand All @@ -55,6 +56,7 @@ const MouseRightButton = 2;
const DRAG_ID = '_dragging';
const IMAGE_EDIT_CLASS = 'imageEdit';
const IMAGE_EDIT_CLASS_CARET = 'imageEditCaretColor';
const IMAGE_EDIT_FORMAT_EVENT = 'ImageEditEvent';

/**
* ImageEdit plugin handles the following image editing features:
Expand Down Expand Up @@ -170,9 +172,7 @@ export class ImageEditPlugin implements ImageEditor, EditorPlugin {
this.keyDownHandler(this.editor, event);
break;
case 'contentChanged':
if (event.source == ChangeSource.Drop) {
this.onDropHandler(this.editor);
}
this.contentChangedHandler(this.editor, event);
break;
case 'extractContentWithDom':
this.removeImageEditing(event.clonedRoot);
Expand Down Expand Up @@ -279,6 +279,35 @@ export class ImageEditPlugin implements ImageEditor, EditorPlugin {
}
}

private setContentHandler(editor: IEditor) {
const selection = editor.getDOMSelection();
if (selection?.type == 'image' && selection.image.dataset.isEditing && !this.isEditing) {
delete selection.image.dataset.isEditing;
}
}

private formatEventHandler(event: ContentChangedEvent) {
if (this.isEditing && event.formatApiName !== IMAGE_EDIT_FORMAT_EVENT) {
this.cleanInfo();
this.isEditing = false;
this.isCropMode = false;
}
}

private contentChangedHandler(editor: IEditor, event: ContentChangedEvent) {
switch (event.source) {
case ChangeSource.SetContent:
this.setContentHandler(editor);
break;
case ChangeSource.Format:
this.formatEventHandler(event);
break;
case ChangeSource.Drop:
this.onDropHandler(editor);
break;
}
}

/**
* EXPOSED FOR TESTING PURPOSE ONLY
*/
Expand Down Expand Up @@ -392,6 +421,7 @@ export class ImageEditPlugin implements ImageEditor, EditorPlugin {
}
}
},
apiName: IMAGE_EDIT_FORMAT_EVENT,
},
{
tryGetFromCache: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import {
ContentModelDocument,
ContentModelFormatter,
DOMEventRecord,
DOMSelection,
EditorEnvironment,
FormatContentModelOptions,
IEditor,
ImageSelection,
} from 'roosterjs-content-model-types';

describe('ImageEditPlugin', () => {
Expand Down Expand Up @@ -670,6 +672,55 @@ describe('ImageEditPlugin', () => {
expect(event.clonedRoot).toEqual(expectedClonedRoot);
plugin.dispose();
});

it('contentChanged - should remove isEditing', () => {
const plugin = new ImageEditPlugin();
const editor = initEditor('image_edit', [plugin], model);
plugin.initialize(editor);
const image = document.createElement('img');
image.dataset['isEditing'] = 'true';
const selection = {
type: 'image',
image,
} as DOMSelection;
spyOn(editor, 'getDOMSelection').and.returnValue(selection);
const event = {
eventType: 'contentChanged',
source: ChangeSource.SetContent,
} as any;
plugin.onPluginEvent(event);
const newSelection = editor.getDOMSelection() as ImageSelection;
expect(newSelection!.type).toBe('image');
expect(newSelection!.image.dataset.isEditing).toBeUndefined();
plugin.dispose();
});

it('contentChanged - should remove editor caret style', () => {
const plugin = new TestPlugin();
plugin.initialize(editor);
plugin.setIsEditing(true);
const event = {
eventType: 'contentChanged',
source: ChangeSource.Format,
} as any;
plugin.onPluginEvent(event);
expect(editor.setEditorStyle).toHaveBeenCalledWith('imageEditCaretColor', null);
plugin.dispose();
});

it('contentChanged - should not remove editor caret style', () => {
const plugin = new TestPlugin();
plugin.initialize(editor);
plugin.setIsEditing(true);
const event = {
eventType: 'contentChanged',
source: ChangeSource.Format,
formatApiName: 'ImageEditEvent',
} as any;
plugin.onPluginEvent(event);
expect(editor.setEditorStyle).not.toHaveBeenCalled();
plugin.dispose();
});
});

class TestPlugin extends ImageEditPlugin {
Expand Down

0 comments on commit 555c239

Please sign in to comment.