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

Fix Image range selection #2768

Merged
merged 5 commits into from
Aug 19, 2024
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 @@ -21,7 +21,6 @@ import type {
ParsedTable,
TableSelectionInfo,
TableCellCoordinate,
RangeSelection,
MouseUpEvent,
} from 'roosterjs-content-model-types';

Expand Down Expand Up @@ -362,6 +361,23 @@ class SelectionPlugin implements PluginWithState<SelectionPluginState> {
this.selectBeforeOrAfterElement(editor, selection.image);
}
}

if (
(isModifierKey(rawEvent) || rawEvent.shiftKey) &&
selection.image &&
!this.isSafari
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JiuqingSong Safari will ignore range selection

) {
const range = selection.image.ownerDocument.createRange();
range.selectNode(selection.image);
this.setDOMSelection(
{
type: 'range',
range,
isReverted: false,
},
null /* tableSelection */
);
}
break;

case 'range':
Expand Down Expand Up @@ -696,7 +712,6 @@ class SelectionPlugin implements PluginWithState<SelectionPluginState> {
if (this.isSafari) {
this.state.selection = newSelection;
}
this.trySelectSingleImage(newSelection);
}
}
};
Expand Down Expand Up @@ -768,21 +783,6 @@ class SelectionPlugin implements PluginWithState<SelectionPluginState> {
this.state.mouseDisposer = undefined;
}
}

private trySelectSingleImage(selection: RangeSelection) {
if (!selection.range.collapsed) {
const image = isSingleImageInSelection(selection.range);
if (image) {
this.setDOMSelection(
{
type: 'image',
image: image,
},
null /*tableSelection*/
);
}
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,18 @@ describe('SelectionPlugin handle image selection', () => {
key: 'A',
stopPropagation: stopPropagationSpy,
ctrlKey: true,
shiftKey: false,
} as any;

const mockedImage = {
parentNode: { childNodes: [] },
ownerDocument: {
createRange: () => {
return {
selectNode: (node: any) => {},
};
},
},
} as any;

mockedImage.parentNode.childNodes.push(mockedImage);
Expand All @@ -674,7 +682,7 @@ describe('SelectionPlugin handle image selection', () => {
});

expect(stopPropagationSpy).not.toHaveBeenCalled();
expect(setDOMSelectionSpy).not.toHaveBeenCalled();
expect(setDOMSelectionSpy).toHaveBeenCalled();
});

it('key down - other key, image has no parent', () => {
Expand Down Expand Up @@ -2823,40 +2831,4 @@ describe('SelectionPlugin selectionChange on image selected', () => {
expect(setDOMSelectionSpy).not.toHaveBeenCalled();
expect(getDOMSelectionSpy).toHaveBeenCalledTimes(1);
});

it('onSelectionChange on image | 4', () => {
const image = document.createElement('img');
spyOn(isSingleImageInSelection, 'isSingleImageInSelection').and.returnValue(image);

const plugin = createSelectionPlugin({});
const state = plugin.getState();
const mockedOldSelection = {
type: 'image',
image: {} as any,
} as DOMSelection;

state.selection = mockedOldSelection;

plugin.initialize(editor);

const onSelectionChange = addEventListenerSpy.calls.argsFor(0)[1] as Function;
const mockedNewSelection = {
type: 'range',
range: {} as any,
} as any;

hasFocusSpy.and.returnValue(true);
isInShadowEditSpy.and.returnValue(false);
getDOMSelectionSpy.and.returnValue(mockedNewSelection);
getRangeAtSpy.and.returnValue({ startContainer: {} });

onSelectionChange();

expect(setDOMSelectionSpy).toHaveBeenCalled();
expect(setDOMSelectionSpy).toHaveBeenCalledWith({
type: 'image',
image,
});
expect(getDOMSelectionSpy).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ import { updateWrapper } from './utils/updateWrapper';
import {
getSafeIdSelector,
isElementOfType,
isModifierKey,
isNodeOfType,
mutateSegment,
toArray,
unwrap,
} from 'roosterjs-content-model-dom';
import type { DragAndDropHelper } from '../pluginUtils/DragAndDrop/DragAndDropHelper';
Expand Down Expand Up @@ -168,41 +166,16 @@ export class ImageEditPlugin implements ImageEditor, EditorPlugin {
}
}

//Sometimes the cursor can be inside the editing image and inside shadow dom, then the cursor need to moved out of shadow dom
private selectBeforeEditingImage(editor: IEditor, element: HTMLElement) {
let parent = element.parentNode;
if (parent && isNodeOfType(parent, 'ELEMENT_NODE') && parent.shadowRoot) {
element = parent;
parent = parent.parentNode;
}
const index = parent && toArray(parent.childNodes).indexOf(element);
if (index !== null && index >= 0 && parent) {
const doc = editor.getDocument();
const range = doc.createRange();
range.setStart(parent, index);
range.collapse();
editor.setDOMSelection({
type: 'range',
range,
isReverted: false,
});
}
}

private keyDownHandler(editor: IEditor, event: KeyDownEvent) {
if (this.isEditing) {
if (event.rawEvent.key === 'Escape') {
this.removeImageWrapper();
} else {
const selection = editor.getDOMSelection();
const isImageSelection = selection?.type == 'image';
if (isImageSelection) {
this.selectBeforeEditingImage(editor, selection.image);
}
this.applyFormatWithContentModel(
editor,
this.isCropMode,
(isModifierKey(event.rawEvent) || event.rawEvent.shiftKey) && isImageSelection //if it's a modifier key over a image, the image should select the image
true /** should selectImage */,
false /* isApiOperation */
);
}
}
Expand Down Expand Up @@ -260,6 +233,7 @@ export class ImageEditPlugin implements ImageEditor, EditorPlugin {
if (shouldSelectImage) {
normalizeImageSelection(previousSelectedImage);
}

this.cleanInfo();
result = true;
}
Expand Down
Loading