-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Writing Flow: Collapse range in horizontal edge check by selection direction #6467
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
Changes from 1 commit
654eb22
f7dcc8a
bef44c3
a8728bf
79fc388
83d3ff7
33184f9
f2ae9ef
d81ac98
284900e
bc252af
a961282
6431770
c0a0fc3
b5a54df
e087ddd
7b174ad
a528868
d6f1ab9
4523390
31596df
fa4ad6d
2afb649
dab93de
9dde841
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,37 @@ import tinymce from 'tinymce'; | |
| * Browser dependencies | ||
| */ | ||
| const { getComputedStyle, DOMRect } = window; | ||
| const { TEXT_NODE, ELEMENT_NODE } = window.Node; | ||
| const { TEXT_NODE, ELEMENT_NODE, DOCUMENT_POSITION_PRECEDING } = window.Node; | ||
|
|
||
| /** | ||
| * Returns true if the given selection object is in the forward direction, or | ||
| * false otherwise. | ||
| * | ||
| * @see https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition | ||
| * | ||
| * @param {Selection} selection Selection object to check. | ||
| * | ||
| * @return {boolean} Whether the selection is forward. | ||
| */ | ||
| function isSelectionForward( selection ) { | ||
| const { | ||
| anchorNode, | ||
| focusNode, | ||
| anchorOffset, | ||
| focusOffset, | ||
| } = selection; | ||
|
|
||
| const position = anchorNode.compareDocumentPosition( focusNode ); | ||
|
|
||
| return ( | ||
| // Compare whether anchor node precedes focus node. | ||
| position !== DOCUMENT_POSITION_PRECEDING && | ||
|
|
||
| // `compareDocumentPosition` returns 0 when passed the same node, in | ||
| // which case compare offsets. | ||
| ! ( position === 0 && anchorOffset > focusOffset ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether the caret is horizontally at the edge of the container. | ||
|
|
@@ -45,7 +75,9 @@ export function isHorizontalEdge( container, isReverse, collapseRanges = false ) | |
| let range = selection.rangeCount ? selection.getRangeAt( 0 ) : null; | ||
| if ( collapseRanges ) { | ||
| range = range.cloneRange(); | ||
| range.collapse( isReverse ); | ||
|
|
||
| // Collapse to the extent of the selection by direction. | ||
| range.collapse( ! isSelectionForward( selection ) ); | ||
|
||
| } | ||
|
|
||
| if ( ! range || ! range.collapsed ) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to update "caret" to "selection", as "caret" implies a collapsed selection I think.