-
Notifications
You must be signed in to change notification settings - Fork 162
Html Data Model Annotation
The [Html]
data annotation can be used to decorate a string property and provides a UI hint to the admin interface to display an html editor field. Cofoundry uses the TinyMCE editor to edit html content.
public class ExampleDataModel : ICustomEntityDataModel
{
[Html]
public string Content { get; set; }
}
The HtmlToolbarPreset
enum defines a few preset toolbars that can be used to control the set of toolbars that are rendered with the html editor:
- Headings: A style selector for h1-h3 headings and normal text.
- BasicFormatting: Buttons for bold, italic, underline, links
- AdvancedFormatting: Buttons for alignment, blockquote & lists, strikethrough and superscript/subscript.
- Media: Buttons to insert pictures, video
- Source: Edit html source and clear formatting
- Custom: Indicates the position to insert a custom toolbar (see below)
By default the Headings
and BasicFormatting
toolbars will be displayed. You can change the default by specifying them in the attribute constructor:
public class ExampleDataModel : ICustomEntityDataModel
{
[Html(HtmlToolbarPreset.BasicFormatting, HtmlToolbarPreset.Media, HtmlToolbarPreset.Source)]
public string Content { get; set; }
}
You can use the HtmlToolbarPreset.Custom
enum value to insert a custom toolbar, and use the CustomToolbar
property to define the buttons. You can see a full list of buttons in the TinyMCE toolbar documentation, and use the pipe separator to split toolbars.
public class ExampleDataModel : ICustomEntityDataModel
{
[Html(HtmlToolbarPreset.Custom, CustomToolbar = "undo redo | bold italic underline | link unlink")]
public string Content { get; set; }
}
You can set the editor heigh using the Rows
property, the same way you can with the [MultiLineText]
attribute. The default is 20.
public class ExampleDataModel : ICustomEntityDataModel
{
[Html(Rows=10)]
public string Content { get; set; }
}
You can completely control the configuration of TinyMCE by defining your own configuration. There ware two properties you can use to do this:
A type to use to determine any additional configuration options to apply to the html editor. This should be a class that inherits from IHtmlEditorConfigSource, which provides a .net code generated set of options.
public class ExampleHtmlEditorConfigSource : IHtmlEditorConfigSource
{
private static readonly Dictionary<string, object> _options = new Dictionary<string, object>()
{
{ "resize", false },
{ "browser_spellcheck", false }
};
public IDictionary<string, object> Create()
{
return _options;
}
}
public class ExampleDataModel : ICustomEntityDataModel
{
[Html(ConfigSource = typeof(ExampleHtmlEditorConfigSource))]
public string Content { get; set; }
}
You can also define a path to a json configuration file if you prefer to write your config in json.
public class ExampleDataModel : ICustomEntityDataModel
{
[Html(ConfigFilePath = "/content/html-editor-config.json")]
public string Content { get; set; }
}
When working with html you need to be aware of two things:
TinyMCE will try and clean up any html and remove what it thinks are invalid or dangerous elements. If you have users pasting in code from other sources e.g. internet or document files, then this can be useful to keep the input clean, however this can be a problem if your inputting raw html snippets or JavaScript content.
To prevent TinyMCE from cleaning up code you can override the config to allow all elements:
public class AllowAllElementsHtmlEditorConfigSource : IHtmlEditorConfigSource
{
public IDictionary<string, object> Create()
{
// configure TinyMCE to permit all elements
return new Dictionary<string, object>()
{
{ "valid_elements", "+*[*]" }
};
}
}
public class ExampleDataModel : ICustomEntityDataModel
{
[Html(ConfigSource = typeof(AllowAllElementsHtmlEditorConfigSource))]
public string Content { get; set; }
}
Cofoundry includes a sanitizer than you can use to clean up HTML when rendering content and prevent XSS attacks.
Take a look at the sanitizer documentation for more information.