This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_oembed.go
69 lines (52 loc) · 1.75 KB
/
widget_oembed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package nebula
import (
"math/rand"
"github.com/benpate/html"
"github.com/benpate/rosetta/convert"
"github.com/benpate/rosetta/list"
)
// ItemTypeOEmbed describes an oEmbed object (see https://oembed.com)
const ItemTypeOEmbed = "OEMBED"
type OEmbed struct{}
func (w OEmbed) View(b *html.Builder, container *Container, itemID int) {
item := container.GetItem(itemID)
// If the oEmbed data includes HTML, then just use that and be done.
if html := item.GetString("html"); html != "" {
b.WriteString(html)
return
}
switch list.Slash(item.GetString("mimeType")).Head() {
case "video":
b.Span().InnerHTML("video here...")
b.Close()
default:
b.Empty("img").Class("pure-img").Attr("src", item.GetString("file"))
b.Close()
}
}
func (w OEmbed) Edit(b *html.Builder, container *Container, itemID int, endpoint string) {
item := container.GetItem(itemID)
idString := convert.String(itemID)
idStringUnique := "file-" + idString + "-" + convert.String(rand.Int())
b.Container("label").For(idStringUnique).Class("block")
b.Form("", "").
Data("id", idString).
Data("hx-post", endpoint).
Data("hx-trigger", "change").
Data("hx-encoding", "multipart/form-data").
Script("install DropToUpload").
Class("uploader")
b.Input("hidden", "action").Value("upload-file")
b.Input("hidden", "itemId").Value(convert.String(itemID))
b.Input("hidden", "check").Value(item.Check)
b.Input("file", "file").ID(idStringUnique).Attr("accept", "image/*")
if item.GetString("file") == "" {
b.Div().Class("space-above", "space-below").InnerHTML("Drag Files Here, or Click To Select").Close()
} else {
w.View(b, container, itemID)
}
b.CloseAll()
}
// Validate cleans the container for invalid content
func (w OEmbed) Validate(container *Container, index int) {
}