@wordpress/block-editor
Version:
167 lines (165 loc) • 5.96 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-editor/src/components/writing-flow/use-input.js
var use_input_exports = {};
__export(use_input_exports, {
default: () => useInput
});
module.exports = __toCommonJS(use_input_exports);
var import_data = require("@wordpress/data");
var import_compose = require("@wordpress/compose");
var import_keycodes = require("@wordpress/keycodes");
var import_blocks = require("@wordpress/blocks");
var import_store = require("../../store");
function useInput() {
const {
__unstableIsFullySelected,
getSelectedBlockClientIds,
getSelectedBlockClientId,
__unstableIsSelectionMergeable,
hasMultiSelection,
getBlockName,
canInsertBlockType,
getBlockRootClientId,
getSelectionStart,
getSelectionEnd,
getBlockAttributes
} = (0, import_data.useSelect)(import_store.store);
const {
replaceBlocks,
__unstableSplitSelection,
removeBlocks,
__unstableDeleteSelection,
__unstableExpandSelection,
__unstableMarkAutomaticChange
} = (0, import_data.useDispatch)(import_store.store);
return (0, import_compose.useRefEffect)((node) => {
function onBeforeInput(event) {
if (node.contentEditable === "true") {
event.preventDefault();
}
}
function onKeyDown(event) {
if (event.defaultPrevented) {
return;
}
if (!hasMultiSelection()) {
if (event.keyCode === import_keycodes.ENTER) {
if (event.shiftKey || __unstableIsFullySelected()) {
return;
}
const clientId = getSelectedBlockClientId();
const blockName = getBlockName(clientId);
const selectionStart = getSelectionStart();
const selectionEnd = getSelectionEnd();
if (selectionStart.attributeKey === selectionEnd.attributeKey) {
const selectedAttributeValue = getBlockAttributes(clientId)[selectionStart.attributeKey];
const transforms = (0, import_blocks.getBlockTransforms)("from").filter(
({ type }) => type === "enter"
);
const transformation = (0, import_blocks.findTransform)(
transforms,
(item) => {
return item.regExp.test(
selectedAttributeValue
);
}
);
if (transformation) {
replaceBlocks(
clientId,
transformation.transform({
content: selectedAttributeValue
})
);
__unstableMarkAutomaticChange();
return;
}
}
if (!(0, import_blocks.hasBlockSupport)(blockName, "splitting", false) && !event.__deprecatedOnSplit) {
return;
}
if (canInsertBlockType(
blockName,
getBlockRootClientId(clientId)
)) {
__unstableSplitSelection();
event.preventDefault();
}
}
return;
}
if (event.keyCode === import_keycodes.ENTER) {
node.contentEditable = false;
event.preventDefault();
if (__unstableIsFullySelected()) {
replaceBlocks(
getSelectedBlockClientIds(),
(0, import_blocks.createBlock)((0, import_blocks.getDefaultBlockName)())
);
} else {
__unstableSplitSelection();
}
} else if (event.keyCode === import_keycodes.BACKSPACE || event.keyCode === import_keycodes.DELETE) {
node.contentEditable = false;
event.preventDefault();
if (__unstableIsFullySelected()) {
removeBlocks(getSelectedBlockClientIds());
} else if (__unstableIsSelectionMergeable()) {
__unstableDeleteSelection(event.keyCode === import_keycodes.DELETE);
} else {
__unstableExpandSelection();
}
} else if (
// If key.length is longer than 1, it's a control key that doesn't
// input anything.
event.key.length === 1 && !(event.metaKey || event.ctrlKey)
) {
node.contentEditable = false;
if (__unstableIsSelectionMergeable()) {
__unstableDeleteSelection(event.keyCode === import_keycodes.DELETE);
} else {
event.preventDefault();
node.ownerDocument.defaultView.getSelection().removeAllRanges();
}
}
}
function onCompositionStart(event) {
if (!hasMultiSelection()) {
return;
}
node.contentEditable = false;
if (__unstableIsSelectionMergeable()) {
__unstableDeleteSelection();
} else {
event.preventDefault();
node.ownerDocument.defaultView.getSelection().removeAllRanges();
}
}
node.addEventListener("beforeinput", onBeforeInput);
node.addEventListener("keydown", onKeyDown);
node.addEventListener("compositionstart", onCompositionStart);
return () => {
node.removeEventListener("beforeinput", onBeforeInput);
node.removeEventListener("keydown", onKeyDown);
node.removeEventListener("compositionstart", onCompositionStart);
};
}, []);
}
//# sourceMappingURL=use-input.js.map