@gechiui/block-editor
Version:
66 lines (51 loc) • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useCanvasClickRedirect = useCanvasClickRedirect;
var _lodash = require("lodash");
var _compose = require("@gechiui/compose");
var _dom = require("@gechiui/dom");
/**
* External dependencies
*/
/**
* GeChiUI dependencies
*/
/**
* Given an element, returns true if the element is a tabbable text field, or
* false otherwise.
*
* @param {Element} element Element to test.
*
* @return {boolean} Whether element is a tabbable text field.
*/
const isTabbableTextField = (0, _lodash.overEvery)([_dom.isTextField, _dom.focus.tabbable.isTabbableIndex]);
function useCanvasClickRedirect() {
return (0, _compose.useRefEffect)(node => {
function onMouseDown(event) {
// Only handle clicks on the canvas, not the content.
if (event.target !== node) {
return;
}
const focusableNodes = _dom.focus.focusable.find(node);
const target = (0, _lodash.findLast)(focusableNodes, isTabbableTextField);
if (!target) {
return;
}
const {
bottom
} = target.getBoundingClientRect(); // Ensure the click is below the last block.
if (event.clientY < bottom) {
return;
}
(0, _dom.placeCaretAtHorizontalEdge)(target, true);
event.preventDefault();
}
node.addEventListener('mousedown', onMouseDown);
return () => {
node.addEventListener('mousedown', onMouseDown);
};
}, []);
}
//# sourceMappingURL=index.js.map