question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to configure the Vue component of a custom block with a SelectField?

See original GitHub issue

Hi @tidyui,

I am now moving towards version 7, it’s very nice. I am stuck trying to write the Vue component of a custom block that contains both, an HtmlField and a SelectField. I have been able to add the HtmlField without any issues basing the code from the HtmlBlock, but I am stuck trying ti display the SelecField.

Do you have an example of this with Vue?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tidyuicommented, Sep 10, 2019

Block does not export meta-data for fields atm, this means you can’t call the select-field component from your custom component. You can however add a select tag to it, for example here I have added it to the standard HtmlBlock.

HtmlBlock.cs

namespace Piranha.Extend.Blocks
{
    /// <summary>
    /// Single column HTML block.
    /// </summary>
    [BlockType(Name = "Content", Category = "Content", Icon = "fas fa-paragraph", Component = "html-block")]
    public class HtmlBlock : Block
    {
        public enum BackgroundColor {
            Black, White
        }

        public SelectField<BackgroundColor> Background { get; set; }

        /// <summary>
        /// Gets/sets the HTML body.
        /// </summary>
        public HtmlField Body { get; set; }
    }
}

html-block.js

/*global
    piranha
*/

Vue.component("html-block", {
    props: ["uid", "toolbar", "model"],
    data: function () {
        return {
            body: this.model.body.value
        };
    },
    methods: {
        onBlur: function (e) {
            this.model.body.value = e.target.innerHTML;
        }
    },
    computed: {
        isEmpty: function () {
            return piranha.utils.isEmptyHtml(this.model.body.value);
        }
    },
    mounted: function () {
        piranha.editor.addInline(this.uid, this.toolbar);
    },
    beforeDestroy: function () {
        piranha.editor.remove(this.uid);
    },
    template:
        "<div class='block-body' :class='{ empty: isEmpty }'>" +
        "  <select v-model='model.background.value'><option value='0'>Black</option><option value='1'>White</option></select>" +
        "  <div contenteditable='true' :id='uid' spellcheck='false' v-html='body' v-on:blur='onBlur'></div>" +
        "</div>"
});

Please note that in the current version you have to manually register select fields for them to work in blocks. SelectFields added to regions are automatically registered in the application by the AttributeBuilder, but this does not process blocks.

Startup.cs

App.Fields.RegisterSelect<Piranha.Extend.Blocks.HtmlBlock.BackgroundColor>();

All of this could be changed to work a bit smoother, but to make it solid it requires more changes and testing than what we have the possibility to add to this service release. Because of this I will postpone development for this issue.

1reaction
tidyuicommented, Sep 9, 2019

We try to have a fast release schedule for service releases after a new major version!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create a custom selected list using Multiselect for ...
So today I decided to create a quick tutorial that takes that select field and with a bit of code we will create...
Read more >
Access kirby objects in Custom Block Vue component ...
A step-by-step guide to creating custom blocks with a preview using the example of an audio block with a poster and some additional...
Read more >
Building Custom Multi-Option Form Components with Vue.js
Select field with objects as values. As you can see in the screenshot below, the custom select component we're going to build, makes...
Read more >
Piranha CMS - Custom block won't save
If I omit the Component property in the BlockTypeAttribute, the block works, saves the image and content to draft and updates the site...
Read more >
Custom Blocks
Loaders applied for a custom block are matched based on the lang attribute of the block, the block's tag name, and the rules...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found