Last week I was investigating the editor plugin capabilities of EPiServer CMS R2. My goal was to plug into the context menu with my own submenu, you know the kind of menu item that opens another list of options. The lack of documentation in the SDK and elsewhere made the task a bigger headache than it should have. So here’s an example of how to create a context menu item with a submenu, or subitems if you like.

submenu

First we need a “root” item from our context menu. The plugin attribute has the parameter “SubMenuName”, the name we enter here is important to the subitems.

    [EditorPlugIn(Usage = ToolUsage.ContextMenu,
        DisplayName = "ImageAlign",
        SubMenuName = "ImageAlign",
        LanguageKey = "imagealignroot",
        MenuGroup = "CssFormatParagraph")]
    public class AlignImagePlugin : ToolBase, IInitializableTool
    {
        void IInitializableTool.Initialize(HtmlEditor editor)
        {
            // Set the script that check if the menu item is enabled
            ClientSideEnabled = "IsAlignImageAllowed(this)";
        }
    }

To create the subitems use the name we gave the “SubMenuName” parameter for our parent menu item for the “MenuName”. Other than that you go about implementing the subitem just as you would a normal context menu item.

    [EditorPlugIn(Usage = ToolUsage.ContextMenu,
        DisplayName = "Align left",
        MenuName = "ImageAlign",
        LanguageKey = "imagealignleft",
        MenuIndex = 0)]
    public class AlignImageLeftPlugin : ToolBase, IInitializableTool
    {
        void IInitializableTool.Initialize(HtmlEditor editor)
        {
            ClientSideOnClick = string.Format("AlignImage('{0}', 'left');", editor.ClientID);
        }
    }

To make the plugin easy to distribute and deploy, embed the javascript into the dll and register it on the page like this in the root menu item:

        /// CreateChildControls because Page is null in Initialize and we want to register the
        /// javascript file.
        ///
        protected override void CreateChildControls()
        {
            if (Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "CMSEDITOR_ImageAlign") == false)
            {
                Page.ClientScript.RegisterClientScriptInclude("CMSEDITOR_ImageAlign",
                             Page.ClientScript.GetWebResourceUrl(this.GetType(), "CMSEditorPlugin.Editor.Javascript.AlignImage.js"));
            }
 
            base.CreateChildControls();
        }

Good luck on creating your editor plugins! Here’s the full demo project. The project format is Visual Studio 2008.

Related posts