@wordpress/edit-post
Version:
Edit Post module for WordPress.
166 lines (149 loc) • 5.37 kB
JavaScript
import { createElement } from "@wordpress/element";
/**
* External dependencies
*/
import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { PanelBody, TextControl, ExternalLink } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose, ifCondition, withState } from '@wordpress/compose';
import { cleanForSlug } from '@wordpress/editor';
import { safeDecodeURIComponent } from '@wordpress/url';
/**
* Module Constants
*/
var PANEL_NAME = 'post-link';
function PostLink(_ref) {
var isOpened = _ref.isOpened,
onTogglePanel = _ref.onTogglePanel,
isEditable = _ref.isEditable,
postLink = _ref.postLink,
permalinkParts = _ref.permalinkParts,
editPermalink = _ref.editPermalink,
forceEmptyField = _ref.forceEmptyField,
setState = _ref.setState,
postTitle = _ref.postTitle,
postSlug = _ref.postSlug,
postID = _ref.postID;
var prefix = permalinkParts.prefix,
suffix = permalinkParts.suffix;
var prefixElement, postNameElement, suffixElement;
var currentSlug = safeDecodeURIComponent(postSlug) || cleanForSlug(postTitle) || postID;
if (isEditable) {
prefixElement = prefix && createElement("span", {
className: "edit-post-post-link__link-prefix"
}, prefix);
postNameElement = currentSlug && createElement("span", {
className: "edit-post-post-link__link-post-name"
}, currentSlug);
suffixElement = suffix && createElement("span", {
className: "edit-post-post-link__link-suffix"
}, suffix);
}
return createElement(PanelBody, {
title: __('Permalink'),
opened: isOpened,
onToggle: onTogglePanel
}, isEditable && createElement("div", {
className: "editor-post-link"
}, createElement(TextControl, {
label: __('URL Slug'),
value: forceEmptyField ? '' : currentSlug,
onChange: function onChange(newValue) {
editPermalink(newValue); // When we delete the field the permalink gets
// reverted to the original value.
// The forceEmptyField logic allows the user to have
// the field temporarily empty while typing.
if (!newValue) {
if (!forceEmptyField) {
setState({
forceEmptyField: true
});
}
return;
}
if (forceEmptyField) {
setState({
forceEmptyField: false
});
}
},
onBlur: function onBlur(event) {
editPermalink(cleanForSlug(event.target.value));
if (forceEmptyField) {
setState({
forceEmptyField: false
});
}
}
}), createElement("p", null, __('The last part of the URL. '), createElement(ExternalLink, {
href: "https://codex.wordpress.org/Posts_Add_New_Screen"
}, __('Read about permalinks')))), createElement("p", {
className: "edit-post-post-link__preview-label"
}, __('Preview')), createElement(ExternalLink, {
className: "edit-post-post-link__link",
href: postLink,
target: "_blank"
}, isEditable ? createElement(Fragment, null, prefixElement, postNameElement, suffixElement) : postLink));
}
export default compose([withSelect(function (select) {
var _select = select('core/editor'),
isEditedPostNew = _select.isEditedPostNew,
isPermalinkEditable = _select.isPermalinkEditable,
getCurrentPost = _select.getCurrentPost,
isCurrentPostPublished = _select.isCurrentPostPublished,
getPermalinkParts = _select.getPermalinkParts,
getEditedPostAttribute = _select.getEditedPostAttribute;
var _select2 = select('core/edit-post'),
isEditorPanelEnabled = _select2.isEditorPanelEnabled,
isEditorPanelOpened = _select2.isEditorPanelOpened;
var _select3 = select('core'),
getPostType = _select3.getPostType;
var _getCurrentPost = getCurrentPost(),
link = _getCurrentPost.link,
id = _getCurrentPost.id;
var postTypeName = getEditedPostAttribute('type');
var postType = getPostType(postTypeName);
return {
isNew: isEditedPostNew(),
postLink: link,
isEditable: isPermalinkEditable(),
isPublished: isCurrentPostPublished(),
isOpened: isEditorPanelOpened(PANEL_NAME),
permalinkParts: getPermalinkParts(),
isEnabled: isEditorPanelEnabled(PANEL_NAME),
isViewable: get(postType, ['viewable'], false),
postTitle: getEditedPostAttribute('title'),
postSlug: getEditedPostAttribute('slug'),
postID: id
};
}), ifCondition(function (_ref2) {
var isEnabled = _ref2.isEnabled,
isNew = _ref2.isNew,
postLink = _ref2.postLink,
isViewable = _ref2.isViewable,
permalinkParts = _ref2.permalinkParts;
return isEnabled && !isNew && postLink && isViewable && permalinkParts;
}), withDispatch(function (dispatch) {
var _dispatch = dispatch('core/edit-post'),
toggleEditorPanelOpened = _dispatch.toggleEditorPanelOpened;
var _dispatch2 = dispatch('core/editor'),
editPost = _dispatch2.editPost;
return {
onTogglePanel: function onTogglePanel() {
return toggleEditorPanelOpened(PANEL_NAME);
},
editPermalink: function editPermalink(newSlug) {
editPost({
slug: newSlug
});
}
};
}), withState({
forceEmptyField: false
})])(PostLink);
//# sourceMappingURL=index.js.map