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_wysiwyg.go
94 lines (81 loc) · 3.43 KB
/
widget_wysiwyg.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package nebula
import (
"net/url"
"github.com/benpate/derp"
"github.com/benpate/html"
"github.com/benpate/rosetta/convert"
)
// ItemTypeWYSIWYG describes a what-you-see-is-what-you-get content editor
const ItemTypeWYSIWYG = "WYSIWYG"
type WYSIWYG struct{}
// Init sets up an empty "html" property
func (w WYSIWYG) Init(container *Container, id int) {
(*container)[id].Set("html", "")
}
func (w WYSIWYG) View(b *html.Builder, container *Container, id int) {
item := container.GetItem(id)
result := item.GetString("html")
b.WriteString(result)
}
func (w WYSIWYG) Edit(b *html.Builder, container *Container, id int, endpoint string) {
item := container.GetItem(id)
result := item.GetString("html")
idString := convert.String(id)
b.Form("", "").
Data("hx-post", endpoint).
Data("hx-trigger", "autosave").
Data("hx-swap", "none")
// Form fields here
b.Input("hidden", "action").Value("update-item")
b.Input("hidden", "itemId").Value(idString)
b.Input("hidden", "html").Value(idString)
b.Input("hidden", "check").Value(item.Check)
b.Div().Class("wysiwyg").Script("install wysiwyg(name:'html') install hotkey")
b.Div().Class("wysiwyg-toolbar").Attr("hidden", "true").ID("toolbar-" + idString)
{
b.Span().Class("wysiwyg-toolbar-group").EndBracket()
b.Button().Data("command", "formatBlock").Data("command-value", "h1").InnerHTML("H1").Close()
b.Button().Data("command", "formatBlock").Data("command-value", "h2").InnerHTML("H2").Close()
b.Button().Data("command", "formatBlock").Data("command-value", "h3").InnerHTML("H3").Close()
b.Button().Data("command", "formatBlock").Data("command-value", "p").InnerHTML("P").Close()
b.Close()
}
{
b.Span().Class("wysiwyg-toolbar-group").EndBracket()
b.Button().Data("command", "bold").Aria("keyshortcuts", "Ctrl+B").InnerHTML("B").Close()
b.Button().Data("command", "italic").Aria("keyshortcuts", "Ctrl+I").InnerHTML("I").Close()
b.Button().Data("command", "underline").Aria("keyshortcuts", "Ctrl+U").InnerHTML("U").Close()
b.Close()
}
{
b.Span().Class("wysiwyg-toolbar-group").EndBracket()
b.Button().Data("command", "createLink").Aria("keyshortcuts", "Ctrl+K")
b.Container("i").Class("ti", "ti-link").Close()
b.Close()
b.Button().Data("command", "unlink").Aria("keyshortcuts", "Ctrl+Shift+K")
b.Container("i").Class("ti", "ti-unlink").Close()
b.Close()
b.Close()
}
{
b.Span().Class("wysiwyg-toolbar-group").Attr("hidden", "true").EndBracket()
b.Button().Data("command", "cut").Aria("keyshortcuts", "Ctrl+X").InnerHTML("Cut").Close()
b.Button().Data("command", "copy").Aria("keyshortcuts", "Ctrl+C").InnerHTML("Copy").Close()
b.Button().Data("command", "paste").Aria("keyshortcuts", "Ctrl+V").InnerHTML("Paste").Close()
b.Button().Data("command", "undo").Aria("keyshortcuts", "Ctrl+Z").InnerHTML("Undo").Close()
b.Button().Data("command", "redo").Aria("keyshortcuts", "Ctrl+Shift+Z").InnerHTML("Redo").Close()
b.Close()
}
b.Close()
b.Div().Class("wysiwyg-editor").Script("install Autosave").InnerHTML(result)
b.CloseAll()
}
// Validate cleans the container for invalid content
func (w WYSIWYG) Validate(container *Container, index int) {
originalHTML := (*container)[index].GetString("html")
cleanHTML := htmlPolicy.Sanitize(originalHTML)
(*container)[index].Data.SetString("html", cleanHTML)
}
func (w WYSIWYG) Prop(container *Container, id int, params url.Values) (string, error) {
return "", derp.NewNotFoundError("content.WYSIWYG.Prop", "Unrecognized panel", params)
}