@wordpress/block-editor
Version:
61 lines (58 loc) • 2.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useClickSelection;
var _data = require("@wordpress/data");
var _compose = require("@wordpress/compose");
var _store = require("../../store");
var _dom = require("../../utils/dom");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
function useClickSelection() {
const {
selectBlock
} = (0, _data.useDispatch)(_store.store);
const {
isSelectionEnabled,
getBlockSelectionStart,
hasMultiSelection
} = (0, _data.useSelect)(_store.store);
return (0, _compose.useRefEffect)(node => {
function onMouseDown(event) {
// The main button.
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
if (!isSelectionEnabled() || event.button !== 0) {
return;
}
const startClientId = getBlockSelectionStart();
const clickedClientId = (0, _dom.getBlockClientId)(event.target);
if (event.shiftKey) {
// When selecting a single block in a document by holding the shift key,
// don't mark this action as multiselection.
if (startClientId && startClientId !== clickedClientId) {
node.contentEditable = true;
// Firefox doesn't automatically move focus.
node.focus();
}
} else if (hasMultiSelection()) {
// Allow user to escape out of a multi-selection to a
// singular selection of a block via click. This is handled
// here since focus handling excludes blocks when there is
// multiselection, as focus can be incurred by starting a
// multiselection (focus moved to first block's multi-
// controls).
selectBlock(clickedClientId);
}
}
node.addEventListener('mousedown', onMouseDown);
return () => {
node.removeEventListener('mousedown', onMouseDown);
};
}, [selectBlock, isSelectionEnabled, getBlockSelectionStart, hasMultiSelection]);
}
//# sourceMappingURL=use-click-selection.js.map