diff --git a/config.go b/config.go index a21e107..d848431 100644 --- a/config.go +++ b/config.go @@ -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 diff --git a/httpRouters.go b/httpRouters.go index 43ef2d5..1c36e86 100644 --- a/httpRouters.go +++ b/httpRouters.go @@ -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) } } diff --git a/settings.go b/settings.go index ab3e979..cedeee5 100644 --- a/settings.go +++ b/settings.go @@ -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) +} diff --git a/settingsDb.go b/settingsDb.go index 1e6c55b..4ff1be3 100644 --- a/settingsDb.go +++ b/settingsDb.go @@ -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) { diff --git a/strings/de.yaml b/strings/de.yaml index 5c806e1..dc006e4 100644 --- a/strings/de.yaml +++ b/strings/de.yaml @@ -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" diff --git a/strings/default.yaml b/strings/default.yaml index aabf92d..e1f1e73 100644 --- a/strings/default.yaml +++ b/strings/default.yaml @@ -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." diff --git a/strings/pt-br.yaml b/strings/pt-br.yaml index 207ccf0..a15bb8f 100644 --- a/strings/pt-br.yaml +++ b/strings/pt-br.yaml @@ -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." diff --git a/templates/assets/js/settings.js b/templates/assets/js/settings.js new file mode 100644 index 0000000..ab6b0d0 --- /dev/null +++ b/templates/assets/js/settings.js @@ -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() + }) + }) + +})() \ No newline at end of file diff --git a/ui.go b/ui.go index 0308393..20f47e8 100644 --- a/ui.go +++ b/ui.go @@ -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") diff --git a/uiComponents.go b/uiComponents.go index 3416466..3308457 100644 --- a/uiComponents.go +++ b/uiComponents.go @@ -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") }