Skip to content

Commit 00e608e

Browse files
committed
Make share and translation button hideable (#11)
1 parent 18c129d commit 00e608e

10 files changed

+119
-35
lines changed

config.go

+10
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ type configBlog struct {
9898
Contact *configContact `mapstructure:"contact"`
9999
Announcement *configAnnouncement `mapstructure:"announcement"`
100100
hideOldContentWarning bool
101+
hideShareButton bool
102+
hideTranslateButton bool
101103
}
102104

103105
type configSection struct {
@@ -467,6 +469,14 @@ func (a *goBlog) initConfig(logging bool) error {
467469
if err != nil {
468470
return err
469471
}
472+
bc.hideShareButton, err = a.getBooleanSettingValue(settingNameWithBlog(blog, hideShareButtonSetting), false)
473+
if err != nil {
474+
return err
475+
}
476+
bc.hideTranslateButton, err = a.getBooleanSettingValue(settingNameWithBlog(blog, hideTranslateButtonSetting), false)
477+
if err != nil {
478+
return err
479+
}
470480
}
471481
// Log success
472482
a.cfg.initialized = true

httpRouters.go

+2
Original file line numberDiff line numberDiff line change
@@ -456,5 +456,7 @@ func (a *goBlog) blogSettingsRouter(_ *configBlog) func(r chi.Router) {
456456
r.Post(settingsUpdateSectionPath, a.settingsUpdateSection)
457457
r.Post(settingsUpdateDefaultSectionPath, a.settingsUpdateDefaultSection)
458458
r.Post(settingsHideOldContentWarningPath, a.settingsHideOldContentWarning)
459+
r.Post(settingsHideShareButtonPath, a.settingsHideShareButton)
460+
r.Post(settingsHideTranslateButtonPath, a.settingsHideTranslateButton)
459461
}
460462
}

settings.go

+37-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ func (a *goBlog) serveSettings(w http.ResponseWriter, r *http.Request) {
2121
sections: sections,
2222
defaultSection: bc.DefaultSection,
2323
hideOldContentWarning: bc.hideOldContentWarning,
24+
hideShareButton: bc.hideShareButton,
25+
hideTranslateButton: bc.hideTranslateButton,
2426
},
2527
})
2628
}
@@ -160,10 +162,44 @@ func (a *goBlog) settingsHideOldContentWarning(w http.ResponseWriter, r *http.Re
160162
// Update
161163
err := a.saveBooleanSettingValue(settingNameWithBlog(blog, hideOldContentWarningSetting), hideOldContentWarning)
162164
if err != nil {
163-
a.serveError(w, r, "Failed to update default section in database", http.StatusInternalServerError)
165+
a.serveError(w, r, "Failed to update setting to hide old content warning in database", http.StatusInternalServerError)
164166
return
165167
}
166168
bc.hideOldContentWarning = hideOldContentWarning
167169
a.cache.purge()
168170
http.Redirect(w, r, bc.getRelativePath(settingsPath), http.StatusFound)
169171
}
172+
173+
const settingsHideShareButtonPath = "/sharebutton"
174+
175+
func (a *goBlog) settingsHideShareButton(w http.ResponseWriter, r *http.Request) {
176+
blog, bc := a.getBlog(r)
177+
// Read values
178+
hideShareButton := r.FormValue(hideShareButtonSetting) == "on"
179+
// Update
180+
err := a.saveBooleanSettingValue(settingNameWithBlog(blog, hideShareButtonSetting), hideShareButton)
181+
if err != nil {
182+
a.serveError(w, r, "Failed to update setting to hide share button in database", http.StatusInternalServerError)
183+
return
184+
}
185+
bc.hideShareButton = hideShareButton
186+
a.cache.purge()
187+
http.Redirect(w, r, bc.getRelativePath(settingsPath), http.StatusFound)
188+
}
189+
190+
const settingsHideTranslateButtonPath = "/translatebutton"
191+
192+
func (a *goBlog) settingsHideTranslateButton(w http.ResponseWriter, r *http.Request) {
193+
blog, bc := a.getBlog(r)
194+
// Read values
195+
hideTranslateButton := r.FormValue(hideTranslateButtonSetting) == "on"
196+
// Update
197+
err := a.saveBooleanSettingValue(settingNameWithBlog(blog, hideTranslateButtonSetting), hideTranslateButton)
198+
if err != nil {
199+
a.serveError(w, r, "Failed to update setting to hide translate button in database", http.StatusInternalServerError)
200+
return
201+
}
202+
bc.hideTranslateButton = hideTranslateButton
203+
a.cache.purge()
204+
http.Redirect(w, r, bc.getRelativePath(settingsPath), http.StatusFound)
205+
}

settingsDb.go

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func settingNameWithBlog(blog, name string) string {
1515
const (
1616
defaultSectionSetting = "defaultsection"
1717
hideOldContentWarningSetting = "hideoldcontentwarning"
18+
hideShareButtonSetting = "hidesharebutton"
19+
hideTranslateButtonSetting = "hidetranslatebutton"
1820
)
1921

2022
func (a *goBlog) getSettingValue(name string) (string, error) {

strings/de.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ gentts: "Text-To-Speech-Audio erzeugen"
2727
gpxhelper: "GPX-Helfer"
2828
gpxhelperdesc: "💡 GPX minimieren und YAML für das Frontmatter generieren."
2929
hideoldcontentwarningdesc: "Die Warnung für alte Posts (älter als 1 Jahr) ausblenden"
30-
hideoldcontentwarningtitle: "Warnung vor altem Inhalt"
30+
hidesharebuttondesc: "Teilen-Button für Beiträge ausblenden"
31+
hidetranslatebuttondesc: "Übersetzen-Button für Beiträge ausblenden"
3132
interactions: "Interaktionen & Kommentare"
3233
interactionslabel: "Hast du eine Antwort hierzu veröffentlicht? Füge hier die URL ein."
3334
kilometers: "Kilometer"

strings/default.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ gentts: "Generate Text-To-Speech audio"
3131
gpxhelper: "GPX helper"
3232
gpxhelperdesc: "💡 Minify GPX and generate YAML for the frontmatter."
3333
hideoldcontentwarningdesc: "Hide the warning for old posts (older than 1 year)"
34-
hideoldcontentwarningtitle: "Old content warning"
34+
hidesharebuttondesc: "Hide share button for posts"
35+
hidetranslatebuttondesc: "Hide translate button for posts"
3536
indieauth: "IndieAuth"
3637
interactions: "Interactions & Comments"
3738
interactionslabel: "Have you published a response to this? Paste the URL here."

strings/pt-br.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ gentts: "Gerar áudio Text-To-Speech"
3030
gpxhelper: "Ajuda GPX"
3131
gpxhelperdesc: "💡 Minimize o GPX e gere YAML para o frontmatter."
3232
hideoldcontentwarningdesc: "Esconder alerta para posts antigos (mais de 1 ano)"
33-
hideoldcontentwarningtitle: "Alerta para conteúdo antigo"
3433
indieauth: "IndieAuth"
3534
interactions: "Interações & Comentários"
3635
interactionslabel: "Você publicou uma resposta pra isso? Cole a URL aqui."

templates/assets/js/settings.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(function () {
2+
3+
// Make checkboxes with class '.autosubmit' submit form automatically
4+
Array.from(document.querySelectorAll("form input[type='checkbox'].autosubmit")).forEach(element => {
5+
element.addEventListener('change', event => {
6+
event.currentTarget.closest('form').submit()
7+
})
8+
})
9+
10+
})()

ui.go

+21-17
Original file line numberDiff line numberDiff line change
@@ -890,22 +890,9 @@ func (a *goBlog) renderPost(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
890890
// Post actions
891891
hb.WriteElementOpen("div", "class", "actions")
892892
// Share button
893-
hb.WriteElementOpen("a", "class", "button", "href", fmt.Sprintf("https://www.addtoany.com/share#url=%s%s", a.shortPostURL(p), lo.If(p.RenderedTitle != "", "&title="+p.RenderedTitle).Else("")), "target", "_blank", "rel", "nofollow noopener noreferrer")
894-
hb.WriteEscaped(a.ts.GetTemplateStringVariant(rd.Blog.Lang, "share"))
895-
hb.WriteElementClose("a")
893+
a.renderShareButton(hb, p, rd.Blog)
896894
// Translate button
897-
hb.WriteElementOpen(
898-
"a", "id", "translateBtn",
899-
"class", "button",
900-
"href", fmt.Sprintf("https://translate.google.com/translate?u=%s", a.getFullAddress(p.Path)),
901-
"target", "_blank", "rel", "nofollow noopener noreferrer",
902-
"title", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "translate"),
903-
"translate", "no",
904-
)
905-
hb.WriteEscaped("A ⇄ 文")
906-
hb.WriteElementClose("a")
907-
hb.WriteElementOpen("script", "defer", "", "src", a.assetFileName("js/translate.js"))
908-
hb.WriteElementClose("script")
895+
a.renderTranslateButton(hb, p, rd.Blog)
909896
// Speak button
910897
hb.WriteElementOpen("button", "id", "speakBtn", "class", "hide", "data-speak", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "speak"), "data-stopspeak", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "stopspeak"))
911898
hb.WriteElementClose("button")
@@ -1511,6 +1498,8 @@ type settingsRenderData struct {
15111498
sections []*configSection
15121499
defaultSection string
15131500
hideOldContentWarning bool
1501+
hideShareButton bool
1502+
hideTranslateButton bool
15141503
}
15151504

15161505
func (a *goBlog) renderSettings(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
@@ -1537,18 +1526,33 @@ func (a *goBlog) renderSettings(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
15371526
hb.WriteElementClose("h2")
15381527

15391528
// Hide old content warning
1540-
a.renderCollapsibleBooleanSetting(hb, rd,
1529+
a.renderBooleanSetting(hb, rd,
15411530
rd.Blog.getRelativePath(settingsPath+settingsHideOldContentWarningPath),
1542-
a.ts.GetTemplateStringVariant(rd.Blog.Lang, "hideoldcontentwarningtitle"),
15431531
a.ts.GetTemplateStringVariant(rd.Blog.Lang, "hideoldcontentwarningdesc"),
15441532
hideOldContentWarningSetting,
15451533
srd.hideOldContentWarning,
15461534
)
1535+
// Hide share button
1536+
a.renderBooleanSetting(hb, rd,
1537+
rd.Blog.getRelativePath(settingsPath+settingsHideShareButtonPath),
1538+
a.ts.GetTemplateStringVariant(rd.Blog.Lang, "hidesharebuttondesc"),
1539+
hideShareButtonSetting,
1540+
srd.hideShareButton,
1541+
)
1542+
// Hide translate button
1543+
a.renderBooleanSetting(hb, rd,
1544+
rd.Blog.getRelativePath(settingsPath+settingsHideTranslateButtonPath),
1545+
a.ts.GetTemplateStringVariant(rd.Blog.Lang, "hidetranslatebuttondesc"),
1546+
hideTranslateButtonSetting,
1547+
srd.hideTranslateButton,
1548+
)
15471549

15481550
// Post sections
15491551
a.renderPostSectionSettings(hb, rd, srd)
15501552

15511553
// Scripts
1554+
hb.WriteElementOpen("script", "src", a.assetFileName("js/settings.js"), "defer", "")
1555+
hb.WriteElementClose("script")
15521556
hb.WriteElementOpen("script", "src", a.assetFileName("js/formconfirm.js"), "defer", "")
15531557
hb.WriteElementClose("script")
15541558

uiComponents.go

+33-14
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,33 @@ func (a *goBlog) renderOldContentWarning(hb *htmlbuilder.HtmlBuilder, p *post, b
282282
hb.WriteElementClose("strong")
283283
}
284284

285+
func (a *goBlog) renderShareButton(hb *htmlbuilder.HtmlBuilder, p *post, b *configBlog) {
286+
if b == nil || b.hideShareButton {
287+
return
288+
}
289+
hb.WriteElementOpen("a", "class", "button", "href", fmt.Sprintf("https://www.addtoany.com/share#url=%s%s", a.shortPostURL(p), lo.If(p.RenderedTitle != "", "&title="+p.RenderedTitle).Else("")), "target", "_blank", "rel", "nofollow noopener noreferrer")
290+
hb.WriteEscaped(a.ts.GetTemplateStringVariant(b.Lang, "share"))
291+
hb.WriteElementClose("a")
292+
}
293+
294+
func (a *goBlog) renderTranslateButton(hb *htmlbuilder.HtmlBuilder, p *post, b *configBlog) {
295+
if b == nil || b.hideTranslateButton {
296+
return
297+
}
298+
hb.WriteElementOpen(
299+
"a", "id", "translateBtn",
300+
"class", "button",
301+
"href", fmt.Sprintf("https://translate.google.com/translate?u=%s", a.getFullAddress(p.Path)),
302+
"target", "_blank", "rel", "nofollow noopener noreferrer",
303+
"title", a.ts.GetTemplateStringVariant(b.Lang, "translate"),
304+
"translate", "no",
305+
)
306+
hb.WriteEscaped("A ⇄ 文")
307+
hb.WriteElementClose("a")
308+
hb.WriteElementOpen("script", "defer", "", "src", a.assetFileName("js/translate.js"))
309+
hb.WriteElementClose("script")
310+
}
311+
285312
func (a *goBlog) renderInteractions(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
286313
// Start accordion
287314
hb.WriteElementOpen("details", "class", "p", "id", "interactions")
@@ -609,29 +636,21 @@ func (a *goBlog) renderPostSectionSettings(hb *htmlbuilder.HtmlBuilder, rd *rend
609636
hb.WriteElementClose("form")
610637
}
611638

612-
func (a *goBlog) renderCollapsibleBooleanSetting(hb *htmlbuilder.HtmlBuilder, rd *renderData, path, title, description, name string, value bool) {
613-
hb.WriteElementOpen("details")
614-
615-
hb.WriteElementOpen("summary")
616-
hb.WriteElementOpen("h3")
617-
hb.WriteEscaped(title)
618-
hb.WriteElementClose("h3")
619-
hb.WriteElementClose("summary")
639+
func (a *goBlog) renderBooleanSetting(hb *htmlbuilder.HtmlBuilder, rd *renderData, path, description, name string, value bool) {
640+
hb.WriteElementOpen("form", "class", "fw p", "method", "post", "action", path)
620641

621-
hb.WriteElementOpen("form", "class", "fw p", "method", "post")
622-
623-
hb.WriteElementOpen("input", "type", "checkbox", "name", name, "id", "cb-"+name, lo.If(value, "checked").Else(""), "")
642+
hb.WriteElementOpen("input", "type", "checkbox", "class", "autosubmit", "name", name, "id", "cb-"+name, lo.If(value, "checked").Else(""), "")
624643
hb.WriteElementOpen("label", "for", "cb-"+name)
625644
hb.WriteEscaped(description)
626645
hb.WriteElementClose("label")
627646

647+
hb.WriteElementOpen("noscript")
628648
hb.WriteElementOpen("div", "class", "p")
629649
hb.WriteElementOpen(
630-
"input", "type", "submit", "value", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "update"), "formaction", path,
650+
"input", "type", "submit", "value", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "update"),
631651
)
632652
hb.WriteElementClose("div")
653+
hb.WriteElementClose("noscript")
633654

634655
hb.WriteElementClose("form")
635-
636-
hb.WriteElementClose("details")
637656
}

0 commit comments

Comments
 (0)