@wordpress/dom 3.2.3 → 3.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -1
- package/build/dom/caret-range-from-point.js +2 -2
- package/build/dom/caret-range-from-point.js.map +1 -1
- package/build/dom/is-edge.js +1 -1
- package/build/dom/is-edge.js.map +1 -1
- package/build/dom/place-caret-at-edge.js +108 -0
- package/build/dom/place-caret-at-edge.js.map +1 -0
- package/build/dom/place-caret-at-horizontal-edge.js +2 -84
- package/build/dom/place-caret-at-horizontal-edge.js.map +1 -1
- package/build/dom/place-caret-at-vertical-edge.js +6 -56
- package/build/dom/place-caret-at-vertical-edge.js.map +1 -1
- package/build-module/dom/caret-range-from-point.js +2 -2
- package/build-module/dom/caret-range-from-point.js.map +1 -1
- package/build-module/dom/is-edge.js +1 -1
- package/build-module/dom/is-edge.js.map +1 -1
- package/build-module/dom/place-caret-at-edge.js +95 -0
- package/build-module/dom/place-caret-at-edge.js.map +1 -0
- package/build-module/dom/place-caret-at-horizontal-edge.js +2 -81
- package/build-module/dom/place-caret-at-horizontal-edge.js.map +1 -1
- package/build-module/dom/place-caret-at-vertical-edge.js +6 -54
- package/build-module/dom/place-caret-at-vertical-edge.js.map +1 -1
- package/build-types/dom/caret-range-from-point.d.ts +2 -2
- package/build-types/dom/is-edge.d.ts.map +1 -1
- package/build-types/dom/place-caret-at-edge.d.ts +9 -0
- package/build-types/dom/place-caret-at-edge.d.ts.map +1 -0
- package/build-types/dom/place-caret-at-horizontal-edge.d.ts.map +1 -1
- package/build-types/dom/place-caret-at-vertical-edge.d.ts +4 -5
- package/build-types/dom/place-caret-at-vertical-edge.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/dom/caret-range-from-point.js +2 -2
- package/src/dom/is-edge.js +4 -1
- package/src/dom/place-caret-at-edge.js +95 -0
- package/src/dom/place-caret-at-horizontal-edge.js +2 -83
- package/src/dom/place-caret-at-vertical-edge.js +6 -64
- package/src/test/dom.js +8 -0
- package/tsconfig.tsbuildinfo +1 -1
@@ -1,36 +1,7 @@
|
|
1
1
|
/**
|
2
2
|
* Internal dependencies
|
3
3
|
*/
|
4
|
-
import
|
5
|
-
/**
|
6
|
-
* Internal dependencies
|
7
|
-
*/
|
8
|
-
|
9
|
-
import hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';
|
10
|
-
import isInputOrTextArea from './is-input-or-text-area';
|
11
|
-
import isRTL from './is-rtl';
|
12
|
-
/**
|
13
|
-
* Gets the range to place.
|
14
|
-
*
|
15
|
-
* @param {HTMLElement} container Focusable element.
|
16
|
-
* @param {boolean} isReverse True for end, false for start.
|
17
|
-
*
|
18
|
-
* @return {Range|null} The range to place.
|
19
|
-
*/
|
20
|
-
|
21
|
-
function getRange(container, isReverse) {
|
22
|
-
const {
|
23
|
-
ownerDocument
|
24
|
-
} = container; // In the case of RTL scripts, the horizontal edge is at the opposite side.
|
25
|
-
|
26
|
-
const isReverseDir = isRTL(container) ? !isReverse : isReverse;
|
27
|
-
const containerRect = container.getBoundingClientRect(); // When placing at the end (isReverse), find the closest range to the bottom
|
28
|
-
// right corner. When placing at the start, to the top left corner.
|
29
|
-
|
30
|
-
const x = isReverse ? containerRect.right - 1 : containerRect.left + 1;
|
31
|
-
const y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;
|
32
|
-
return hiddenCaretRangeFromPoint(ownerDocument, x, y, container);
|
33
|
-
}
|
4
|
+
import placeCaretAtEdge from './place-caret-at-edge';
|
34
5
|
/**
|
35
6
|
* Places the caret at start or end of a given element.
|
36
7
|
*
|
@@ -38,57 +9,7 @@ function getRange(container, isReverse) {
|
|
38
9
|
* @param {boolean} isReverse True for end, false for start.
|
39
10
|
*/
|
40
11
|
|
41
|
-
|
42
12
|
export default function placeCaretAtHorizontalEdge(container, isReverse) {
|
43
|
-
|
44
|
-
return;
|
45
|
-
}
|
46
|
-
|
47
|
-
container.focus();
|
48
|
-
|
49
|
-
if (isInputOrTextArea(container)) {
|
50
|
-
// The element may not support selection setting.
|
51
|
-
if (typeof container.selectionStart !== 'number') {
|
52
|
-
return;
|
53
|
-
}
|
54
|
-
|
55
|
-
if (isReverse) {
|
56
|
-
container.selectionStart = container.value.length;
|
57
|
-
container.selectionEnd = container.value.length;
|
58
|
-
} else {
|
59
|
-
container.selectionStart = 0;
|
60
|
-
container.selectionEnd = 0;
|
61
|
-
}
|
62
|
-
|
63
|
-
return;
|
64
|
-
}
|
65
|
-
|
66
|
-
if (!container.isContentEditable) {
|
67
|
-
return;
|
68
|
-
}
|
69
|
-
|
70
|
-
let range = getRange(container, isReverse); // If no range range can be created or it is outside the container, the
|
71
|
-
// element may be out of view.
|
72
|
-
|
73
|
-
if (!range || !range.startContainer || !container.contains(range.startContainer)) {
|
74
|
-
container.scrollIntoView(isReverse);
|
75
|
-
range = getRange(container, isReverse);
|
76
|
-
|
77
|
-
if (!range || !range.startContainer || !container.contains(range.startContainer)) {
|
78
|
-
return;
|
79
|
-
}
|
80
|
-
}
|
81
|
-
|
82
|
-
const {
|
83
|
-
ownerDocument
|
84
|
-
} = container;
|
85
|
-
const {
|
86
|
-
defaultView
|
87
|
-
} = ownerDocument;
|
88
|
-
assertIsDefined(defaultView, 'defaultView');
|
89
|
-
const selection = defaultView.getSelection();
|
90
|
-
assertIsDefined(selection, 'selection');
|
91
|
-
selection.removeAllRanges();
|
92
|
-
selection.addRange(range);
|
13
|
+
return placeCaretAtEdge(container, isReverse, undefined);
|
93
14
|
}
|
94
15
|
//# sourceMappingURL=place-caret-at-horizontal-edge.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["@wordpress/dom/src/dom/place-caret-at-horizontal-edge.js"],"names":["
|
1
|
+
{"version":3,"sources":["@wordpress/dom/src/dom/place-caret-at-horizontal-edge.js"],"names":["placeCaretAtEdge","placeCaretAtHorizontalEdge","container","isReverse","undefined"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,gBAAP,MAA6B,uBAA7B;AAEA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,0BAAT,CAAqCC,SAArC,EAAgDC,SAAhD,EAA4D;AAC1E,SAAOH,gBAAgB,CAAEE,SAAF,EAAaC,SAAb,EAAwBC,SAAxB,CAAvB;AACA","sourcesContent":["/**\n * Internal dependencies\n */\nimport placeCaretAtEdge from './place-caret-at-edge';\n\n/**\n * Places the caret at start or end of a given element.\n *\n * @param {HTMLElement} container Focusable element.\n * @param {boolean} isReverse True for end, false for start.\n */\nexport default function placeCaretAtHorizontalEdge( container, isReverse ) {\n\treturn placeCaretAtEdge( container, isReverse, undefined );\n}\n"]}
|
@@ -1,64 +1,16 @@
|
|
1
1
|
/**
|
2
2
|
* Internal dependencies
|
3
3
|
*/
|
4
|
-
import
|
5
|
-
import hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';
|
6
|
-
import { assertIsDefined } from '../utils/assert-is-defined';
|
4
|
+
import placeCaretAtEdge from './place-caret-at-edge';
|
7
5
|
/**
|
8
6
|
* Places the caret at the top or bottom of a given element.
|
9
7
|
*
|
10
|
-
* @param {HTMLElement} container
|
11
|
-
* @param {boolean} isReverse
|
12
|
-
* @param {DOMRect} [rect]
|
13
|
-
* @param {boolean} [mayUseScroll=true] True to allow scrolling, false to disallow.
|
8
|
+
* @param {HTMLElement} container Focusable element.
|
9
|
+
* @param {boolean} isReverse True for bottom, false for top.
|
10
|
+
* @param {DOMRect} [rect] The rectangle to position the caret with.
|
14
11
|
*/
|
15
12
|
|
16
|
-
export default function placeCaretAtVerticalEdge(container, isReverse, rect
|
17
|
-
|
18
|
-
return;
|
19
|
-
}
|
20
|
-
|
21
|
-
if (!rect || !container.isContentEditable) {
|
22
|
-
placeCaretAtHorizontalEdge(container, isReverse);
|
23
|
-
return;
|
24
|
-
}
|
25
|
-
|
26
|
-
container.focus(); // Offset by a buffer half the height of the caret rect. This is needed
|
27
|
-
// because caretRangeFromPoint may default to the end of the selection if
|
28
|
-
// offset is too close to the edge. It's unclear how to precisely calculate
|
29
|
-
// this threshold; it may be the padded area of some combination of line
|
30
|
-
// height, caret height, and font size. The buffer offset is effectively
|
31
|
-
// equivalent to a point at half the height of a line of text.
|
32
|
-
|
33
|
-
const buffer = rect.height / 2;
|
34
|
-
const editableRect = container.getBoundingClientRect();
|
35
|
-
const x = rect.left;
|
36
|
-
const y = isReverse ? editableRect.bottom - buffer : editableRect.top + buffer;
|
37
|
-
const {
|
38
|
-
ownerDocument
|
39
|
-
} = container;
|
40
|
-
const {
|
41
|
-
defaultView
|
42
|
-
} = ownerDocument;
|
43
|
-
const range = hiddenCaretRangeFromPoint(ownerDocument, x, y, container);
|
44
|
-
|
45
|
-
if (!range || !container.contains(range.startContainer)) {
|
46
|
-
if (mayUseScroll && (!range || !range.startContainer || !range.startContainer.contains(container))) {
|
47
|
-
// Might be out of view.
|
48
|
-
// Easier than attempting to calculate manually.
|
49
|
-
container.scrollIntoView(isReverse);
|
50
|
-
placeCaretAtVerticalEdge(container, isReverse, rect, false);
|
51
|
-
return;
|
52
|
-
}
|
53
|
-
|
54
|
-
placeCaretAtHorizontalEdge(container, isReverse);
|
55
|
-
return;
|
56
|
-
}
|
57
|
-
|
58
|
-
assertIsDefined(defaultView, 'defaultView');
|
59
|
-
const selection = defaultView.getSelection();
|
60
|
-
assertIsDefined(selection, 'selection');
|
61
|
-
selection.removeAllRanges();
|
62
|
-
selection.addRange(range);
|
13
|
+
export default function placeCaretAtVerticalEdge(container, isReverse, rect) {
|
14
|
+
return placeCaretAtEdge(container, isReverse, rect === null || rect === void 0 ? void 0 : rect.left);
|
63
15
|
}
|
64
16
|
//# sourceMappingURL=place-caret-at-vertical-edge.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["@wordpress/dom/src/dom/place-caret-at-vertical-edge.js"],"names":["
|
1
|
+
{"version":3,"sources":["@wordpress/dom/src/dom/place-caret-at-vertical-edge.js"],"names":["placeCaretAtEdge","placeCaretAtVerticalEdge","container","isReverse","rect","left"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,gBAAP,MAA6B,uBAA7B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,wBAAT,CAAmCC,SAAnC,EAA8CC,SAA9C,EAAyDC,IAAzD,EAAgE;AAC9E,SAAOJ,gBAAgB,CAAEE,SAAF,EAAaC,SAAb,EAAwBC,IAAxB,aAAwBA,IAAxB,uBAAwBA,IAAI,CAAEC,IAA9B,CAAvB;AACA","sourcesContent":["/**\n * Internal dependencies\n */\nimport placeCaretAtEdge from './place-caret-at-edge';\n\n/**\n * Places the caret at the top or bottom of a given element.\n *\n * @param {HTMLElement} container Focusable element.\n * @param {boolean} isReverse True for bottom, false for top.\n * @param {DOMRect} [rect] The rectangle to position the caret with.\n */\nexport default function placeCaretAtVerticalEdge( container, isReverse, rect ) {\n\treturn placeCaretAtEdge( container, isReverse, rect?.left );\n}\n"]}
|
@@ -5,8 +5,8 @@
|
|
5
5
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint
|
6
6
|
*
|
7
7
|
* @param {DocumentMaybeWithCaretPositionFromPoint} doc The document of the range.
|
8
|
-
* @param {number}
|
9
|
-
* @param {number}
|
8
|
+
* @param {number} x Horizontal position within the current viewport.
|
9
|
+
* @param {number} y Vertical position within the current viewport.
|
10
10
|
*
|
11
11
|
* @return {Range | null} The best range for the given point.
|
12
12
|
*/
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"is-edge.d.ts","sourceRoot":"","sources":["../../src/dom/is-edge.js"],"names":[],"mappings":"AAWA;;;;;;;;;;GAUG;AACH,0CANW,OAAO,aACP,OAAO,uCAGN,OAAO,
|
1
|
+
{"version":3,"file":"is-edge.d.ts","sourceRoot":"","sources":["../../src/dom/is-edge.js"],"names":[],"mappings":"AAWA;;;;;;;;;;GAUG;AACH,0CANW,OAAO,aACP,OAAO,uCAGN,OAAO,CA4GlB"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
/**
|
2
|
+
* Places the caret at start or end of a given element.
|
3
|
+
*
|
4
|
+
* @param {HTMLElement} container Focusable element.
|
5
|
+
* @param {boolean} isReverse True for end, false for start.
|
6
|
+
* @param {number|undefined} x X coordinate to vertically position.
|
7
|
+
*/
|
8
|
+
export default function placeCaretAtEdge(container: HTMLElement, isReverse: boolean, x: number | undefined): void;
|
9
|
+
//# sourceMappingURL=place-caret-at-edge.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"place-caret-at-edge.d.ts","sourceRoot":"","sources":["../../src/dom/place-caret-at-edge.js"],"names":[],"mappings":"AA+BA;;;;;;GAMG;AACH,oDAJW,WAAW,aACX,OAAO,KACP,MAAM,GAAC,SAAS,QA0D1B"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"place-caret-at-horizontal-edge.d.ts","sourceRoot":"","sources":["../../src/dom/place-caret-at-horizontal-edge.js"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"place-caret-at-horizontal-edge.d.ts","sourceRoot":"","sources":["../../src/dom/place-caret-at-horizontal-edge.js"],"names":[],"mappings":"AAKA;;;;;GAKG;AACH,8DAHW,WAAW,aACX,OAAO,QAIjB"}
|
@@ -1,10 +1,9 @@
|
|
1
1
|
/**
|
2
2
|
* Places the caret at the top or bottom of a given element.
|
3
3
|
*
|
4
|
-
* @param {HTMLElement} container
|
5
|
-
* @param {boolean} isReverse
|
6
|
-
* @param {DOMRect} [rect]
|
7
|
-
* @param {boolean} [mayUseScroll=true] True to allow scrolling, false to disallow.
|
4
|
+
* @param {HTMLElement} container Focusable element.
|
5
|
+
* @param {boolean} isReverse True for bottom, false for top.
|
6
|
+
* @param {DOMRect} [rect] The rectangle to position the caret with.
|
8
7
|
*/
|
9
|
-
export default function placeCaretAtVerticalEdge(container: HTMLElement, isReverse: boolean, rect?: DOMRect | undefined
|
8
|
+
export default function placeCaretAtVerticalEdge(container: HTMLElement, isReverse: boolean, rect?: DOMRect | undefined): void;
|
10
9
|
//# sourceMappingURL=place-caret-at-vertical-edge.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"place-caret-at-vertical-edge.d.ts","sourceRoot":"","sources":["../../src/dom/place-caret-at-vertical-edge.js"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"place-caret-at-vertical-edge.d.ts","sourceRoot":"","sources":["../../src/dom/place-caret-at-vertical-edge.js"],"names":[],"mappings":"AAKA;;;;;;GAMG;AACH,4DAJW,WAAW,aACX,OAAO,oCAKjB"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@wordpress/dom",
|
3
|
-
"version": "3.2.
|
3
|
+
"version": "3.2.4",
|
4
4
|
"description": "DOM utilities module for WordPress.",
|
5
5
|
"author": "The WordPress Contributors",
|
6
6
|
"license": "GPL-2.0-or-later",
|
@@ -34,5 +34,5 @@
|
|
34
34
|
"publishConfig": {
|
35
35
|
"access": "public"
|
36
36
|
},
|
37
|
-
"gitHead": "
|
37
|
+
"gitHead": "8f7f052bc04e3f4eb50f479ced14be1489b9fa79"
|
38
38
|
}
|
@@ -5,8 +5,8 @@
|
|
5
5
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint
|
6
6
|
*
|
7
7
|
* @param {DocumentMaybeWithCaretPositionFromPoint} doc The document of the range.
|
8
|
-
* @param {number}
|
9
|
-
* @param {number}
|
8
|
+
* @param {number} x Horizontal position within the current viewport.
|
9
|
+
* @param {number} y Vertical position within the current viewport.
|
10
10
|
*
|
11
11
|
* @return {Range | null} The best range for the given point.
|
12
12
|
*/
|
package/src/dom/is-edge.js
CHANGED
@@ -21,7 +21,10 @@ import isInputOrTextArea from './is-input-or-text-area';
|
|
21
21
|
* @return {boolean} True if at the edge, false if not.
|
22
22
|
*/
|
23
23
|
export default function isEdge( container, isReverse, onlyVertical = false ) {
|
24
|
-
if (
|
24
|
+
if (
|
25
|
+
isInputOrTextArea( container ) &&
|
26
|
+
typeof container.selectionStart === 'number'
|
27
|
+
) {
|
25
28
|
if ( container.selectionStart !== container.selectionEnd ) {
|
26
29
|
return false;
|
27
30
|
}
|
@@ -0,0 +1,95 @@
|
|
1
|
+
/**
|
2
|
+
* Internal dependencies
|
3
|
+
*/
|
4
|
+
import hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';
|
5
|
+
import { assertIsDefined } from '../utils/assert-is-defined';
|
6
|
+
import isInputOrTextArea from './is-input-or-text-area';
|
7
|
+
import isRTL from './is-rtl';
|
8
|
+
|
9
|
+
/**
|
10
|
+
* Gets the range to place.
|
11
|
+
*
|
12
|
+
* @param {HTMLElement} container Focusable element.
|
13
|
+
* @param {boolean} isReverse True for end, false for start.
|
14
|
+
* @param {number|undefined} x X coordinate to vertically position.
|
15
|
+
*
|
16
|
+
* @return {Range|null} The range to place.
|
17
|
+
*/
|
18
|
+
function getRange( container, isReverse, x ) {
|
19
|
+
const { ownerDocument } = container;
|
20
|
+
// In the case of RTL scripts, the horizontal edge is at the opposite side.
|
21
|
+
const isReverseDir = isRTL( container ) ? ! isReverse : isReverse;
|
22
|
+
const containerRect = container.getBoundingClientRect();
|
23
|
+
// When placing at the end (isReverse), find the closest range to the bottom
|
24
|
+
// right corner. When placing at the start, to the top left corner.
|
25
|
+
if ( x === undefined ) {
|
26
|
+
x = isReverse ? containerRect.right - 1 : containerRect.left + 1;
|
27
|
+
}
|
28
|
+
const y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;
|
29
|
+
return hiddenCaretRangeFromPoint( ownerDocument, x, y, container );
|
30
|
+
}
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Places the caret at start or end of a given element.
|
34
|
+
*
|
35
|
+
* @param {HTMLElement} container Focusable element.
|
36
|
+
* @param {boolean} isReverse True for end, false for start.
|
37
|
+
* @param {number|undefined} x X coordinate to vertically position.
|
38
|
+
*/
|
39
|
+
export default function placeCaretAtEdge( container, isReverse, x ) {
|
40
|
+
if ( ! container ) {
|
41
|
+
return;
|
42
|
+
}
|
43
|
+
|
44
|
+
container.focus();
|
45
|
+
|
46
|
+
if ( isInputOrTextArea( container ) ) {
|
47
|
+
// The element may not support selection setting.
|
48
|
+
if ( typeof container.selectionStart !== 'number' ) {
|
49
|
+
return;
|
50
|
+
}
|
51
|
+
|
52
|
+
if ( isReverse ) {
|
53
|
+
container.selectionStart = container.value.length;
|
54
|
+
container.selectionEnd = container.value.length;
|
55
|
+
} else {
|
56
|
+
container.selectionStart = 0;
|
57
|
+
container.selectionEnd = 0;
|
58
|
+
}
|
59
|
+
|
60
|
+
return;
|
61
|
+
}
|
62
|
+
|
63
|
+
if ( ! container.isContentEditable ) {
|
64
|
+
return;
|
65
|
+
}
|
66
|
+
|
67
|
+
let range = getRange( container, isReverse, x );
|
68
|
+
|
69
|
+
// If no range range can be created or it is outside the container, the
|
70
|
+
// element may be out of view.
|
71
|
+
if (
|
72
|
+
! range ||
|
73
|
+
! range.startContainer ||
|
74
|
+
! container.contains( range.startContainer )
|
75
|
+
) {
|
76
|
+
container.scrollIntoView( isReverse );
|
77
|
+
range = range = getRange( container, isReverse, x );
|
78
|
+
|
79
|
+
if (
|
80
|
+
! range ||
|
81
|
+
! range.startContainer ||
|
82
|
+
! container.contains( range.startContainer )
|
83
|
+
) {
|
84
|
+
return;
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
const { ownerDocument } = container;
|
89
|
+
const { defaultView } = ownerDocument;
|
90
|
+
assertIsDefined( defaultView, 'defaultView' );
|
91
|
+
const selection = defaultView.getSelection();
|
92
|
+
assertIsDefined( selection, 'selection' );
|
93
|
+
selection.removeAllRanges();
|
94
|
+
selection.addRange( range );
|
95
|
+
}
|
@@ -1,34 +1,7 @@
|
|
1
1
|
/**
|
2
2
|
* Internal dependencies
|
3
3
|
*/
|
4
|
-
import
|
5
|
-
|
6
|
-
/**
|
7
|
-
* Internal dependencies
|
8
|
-
*/
|
9
|
-
import hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';
|
10
|
-
import isInputOrTextArea from './is-input-or-text-area';
|
11
|
-
import isRTL from './is-rtl';
|
12
|
-
|
13
|
-
/**
|
14
|
-
* Gets the range to place.
|
15
|
-
*
|
16
|
-
* @param {HTMLElement} container Focusable element.
|
17
|
-
* @param {boolean} isReverse True for end, false for start.
|
18
|
-
*
|
19
|
-
* @return {Range|null} The range to place.
|
20
|
-
*/
|
21
|
-
function getRange( container, isReverse ) {
|
22
|
-
const { ownerDocument } = container;
|
23
|
-
// In the case of RTL scripts, the horizontal edge is at the opposite side.
|
24
|
-
const isReverseDir = isRTL( container ) ? ! isReverse : isReverse;
|
25
|
-
const containerRect = container.getBoundingClientRect();
|
26
|
-
// When placing at the end (isReverse), find the closest range to the bottom
|
27
|
-
// right corner. When placing at the start, to the top left corner.
|
28
|
-
const x = isReverse ? containerRect.right - 1 : containerRect.left + 1;
|
29
|
-
const y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;
|
30
|
-
return hiddenCaretRangeFromPoint( ownerDocument, x, y, container );
|
31
|
-
}
|
4
|
+
import placeCaretAtEdge from './place-caret-at-edge';
|
32
5
|
|
33
6
|
/**
|
34
7
|
* Places the caret at start or end of a given element.
|
@@ -37,59 +10,5 @@ function getRange( container, isReverse ) {
|
|
37
10
|
* @param {boolean} isReverse True for end, false for start.
|
38
11
|
*/
|
39
12
|
export default function placeCaretAtHorizontalEdge( container, isReverse ) {
|
40
|
-
|
41
|
-
return;
|
42
|
-
}
|
43
|
-
|
44
|
-
container.focus();
|
45
|
-
|
46
|
-
if ( isInputOrTextArea( container ) ) {
|
47
|
-
// The element may not support selection setting.
|
48
|
-
if ( typeof container.selectionStart !== 'number' ) {
|
49
|
-
return;
|
50
|
-
}
|
51
|
-
|
52
|
-
if ( isReverse ) {
|
53
|
-
container.selectionStart = container.value.length;
|
54
|
-
container.selectionEnd = container.value.length;
|
55
|
-
} else {
|
56
|
-
container.selectionStart = 0;
|
57
|
-
container.selectionEnd = 0;
|
58
|
-
}
|
59
|
-
|
60
|
-
return;
|
61
|
-
}
|
62
|
-
|
63
|
-
if ( ! container.isContentEditable ) {
|
64
|
-
return;
|
65
|
-
}
|
66
|
-
|
67
|
-
let range = getRange( container, isReverse );
|
68
|
-
|
69
|
-
// If no range range can be created or it is outside the container, the
|
70
|
-
// element may be out of view.
|
71
|
-
if (
|
72
|
-
! range ||
|
73
|
-
! range.startContainer ||
|
74
|
-
! container.contains( range.startContainer )
|
75
|
-
) {
|
76
|
-
container.scrollIntoView( isReverse );
|
77
|
-
range = getRange( container, isReverse );
|
78
|
-
|
79
|
-
if (
|
80
|
-
! range ||
|
81
|
-
! range.startContainer ||
|
82
|
-
! container.contains( range.startContainer )
|
83
|
-
) {
|
84
|
-
return;
|
85
|
-
}
|
86
|
-
}
|
87
|
-
|
88
|
-
const { ownerDocument } = container;
|
89
|
-
const { defaultView } = ownerDocument;
|
90
|
-
assertIsDefined( defaultView, 'defaultView' );
|
91
|
-
const selection = defaultView.getSelection();
|
92
|
-
assertIsDefined( selection, 'selection' );
|
93
|
-
selection.removeAllRanges();
|
94
|
-
selection.addRange( range );
|
13
|
+
return placeCaretAtEdge( container, isReverse, undefined );
|
95
14
|
}
|
@@ -1,73 +1,15 @@
|
|
1
1
|
/**
|
2
2
|
* Internal dependencies
|
3
3
|
*/
|
4
|
-
import
|
5
|
-
import hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';
|
6
|
-
import { assertIsDefined } from '../utils/assert-is-defined';
|
4
|
+
import placeCaretAtEdge from './place-caret-at-edge';
|
7
5
|
|
8
6
|
/**
|
9
7
|
* Places the caret at the top or bottom of a given element.
|
10
8
|
*
|
11
|
-
* @param {HTMLElement} container
|
12
|
-
* @param {boolean} isReverse
|
13
|
-
* @param {DOMRect} [rect]
|
14
|
-
* @param {boolean} [mayUseScroll=true] True to allow scrolling, false to disallow.
|
9
|
+
* @param {HTMLElement} container Focusable element.
|
10
|
+
* @param {boolean} isReverse True for bottom, false for top.
|
11
|
+
* @param {DOMRect} [rect] The rectangle to position the caret with.
|
15
12
|
*/
|
16
|
-
export default function placeCaretAtVerticalEdge(
|
17
|
-
container,
|
18
|
-
isReverse,
|
19
|
-
rect,
|
20
|
-
mayUseScroll = true
|
21
|
-
) {
|
22
|
-
if ( ! container ) {
|
23
|
-
return;
|
24
|
-
}
|
25
|
-
|
26
|
-
if ( ! rect || ! container.isContentEditable ) {
|
27
|
-
placeCaretAtHorizontalEdge( container, isReverse );
|
28
|
-
return;
|
29
|
-
}
|
30
|
-
|
31
|
-
container.focus();
|
32
|
-
|
33
|
-
// Offset by a buffer half the height of the caret rect. This is needed
|
34
|
-
// because caretRangeFromPoint may default to the end of the selection if
|
35
|
-
// offset is too close to the edge. It's unclear how to precisely calculate
|
36
|
-
// this threshold; it may be the padded area of some combination of line
|
37
|
-
// height, caret height, and font size. The buffer offset is effectively
|
38
|
-
// equivalent to a point at half the height of a line of text.
|
39
|
-
const buffer = rect.height / 2;
|
40
|
-
const editableRect = container.getBoundingClientRect();
|
41
|
-
const x = rect.left;
|
42
|
-
const y = isReverse
|
43
|
-
? editableRect.bottom - buffer
|
44
|
-
: editableRect.top + buffer;
|
45
|
-
|
46
|
-
const { ownerDocument } = container;
|
47
|
-
const { defaultView } = ownerDocument;
|
48
|
-
const range = hiddenCaretRangeFromPoint( ownerDocument, x, y, container );
|
49
|
-
|
50
|
-
if ( ! range || ! container.contains( range.startContainer ) ) {
|
51
|
-
if (
|
52
|
-
mayUseScroll &&
|
53
|
-
( ! range ||
|
54
|
-
! range.startContainer ||
|
55
|
-
! range.startContainer.contains( container ) )
|
56
|
-
) {
|
57
|
-
// Might be out of view.
|
58
|
-
// Easier than attempting to calculate manually.
|
59
|
-
container.scrollIntoView( isReverse );
|
60
|
-
placeCaretAtVerticalEdge( container, isReverse, rect, false );
|
61
|
-
return;
|
62
|
-
}
|
63
|
-
|
64
|
-
placeCaretAtHorizontalEdge( container, isReverse );
|
65
|
-
return;
|
66
|
-
}
|
67
|
-
|
68
|
-
assertIsDefined( defaultView, 'defaultView' );
|
69
|
-
const selection = defaultView.getSelection();
|
70
|
-
assertIsDefined( selection, 'selection' );
|
71
|
-
selection.removeAllRanges();
|
72
|
-
selection.addRange( range );
|
13
|
+
export default function placeCaretAtVerticalEdge( container, isReverse, rect ) {
|
14
|
+
return placeCaretAtEdge( container, isReverse, rect?.left );
|
73
15
|
}
|
package/src/test/dom.js
CHANGED
@@ -84,6 +84,14 @@ describe( 'DOM', () => {
|
|
84
84
|
expect( isHorizontalEdge( div, true ) ).toBe( true );
|
85
85
|
expect( isHorizontalEdge( div, false ) ).toBe( true );
|
86
86
|
} );
|
87
|
+
|
88
|
+
it( 'should return true for input types that do not have selection ranges', () => {
|
89
|
+
const input = document.createElement( 'input' );
|
90
|
+
input.setAttribute( 'type', 'checkbox' );
|
91
|
+
parent.appendChild( input );
|
92
|
+
expect( isHorizontalEdge( input, true ) ).toBe( true );
|
93
|
+
expect( isHorizontalEdge( input, false ) ).toBe( true );
|
94
|
+
} );
|
87
95
|
} );
|
88
96
|
|
89
97
|
describe( 'placeCaretAtHorizontalEdge', () => {
|