feat: support upvote and comment for moments

This commit is contained in:
Erzbir
2026-04-09 16:31:43 +08:00
parent 1a3fe6bb26
commit 61ea8bd77b
4 changed files with 185 additions and 24 deletions
+1
View File
@@ -3,6 +3,7 @@ import './styles/style.scss';
import './module/post.js' import './module/post.js'
import './module/header.js' import './module/header.js'
import './module/effect.js' import './module/effect.js'
import './module/moments.js'
terminal.refresh(); terminal.refresh();
+67
View File
@@ -0,0 +1,67 @@
import {terminal} from "../terminal.js";
const key = 'moment';
const group = 'moment.halo.run';
const plural = 'moments';
let upvotedNames = JSON.parse(localStorage.getItem('walker.upvoted.' + key + '.names') || '[]')
function upvoted(id) {
return upvotedNames.includes(id);
}
function handleUpvote(name) {
if (upvoted(name)) return;
const xhr = new XMLHttpRequest();
xhr.open('POST', '/apis/api.halo.run/v1alpha1/trackers/upvote');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = () => {
upvotedNames = [...upvotedNames, name];
localStorage.setItem('walker.upvoted.' + key + '.names', JSON.stringify(upvotedNames));
const upvoteNode = document.querySelector('[data-' + key + '-name="' + name + '"]');
if (!upvoteNode) return;
const upvoteCount = parseInt(upvoteNode.textContent || '0');
upvoteNode.textContent = upvoteCount + 1 + '';
};
xhr.onerror = function () {
alert('Network Error');
};
xhr.send(JSON.stringify({group: group, plural: plural, name: name}));
}
function register_upvoted() {
document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('.moment-upvote-btn').forEach(btn => {
const name = btn.dataset.name
if (upvoted(name)) {
btn.classList.add('active')
}
})
});
}
function showComment(name) {
const section = document.getElementById(`${name}`);
if (!section) return;
const show = section.classList.contains('active');
if (show) {
section.classList.remove('active');
} else {
section.classList.add('active');
}
}
terminal.registerInitFunc(register_upvoted)
window.handleUpvote = handleUpvote;
window.showComment = showComment;
+34
View File
@@ -11,4 +11,38 @@
object-fit: cover; object-fit: cover;
border-radius: var(--size-theme-font); border-radius: var(--size-theme-font);
transition: transform 0.3s ease; transition: transform 0.3s ease;
}
.moment-footer {
display: flex;
align-items: center;
gap: 1rem;
margin-top: calc(var(--size-theme-font) * 0.8);
}
.moment-action-btn {
padding: 0;
display: inline-flex;
align-items: center;
gap: 0.35em;
border: none;
font-size: calc(var(--size-theme-font) * 0.85);
color: var(--color-theme-foreground);
&:hover {
background: none;
}
&.active {
color: var(--color-theme-accent);
}
}
.moment-comment-section {
display: none;
&.active {
display: block;
}
} }
+83 -24
View File
@@ -4,42 +4,101 @@
th:replace="~{modules/layout :: html(title = |#{page.moments.title} - ${site.title}|, header = null, content = ~{::content}, footer = null)}" th:replace="~{modules/layout :: html(title = |#{page.moments.title} - ${site.title}|, header = null, content = ~{::content}, footer = null)}"
> >
<th:block th:fragment="content"> <th:block th:fragment="content">
<article <article th:each="moment, stat : ${moments.items}"
class="moment on-list post" th:with="
th:each="moment,iterStat : ${moments.items}" name=${moment.metadata.name},
th:with="content=${moment.spec.content}" content=${moment.spec.content},
th:attr="x-data=|{name:'${moment.metadata.name}',showComment:false}|"> medium=${content.medium},
upvoteCount=${moment.stats.upvote ?: 0},
commentCount=${moment.stats.approvedComment ?: 0},
commentId=|comment-${name}|,
galleryId=|gallery-${stat.index}|,
mediaSize=${#lists.size(medium)},
mediaGridClass=${mediaSize == 1 ? 'grid-cols-1' : (mediaSize < 5 ? 'grid-cols-2' : 'grid-cols-3')}
"
class="moment on-list post"
th:attr="x-data=|{ name: '${name}', showComment: false }|">
<header> <header>
<div class="post-meta"> <div class="post-meta">
<time class="post-date" th:text="${#dates.format(moment.spec.releaseTime,'yyyy-MM-dd')}"> <time class="post-date"
th:text="${#dates.format(moment.spec.releaseTime, 'yyyy-MM-dd')}">
Moment CreateTime Moment CreateTime
</time> </time>
<span class="post-author" th:with="owner = ${moment.owner}" th:text="${owner.displayName}"> <span class="post-author"
th:with="owner=${moment.owner}"
th:text="${owner.displayName}">
Author Author
</span> </span>
</div> </div>
</header> </header>
<div class="post-content" th:utext="${content.html}">Moment Content...</div>
<th:block th:with="medium=${moment.spec.content.medium}"> <div class="post-content" th:utext="${content.html}">
<div Moment Content...
th:unless="${#lists.isEmpty(moments)}" </div>
class="moment-media"
th:classappend="${(#lists.size(medium) > 4 ? 'grid-cols-3' : '') + (#lists.size(medium) == 1 ? 'grid-cols-1' :'') + ((#lists.size(medium) > 1 && #lists.size(medium) < 5) ? 'grid-cols-2' :'')}" <th:block th:if="${!#lists.isEmpty(medium)}">
> <div class="moment-media" th:classappend="${mediaGridClass}">
<div class="" th:each="media : ${medium}"> <div th:each="media : ${medium}">
<a th:class="|glightbox|" th:data-gallery="|gallery${iterStat.count}|" th:href="${media.url}"> <a class="glightbox"
<img th:data-gallery="${galleryId}"
th:if="${#strings.equals(media.type,'PHOTO')}" th:href="${media.url}">
class="transform-gpu rounded-lg object-cover" <img th:if="${media.type == 'PHOTO'}"
th:src="${media.url}" class="transform-gpu rounded-lg object-cover"
alt="photo"/> th:src="${media.url}"
alt="photo"/>
</a> </a>
<div th:if="${#strings.equals(media.type,'VIDEO')}">
<video controls th:src="${media.url}"></video> <video th:if="${media.type == 'VIDEO'}"
</div> controls
th:src="${media.url}"></video>
</div> </div>
</div> </div>
</th:block> </th:block>
<footer class="moment-footer">
<button class="moment-action-btn moment-upvote-btn"
th:attr="data-name=${name},data-count=${upvoteCount}"
onclick="handleUpvote(this.dataset.name)">
<svg class="icon-regular" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
fill="currentColor" width="16" height="16">
<path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
</svg>
<svg class="icon-pixel" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
width="16" height="16">
<path fill="currentColor"
d="M9 2H5v2H3v2H1v6h2v2h2v2h2v2h2v2h2v2h2v-2h2v-2h2v-2h2v-2h2v-2h2V6h-2V4h-2V2h-4v2h-2v2h-2V4H9V2zm0 2v2h2v2h2V6h2V4h4v2h2v6h-2v2h-2v2h-2v2h-2v2h-2v-2H9v-2H7v-2H5v-2H3V6h2V4h4z"/>
</svg>
<span class="upvote-count"
th:attr="data-moment-name=${name}"
th:text="${upvoteCount}">0</span>
</button>
<button class="moment-action-btn moment-comment-btn"
th:attr="data-target=${commentId}"
onclick="showComment(this.dataset.target)">
<svg class="icon-regular" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
fill="currentColor" width="16" height="16">
<path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2z"/>
</svg>
<svg class="icon-pixel" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
width="16" height="16">
<path fill="currentColor"
d="M22 2H2v14h2V4h16v12h-8v2h-2v2H8v-4H2v2h4v4h4v-2h2v-2h10V2z"/>
</svg>
<span th:text="${commentCount}">0</span>
</button>
</footer>
<div class="moment-comment-section"
th:id="${commentId}"
th:attr="data-name=${name}">
<th:block th:if="${haloCommentEnabled}"
th:replace="~{modules/comment :: comment(
group = 'moment.halo.run',
kind = 'Moment',
name = ${name}
)}"/>
</div>
</article> </article>
<nav class="pagination" th:if="${moments.hasPrevious() || moments.hasNext()}"> <nav class="pagination" th:if="${moments.hasPrevious() || moments.hasNext()}">
<div class="pagination__buttons"> <div class="pagination__buttons">