feat(web): Markdown editor with toolbar limited to what is rendered
Legal documents, maintenance and status page descriptions get an editor with a toolbar of the formatting their field supports, keyboard shortcuts, list continuation, a character count and a preview rendered by the server. Images are no longer rendered: an external image on a status page would hand every visitor's address to its host, so the editor offers none. Editor behavior is tested in a headless browser when Chromium is available. Closes #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
532bb0d1b0
commit
d7126b1e2c
20 files changed
+1206
-602
No files matched your search
@@ -0,0 +1,59 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
||||
)
|
||||
|
||||
// Markdown editor feature sets. Every entry is something RenderMarkdown
|
||||
// shows; images and raw HTML are not, so the editor offers neither.
|
||||
var (
|
||||
// MarkdownDocument suits standalone pages such as a legal notice.
|
||||
MarkdownDocument = []string{"heading", "bold", "italic", "strike", "link", "ul", "ol", "quote", "code", "codeblock", "table", "hr"}
|
||||
// MarkdownInline suits descriptions shown inside a page, where headings
|
||||
// and tables would compete with the page's own structure.
|
||||
MarkdownInline = []string{"bold", "italic", "strike", "link", "ul", "ol", "quote", "code"}
|
||||
)
|
||||
|
||||
var markdownGlyphs = map[string]string{
|
||||
"heading": "H", "bold": "B", "italic": "I", "strike": "S", "link": "↗", "ul": "•", "ol": "1.",
|
||||
"quote": "❝", "code": "`", "codeblock": "{ }", "table": "▦", "hr": "—",
|
||||
}
|
||||
|
||||
var markdownGlyphClass = map[string]string{"bold": "font-bold", "italic": "italic", "strike": "line-through"}
|
||||
|
||||
// MarkdownEditor is a textarea with a toolbar of features, a preview
|
||||
// rendered by the server, list continuation and a character count.
|
||||
templ MarkdownEditor(name, value string, rows, maxLength int, features []string) {
|
||||
<div data-md-editor class="border border-rule focus-within:border-ink">
|
||||
<div class="flex flex-wrap items-center border-b border-rule">
|
||||
<div role="toolbar" aria-label={ i18n.T(ctx, "md.toolbar") } class="flex flex-wrap" data-md-toolbar>
|
||||
for _, f := range features {
|
||||
<button
|
||||
type="button"
|
||||
data-md-action={ f }
|
||||
title={ i18n.T(ctx, "md."+f) }
|
||||
aria-label={ i18n.T(ctx, "md."+f) }
|
||||
class={ "min-w-8 px-2 py-1 font-mono text-xs text-muted hover:bg-rule/50 hover:text-ink", markdownGlyphClass[f] }
|
||||
>{ markdownGlyphs[f] }</button>
|
||||
}
|
||||
</div>
|
||||
<div class="ml-auto flex text-xs" role="tablist">
|
||||
<button type="button" role="tab" data-md-tab="write" aria-selected="true" class="px-3 py-1 aria-selected:bg-ink aria-selected:text-paper">{ i18n.T(ctx, "md.write") }</button>
|
||||
<button type="button" role="tab" data-md-tab="preview" aria-selected="false" class="px-3 py-1 aria-selected:bg-ink aria-selected:text-paper">{ i18n.T(ctx, "md.preview") }</button>
|
||||
</div>
|
||||
</div>
|
||||
<textarea
|
||||
name={ name }
|
||||
rows={ strconv.Itoa(rows) }
|
||||
maxlength={ strconv.Itoa(maxLength) }
|
||||
class="block w-full resize-y bg-transparent px-2.5 py-2 font-mono text-xs leading-relaxed outline-none"
|
||||
>{ value }</textarea>
|
||||
<div data-md-preview hidden class="legal-text min-h-24 px-3 py-2" data-empty={ i18n.T(ctx, "md.empty") }></div>
|
||||
<div class="flex justify-between gap-4 border-t border-rule px-2.5 py-1 text-xs text-muted">
|
||||
<span>{ i18n.T(ctx, "md.hint") }</span>
|
||||
<span class="font-mono" data-md-count></span>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
Reference in new issue
Block a user