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

Add template part previews to placeholder block #22760

Merged
merged 53 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
821a1eb
initial commit
Addison-Stavlo May 22, 2020
43f1b51
conditioned behind experiment
Addison-Stavlo May 22, 2020
22a1158
create TP component
Addison-Stavlo May 25, 2020
7f1b30a
show previews of each template part
Addison-Stavlo May 25, 2020
5372af6
remove early return
Addison-Stavlo May 25, 2020
ff8bcff
kind of inserting
Addison-Stavlo May 25, 2020
932e355
styles
Addison-Stavlo May 25, 2020
37ee5ae
better inserting / bug fix
Addison-Stavlo May 25, 2020
17d7b63
group by theme
Addison-Stavlo May 26, 2020
49dce60
added success notice
Addison-Stavlo May 26, 2020
9b8ca36
set up for search section
Addison-Stavlo May 26, 2020
6892fcd
filtering working
Addison-Stavlo May 27, 2020
d52e5ab
placeholders and async list
Addison-Stavlo May 27, 2020
2de5f38
comment and var name change
Addison-Stavlo May 27, 2020
86b9b32
initial
Addison-Stavlo May 28, 2020
1776c99
moving stuff over
Addison-Stavlo May 28, 2020
95601e6
carried styles, changed classnames
Addison-Stavlo May 28, 2020
4d1e3da
more styles for previews
Addison-Stavlo May 28, 2020
4844fa3
style tweaks
Addison-Stavlo May 29, 2020
b6c70ad
add filter input
Addison-Stavlo May 29, 2020
1faecf6
revert inserter changes
Addison-Stavlo May 29, 2020
5b79c1c
erm and style tags too
Addison-Stavlo May 29, 2020
8680824
remove unused file
Addison-Stavlo May 29, 2020
bb025be
insert/selection function updated
Addison-Stavlo May 29, 2020
6416e82
remove old preview
Addison-Stavlo May 29, 2020
e6fe269
separate concerns with tabs
Addison-Stavlo May 29, 2020
616306a
carried over styles, updated class name
Addison-Stavlo May 29, 2020
3bd397f
more style edits, dont hide preview on small width
Addison-Stavlo May 29, 2020
411db1d
updated help and guiding phrases
Addison-Stavlo May 29, 2020
2086843
more classNames, styles, and refactoring
Addison-Stavlo May 29, 2020
50051aa
change 'slug' to 'name' in editor
Addison-Stavlo May 29, 2020
5b9af44
rename subcomponent
Addison-Stavlo May 29, 2020
b763adc
updating some e2e's
Addison-Stavlo May 29, 2020
6d6729c
updated first placeholder e2e, skipped ones that need to be rewritten…
Addison-Stavlo Jun 1, 2020
31a79e4
added new test for preview
Addison-Stavlo Jun 2, 2020
ac60bfb
test for selecting
Addison-Stavlo Jun 2, 2020
23cb174
e2e should be g2g
Addison-Stavlo Jun 2, 2020
e6d4d66
package lock
Addison-Stavlo Jun 3, 2020
6c23e01
move useAsyncList to compose package
Addison-Stavlo Jun 8, 2020
cd7388d
Update packages/block-editor/src/components/inserter/block-patterns.js
Addison-Stavlo Jun 10, 2020
dbe4dcc
Update packages/block-library/src/template-part/edit/placeholder.js
Addison-Stavlo Jun 10, 2020
e3283de
remove unnecessary frags
Addison-Stavlo Jun 10, 2020
edbb498
use setAttributes instead of rename
Addison-Stavlo Jun 10, 2020
8e503db
add per_page, remove auto-draft
Addison-Stavlo Jun 10, 2020
0c5fc43
rename placeholder
Addison-Stavlo Jun 10, 2020
37a2c99
update classnames missing placeholder prefix
Addison-Stavlo Jun 10, 2020
e6fabe5
use placeholder subdirectory
Addison-Stavlo Jun 10, 2020
bbe8134
merge conflict
Addison-Stavlo Jun 10, 2020
c027167
added resolved query merge
Addison-Stavlo Jun 10, 2020
294afbe
remove helpPhrase memo
Addison-Stavlo Jun 10, 2020
9298f99
add deburr to filtering
Addison-Stavlo Jun 11, 2020
e89d1ce
rebase
Addison-Stavlo Jun 11, 2020
3797d17
update test selector for new class name
Addison-Stavlo Jun 11, 2020
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
"@wordpress/keyboard-shortcuts": "file:../keyboard-shortcuts",
"@wordpress/keycodes": "file:../keycodes",
"@wordpress/priority-queue": "file:../priority-queue",
"@wordpress/rich-text": "file:../rich-text",
"@wordpress/shortcode": "file:../shortcode",
"@wordpress/token-list": "file:../token-list",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { useMemo, useCallback } from '@wordpress/element';
import { parse } from '@wordpress/blocks';
import { ENTER, SPACE } from '@wordpress/keycodes';
import { __, _x } from '@wordpress/i18n';
import { useAsyncList } from '@wordpress/compose';
Addison-Stavlo marked this conversation as resolved.
Show resolved Hide resolved

/**
* Internal dependencies
*/
import BlockPreview from '../block-preview';
import useAsyncList from './use-async-list';
import InserterPanel from './panel';
import { searchItems } from './search-items';
import InserterNoResults from './no-results';
Expand Down
131 changes: 0 additions & 131 deletions packages/block-library/src/template-part/edit/placeholder.js

This file was deleted.

171 changes: 171 additions & 0 deletions packages/block-library/src/template-part/edit/placeholder/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState, useCallback } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { cleanForSlug } from '@wordpress/url';
import {
Placeholder,
TextControl,
Button,
TabPanel,
} from '@wordpress/components';
import { layout } from '@wordpress/icons';

/**
* Internal dependencies
*/
import useTemplatePartPost from '../use-template-part-post';
import TemplatePartPreviews from './template-part-previews';

const HELP_PHRASES = {
initial: __( 'Please add a name and theme for your new Template Part.' ),
unavailable: __(
'Name and theme combination already in use, please try another.'
),
available: __( 'This name is available!' ),
error: __( 'Error adding template.' ),
};

export default function TemplatePartPlaceholder( { setAttributes } ) {
const [ slug, _setSlug ] = useState( '' );
const [ theme, _setTheme ] = useState( '' );
const [ help, setHelp ] = useState( '' );

// Try to find an existing template part.
const postId = useTemplatePartPost( null, slug, theme );

const setSlug = useCallback(
( nextSlug ) => {
_setSlug( nextSlug );
if ( help ) {
setHelp( '' );
}
},
[ help ]
);

const setTheme = useCallback(
( nextTheme ) => {
_setTheme( nextTheme );
if ( help ) {
setHelp( '' );
}
},
[ help ]
);

const getHelpPhrase = () => {
if ( ! slug || ! theme ) {
return HELP_PHRASES.initial;
} else if ( postId ) {
return HELP_PHRASES.unavailable;
}

return HELP_PHRASES.available;
};

const { saveEntityRecord } = useDispatch( 'core' );
const onCreate = useCallback( async () => {
const nextAttributes = { slug, theme };
// Create a new template part.
try {
const cleanSlug = cleanForSlug( slug );
const templatePart = await saveEntityRecord(
'postType',
'wp_template_part',
{
title: cleanSlug,
status: 'publish',
slug: cleanSlug,
meta: { theme },
}
);
nextAttributes.postId = templatePart.id;
} catch ( err ) {
setHelp( HELP_PHRASES.error );
}
setAttributes( nextAttributes );
}, [ postId, slug, theme ] );

const [ filterValue, setFilterValue ] = useState( '' );

const createTab = (
<>
<div className="wp-block-template-part__placeholder-input-container">
<TextControl
label={ __( 'Name' ) }
placeholder={ __( 'header' ) }
value={ slug }
onChange={ setSlug }
className="wp-block-template-part__placeholder-input"
/>
<TextControl
label={ __( 'Theme' ) }
placeholder={ __( 'twentytwenty' ) }
value={ theme }
onChange={ setTheme }
className="wp-block-template-part__placeholder-input"
/>
</div>
<div className="wp-block-template-part__placeholder-help-phrase">
{ help || getHelpPhrase() }
</div>
<Button
isPrimary
disabled={ ! slug || ! theme || postId }
onClick={ onCreate }
className="wp-block-template-part__placeholder-create-button"
>
{ __( 'Create' ) }
</Button>
</>
);

const selectTab = (
<>
<TextControl
label={ __( 'Search' ) }
placeholder={ __( 'header' ) }
value={ filterValue }
onChange={ setFilterValue }
className="wp-block-template-part__placeholder-preview-filter-input"
/>

<div className="wp-block-template-part__placeholder-preview-container">
<TemplatePartPreviews
setAttributes={ setAttributes }
filterValue={ filterValue }
/>
</div>
</>
);

return (
<Placeholder icon={ layout } label={ __( 'Template Part' ) }>
<TabPanel
className="wp-block-template-part__placeholder-tabs"
tabs={ [
{
name: 'select',
/* translators: Select tab of template part creation placeholder. */
title: __( 'Select from existing' ),
},
{
name: 'create',
/* translators: Create tab of template part placeholder. */
title: __( 'Create new' ),
},
] }
>
{ ( tab ) => {
if ( tab.name === 'create' ) {
return createTab;
}
return selectTab;
} }
</TabPanel>
</Placeholder>
);
}
Loading