Skip to content

Commit

Permalink
New plugin: imagetooltips, show image titles in alert when clicking o…
Browse files Browse the repository at this point in the history
…n them
  • Loading branch information
jlelse committed Jan 27, 2025
1 parent 082469f commit 1b59335
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
8 changes: 8 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,12 @@ A simple plugin that enhances the `/robots.txt` with AI bot user-agents and also

#### Config

This plugin has no config options.

### Image tooltips (Path `embedded:imagetooltips`, Import `imagetooltips`)

A simple plugin that shows image titles in an alert when clicking on the image. This improves the user experience on mobile devices where hovering over the image to trigger a tooltip is not possible.

#### Config

This plugin has no config options.
6 changes: 0 additions & 6 deletions plugins/customcss/src/customcss/customcss.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type plugin struct {

customCSS string
init sync.Once
inited bool
}

func GetPlugin() (plugintypes.SetConfig, plugintypes.SetApp, plugintypes.UI2) {
Expand Down Expand Up @@ -55,14 +54,9 @@ func (p *plugin) RenderWithDocument(_ plugintypes.RenderContext, doc *goquery.Do
return
}

p.inited = true
fmt.Println("Custom CSS compiled")
})

if !p.inited {
return
}

buf := bufferpool.Get()
defer bufferpool.Put(buf)
hb := htmlbuilder.NewHtmlBuilder(buf)
Expand Down
66 changes: 66 additions & 0 deletions plugins/imagetooltips/src/imagetooltips/imagetooltips.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package imagetooltips

import (
"fmt"
"strings"
"sync"

"github.com/PuerkitoBio/goquery"
"go.goblog.app/app/pkgs/bufferpool"
"go.goblog.app/app/pkgs/htmlbuilder"
"go.goblog.app/app/pkgs/plugintypes"
)

type plugin struct {
app plugintypes.App
init sync.Once
}

func GetPlugin() (plugintypes.SetApp, plugintypes.UI2) {
p := &plugin{}
return p, p
}

func (p *plugin) SetApp(app plugintypes.App) {
p.app = app
}

func (p *plugin) RenderWithDocument(_ plugintypes.RenderContext, doc *goquery.Document) {
if p.app == nil {
return
}

p.init.Do(func() {
err := p.app.CompileAsset("imagetooltips.js", strings.NewReader(imagetooltipsJs))
if err != nil {
fmt.Println("Failed to compile js: ", err.Error())
return
}
})

bufJs := bufferpool.Get()
defer bufferpool.Put(bufJs)
hbJs := htmlbuilder.NewHtmlBuilder(bufJs)

doc.Find(".e-content a > img").Each(func(_ int, element *goquery.Selection) {
element.Parent().ReplaceWithSelection(element)
element.AppendHtml("")
})

hbJs.WriteElementOpen("script", "src", p.app.AssetPath("imagetooltips.js"), "defer", "")
hbJs.WriteElementClose("script")
doc.Find("main").AppendHtml(bufJs.String())
}

// Copy as strings, as embedding is not supported by Yaegi

const imagetooltipsJs = `
(function () {
document.querySelectorAll('.e-content img[title]').forEach((image) => {
image.addEventListener('click', (e) => {
e.preventDefault();
alert(image.getAttribute('title'));
});
});
})();
`

0 comments on commit 1b59335

Please sign in to comment.