Skip to content
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

Merged
merged 22 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
69408f4
Display the help text for the TextareaArray setting
kienstra Feb 4, 2021
9176d61
Let e2e tests run by changing to wp-env
kienstra Feb 4, 2021
dbfdfac
Merge in develop, resolve conflicts.
kienstra Apr 30, 2021
e0c1fc5
Remove sanitize and validate values from settings
kienstra Apr 30, 2021
4af23d4
If a field is deleted, also delete it from the previewAttributes
kienstra May 3, 2021
a344c51
See if upgrading server-side-render is possible
kienstra May 3, 2021
5e84929
Revert "See if upgrading server-side-render is possible"
kienstra May 3, 2021
81c1820
When changing a field name, change the name in the previewAttributes
kienstra May 3, 2021
66452fe
On changing the field type, delete the previewAttribute
kienstra May 3, 2021
f1c4aff
Refactor preview into <FrontEndPreview> component
kienstra May 3, 2021
67ae09b
Possible approach to handling <ServerSideRender> error
kienstra May 3, 2021
236e885
Simpler approach to front-end preview (if it works)
kienstra May 3, 2021
19632aa
Handle the Front-end preview for new post
kienstra May 3, 2021
7cb75db
If there is no field to preview, suggest that they add a field
kienstra May 3, 2021
5028610
Change text to 'edit the block,' as it doesn't need fields
kienstra May 3, 2021
5492839
See if userEven helps the test pass
kienstra May 4, 2021
abd6979
Fix failed e2e test
kienstra May 4, 2021
4a8b58a
Make e2e test more robust: query for role, not selector
kienstra May 4, 2021
094953f
Add artifacts/ to .gitignore, it looks like the new wordpress/scripts…
kienstra May 4, 2021
ce7d120
Pluralize the attributes names and values
kienstra May 5, 2021
a538af9
Add a new component for the Multiselect defaults
kienstra May 5, 2021
96156f2
Fix the failed PHPUnit test
kienstra May 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ node_modules
vendor

# Tests
artifacts/
tests/data/
tests/includes/
coverage/
Expand Down
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ gulp.task( 'wporg:trunk', function() {
gulp.task( 'clean:bundle', function() {
return del( [
'package/trunk/package',
'package/trunk/artifacts',
'package/trunk/assets/wporg',
'package/trunk/coverage',
'package/trunk/js/src',
Expand Down
11 changes: 7 additions & 4 deletions js/src/block-editor/components/repeater-rows.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import * as React from 'react';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -135,10 +140,8 @@ class RepeaterRows extends Component {
const attribute = attr[ parentName ];
const repeaterRows = this.getRows( attribute );

/*
* Calling slice() essentially creates a copy of repeaterRows.
* Without this, it looks like setAttributes() doesn't recognize a change to the array, and the component doesn't re-render.
*/
// Calling slice() essentially creates a copy of repeaterRows.
// Without this, it looks like setAttributes() doesn't recognize a change to the array, and the component doesn't re-render.
const rows = repeaterRows.slice();
[ rows[ from ], rows[ to ] ] = [ rows[ to ], rows[ from ] ];

Expand Down
62 changes: 62 additions & 0 deletions js/src/edit-block/components/editor-preview.js
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 = () => {
Copy link
Contributor Author

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

const { block, changeBlock } = useBlock();
const { getFields } = useField();
const { previewAttributes = {} } = block;
const fields = getFields();

/** @param {Object} newAttributes Attribute (field) names and values. */
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;
55 changes: 18 additions & 37 deletions js/src/edit-block/components/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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.
*/
Copy link
Contributor Author

@kienstra kienstra Feb 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, but noticed I forgot to give Setting a @typedef before.


/**
* 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 ]
Expand All @@ -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;
}
Expand All @@ -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
? (
Expand All @@ -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 />
Expand Down
72 changes: 72 additions & 0 deletions js/src/edit-block/components/front-end-preview.js
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 = () => {
Copy link
Contributor Author

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 from editor.js, with some conditionals in case there's nothing to preview.

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' ) }
&nbsp;
<PostSavedState />
</div>
)
: (
<ServerSideRender
block={ `genesis-custom-blocks/${ block?.name }` }
attributes={ block?.previewAttributes }
className="genesis-custom-blocks-editor__ssr"
httpMethod="POST"
/>
)
}
</>
);
};

export default FrontEndPreview;
5 changes: 1 addition & 4 deletions js/src/edit-block/components/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link
Contributor Author

@kienstra kienstra May 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are essentially false by default. So no need to pass false.

/>
<PostSavedState />
</span>
<span className="mr-3">
<PostPublishButton />
Expand Down
2 changes: 2 additions & 0 deletions js/src/edit-block/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ export { default as BrowserURL } from './browser-url';
export { default as CategorySection } from './category-section';
export { default as ClipboardCopy } from './clipboard-copy';
export { default as Editor } from './editor';
export { default as EditorPreview } from './editor-preview';
export { default as EditorProvider } from './editor-provider';
export { default as Field } from './field';
export { default as FieldPanel } from './field-panel';
export { default as FieldsGrid } from './fields-grid';
export { default as FieldSettings } from './field-settings';
export { default as FrontEndPreview } from './front-end-preview';
export { default as Header } from './header';
export { default as IconSection } from './icon-section';
export { default as Input } from './input';
Expand Down
2 changes: 1 addition & 1 deletion js/src/edit-block/components/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as React from 'react';
/**
* @typedef {Object} InputProps The component props.
* @property {Function} handleOnChange Handles a change in this setting.
* @property {Object} setting This setting.
* @property {import('./editor').Setting} setting This setting.
* @property {string} type The type of <input>, like 'text'.
* @property {string|undefined} value The setting value.
* @property {number} [min] The min attribute of an input[type="number"].
Expand Down
2 changes: 1 addition & 1 deletion js/src/edit-block/components/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as React from 'react';
* @property {Function} handleOnChange Handles a change in this setting.
* @property {string} id The id attribute.
* @property {Array} options The options, including their label and value.
* @property {Object} setting This setting.
* @property {import('./editor').Setting} setting This setting.
* @property {string|undefined} value The setting value.
*/

Expand Down
4 changes: 2 additions & 2 deletions js/src/edit-block/components/settings/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import * as React from 'react';
/**
* @typedef {Object} CheckboxProps The component props.
* @property {Function} handleOnChange Handles a change in this setting.
* @property {Object} setting This setting.
* @property {boolean|number|undefined} value The setting value.
* @property {import('../editor').Setting} setting This setting.
* @property {boolean|undefined} value The setting value.
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion js/src/edit-block/components/settings/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Input } from '../';

/**
* @typedef {Object} EmailProps The component props.
* @property {Object} setting This setting.
* @property {import('../editor').Setting} setting This setting.
* @property {string|undefined} value The setting value.
* @property {Function} handleOnChange Handles a change in this setting.
*/
Expand Down
1 change: 1 addition & 0 deletions js/src/edit-block/components/settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export { default as NumberNonNegative } from './number-non-negative';
export { default as Text } from './text';
export { default as Textarea } from './textarea';
export { default as TextareaArray } from './textarea-array';
export { default as TextareaDefault } from './textarea-default';
export { default as Width } from './width';
2 changes: 1 addition & 1 deletion js/src/edit-block/components/settings/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { LOCATIONS_WITH_LABEL, LOCATIONS } from '../../../common/constants';

/**
* @typedef {Object} LocationProps The component props.
* @property {Object} setting This setting.
* @property {import('../editor').Setting} setting This setting.
* @property {string|undefined} value The setting value.
* @property {Function} handleOnChange Handles a change to this setting.
* @property {Function} setCurrentLocation Sets the selected location, like 'editor'.
Expand Down
2 changes: 1 addition & 1 deletion js/src/edit-block/components/settings/new-line-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Select } from '../';

/**
* @typedef {Object} NewLineFormatProps The component props.
* @property {Object} setting This setting.
* @property {import('../editor').Setting} setting This setting.
* @property {string|undefined} value The setting value.
* @property {Function} handleOnChange Handles a change to this setting.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Input } from '../';

/**
* @typedef {Object} NumberNonNegativeProps The component props.
* @property {Object} setting This setting.
* @property {import('../editor').Setting} setting This setting.
* @property {string|undefined} value The setting value.
* @property {Function} handleOnChange Handles a change in this setting.
*/
Expand Down
2 changes: 1 addition & 1 deletion js/src/edit-block/components/settings/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Input } from '../';

/**
* @typedef {Object} NumberProps The component props.
* @property {Object} setting This setting.
* @property {import('../editor').Setting} setting This setting.
* @property {string|undefined} value The setting value.
* @property {Function} handleOnChange Handles a change in this setting.
*/
Expand Down
Loading