Creating inline editors for widget properties

Inline editors allow content editors to configure the properties of widgets. They can be used as an alternative to the property configuration dialog, or in combination with it.

Unlike the configuration dialog, inline editors are interactive elements directly within the output of the related widget. For example, inline editors can range from basic value inputs to more advanced options, such as file uploaders or text areas with rich formatting options.

The editors are visible only in the Kentico administration interface when working with pages that contain editable areas. However, the configured properties then affect the appearance or functionality of the given widget on the live site.

Note: Each inline editor manages only a single widget property. For widgets that have multiple properties, you can add multiple inline editors into the widget’s output.

MVC Areas

Widgets are designed as global components. Adding related files, such as inline editor files, into MVC Areas is not supported and may lead to unexpected behavior.

Implementing inline editors

To create an inline editor for a property and integrate it into your widgets:

For a full scenario with code samples which will guide you through the process of developing a widget with a property and inline editor, visit Example - Developing a widget in MVC.

  1. Add an editor view model class to your MVC project.
    • The model class should contain at least two properties – the name and value of the widget property managed by the editor.

    • We recommend storing the view models of inline editors in a designated folder, for example: ~/Models/InlineEditors/<editor name>/

      
      
      
        public class CustomEditorModel
        {
            public string PropertyName { get; set; }
            ...
        }
      
      
        
  2. Create a partial view that defines the inline editor’s interface.
    • Make the partial view use the view model that you created for the editor.

    • The view code must encapsulate inner HTML elements using the Kentico().BeginInlineEditor() helper method. Specify the following parameters for the method:

      • A unique identifier of the inline editor.
      • The name of the widget property managed by the editor.
    
    
    
     @using Kentico.PageBuilder.Web.Mvc
     @using Kentico.Web.Mvc
    
     @model CustomEditorModel
    
     @using (Html.Kentico().BeginInlineEditor("custom-editor", Model.PropertyName))
     {
         ...
     }
    
    
     
    • We recommend storing the partial views of inline editors in a designated folder, for example: ~/Views/Shared/InlineEditors/
  3. Create a JavaScript file containing a self invoking function that registers the property editor. Every widget property editor must be registered using client-side code.
    • To register the property editor, call the window.kentico.pageBuilder.registerInlineEditor function.

      
      
      
        (function () {
          // Registers the 'custom-editor' inline property editor within the page builder scripts
          window.kentico.pageBuilder.registerInlineEditor("custom-editor", {
            init: function (options) {
                var editor = options.editor;
                ...
            }
          });
        })();
      
      
        

      Specify the following parameters when calling the registerInlineEditor function:

      • The identifier of the property editor, i.e. the first parameter set for the BeginInlineEditor helper method in the HTML code of the editor’s partial view.
      • An anonymous object with an init property function that is executed whenever the given property editor is loaded. Use this function to run custom JavaScript that modifies the widget’s content or behavior based on the property value. The options parameter is an object that wraps the following properties:
        • editor – the DOM element encapsulating the editor
        • propertyName – the name of the related widget property
        • propertyValue – the value of the related widget property
        • localizationService – the localization service

    Executing scripts on inline editor events

    If you need to execute custom scripts on certain events, you can use additional property functions of the anonymous object defined when registering the inline editor.

    
    
    
     // Mandatory:
     init: function (options) { ... },
     // Optional:
     destroy: function (options) { ... },   // Executed when a user deletes a widget containing the inline editor
     dragStart: function (options) { ... }, // Executed when a user starts to drag a widget with the inline editor
     drop: function (options) { ... }       // Executed when a user successfully drops a widget with the inline editor
    
    
     

    Only the editor property is available via the options parameter in the destroy, dragStart, and drop propertyfunctions.

    • To notify the widget about changes in the property value, dispatch a custom event named updateProperty (within the init function that you registered for the editor).

      
      
      
        // Creates a custom event that notifies the widget about a change in the value of a property
        var event = new CustomEvent("updateProperty", {
            detail: {
                value: "newValue",
                name: propertyName
            }
        });
        editor.dispatchEvent(event);
      
      
        

      The updateProperty event’s data must contain the detail property, with the following values:

      • value – the new value of the widget property.
      • name – the name of the related widget property.
      • (Optional) refreshMarkup – bool value that determines whether the event automatically triggers a refresh of the widget’s entire markup from the server. True by default.
  4. Inline editor scripts on multilingual sites

    When using inline editors on a site with a multilingual user interface, you need to display and submit values (e.g. dates and numbers) in the correct format for the respective culture. Use the Date and Number objects to store and handle any date and number values within your inline editor scripts.

  5. Prepare any additional assets required by the editor, such as further JavaScript files, CSS stylesheets, images, etc.
  6. Edit the partial views of the widgets where you want to integrate the editor:
    1. Evaluate Context.Kentico().PageBuilder().EditMode to determine whether the widget is being displayed in edit mode (in the Kentico administration interface).

    2. Render the editor’s partial view in the appropriate location. Use the editor’s view model to pass the name and current value of the related widget property.

      
      
      
       @using Kentico.PageBuilder.Web.Mvc
       @using Kentico.Web.Mvc
      
       @if (Context.Kentico().PageBuilder().EditMode)
       {
           Html.RenderPartial("InlineEditors/CustomEditor", new CustomEditorModel
           {
               @* Use the nameof() operator to get the name of the widget property from the widget properties model *@
               PropertyName = nameof(CustomWidgetProperties.Number), 
               PropertyValue = Model.Number
           });
       }
      
      
       

The modified widgets now display your inline editor when working with page content in the Kentico administration interface. The appearance or functionality of widgets on the live site is affected by the property value configured through the inline editor.

Tip: If you need to create advanced property editors, consider implementing a modal dialog for your inline editor. This allows users to set the widget property’s value in a custom dialog, which is opened separately from the page builder interface.

Adding scripts and styles for inline editors

To add JavaScript and CSS styles required by your inline editors (including the script file containing the editor’s registration code), we recommend placing script and stylesheet files into sub-folders under the ~/Content/InlineEditors directory of your MVC project (you may need to create the InlineEditors directory). For example, use sub-folders that match the identifiers of individual inline editors, or a Shared sub-folder for assets used by multiple editors.

The system automatically creates bundles containing all .js and .css files located under ~/Content/InlineEditors. The bundles are then linked on all pages with page builder editable areas. Inline editor scripts and styles are only included in the administration bundles, which are linked when pages are displayed in Edit mode within the Pages application of Kentico.

The same bundles also contain script and stylesheet files added for widgets in the ~/Content/Widgets directory and sections in the ~/Content/Sections directory.

Notes – Scripts

  • Avoid linking or executing scripts directly within the views of inline editors. This could lead to duplicated scripts on pages containing multiple widgets that use the editor.

Notes – Styles

  • To prevent your editor styles from overriding the site’s styles, add a prefix to all CSS selectors used for the editor (for example custom-editor-class).
  • To prevent the editor’s styles from being overridden by the site’s styles, make all CSS selectors used for the editor as specific as possible. If necessary, use the !important rule for critical selectors.
  • Do not make any assumptions about the relative order of the source CSS in the resulting bundles – individual stylesheet files contained in the bundle may or may not precede each other.