@wordpress/block-editor
Version:
38 lines (37 loc) • 1.2 kB
JavaScript
// packages/block-editor/src/components/block-list/use-block-props/use-focus-handler.js
import { useSelect, useDispatch } from "@wordpress/data";
import { useRefEffect } from "@wordpress/compose";
import { isInsideRootBlock } from "../../../utils/dom";
import { store as blockEditorStore } from "../../../store";
function useFocusHandler(clientId) {
const { isBlockSelected } = useSelect(blockEditorStore);
const { selectBlock, selectionChange } = useDispatch(blockEditorStore);
return useRefEffect(
(node) => {
function onFocus(event) {
if (node.parentElement.closest('[contenteditable="true"]')) {
return;
}
if (isBlockSelected(clientId)) {
if (!event.target.isContentEditable) {
selectionChange(clientId);
}
return;
}
if (!isInsideRootBlock(node, event.target)) {
return;
}
selectBlock(clientId);
}
node.addEventListener("focusin", onFocus);
return () => {
node.removeEventListener("focusin", onFocus);
};
},
[isBlockSelected, selectBlock]
);
}
export {
useFocusHandler
};
//# sourceMappingURL=use-focus-handler.js.map