<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><language>en</language><title>Blog posts by Torjus Eidet</title> <link>https://world.optimizely.com/blogs/Torjus-Eidet/</link><description></description><ttl>60</ttl><generator>Optimizely World</generator><item> <title>Strongly typed TinyMCE settings</title>            <link>https://world.optimizely.com/blogs/Torjus-Eidet/Dates/2014/4/Strongly-typed-TinyMCE-settings/</link>            <description>&lt;p&gt;One of the things that bugs me when migrating between environments is to set up the TinyMCE settings. Wouldn’t it be great if we could just define it in code, like we do with our content types? It’s actually not that hard to accomplish.&lt;/p&gt;  &lt;p&gt;With this solution, all you have to do is to decorate you XHtmlString properties with an attribute, TinyMceSettings.&lt;/p&gt;  &lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;   &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; ExamplePage : PageData
   {
       [Display(Name = &lt;span class=&quot;str&quot;&gt;&amp;quot;Main body&amp;quot;&lt;/span&gt;)]
       [TinyMceSettings(&lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt;(StandardTinyMceSettings))]
       &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;virtual&lt;/span&gt; XhtmlString MainBody { get; set; }
   }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This attribute takes the type of a settings class as it parameter.&lt;/p&gt;

&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;[AttributeUsage(AttributeTargets.Property, AllowMultiple = &lt;span class=&quot;kwrd&quot;&gt;false&lt;/span&gt;)]
   &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; TinyMceSettingsAttribute : Attribute
   {
       &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; TinyMceSettingsAttribute(Type settingsType)
       {
           SettingsType = settingsType;
       }
       &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; Type SettingsType { get; set; }
   }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A settings class looks like this:&lt;/p&gt;

&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;   &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; StandardTinyMceSettings : ITinyMceSettings
   {
       &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; StandardTinyMceSettings()
       {
           DisplayName = &lt;span class=&quot;str&quot;&gt;&amp;quot;Some TinyMce Settings&amp;quot;&lt;/span&gt;;
           Id = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Guid(&lt;span class=&quot;str&quot;&gt;&amp;quot;2235405F-A998-4371-81AE-F45F8899A03A&amp;quot;&lt;/span&gt;);
           ContentCss = &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;;
           Toolbars = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; []
           {
               &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; []
               {
                   &lt;span class=&quot;str&quot;&gt;&amp;quot;bold&amp;quot;&lt;/span&gt;, &lt;span class=&quot;str&quot;&gt;&amp;quot;italic&amp;quot;&lt;/span&gt;, &lt;span class=&quot;str&quot;&gt;&amp;quot;underline&amp;quot;&lt;/span&gt;, &lt;span class=&quot;str&quot;&gt;&amp;quot;strikethrough&amp;quot;&lt;/span&gt;, &lt;span class=&quot;str&quot;&gt;&amp;quot;separator&amp;quot;&lt;/span&gt;,
                   &lt;span class=&quot;str&quot;&gt;&amp;quot;justifyleft&amp;quot;&lt;/span&gt;, &lt;span class=&quot;str&quot;&gt;&amp;quot;justifycenter&amp;quot;&lt;/span&gt;, &lt;span class=&quot;str&quot;&gt;&amp;quot;justifyright&amp;quot;&lt;/span&gt;, &lt;span class=&quot;str&quot;&gt;&amp;quot;separator&amp;quot;&lt;/span&gt;,
                   &lt;span class=&quot;str&quot;&gt;&amp;quot;epilink&amp;quot;&lt;/span&gt;, &lt;span class=&quot;str&quot;&gt;&amp;quot;unlink&amp;quot;&lt;/span&gt;
               }
           };
           NonVisualPlugins = &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;;
       }
 
       &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; DisplayName { get; set; }
       &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; Guid Id { get; set; }
       &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; ContentCss { get; set; }
       &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[][] Toolbars { get; set; }
       &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] NonVisualPlugins { get; set; }
   }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, you can define toolbars, display name, non visual plugins, editor css file path and so on. In order for this solution to work, you will need to implement the interface ITinyMceSettings:&lt;/p&gt;

&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;   &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;interface&lt;/span&gt; ITinyMceSettings
   {
       &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; DisplayName { get; set; }
       Guid Id { get; set; }
       &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; ContentCss { get; set; }
       &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[][] Toolbars { get; set; }
       &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] NonVisualPlugins { get; set; }
   }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In order to hook things up you will need to include the following initializable module:&lt;/p&gt;

&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;    [ModuleDependency(&lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt;(EPiServer.Web.InitializationModule))]
    [InitializableModule]
    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; TinyMceSettingsInitialization : IInitializableModule
    {
        &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; _initialized;
        IPropertySettingsRepository _propertySettingsRepository;
        IContentTypeRepository _contentTypeRepository;
        IPropertyDefinitionRepository _propertyDefinitionRepository;
 
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Initialize(InitializationEngine context)
        {
            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (_initialized) &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;
            _propertySettingsRepository = ServiceLocator.Current.GetInstance&amp;lt;IPropertySettingsRepository&amp;gt;();
            _contentTypeRepository = ServiceLocator.Current.GetInstance&amp;lt;IContentTypeRepository&amp;gt;();
            _propertyDefinitionRepository = ServiceLocator.Current.GetInstance&amp;lt;IPropertyDefinitionRepository&amp;gt;();
 
 
            var allContentTypes = _contentTypeRepository.List();
            &lt;span class=&quot;kwrd&quot;&gt;foreach&lt;/span&gt; (var contentType &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; allContentTypes)
            {
                &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (contentType == &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt; || contentType.ModelType == &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)
                {
                    &lt;span class=&quot;kwrd&quot;&gt;continue&lt;/span&gt;;
                }
 
                var properties = contentType.ModelType.GetProperties();
                &lt;span class=&quot;kwrd&quot;&gt;foreach&lt;/span&gt; (var propertyInfo &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; properties)
                {
                    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (propertyInfo.PropertyType != &lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt; (XhtmlString))
                    {
                        &lt;span class=&quot;kwrd&quot;&gt;continue&lt;/span&gt;;
                    }
 
                    var settings = GetSettingsFromAttrubte(propertyInfo);
                    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (settings == &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)
                    {
                        &lt;span class=&quot;kwrd&quot;&gt;continue&lt;/span&gt;;
                    }
 
                    CreateOrUpdateSettingsContainer(settings);
 
                    var property = contentType.PropertyDefinitions.First(x =&amp;gt; x.Name == propertyInfo.Name);
 
                    SaveSettingsToProperty(property, settings);
                }
            }
 
            _initialized = &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;;
        }
 
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; ITinyMceSettings GetSettingsFromAttrubte(PropertyInfo propertyInfo)
        {
            var settingsAttribute = (TinyMceSettingsAttribute)Attribute.GetCustomAttribute(propertyInfo, &lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt;(TinyMceSettingsAttribute));
            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (settingsAttribute == &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;) &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;;
 
            var settingsType = settingsAttribute.SettingsType;
            var settings = Activator.CreateInstance(settingsType) &lt;span class=&quot;kwrd&quot;&gt;as&lt;/span&gt; ITinyMceSettings;
            &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (settings == &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;) &lt;span class=&quot;kwrd&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Exception(&lt;span class=&quot;str&quot;&gt;&amp;quot;Defined TinyMceSettings type is not implementing ITinyMceSettings&amp;quot;&lt;/span&gt;);
 
            &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; settings;
        }
 
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; CreateOrUpdateSettingsContainer(ITinyMceSettings settings)
        {
            PropertySettingsContainer container;
            _propertySettingsRepository.TryGetContainer(settings.Id, &lt;span class=&quot;kwrd&quot;&gt;out&lt;/span&gt; container);
            container = container ?? &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; PropertySettingsContainer(settings.Id);
 
            var wrapper = container.GetSetting(&lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt;(TinyMCESettings));
            wrapper = wrapper ?? &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; PropertySettingsWrapper();
 
            var propertySettings = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; TinyMCESettings();
            &lt;span class=&quot;kwrd&quot;&gt;foreach&lt;/span&gt; (var toolbarRow &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; settings.Toolbars)
            {
                propertySettings.ToolbarRows.Add(&lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ToolbarRow(toolbarRow));
            }
            propertySettings.NonVisualPlugins = settings.NonVisualPlugins ?? &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] { };
            propertySettings.ContentCss = settings.ContentCss ?? &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;.Empty;
 
            wrapper.PropertySettings = propertySettings;
            wrapper.IsGlobal = &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;;
            wrapper.IsDefault = &lt;span class=&quot;kwrd&quot;&gt;false&lt;/span&gt;;
            wrapper.DisplayName = settings.DisplayName;
 
            container.AddSettings(wrapper);
 
            _propertySettingsRepository.Save(container);
        }
        
        &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; SaveSettingsToProperty(PropertyDefinition property, ITinyMceSettings settings)
        {
            var writableProperty = property.CreateWritableClone();
            writableProperty.SettingsID = settings.Id;
            _propertyDefinitionRepository.Save(writableProperty);
        }
 
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Uninitialize(InitializationEngine context) { }
        &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Preload(&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;[] parameters)  { }
    }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is just my first thoughts around such a system. This might not be the best way to solve this, so if you have any suggestions please feel free to share them with me. My plans are to turn this into a nuget package, but I’m hoping for some feedback first.&lt;/p&gt;

&lt;p&gt;Code: &lt;a href=&quot;https://gist.github.com/torjue/1ad223a637a024859428&quot;&gt;https://gist.github.com/torjue/1ad223a637a024859428&lt;/a&gt;&lt;/p&gt;
&lt;style type=&quot;text/css&quot;&gt;


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &quot;Courier New&quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;</description>            <guid>https://world.optimizely.com/blogs/Torjus-Eidet/Dates/2014/4/Strongly-typed-TinyMCE-settings/</guid>            <pubDate>Tue, 08 Apr 2014 21:39:17 GMT</pubDate>           <category>Blog post</category></item></channel>
</rss>