2020-10-05 03:45:34 +00:00
|
|
|
package emoji
|
|
|
|
|
|
|
|
import (
|
2020-12-20 05:44:26 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/discord"
|
2020-10-05 03:45:34 +00:00
|
|
|
"github.com/diamondburned/cchat-discord/internal/segments/renderer"
|
2021-01-03 04:44:30 +00:00
|
|
|
"github.com/diamondburned/cchat-discord/internal/urlutils"
|
2020-10-05 03:45:34 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
|
|
|
"github.com/diamondburned/cchat/utils/empty"
|
2020-12-20 05:44:26 +00:00
|
|
|
"github.com/diamondburned/ningen/v2/md"
|
2020-10-05 03:45:34 +00:00
|
|
|
"github.com/yuin/goldmark/ast"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
renderer.Register(md.KindEmoji, emoji)
|
|
|
|
}
|
|
|
|
|
|
|
|
func emoji(r *renderer.Text, node ast.Node, enter bool) ast.WalkStatus {
|
|
|
|
n := node.(*md.Emoji)
|
|
|
|
|
|
|
|
if enter {
|
|
|
|
r.Append(Segment{
|
|
|
|
Start: r.Buffer.Len(),
|
|
|
|
Emoji: EmojiFromNode(n),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
InlineSize = 22
|
|
|
|
LargeSize = 48
|
|
|
|
)
|
|
|
|
|
|
|
|
type Emoji struct {
|
|
|
|
Name string
|
|
|
|
EmojiURL string
|
|
|
|
Large bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ text.Imager = (*Emoji)(nil)
|
|
|
|
|
|
|
|
func injectSizeURL(fullURL string) string {
|
2021-01-03 04:44:30 +00:00
|
|
|
return urlutils.Sized(fullURL, 64)
|
2020-10-05 03:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func EmojiFromNode(n *md.Emoji) Emoji {
|
|
|
|
return Emoji{
|
|
|
|
Name: n.Name,
|
|
|
|
Large: n.Large,
|
|
|
|
EmojiURL: injectSizeURL(n.EmojiURL()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func EmojiFromDiscord(e discord.Emoji, large bool) Emoji {
|
|
|
|
return Emoji{
|
|
|
|
Name: e.Name,
|
|
|
|
Large: large,
|
2021-01-03 04:44:30 +00:00
|
|
|
EmojiURL: injectSizeURL(e.EmojiURL()),
|
2020-10-05 03:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Emoji) Image() string {
|
|
|
|
return e.EmojiURL
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Emoji) ImageSize() (w, h int) {
|
|
|
|
if e.Large {
|
|
|
|
return LargeSize, LargeSize
|
|
|
|
}
|
|
|
|
return InlineSize, InlineSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Emoji) ImageText() string {
|
|
|
|
return ":" + e.Name + ":"
|
|
|
|
}
|
|
|
|
|
|
|
|
type Segment struct {
|
|
|
|
empty.TextSegment
|
|
|
|
Start int
|
|
|
|
Emoji Emoji
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ text.Segment = (*Segment)(nil)
|
|
|
|
|
|
|
|
func (e Segment) Bounds() (start, end int) {
|
|
|
|
return e.Start, e.Start
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Segment) AsImager() text.Imager { return e.Emoji }
|