Skip to content

Commit d2df939

Browse files
committed
simplified the edit link
removed all of the styling options and left a bool for use default styles or not updated readme to reflect changes
1 parent dea34f0 commit d2df939

File tree

5 files changed

+137
-232
lines changed

5 files changed

+137
-232
lines changed

Our.Umbraco.TagHelpers/EditLinkTagHelper.cs

Lines changed: 79 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Microsoft.AspNetCore.Razor.TagHelpers;
2-
using Our.Umbraco.TagHelpers.Enums;
2+
using Our.Umbraco.TagHelpers.Extensions;
33
using Our.Umbraco.TagHelpers.Services;
4-
using System.Linq;
54
using System.Text;
65

76
namespace Our.Umbraco.TagHelpers
@@ -11,7 +10,7 @@ namespace Our.Umbraco.TagHelpers
1110
/// then an edit link will display on the front end of the site. This will
1211
/// take you to the umbraco backoffice to edit the current page.
1312
/// </summary>
14-
[HtmlTargetElement("our-editlink")]
13+
[HtmlTargetElement("our-edit-link")]
1514
public class EditLinkTagHelper : TagHelper
1615
{
1716
private readonly IBackofficeUserAccessor _backofficeUserAccessor;
@@ -28,150 +27,99 @@ public EditLinkTagHelper(IBackofficeUserAccessor backofficeUserAccessor)
2827
public int ContentId { get; set; } = 0;
2928

3029
/// <summary>
31-
/// An enum to say which corner of the screen you would like
32-
/// the edit link to show. Defaults to bottom left.
33-
/// </summary>
34-
[HtmlAttributeName("position")]
35-
public EditLinkPosition Position { get; set; } = EditLinkPosition.BottomLeft;
36-
37-
/// <summary>
38-
/// A bool to say whether or not you would like to apply the inline link styles. Defaults to true.
39-
/// </summary>
40-
[HtmlAttributeName("apply-inline-link-styles")]
41-
public bool ApplyInlineLinkStyles { get; set; } = true;
42-
43-
/// <summary>
44-
/// The 'Edit' text in the link. Defaults to "Edit"
45-
/// </summary>
46-
[HtmlAttributeName("edit-message")]
47-
public string EditMessage { get; set; } = "Edit";
48-
49-
/// <summary>
50-
/// The CSS colour of the link text. Defaults to #fff
51-
/// </summary>
52-
[HtmlAttributeName("link-colour")]
53-
public string LinkColour { get; set; } = "#fff";
54-
55-
/// <summary>
56-
/// The CSS colour of the link background. Defaults to "#1b264f"
57-
/// </summary>
58-
[HtmlAttributeName("link-background-colour")]
59-
public string LinkBackgroundColour { get; set; } = "#1b264f";
60-
61-
/// <summary>
62-
/// The font size of the link text in pixels. Defaults to 16
63-
/// </summary>
64-
[HtmlAttributeName("font-size")]
65-
public int FontSize { get; set; } = 16;
66-
67-
/// <summary>
68-
/// The padding around the link in pixels. Defaults to 10
69-
/// </summary>
70-
[HtmlAttributeName("link-padding")]
71-
public int LinkPadding { get; set; } = 10;
72-
73-
/// <summary>
74-
/// The border radius of the link in pixels. Defaults to 6
75-
/// </summary>
76-
[HtmlAttributeName("border-radius")]
77-
public int BorderRadius { get; set; } = 6;
78-
79-
/// <summary>
80-
/// The class you would like to add to the link. Defaults to "edit-link-inner"
30+
/// Override the umbraco edit content url if yours is different
8131
/// </summary>
82-
[HtmlAttributeName("link-class-name")]
83-
public string LinkClassName { get; set; } = "edit-link-inner";
32+
[HtmlAttributeName("edit-url")]
33+
public string EditUrl { get; set; } = "/umbraco#/content/content/edit/";
8434

8535
/// <summary>
86-
/// Whether or not you would like to apply the inline styles for the outer element. Defaults to true
36+
/// Override the text of the link
8737
/// </summary>
88-
[HtmlAttributeName("apply-inline-outer-element-styles")]
89-
public bool ApplyInlineOuterElementStyles { get; set; } = true;
38+
[HtmlAttributeName("text")]
39+
public string Text { get; set; } = "Edit";
9040

9141
/// <summary>
92-
/// The margin around the link. Defaults to 10
42+
/// A boolean to say whether or not you would like to use the default styling.
9343
/// </summary>
94-
[HtmlAttributeName("margin")]
95-
public int Margin { get; set; } = 10;
44+
[HtmlAttributeName("use-default-styles")]
45+
public bool UseDefaultStyles { get; set; } = false;
9646

9747
/// <summary>
98-
/// The zindex of this link block. Defaults to 10000
48+
/// Set the id attribute if you want
9949
/// </summary>
100-
[HtmlAttributeName("zindex")]
101-
public int Zindex { get; set; } = 10000;
50+
[HtmlAttributeName("id")]
51+
public string Id { get; set; } = "";
10252

10353
/// <summary>
104-
/// Override the umbraco edit content url if yours is different
54+
/// The class attribute for the link
10555
/// </summary>
106-
[HtmlAttributeName("umbraco-edit-content-url")]
107-
public string UmbracoEditContentUrl { get; set; } = "/umbraco#/content/content/edit/";
56+
[HtmlAttributeName("class")]
57+
public string Class { get; set; } = "";
10858

10959
/// <summary>
110-
/// The class name for the outer element. Defaults to "edit-link-outer"
60+
/// Set the target attribute if you want. Defaults to _blank
11161
/// </summary>
112-
[HtmlAttributeName("outer-class-name")]
113-
public string OuterClassName { get; set; } = "edit-link-outer";
62+
[HtmlAttributeName("target")]
63+
public string Target { get; set; } = "_blank";
11464

11565
/// <summary>
116-
/// The CSS position for the outer element. Defaults to "fixed"
66+
/// Add some inline styles to the link if you want
11767
/// </summary>
118-
[HtmlAttributeName("outer-position")]
119-
public string OuterPosition { get; set; } = "fixed";
68+
[HtmlAttributeName("style")]
69+
public string Style { get; set; } = "";
12070

12171
/// <summary>
122-
/// The CSS position for the link. Defaults to "absolute"
72+
/// Set the title attribute if you want
12373
/// </summary>
124-
[HtmlAttributeName("link-position")]
125-
public string LinkPosition { get; set; } = "absolute";
126-
74+
[HtmlAttributeName("title")]
75+
public string Title { get; set; } = "";
12776

12877
public override void Process(TagHelperContext context, TagHelperOutput output)
12978
{
79+
//don't output the tag name as it's not valid HTML
13080
output.TagName = "";
13181

132-
if(_backofficeUserAccessor.BackofficeUser != null
133-
&& _backofficeUserAccessor.BackofficeUser.AuthenticationType
134-
== global::Umbraco.Cms.Core.Constants.Security.BackOfficeAuthenticationType
135-
&& _backofficeUserAccessor.BackofficeUser.Claims != null
136-
&& _backofficeUserAccessor.BackofficeUser.Claims.Any(x =>
137-
x.Type == global::Umbraco.Cms.Core.Constants.Security.AllowedApplicationsClaimType
138-
&& x.Value == global::Umbraco.Cms.Core.Constants.Conventions.PermissionCategories.ContentCategory))
82+
//check if the user is logged in to the backoffice
83+
//and they have access to the content section
84+
if(_backofficeUserAccessor.BackofficeUser.IsAllowedToSeeEditLink())
13985
{
140-
var editLink = $"/umbraco#/content/content/edit/{ContentId}";
86+
var editLinkUrl = $"{EditUrl}{ContentId}";
14187

88+
StringBuilder editLinkCode = new StringBuilder();
89+
SetAttributeValue("style", Style, editLinkCode);
14290

91+
if (UseDefaultStyles)
92+
{
93+
//Render the outer div with some inline styles
94+
editLinkCode.Append($"<div");
95+
SetAttributeValue("style", GetOuterElementStyles(), editLinkCode);
96+
editLinkCode.Append($">");
97+
}
14398

144-
StringBuilder editLinkCode = new StringBuilder();
99+
//Add the opening tag of the link
100+
editLinkCode.Append($"<a");
145101

146-
//Render the starting outer div
147-
editLinkCode.Append($"<div");
148-
editLinkCode.Append($" class=\"{OuterClassName}\"");
102+
SetAttributeValue("href", editLinkUrl, editLinkCode);
103+
SetAttributeValue("id", Id, editLinkCode);
104+
SetAttributeValue("class", Class, editLinkCode);
105+
SetAttributeValue("target", Target, editLinkCode);
106+
SetAttributeValue("title", Title, editLinkCode);
149107

150-
//Render the inline styles for the outer div
151-
if (ApplyInlineOuterElementStyles)
108+
if (UseDefaultStyles)
152109
{
153-
string outerStyles = GetOuterElementStyles(OuterPosition, Position, Margin, Zindex, LinkPadding);
154-
editLinkCode.Append($" style=\"{outerStyles}\"");
110+
SetAttributeValue("style", GetLinkStyles(), editLinkCode);
155111
}
156-
editLinkCode.Append($">");
157112

158-
//Render the link
159-
editLinkCode.Append($"<a href=\"{UmbracoEditContentUrl}{ContentId}\"");
160-
editLinkCode.Append($" target=\"_blank\"");
161-
editLinkCode.Append($" class=\"{LinkClassName}\"");
113+
//Add the link text and close the link tag
114+
editLinkCode.Append($">{Text}</a>");
162115

163-
//Render the inline styles for the link
164-
if (ApplyInlineLinkStyles)
116+
if (UseDefaultStyles)
165117
{
166-
string linkStyles = GetLinkStyles(LinkColour, LinkBackgroundColour, LinkPadding, FontSize, BorderRadius);
167-
editLinkCode.Append($"style=\"{linkStyles}\"");
118+
//Add the closing outer div
119+
editLinkCode.Append($"</div>");
168120
}
169121

170-
//Render the link text and closing tag
171-
editLinkCode.Append($">{EditMessage}</a>");
172-
173-
//Render the closing outer div
174-
editLinkCode.Append($"</div>");
122+
//Set the content of the tag helper
175123
output.Content.SetHtmlContent(editLinkCode.ToString());
176124
return;
177125
}
@@ -182,6 +130,14 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
182130
}
183131
}
184132

133+
private static void SetAttributeValue(string attributeName, string attributeValue, StringBuilder editLinkCode)
134+
{
135+
if (!string.IsNullOrWhiteSpace(attributeValue))
136+
{
137+
editLinkCode.Append($" {attributeName}=\"{attributeValue}\"");
138+
}
139+
}
140+
185141
/// <summary>
186142
/// Helper method to get the link styles
187143
/// </summary>
@@ -191,8 +147,12 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
191147
/// <param name="fontSize">The font size of the link text</param>
192148
/// <param name="borderRadius">The border radius of the link</param>
193149
/// <returns></returns>
194-
private static string GetLinkStyles(string linkColour, string linkBackgroundColour,
195-
int linkPadding, int fontSize, int borderRadius)
150+
private static string GetLinkStyles(
151+
string linkColour = "#ffffff",
152+
string linkBackgroundColour = "#1b264f",
153+
int linkPadding = 10,
154+
int fontSize = 16,
155+
int borderRadius = 6)
196156
{
197157
StringBuilder linkStyles = new StringBuilder();
198158
linkStyles.Append($"color:{linkColour};");
@@ -207,40 +167,25 @@ private static string GetLinkStyles(string linkColour, string linkBackgroundColo
207167
/// Helper method to get the outer element styles
208168
/// </summary>
209169
/// <param name="outerPosition">The CSS position of the outer element</param>
210-
/// <param name="position">The CSS position of the link element</param>
211170
/// <param name="margin">The margin around the outer element</param>
212-
/// <param name="zindex">The zindex of the outer element</param>
171+
/// <param name="zindex">The z-index of the outer element</param>
213172
/// <param name="linkPadding">The padding around the link</param>
214173
/// <returns></returns>
215-
private static string GetOuterElementStyles(string outerPosition, EditLinkPosition position,
216-
int margin, int zindex, int linkPadding)
174+
private static string GetOuterElementStyles(
175+
string outerPosition = "fixed",
176+
int margin = 10,
177+
int zindex = 10000,
178+
int linkPadding = 10)
217179
{
218-
linkPadding = linkPadding / 2;
180+
linkPadding /= 2;
219181

220-
StringBuilder outerStyles = new StringBuilder();
182+
var outerStyles = new StringBuilder();
221183

222184
outerStyles.Append("display:block;");
223185
if (outerPosition == "fixed")
224186
{
225-
switch (position)
226-
{
227-
case EditLinkPosition.TopLeft:
228-
outerStyles.Append($"top:{margin + linkPadding}px;");
229-
outerStyles.Append($"left:{margin}px;");
230-
break;
231-
case EditLinkPosition.TopRight:
232-
outerStyles.Append($"top:{margin + linkPadding}px;");
233-
outerStyles.Append($"right:{margin}px;");
234-
break;
235-
case EditLinkPosition.BottomRight:
236-
outerStyles.Append($"bottom:{margin + linkPadding}px;");
237-
outerStyles.Append($"right:{margin}px;");
238-
break;
239-
case EditLinkPosition.BottomLeft:
240-
outerStyles.Append($"bottom:{margin + linkPadding}px;");
241-
outerStyles.Append($"left:{margin}px;");
242-
break;
243-
}
187+
outerStyles.Append($"bottom:{margin + linkPadding}px;");
188+
outerStyles.Append($"left:{margin}px;");
244189
}
245190

246191
outerStyles.Append($"z-index:{zindex};");

Our.Umbraco.TagHelpers/Enums/EditLinkPosition.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Linq;
2+
using System.Security.Claims;
3+
using Umbraco.Cms.Core;
4+
5+
namespace Our.Umbraco.TagHelpers.Extensions
6+
{
7+
public static class ClaimsIdentityExtensions
8+
{
9+
public static bool IsAllowedToSeeEditLink(this ClaimsIdentity identity)
10+
{
11+
return IsLoggedIntoUmbraco(identity) && HasAccessToContentSection(identity);
12+
}
13+
14+
public static bool IsLoggedIntoUmbraco(this ClaimsIdentity identity)
15+
{
16+
return identity?.AuthenticationType != null
17+
&& identity.AuthenticationType == Constants.Security.BackOfficeAuthenticationType;
18+
}
19+
20+
public static bool HasAccessToContentSection(this ClaimsIdentity identity)
21+
{
22+
return identity?.Claims != null && identity.Claims.Any(x =>
23+
x.Type == Constants.Security.AllowedApplicationsClaimType
24+
&& x.Value == Constants.Conventions.PermissionCategories.ContentCategory);
25+
}
26+
}
27+
}

Our.Umbraco.TagHelpers/Our.Umbraco.TagHelpers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
</ItemGroup>
4444

4545
<ItemGroup>
46-
<None Include="TagHelperLogo.png" Pack="true" PackagePath="\"/>
46+
<None Include="TagHelperLogo.png" Pack="true" PackagePath="\" />
4747
</ItemGroup>
4848

4949
</Project>

0 commit comments

Comments
 (0)