gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Add ability to render custom props in RichText's saved output

Open sanderteirlynck opened this issue 8 years ago • 6 comments

Issue Overview

One of the benefits of the new Gutenberg editor is that it allows you to have a clear idea of what you’re building will look like on your website once published. Having the ability to render custom properties would allow for more freedom in visualising what a block will look like.

For instance: I’ve written a custom block that adds a number in front of a div. The number that should appear in front of the div depends on the value of a block attribute. To add this number in front of the block, I could use the following LESS:

.numbered-div {
    &:before {
        content: attr(data-number) '. ';
    }
}

Changing the value of the block attribute however, does not reflect the changes in the editor, since the custom ‘data-number’ attribute does not get added to the HTML of the custom block in the editor. This is because the render-function of a block does not account for custom properties.

To see what my block actually looks like, I have to save whatever I created and refresh the website, as the custom ‘data-number’ attribute appears in the HTML on the website. Changing the block from ‘Edit visually’ to ‘Edit as HTML’ in the editor does show the custom data-attribute in the HTML of the block.

I am aware that I could fix this issue by using classes, but that also means I have to add classes for every potential value, for instance:

.numbered-div-1 {
    &:before {
        content: ‘1. ';
    }
}

.numbered-div-2 {
    &:before {
        content: ‘2. ';
    }
}

…

.numbered-div-10 {
    &:before {
        content: ‘10. ';
    }
}

Expected Behavior

Custom block properties are rendered in the editor to get an accurate idea of what your project looks like on the webpage.

Current Behavior

Custom block properties aren’t rendered in the editor. To get an accurate idea of what your custom block looks like you have to save your project and refresh the webpage it appears on.

Todos

  • [ ] Tests
  • [ ] Documentation

sanderteirlynck avatar Feb 02 '18 10:02 sanderteirlynck

The issue is not very clear to me, I'm not sure I understand what you're trying to achieve here. Could you provide your block code for instance?

youknowriad avatar Feb 15 '18 09:02 youknowriad

I am aware that I could fix this issue by using classes, but that also means I have to add classes for every potential value

For the particular issue you've described you can use CSS counters to avoid writing a class for each value.

.numbered-divs {
    counter-reset: plus-one;
    .numbered-div {
        &::before {
            content: counter(plus-one) ". ";
            counter-increment: plus-one;
        }
    }
}

More here: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Lists_and_Counters/Using_CSS_counters

chthonic-ds avatar Feb 15 '18 10:02 chthonic-ds

@youknowriad An example of my code would indeed make this easier to understand. An example of the block in the edit function:

...
el(
    wp.blocks.Editable, {
        tagName: ‘div,
        className: ‘numbered-div’,
        ‘data-list-number’: list_number
        value: content,
        onChange: on_change_content,
        ...
    }
)
...

This outputs the following code in the editor (using the Inspector):

...
<div class="blocks-editable">
    <h2 contenteditable="true" class="numbered-div blocks-editable__tinymce mce-content-body" aria-label="Numbered div test" id="mce_1" data-is-placeholder-visible="false">Numbered div test</h2>
    <div></div>
</div>
...

An example of the block in the save function:

...
el(
    'div',
    {
        className: 'numbered-div',
        'data-list-number': list_number
    },
    content
)
...

This outputs the following code on the previewed/published page:

<h2 class="numbered-div" data-list-number="1">Numbered div test</h2>

As you can tell, the custom 'data-list-number' attribute is not rendered in the editor, but is saved and being output to the previewed/published page.

However, if I toggle 'Edit as HTML' in the editor, the following code appears:

<h2 class="numbered-div" data-list-number="1">Numbered div test</h2>

The 'data-list-number' HTML attribute is simply not rendered in the editor since the 'render' function does not support rendering custom properties.

Hopefully that clarifies this issue a bit more.

@chthonic-ds Thank you for the suggestion! I had already tried this approach before I posted my original issue. The problem with this approach is that this only works as expected on the previewed/published page, since all the 'numbered-div' elements are a child of one container. In the editor, however, all blocks are wrapper in a separate container, resulting in all 'Numbered div' blocks having a '1. ' in front of them, instead of incrementing numbers. This means that once again, the editor will not reflect what the previewed/published page will look like. Having a custom HTML attribute for every block would allow more flexibility in what number is being displayed in the editor.

sanderteirlynck avatar Feb 16 '18 08:02 sanderteirlynck

Thanks for the clarifications @sanderteirlynck So basically, the issue is the ability to pass extra props to the TinyMCE container element.

Not that there'll always be a small difference between the rendering and the editor markup (the .blocks-editable wrapper) but we can probably add a way to propagate the extra props to the inner TinyMCE node.

youknowriad avatar Feb 16 '18 08:02 youknowriad

@sanderteirlynck how about wrapping RichText with div in the edit method:

...
el(
    'div',
    {
        ‘data-list-number’: list_number
    },
    el(
        wp.blocks.Editable,
        {
            tagName: ‘div,
            className: ‘numbered-div’,
            value: content,
            onChange: on_change_content,
            ...
        }
    )
)
...

Would that solve your issue?

gziolo avatar Oct 22 '18 10:10 gziolo

@sanderteirlynck I will close this issue since a proposed solution was added and there haven't been any replies since. Please re-open this issue with a note if the solution doesn't solve the problem for you!

designsimply avatar Dec 18 '18 00:12 designsimply