web: support content warnings

This commit is contained in:
William Casarin 2022-11-11 11:14:52 -08:00
parent 917fa90398
commit 17d4f9e468
3 changed files with 61 additions and 3 deletions

View file

@ -83,6 +83,17 @@ body {
background: rgba(0,0,0,0.4);
}
summary {
cursor: pointer;
}
details {
background-color: rgba(255,255,255,0.2);
padding: 10px;
margin-right: 30px;
border-radius: 20px;
}
textarea, input {
background-color: rgba(255,255,255,0.2);
border: 0;

View file

@ -45,6 +45,7 @@ function init_home_model() {
events: [],
chatrooms: {},
deletions: {},
cw_open: {},
deleted: {},
profiles: {},
profile_events: {},
@ -533,6 +534,13 @@ function redraw_events(model) {
//log_debug("rendering home view")
model.rendered = {}
model.events_el.innerHTML = render_events(model)
setup_home_event_handlers(model.events_el)
}
function setup_home_event_handlers(events_el)
{
for (const el of events_el.querySelectorAll(".cw"))
el.addEventListener("toggle", toggle_content_warning.bind(null))
}
function redraw_home_view(model) {
@ -1222,6 +1230,30 @@ function convert_quote_blocks(content, show_media)
}, "")
}
function get_content_warning(tags)
{
for (const tag of tags) {
if (tag.length >= 1 && tag[0] === "content-warning")
return tag[1] || ""
}
return null
}
function toggle_content_warning(e)
{
const el = e.target
const id = el.id.split("_")[1]
const ev = DSTATE.all_events[id]
if (!ev) {
log_debug("could not find content-warning event", id)
return
}
DSTATE.cw_open[id] = el.open
}
function format_content(ev, show_media)
{
if (ev.kind === 7) {
@ -1230,7 +1262,22 @@ function format_content(ev, show_media)
return sanitize(ev.content.trim())
}
return convert_quote_blocks(ev.content.trim(), show_media)
const content = ev.content.trim()
const body = convert_quote_blocks(content, show_media)
let cw = get_content_warning(ev.tags)
if (cw !== null) {
cw = cw === ""? "Content Warning" : `Content Warning: ${cw}`
const open = !!DSTATE.cw_open[ev.id]? "open" : ""
return `
<details class="cw" id="cw_${ev.id}" ${open}>
<summary>${cw}</summary>
${body}
</details>
`
}
return body
}
function sanitize(content)

View file

@ -6,7 +6,7 @@
<title>Damus</title>
<link rel="stylesheet" href="damus.css?v=17">
<link rel="stylesheet" href="damus.css?v=18">
</head>
<body>
<section class="header">
@ -41,7 +41,7 @@
<script src="noble-secp256k1.js?v=1"></script>
<script src="bech32.js?v=1"></script>
<script src="nostr.js?v=6"></script>
<script src="damus.js?v=54"></script>
<script src="damus.js?v=55"></script>
<script>
// I have to delay loading to wait for nos2x
const relay = setTimeout(damus_web_init, 100)