Adding configuration to a property editor

    Overview

    This is step 2 in our guide to building a property editor. This step continues work on the markdown editor we built in step 1, but goes further to show how to add configuration options to our editor.

    Configuration?

    An important part of building good property editors is to build something relatively flexible, so we can reuse it many times, for different things. Like the Rich Text Editor in Umbraco, that allows us to choose which buttons and stylesheets we want to use on each instance of the editor.

    So an editor can be used several times, with different configurations, and that is what we will be working on now.

    package.manifest

    To add configuration options to our markdown editor, open the package.manifest file. Right below the editor definition, paste in the prevalues block:

    ...
    editor: {
        view: "~/App_Plugins/MarkDownEditor/markdowneditor.html"
    }, // Remeber a comma seperator here at the end of the editor block!
    prevalues: {
        fields: [
            {
                label: "Preview",
                description: "Display a live preview",
                key: "preview",
                view: "boolean"
            },
            {
                label: "Default value",
                description: "If value is blank, the editor will show this",
                key: "defaultValue",
                view: "textarea"
            }
        ]
    }
    

    So what did we add? We added a prevalue editor, with a fields collection. This collection contains information about the UI we will render on the data type configuration for this editor.

    So the first one gets the label "Preview" and uses the view "boolean", so this will allow us to turn preview on/off and will provide the user with a checkbox. The name "boolean" comes from the convention that all preview editors are stored in /umbraco/views/prevalueeditors/ and then found via <name>.html

    Same with the next one, only that it will provide the user with a textarea to input a default value for the editor.

    Save the manifest, restart the app pool and have a look at the markdown data type in Umbraco now. You should now see that you have 2 configuration options:

    An example of how the configuration will look

    Using the configuration

    The next step is to gain access to our new configuration options. For this, open the markdowneditor.controller.js file.

    Let's first add the default value functionality. Basically, when the $scope.model.value is empty or undefined, we want to use the default value. To do that, we add the following to the very beginning of the controller:

    if($scope.model.value === null || $scope.model.value === ""){
        $scope.model.value = $scope.model.config.defaultValue;
    }
    

    See what's new? - the $scope.model.config object is. Also, because of this configuration, we now have access to $scope.model.config.defaultValue which contains the configuration value for that key.

    However, we may also use these values without using JavaScript, by opening the markdowneditor.html file instead.

    Here we can use the configuration directly in our HTML instead, where we use it to toggle the preview <div>, using the ng-show attribute:

    <div id="wmd-preview-{{model.alias}}" ng-show="{{model.config.preview}}" class="wmd-panel wmd-preview"></div>
    

    Next - Integrating services with a property editor