@gechiui/block-editor
Version:
134 lines (125 loc) • 3.69 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { createElement, Fragment } from "@gechiui/element";
/**
* External dependencies
*/
import classNames from 'classnames';
/**
* GeChiUI dependencies
*/
import { __ } from '@gechiui/i18n';
import { ToolbarDropdownMenu, ToolbarGroup, MenuGroup, MenuItem } from '@gechiui/components';
import { alignNone, positionCenter, positionLeft, positionRight, stretchFullWidth, stretchWide } from '@gechiui/icons';
import { Platform } from '@gechiui/element';
/**
* Internal dependencies
*/
import useAvailableAlignments from './use-available-alignments';
const BLOCK_ALIGNMENTS_CONTROLS = {
none: {
icon: alignNone,
title: __('无')
},
left: {
icon: positionLeft,
title: __('左对齐')
},
center: {
icon: positionCenter,
title: __('居中对齐')
},
right: {
icon: positionRight,
title: __('右对齐')
},
wide: {
icon: stretchWide,
title: __('宽幅')
},
full: {
icon: stretchFullWidth,
title: __('全幅')
}
};
const DEFAULT_CONTROL = 'none';
const POPOVER_PROPS = {
isAlternate: true
};
function BlockAlignmentUI(_ref) {
let {
value,
onChange,
controls,
isToolbar,
isCollapsed = true
} = _ref;
const enabledControls = useAvailableAlignments(controls);
const hasEnabledControls = !!enabledControls.length;
if (!hasEnabledControls) {
return null;
}
function onChangeAlignment(align) {
onChange([value, 'none'].includes(align) ? undefined : align);
}
const activeAlignmentControl = BLOCK_ALIGNMENTS_CONTROLS[value];
const defaultAlignmentControl = BLOCK_ALIGNMENTS_CONTROLS[DEFAULT_CONTROL];
const UIComponent = isToolbar ? ToolbarGroup : ToolbarDropdownMenu;
const commonProps = {
popoverProps: POPOVER_PROPS,
icon: activeAlignmentControl ? activeAlignmentControl.icon : defaultAlignmentControl.icon,
label: __('对齐'),
toggleProps: {
describedBy: __('修改对齐方式')
}
};
const extraProps = isToolbar || Platform.isNative ? {
isCollapsed: isToolbar ? isCollapsed : undefined,
controls: enabledControls.map(_ref2 => {
let {
name: controlName
} = _ref2;
return { ...BLOCK_ALIGNMENTS_CONTROLS[controlName],
isActive: value === controlName || !value && controlName === 'none',
role: isCollapsed ? 'menuitemradio' : undefined,
onClick: () => onChangeAlignment(controlName)
};
})
} : {
children: _ref3 => {
let {
onClose
} = _ref3;
return createElement(Fragment, null, createElement(MenuGroup, {
className: "block-editor-block-alignment-control__menu-group"
}, enabledControls.map(_ref4 => {
let {
name: controlName,
info
} = _ref4;
const {
icon,
title
} = BLOCK_ALIGNMENTS_CONTROLS[controlName]; // If no value is provided, mark as selected the `none` option.
const isSelected = controlName === value || !value && controlName === 'none';
return createElement(MenuItem, {
key: controlName,
icon: icon,
iconPosition: "left",
className: classNames('components-dropdown-menu__menu-item', {
'is-active': isSelected
}),
isSelected: isSelected,
onClick: () => {
onChangeAlignment(controlName);
onClose();
},
role: "menuitemradio",
info: info
}, title);
})));
}
};
return createElement(UIComponent, _extends({}, commonProps, extraProps));
}
export default BlockAlignmentUI;
//# sourceMappingURL=ui.js.map