feat: support upvote and comment for moments
This commit is contained in:
@@ -3,6 +3,7 @@ import './styles/style.scss';
|
||||
import './module/post.js'
|
||||
import './module/header.js'
|
||||
import './module/effect.js'
|
||||
import './module/moments.js'
|
||||
|
||||
terminal.refresh();
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -11,4 +11,38 @@
|
||||
object-fit: cover;
|
||||
border-radius: var(--size-theme-font);
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user