forked from tangled.org/core
this repo has no description

(wip)appview: add copy button

right now, only the textnode inside the button gets added, without the
<button> around it. can't figure out why. the DataAtom and Type match
what html.Parse gives, per https://go.dev/play/p/eW2o3v2eBbY

Changed files
+68
appview
pages
markup
+68
appview/pages/markup/markdown.go
··· 20 20 "github.com/yuin/goldmark/text" 21 21 "github.com/yuin/goldmark/util" 22 22 htmlparse "golang.org/x/net/html" 23 + "golang.org/x/net/html/atom" 23 24 24 25 "tangled.sh/tangled.sh/core/appview/pages/repoinfo" 25 26 ) ··· 151 152 node.Attr[i] = attr 152 153 } 153 154 } 155 + } 156 + 157 + if node.Data == "pre" { 158 + // TODO only show when :hover or :focus on the pre element 159 + button := &htmlparse.Node{ 160 + Type: htmlparse.ElementNode, 161 + DataAtom: atom.Button, 162 + Data: "button", 163 + Attr: []htmlparse.Attribute{ 164 + { 165 + Key: "class", 166 + Val: "absolute top-2 right-2 btn", 167 + }, 168 + { 169 + Key: "style", 170 + // FIXME .#watch-tailwind doesnt seem to catch top-2 and right-2, probably cuz it's not used anywhere inside of templates/ ? 171 + Val: "top: 0.5rem; right: 0.5rem;", 172 + }, 173 + { 174 + Key: "onclick", 175 + Val: ` 176 + navigator.clipboard.writeText(this.closest('pre').querySelector('code').innerText); 177 + this.innerText = 'Copied!'; 178 + setTimeout(() => { this.innerText = 'Copy' }, 1500); 179 + `, 180 + }, 181 + { 182 + Key: "onload", 183 + Val: "this.removeAttribute('aria-hidden')", 184 + }, 185 + { 186 + Key: "aria-hidden", 187 + Val: "true", 188 + }, 189 + { 190 + Key: "title", 191 + Val: "Copy to clipboard", 192 + }, 193 + }, 194 + } 195 + 196 + // TODO 197 + // if copyIcon, err := icons.IconNode("copy", "h-4", "w-4"); err != nil { 198 + // button.AppendChild(copyIcon) 199 + // } else { 200 + button.AppendChild(&htmlparse.Node{ 201 + Type: htmlparse.TextNode, 202 + Data: "Copy", 203 + }) 204 + 205 + var classWasSetOnNode bool 206 + for i, attr := range node.Attr { 207 + if attr.Key == "class" { 208 + node.Attr[i].Val += " relative" 209 + classWasSetOnNode = true 210 + break 211 + } 212 + } 213 + 214 + if !classWasSetOnNode { 215 + node.Attr = append(node.Attr, htmlparse.Attribute{ 216 + Key: "class", 217 + Val: "relative", 218 + }) 219 + } 220 + 221 + node.AppendChild(button) 154 222 } 155 223 156 224 for n := node.FirstChild; n != nil; n = n.NextSibling {