-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2792 from microsoft/u/juliaroldi/safari-image-sel…
…ection Do not remove image selection when copy
- Loading branch information
Showing
5 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...osterjs-content-model-core/lib/corePlugin/copyPaste/utils/adjustImageSelectionOnSafari.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { IEditor, DOMSelection } from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* @internal | ||
* Adjust Image selection, so the copy by keyboard does not remove image selection. | ||
*/ | ||
export function adjustImageSelectionOnSafari(editor: IEditor, selection: DOMSelection | null) { | ||
if (editor.getEnvironment().isSafari && selection?.type == 'image') { | ||
const range = new Range(); | ||
range.setStartBefore(selection.image); | ||
range.setEndAfter(selection.image); | ||
editor.setDOMSelection({ | ||
range, | ||
type: 'range', | ||
isReverted: false, | ||
}); | ||
} | ||
} |
File renamed without changes.
70 changes: 70 additions & 0 deletions
70
...js-content-model-core/test/corePlugin/copyPaste/utils/adjustImageSelectionOnSafariTest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { adjustImageSelectionOnSafari } from '../../../../lib/corePlugin/copyPaste/utils/adjustImageSelectionOnSafari'; | ||
import type { DOMSelection, IEditor } from 'roosterjs-content-model-types'; | ||
|
||
describe('adjustImageSelectionOnSafari', () => { | ||
let getEnvironmentSpy: jasmine.Spy; | ||
let setDOMSelectionSpy: jasmine.Spy; | ||
let editor: IEditor; | ||
|
||
beforeEach(() => { | ||
getEnvironmentSpy = jasmine.createSpy('getEnvironment'); | ||
setDOMSelectionSpy = jasmine.createSpy('setDOMSelection'); | ||
editor = ({ | ||
getEnvironment: getEnvironmentSpy, | ||
setDOMSelection: setDOMSelectionSpy, | ||
} as any) as IEditor; | ||
}); | ||
|
||
it('should adjustSelection', () => { | ||
getEnvironmentSpy.and.returnValue({ | ||
isSafari: true, | ||
}); | ||
const image = document.createElement('img'); | ||
document.body.appendChild(image); | ||
const selection: DOMSelection = { | ||
type: 'image', | ||
image: image, | ||
}; | ||
const range = new Range(); | ||
range.setStartBefore(image); | ||
range.setEndAfter(image); | ||
|
||
adjustImageSelectionOnSafari(editor, selection); | ||
expect(setDOMSelectionSpy).toHaveBeenCalledTimes(1); | ||
expect(setDOMSelectionSpy).toHaveBeenCalledWith({ | ||
range: range, | ||
type: 'range', | ||
isReverted: false, | ||
}); | ||
|
||
document.body.removeChild(image); | ||
}); | ||
|
||
it('should not adjustSelection - it is not safari', () => { | ||
getEnvironmentSpy.and.returnValue({ | ||
isSafari: false, | ||
}); | ||
const image = new Image(); | ||
const selection: DOMSelection = { | ||
type: 'image', | ||
image: image, | ||
}; | ||
|
||
adjustImageSelectionOnSafari(editor, selection); | ||
expect(setDOMSelectionSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not adjustSelection - it is not image', () => { | ||
getEnvironmentSpy.and.returnValue({ | ||
isSafari: true, | ||
}); | ||
const selection: DOMSelection = { | ||
type: 'range', | ||
range: new Range(), | ||
isReverted: false, | ||
}; | ||
|
||
adjustImageSelectionOnSafari(editor, selection); | ||
expect(setDOMSelectionSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
...rePlugin/copyPaste/deleteEmptyListTest.ts → ...in/copyPaste/utils/deleteEmptyListTest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters