Add a heading element that renders as h1-h6 tags.

This commit is contained in:
Bertrand Le Roy
2015-06-19 18:54:54 -07:00
parent 51409346cc
commit c1359f7a86
6 changed files with 110 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
using System.Collections.Generic;
using System.Linq;
using Orchard.Layouts.Elements;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.ViewModels;
using Orchard.Services;
namespace Orchard.Layouts.Drivers {
public class HeadingElementDriver : ElementDriver<Heading> {
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
public HeadingElementDriver(IEnumerable<IHtmlFilter> htmlFilters) {
_htmlFilters = htmlFilters;
}
protected override EditorResult OnBuildEditor(Heading element, ElementEditorContext context) {
var viewModel = new HeadingEditorViewModel {
Text = element.Content,
Level = element.Level
};
var editor = context.ShapeFactory.EditorTemplate(TemplateName: "Elements.Heading", Model: viewModel);
if (context.Updater != null) {
context.Updater.TryUpdateModel(viewModel, context.Prefix, null, null);
element.Content = viewModel.Text;
element.Level = viewModel.Level;
}
return Editor(context, editor);
}
protected override void OnDisplaying(Heading element, ElementDisplayContext context) {
var text = element.Content;
var flavor = "html";
var processedText = ApplyHtmlFilters(text, flavor);
context.ElementShape.ProcessedText = processedText;
context.ElementShape.Level = element.Level;
}
private string ApplyHtmlFilters(string content, string flavor) {
return _htmlFilters.Aggregate(content, (t, filter) => filter.ProcessContent(t, flavor));
}
}
}

View File

@@ -0,0 +1,24 @@
using Orchard.Layouts.Helpers;
using Orchard.Localization;
namespace Orchard.Layouts.Elements {
public class Heading : ContentElement {
public override string Category {
get { return "Content"; }
}
public override LocalizedString DisplayText {
get { return T("Heading h1-h6"); }
}
public override string ToolboxIcon {
get { return "\uf1dc"; }
}
public int Level {
get { return this.Retrieve(h => h.Level); }
set { this.Store(h => h.Level, value);}
}
}
}

View File

@@ -313,10 +313,12 @@
<Compile Include="Controllers\BlueprintAdminController.cs" />
<Compile Include="Controllers\TemplateController.cs" />
<Compile Include="Drivers\CanvasElementDriver.cs" />
<Compile Include="Drivers\HeadingElementDriver.cs" />
<Compile Include="Drivers\ShapeElementDriver.cs" />
<Compile Include="Drivers\BreakElementDriver.cs" />
<Compile Include="Elements\Canvas.cs" />
<Compile Include="Elements\ContentElement.cs" />
<Compile Include="Elements\Heading.cs" />
<Compile Include="Elements\Shape.cs" />
<Compile Include="Elements\Break.cs" />
<Compile Include="Filters\TokensFilter.cs" />
@@ -408,6 +410,7 @@
<Compile Include="ViewModels\HtmlEditorViewModel.cs" />
<Compile Include="ViewModels\LayoutEditor.cs" />
<Compile Include="ViewModels\LayoutEditorPropertiesViewModel.cs" />
<Compile Include="ViewModels\HeadingEditorViewModel.cs" />
<Compile Include="ViewModels\VectorImageEditorViewModel.cs" />
<Compile Include="ViewModels\ImageEditorViewModel.cs" />
<Compile Include="ViewModels\MarkdownEditorViewModel.cs" />
@@ -539,6 +542,12 @@
<Content Include="Views\Parts.Layout.Recursive.cshtml" />
<Content Include="Views\Parts.Layout.Summary.Recursive.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Elements\Heading.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\EditorTemplates\Elements.Heading.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@@ -0,0 +1,6 @@
namespace Orchard.Layouts.ViewModels {
public class HeadingEditorViewModel {
public string Text { get; set; }
public int Level { get; set; }
}
}

View File

@@ -0,0 +1,20 @@
@model Orchard.Layouts.ViewModels.HeadingEditorViewModel
@{
Script.Include("AutoFocus.js");
}
<fieldset>
<legend>@T("Level")</legend>
<ul>
@for (var i = 1; i <= 6; i++) {
<li>
@Html.RadioButtonFor(m => m.Level, i, new {id = "h" + i})
<label for="@("h" + i)" class="forcheckbox">@T("&lt;h{0}/&gt;", i)</label>
</li>
}
</ul>
</fieldset>
<fieldset class="autofocus">
@Html.LabelFor(m => m.Text, T("Text"))
@Html.TextBoxFor(m => m.Text, new { @class = "text large tokenized", autofocus = "autofocus" })
@Html.Hint(T("Note: HTML markup will be rendered unencoded."))
</fieldset>

View File

@@ -0,0 +1,6 @@
@using Orchard.Layouts.Helpers
@{
var tagBuilder = TagBuilderExtensions.CreateElementTagBuilder(Model, "h" + (Model.Level >= 1 && Model.Level <= 6 ? Model.Level : 1));
tagBuilder.InnerHtml = Model.ProcessedText;
}
@tagBuilder.ToHtmlString()