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

Stop editing when content changed #2931

Merged
merged 5 commits into from
Jan 28, 2025
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 @@ -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
Loading