mirror of https://github.com/jlelse/GoBlog
Make share and translation button hideable (#11)
This commit is contained in:
parent
18c129d163
commit
00e608ea27
10
config.go
10
config.go
|
@ -98,6 +98,8 @@ type configBlog struct {
|
||||||
Contact *configContact `mapstructure:"contact"`
|
Contact *configContact `mapstructure:"contact"`
|
||||||
Announcement *configAnnouncement `mapstructure:"announcement"`
|
Announcement *configAnnouncement `mapstructure:"announcement"`
|
||||||
hideOldContentWarning bool
|
hideOldContentWarning bool
|
||||||
|
hideShareButton bool
|
||||||
|
hideTranslateButton bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type configSection struct {
|
type configSection struct {
|
||||||
|
@ -467,6 +469,14 @@ func (a *goBlog) initConfig(logging bool) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
// Log success
|
||||||
a.cfg.initialized = true
|
a.cfg.initialized = true
|
||||||
|
|
|
@ -456,5 +456,7 @@ func (a *goBlog) blogSettingsRouter(_ *configBlog) func(r chi.Router) {
|
||||||
r.Post(settingsUpdateSectionPath, a.settingsUpdateSection)
|
r.Post(settingsUpdateSectionPath, a.settingsUpdateSection)
|
||||||
r.Post(settingsUpdateDefaultSectionPath, a.settingsUpdateDefaultSection)
|
r.Post(settingsUpdateDefaultSectionPath, a.settingsUpdateDefaultSection)
|
||||||
r.Post(settingsHideOldContentWarningPath, a.settingsHideOldContentWarning)
|
r.Post(settingsHideOldContentWarningPath, a.settingsHideOldContentWarning)
|
||||||
|
r.Post(settingsHideShareButtonPath, a.settingsHideShareButton)
|
||||||
|
r.Post(settingsHideTranslateButtonPath, a.settingsHideTranslateButton)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
38
settings.go
38
settings.go
|
@ -21,6 +21,8 @@ func (a *goBlog) serveSettings(w http.ResponseWriter, r *http.Request) {
|
||||||
sections: sections,
|
sections: sections,
|
||||||
defaultSection: bc.DefaultSection,
|
defaultSection: bc.DefaultSection,
|
||||||
hideOldContentWarning: bc.hideOldContentWarning,
|
hideOldContentWarning: bc.hideOldContentWarning,
|
||||||
|
hideShareButton: bc.hideShareButton,
|
||||||
|
hideTranslateButton: bc.hideTranslateButton,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -160,10 +162,44 @@ func (a *goBlog) settingsHideOldContentWarning(w http.ResponseWriter, r *http.Re
|
||||||
// Update
|
// Update
|
||||||
err := a.saveBooleanSettingValue(settingNameWithBlog(blog, hideOldContentWarningSetting), hideOldContentWarning)
|
err := a.saveBooleanSettingValue(settingNameWithBlog(blog, hideOldContentWarningSetting), hideOldContentWarning)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
bc.hideOldContentWarning = hideOldContentWarning
|
bc.hideOldContentWarning = hideOldContentWarning
|
||||||
a.cache.purge()
|
a.cache.purge()
|
||||||
http.Redirect(w, r, bc.getRelativePath(settingsPath), http.StatusFound)
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -15,6 +15,8 @@ func settingNameWithBlog(blog, name string) string {
|
||||||
const (
|
const (
|
||||||
defaultSectionSetting = "defaultsection"
|
defaultSectionSetting = "defaultsection"
|
||||||
hideOldContentWarningSetting = "hideoldcontentwarning"
|
hideOldContentWarningSetting = "hideoldcontentwarning"
|
||||||
|
hideShareButtonSetting = "hidesharebutton"
|
||||||
|
hideTranslateButtonSetting = "hidetranslatebutton"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *goBlog) getSettingValue(name string) (string, error) {
|
func (a *goBlog) getSettingValue(name string) (string, error) {
|
||||||
|
|
|
@ -27,7 +27,8 @@ gentts: "Text-To-Speech-Audio erzeugen"
|
||||||
gpxhelper: "GPX-Helfer"
|
gpxhelper: "GPX-Helfer"
|
||||||
gpxhelperdesc: "💡 GPX minimieren und YAML für das Frontmatter generieren."
|
gpxhelperdesc: "💡 GPX minimieren und YAML für das Frontmatter generieren."
|
||||||
hideoldcontentwarningdesc: "Die Warnung für alte Posts (älter als 1 Jahr) ausblenden"
|
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"
|
interactions: "Interaktionen & Kommentare"
|
||||||
interactionslabel: "Hast du eine Antwort hierzu veröffentlicht? Füge hier die URL ein."
|
interactionslabel: "Hast du eine Antwort hierzu veröffentlicht? Füge hier die URL ein."
|
||||||
kilometers: "Kilometer"
|
kilometers: "Kilometer"
|
||||||
|
|
|
@ -31,7 +31,8 @@ gentts: "Generate Text-To-Speech audio"
|
||||||
gpxhelper: "GPX helper"
|
gpxhelper: "GPX helper"
|
||||||
gpxhelperdesc: "💡 Minify GPX and generate YAML for the frontmatter."
|
gpxhelperdesc: "💡 Minify GPX and generate YAML for the frontmatter."
|
||||||
hideoldcontentwarningdesc: "Hide the warning for old posts (older than 1 year)"
|
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"
|
indieauth: "IndieAuth"
|
||||||
interactions: "Interactions & Comments"
|
interactions: "Interactions & Comments"
|
||||||
interactionslabel: "Have you published a response to this? Paste the URL here."
|
interactionslabel: "Have you published a response to this? Paste the URL here."
|
||||||
|
|
|
@ -30,7 +30,6 @@ gentts: "Gerar áudio Text-To-Speech"
|
||||||
gpxhelper: "Ajuda GPX"
|
gpxhelper: "Ajuda GPX"
|
||||||
gpxhelperdesc: "💡 Minimize o GPX e gere YAML para o frontmatter."
|
gpxhelperdesc: "💡 Minimize o GPX e gere YAML para o frontmatter."
|
||||||
hideoldcontentwarningdesc: "Esconder alerta para posts antigos (mais de 1 ano)"
|
hideoldcontentwarningdesc: "Esconder alerta para posts antigos (mais de 1 ano)"
|
||||||
hideoldcontentwarningtitle: "Alerta para conteúdo antigo"
|
|
||||||
indieauth: "IndieAuth"
|
indieauth: "IndieAuth"
|
||||||
interactions: "Interações & Comentários"
|
interactions: "Interações & Comentários"
|
||||||
interactionslabel: "Você publicou uma resposta pra isso? Cole a URL aqui."
|
interactionslabel: "Você publicou uma resposta pra isso? Cole a URL aqui."
|
||||||
|
|
|
@ -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
38
ui.go
|
@ -890,22 +890,9 @@ func (a *goBlog) renderPost(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
|
||||||
// Post actions
|
// Post actions
|
||||||
hb.WriteElementOpen("div", "class", "actions")
|
hb.WriteElementOpen("div", "class", "actions")
|
||||||
// Share button
|
// 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")
|
a.renderShareButton(hb, p, rd.Blog)
|
||||||
hb.WriteEscaped(a.ts.GetTemplateStringVariant(rd.Blog.Lang, "share"))
|
|
||||||
hb.WriteElementClose("a")
|
|
||||||
// Translate button
|
// Translate button
|
||||||
hb.WriteElementOpen(
|
a.renderTranslateButton(hb, p, rd.Blog)
|
||||||
"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")
|
|
||||||
// Speak button
|
// 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.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")
|
hb.WriteElementClose("button")
|
||||||
|
@ -1511,6 +1498,8 @@ type settingsRenderData struct {
|
||||||
sections []*configSection
|
sections []*configSection
|
||||||
defaultSection string
|
defaultSection string
|
||||||
hideOldContentWarning bool
|
hideOldContentWarning bool
|
||||||
|
hideShareButton bool
|
||||||
|
hideTranslateButton bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *goBlog) renderSettings(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
|
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")
|
hb.WriteElementClose("h2")
|
||||||
|
|
||||||
// Hide old content warning
|
// Hide old content warning
|
||||||
a.renderCollapsibleBooleanSetting(hb, rd,
|
a.renderBooleanSetting(hb, rd,
|
||||||
rd.Blog.getRelativePath(settingsPath+settingsHideOldContentWarningPath),
|
rd.Blog.getRelativePath(settingsPath+settingsHideOldContentWarningPath),
|
||||||
a.ts.GetTemplateStringVariant(rd.Blog.Lang, "hideoldcontentwarningtitle"),
|
|
||||||
a.ts.GetTemplateStringVariant(rd.Blog.Lang, "hideoldcontentwarningdesc"),
|
a.ts.GetTemplateStringVariant(rd.Blog.Lang, "hideoldcontentwarningdesc"),
|
||||||
hideOldContentWarningSetting,
|
hideOldContentWarningSetting,
|
||||||
srd.hideOldContentWarning,
|
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
|
// Post sections
|
||||||
a.renderPostSectionSettings(hb, rd, srd)
|
a.renderPostSectionSettings(hb, rd, srd)
|
||||||
|
|
||||||
// Scripts
|
// 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.WriteElementOpen("script", "src", a.assetFileName("js/formconfirm.js"), "defer", "")
|
||||||
hb.WriteElementClose("script")
|
hb.WriteElementClose("script")
|
||||||
|
|
||||||
|
|
|
@ -282,6 +282,33 @@ func (a *goBlog) renderOldContentWarning(hb *htmlbuilder.HtmlBuilder, p *post, b
|
||||||
hb.WriteElementClose("strong")
|
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) {
|
func (a *goBlog) renderInteractions(hb *htmlbuilder.HtmlBuilder, rd *renderData) {
|
||||||
// Start accordion
|
// Start accordion
|
||||||
hb.WriteElementOpen("details", "class", "p", "id", "interactions")
|
hb.WriteElementOpen("details", "class", "p", "id", "interactions")
|
||||||
|
@ -609,29 +636,21 @@ func (a *goBlog) renderPostSectionSettings(hb *htmlbuilder.HtmlBuilder, rd *rend
|
||||||
hb.WriteElementClose("form")
|
hb.WriteElementClose("form")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *goBlog) renderCollapsibleBooleanSetting(hb *htmlbuilder.HtmlBuilder, rd *renderData, path, title, description, name string, value bool) {
|
func (a *goBlog) renderBooleanSetting(hb *htmlbuilder.HtmlBuilder, rd *renderData, path, description, name string, value bool) {
|
||||||
hb.WriteElementOpen("details")
|
hb.WriteElementOpen("form", "class", "fw p", "method", "post", "action", path)
|
||||||
|
|
||||||
hb.WriteElementOpen("summary")
|
hb.WriteElementOpen("input", "type", "checkbox", "class", "autosubmit", "name", name, "id", "cb-"+name, lo.If(value, "checked").Else(""), "")
|
||||||
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("label", "for", "cb-"+name)
|
hb.WriteElementOpen("label", "for", "cb-"+name)
|
||||||
hb.WriteEscaped(description)
|
hb.WriteEscaped(description)
|
||||||
hb.WriteElementClose("label")
|
hb.WriteElementClose("label")
|
||||||
|
|
||||||
|
hb.WriteElementOpen("noscript")
|
||||||
hb.WriteElementOpen("div", "class", "p")
|
hb.WriteElementOpen("div", "class", "p")
|
||||||
hb.WriteElementOpen(
|
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("div")
|
||||||
|
hb.WriteElementClose("noscript")
|
||||||
|
|
||||||
hb.WriteElementClose("form")
|
hb.WriteElementClose("form")
|
||||||
|
|
||||||
hb.WriteElementClose("details")
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue