Make share and translation button hideable (#11)

This commit is contained in:
Jan-Lukas Else 2022-10-07 17:08:37 +02:00
parent 18c129d163
commit 00e608ea27
10 changed files with 119 additions and 35 deletions

View File

@ -98,6 +98,8 @@ type configBlog struct {
Contact *configContact `mapstructure:"contact"`
Announcement *configAnnouncement `mapstructure:"announcement"`
hideOldContentWarning bool
hideShareButton bool
hideTranslateButton bool
}
type configSection struct {
@ -467,6 +469,14 @@ func (a *goBlog) initConfig(logging bool) error {
if err != nil {
return err
}
bc.hideShareButton, err = a.getBooleanSettingValue(settingNameWithBlog(blog, hideShareButtonSetting), false)
if err != nil {
return err
}
bc.hideTranslateButton, err = a.getBooleanSettingValue(settingNameWithBlog(blog, hideTranslateButtonSetting), false)
if err != nil {
return err
}
}
// Log success
a.cfg.initialized = true

View File

@ -456,5 +456,7 @@ func (a *goBlog) blogSettingsRouter(_ *configBlog) func(r chi.Router) {
r.Post(settingsUpdateSectionPath, a.settingsUpdateSection)
r.Post(settingsUpdateDefaultSectionPath, a.settingsUpdateDefaultSection)
r.Post(settingsHideOldContentWarningPath, a.settingsHideOldContentWarning)
r.Post(settingsHideShareButtonPath, a.settingsHideShareButton)
r.Post(settingsHideTranslateButtonPath, a.settingsHideTranslateButton)
}
}

View File

@ -21,6 +21,8 @@ func (a *goBlog) serveSettings(w http.ResponseWriter, r *http.Request) {
sections: sections,
defaultSection: bc.DefaultSection,
hideOldContentWarning: bc.hideOldContentWarning,
hideShareButton: bc.hideShareButton,
hideTranslateButton: bc.hideTranslateButton,
},
})
}
@ -160,10 +162,44 @@ func (a *goBlog) settingsHideOldContentWarning(w http.ResponseWriter, r *http.Re
// Update
err := a.saveBooleanSettingValue(settingNameWithBlog(blog, hideOldContentWarningSetting), hideOldContentWarning)
if err != nil {
a.serveError(w, r, "Failed to update default section in database", http.StatusInternalServerError)
a.serveError(w, r, "Failed to update setting to hide old content warning in database", http.StatusInternalServerError)
return
}
bc.hideOldContentWarning = hideOldContentWarning
a.cache.purge()
http.Redirect(w, r, bc.getRelativePath(settingsPath), http.StatusFound)
}
const settingsHideShareButtonPath = "/sharebutton"
func (a *goBlog) settingsHideShareButton(w http.ResponseWriter, r *http.Request) {
blog, bc := a.getBlog(r)
// Read values
hideShareButton := r.FormValue(hideShareButtonSetting) == "on"
// Update
err := a.saveBooleanSettingValue(settingNameWithBlog(blog, hideShareButtonSetting), hideShareButton)
if err != nil {
a.serveError(w, r, "Failed to update setting to hide share button in database", http.StatusInternalServerError)
return
}
bc.hideShareButton = hideShareButton
a.cache.purge()
http.Redirect(w, r, bc.getRelativePath(settingsPath), http.StatusFound)
}
const settingsHideTranslateButtonPath = "/translatebutton"
func (a *goBlog) settingsHideTranslateButton(w http.ResponseWriter, r *http.Request) {
blog, bc := a.getBlog(r)
// Read values
hideTranslateButton := r.FormValue(hideTranslateButtonSetting) == "on"
// Update
err := a.saveBooleanSettingValue(settingNameWithBlog(blog, hideTranslateButtonSetting), hideTranslateButton)
if err != nil {
a.serveError(w, r, "Failed to update setting to hide translate button in database", http.StatusInternalServerError)
return
}
bc.hideTranslateButton = hideTranslateButton
a.cache.purge()
http.Redirect(w, r, bc.getRelativePath(settingsPath), http.StatusFound)
}

View File

@ -15,6 +15,8 @@ func settingNameWithBlog(blog, name string) string {
const (
defaultSectionSetting = "defaultsection"
hideOldContentWarningSetting = "hideoldcontentwarning"
hideShareButtonSetting = "hidesharebutton"
hideTranslateButtonSetting = "hidetranslatebutton"
)
func (a *goBlog) getSettingValue(name string) (string, error) {

View File

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

View File

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

View File

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

View File

@ -0,0 +1,10 @@
(function () {
// Make checkboxes with class '.autosubmit' submit form automatically
Array.from(document.querySelectorAll("form input[type='checkbox'].autosubmit")).forEach(element => {
element.addEventListener('change', event => {
event.currentTarget.closest('form').submit()
})
})
})()

38
ui.go
View File

@ -890,22 +890,9 @@ func (a *goBlog) renderPost(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
// Post actions
hb.WriteElementOpen("div", "class", "actions")
// Share button
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")
hb.WriteEscaped(a.ts.GetTemplateStringVariant(rd.Blog.Lang, "share"))
hb.WriteElementClose("a")
a.renderShareButton(hb, p, rd.Blog)
// Translate button
hb.WriteElementOpen(
"a", "id", "translateBtn",
"class", "button",
"href", fmt.Sprintf("https://translate.google.com/translate?u=%s", a.getFullAddress(p.Path)),
"target", "_blank", "rel", "nofollow noopener noreferrer",
"title", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "translate"),
"translate", "no",
)
hb.WriteEscaped("A ⇄ 文")
hb.WriteElementClose("a")
hb.WriteElementOpen("script", "defer", "", "src", a.assetFileName("js/translate.js"))
hb.WriteElementClose("script")
a.renderTranslateButton(hb, p, rd.Blog)
// Speak button
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"))
hb.WriteElementClose("button")
@ -1511,6 +1498,8 @@ type settingsRenderData struct {
sections []*configSection
defaultSection string
hideOldContentWarning bool
hideShareButton bool
hideTranslateButton bool
}
func (a *goBlog) renderSettings(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
@ -1537,18 +1526,33 @@ func (a *goBlog) renderSettings(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
hb.WriteElementClose("h2")
// Hide old content warning
a.renderCollapsibleBooleanSetting(hb, rd,
a.renderBooleanSetting(hb, rd,
rd.Blog.getRelativePath(settingsPath+settingsHideOldContentWarningPath),
a.ts.GetTemplateStringVariant(rd.Blog.Lang, "hideoldcontentwarningtitle"),
a.ts.GetTemplateStringVariant(rd.Blog.Lang, "hideoldcontentwarningdesc"),
hideOldContentWarningSetting,
srd.hideOldContentWarning,
)
// Hide share button
a.renderBooleanSetting(hb, rd,
rd.Blog.getRelativePath(settingsPath+settingsHideShareButtonPath),
a.ts.GetTemplateStringVariant(rd.Blog.Lang, "hidesharebuttondesc"),
hideShareButtonSetting,
srd.hideShareButton,
)
// Hide translate button
a.renderBooleanSetting(hb, rd,
rd.Blog.getRelativePath(settingsPath+settingsHideTranslateButtonPath),
a.ts.GetTemplateStringVariant(rd.Blog.Lang, "hidetranslatebuttondesc"),
hideTranslateButtonSetting,
srd.hideTranslateButton,
)
// Post sections
a.renderPostSectionSettings(hb, rd, srd)
// Scripts
hb.WriteElementOpen("script", "src", a.assetFileName("js/settings.js"), "defer", "")
hb.WriteElementClose("script")
hb.WriteElementOpen("script", "src", a.assetFileName("js/formconfirm.js"), "defer", "")
hb.WriteElementClose("script")

View File

@ -282,6 +282,33 @@ func (a *goBlog) renderOldContentWarning(hb *htmlbuilder.HtmlBuilder, p *post, b
hb.WriteElementClose("strong")
}
func (a *goBlog) renderShareButton(hb *htmlbuilder.HtmlBuilder, p *post, b *configBlog) {
if b == nil || b.hideShareButton {
return
}
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")
hb.WriteEscaped(a.ts.GetTemplateStringVariant(b.Lang, "share"))
hb.WriteElementClose("a")
}
func (a *goBlog) renderTranslateButton(hb *htmlbuilder.HtmlBuilder, p *post, b *configBlog) {
if b == nil || b.hideTranslateButton {
return
}
hb.WriteElementOpen(
"a", "id", "translateBtn",
"class", "button",
"href", fmt.Sprintf("https://translate.google.com/translate?u=%s", a.getFullAddress(p.Path)),
"target", "_blank", "rel", "nofollow noopener noreferrer",
"title", a.ts.GetTemplateStringVariant(b.Lang, "translate"),
"translate", "no",
)
hb.WriteEscaped("A ⇄ 文")
hb.WriteElementClose("a")
hb.WriteElementOpen("script", "defer", "", "src", a.assetFileName("js/translate.js"))
hb.WriteElementClose("script")
}
func (a *goBlog) renderInteractions(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
// Start accordion
hb.WriteElementOpen("details", "class", "p", "id", "interactions")
@ -609,29 +636,21 @@ func (a *goBlog) renderPostSectionSettings(hb *htmlbuilder.HtmlBuilder, rd *rend
hb.WriteElementClose("form")
}
func (a *goBlog) renderCollapsibleBooleanSetting(hb *htmlbuilder.HtmlBuilder, rd *renderData, path, title, description, name string, value bool) {
hb.WriteElementOpen("details")
func (a *goBlog) renderBooleanSetting(hb *htmlbuilder.HtmlBuilder, rd *renderData, path, description, name string, value bool) {
hb.WriteElementOpen("form", "class", "fw p", "method", "post", "action", path)
hb.WriteElementOpen("summary")
hb.WriteElementOpen("h3")
hb.WriteEscaped(title)
hb.WriteElementClose("h3")
hb.WriteElementClose("summary")
hb.WriteElementOpen("form", "class", "fw p", "method", "post")
hb.WriteElementOpen("input", "type", "checkbox", "name", name, "id", "cb-"+name, lo.If(value, "checked").Else(""), "")
hb.WriteElementOpen("input", "type", "checkbox", "class", "autosubmit", "name", name, "id", "cb-"+name, lo.If(value, "checked").Else(""), "")
hb.WriteElementOpen("label", "for", "cb-"+name)
hb.WriteEscaped(description)
hb.WriteElementClose("label")
hb.WriteElementOpen("noscript")
hb.WriteElementOpen("div", "class", "p")
hb.WriteElementOpen(
"input", "type", "submit", "value", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "update"), "formaction", path,
"input", "type", "submit", "value", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "update"),
)
hb.WriteElementClose("div")
hb.WriteElementClose("noscript")
hb.WriteElementClose("form")
hb.WriteElementClose("details")
}