@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
package/README.md
CHANGED
@@ -294,7 +294,6 @@ _Parameters_
|
|
294
294
|
- _container_ `HTMLElement`: Focusable element.
|
295
295
|
- _isReverse_ `boolean`: True for bottom, false for top.
|
296
296
|
- _rect_ `[DOMRect]`: The rectangle to position the caret with.
|
297
|
-
- _mayUseScroll_ `[boolean]`: True to allow scrolling, false to disallow.
|
298
297
|
|
299
298
|
### remove
|
300
299
|
|
@@ -12,8 +12,8 @@ exports.default = caretRangeFromPoint;
|
|
12
12
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint
|
13
13
|
*
|
14
14
|
* @param {DocumentMaybeWithCaretPositionFromPoint} doc The document of the range.
|
15
|
-
* @param {number}
|
16
|
-
* @param {number}
|
15
|
+
* @param {number} x Horizontal position within the current viewport.
|
16
|
+
* @param {number} y Vertical position within the current viewport.
|
17
17
|
*
|
18
18
|
* @return {Range | null} The best range for the given point.
|
19
19
|
*/
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["@wordpress/dom/src/dom/caret-range-from-point.js"],"names":["caretRangeFromPoint","doc","x","y","caretPositionFromPoint","point","range","createRange","setStart","offsetNode","offset","collapse"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,mBAAT,CAA8BC,GAA9B,EAAmCC,CAAnC,EAAsCC,CAAtC,EAA0C;AACxD,MAAKF,GAAG,CAACD,mBAAT,EAA+B;AAC9B,WAAOC,GAAG,CAACD,mBAAJ,CAAyBE,CAAzB,EAA4BC,CAA5B,CAAP;AACA;;AAED,MAAK,CAAEF,GAAG,CAACG,sBAAX,EAAoC;AACnC,WAAO,IAAP;AACA;;AAED,QAAMC,KAAK,GAAGJ,GAAG,CAACG,sBAAJ,CAA4BF,CAA5B,EAA+BC,CAA/B,CAAd,CATwD,CAWxD;AACA;;AACA,MAAK,CAAEE,KAAP,EAAe;AACd,WAAO,IAAP;AACA;;AAED,QAAMC,KAAK,GAAGL,GAAG,CAACM,WAAJ,EAAd;AAEAD,EAAAA,KAAK,CAACE,QAAN,CAAgBH,KAAK,CAACI,UAAtB,EAAkCJ,KAAK,CAACK,MAAxC;AACAJ,EAAAA,KAAK,CAACK,QAAN,CAAgB,IAAhB;AAEA,SAAOL,KAAP;AACA;AAED;AACA;AACA;AACA","sourcesContent":["/**\n * Polyfill.\n * Get a collapsed range for a given point.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint\n *\n * @param {DocumentMaybeWithCaretPositionFromPoint} doc The document of the range.\n * @param {number}
|
1
|
+
{"version":3,"sources":["@wordpress/dom/src/dom/caret-range-from-point.js"],"names":["caretRangeFromPoint","doc","x","y","caretPositionFromPoint","point","range","createRange","setStart","offsetNode","offset","collapse"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,mBAAT,CAA8BC,GAA9B,EAAmCC,CAAnC,EAAsCC,CAAtC,EAA0C;AACxD,MAAKF,GAAG,CAACD,mBAAT,EAA+B;AAC9B,WAAOC,GAAG,CAACD,mBAAJ,CAAyBE,CAAzB,EAA4BC,CAA5B,CAAP;AACA;;AAED,MAAK,CAAEF,GAAG,CAACG,sBAAX,EAAoC;AACnC,WAAO,IAAP;AACA;;AAED,QAAMC,KAAK,GAAGJ,GAAG,CAACG,sBAAJ,CAA4BF,CAA5B,EAA+BC,CAA/B,CAAd,CATwD,CAWxD;AACA;;AACA,MAAK,CAAEE,KAAP,EAAe;AACd,WAAO,IAAP;AACA;;AAED,QAAMC,KAAK,GAAGL,GAAG,CAACM,WAAJ,EAAd;AAEAD,EAAAA,KAAK,CAACE,QAAN,CAAgBH,KAAK,CAACI,UAAtB,EAAkCJ,KAAK,CAACK,MAAxC;AACAJ,EAAAA,KAAK,CAACK,QAAN,CAAgB,IAAhB;AAEA,SAAOL,KAAP;AACA;AAED;AACA;AACA;AACA","sourcesContent":["/**\n * Polyfill.\n * Get a collapsed range for a given point.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint\n *\n * @param {DocumentMaybeWithCaretPositionFromPoint} doc The document of the range.\n * @param {number} x Horizontal position within the current viewport.\n * @param {number} y Vertical position within the current viewport.\n *\n * @return {Range | null} The best range for the given point.\n */\nexport default function caretRangeFromPoint( doc, x, y ) {\n\tif ( doc.caretRangeFromPoint ) {\n\t\treturn doc.caretRangeFromPoint( x, y );\n\t}\n\n\tif ( ! doc.caretPositionFromPoint ) {\n\t\treturn null;\n\t}\n\n\tconst point = doc.caretPositionFromPoint( x, y );\n\n\t// If x or y are negative, outside viewport, or there is no text entry node.\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint\n\tif ( ! point ) {\n\t\treturn null;\n\t}\n\n\tconst range = doc.createRange();\n\n\trange.setStart( point.offsetNode, point.offset );\n\trange.collapse( true );\n\n\treturn range;\n}\n\n/**\n * @typedef {{caretPositionFromPoint?: (x: number, y: number)=> CaretPosition | null} & Document } DocumentMaybeWithCaretPositionFromPoint\n * @typedef {{ readonly offset: number; readonly offsetNode: Node; getClientRect(): DOMRect | null; }} CaretPosition\n */\n"]}
|
package/build/dom/is-edge.js
CHANGED
@@ -37,7 +37,7 @@ var _isInputOrTextArea = _interopRequireDefault(require("./is-input-or-text-area
|
|
37
37
|
* @return {boolean} True if at the edge, false if not.
|
38
38
|
*/
|
39
39
|
function isEdge(container, isReverse, onlyVertical = false) {
|
40
|
-
if ((0, _isInputOrTextArea.default)(container)) {
|
40
|
+
if ((0, _isInputOrTextArea.default)(container) && typeof container.selectionStart === 'number') {
|
41
41
|
if (container.selectionStart !== container.selectionEnd) {
|
42
42
|
return false;
|
43
43
|
}
|
package/build/dom/is-edge.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["@wordpress/dom/src/dom/is-edge.js"],"names":["isEdge","container","isReverse","onlyVertical","selectionStart","selectionEnd","value","length","isContentEditable","ownerDocument","defaultView","selection","getSelection","rangeCount","range","getRangeAt","collapsedRange","cloneRange","isForward","isCollapsed","collapse","collapsedRangeRect","rangeRect","rangeHeight","height","isReverseDir","containerRect","getBoundingClientRect","x","left","right","y","top","bottom","testRange","testRect","verticalSide","horizontalSide","verticalDiff","horizontalDiff","hasVerticalDiff","Math","abs","hasHorizontalDiff"],"mappings":";;;;;;;;;AAGA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AATA;AACA;AACA;;AASA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,MAAT,CAAiBC,SAAjB,EAA4BC,SAA5B,EAAuCC,YAAY,GAAG,KAAtD,EAA8D;AAC5E,
|
1
|
+
{"version":3,"sources":["@wordpress/dom/src/dom/is-edge.js"],"names":["isEdge","container","isReverse","onlyVertical","selectionStart","selectionEnd","value","length","isContentEditable","ownerDocument","defaultView","selection","getSelection","rangeCount","range","getRangeAt","collapsedRange","cloneRange","isForward","isCollapsed","collapse","collapsedRangeRect","rangeRect","rangeHeight","height","isReverseDir","containerRect","getBoundingClientRect","x","left","right","y","top","bottom","testRange","testRect","verticalSide","horizontalSide","verticalDiff","horizontalDiff","hasVerticalDiff","Math","abs","hasHorizontalDiff"],"mappings":";;;;;;;;;AAGA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AATA;AACA;AACA;;AASA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,MAAT,CAAiBC,SAAjB,EAA4BC,SAA5B,EAAuCC,YAAY,GAAG,KAAtD,EAA8D;AAC5E,MACC,gCAAmBF,SAAnB,KACA,OAAOA,SAAS,CAACG,cAAjB,KAAoC,QAFrC,EAGE;AACD,QAAKH,SAAS,CAACG,cAAV,KAA6BH,SAAS,CAACI,YAA5C,EAA2D;AAC1D,aAAO,KAAP;AACA;;AAED,QAAKH,SAAL,EAAiB;AAChB,a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isRTL from './is-rtl';\nimport getRangeHeight from './get-range-height';\nimport getRectangleFromRange from './get-rectangle-from-range';\nimport isSelectionForward from './is-selection-forward';\nimport hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';\nimport { assertIsDefined } from '../utils/assert-is-defined';\nimport isInputOrTextArea from './is-input-or-text-area';\n\n/**\n * Check whether the selection is at the edge of the container. Checks for\n * horizontal position by default. Set `onlyVertical` to true to check only\n * vertically.\n *\n * @param {Element} container Focusable element.\n * @param {boolean} isReverse Set to true to check left, false to check right.\n * @param {boolean} [onlyVertical=false] Set to true to check only vertical position.\n *\n * @return {boolean} True if at the edge, false if not.\n */\nexport default function isEdge( container, isReverse, onlyVertical = false ) {\n\tif (\n\t\tisInputOrTextArea( container ) &&\n\t\ttypeof container.selectionStart === 'number'\n\t) {\n\t\tif ( container.selectionStart !== container.selectionEnd ) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif ( isReverse ) {\n\t\t\treturn container.selectionStart === 0;\n\t\t}\n\n\t\treturn container.value.length === container.selectionStart;\n\t}\n\n\tif ( ! (/** @type {HTMLElement} */ ( container ).isContentEditable) ) {\n\t\treturn true;\n\t}\n\n\tconst { ownerDocument } = container;\n\tconst { defaultView } = ownerDocument;\n\n\tassertIsDefined( defaultView, 'defaultView' );\n\tconst selection = defaultView.getSelection();\n\n\tif ( ! selection || ! selection.rangeCount ) {\n\t\treturn false;\n\t}\n\n\tconst range = selection.getRangeAt( 0 );\n\tconst collapsedRange = range.cloneRange();\n\tconst isForward = isSelectionForward( selection );\n\tconst isCollapsed = selection.isCollapsed;\n\n\t// Collapse in direction of selection.\n\tif ( ! isCollapsed ) {\n\t\tcollapsedRange.collapse( ! isForward );\n\t}\n\n\tconst collapsedRangeRect = getRectangleFromRange( collapsedRange );\n\tconst rangeRect = getRectangleFromRange( range );\n\n\tif ( ! collapsedRangeRect || ! rangeRect ) {\n\t\treturn false;\n\t}\n\n\t// Only consider the multiline selection at the edge if the direction is\n\t// towards the edge. The selection is multiline if it is taller than the\n\t// collapsed selection.\n\tconst rangeHeight = getRangeHeight( range );\n\tif (\n\t\t! isCollapsed &&\n\t\trangeHeight &&\n\t\trangeHeight > collapsedRangeRect.height &&\n\t\tisForward === isReverse\n\t) {\n\t\treturn false;\n\t}\n\n\t// In the case of RTL scripts, the horizontal edge is at the opposite side.\n\tconst isReverseDir = isRTL( container ) ? ! isReverse : isReverse;\n\tconst containerRect = container.getBoundingClientRect();\n\n\t// To check if a selection is at the edge, we insert a test selection at the\n\t// edge of the container and check if the selections have the same vertical\n\t// or horizontal position. If they do, the selection is at the edge.\n\t// This method proves to be better than a DOM-based calculation for the\n\t// horizontal edge, since it ignores empty textnodes and a trailing line\n\t// break element. In other words, we need to check visual positioning, not\n\t// DOM positioning.\n\t// It also proves better than using the computed style for the vertical\n\t// edge, because we cannot know the padding and line height reliably in\n\t// pixels. `getComputedStyle` may return a value with different units.\n\tconst x = isReverseDir ? containerRect.left + 1 : containerRect.right - 1;\n\tconst y = isReverse ? containerRect.top + 1 : containerRect.bottom - 1;\n\tconst testRange = hiddenCaretRangeFromPoint(\n\t\townerDocument,\n\t\tx,\n\t\ty,\n\t\t/** @type {HTMLElement} */ ( container )\n\t);\n\n\tif ( ! testRange ) {\n\t\treturn false;\n\t}\n\n\tconst testRect = getRectangleFromRange( testRange );\n\n\tif ( ! testRect ) {\n\t\treturn false;\n\t}\n\n\tconst verticalSide = isReverse ? 'top' : 'bottom';\n\tconst horizontalSide = isReverseDir ? 'left' : 'right';\n\tconst verticalDiff = testRect[ verticalSide ] - rangeRect[ verticalSide ];\n\tconst horizontalDiff =\n\t\ttestRect[ horizontalSide ] - collapsedRangeRect[ horizontalSide ];\n\n\t// Allow the position to be 1px off.\n\tconst hasVerticalDiff = Math.abs( verticalDiff ) <= 1;\n\tconst hasHorizontalDiff = Math.abs( horizontalDiff ) <= 1;\n\n\treturn onlyVertical\n\t\t? hasVerticalDiff\n\t\t: hasVerticalDiff && hasHorizontalDiff;\n}\n"]}
|
@@ -0,0 +1,108 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
+
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
6
|
+
value: true
|
7
|
+
});
|
8
|
+
exports.default = placeCaretAtEdge;
|
9
|
+
|
10
|
+
var _hiddenCaretRangeFromPoint = _interopRequireDefault(require("./hidden-caret-range-from-point"));
|
11
|
+
|
12
|
+
var _assertIsDefined = require("../utils/assert-is-defined");
|
13
|
+
|
14
|
+
var _isInputOrTextArea = _interopRequireDefault(require("./is-input-or-text-area"));
|
15
|
+
|
16
|
+
var _isRtl = _interopRequireDefault(require("./is-rtl"));
|
17
|
+
|
18
|
+
/**
|
19
|
+
* Internal dependencies
|
20
|
+
*/
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Gets the range to place.
|
24
|
+
*
|
25
|
+
* @param {HTMLElement} container Focusable element.
|
26
|
+
* @param {boolean} isReverse True for end, false for start.
|
27
|
+
* @param {number|undefined} x X coordinate to vertically position.
|
28
|
+
*
|
29
|
+
* @return {Range|null} The range to place.
|
30
|
+
*/
|
31
|
+
function getRange(container, isReverse, x) {
|
32
|
+
const {
|
33
|
+
ownerDocument
|
34
|
+
} = container; // In the case of RTL scripts, the horizontal edge is at the opposite side.
|
35
|
+
|
36
|
+
const isReverseDir = (0, _isRtl.default)(container) ? !isReverse : isReverse;
|
37
|
+
const containerRect = container.getBoundingClientRect(); // When placing at the end (isReverse), find the closest range to the bottom
|
38
|
+
// right corner. When placing at the start, to the top left corner.
|
39
|
+
|
40
|
+
if (x === undefined) {
|
41
|
+
x = isReverse ? containerRect.right - 1 : containerRect.left + 1;
|
42
|
+
}
|
43
|
+
|
44
|
+
const y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;
|
45
|
+
return (0, _hiddenCaretRangeFromPoint.default)(ownerDocument, x, y, container);
|
46
|
+
}
|
47
|
+
/**
|
48
|
+
* Places the caret at start or end of a given element.
|
49
|
+
*
|
50
|
+
* @param {HTMLElement} container Focusable element.
|
51
|
+
* @param {boolean} isReverse True for end, false for start.
|
52
|
+
* @param {number|undefined} x X coordinate to vertically position.
|
53
|
+
*/
|
54
|
+
|
55
|
+
|
56
|
+
function placeCaretAtEdge(container, isReverse, x) {
|
57
|
+
if (!container) {
|
58
|
+
return;
|
59
|
+
}
|
60
|
+
|
61
|
+
container.focus();
|
62
|
+
|
63
|
+
if ((0, _isInputOrTextArea.default)(container)) {
|
64
|
+
// The element may not support selection setting.
|
65
|
+
if (typeof container.selectionStart !== 'number') {
|
66
|
+
return;
|
67
|
+
}
|
68
|
+
|
69
|
+
if (isReverse) {
|
70
|
+
container.selectionStart = container.value.length;
|
71
|
+
container.selectionEnd = container.value.length;
|
72
|
+
} else {
|
73
|
+
container.selectionStart = 0;
|
74
|
+
container.selectionEnd = 0;
|
75
|
+
}
|
76
|
+
|
77
|
+
return;
|
78
|
+
}
|
79
|
+
|
80
|
+
if (!container.isContentEditable) {
|
81
|
+
return;
|
82
|
+
}
|
83
|
+
|
84
|
+
let range = getRange(container, isReverse, x); // If no range range can be created or it is outside the container, the
|
85
|
+
// element may be out of view.
|
86
|
+
|
87
|
+
if (!range || !range.startContainer || !container.contains(range.startContainer)) {
|
88
|
+
container.scrollIntoView(isReverse);
|
89
|
+
range = range = getRange(container, isReverse, x);
|
90
|
+
|
91
|
+
if (!range || !range.startContainer || !container.contains(range.startContainer)) {
|
92
|
+
return;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
const {
|
97
|
+
ownerDocument
|
98
|
+
} = container;
|
99
|
+
const {
|
100
|
+
defaultView
|
101
|
+
} = ownerDocument;
|
102
|
+
(0, _assertIsDefined.assertIsDefined)(defaultView, 'defaultView');
|
103
|
+
const selection = defaultView.getSelection();
|
104
|
+
(0, _assertIsDefined.assertIsDefined)(selection, 'selection');
|
105
|
+
selection.removeAllRanges();
|
106
|
+
selection.addRange(range);
|
107
|
+
}
|
108
|
+
//# sourceMappingURL=place-caret-at-edge.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["@wordpress/dom/src/dom/place-caret-at-edge.js"],"names":["getRange","container","isReverse","x","ownerDocument","isReverseDir","containerRect","getBoundingClientRect","undefined","right","left","y","bottom","top","placeCaretAtEdge","focus","selectionStart","value","length","selectionEnd","isContentEditable","range","startContainer","contains","scrollIntoView","defaultView","selection","getSelection","removeAllRanges","addRange"],"mappings":";;;;;;;;;AAGA;;AACA;;AACA;;AACA;;AANA;AACA;AACA;;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,QAAT,CAAmBC,SAAnB,EAA8BC,SAA9B,EAAyCC,CAAzC,EAA6C;AAC5C,QAAM;AAAEC,IAAAA;AAAF,MAAoBH,SAA1B,CAD4C,CAE5C;;AACA,QAAMI,YAAY,GAAG,oBAAOJ,SAAP,IAAqB,CAAEC,SAAvB,GAAmCA,SAAxD;AACA,QAAMI,aAAa,GAAGL,SAAS,CAACM,qBAAV,EAAtB,CAJ4C,CAK5C;AACA;;AACA,MAAKJ,CAAC,KAAKK,SAAX,EAAuB;AACtBL,IAAAA,CAAC,GAAGD,SAAS,GAAGI,aAAa,CAACG,KAAd,GAAsB,CAAzB,GAA6BH,aAAa,CAACI,IAAd,GAAqB,CAA/D;AACA;;AACD,QAAMC,CAAC,GAAGN,YAAY,GAAGC,aAAa,CAACM,MAAd,GAAuB,CAA1B,GAA8BN,aAAa,CAACO,GAAd,GAAoB,CAAxE;AACA,SAAO,wCAA2BT,aAA3B,EAA0CD,CAA1C,EAA6CQ,CAA7C,EAAgDV,SAAhD,CAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACe,SAASa,gBAAT,CAA2Bb,SAA3B,EAAsCC,SAAtC,EAAiDC,CAAjD,EAAqD;AACnE,MAAK,CAAEF,SAAP,EAAmB;AAClB;AACA;;AAEDA,EAAAA,SAAS,CAACc,KAAV;;AAEA,MAAK,gCA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,CAACE,eAAV;AACAF,EAAAA,SAAS,CAACG,QAAV,CAAoBR,KAApB;AACA","sourcesContent":["/**\n * Internal dependencies\n */\nimport hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';\nimport { assertIsDefined } from '../utils/assert-is-defined';\nimport isInputOrTextArea from './is-input-or-text-area';\nimport isRTL from './is-rtl';\n\n/**\n * Gets the range to place.\n *\n * @param {HTMLElement} container Focusable element.\n * @param {boolean} isReverse True for end, false for start.\n * @param {number|undefined} x X coordinate to vertically position.\n *\n * @return {Range|null} The range to place.\n */\nfunction getRange( container, isReverse, x ) {\n\tconst { ownerDocument } = container;\n\t// In the case of RTL scripts, the horizontal edge is at the opposite side.\n\tconst isReverseDir = isRTL( container ) ? ! isReverse : isReverse;\n\tconst containerRect = container.getBoundingClientRect();\n\t// When placing at the end (isReverse), find the closest range to the bottom\n\t// right corner. When placing at the start, to the top left corner.\n\tif ( x === undefined ) {\n\t\tx = isReverse ? containerRect.right - 1 : containerRect.left + 1;\n\t}\n\tconst y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;\n\treturn hiddenCaretRangeFromPoint( ownerDocument, x, y, container );\n}\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 * @param {number|undefined} x X coordinate to vertically position.\n */\nexport default function placeCaretAtEdge( container, isReverse, x ) {\n\tif ( ! container ) {\n\t\treturn;\n\t}\n\n\tcontainer.focus();\n\n\tif ( isInputOrTextArea( container ) ) {\n\t\t// The element may not support selection setting.\n\t\tif ( typeof container.selectionStart !== 'number' ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( isReverse ) {\n\t\t\tcontainer.selectionStart = container.value.length;\n\t\t\tcontainer.selectionEnd = container.value.length;\n\t\t} else {\n\t\t\tcontainer.selectionStart = 0;\n\t\t\tcontainer.selectionEnd = 0;\n\t\t}\n\n\t\treturn;\n\t}\n\n\tif ( ! container.isContentEditable ) {\n\t\treturn;\n\t}\n\n\tlet range = getRange( container, isReverse, x );\n\n\t// If no range range can be created or it is outside the container, the\n\t// element may be out of view.\n\tif (\n\t\t! range ||\n\t\t! range.startContainer ||\n\t\t! container.contains( range.startContainer )\n\t) {\n\t\tcontainer.scrollIntoView( isReverse );\n\t\trange = range = getRange( container, isReverse, x );\n\n\t\tif (\n\t\t\t! range ||\n\t\t\t! range.startContainer ||\n\t\t\t! container.contains( range.startContainer )\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t}\n\n\tconst { ownerDocument } = container;\n\tconst { defaultView } = ownerDocument;\n\tassertIsDefined( defaultView, 'defaultView' );\n\tconst selection = defaultView.getSelection();\n\tassertIsDefined( selection, 'selection' );\n\tselection.removeAllRanges();\n\tselection.addRange( range );\n}\n"]}
|
@@ -7,101 +7,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
7
7
|
});
|
8
8
|
exports.default = placeCaretAtHorizontalEdge;
|
9
9
|
|
10
|
-
var
|
11
|
-
|
12
|
-
var _hiddenCaretRangeFromPoint = _interopRequireDefault(require("./hidden-caret-range-from-point"));
|
13
|
-
|
14
|
-
var _isInputOrTextArea = _interopRequireDefault(require("./is-input-or-text-area"));
|
15
|
-
|
16
|
-
var _isRtl = _interopRequireDefault(require("./is-rtl"));
|
17
|
-
|
18
|
-
/**
|
19
|
-
* Internal dependencies
|
20
|
-
*/
|
10
|
+
var _placeCaretAtEdge = _interopRequireDefault(require("./place-caret-at-edge"));
|
21
11
|
|
22
12
|
/**
|
23
13
|
* Internal dependencies
|
24
14
|
*/
|
25
15
|
|
26
|
-
/**
|
27
|
-
* Gets the range to place.
|
28
|
-
*
|
29
|
-
* @param {HTMLElement} container Focusable element.
|
30
|
-
* @param {boolean} isReverse True for end, false for start.
|
31
|
-
*
|
32
|
-
* @return {Range|null} The range to place.
|
33
|
-
*/
|
34
|
-
function getRange(container, isReverse) {
|
35
|
-
const {
|
36
|
-
ownerDocument
|
37
|
-
} = container; // In the case of RTL scripts, the horizontal edge is at the opposite side.
|
38
|
-
|
39
|
-
const isReverseDir = (0, _isRtl.default)(container) ? !isReverse : isReverse;
|
40
|
-
const containerRect = container.getBoundingClientRect(); // When placing at the end (isReverse), find the closest range to the bottom
|
41
|
-
// right corner. When placing at the start, to the top left corner.
|
42
|
-
|
43
|
-
const x = isReverse ? containerRect.right - 1 : containerRect.left + 1;
|
44
|
-
const y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;
|
45
|
-
return (0, _hiddenCaretRangeFromPoint.default)(ownerDocument, x, y, container);
|
46
|
-
}
|
47
16
|
/**
|
48
17
|
* Places the caret at start or end of a given element.
|
49
18
|
*
|
50
19
|
* @param {HTMLElement} container Focusable element.
|
51
20
|
* @param {boolean} isReverse True for end, false for start.
|
52
21
|
*/
|
53
|
-
|
54
|
-
|
55
22
|
function placeCaretAtHorizontalEdge(container, isReverse) {
|
56
|
-
|
57
|
-
return;
|
58
|
-
}
|
59
|
-
|
60
|
-
container.focus();
|
61
|
-
|
62
|
-
if ((0, _isInputOrTextArea.default)(container)) {
|
63
|
-
// The element may not support selection setting.
|
64
|
-
if (typeof container.selectionStart !== 'number') {
|
65
|
-
return;
|
66
|
-
}
|
67
|
-
|
68
|
-
if (isReverse) {
|
69
|
-
container.selectionStart = container.value.length;
|
70
|
-
container.selectionEnd = container.value.length;
|
71
|
-
} else {
|
72
|
-
container.selectionStart = 0;
|
73
|
-
container.selectionEnd = 0;
|
74
|
-
}
|
75
|
-
|
76
|
-
return;
|
77
|
-
}
|
78
|
-
|
79
|
-
if (!container.isContentEditable) {
|
80
|
-
return;
|
81
|
-
}
|
82
|
-
|
83
|
-
let range = getRange(container, isReverse); // If no range range can be created or it is outside the container, the
|
84
|
-
// element may be out of view.
|
85
|
-
|
86
|
-
if (!range || !range.startContainer || !container.contains(range.startContainer)) {
|
87
|
-
container.scrollIntoView(isReverse);
|
88
|
-
range = getRange(container, isReverse);
|
89
|
-
|
90
|
-
if (!range || !range.startContainer || !container.contains(range.startContainer)) {
|
91
|
-
return;
|
92
|
-
}
|
93
|
-
}
|
94
|
-
|
95
|
-
const {
|
96
|
-
ownerDocument
|
97
|
-
} = container;
|
98
|
-
const {
|
99
|
-
defaultView
|
100
|
-
} = ownerDocument;
|
101
|
-
(0, _assertIsDefined.assertIsDefined)(defaultView, 'defaultView');
|
102
|
-
const selection = defaultView.getSelection();
|
103
|
-
(0, _assertIsDefined.assertIsDefined)(selection, 'selection');
|
104
|
-
selection.removeAllRanges();
|
105
|
-
selection.addRange(range);
|
23
|
+
return (0, _placeCaretAtEdge.default)(container, isReverse, undefined);
|
106
24
|
}
|
107
25
|
//# 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":["placeCaretAtHorizontalEdge","container","isReverse","undefined"],"mappings":";;;;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,0BAAT,CAAqCC,SAArC,EAAgDC,SAAhD,EAA4D;AAC1E,SAAO,+BAAkBD,SAAlB,EAA6BC,SAA7B,EAAwCC,SAAxC,CAAP;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"]}
|
@@ -7,11 +7,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
7
7
|
});
|
8
8
|
exports.default = placeCaretAtVerticalEdge;
|
9
9
|
|
10
|
-
var
|
11
|
-
|
12
|
-
var _hiddenCaretRangeFromPoint = _interopRequireDefault(require("./hidden-caret-range-from-point"));
|
13
|
-
|
14
|
-
var _assertIsDefined = require("../utils/assert-is-defined");
|
10
|
+
var _placeCaretAtEdge = _interopRequireDefault(require("./place-caret-at-edge"));
|
15
11
|
|
16
12
|
/**
|
17
13
|
* Internal dependencies
|
@@ -20,57 +16,11 @@ var _assertIsDefined = require("../utils/assert-is-defined");
|
|
20
16
|
/**
|
21
17
|
* Places the caret at the top or bottom of a given element.
|
22
18
|
*
|
23
|
-
* @param {HTMLElement} container
|
24
|
-
* @param {boolean} isReverse
|
25
|
-
* @param {DOMRect} [rect]
|
26
|
-
* @param {boolean} [mayUseScroll=true] True to allow scrolling, false to disallow.
|
19
|
+
* @param {HTMLElement} container Focusable element.
|
20
|
+
* @param {boolean} isReverse True for bottom, false for top.
|
21
|
+
* @param {DOMRect} [rect] The rectangle to position the caret with.
|
27
22
|
*/
|
28
|
-
function placeCaretAtVerticalEdge(container, isReverse, rect
|
29
|
-
|
30
|
-
return;
|
31
|
-
}
|
32
|
-
|
33
|
-
if (!rect || !container.isContentEditable) {
|
34
|
-
(0, _placeCaretAtHorizontalEdge.default)(container, isReverse);
|
35
|
-
return;
|
36
|
-
}
|
37
|
-
|
38
|
-
container.focus(); // Offset by a buffer half the height of the caret rect. This is needed
|
39
|
-
// because caretRangeFromPoint may default to the end of the selection if
|
40
|
-
// offset is too close to the edge. It's unclear how to precisely calculate
|
41
|
-
// this threshold; it may be the padded area of some combination of line
|
42
|
-
// height, caret height, and font size. The buffer offset is effectively
|
43
|
-
// equivalent to a point at half the height of a line of text.
|
44
|
-
|
45
|
-
const buffer = rect.height / 2;
|
46
|
-
const editableRect = container.getBoundingClientRect();
|
47
|
-
const x = rect.left;
|
48
|
-
const y = isReverse ? editableRect.bottom - buffer : editableRect.top + buffer;
|
49
|
-
const {
|
50
|
-
ownerDocument
|
51
|
-
} = container;
|
52
|
-
const {
|
53
|
-
defaultView
|
54
|
-
} = ownerDocument;
|
55
|
-
const range = (0, _hiddenCaretRangeFromPoint.default)(ownerDocument, x, y, container);
|
56
|
-
|
57
|
-
if (!range || !container.contains(range.startContainer)) {
|
58
|
-
if (mayUseScroll && (!range || !range.startContainer || !range.startContainer.contains(container))) {
|
59
|
-
// Might be out of view.
|
60
|
-
// Easier than attempting to calculate manually.
|
61
|
-
container.scrollIntoView(isReverse);
|
62
|
-
placeCaretAtVerticalEdge(container, isReverse, rect, false);
|
63
|
-
return;
|
64
|
-
}
|
65
|
-
|
66
|
-
(0, _placeCaretAtHorizontalEdge.default)(container, isReverse);
|
67
|
-
return;
|
68
|
-
}
|
69
|
-
|
70
|
-
(0, _assertIsDefined.assertIsDefined)(defaultView, 'defaultView');
|
71
|
-
const selection = defaultView.getSelection();
|
72
|
-
(0, _assertIsDefined.assertIsDefined)(selection, 'selection');
|
73
|
-
selection.removeAllRanges();
|
74
|
-
selection.addRange(range);
|
23
|
+
function placeCaretAtVerticalEdge(container, isReverse, rect) {
|
24
|
+
return (0, _placeCaretAtEdge.default)(container, isReverse, rect === null || rect === void 0 ? void 0 : rect.left);
|
75
25
|
}
|
76
26
|
//# 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":["placeCaretAtVerticalEdge","container","isReverse","rect","
|
1
|
+
{"version":3,"sources":["@wordpress/dom/src/dom/place-caret-at-vertical-edge.js"],"names":["placeCaretAtVerticalEdge","container","isReverse","rect","left"],"mappings":";;;;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,wBAAT,CAAmCC,SAAnC,EAA8CC,SAA9C,EAAyDC,IAAzD,EAAgE;AAC9E,SAAO,+BAAkBF,SAAlB,EAA6BC,SAA7B,EAAwCC,IAAxC,aAAwCA,IAAxC,uBAAwCA,IAAI,CAAEC,IAA9C,CAAP;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,"sources":["@wordpress/dom/src/dom/caret-range-from-point.js"],"names":["caretRangeFromPoint","doc","x","y","caretPositionFromPoint","point","range","createRange","setStart","offsetNode","offset","collapse"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASA,mBAAT,CAA8BC,GAA9B,EAAmCC,CAAnC,EAAsCC,CAAtC,EAA0C;AACxD,MAAKF,GAAG,CAACD,mBAAT,EAA+B;AAC9B,WAAOC,GAAG,CAACD,mBAAJ,CAAyBE,CAAzB,EAA4BC,CAA5B,CAAP;AACA;;AAED,MAAK,CAAEF,GAAG,CAACG,sBAAX,EAAoC;AACnC,WAAO,IAAP;AACA;;AAED,QAAMC,KAAK,GAAGJ,GAAG,CAACG,sBAAJ,CAA4BF,CAA5B,EAA+BC,CAA/B,CAAd,CATwD,CAWxD;AACA;;AACA,MAAK,CAAEE,KAAP,EAAe;AACd,WAAO,IAAP;AACA;;AAED,QAAMC,KAAK,GAAGL,GAAG,CAACM,WAAJ,EAAd;AAEAD,EAAAA,KAAK,CAACE,QAAN,CAAgBH,KAAK,CAACI,UAAtB,EAAkCJ,KAAK,CAACK,MAAxC;AACAJ,EAAAA,KAAK,CAACK,QAAN,CAAgB,IAAhB;AAEA,SAAOL,KAAP;AACA;AAED;AACA;AACA;AACA","sourcesContent":["/**\n * Polyfill.\n * Get a collapsed range for a given point.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint\n *\n * @param {DocumentMaybeWithCaretPositionFromPoint} doc The document of the range.\n * @param {number}
|
1
|
+
{"version":3,"sources":["@wordpress/dom/src/dom/caret-range-from-point.js"],"names":["caretRangeFromPoint","doc","x","y","caretPositionFromPoint","point","range","createRange","setStart","offsetNode","offset","collapse"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASA,mBAAT,CAA8BC,GAA9B,EAAmCC,CAAnC,EAAsCC,CAAtC,EAA0C;AACxD,MAAKF,GAAG,CAACD,mBAAT,EAA+B;AAC9B,WAAOC,GAAG,CAACD,mBAAJ,CAAyBE,CAAzB,EAA4BC,CAA5B,CAAP;AACA;;AAED,MAAK,CAAEF,GAAG,CAACG,sBAAX,EAAoC;AACnC,WAAO,IAAP;AACA;;AAED,QAAMC,KAAK,GAAGJ,GAAG,CAACG,sBAAJ,CAA4BF,CAA5B,EAA+BC,CAA/B,CAAd,CATwD,CAWxD;AACA;;AACA,MAAK,CAAEE,KAAP,EAAe;AACd,WAAO,IAAP;AACA;;AAED,QAAMC,KAAK,GAAGL,GAAG,CAACM,WAAJ,EAAd;AAEAD,EAAAA,KAAK,CAACE,QAAN,CAAgBH,KAAK,CAACI,UAAtB,EAAkCJ,KAAK,CAACK,MAAxC;AACAJ,EAAAA,KAAK,CAACK,QAAN,CAAgB,IAAhB;AAEA,SAAOL,KAAP;AACA;AAED;AACA;AACA;AACA","sourcesContent":["/**\n * Polyfill.\n * Get a collapsed range for a given point.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint\n *\n * @param {DocumentMaybeWithCaretPositionFromPoint} doc The document of the range.\n * @param {number} x Horizontal position within the current viewport.\n * @param {number} y Vertical position within the current viewport.\n *\n * @return {Range | null} The best range for the given point.\n */\nexport default function caretRangeFromPoint( doc, x, y ) {\n\tif ( doc.caretRangeFromPoint ) {\n\t\treturn doc.caretRangeFromPoint( x, y );\n\t}\n\n\tif ( ! doc.caretPositionFromPoint ) {\n\t\treturn null;\n\t}\n\n\tconst point = doc.caretPositionFromPoint( x, y );\n\n\t// If x or y are negative, outside viewport, or there is no text entry node.\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint\n\tif ( ! point ) {\n\t\treturn null;\n\t}\n\n\tconst range = doc.createRange();\n\n\trange.setStart( point.offsetNode, point.offset );\n\trange.collapse( true );\n\n\treturn range;\n}\n\n/**\n * @typedef {{caretPositionFromPoint?: (x: number, y: number)=> CaretPosition | null} & Document } DocumentMaybeWithCaretPositionFromPoint\n * @typedef {{ readonly offset: number; readonly offsetNode: Node; getClientRect(): DOMRect | null; }} CaretPosition\n */\n"]}
|
@@ -21,7 +21,7 @@ import isInputOrTextArea from './is-input-or-text-area';
|
|
21
21
|
*/
|
22
22
|
|
23
23
|
export default function isEdge(container, isReverse, onlyVertical = false) {
|
24
|
-
if (isInputOrTextArea(container)) {
|
24
|
+
if (isInputOrTextArea(container) && typeof container.selectionStart === 'number') {
|
25
25
|
if (container.selectionStart !== container.selectionEnd) {
|
26
26
|
return false;
|
27
27
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["@wordpress/dom/src/dom/is-edge.js"],"names":["isRTL","getRangeHeight","getRectangleFromRange","isSelectionForward","hiddenCaretRangeFromPoint","assertIsDefined","isInputOrTextArea","isEdge","container","isReverse","onlyVertical","selectionStart","selectionEnd","value","length","isContentEditable","ownerDocument","defaultView","selection","getSelection","rangeCount","range","getRangeAt","collapsedRange","cloneRange","isForward","isCollapsed","collapse","collapsedRangeRect","rangeRect","rangeHeight","height","isReverseDir","containerRect","getBoundingClientRect","x","left","right","y","top","bottom","testRange","testRect","verticalSide","horizontalSide","verticalDiff","horizontalDiff","hasVerticalDiff","Math","abs","hasHorizontalDiff"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,KAAP,MAAkB,UAAlB;AACA,OAAOC,cAAP,MAA2B,oBAA3B;AACA,OAAOC,qBAAP,MAAkC,4BAAlC;AACA,OAAOC,kBAAP,MAA+B,wBAA/B;AACA,OAAOC,yBAAP,MAAsC,iCAAtC;AACA,SAASC,eAAT,QAAgC,4BAAhC;AACA,OAAOC,iBAAP,MAA8B,yBAA9B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,MAAT,CAAiBC,SAAjB,EAA4BC,SAA5B,EAAuCC,YAAY,GAAG,KAAtD,EAA8D;AAC5E,
|
1
|
+
{"version":3,"sources":["@wordpress/dom/src/dom/is-edge.js"],"names":["isRTL","getRangeHeight","getRectangleFromRange","isSelectionForward","hiddenCaretRangeFromPoint","assertIsDefined","isInputOrTextArea","isEdge","container","isReverse","onlyVertical","selectionStart","selectionEnd","value","length","isContentEditable","ownerDocument","defaultView","selection","getSelection","rangeCount","range","getRangeAt","collapsedRange","cloneRange","isForward","isCollapsed","collapse","collapsedRangeRect","rangeRect","rangeHeight","height","isReverseDir","containerRect","getBoundingClientRect","x","left","right","y","top","bottom","testRange","testRect","verticalSide","horizontalSide","verticalDiff","horizontalDiff","hasVerticalDiff","Math","abs","hasHorizontalDiff"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,KAAP,MAAkB,UAAlB;AACA,OAAOC,cAAP,MAA2B,oBAA3B;AACA,OAAOC,qBAAP,MAAkC,4BAAlC;AACA,OAAOC,kBAAP,MAA+B,wBAA/B;AACA,OAAOC,yBAAP,MAAsC,iCAAtC;AACA,SAASC,eAAT,QAAgC,4BAAhC;AACA,OAAOC,iBAAP,MAA8B,yBAA9B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;A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AAR,GAA2Bd,SAAS,CAAEc,YAAF,CAAzD;AACA,QAAMG,cAAc,GACnBJ,QAAQ,CAAEE,cAAF,CAAR,GAA6BhB,kBAAkB,CAAEgB,cAAF,CADhD,CAhG4E,CAmG5E;;AACA,QAAMG,eAAe,GAAGC,IAAI,CAACC,GAAL,CAAUJ,YAAV,KAA4B,CAApD;AACA,QAAMK,iBAAiB,GAAGF,IAAI,CAACC,GAAL,CAAUH,cAAV,KAA8B,CAAxD;AAEA,SAAOpC,YAAY,GAChBqC,eADgB,GAEhBA,eAAe,IAAIG,iBAFtB;AAGA","sourcesContent":["/**\n * Internal dependencies\n */\nimport isRTL from './is-rtl';\nimport getRangeHeight from './get-range-height';\nimport getRectangleFromRange from './get-rectangle-from-range';\nimport isSelectionForward from './is-selection-forward';\nimport hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';\nimport { assertIsDefined } from '../utils/assert-is-defined';\nimport isInputOrTextArea from './is-input-or-text-area';\n\n/**\n * Check whether the selection is at the edge of the container. Checks for\n * horizontal position by default. Set `onlyVertical` to true to check only\n * vertically.\n *\n * @param {Element} container Focusable element.\n * @param {boolean} isReverse Set to true to check left, false to check right.\n * @param {boolean} [onlyVertical=false] Set to true to check only vertical position.\n *\n * @return {boolean} True if at the edge, false if not.\n */\nexport default function isEdge( container, isReverse, onlyVertical = false ) {\n\tif (\n\t\tisInputOrTextArea( container ) &&\n\t\ttypeof container.selectionStart === 'number'\n\t) {\n\t\tif ( container.selectionStart !== container.selectionEnd ) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif ( isReverse ) {\n\t\t\treturn container.selectionStart === 0;\n\t\t}\n\n\t\treturn container.value.length === container.selectionStart;\n\t}\n\n\tif ( ! (/** @type {HTMLElement} */ ( container ).isContentEditable) ) {\n\t\treturn true;\n\t}\n\n\tconst { ownerDocument } = container;\n\tconst { defaultView } = ownerDocument;\n\n\tassertIsDefined( defaultView, 'defaultView' );\n\tconst selection = defaultView.getSelection();\n\n\tif ( ! selection || ! selection.rangeCount ) {\n\t\treturn false;\n\t}\n\n\tconst range = selection.getRangeAt( 0 );\n\tconst collapsedRange = range.cloneRange();\n\tconst isForward = isSelectionForward( selection );\n\tconst isCollapsed = selection.isCollapsed;\n\n\t// Collapse in direction of selection.\n\tif ( ! isCollapsed ) {\n\t\tcollapsedRange.collapse( ! isForward );\n\t}\n\n\tconst collapsedRangeRect = getRectangleFromRange( collapsedRange );\n\tconst rangeRect = getRectangleFromRange( range );\n\n\tif ( ! collapsedRangeRect || ! rangeRect ) {\n\t\treturn false;\n\t}\n\n\t// Only consider the multiline selection at the edge if the direction is\n\t// towards the edge. The selection is multiline if it is taller than the\n\t// collapsed selection.\n\tconst rangeHeight = getRangeHeight( range );\n\tif (\n\t\t! isCollapsed &&\n\t\trangeHeight &&\n\t\trangeHeight > collapsedRangeRect.height &&\n\t\tisForward === isReverse\n\t) {\n\t\treturn false;\n\t}\n\n\t// In the case of RTL scripts, the horizontal edge is at the opposite side.\n\tconst isReverseDir = isRTL( container ) ? ! isReverse : isReverse;\n\tconst containerRect = container.getBoundingClientRect();\n\n\t// To check if a selection is at the edge, we insert a test selection at the\n\t// edge of the container and check if the selections have the same vertical\n\t// or horizontal position. If they do, the selection is at the edge.\n\t// This method proves to be better than a DOM-based calculation for the\n\t// horizontal edge, since it ignores empty textnodes and a trailing line\n\t// break element. In other words, we need to check visual positioning, not\n\t// DOM positioning.\n\t// It also proves better than using the computed style for the vertical\n\t// edge, because we cannot know the padding and line height reliably in\n\t// pixels. `getComputedStyle` may return a value with different units.\n\tconst x = isReverseDir ? containerRect.left + 1 : containerRect.right - 1;\n\tconst y = isReverse ? containerRect.top + 1 : containerRect.bottom - 1;\n\tconst testRange = hiddenCaretRangeFromPoint(\n\t\townerDocument,\n\t\tx,\n\t\ty,\n\t\t/** @type {HTMLElement} */ ( container )\n\t);\n\n\tif ( ! testRange ) {\n\t\treturn false;\n\t}\n\n\tconst testRect = getRectangleFromRange( testRange );\n\n\tif ( ! testRect ) {\n\t\treturn false;\n\t}\n\n\tconst verticalSide = isReverse ? 'top' : 'bottom';\n\tconst horizontalSide = isReverseDir ? 'left' : 'right';\n\tconst verticalDiff = testRect[ verticalSide ] - rangeRect[ verticalSide ];\n\tconst horizontalDiff =\n\t\ttestRect[ horizontalSide ] - collapsedRangeRect[ horizontalSide ];\n\n\t// Allow the position to be 1px off.\n\tconst hasVerticalDiff = Math.abs( verticalDiff ) <= 1;\n\tconst hasHorizontalDiff = Math.abs( horizontalDiff ) <= 1;\n\n\treturn onlyVertical\n\t\t? hasVerticalDiff\n\t\t: hasVerticalDiff && hasHorizontalDiff;\n}\n"]}
|
@@ -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
|
+
* Gets the range to place.
|
10
|
+
*
|
11
|
+
* @param {HTMLElement} container Focusable element.
|
12
|
+
* @param {boolean} isReverse True for end, false for start.
|
13
|
+
* @param {number|undefined} x X coordinate to vertically position.
|
14
|
+
*
|
15
|
+
* @return {Range|null} The range to place.
|
16
|
+
*/
|
17
|
+
|
18
|
+
function getRange(container, isReverse, x) {
|
19
|
+
const {
|
20
|
+
ownerDocument
|
21
|
+
} = container; // In the case of RTL scripts, the horizontal edge is at the opposite side.
|
22
|
+
|
23
|
+
const isReverseDir = isRTL(container) ? !isReverse : isReverse;
|
24
|
+
const containerRect = container.getBoundingClientRect(); // When placing at the end (isReverse), find the closest range to the bottom
|
25
|
+
// right corner. When placing at the start, to the top left corner.
|
26
|
+
|
27
|
+
if (x === undefined) {
|
28
|
+
x = isReverse ? containerRect.right - 1 : containerRect.left + 1;
|
29
|
+
}
|
30
|
+
|
31
|
+
const y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;
|
32
|
+
return hiddenCaretRangeFromPoint(ownerDocument, x, y, container);
|
33
|
+
}
|
34
|
+
/**
|
35
|
+
* Places the caret at start or end of a given element.
|
36
|
+
*
|
37
|
+
* @param {HTMLElement} container Focusable element.
|
38
|
+
* @param {boolean} isReverse True for end, false for start.
|
39
|
+
* @param {number|undefined} x X coordinate to vertically position.
|
40
|
+
*/
|
41
|
+
|
42
|
+
|
43
|
+
export default function placeCaretAtEdge(container, isReverse, x) {
|
44
|
+
if (!container) {
|
45
|
+
return;
|
46
|
+
}
|
47
|
+
|
48
|
+
container.focus();
|
49
|
+
|
50
|
+
if (isInputOrTextArea(container)) {
|
51
|
+
// The element may not support selection setting.
|
52
|
+
if (typeof container.selectionStart !== 'number') {
|
53
|
+
return;
|
54
|
+
}
|
55
|
+
|
56
|
+
if (isReverse) {
|
57
|
+
container.selectionStart = container.value.length;
|
58
|
+
container.selectionEnd = container.value.length;
|
59
|
+
} else {
|
60
|
+
container.selectionStart = 0;
|
61
|
+
container.selectionEnd = 0;
|
62
|
+
}
|
63
|
+
|
64
|
+
return;
|
65
|
+
}
|
66
|
+
|
67
|
+
if (!container.isContentEditable) {
|
68
|
+
return;
|
69
|
+
}
|
70
|
+
|
71
|
+
let range = getRange(container, isReverse, x); // If no range range can be created or it is outside the container, the
|
72
|
+
// element may be out of view.
|
73
|
+
|
74
|
+
if (!range || !range.startContainer || !container.contains(range.startContainer)) {
|
75
|
+
container.scrollIntoView(isReverse);
|
76
|
+
range = range = getRange(container, isReverse, x);
|
77
|
+
|
78
|
+
if (!range || !range.startContainer || !container.contains(range.startContainer)) {
|
79
|
+
return;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
const {
|
84
|
+
ownerDocument
|
85
|
+
} = container;
|
86
|
+
const {
|
87
|
+
defaultView
|
88
|
+
} = ownerDocument;
|
89
|
+
assertIsDefined(defaultView, 'defaultView');
|
90
|
+
const selection = defaultView.getSelection();
|
91
|
+
assertIsDefined(selection, 'selection');
|
92
|
+
selection.removeAllRanges();
|
93
|
+
selection.addRange(range);
|
94
|
+
}
|
95
|
+
//# sourceMappingURL=place-caret-at-edge.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["@wordpress/dom/src/dom/place-caret-at-edge.js"],"names":["hiddenCaretRangeFromPoint","assertIsDefined","isInputOrTextArea","isRTL","getRange","container","isReverse","x","ownerDocument","isReverseDir","containerRect","getBoundingClientRect","undefined","right","left","y","bottom","top","placeCaretAtEdge","focus","selectionStart","value","length","selectionEnd","isContentEditable","range","startContainer","contains","scrollIntoView","defaultView","selection","getSelection","removeAllRanges","addRange"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,yBAAP,MAAsC,iCAAtC;AACA,SAASC,eAAT,QAAgC,4BAAhC;AACA,OAAOC,iBAAP,MAA8B,yBAA9B;AACA,OAAOC,KAAP,MAAkB,UAAlB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,QAAT,CAAmBC,SAAnB,EAA8BC,SAA9B,EAAyCC,CAAzC,EAA6C;AAC5C,QAAM;AAAEC,IAAAA;AAAF,MAAoBH,SAA1B,CAD4C,CAE5C;;AACA,QAAMI,YAAY,GAAGN,KAAK,CAAEE,SAAF,CAAL,GAAqB,CAAEC,SAAvB,GAAmCA,SAAxD;AACA,QAAMI,aAAa,GAAGL,SAAS,CAACM,qBAAV,EAAtB,CAJ4C,CAK5C;AACA;;AACA,MAAKJ,CAAC,KAAKK,SAAX,EAAuB;AACtBL,IAAAA,CAAC,GAAGD,SAAS,GAAGI,aAAa,CAACG,KAAd,GAAsB,CAAzB,GAA6BH,aAAa,CAACI,IAAd,GAAqB,CAA/D;AACA;;AACD,QAAMC,CAAC,GAAGN,YAAY,GAAGC,aAAa,CAACM,MAAd,GAAuB,CAA1B,GAA8BN,aAAa,CAACO,GAAd,GAAoB,CAAxE;AACA,SAAOjB,yBAAyB,CAAEQ,aAAF,EAAiBD,CAAjB,EAAoBQ,CA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ACA;AACD;;AAED,QAAM;AAAElB,IAAAA;AAAF,MAAoBH,SAA1B;AACA,QAAM;AAAEwB,IAAAA;AAAF,MAAkBrB,aAAxB;AACAP,EAAAA,eAAe,CAAE4B,WAAF,EAAe,aAAf,CAAf;AACA,QAAMC,SAAS,GAAGD,WAAW,CAACE,YAAZ,EAAlB;AACA9B,EAAAA,eAAe,CAAE6B,SAAF,EAAa,WAAb,CAAf;AACAA,EAAAA,SAAS,CAACE,eAAV;AACAF,EAAAA,SAAS,CAACG,QAAV,CAAoBR,KAApB;AACA","sourcesContent":["/**\n * Internal dependencies\n */\nimport hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';\nimport { assertIsDefined } from '../utils/assert-is-defined';\nimport isInputOrTextArea from './is-input-or-text-area';\nimport isRTL from './is-rtl';\n\n/**\n * Gets the range to place.\n *\n * @param {HTMLElement} container Focusable element.\n * @param {boolean} isReverse True for end, false for start.\n * @param {number|undefined} x X coordinate to vertically position.\n *\n * @return {Range|null} The range to place.\n */\nfunction getRange( container, isReverse, x ) {\n\tconst { ownerDocument } = container;\n\t// In the case of RTL scripts, the horizontal edge is at the opposite side.\n\tconst isReverseDir = isRTL( container ) ? ! isReverse : isReverse;\n\tconst containerRect = container.getBoundingClientRect();\n\t// When placing at the end (isReverse), find the closest range to the bottom\n\t// right corner. When placing at the start, to the top left corner.\n\tif ( x === undefined ) {\n\t\tx = isReverse ? containerRect.right - 1 : containerRect.left + 1;\n\t}\n\tconst y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;\n\treturn hiddenCaretRangeFromPoint( ownerDocument, x, y, container );\n}\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 * @param {number|undefined} x X coordinate to vertically position.\n */\nexport default function placeCaretAtEdge( container, isReverse, x ) {\n\tif ( ! container ) {\n\t\treturn;\n\t}\n\n\tcontainer.focus();\n\n\tif ( isInputOrTextArea( container ) ) {\n\t\t// The element may not support selection setting.\n\t\tif ( typeof container.selectionStart !== 'number' ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( isReverse ) {\n\t\t\tcontainer.selectionStart = container.value.length;\n\t\t\tcontainer.selectionEnd = container.value.length;\n\t\t} else {\n\t\t\tcontainer.selectionStart = 0;\n\t\t\tcontainer.selectionEnd = 0;\n\t\t}\n\n\t\treturn;\n\t}\n\n\tif ( ! container.isContentEditable ) {\n\t\treturn;\n\t}\n\n\tlet range = getRange( container, isReverse, x );\n\n\t// If no range range can be created or it is outside the container, the\n\t// element may be out of view.\n\tif (\n\t\t! range ||\n\t\t! range.startContainer ||\n\t\t! container.contains( range.startContainer )\n\t) {\n\t\tcontainer.scrollIntoView( isReverse );\n\t\trange = range = getRange( container, isReverse, x );\n\n\t\tif (\n\t\t\t! range ||\n\t\t\t! range.startContainer ||\n\t\t\t! container.contains( range.startContainer )\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t}\n\n\tconst { ownerDocument } = container;\n\tconst { defaultView } = ownerDocument;\n\tassertIsDefined( defaultView, 'defaultView' );\n\tconst selection = defaultView.getSelection();\n\tassertIsDefined( selection, 'selection' );\n\tselection.removeAllRanges();\n\tselection.addRange( range );\n}\n"]}
|