Inline new comments code. Other small fixes

remotes/1693045480750635534/spooky-22
Yo Mama 2021-09-27 23:56:08 +02:00
parent a365bd9a7b
commit 530de5ee08
2 changed files with 63 additions and 27 deletions

View File

@ -68,6 +68,50 @@
{% if v %}
<script src="https://js.pusher.com/beams/1.0/push-notifications-cdn.js"></script>
<script src="/assets/js/default.js"></script>
<script>
// New comment counts
// localstorage comment counts format: {"<postId>": {c: <totalComments>, t: <timestampUpdated>}}
const COMMENT_COUNTS_ID = "comment-counts"
/**
* Display the number of new comments present since the last time the post was opened
*/
function showNewCommentCounts(postId, newTotal) {
const comments = JSON.parse(localStorage.getItem(COMMENT_COUNTS_ID)) || {}
const lastCount = comments[postId]
if (lastCount) {
const newComments = newTotal - lastCount.c
if (newComments > 0) {
document.querySelectorAll(`#post-${postId} .new-comments`).forEach(elem => {
elem.textContent = ` (+${newComments})`
elem.classList.remove("d-none")
})
}
}
}
function incrementCommentCount(postId) {
saveCommentsCount(postId)
}
/**
* Saves the comment count to the localStorage
*
* @param postId The id of the post associated with the comments
* @param lastTotalComs The new amount, If null it will just increment the previous amount
*/
function saveCommentsCount(postId, lastTotalComs = null) {
const comments = JSON.parse(localStorage.getItem(COMMENT_COUNTS_ID)) || {}
const newTotal = lastTotalComs || ((comments[postId] || { c: 0 }).c + 1)
comments[postId] = { c: newTotal, t: Date.now() }
window.localStorage.setItem(COMMENT_COUNTS_ID, JSON.stringify(comments))
}
</script>
{% endif %}

View File

@ -33,15 +33,6 @@
</script>
{% endif %}
<script>
var date = new Date('{{p.created_datetime}}');
document.getElementById('timestamp').title = date.toString();
{% if p.edited_utc %}
var date = new Date('{{p.edited_datetime}}');
document.getElementById('edited_timestamp').title = date.toString();
{% endif %}
</script>
{% if p.award_count("shit") %}
<script src="/assets/js/bug-min.js"></script>
{% set minbugs = 10*p.award_count("shit") if p.award_count("shit") < 3 else 20 %}
@ -587,6 +578,25 @@
</div>
<script>
// closure to avoid variable conflicts
(() => {
const date = new Date('{{p.created_datetime}}');
document.getElementById('timestamp').title = date.toString();
{% if p.edited_utc %}
const dateEdited = new Date('{{p.edited_datetime}}');
document.getElementById('edited_timestamp').title = dateEdited.toString();
{% endif %}
{% if (not v or v.highlightcomments) %}
showNewCommentCounts('{{p.id}}', {{p.comment_count}})
{% endif %}
saveCommentsCount('{{p.id}}', {{p.comment_count}})
})()
</script>
<div class="row border-md-0 comment-section pb-3">
<div class="col border-top">
<div class="comments-count py-3">
@ -692,24 +702,6 @@
</div>
<script>
// closure to avoid variable conflicts
(() => {
const date = new Date('{{p.created_datetime}}');
document.getElementById('timestamp').title = date.toString();
{% if p.edited_utc %}
const dateEdited = new Date('{{p.edited_datetime}}');
document.getElementById('edited_timestamp').title = dateEdited.toString();
{% endif %}
{% if (not v or v.highlightcomments) %}
showNewCommentCounts('{{p.id}}', {{p.comment_count}})
{% endif %}
saveCommentsCount('{{p.id}}', {{p.comment_count}})
})()
</script>
{% if v and v.id==p.author_id %}
{% include "delete_post_modal.html" %}
{% endif %}