@wordpress/block-library
Version:
Block library for the WordPress editor.
248 lines (207 loc) • 8.68 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _element = require("@wordpress/element");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _lodash = require("lodash");
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _i18n = require("@wordpress/i18n");
var _blockEditor = require("@wordpress/block-editor");
var _compose = require("@wordpress/compose");
var _editPanel = _interopRequireDefault(require("./edit-panel"));
var _indicator = _interopRequireDefault(require("./indicator"));
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
var ReusableBlockEdit =
/*#__PURE__*/
function (_Component) {
(0, _inherits2.default)(ReusableBlockEdit, _Component);
function ReusableBlockEdit(_ref) {
var _this;
var reusableBlock = _ref.reusableBlock;
(0, _classCallCheck2.default)(this, ReusableBlockEdit);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(ReusableBlockEdit).apply(this, arguments));
_this.startEditing = _this.startEditing.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
_this.stopEditing = _this.stopEditing.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
_this.setAttributes = _this.setAttributes.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
_this.setTitle = _this.setTitle.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
_this.save = _this.save.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
if (reusableBlock && reusableBlock.isTemporary) {
// Start in edit mode when we're working with a newly created reusable block
_this.state = {
isEditing: true,
title: reusableBlock.title,
changedAttributes: {}
};
} else {
// Start in preview mode when we're working with an existing reusable block
_this.state = {
isEditing: false,
title: null,
changedAttributes: null
};
}
return _this;
}
(0, _createClass2.default)(ReusableBlockEdit, [{
key: "componentDidMount",
value: function componentDidMount() {
if (!this.props.reusableBlock) {
this.props.fetchReusableBlock();
}
}
}, {
key: "startEditing",
value: function startEditing() {
var reusableBlock = this.props.reusableBlock;
this.setState({
isEditing: true,
title: reusableBlock.title,
changedAttributes: {}
});
}
}, {
key: "stopEditing",
value: function stopEditing() {
this.setState({
isEditing: false,
title: null,
changedAttributes: null
});
}
}, {
key: "setAttributes",
value: function setAttributes(attributes) {
this.setState(function (prevState) {
if (prevState.changedAttributes !== null) {
return {
changedAttributes: (0, _objectSpread2.default)({}, prevState.changedAttributes, attributes)
};
}
});
}
}, {
key: "setTitle",
value: function setTitle(title) {
this.setState({
title: title
});
}
}, {
key: "save",
value: function save() {
var _this$props = this.props,
reusableBlock = _this$props.reusableBlock,
onUpdateTitle = _this$props.onUpdateTitle,
updateAttributes = _this$props.updateAttributes,
block = _this$props.block,
onSave = _this$props.onSave;
var _this$state = this.state,
title = _this$state.title,
changedAttributes = _this$state.changedAttributes;
if (title !== reusableBlock.title) {
onUpdateTitle(title);
}
updateAttributes(block.clientId, changedAttributes);
onSave();
this.stopEditing();
}
}, {
key: "render",
value: function render() {
var _this$props2 = this.props,
isSelected = _this$props2.isSelected,
reusableBlock = _this$props2.reusableBlock,
block = _this$props2.block,
isFetching = _this$props2.isFetching,
isSaving = _this$props2.isSaving,
canUpdateBlock = _this$props2.canUpdateBlock;
var _this$state2 = this.state,
isEditing = _this$state2.isEditing,
title = _this$state2.title,
changedAttributes = _this$state2.changedAttributes;
if (!reusableBlock && isFetching) {
return (0, _element.createElement)(_components.Placeholder, null, (0, _element.createElement)(_components.Spinner, null));
}
if (!reusableBlock || !block) {
return (0, _element.createElement)(_components.Placeholder, null, (0, _i18n.__)('Block has been deleted or is unavailable.'));
}
var element = (0, _element.createElement)(_blockEditor.BlockEdit, (0, _extends2.default)({}, this.props, {
isSelected: isEditing && isSelected,
clientId: block.clientId,
name: block.name,
attributes: (0, _objectSpread2.default)({}, block.attributes, changedAttributes),
setAttributes: isEditing ? this.setAttributes : _lodash.noop
}));
if (!isEditing) {
element = (0, _element.createElement)(_components.Disabled, null, element);
}
return (0, _element.createElement)(_element.Fragment, null, (isSelected || isEditing) && (0, _element.createElement)(_editPanel.default, {
isEditing: isEditing,
title: title !== null ? title : reusableBlock.title,
isSaving: isSaving && !reusableBlock.isTemporary,
isEditDisabled: !canUpdateBlock,
onEdit: this.startEditing,
onChangeTitle: this.setTitle,
onSave: this.save,
onCancel: this.stopEditing
}), !isSelected && !isEditing && (0, _element.createElement)(_indicator.default, {
title: reusableBlock.title
}), element);
}
}]);
return ReusableBlockEdit;
}(_element.Component);
var _default = (0, _compose.compose)([(0, _data.withSelect)(function (select, ownProps) {
var _select = select('core/editor'),
getReusableBlock = _select.__experimentalGetReusableBlock,
isFetchingReusableBlock = _select.__experimentalIsFetchingReusableBlock,
isSavingReusableBlock = _select.__experimentalIsSavingReusableBlock;
var _select2 = select('core'),
canUser = _select2.canUser;
var _select3 = select('core/block-editor'),
getBlock = _select3.getBlock;
var ref = ownProps.attributes.ref;
var reusableBlock = getReusableBlock(ref);
return {
reusableBlock: reusableBlock,
isFetching: isFetchingReusableBlock(ref),
isSaving: isSavingReusableBlock(ref),
block: reusableBlock ? getBlock(reusableBlock.clientId) : null,
canUpdateBlock: !!reusableBlock && !reusableBlock.isTemporary && !!canUser('update', 'blocks', ref)
};
}), (0, _data.withDispatch)(function (dispatch, ownProps) {
var _dispatch = dispatch('core/editor'),
fetchReusableBlocks = _dispatch.__experimentalFetchReusableBlocks,
updateReusableBlockTitle = _dispatch.__experimentalUpdateReusableBlockTitle,
saveReusableBlock = _dispatch.__experimentalSaveReusableBlock;
var _dispatch2 = dispatch('core/block-editor'),
updateBlockAttributes = _dispatch2.updateBlockAttributes;
var ref = ownProps.attributes.ref;
return {
fetchReusableBlock: (0, _lodash.partial)(fetchReusableBlocks, ref),
updateAttributes: updateBlockAttributes,
onUpdateTitle: (0, _lodash.partial)(updateReusableBlockTitle, ref),
onSave: (0, _lodash.partial)(saveReusableBlock, ref)
};
})])(ReusableBlockEdit);
exports.default = _default;
//# sourceMappingURL=edit.js.map