-
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 #2744 from microsoft/u/juliaroldi/crop-context-menu
- Loading branch information
Showing
3 changed files
with
170 additions
and
0 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
31 changes: 31 additions & 0 deletions
31
packages/roosterjs-content-model-plugins/lib/imageEdit/utils/normalizeImageSelection.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,31 @@ | ||
import { mutateBlock } from 'roosterjs-content-model-dom'; | ||
import type { ImageAndParagraph } from '../types/ImageAndParagraph'; | ||
|
||
/** | ||
* Selecting directly on the image will only capture the image segment. | ||
* However, if the selection is made while the image is within a wrapper, it will capture the span that encloses the image. | ||
* In the last case, the selection will be marked as <---SelectionMarker---><---Image---><---SelectionMarker--->. | ||
* To fix this behavior the extra selection markers are removed. | ||
* @internal | ||
*/ | ||
export function normalizeImageSelection(imageAndParagraph: ImageAndParagraph) { | ||
const paragraph = imageAndParagraph.paragraph; | ||
const index = paragraph.segments.indexOf(imageAndParagraph.image); | ||
if (index > 0) { | ||
const markerBefore = paragraph.segments[index - 1]; | ||
const markerAfter = paragraph.segments[index + 1]; | ||
if ( | ||
markerBefore && | ||
markerAfter && | ||
markerAfter.segmentType == 'SelectionMarker' && | ||
markerBefore.segmentType == 'SelectionMarker' && | ||
markerAfter.isSelected && | ||
markerBefore.isSelected | ||
) { | ||
const mutatedParagraph = mutateBlock(paragraph); | ||
mutatedParagraph.segments.splice(index - 1, 1); | ||
mutatedParagraph.segments.splice(index, 1); | ||
} | ||
return imageAndParagraph; | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
packages/roosterjs-content-model-plugins/test/imageEdit/utils/normalizeImageSelection.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,135 @@ | ||
import { ImageAndParagraph } from '../../../lib/imageEdit/types/ImageAndParagraph'; | ||
import { normalizeImageSelection } from '../../../lib/imageEdit/utils/normalizeImageSelection'; | ||
import type { ReadonlyContentModelImage } from 'roosterjs-content-model-types'; | ||
|
||
describe('normalizeImageSelection', () => { | ||
it('normalize image selection', () => { | ||
const image: ReadonlyContentModelImage = { | ||
segmentType: 'Image', | ||
src: 'test', | ||
format: { | ||
fontFamily: 'Calibri', | ||
fontSize: '11pt', | ||
textColor: 'rgb(0, 0, 0)', | ||
id: 'image_0', | ||
maxWidth: '1800px', | ||
}, | ||
dataset: {}, | ||
isSelectedAsImageSelection: true, | ||
isSelected: true, | ||
}; | ||
const imageAndParagraph: ImageAndParagraph = { | ||
paragraph: { | ||
blockType: 'Paragraph', | ||
segments: [ | ||
{ | ||
segmentType: 'SelectionMarker', | ||
isSelected: true, | ||
format: {}, | ||
}, | ||
image, | ||
{ | ||
segmentType: 'SelectionMarker', | ||
isSelected: true, | ||
format: {}, | ||
}, | ||
], | ||
format: {}, | ||
segmentFormat: { | ||
fontFamily: 'Calibri', | ||
fontSize: '11pt', | ||
textColor: 'rgb(0, 0, 0)', | ||
}, | ||
}, | ||
image: image, | ||
}; | ||
|
||
const result = normalizeImageSelection(imageAndParagraph); | ||
expect(result?.paragraph).toEqual({ | ||
blockType: 'Paragraph', | ||
segments: [ | ||
{ | ||
segmentType: 'Image', | ||
src: 'test', | ||
format: { | ||
fontFamily: 'Calibri', | ||
fontSize: '11pt', | ||
textColor: 'rgb(0, 0, 0)', | ||
id: 'image_0', | ||
maxWidth: '1800px', | ||
}, | ||
dataset: {}, | ||
isSelectedAsImageSelection: true, | ||
isSelected: true, | ||
}, | ||
], | ||
format: {}, | ||
segmentFormat: { | ||
fontFamily: 'Calibri', | ||
fontSize: '11pt', | ||
textColor: 'rgb(0, 0, 0)', | ||
}, | ||
}); | ||
}); | ||
|
||
it('normalize image selection', () => { | ||
const imageAndParagraph: ImageAndParagraph = { | ||
paragraph: { | ||
blockType: 'Paragraph', | ||
segments: [ | ||
{ | ||
segmentType: 'SelectionMarker', | ||
format: {}, | ||
}, | ||
{ | ||
segmentType: 'Text', | ||
text: 'test', | ||
format: {}, | ||
}, | ||
{ | ||
segmentType: 'Image', | ||
src: 'test', | ||
format: { | ||
fontFamily: 'Calibri', | ||
fontSize: '11pt', | ||
textColor: 'rgb(0, 0, 0)', | ||
id: 'image_0', | ||
maxWidth: '1800px', | ||
}, | ||
dataset: {}, | ||
isSelectedAsImageSelection: true, | ||
isSelected: true, | ||
}, | ||
{ | ||
segmentType: 'SelectionMarker', | ||
isSelected: true, | ||
format: {}, | ||
}, | ||
], | ||
format: {}, | ||
segmentFormat: { | ||
fontFamily: 'Calibri', | ||
fontSize: '11pt', | ||
textColor: 'rgb(0, 0, 0)', | ||
}, | ||
}, | ||
image: { | ||
segmentType: 'Image', | ||
src: 'test', | ||
format: { | ||
fontFamily: 'Calibri', | ||
fontSize: '11pt', | ||
textColor: 'rgb(0, 0, 0)', | ||
id: 'image_0', | ||
maxWidth: '1800px', | ||
}, | ||
dataset: {}, | ||
isSelectedAsImageSelection: true, | ||
isSelected: true, | ||
}, | ||
}; | ||
|
||
const result = normalizeImageSelection(imageAndParagraph); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |