Skip to content

Instantly share code, notes, and snippets.

@torjue
Last active December 29, 2016 03:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save torjue/1ad223a637a024859428 to your computer and use it in GitHub Desktop.
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using Web.Business.TinyMce.EditorSettings;
namespace Web.Models.Pages
{
[ContentType(
DisplayName = "Start Page",
GUID = "D20D4771-ED6E-4C8F-A106-921898B5F681"
)]
public class StartPage : PageData
{
[Display(
Name = "Main body",
Description = "",
GroupName = SystemTabNames.Content,
Order = 200)]
[Searchable]
[CultureSpecific]
[TinyMceSettings(typeof(StandardTinyMceSettings))]
public virtual XhtmlString MainBody { get; set; }
}
}
using System;
namespace Web.Business.TinyMce.EditorSettings
{
public interface ITinyMceSettings
{
string DisplayName { get; set; }
Guid Id { get; set; }
string ContentCss { get; set; }
string[][] Toolbars { get; set; }
string[] NonVisualPlugins { get; set; }
}
}
using System;
namespace Web.Business.TinyMce.EditorSettings
{
public class StandardTinyMceSettings : ITinyMceSettings
{
public StandardTinyMceSettings()
{
DisplayName = "Some TinyMce Settings";
Id = new Guid("2235405F-A998-4371-81AE-F45F8899A03A");
ContentCss = null;
Toolbars = new []
{
new []
{
"bold", "italic", "underline", "strikethrough", "separator",
"justifyleft", "justifycenter", "justifyright", "separator",
"epilink", "unlink"
}
};
NonVisualPlugins = null;
}
public string DisplayName { get; set; }
public Guid Id { get; set; }
public string ContentCss { get; set; }
public string[][] Toolbars { get; set; }
public string[] NonVisualPlugins { get; set; }
}
}
using System;
namespace Web.Business.TinyMce.EditorSettings
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class TinyMceSettingsAttribute : Attribute
{
public TinyMceSettingsAttribute(Type settingsType)
{
SettingsType = settingsType;
}
public Type SettingsType { get; set; }
}
}
using System;
using System.Linq;
using System.Reflection;
using EPiServer.Core;
using EPiServer.Core.PropertySettings;
using EPiServer.DataAbstraction;
using EPiServer.Editor.TinyMCE;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using Web.Business.TinyMce.EditorSettings;
namespace Web
{
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
[InitializableModule]
public class TinyMceSettingsInitialization : IInitializableModule
{
private bool _initialized;
IPropertySettingsRepository _propertySettingsRepository;
IContentTypeRepository _contentTypeRepository;
IPropertyDefinitionRepository _propertyDefinitionRepository;
public void Initialize(InitializationEngine context)
{
if (_initialized) return;
_propertySettingsRepository = ServiceLocator.Current.GetInstance<IPropertySettingsRepository>();
_contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
_propertyDefinitionRepository = ServiceLocator.Current.GetInstance<IPropertyDefinitionRepository>();
var allContentTypes = _contentTypeRepository.List();
foreach (var contentType in allContentTypes)
{
if (contentType == null || contentType.ModelType == null)
{
continue;
}
var properties = contentType.ModelType.GetProperties();
foreach (var propertyInfo in properties)
{
if (propertyInfo.PropertyType != typeof (XhtmlString))
{
continue;
}
var settings = GetSettingsFromAttrubte(propertyInfo);
if (settings == null)
{
continue;
}
CreateOrUpdateSettingsContainer(settings);
var property = contentType.PropertyDefinitions.First(x => x.Name == propertyInfo.Name);
SaveSettingsToProperty(property, settings);
}
}
_initialized = true;
}
public ITinyMceSettings GetSettingsFromAttrubte(PropertyInfo propertyInfo)
{
var settingsAttribute = (TinyMceSettingsAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(TinyMceSettingsAttribute));
if (settingsAttribute == null) return null;
var settingsType = settingsAttribute.SettingsType;
var settings = Activator.CreateInstance(settingsType) as ITinyMceSettings;
if (settings == null) throw new Exception("Defined TinyMceSettings type is not implementing ITinyMceSettings");
return settings;
}
public void CreateOrUpdateSettingsContainer(ITinyMceSettings settings)
{
PropertySettingsContainer container;
_propertySettingsRepository.TryGetContainer(settings.Id, out container);
container = container ?? new PropertySettingsContainer(settings.Id);
var wrapper = container.GetSetting(typeof(TinyMCESettings));
wrapper = wrapper ?? new PropertySettingsWrapper();
var propertySettings = new TinyMCESettings();
foreach (var toolbarRow in settings.Toolbars)
{
propertySettings.ToolbarRows.Add(new ToolbarRow(toolbarRow));
}
propertySettings.NonVisualPlugins = settings.NonVisualPlugins ?? new string[] { };
propertySettings.ContentCss = settings.ContentCss ?? string.Empty;
wrapper.PropertySettings = propertySettings;
wrapper.IsGlobal = true;
wrapper.IsDefault = false;
wrapper.DisplayName = settings.DisplayName;
container.AddSettings(wrapper);
_propertySettingsRepository.Save(container);
}
private void SaveSettingsToProperty(PropertyDefinition property, ITinyMceSettings settings)
{
var writableProperty = property.CreateWritableClone();
writableProperty.SettingsID = settings.Id;
_propertyDefinitionRepository.Save(writableProperty);
}
public void Uninitialize(InitializationEngine context) { }
public void Preload(string[] parameters) { }
}
}
@ocrenaka
Copy link

var propertySettings = new TinyMCESettings();

foreach (var toolbarRow in settings.Toolbars)
{
      propertySettings.ToolbarRows.Add(new ToolbarRow(toolbarRow));
}

It has performance issue, each time we run initialize module it will insert new configuration records for TinyMCE BigTable. And it makes the EPIServer/shell/Stores/component in the editor interface call a lot of time to database to load TinceMCE settings.

We can fix it to re-use TinyMCESettings and clear up ToolbarRows before we init

var propertySettings = wrapper.PropertySettings as TinyMCESettings;
if (propertySettings == null)
{
          propertySettings = new TinyMCESettings();
}
propertySettings.ToolbarRows.Clear();
foreach (var toolbarRow in this.Toolbars)
{
       propertySettings.ToolbarRows.Add(new ToolbarRow(toolbarRow));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment