@wordpress/block-library
Version:
Block library for the WordPress editor.
45 lines (44 loc) • 1.63 kB
JavaScript
// packages/block-library/src/list-item/hooks/use-tab.js
import { useRefEffect } from "@wordpress/compose";
import { privateApis as richTextPrivateApis } from "@wordpress/rich-text";
import { SPACE, TAB } from "@wordpress/keycodes";
import { store as blockEditorStore } from "@wordpress/block-editor";
import { useRegistry } from "@wordpress/data";
import { isEntirelySelected } from "@wordpress/dom";
import { indentListItems, outdentListItems } from "../utils.mjs";
import { unlock } from "../../lock-unlock.mjs";
var { subscribeOwnedListener } = unlock(richTextPrivateApis);
function useTab() {
const registry = useRegistry();
return useRefEffect(
(element) => {
function onKeyDown(event) {
const { keyCode, shiftKey, altKey, metaKey, ctrlKey } = event;
if (event.defaultPrevented || keyCode !== SPACE && keyCode !== TAB || // Shift selects outdent; other modifiers pass through.
altKey || metaKey || ctrlKey) {
return;
}
const { getSelectionStart, getSelectionEnd } = registry.select(blockEditorStore);
const isAtStart = getSelectionStart().offset === 0 && getSelectionEnd().offset === 0;
if (!isAtStart && !(keyCode === TAB && isEntirelySelected(element))) {
return;
}
const move = shiftKey ? outdentListItems : indentListItems;
if (move(registry)) {
event.preventDefault();
}
}
return subscribeOwnedListener(
element,
"keydown",
onKeyDown,
true
);
},
[registry]
);
}
export {
useTab as default
};
//# sourceMappingURL=use-tab.mjs.map