Skip to content

Commit

Permalink
Writing flow: fix triple click inside text blocks (#64928)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: annezazu <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: eidolonnight <[email protected]>
  • Loading branch information
6 people authored Aug 30, 2024
1 parent 78f4af2 commit a3d1c07
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { useSelect, useDispatch } from '@wordpress/data';
import { useRefEffect } from '@wordpress/compose';
import { create } from '@wordpress/rich-text';
import { isSelectionForward } from '@wordpress/dom';

/**
* Internal dependencies
Expand Down Expand Up @@ -53,6 +54,14 @@ function extractSelectionEndNode( selection ) {
return focusNode;
}

// When the selection is forward (the selection ends with the focus node),
// the selection may extend into the next element with an offset of 0. This
// may trigger multi selection even though the selection does not visually
// end in the next block.
if ( focusOffset === 0 && isSelectionForward( selection ) ) {
return focusNode.previousSibling ?? focusNode.parentElement;
}

return focusNode.childNodes[ focusOffset ];
}

Expand Down
16 changes: 16 additions & 0 deletions packages/dom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,22 @@ _Returns_

- `boolean`: True if rtl, false if ltr.

### isSelectionForward

Returns true if the given selection object is in the forward direction, or false otherwise.

_Related_

- <https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition>

_Parameters_

- _selection_ `Selection`: Selection object to check.

_Returns_

- `boolean`: Whether the selection is forward.

### isTextContent

_Parameters_
Expand Down
1 change: 1 addition & 0 deletions packages/dom/src/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export { default as isEmpty } from './is-empty';
export { default as removeInvalidHTML } from './remove-invalid-html';
export { default as isRTL } from './is-rtl';
export { default as safeHTML } from './safe-html';
export { default as isSelectionForward } from './is-selection-forward';

0 comments on commit a3d1c07

Please sign in to comment.