@gechiui/block-editor
Version:
306 lines (274 loc) • 10.2 kB
JavaScript
import { createElement } from "@gechiui/element";
/**
* External dependencies
*/
import { find } from 'lodash';
import classnames from 'classnames';
/**
* GeChiUI dependencies
*/
import { useState, useRef, useEffect } from '@gechiui/element';
import { isUnmodifiedDefaultBlock } from '@gechiui/blocks';
import { Popover } from '@gechiui/components';
import { useDispatch, useSelect } from '@gechiui/data';
import { useShortcut } from '@gechiui/keyboard-shortcuts';
import { useViewportMatch } from '@gechiui/compose';
import { getScrollContainer } from '@gechiui/dom';
/**
* Internal dependencies
*/
import BlockSelectionButton from './block-selection-button';
import BlockContextualToolbar from './block-contextual-toolbar';
import Inserter from '../inserter';
import { store as blockEditorStore } from '../../store';
import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs';
import { usePopoverScroll } from './use-popover-scroll';
function selector(select) {
const {
isNavigationMode,
isMultiSelecting,
hasMultiSelection,
isTyping,
isCaretWithinFormattedText,
getSettings,
getLastMultiSelectedBlockClientId
} = select(blockEditorStore);
return {
isNavigationMode: isNavigationMode(),
isMultiSelecting: isMultiSelecting(),
isTyping: isTyping(),
isCaretWithinFormattedText: isCaretWithinFormattedText(),
hasMultiSelection: hasMultiSelection(),
hasFixedToolbar: getSettings().hasFixedToolbar,
lastClientId: getLastMultiSelectedBlockClientId()
};
}
function BlockPopover(_ref) {
let {
clientId,
rootClientId,
isValid,
isEmptyDefaultBlock,
capturingClientId,
__unstablePopoverSlot,
__unstableContentRef
} = _ref;
const {
isNavigationMode,
isMultiSelecting,
isTyping,
isCaretWithinFormattedText,
hasMultiSelection,
hasFixedToolbar,
lastClientId
} = useSelect(selector, []);
const isInsertionPointVisible = useSelect(select => {
const {
isBlockInsertionPointVisible,
getBlockInsertionPoint,
getBlockOrder
} = select(blockEditorStore);
if (!isBlockInsertionPointVisible()) {
return false;
}
const insertionPoint = getBlockInsertionPoint();
const order = getBlockOrder(insertionPoint.rootClientId);
return order[insertionPoint.index] === clientId;
}, [clientId]);
const isLargeViewport = useViewportMatch('medium');
const [isToolbarForced, setIsToolbarForced] = useState(false);
const [isInserterShown, setIsInserterShown] = useState(false);
const {
stopTyping
} = useDispatch(blockEditorStore); // Controls when the side inserter on empty lines should
// be shown, including writing and selection modes.
const showEmptyBlockSideInserter = !isTyping && !isNavigationMode && isEmptyDefaultBlock && isValid;
const shouldShowBreadcrumb = isNavigationMode;
const shouldShowContextualToolbar = !isNavigationMode && !hasFixedToolbar && isLargeViewport && !showEmptyBlockSideInserter && !isMultiSelecting && (!isTyping || isCaretWithinFormattedText);
const canFocusHiddenToolbar = !isNavigationMode && !shouldShowContextualToolbar && !hasFixedToolbar && !isEmptyDefaultBlock;
useShortcut('core/block-editor/focus-toolbar', () => {
setIsToolbarForced(true);
stopTyping(true);
}, {
isDisabled: !canFocusHiddenToolbar
});
useEffect(() => {
if (!shouldShowContextualToolbar) {
setIsToolbarForced(false);
}
}, [shouldShowContextualToolbar]); // Stores the active toolbar item index so the block toolbar can return focus
// to it when re-mounting.
const initialToolbarItemIndexRef = useRef();
const selectedElement = useBlockElement(clientId);
const lastSelectedElement = useBlockElement(lastClientId);
const capturingElement = useBlockElement(capturingClientId);
const popoverScrollRef = usePopoverScroll(__unstableContentRef);
if (!shouldShowBreadcrumb && !shouldShowContextualToolbar && !isToolbarForced && !showEmptyBlockSideInserter) {
return null;
}
let node = selectedElement;
if (!node) {
return null;
}
if (capturingClientId) {
node = capturingElement;
}
let anchorRef = node;
if (hasMultiSelection) {
// Wait to render the popover until the bottom reference is available
// as well.
if (!lastSelectedElement) {
return null;
}
anchorRef = {
top: node,
bottom: lastSelectedElement
};
}
function onFocus() {
setIsInserterShown(true);
}
function onBlur() {
setIsInserterShown(false);
} // Position above the anchor, pop out towards the right, and position in the
// left corner. For the side inserter, pop out towards the left, and
// position in the right corner.
// To do: refactor `Popover` to make this prop clearer.
const popoverPosition = showEmptyBlockSideInserter ? 'top left right' : 'top right left';
const {
ownerDocument
} = node;
const stickyBoundaryElement = showEmptyBlockSideInserter ? undefined : // The sticky boundary element should be the boundary at which the
// the block toolbar becomes sticky when the block scolls out of view.
// In case of an iframe, this should be the iframe boundary, otherwise
// the scroll container.
ownerDocument.defaultView.frameElement || getScrollContainer(node) || ownerDocument.body;
return createElement(Popover, {
ref: popoverScrollRef,
noArrow: true,
animate: false,
position: popoverPosition,
focusOnMount: false,
anchorRef: anchorRef,
className: classnames('block-editor-block-list__block-popover', {
'is-insertion-point-visible': isInsertionPointVisible
}),
__unstableStickyBoundaryElement: stickyBoundaryElement // Render in the old slot if needed for backward compatibility,
// otherwise render in place (not in the the default popover slot).
,
__unstableSlotName: __unstablePopoverSlot || null,
__unstableBoundaryParent: true // Observe movement for block animations (especially horizontal).
,
__unstableObserveElement: node,
shouldAnchorIncludePadding: true // Used to safeguard sticky position behavior against cases where it would permanently
// obscure specific sections of a block.
,
__unstableEditorCanvasWrapper: __unstableContentRef === null || __unstableContentRef === void 0 ? void 0 : __unstableContentRef.current
}, (shouldShowContextualToolbar || isToolbarForced) && createElement("div", {
onFocus: onFocus,
onBlur: onBlur // While ideally it would be enough to capture the
// bubbling focus event from the Inserter, due to the
// characteristics of click focusing of `button`s in
// Firefox and Safari, it is not reliable.
//
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
,
tabIndex: -1,
className: classnames('block-editor-block-list__block-popover-inserter', {
'is-visible': isInserterShown
})
}, createElement(Inserter, {
clientId: clientId,
rootClientId: rootClientId,
__experimentalIsQuick: true
})), (shouldShowContextualToolbar || isToolbarForced) && createElement(BlockContextualToolbar // If the toolbar is being shown because of being forced
// it should focus the toolbar right after the mount.
, {
focusOnMount: isToolbarForced,
__experimentalInitialIndex: initialToolbarItemIndexRef.current,
__experimentalOnIndexChange: index => {
initialToolbarItemIndexRef.current = index;
} // Resets the index whenever the active block changes so
// this is not persisted. See https://github.com/GeChiUI/gutenberg/pull/25760#issuecomment-717906169
,
key: clientId
}), shouldShowBreadcrumb && createElement(BlockSelectionButton, {
clientId: clientId,
rootClientId: rootClientId,
blockElement: node
}), showEmptyBlockSideInserter && createElement("div", {
className: "block-editor-block-list__empty-block-inserter"
}, createElement(Inserter, {
position: "bottom right",
rootClientId: rootClientId,
clientId: clientId,
__experimentalIsQuick: true
})));
}
function wrapperSelector(select) {
const {
getSelectedBlockClientId,
getFirstMultiSelectedBlockClientId,
getBlockRootClientId,
getBlock,
getBlockParents,
__experimentalGetBlockListSettingsForBlocks
} = select(blockEditorStore);
const clientId = getSelectedBlockClientId() || getFirstMultiSelectedBlockClientId();
if (!clientId) {
return;
}
const {
name,
attributes = {},
isValid
} = getBlock(clientId) || {};
const blockParentsClientIds = getBlockParents(clientId); // Get Block List Settings for all ancestors of the current Block clientId
const parentBlockListSettings = __experimentalGetBlockListSettingsForBlocks(blockParentsClientIds); // Get the clientId of the topmost parent with the capture toolbars setting.
const capturingClientId = find(blockParentsClientIds, parentClientId => {
var _parentBlockListSetti;
return (_parentBlockListSetti = parentBlockListSettings[parentClientId]) === null || _parentBlockListSetti === void 0 ? void 0 : _parentBlockListSetti.__experimentalCaptureToolbars;
});
return {
clientId,
rootClientId: getBlockRootClientId(clientId),
name,
isValid,
isEmptyDefaultBlock: name && isUnmodifiedDefaultBlock({
name,
attributes
}),
capturingClientId
};
}
export default function WrappedBlockPopover(_ref2) {
let {
__unstablePopoverSlot,
__unstableContentRef
} = _ref2;
const selected = useSelect(wrapperSelector, []);
if (!selected) {
return null;
}
const {
clientId,
rootClientId,
name,
isValid,
isEmptyDefaultBlock,
capturingClientId
} = selected;
if (!name) {
return null;
}
return createElement(BlockPopover, {
clientId: clientId,
rootClientId: rootClientId,
isValid: isValid,
isEmptyDefaultBlock: isEmptyDefaultBlock,
capturingClientId: capturingClientId,
__unstablePopoverSlot: __unstablePopoverSlot,
__unstableContentRef: __unstableContentRef
});
}
//# sourceMappingURL=block-popover.js.map