@wordpress/block-editor
Version:
246 lines (215 loc) • 7.08 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.InsertionPointOpenRef = void 0;
exports.default = InsertionPoint;
var _element = require("@wordpress/element");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _classnames = _interopRequireDefault(require("classnames"));
var _data = require("@wordpress/data");
var _components = require("@wordpress/components");
var _compose = require("@wordpress/compose");
var _inserter = _interopRequireDefault(require("../inserter"));
var _store = require("../../store");
var _inbetween = _interopRequireDefault(require("../block-popover/inbetween"));
var _dropZone = _interopRequireDefault(require("../block-popover/drop-zone"));
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const InsertionPointOpenRef = (0, _element.createContext)();
exports.InsertionPointOpenRef = InsertionPointOpenRef;
function InbetweenInsertionPointPopover({
__unstablePopoverSlot,
__unstableContentRef
}) {
const {
selectBlock,
hideInsertionPoint
} = (0, _data.useDispatch)(_store.store);
const openRef = (0, _element.useContext)(InsertionPointOpenRef);
const ref = (0, _element.useRef)();
const {
orientation,
previousClientId,
nextClientId,
rootClientId,
isInserterShown,
isDistractionFree,
isNavigationMode
} = (0, _data.useSelect)(select => {
const {
getBlockOrder,
getBlockListSettings,
getBlockInsertionPoint,
isBlockBeingDragged,
getPreviousBlockClientId,
getNextBlockClientId,
getSettings,
isNavigationMode: _isNavigationMode
} = select(_store.store);
const insertionPoint = getBlockInsertionPoint();
const order = getBlockOrder(insertionPoint.rootClientId);
if (!order.length) {
return {};
}
let _previousClientId = order[insertionPoint.index - 1];
let _nextClientId = order[insertionPoint.index];
while (isBlockBeingDragged(_previousClientId)) {
_previousClientId = getPreviousBlockClientId(_previousClientId);
}
while (isBlockBeingDragged(_nextClientId)) {
_nextClientId = getNextBlockClientId(_nextClientId);
}
const settings = getSettings();
return {
previousClientId: _previousClientId,
nextClientId: _nextClientId,
orientation: getBlockListSettings(insertionPoint.rootClientId)?.orientation || 'vertical',
rootClientId: insertionPoint.rootClientId,
isNavigationMode: _isNavigationMode(),
isDistractionFree: settings.isDistractionFree,
isInserterShown: insertionPoint?.__unstableWithInserter
};
}, []);
const disableMotion = (0, _compose.useReducedMotion)();
function onClick(event) {
if (event.target === ref.current && nextClientId) {
selectBlock(nextClientId, -1);
}
}
function maybeHideInserterPoint(event) {
// Only hide the inserter if it's triggered on the wrapper,
// and the inserter is not open.
if (event.target === ref.current && !openRef.current) {
hideInsertionPoint();
}
}
function onFocus(event) {
// Only handle click on the wrapper specifically, and not an event
// bubbled from the inserter itself.
if (event.target !== ref.current) {
openRef.current = true;
}
}
const lineVariants = {
// Initial position starts from the center and invisible.
start: {
opacity: 0,
scale: 0
},
// The line expands to fill the container. If the inserter is visible it
// is delayed so it appears orchestrated.
rest: {
opacity: 1,
scale: 1,
transition: {
delay: isInserterShown ? 0.5 : 0,
type: 'tween'
}
},
hover: {
opacity: 1,
scale: 1,
transition: {
delay: 0.5,
type: 'tween'
}
}
};
const inserterVariants = {
start: {
scale: disableMotion ? 1 : 0
},
rest: {
scale: 1,
transition: {
delay: 0.4,
type: 'tween'
}
}
};
if (isDistractionFree && !isNavigationMode) {
return null;
}
const className = (0, _classnames.default)('block-editor-block-list__insertion-point', 'is-' + orientation);
return (0, _element.createElement)(_inbetween.default, {
previousClientId: previousClientId,
nextClientId: nextClientId,
__unstablePopoverSlot: __unstablePopoverSlot,
__unstableContentRef: __unstableContentRef
}, (0, _element.createElement)(_components.__unstableMotion.div, {
layout: !disableMotion,
initial: disableMotion ? 'rest' : 'start',
animate: "rest",
whileHover: "hover",
whileTap: "pressed",
exit: "start",
ref: ref,
tabIndex: -1,
onClick: onClick,
onFocus: onFocus,
className: (0, _classnames.default)(className, {
'is-with-inserter': isInserterShown
}),
onHoverEnd: maybeHideInserterPoint
}, (0, _element.createElement)(_components.__unstableMotion.div, {
variants: lineVariants,
className: "block-editor-block-list__insertion-point-indicator",
"data-testid": "block-list-insertion-point-indicator"
}), isInserterShown && (0, _element.createElement)(_components.__unstableMotion.div, {
variants: inserterVariants,
className: (0, _classnames.default)('block-editor-block-list__insertion-point-inserter')
}, (0, _element.createElement)(_inserter.default, {
position: "bottom center",
clientId: nextClientId,
rootClientId: rootClientId,
__experimentalIsQuick: true,
onToggle: isOpen => {
openRef.current = isOpen;
},
onSelectOrClose: () => {
openRef.current = false;
}
}))));
}
function InsertionPoint(props) {
const {
insertionPoint,
isVisible,
isBlockListEmpty
} = (0, _data.useSelect)(select => {
const {
getBlockInsertionPoint,
isBlockInsertionPointVisible,
getBlockCount
} = select(_store.store);
const blockInsertionPoint = getBlockInsertionPoint();
return {
insertionPoint: blockInsertionPoint,
isVisible: isBlockInsertionPointVisible(),
isBlockListEmpty: getBlockCount(blockInsertionPoint?.rootClientId) === 0
};
}, []);
if (!isVisible || // Don't render the insertion point if the block list is empty.
// The insertion point will be represented by the appender instead.
isBlockListEmpty) {
return null;
}
/**
* Render a popover that overlays the block when the desired operation is to replace it.
* Otherwise, render a popover in between blocks for the indication of inserting between them.
*/
return insertionPoint.operation === 'replace' ? (0, _element.createElement)(_dropZone.default // Force remount to trigger the animation.
, (0, _extends2.default)({
key: `${insertionPoint.rootClientId}-${insertionPoint.index}`
}, props)) : (0, _element.createElement)(InbetweenInsertionPointPopover, props);
}
//# sourceMappingURL=insertion-point.js.map