-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Conversation
This is a duplicate to #347 (regarding the config of the root node) |
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.
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> |
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.
I think this demo page should be removed, we already have the default post content viewable in the plugin page.
editor/blocks/heading/index.js
Outdated
const { headingType = 'H2', value } = attributes; | ||
return <Heading nodeName={ headingType }>{ value }</Heading>; | ||
} | ||
} ); |
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.
I'd prefer a separate PR for each block, it eases the discussion.
See #327 (comment) for relevant changes to |
Removed the new demo files; Removed the changes for headings; Fixed linting issues in the new list plugin;
blocks/components/editable/index.js
Outdated
@@ -84,3 +84,7 @@ export default class Editable extends wp.element.Component { | |||
); | |||
} | |||
} | |||
|
|||
Editable.defaultProps = { | |||
nodeName: 'div' |
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.
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; |
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.
Should these be generic across the entire editor canvas or specific to the blocks we're implementing?
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.
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.
editor/blocks/list/index.js
Outdated
|
||
return ( | ||
<Editable | ||
nodeName={ listType } |
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.
Editable
accepts a tagName
, the difference being that the nodeName
here will be capitalized. We should toLowerCase
it and pass as tagName
.
editor/blocks/list/index.js
Outdated
|
||
edit( { attributes, onChange } ) { | ||
const { listType = 'ol', items = [] } = attributes; | ||
const value = items.map( i => { |
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.
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; |
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.
When not default, listType
will be uppercased (nodeName
). Should the default be uppercased as well? See note below about Editable
tagName
.
editor/blocks/list/index.js
Outdated
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>; |
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.
Since we're not reusing the List
function anywhere except here, might be clearer to simply inline it? Up to you.
editor/blocks/list/index.js
Outdated
|
||
save( { attributes } ) { | ||
const { listType = 'ol', items = [] } = attributes; | ||
const children = items.map( ( i, index ) => <li key={ index }>{ i.value }</li> ); |
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.
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.
languages/gutenberg.pot
Outdated
@@ -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 |
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.
See also: #377
We could probably skip these changes. I'll hope to tackle this in the next day.
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.
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
<Editable | ||
tagName={ listType } | ||
value={ value } | ||
onChange={ onChange } /> |
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.
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.
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:
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.
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 asvalue
. 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.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?