@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
94 lines (91 loc) • 2.76 kB
JavaScript
/**
* External dependencies
*/
import Textarea from 'react-autosize-textarea';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { useMemo } from '@wordpress/element';
import { __unstableSerializeAndClean } from '@wordpress/blocks';
import { useDispatch, useSelect } from '@wordpress/data';
import { useInstanceId } from '@wordpress/compose';
import { VisuallyHidden } from '@wordpress/components';
/**
* Internal dependencies
*/
import { store as editorStore } from '../../store';
/**
* Displays the Post Text Editor along with content in Visual and Text mode.
*
* @return {JSX.Element|null} The rendered PostTextEditor component.
*/
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
export default function PostTextEditor() {
const instanceId = useInstanceId(PostTextEditor);
const {
content,
blocks,
type,
id
} = useSelect(select => {
const {
getEditedEntityRecord
} = select(coreStore);
const {
getCurrentPostType,
getCurrentPostId
} = select(editorStore);
const _type = getCurrentPostType();
const _id = getCurrentPostId();
const editedRecord = getEditedEntityRecord('postType', _type, _id);
return {
content: editedRecord?.content,
blocks: editedRecord?.blocks,
type: _type,
id: _id
};
}, []);
const {
editEntityRecord
} = useDispatch(coreStore);
// Replicates the logic found in getEditedPostContent().
const value = useMemo(() => {
if (content instanceof Function) {
return content({
blocks
});
} else if (blocks) {
// If we have parsed blocks already, they should be our source of truth.
// Parsing applies block deprecations and legacy block conversions that
// unparsed content will not have.
return __unstableSerializeAndClean(blocks);
}
return content;
}, [content, blocks]);
return /*#__PURE__*/_jsxs(_Fragment, {
children: [/*#__PURE__*/_jsx(VisuallyHidden, {
as: "label",
htmlFor: `post-content-${instanceId}`,
children: __('Type text or HTML')
}), /*#__PURE__*/_jsx(Textarea, {
autoComplete: "off",
dir: "auto",
value: value,
onChange: event => {
editEntityRecord('postType', type, id, {
content: event.target.value,
blocks: undefined,
selection: undefined
});
},
className: "editor-post-text-editor",
id: `post-content-${instanceId}`,
placeholder: __('Start writing with text or HTML')
})]
});
}
//# sourceMappingURL=index.js.map