@gechiui/block-editor
Version:
149 lines (131 loc) • 4.1 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _element = require("@gechiui/element");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _classnames = _interopRequireDefault(require("classnames"));
var _i18n = require("@gechiui/i18n");
var _components = require("@gechiui/components");
var _icons = require("@gechiui/icons");
var _useAvailableAlignments = _interopRequireDefault(require("./use-available-alignments"));
/**
* External dependencies
*/
/**
* GeChiUI dependencies
*/
/**
* Internal dependencies
*/
const BLOCK_ALIGNMENTS_CONTROLS = {
none: {
icon: _icons.alignNone,
title: (0, _i18n.__)('无')
},
left: {
icon: _icons.positionLeft,
title: (0, _i18n.__)('左对齐')
},
center: {
icon: _icons.positionCenter,
title: (0, _i18n.__)('居中对齐')
},
right: {
icon: _icons.positionRight,
title: (0, _i18n.__)('右对齐')
},
wide: {
icon: _icons.stretchWide,
title: (0, _i18n.__)('宽幅')
},
full: {
icon: _icons.stretchFullWidth,
title: (0, _i18n.__)('全幅')
}
};
const DEFAULT_CONTROL = 'none';
const POPOVER_PROPS = {
isAlternate: true
};
function BlockAlignmentUI(_ref) {
let {
value,
onChange,
controls,
isToolbar,
isCollapsed = true
} = _ref;
const enabledControls = (0, _useAvailableAlignments.default)(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 ? _components.ToolbarGroup : _components.ToolbarDropdownMenu;
const commonProps = {
popoverProps: POPOVER_PROPS,
icon: activeAlignmentControl ? activeAlignmentControl.icon : defaultAlignmentControl.icon,
label: (0, _i18n.__)('对齐'),
toggleProps: {
describedBy: (0, _i18n.__)('修改对齐方式')
}
};
const extraProps = isToolbar || _element.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 (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_components.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 (0, _element.createElement)(_components.MenuItem, {
key: controlName,
icon: icon,
iconPosition: "left",
className: (0, _classnames.default)('components-dropdown-menu__menu-item', {
'is-active': isSelected
}),
isSelected: isSelected,
onClick: () => {
onChangeAlignment(controlName);
onClose();
},
role: "menuitemradio",
info: info
}, title);
})));
}
};
return (0, _element.createElement)(UIComponent, (0, _extends2.default)({}, commonProps, extraProps));
}
var _default = BlockAlignmentUI;
exports.default = _default;
//# sourceMappingURL=ui.js.map