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

[AMP Stories] Add template Inserter #2029

Merged
merged 25 commits into from
Apr 1, 2019

Conversation

miina
Copy link
Contributor

@miina miina commented Mar 26, 2019

Closes #1902.

This PR adds template inserter which at the moment displays all reusable blocks which have AMP Story Page as the root block.

Note that at this moment it is possible to create only single Page templates.

  • Show list of story page templates in inserter along with the blank page.
  • Show preview of pages.
  • The template has to be inserted to the editor as regular blocks so that they can be edited/removed, etc.
  • Allow adding reusable blocks within AMP Story.
  • Style properly.

@googlebot googlebot added the cla: yes Signed the Google CLA label Mar 26, 2019
@miina
Copy link
Contributor Author

miina commented Mar 26, 2019

@swissspidy This is still WIP but let me know if you have any initial thoughts which might influence the direction of the approach.

}

export function BlockPreviewContent( { name, attributes } ) {
// @todo Importing this outside of the function causes error for some reason.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is because the new block-editor package is not yet listed in wpDependencies in webpack.config.js nor in package.json.

You can still use @wordpress/editor for now. It just proxies to the new package behind the scenes for BC.

Alternatively, since I am probably going to merge #2000 today and doing some rewriting there from @wordpress/editor to @wordpress/block-editor, you could then just merge in the latest changes from the amp-stories-redux branch.

}

// @todo We only need reusable blocks that can be used by AMP Stories.
const blocksRequest = this.lastRequest = apiFetch( {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thinking out loud here

One downside of doing the API request in the component is that we can't easily use the component twice without duplicating requests.

I was looking a bit at the Inserter and InserterMenu components in Gutenberg and how they use fetchReusableBlocks and some other functions to do the data fetching. Are there perhaps some commonalities that we could re-use (maybe in combination with an api-fetch middleware to do the filtering) or copy over to our own data store?

We only need reusable blocks that can be used by AMP Stories.

This goes the other way around too: In all other editors we don't want to show AMP Stories templates. ?search doesn't work for that. An easy solution could be using a taxonomy behind the scenes. Post meta could work too, but doesn't scale well.

Copy link
Contributor Author

@miina miina Mar 27, 2019

Choose a reason for hiding this comment

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

?search doesn't work for that. An easy solution could be using a taxonomy behind the scenes. Post meta could work too, but doesn't scale well.

Agreed, ?search was added as more of a placeholder for preventing fetching all the reusable blocks which would then just cause errors on insertion or due to unregistered block types. I was thinking that implementing taxonomy / meta could be done either in #2010 / #2011 since those PR-s involve creating template posts.

Are there perhaps some commonalities that we could re-use (maybe in combination with an api-fetch middleware to do the filtering) or copy over to our own data store?

Actually, we could also use the default experimental dispatch/selector for getting all reusable blocks and filter those. I thought initially that this might break the logic due to potentially including unregistered blocks, however, it looks like allowing core/template as an allowed block should resolve that issue. I'll make the change to test it out and then if it seems like it would make sense to manage the reusable blocks in our own data store then we can rework that later. Thoughts?

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 would mean though that we'd need to rework it anyway once taxonomy comes in. So yes, perhaps it does make sense to move it to our own data store.

Copy link
Contributor Author

@miina miina Mar 27, 2019

Choose a reason for hiding this comment

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

Alternatively, we could just filter by having amp-story-page as the root block of the template, and not allow anything else, maybe just that could make sense for now, too. Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Makes sense to me. Let's stick with the current approach and iterate in follow-up PRs.

}

componentDidMount() {
this.props.fetchReusableBlocks();
Copy link
Contributor Author

@miina miina Mar 27, 2019

Choose a reason for hiding this comment

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

@swissspidy I reworked this not to use API fetch here, however, we'll need to change it again once we get to categories.

Also looked into using the local store together with API but didn't come to a reasonable solution at this moment. Maybe for now we could actually use the API fetch as it was before and improve it later.

I'm thinking of improving the style to be acceptable and then leave the rest to the next PR-s / issues, potentially reverting to using API fetch for now. Thoughts?

EDIT: Actually I'll probably add the possibility to add reusable blocks as well (to some extent), for the inserter to make more sense.

@miina
Copy link
Contributor Author

miina commented Mar 28, 2019

@amedina @swissspidy This should be ready for testing. Note that this is the basic implementation, and search, editing, setting up the initial default templates etc. will be added within other PR-s.

The "Save as Template" option has been added to the Block settings, this replaces the "Save as Reusable Block" setting, see it in action here:

template

@miina miina marked this pull request as ready for review March 28, 2019 19:58
@miina miina requested a review from swissspidy March 28, 2019 20:07
@miina miina changed the title [WIP] [AMP Stories] Add template Inserter [AMP Stories] Add template Inserter Mar 28, 2019
assets/src/constants.js Outdated Show resolved Hide resolved
@swissspidy
Copy link
Collaborator

In case anyone is wondering why the "Save as Template" menu item doesn't look so great, I reported the bug earlier and it is being fixed: WordPress/gutenberg#14745

Copy link
Collaborator

@swissspidy swissspidy left a comment

Choose a reason for hiding this comment

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

Let's 🚢 it!

We can address the remaining todos in follow-up PRs.

@miina miina merged commit 258a131 into amp-stories-redux Apr 1, 2019
@miina miina deleted the amp-story/1902-template_inserter branch April 1, 2019 14:56
@westonruter westonruter added this to the v1.2 milestone May 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants