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

Early List block implementation (no UI yet) #358

Merged
merged 17 commits into from
Apr 7, 2017

Conversation

mimo84
Copy link
Contributor

@mimo84 mimo84 commented Mar 31, 2017

Slight change to Editable

The change to Editable that we made was to pass in the tag to use as the editable root for the tinymce instance. This enables inline tinymce mode to run on something that isn't just a div. It also avoids the issues that Edwin highlighted in his PR with forced_root_block.

Heading PR (#337) changes

Due to the change we made to Editable, we changed the heading PR to pass in the heading tag as the root node for the Editable component. It may make sense to do something similar for text blocks as well (i.e. pass in a p tag).

Early List Block (#346)

Using the heading/index.js as a guide, we created a basic list implementation. Using the ol/ul as the editor root, we could see two ways of doing this:

  1. pass the list items to the Editable component as an HTML string (like the heading)

We created a list block to have two attributes: listType and items. ListType is a string (either 'ol' or 'ul') and items is an array of item information. At this point, items only have value. We envisage that you may be able to extend the item object format to allow for nested lists.

e.g.

let items = [{ value: 'item 1' }, { value: 'item 2' }]

When the list blocks gets given these items, it turns them into their equivalent HTML string (e.g. <li>item 1</li><li>item 2</li>), and feeds that into Editable as value. This has the advantage of keeping React components separate from tiny's contents, but the disadvantage of having to manually manage the HTML strings. However, we will probably manage these with tiny commands anyway.

  1. create the list items as react components, and change Editable to be given children as well

The second approach we considered (haven't implemented) is to create the entire list structure as react components, and then pass those list items as children components to the Editable component. This has the advantage of being cleaner when manipulating items, but it has the disadvantage of mixing tinymce content with react's rendering. This will likely lead to issues.

Demo section

In order to effectively test the block editing and saving processes in isolation, we created a test page at demo/demo.html. This can run without the Wordpress environment but still requires webpack to run in the background. Is there a better way that we should be doing this?

@anna-harrison anna-harrison mentioned this pull request Mar 31, 2017
@youknowriad
Copy link
Contributor

This is a duplicate to #347 (regarding the config of the root node)

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. I think it'll be better if we can split this PR to smaller PRs for each block. Also, make sure to check the linting errors :)

I tested the two blocks, they behave properly. But let's get #347 merged before merging those blocks.

demo/demo.html Outdated
</table>
</body>

</html>
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this demo page should be removed, we already have the default post content viewable in the plugin page.

const { headingType = 'H2', value } = attributes;
return <Heading nodeName={ headingType }>{ value }</Heading>;
}
} );
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer a separate PR for each block, it eases the discussion.

@aduth
Copy link
Member

aduth commented Mar 31, 2017

See #327 (comment) for relevant changes to edit and save function signature.

@mimo84 mimo84 added [Feature] Blocks Overall functionality of blocks Framework Issues related to broader framework topics, especially as it relates to javascript labels Apr 4, 2017
@@ -84,3 +84,7 @@ export default class Editable extends wp.element.Component {
);
}
}

Editable.defaultProps = {
nodeName: 'div'
Copy link
Member

Choose a reason for hiding this comment

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

There's no nodeName prop for Editable and an effective default is already assigned in the render. We can remove this set of lines.

@@ -25,6 +25,14 @@ body.toplevel_page_gutenberg {
svg {
fill: currentColor;
}

ul {
list-style-type: disc;
Copy link
Member

Choose a reason for hiding this comment

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

Should these be generic across the entire editor canvas or specific to the blocks we're implementing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would keep these generic as these styles can be applied to the generic freeform block as well. In the future these might go away if we decide that the list block can support multiple styles of lists.
Without these styles the default list style type is none in the wp-admin page.


return (
<Editable
nodeName={ listType }
Copy link
Member

Choose a reason for hiding this comment

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

Editable accepts a tagName, the difference being that the nodeName here will be capitalized. We should toLowerCase it and pass as tagName.


edit( { attributes, onChange } ) {
const { listType = 'ol', items = [] } = attributes;
const value = items.map( i => {
Copy link
Member

Choose a reason for hiding this comment

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

In general I think we should avoid non-descript variable names like i in favor of more verbose and clear alternatives; here, item.

},

edit( { attributes, onChange } ) {
const { listType = 'ol', items = [] } = attributes;
Copy link
Member

Choose a reason for hiding this comment

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

When not default, listType will be uppercased (nodeName). Should the default be uppercased as well? See note below about Editable tagName.

save( { attributes } ) {
const { listType = 'ol', items = [] } = attributes;
const children = items.map( ( i, index ) => <li key={ index }>{ i.value }</li> );
return <List nodeName={ listType }>{ children }</List>;
Copy link
Member

Choose a reason for hiding this comment

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

Since we're not reusing the List function anywhere except here, might be clearer to simply inline it? Up to you.


save( { attributes } ) {
const { listType = 'ol', items = [] } = attributes;
const children = items.map( ( i, index ) => <li key={ index }>{ i.value }</li> );
Copy link
Member

Choose a reason for hiding this comment

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

We'll probably need to dangerouslySetInnerHTML here for HTML in value to not be escaped. I'd like to explore options here to make this less... scary, and more obvious / transparent.

const children = items.map( ( item, index ) => (
	<li dangerouslySetInnerHTML={ { __html: item.value } } />
) );

I'd be curious to know whether key matters at this line since we're not doing any sort of reconciliation, we're simply calling renderToString at this point.

@@ -3,15 +3,15 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"X-Generator: babel-plugin-wp-i18n\n"

#: editor/blocks/freeform/index.js:4
Copy link
Member

Choose a reason for hiding this comment

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

See also: #377

We could probably skip these changes. I'll hope to tackle this in the next day.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, on the topic of localization, we should localize the block's title.

See text bock as example:

https://github.com/WordPress/gutenberg/blob/f1b8d8d/editor/blocks/text/index.js#L5

@intronic intronic mentioned this pull request Apr 6, 2017
<Editable
tagName={ listType }
value={ value }
onChange={ onChange } />
Copy link
Member

Choose a reason for hiding this comment

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

We can improve this in a future pull request, but just FYI that this isn't really doing anything. You'll probably want to set this up so that onChange specifically updates the items entries.

@mimo84 mimo84 merged commit 2f84506 into WordPress:master Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Blocks Overall functionality of blocks Framework Issues related to broader framework topics, especially as it relates to javascript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants