Skip to content

Create an API for defining blocks in the editor. #104

Closed

Description

@BE-Webdesign

This can serve as a discussion for creating an API for the editor. Share your thoughts 😄

Activity

  1. BE-Webdesign commented on Feb 20, 2017

    @BE-Webdesign
    ContributorAuthor

    Here are things I think the API should do/have:

    • Be extensible
    • Be easy to use
    • Code should be easy to read and understand.
    • Have great documentation/tutorials
    • Provide a way to register types of blocks
    • Create a separation between the blocks and their actual presentation. ( Blocks should be plain ole JavaScript objects that could then render themselves into what is needed. Most likely TinyMCE and DOM nodes. )

    Addendum ( Block Types should most likely be registered server side and exposed to the client. )

  2. added
    [Type] Task
    on Feb 20, 2017
  3. changed the title [-]Create an API for the editor.[/-] [+]Create an API for defining blocks in the editor.[/+] on Feb 20, 2017
  4. aduth commented on Feb 20, 2017

    @aduth
    Member

    Related controls discussion: #16

    Taking a few of the current mockups as a basis, I've identified some/all of the pieces a developer should expect to be able to implement:

    • Label
      • To be shown in insertion dialog
    • Icon
    • Block controls
    • Inline controls (optional)
      • In the case of the quote mockup, one should expect to be able to format text. Is this merely supporting nested plain text block?
      • It's possible that some blocks may want to support only a subset of formatting options
    • Children
      • Given sub-note of inline controls, perhaps blocks could allow nested block descendants
      • Future support of a "Columns" block?
    • Custom editor-specific form
      • In the case of the quote mockup, the quote and citation may be separate fields
    • Transform support
      • Mockup
      • e.g. change plain text to heading or quote
    • Advanced settings popover ("Inspector")

    Below are a few of my early prototypes which include rough implementations of some or all of these requirements:

    Given discussion at aduth/wp-block#1, I've also been wondering if we could optionally / alternatively support server-side block registration options, with the following goals in mind:

    • Sanitize block attribute values based on defined types
    • Scaffold UIs based on defined block attribute types
      • These would admittedly not be as great a user experience, but seems to be an easy way for developers to make quick early iterations of a block, or for freelancers/agencies who can't necessarily justify creating extremely polished block UIs
    • Allow blocks to be exposed through REST API or elsewhere in PHP, as current proposals expect registration to occur within browser client

    We could look to Shortcake and JS Widgets for inspiration on implementing / consolidating server-side registration if it's a path we want to pursue (cc @westonruter @mattheu @danielbachhuber )

    Less obvious, but if we decide to add a new syntax for blocks, we'll also want to consider how to add backwards compatibility support for e.g. captioned images (currently shortcode) which should either be supported by or transparently upgraded to the new recommended block. This might need to be a separate issue.

  5. aduth commented on Feb 20, 2017

    @aduth
    Member

    Getting into a little more detail, for both block and inline controls, we'll have the following requirements (some assuming we follow proposed block comment syntax):

    • Label
      • Used as tooltip / title
    • Icon
      • Simplest would be a string reference to icon name of either the Dashicon or Gridicon (whatever we settle on)
      • Optionally supporting image URL, similar to register_post_type's menu_icon
      • Perhaps too complex for the sake of optimization, but in my latest prototype I'd made this a reference to a virtual DOM component primarily to avoid over-including unused icons
    • Is selected?
      • Could either be associated with some attribute of a block, or simply passed a function and expected to return a truthy value (arguments include block attributes, children, ...)
    • Click/tap behavior
      • For inline controls, it's likely we'd need to execute some command on the TinyMCE instance (regardless of whether we take approach of single or multiple instances)
      • For block level controls, this may either change content of the block or affect its attributes
  6. westonruter commented on Feb 20, 2017

    @westonruter
    Member

    We could look to Shortcake and JS Widgets for inspiration on implementing / consolidating server-side registration if it's a path we want to pursue

    Just to note here that JS Widgets 0.4 was recently released and it includes full integration with Shortcake so that all widgets defined as “JS widgets” are automatically also available as Shortcake post elements (content blocks). Likewise it introduces icons for widgets (current Dashicons).

    The server-side registration ensures that that the widgets (blocks) can be exposed in the REST API and that the widget instance data is guaranteed to be JSON-representable so that the data can be manipulated directly with JS (see WP-CORE#35574, WP-CORE#33507). The widget also then defines the interface for manipulating the data, sanitizing the data, and rendering the data with PHP. The shortcode on the other hand only addresses the last piece, the rendering of the data. The Shortcake plugin was developed to implement the shortcode UI which essentially is a JS-driven version of what widgets historically handled with PHP (WP_Widget::form() and WP_Widget::update()), and thus JS Widgets is able to provide the plumbing for how Shortcake manages a widget shortcode's state.

  7. jasmussen commented on Feb 21, 2017

    @jasmussen
    Contributor

    In the case of the quote mockup, one should expect to be able to format text. Is this merely supporting nested plain text block?

    We should probably not tackle nested blocks for the editor focus. This will absolutely become necessary for the customization focus, but probably best as something to keep in mind but not work on.

    Incidentally you pointed out an error in the mockups. Yes, you should be able to format quotes. The omission of controls was in error. Here's an updated mockup:

    quote 2

    It's possible that some blocks may want to support only a subset of formatting options

    Absolutely. Headings probably shouldn't have any formatting at all. I'm sure there will be other blocks with unique requirements.

  8. Lewiscowles1986 commented on Mar 1, 2017

    @Lewiscowles1986
    Contributor

    Are there plans for what to do with oEmbed content like if someone posts a link to a YT in a paragraph?

    I'd think it's safer to

    • simply split the paragraph into two paragraphs with an oEmbed in-between if oEmbed link not at start or very end of content
    • simply end the paragraph before the oEmbed if there is no text after the oEmbed link
    • prepend the oEmbed if it's at the start of a paragraph
  9. jasmussen commented on Mar 1, 2017

    @jasmussen
    Contributor

    Are there plans for what to do with oEmbed content like if someone posts a link to a YT in a paragraph?

    Some discussion here: #81 (comment)

    See also #24 (comment)

  10. aduth commented on Mar 1, 2017

    @aduth
    Member

    Are there plans for what to do with oEmbed content like if someone posts a link to a YT in a paragraph?

    I was going to mention that in complementing discussion in #81, it would be nice to support blocks registering as being associated with a particular oEmbed URL pattern, such as to allow a YouTube block to enable the user to configure features like start time and autoplay. However, in looking at a few examples, it's not commonly supported to customize the embedded URL to affect these changes. In YouTube's case, you'd have to modify the markup of the embedded frame returned, or for Vimeo passing as arguments to the oEmbed request itself. Which is to say, neither of these services would allow you to control their embed behavior by the URL we'd expect to be saved in post content.

  11. added
    [Type] Technical Prototype
    on Mar 7, 2017
  12. aduth commented on Mar 15, 2017

    @aduth
    Member

    Stepping back for a moment, I thought it could be useful to outline some requirements of a block API for both a block implementer (plugin author) and the editor itself, as well as some ideas I've been exploring as conclusions of these requirements.

    Requirements

    Plugin Author:

    At it's most basic, the idea of a user inserting a block is...

    • Inserting some markup into post content
    • In all but a few cases, the markup will need some customization (think "state" of a block)
    • Some overlap exists, but ideally an editor representation of a block could be distinct from what's shown on the front end of a site (think empty states for an embed, placeholders for cite/caption)

    There are a few more nuanced requirements or desires, a few highlighted here:

    • If we need to define complex behaviors for editable fields, it would be good for these to be generalized in such a way for reuse (e.g. allowing only certain inline markup in image caption)
    • I may need to define some default styles for my block's appearance on the front-end in addition to within the editor
    • As a block implementer, I only need to be concerned with the logic of my own block, never with the editor context or sibling blocks

    Editor:

    Ideally, the editor...

    • Can render a block at any point in time given its known state (for the editor or for post content) without a round-trip to the server
    • Can make available the presence and ideally state and structure of a block from PHP / REST API (so as to enable third party clients including mobile apps to make sense of them)

    Conclusions / Ideas:

    Block Demarcation Syntax

    There have been some explorations of a block demarcation syntax, such as that in the "Coda" section of the original technical overview post. Here it was proposed to use comments to mark the bounds of a block:

    <!-- wp:image -->
    <figure>
      <img src="/">
      <figcaption>A picture is worth a thousand words</figcaption>
    </figure>
    <!-- /wp -->

    The post grammar explorations took this further to consider encoding some attributes within the comment syntax itself:

    <!-- wp:image src:"/" caption:"A picture is worth a thousand words" -->
    <figure>
      <img src="/">
      <figcaption>A picture is worth a thousand words</figcaption>
    </figure>
    <!-- /wp -->

    The idea of serializing a block's state entirely within the comment is nice because it can be easily parsed and exposed, even server-side and made available through the REST API. It also enables a block to be more easily rendered because the block's rendering logic becomes a function of its current state (if you're familiar with React paradigms, much like the idea of "components" and "props"). It lessens worries of markup becoming becoming out of sync because we treat the encoded state as the ultimate source of truth, we know what to expect and, if we find that the markup differs, we can provide fallbacks. Finally, it avoids the plugin author needing to manually determine their own block's state by parsing the DOM or traversing an AST.

    The most obvious downside is the amount of duplication that occurs between the attributes and the markup itself. I think there are a few options:

    • Bite the bullet and accept the duplication (see also: post revisions)
    • Encode only a few attributes to be used as hints (this sacrifices most of the benefits)
    • Store state elsewhere outside of the markup itself, but keep block demarcators and create a reference of a block's markup to its associated state

    To this last point, I think an idea that @danielbachhuber had considered for secret data (aduth/wp-block#2 (comment)) could be useful for all cases. If the state of a block is only truly necessary in its raw form in the context of the editor and from the REST API, it seems reasonable to store this elsewhere as post meta. So the markup becomes:

    <!-- wp:image 841 -->
    <figure>
      <img src="/">
      <figcaption>A picture is worth a thousand words</figcaption>
    </figure>
    <!-- /wp -->

    Where 841 is a meta_id of wp_postmeta whose meta_value is the serialized form of block state.

    Edit: Maybe this becomes simpler if the type itself is moved to post meta, where comments are generalized e.g. <!-- wp:block 228 -->

    Block Schemas

    While the above syntax can enable us to more easily make the state of a block available to other clients such as mobile apps, it's still not sufficient to enable them to present any sort of editable form. To support this and inspired by previous discussion in aduth/wp-block#1, there may be value in allowing the minimal representation of a block to be defined as a schema server-side.

    register_block( 'image', [
    	'schema' => [
    		'type' => 'object',
    		'properties' => [
    			'src'     => [ 'type' => 'string' ],
    			'caption' => [ 'type' => 'string' ]
    		]
    	]
    ] );

    The benefits here would be:

    • Consistency with REST API controller schemas
    • Combined with above syntax explorations, enables access to both state and structure of a block outside of the browser context
    • Provides an approachable interface for creating block prototypes, which can be progressively enhanced in the context of the editor, i.e. complementary JavaScript APIs to enhance block editor experience to achieve desired UX

    Componentization of Complex Behaviors

    While it's yet to be decided how far we take the idea of React's "UI a function of state", it leads naturally to componentizing not only blocks, but common behaviors between blocks. In the current multi-instance prototype, @youknowriad has applied this as far as toolbars being the responsibility of a block to render, which is not as difficult as it seems when masked as a simple ToolbarControls component.

    To my own earlier example, if one needed to control complex editable behaviors such as allowable markup, these could act as arguments (props) to a shared editable interface.

    Thinking more concretely and permitting the exploration of React's JSX syntax, a blocks editor render logic could amount to roughly:

    function ImageBlock( state, onChange ) {
    	return (
    		<figure>
    			<Toolbar />
    			<Media
    				src={ state.src }
    				onChange={ ( src ) => onChange( { src } ) } />
    			<Editable
    				inline
    				allowedTags={ [ 'strong', 'em', 'a' ] }
    				value={ state.caption }
    				onChange=( ( caption ) => onChange( { caption } ) ) />
    		</figure>
    	);
    }
  13. youknowriad commented on Mar 15, 2017

    @youknowriad
    Contributor

    Thanks @aduth This is really a great summary of the block API needs.

    As a block implementer, I only need to be concerned with the logic of my own block, never with the editor context or sibling blocks

    I just have some concerns regarding this. I agree it's ideal if we could limit the block API to the current block itself but I think it's not always possible. How do you think switching block types (we have this on the current prototypes) could work without knowledge about the block types to be switched to/from? Do you imagine a block abstraction like block category or something?

    The same concerns could apply while "merging" blocks, merging happens if we hit backspace at the beginning of a block.

  14. Lewiscowles1986 commented on Mar 15, 2017

    @Lewiscowles1986
    Contributor

    @youknowriad AFAIK the oEmbed has similar problems currently. Surely that is something for another issue? Perhaps one referencing this one.

  15. joyously commented on Mar 15, 2017

    @joyously

    What happens when the raw editor is used to modify the HTML? In the example of an image that has a postmeta reference containing the src, it will no longer match if I type over the HTML src tag to show a different image. Is there a way to recover from that sort of thing? Or what about typing over the figure tag to make it a paragraph? Or mangling the quotes or brackets?
    How will the block editor fail gracefully in these situations?
    #testreminder

  16. 11 remaining items

  17. mtias commented on Mar 17, 2017

    @mtias
    Member

    Interesting conversations here, thanks @aduth for igniting it.

    I want to take a small step back and think about the different layers we are interacting with: data source, data interface, editor output, viewer output, etc.

    As discussed in the post where we introduce the idea of HTML comments as demarcation, I believe data source is important to be in post_content for any static block (including blocks that have fixed fields like captions and cites). It is less important for dynamic blocks that need to pull resources from different places to be rendered.

    We only started with simpler blocks because we need to have a solution for them that doesn't fragment its reliability as editable HTML content. I want us to have a robust approach here that allows us to absorb post formats and move on to more complicated blocks.

    That is not to say that the more complicated blocks should have exactly the same way of storing content in HTML. The data interface, which represents how the developer of a block interacts with the state of the block, is separate from where and how we store that data. I see a lot of value in treating blocks as state for the editor experience, and presenting them as state to the block developer, but not in storing it as state separate from post_content, at least for static blocks.

    So let's look at what you see as the data state layer; as a developer I would request for my block:

    type: "image", { 'img.src', 'img.alt, 'caption' }
    

    That's what I need to build my UI and to render it in the editor as content to be manipulated. How we get this info into that structure is a concern of the editor architecture, and whether we infer it from HTML source or retrieve it from post-meta doesn't matter at that point to the developer.

    It does matter for us creating the editor internals and its API. So let's consider the image with caption source in the HTML:

    <!-- wp:image -->
    <figure>
      <img src="/">
      <figcaption>A picture is worth a thousand words</figcaption>
    </figure>
    <!-- /wp -->

    All the data is expressed in regular HTML, so to manipulate the caption separately you need to know about the shape of the markup. That can be as implicit as saying caption: "figcaption" when I define my block data requirements and we'll get the value for that DOM element to you. Or we can add some additional tools to explicitly label these fields like:

    <!-- wp:image -->
    <figure>
      <img src="/">
      <figcaption data-block-field="caption">A picture is worth a thousand words</figcaption>
    </figure>
    <!-- /wp -->

    This is one possible way of storing the relevant pieces to be extracted into the consumable structure for the developer. By default a developer request for a field with name could try to match data-field="name", otherwise it could match an HTML_Tag with name: "name".

    Here's a grammar parser output for a simpler version without a caption:

    [
      {
        "type": "WP_Block",
        "blockType": "image",
        "attrs": {},
        "startText": "<!-- wp:image -->",
        "endText": "<!-- /wp -->",
        "rawContent": "<!-- wp:image -->\n<figure class=\"\">\n  <img src=\"/\">\n</figure>\n<!-- /wp -->",
        "children": [
          {
            "type": "Text",
            "value": "\n"
          },
          {
            "type": "HTML_Tag_Open",
            "name": "figure",
            "attrs": {
              "class": ""
            },
            "text": "<figure class=\"\">"
          },
          {
            "type": "Text",
            "value": "\n  "
          },
          {
            "type": "HTML_Tag",
            "name": "img",
            "attrs": {
              "src": "/"
            },
            "startText": "<img src=\"/\">",
            "endText": "</figure>",
            "children": [
              {
                "type": "Text",
                "value": "\n"
              }
            ]
          },
          {
            "type": "Text",
            "value": "\n"
          }
        ]
      }
    ]

    We may not need to mark the image src as a data-field because we can rely on the semantics of <img> and the block developer knows there will be a single img element in the block returned by HTML_Tag with name img and get its attributes from the attrs object which we expose as name.attr.

    I am okay with having references to data that exists elsewhere for blocks that need the dynamic nature of that data, and it's ok to optimize for that. As long as we design the different parts of the puzzle properly, and the abstractions work at the same level, I don't think we need a single source of truth across the board.

    For complex and dynamic blocks this is a possibility and we don't expect the source of truth of these posts to exist within this HTML.

    <!-- wp:post-list {"query": {} } -->
    ... Fallback HTML ...
    <!-- /wp -->
  18. aduth commented on Mar 17, 2017

    @aduth
    Member

    So let's look at what you see as the data state layer; as a developer I would request for my block:

    type: "image", { 'img.src', 'img.alt, 'caption' }

    How do I as a developer even come to conclude that img.src is the correct path to access? There's a very opaque cloud of abstraction assumed here that I find only works well enough to garner a "Yeah, I suppose that makes sense" retrospective response and lacks an obviousness that solicits the confidence of "I know how to do this on my own".

  19. aduth commented on Mar 18, 2017

    @aduth
    Member

    Another problem I think we've overlooked 'til now is that while we've been discussing options for universal understanding between browser and server of block state and state shape, we've not put the same consideration toward generating block markup. So even if the server or mobile app is able to understand and perhaps even update the raw state values, as of now we have no consensus on how to update the markup accordingly.

    As an addendum to my earlier comment, it should be a requirement of the editor that a client is able to update its markup in response to user input without a round-trip to the server.

    Off the top of my head, we may have a couple options, neither of which I'm particularly happy with:

    • A block's markup rendering is implemented in JavaScript, meaning any client (like the mobile app) must have access to a JavaScript runtime in order to generate the markup (WebView, JavaScriptCore). The server would not be able to generate markup unless Node.js or V8js are detected (we should assume most hosts will not support these).
    • A block's markup rendering is implemented in PHP, but in a template language that can be understood and mimicked in a client. An obvious candidate is sprintf (sprintf.js), which is not a joy to work with for complex markup, might have sanitization implications, and would not provide us with good patterns for sharing common complex behavior among blocks.

    Does anyone have ideas for how we might tackle this?

  20. westonruter commented on Mar 18, 2017

    @westonruter
    Member

    In an ideal world, the client and server would be able to render a block. But as you note, this is really only feasible if both the server and the client are running the same language (e.g. JavaScript)… or if there is a sufficiently powerful templating language that has implementations in both JS and PHP, and I don't think Mustache is powerful enough.

    I'm inclined think it is not reasonable to expect that the arbitrarily-complex rendering logic on the frontend (in PHP) to be able to be run on the client by JavaScript (in the editor) as well. I think the approach that Shortcake took by rendering shortcodes on the server to be a reasonable baseline pattern to follow with rendering blocks in the editor. Otherwise, a block could provide a basic rough/raw rendering of a block for previewing in the context of the editor, such as is presently done with image galleries.

  21. sirbrillig commented on Mar 20, 2017

    @sirbrillig

    I ran into this same challenge when I was exploring these ideas at the end of last year. Mostly I had the same thoughts as you @aduth and @westonruter: shared markup language (that's probably insufficient), or server-rendered only (makes offline impossible, slow to update, and sucks for poor connections). I was only able to come up with two conclusions of my own:

    • It could be possible to write a rendering function in both PHP and JavaScript for each static block. This would be really annoying for a developer, but could possibly work if the tooling/templating was really good.
    • Since the editor is always client-side (JS or native running JS in a web view) we can always generate markup on the client in JS and save it to the server. This is just what you were describing above, but I don't see a big problem with requiring native clients to support JavaScript. I would note, though, that dynamic blocks will likely need to be rendered server-side, so they will need to be written in PHP. I suppose my question is: why does the server need to be able to generate static block markup?
  22. aduth commented on Mar 20, 2017

    @aduth
    Member

    This is just what you were describing above, but I don't see a big problem with requiring native clients to support JavaScript.

    Do you have anything specific in mind for an approach here? I've been thinking about this a bit and was curious if we could either treat the block effectively as an iframe via WebView (with some communication to the parent native context), or generate a scaffolded form from a block's schema and, upon change, execute the JavaScript for generating markup given a particular set of block attributes. This thinking might be a little naive since I've not worked much in a native environment. Those more familiar could indulge us with possible gotchas, particularly around loading scripts from a user's own site.

  23. BE-Webdesign commented on Mar 21, 2017

    @BE-Webdesign
    ContributorAuthor

    To throw in my two cents:

    I agree with azaozz's sentiments. To me the editor should only be concerned with manipulating the post_content which is then sent back to the server and saved. The server would send back out the previously saved post_content pretty much like what WordPress already does. The client would parse the post_content into a state tree which can be manipulated and re rendered, or not; as TinyMCE could essentially handle that for us. By keeping the flow the same we most likely will have 100% backwards compatibility with the current WP Editor. The new one will be a lot shinier though and provide a more seamless inline editing experience.

    Proposed Flow:

    post_content in DB (mostly just HTML) --render-to-editor-> Editor parses post_content into the editable UI (should be handled by TinyMCE) --save-to-server-> The post_content markup is saved in the same fashion WP currently handles everything. The more I think about this project, the more I think the focus in some ways should be centered around making an awesome single TinyMCE instance, that has an inline block editing UI. After looking over some of the things that can be done with TinyMCE, a lot of our problems can be solved by hacking on the APIs it provides.

    On the "secret/private" data idea:

    This type of data should/would exist only on "dynamic blocks" of content. We should make use of the shortcodes API already present in WordPress to handle that kind of stuff and improve that experience via iseulde's idea. Any reference data that needs to be used would be accessed via the shortcode/resource-id to prevent the sensitive data from being displayed in the actual markup publicly.

    Block Registry/Definition API:

    The API for defining/registering blocks should be very simple and only be concerned with defining/registering. As blocks are registered there should be someway for TinyMCE to pull data from the registered blocks and add that to the interface for the editor. The registering of blocks could happen server side, and could be pulled into the JS realm when the editor is being initialized. It could also happen client side and forego PHP, however the more I think about it the less I like this approach. There should probably be an association between a block and its controls, so that TinyMCE can dynamically generate the appropriate controls of a selected block based on the block types registered to the editor.

    Server vs Client:

    I do not see any benefit to trying to have the server render a block via static state, that should be handled via JavaScript. The way in which WordPress interacts with PHP/HTTP is stateless. The initial render and the previously saved output of the editor are one in the same. Adding an extra layer where PHP is rendering static state data as HTML seems like an unnecessary step. Use the server to serve what is already saved and let the client interpret the saved post_content and provide a GUI to manipulate that interpreted state. "Dynamic Blocks" should be rendered via the server in some form or another, (for now shortcodes somewhat work) or at the bare minimum the server should expose what controls/state is necessary to render a dynamic block client side.

    Environment:

    By contrast, what's the alternative? Offering a node or markup where the developer is expected to attach their event handlers and update the markup in response to the user making changes? This works, but is fragile at best.

    This is basically what TinyMCE does and although I agree it is fragile, it is also probably the most practical approach moving forward. I believe digging into the flexibility of TinyMCE to enable it to understand blocks properly is probably more important than worrying about rendering state to markup. A state -> markup system can always be created further down the road if it is ever considered that TinyMCE won't be used anymore.

    We also lose the ability to make meaningful observations about a block's markup in anything except the browser (i.e. how does the mobile app know how to do anything with Plugin X's special image block?).

    Plugin X's special image block type could be registered to expose data that could be used in a non browser environment. The HTML could be parsed as the state and after it is manipulated then rendered as new HTML in a non browser environment. The exposed block type data could be used in tandem with the HTML in a non browser environment. I think veering away from HTML as the main data format of representing post_content, although technically appealing, would be impractical in the context of WordPress and TinyMCE.

    how far we take the idea of React's "UI a function of state"

    Make the UI a function of post_content. Essentially a state tree and the post_content are just a different representation of the same thing.

    tl;dr:

    Don't over complicate things and veer too far away from the flow of data WordPress already uses. I think the Block Type Defining API should be server side in PHP. The work on the editor should mainly be focused on making a Single TinyMCE instance understand how to parse, interpret and modify blocks, which are then saved as post_content in the typical WordPress fashion as a mix of HTML and shortcodesque things.

  24. aduth commented on Mar 22, 2017

    @aduth
    Member

    @BE-Webdesign Good stuff, I can always get behind efforts to avoid introducing unnecessary complexity.

    That said, let's not trivialize making sense of the markup nor working with a DOM. In some ways we've done ourselves a disservice by starting with very simple block types. As far as implementing blocks go, headings and paragraphs are solved problems. The more interesting cases are galleries, contact forms, sliders, maps, repeaters, testimonials, subscribe forms, logo soup, plans comparisons, etc. We should be thinking about all of these block types as we work through the APIs that developers will use to build them.

    Editor parses post_content into the editable UI (should be handled by TinyMCE)

    This is easy for a single element like a paragraph tag, but TinyMCE won't be able to make sense of more complex markup on its own. The step from post_content to editable UI is a big one, because the markup needs to be transformed from one which is appropriate for published content to a UI which makes the most sense for edits, all the while preserving underlying values back and forth.

    TinyMCE can dynamically generate the appropriate controls of a selected block based on the block types registered to the editor

    Dynamically generated fields don't make for a good UX. When I'm adding a map block, I should see a map, not just a text input.

    [...] the developer is expected to attach their event handlers and update the markup in response to the user making changes [...]

    This is basically what TinyMCE does and although I agree it is fragile, it is also probably the most practical approach moving forward

    Kindly I disagree. For anything but the simplest elements, working with and keeping the DOM in sync with our understanding of state quickly becomes very difficult. Introducing conventions or tools is perfectly fine if the cost of becoming familiar with them is strongly negated by an increased sense of overall confidence on the developer's part to create sound implementations.


    Now a few points to concede on my part:

    • Shortcodes would be a reasonable practical solution to a need for secret data. It's not a great overall solution if we were to ever want to consolidate the idea of a shortcode into a single unified block pattern, and shortcodes seem better suited for dynamic content (in my example, "Gravatar" would be a static block).
    • Maybe keeping state in the DOM is fine, but we still need patterns around making sense of the underlying values. We could do this by mapping data- attributes like @mtias described, assuming the implementation of a parse function, or perhaps another solution that's yet to be explored. Despite all its downsides, the one thing "storing in meta" had going for it was being totally transparent to the block implementer.
    • We have a choice on just how opinionated we want to be with patterns. The block proposal in Editor Blocks API proposal #300 is less opinionated in that by providing access to a raw DOM node to work with in the editor, a developer can choose to use whichever tools they're most comfortable with. By contrast, my own in Add API documentation for blocks and element #302 is admittedly more opinionated with the introduction of React, but argues in doing so that it's the best and most succinct fit for the requirements.
  25. BE-Webdesign commented on Mar 23, 2017

    @BE-Webdesign
    ContributorAuthor

    @aduth,

    I think for the most part we are on the same page, and I probably am not articulating my ideas too well. My ideas could also be terrible 😄 . I am hoping there can be a way forward that somehow the new block editor can be jump started by post_content that a WordPress install already has, that could include shortcodes. As the block editor is used the block demarcators will be added and store the state into the post_content, so on the next rendering of the editor, the information for the blocks will be there. Getting TinyMCE to do that will certainly be a pain, but I think it could potentially lead to a smooth transition from the current editor to a new block based editor.

    The step from post_content to editable UI is a big one, because the markup needs to be transformed from one which is appropriate for published content to a UI which makes the most sense for edits, all the while preserving underlying values back and forth.

    Definitely, and this is what TinyMCE kind of does for us or at least closes that gap a bit. I think it is possible to get TinyMCE to understand "blocks" from the post_content and render the appropriate UI based solely off of information stored in post_content.

    Dynamically generated fields don't make for a good UX. When I'm adding a map block, I should see a map, not just a text input.

    I was talking about the controls for a block, a map will definitely be there for a map block. So for a "map block" TinyMCE will generate a control that can open a modal or something to switch up the map settings. I am probably not explaining my ideas well 😄 .

    Kindly I disagree. For anything but the simplest elements, working with and keeping the DOM in sync with our understanding of state quickly becomes very difficult. Introducing conventions or tools is perfectly fine if the cost of becoming familiar with them is strongly negated by an increased sense of overall confidence on the developer's part to create sound implementations.

    You could have React components inside the TinyMCE instance ( or whatever you want really ) to ease the handling of a more complex block.

  26. androb commented on Mar 23, 2017

    @androb
    Contributor

    Re @BE-Webdesign

    You could have React components inside the TinyMCE instance ( or whatever you want really ) to ease the handling of a more complex block.

    This is exactly what the TinyMCE Core team are looking at the moment. We want to be able to support "blocks", "components", "widgets" or whatever to coexist both "in" TinyMCE as well as outside of it. We are trying to get to some sort of 'best of both worlds' approach... but appreciate this project is moving very fast. We're trying to bring the TinyMCE team's ideas in :)

  27. ellatrix commented on Mar 24, 2017

    @ellatrix
    Member

    Experimenting a bit with some of the ideas @aduth had with having a render function and parsing, but without touching the DOM. It would look something like this, where the received content is JSON and h has helpers for this content. Instead of using arrays, you can also use h( name, { ..attrs }, ...children ) and if the developer wants, they can use JSX to generate it. render will need to handle empty props to insert an empty version (placeholder) of the block.

    function render( props ) {
    	return (
    		[ 'figure', { contenteditable: false },
    			( ! props.src ?
    				[ 'div', { onClick: onClick },
    					[ 'svg', ... ],
    					[ 'p', 'Pick image' ]
    				] :
    				[ 'img', { alt: props.alt, src: props.src } ]
    			),
    			[ 'figcaption', {
    				contenteditable: true,
    				placeholder: 'Write caption\u2026',
    			},
    				...props.caption
    			]
    		]
    	);
    }
    
    function getProps( content, h ) {
    	return {
    		alt: h.getAttribute( h.find( content, 'img' ), 'alt' ),
    		src: h.getAttribute( h.find( content, 'img' ), 'src' ),
    		caption: h.getChildren( h.find( content, 'figcaption' ) )
    	};
    }
    
    function onClick( props, callback ) {
    	filePicker().then( function( files ) {
    		...
    		callback( render( { src: src } ) );
    	} );
    }
    
    registerBlock( {
    	name: 'image',
    	...
    	controls: [ ... ],
    	render: render,
    	getProps: getProps
    } );
  28. added a commit that references this issue on Nov 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Type] Task[Type] Technical Prototype

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions