-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Display the help text in the TextareaArray setting #62
Changes from 18 commits
69408f4
9176d61
dbfdfac
e0c1fc5
4af23d4
a344c51
5e84929
81c1820
66452fe
f1c4aff
67ae09b
236e885
19632aa
7cb75db
5028610
5492839
abd6979
4a8b58a
094953f
ce7d120
a538af9
96156f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import * as React from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useBlock, useField } from '../hooks'; | ||
import { Fields } from '../../block-editor/components'; | ||
|
||
/** | ||
* The editor preview. | ||
* | ||
* @return {React.ReactElement} The editor preview. | ||
*/ | ||
const EditorPreview = () => { | ||
const { block, changeBlock } = useBlock(); | ||
const { getFields } = useField(); | ||
const { previewAttributes = {} } = block; | ||
const fields = getFields(); | ||
|
||
/** @param {Object} newAttributes Attribute (field) name and value. */ | ||
const setAttributes = ( newAttributes ) => { | ||
changeBlock( { | ||
previewAttributes: { | ||
...previewAttributes, | ||
...newAttributes, | ||
}, | ||
} ); | ||
}; | ||
|
||
return ( | ||
<div className="block-form"> | ||
{ | ||
Boolean( Object.keys( fields ).length ) | ||
? ( | ||
<Fields | ||
key="example-fields" | ||
fields={ fields } | ||
parentBlockProps={ { | ||
setAttributes, | ||
attributes: previewAttributes, | ||
} } | ||
parentBlock={ {} } | ||
/> | ||
) : ( | ||
<p> | ||
{ __( 'Please add a field to preview the block.', 'genesis-custom-blocks' ) } | ||
</p> | ||
) | ||
} | ||
</div> | ||
); | ||
}; | ||
|
||
export default EditorPreview; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,17 +13,18 @@ import { | |
UnsavedChangesWarning, | ||
} from '@wordpress/editor'; | ||
import { StrictMode, useState } from '@wordpress/element'; | ||
import ServerSideRender from '@wordpress/server-side-render'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
BlockPanel, | ||
BrowserURL, | ||
EditorPreview, | ||
EditorProvider, | ||
FieldPanel, | ||
FieldsGrid, | ||
FrontEndPreview, | ||
Header, | ||
LocationButtons, | ||
Main, | ||
|
@@ -39,8 +40,7 @@ import { | |
TEMPLATE_EDITOR_EDITING_MODE, | ||
} from '../constants'; | ||
import { DEFAULT_LOCATION } from '../../common/constants'; | ||
import { useBlock, useField, useTemplate } from '../hooks'; | ||
import { Fields } from '../../block-editor/components'; | ||
import { useBlock, useTemplate } from '../hooks'; | ||
|
||
/** | ||
* @callback onErrorType Handler for errors. | ||
|
@@ -83,17 +83,25 @@ import { Fields } from '../../block-editor/components'; | |
* @property {string} [width] The width, like '25'. | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} Setting A field setting. | ||
* @see PHP class Genesis\CustomBlocks\Blocks\Controls\ControlSetting | ||
* @property {string} name The name of the setting. | ||
* @property {string} label The label of the setting to display in the GCB editor. | ||
* @property {string} help A help value that display in the GCB editor. | ||
* @property {string} type The setting type, like 'width' or 'text', not a data type like boolean. | ||
* @property {*} default The default value. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not related to this PR, but noticed I forgot to give |
||
|
||
/** | ||
* The editor component. | ||
* | ||
* @param {EditorProps} props The component props. | ||
* @return {React.ReactElement} The editor. | ||
*/ | ||
const Editor = ( { onError, postId, postType, settings } ) => { | ||
const { block, changeBlock } = useBlock(); | ||
const { block } = useBlock(); | ||
const { template } = useTemplate(); | ||
const { previewAttributes = {} } = block; | ||
const { getFields } = useField(); | ||
const post = useSelect( | ||
( select ) => select( 'core' ).getEntityRecord( 'postType', postType, postId ), | ||
[ postId, postType ] | ||
|
@@ -104,16 +112,6 @@ const Editor = ( { onError, postId, postType, settings } ) => { | |
const [ panelDisplaying, setPanelDisplaying ] = useState( BLOCK_PANEL ); | ||
const [ selectedField, setSelectedField ] = useState( NO_FIELD_SELECTED ); | ||
|
||
/** @param {Object} newAttributes Attribute (field) name and value. */ | ||
const setAttributes = ( newAttributes ) => { | ||
changeBlock( { | ||
previewAttributes: { | ||
...previewAttributes, | ||
...newAttributes, | ||
}, | ||
} ); | ||
}; | ||
|
||
if ( ! post ) { | ||
return null; | ||
} | ||
|
@@ -139,19 +137,8 @@ const Editor = ( { onError, postId, postType, settings } ) => { | |
setCurrentLocation={ setCurrentLocation } | ||
/> | ||
{ EDITOR_PREVIEW_EDITING_MODE === editorMode && block && block.fields | ||
? ( | ||
<div className="block-form"> | ||
<Fields | ||
key="example-fields" | ||
fields={ getFields() } | ||
parentBlockProps={ { | ||
setAttributes, | ||
attributes: previewAttributes, | ||
} } | ||
parentBlock={ {} } | ||
/> | ||
</div> | ||
) : null | ||
? <EditorPreview /> | ||
: null | ||
} | ||
{ BUILDER_EDITING_MODE === editorMode | ||
? ( | ||
|
@@ -165,14 +152,8 @@ const Editor = ( { onError, postId, postType, settings } ) => { | |
) : null | ||
} | ||
{ FRONT_END_PREVIEW_EDITING_MODE === editorMode | ||
? ( | ||
<ServerSideRender | ||
block={ `genesis-custom-blocks/${ block.name }` } | ||
attributes={ previewAttributes } | ||
className="genesis-custom-blocks-editor__ssr" | ||
httpMethod="POST" | ||
/> | ||
) : null | ||
? <FrontEndPreview /> | ||
: null | ||
} | ||
{ TEMPLATE_EDITOR_EDITING_MODE === editorMode | ||
? <TemplateEditor /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import * as React from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { PostSavedState } from '@wordpress/editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
import ServerSideRender from '@wordpress/server-side-render'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getBlock, getBlockNameWithNameSpace } from '../helpers'; | ||
|
||
/** | ||
* The front-end preview of the block. | ||
* | ||
* Gets the saved post with .getCurrentPost(), | ||
* not the edited post. | ||
* The server side only has access to the saved post. | ||
* So there can be an error in <ServerSideRender> | ||
* if it passes attributes that aren't yet saved. | ||
* | ||
* @return {React.ReactElement} The front-end preview. | ||
*/ | ||
const FrontEndPreview = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is mainly extracted from |
||
const currentPost = useSelect( ( select ) => select( 'core/editor' ).getCurrentPost() ); | ||
const isPostNew = useSelect( ( select ) => select( 'core/editor' ).isEditedPostNew() ); | ||
const isPostDirty = useSelect( ( select ) => select( 'core/editor' ).isEditedPostDirty() ); | ||
|
||
const fullBlock = getBlock( currentPost?.content ); | ||
const blockNameWithNameSpace = getBlockNameWithNameSpace( fullBlock ); | ||
const block = fullBlock[ blockNameWithNameSpace ] || {}; | ||
|
||
// There's nothing entered or saved, so <ServerSideRender> would have an error. | ||
if ( isPostNew && ! isPostDirty ) { | ||
return ( | ||
<p className="mt-4"> | ||
{ __( 'Please edit the block to preview it.', 'genesis-custom-blocks' ) } | ||
</p> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
{ | ||
isPostNew && isPostDirty | ||
? ( | ||
<div className="mt-4 flex flex-row items-center"> | ||
{ __( 'Please save your block to preview it:', 'genesis-custom-blocks' ) } | ||
| ||
<PostSavedState /> | ||
</div> | ||
) | ||
: ( | ||
<ServerSideRender | ||
block={ `genesis-custom-blocks/${ block?.name }` } | ||
attributes={ block?.previewAttributes } | ||
className="genesis-custom-blocks-editor__ssr" | ||
httpMethod="POST" | ||
/> | ||
) | ||
} | ||
</> | ||
); | ||
}; | ||
|
||
export default FrontEndPreview; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,10 +109,7 @@ const Header = ( { editorMode, setEditorMode } ) => { | |
</button> | ||
<div id="save-and-publish"> | ||
<span className="mr-3 text-sm"> | ||
<PostSavedState | ||
forceIsDirty={ false } | ||
forceIsSaving={ false } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are essentially |
||
/> | ||
<PostSavedState /> | ||
</span> | ||
<span className="mr-3"> | ||
<PostPublishButton /> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is mainly extracted out of
editor.js