diff --git a/src/main.js b/src/main.js index 913be14..1cc03f6 100644 --- a/src/main.js +++ b/src/main.js @@ -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(); diff --git a/src/module/moments.js b/src/module/moments.js new file mode 100644 index 0000000..017788a --- /dev/null +++ b/src/module/moments.js @@ -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; \ No newline at end of file diff --git a/src/styles/moment.scss b/src/styles/moment.scss index 0b6e1aa..d18c2d6 100644 --- a/src/styles/moment.scss +++ b/src/styles/moment.scss @@ -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; + } } \ No newline at end of file diff --git a/templates/moments.html b/templates/moments.html index 519eb57..fdf00b3 100644 --- a/templates/moments.html +++ b/templates/moments.html @@ -4,42 +4,101 @@ th:replace="~{modules/layout :: html(title = |#{page.moments.title} - ${site.title}|, header = null, content = ~{::content}, footer = null)}" > -
+