-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable selecting image when the only element in the range is an Image (…
…#2554) * init * Address comment * Reuse isReverted from Range Selection * Fix build * Fix build * Unselect image when Up or Down, or it remains selected * remove unneeded changes and improve name of tests * Update --------- Co-authored-by: Julia Roldi <87443959+juliaroldi@users.noreply.github.com>
- Loading branch information
1 parent
35c3a33
commit b7d50b4
Showing
7 changed files
with
435 additions
and
75 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
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
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
42 changes: 42 additions & 0 deletions
42
packages/roosterjs-content-model-core/lib/corePlugin/selection/isSingleImageInSelection.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,42 @@ | ||
import { isElementOfType, isNodeOfType } from 'roosterjs-content-model-dom'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function isSingleImageInSelection(selection: Selection | Range): HTMLImageElement | null { | ||
const { startNode, endNode, startOffset, endOffset } = getProps(selection); | ||
|
||
const max = Math.max(startOffset, endOffset); | ||
const min = Math.min(startOffset, endOffset); | ||
|
||
if (startNode && endNode && startNode == endNode && max - min == 1) { | ||
const node = startNode?.childNodes.item(min); | ||
if (isNodeOfType(node, 'ELEMENT_NODE') && isElementOfType(node, 'img')) { | ||
return node; | ||
} | ||
} | ||
return null; | ||
} | ||
function getProps( | ||
selection: Selection | Range | ||
): { startNode: Node | null; endNode: Node | null; startOffset: number; endOffset: number } { | ||
if (isSelection(selection)) { | ||
return { | ||
startNode: selection.anchorNode, | ||
endNode: selection.focusNode, | ||
startOffset: selection.anchorOffset, | ||
endOffset: selection.focusOffset, | ||
}; | ||
} else { | ||
return { | ||
startNode: selection.startContainer, | ||
endNode: selection.endContainer, | ||
startOffset: selection.startOffset, | ||
endOffset: selection.endOffset, | ||
}; | ||
} | ||
} | ||
|
||
function isSelection(selection: Selection | Range): selection is Selection { | ||
return !!(selection as Selection).getRangeAt; | ||
} |
Oops, something went wrong.