refactor: move tenant templates to internal/tenant
- Move internal/build/ to internal/tenant/ - Rename assets for clarity - Add tenant-blog.js for shared blog functionality - Update style.css with improved code block styling Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
bef5dd4437
commit
6e2959f619
11 changed files with 153 additions and 358 deletions
17
internal/tenant/assets/assets.go
Normal file
17
internal/tenant/assets/assets.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package assets
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Embedded static assets for tenant blogs
|
||||
//
|
||||
//go:embed css js
|
||||
var staticFS embed.FS
|
||||
|
||||
func Handler() http.Handler {
|
||||
sub, _ := fs.Sub(staticFS, ".")
|
||||
return http.FileServer(http.FS(sub))
|
||||
}
|
||||
702
internal/tenant/assets/css/style.css
Normal file
702
internal/tenant/assets/css/style.css
Normal file
|
|
@ -0,0 +1,702 @@
|
|||
/* WriteKit - Blog Stylesheet */
|
||||
|
||||
*, *::before, *::after {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
:root {
|
||||
--accent: #2563eb;
|
||||
--font-body: system-ui, -apple-system, sans-serif;
|
||||
--font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
|
||||
|
||||
--text: #18181b;
|
||||
--text-muted: #71717a;
|
||||
--bg: #ffffff;
|
||||
--bg-secondary: #fafafa;
|
||||
--border: #e4e4e7;
|
||||
|
||||
--content-width: 680px;
|
||||
--spacing: 1.5rem;
|
||||
|
||||
/* Compactness defaults (cozy) */
|
||||
--content-spacing: 1.75rem;
|
||||
--paragraph-spacing: 1.25rem;
|
||||
--heading-spacing: 2.5rem;
|
||||
--line-height: 1.7;
|
||||
}
|
||||
|
||||
/* Compactness: Compact */
|
||||
.compactness-compact {
|
||||
--content-spacing: 1.25rem;
|
||||
--paragraph-spacing: 0.875rem;
|
||||
--heading-spacing: 1.75rem;
|
||||
--line-height: 1.55;
|
||||
}
|
||||
|
||||
/* Compactness: Cozy (default) */
|
||||
.compactness-cozy {
|
||||
--content-spacing: 1.75rem;
|
||||
--paragraph-spacing: 1.25rem;
|
||||
--heading-spacing: 2.5rem;
|
||||
--line-height: 1.7;
|
||||
}
|
||||
|
||||
/* Compactness: Spacious */
|
||||
.compactness-spacious {
|
||||
--content-spacing: 2.25rem;
|
||||
--paragraph-spacing: 1.5rem;
|
||||
--heading-spacing: 3rem;
|
||||
--line-height: 1.85;
|
||||
}
|
||||
|
||||
/* Layout: Minimal */
|
||||
.layout-minimal .site-header {
|
||||
justify-content: center;
|
||||
}
|
||||
.layout-minimal .site-nav {
|
||||
display: none;
|
||||
}
|
||||
.layout-minimal .site-footer {
|
||||
border-top: none;
|
||||
opacity: 0.7;
|
||||
}
|
||||
.layout-minimal .profile {
|
||||
margin-bottom: 3.5rem;
|
||||
}
|
||||
|
||||
/* Layout: Magazine */
|
||||
.layout-magazine .posts-list {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
|
||||
gap: 1.25rem;
|
||||
}
|
||||
.layout-magazine .post-card {
|
||||
padding: 1.5rem;
|
||||
border: 1px solid var(--border);
|
||||
background: var(--bg);
|
||||
transition: border-color 0.2s, box-shadow 0.2s;
|
||||
}
|
||||
.layout-magazine .post-card:hover {
|
||||
border-color: var(--accent);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.04);
|
||||
}
|
||||
.layout-magazine .post-card-title {
|
||||
font-size: 1.0625rem;
|
||||
}
|
||||
.layout-magazine .post-card-description {
|
||||
font-size: 0.875rem;
|
||||
-webkit-line-clamp: 2;
|
||||
display: -webkit-box;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
html {
|
||||
font-size: 16px;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: var(--font-body);
|
||||
color: var(--text);
|
||||
background: var(--bg);
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
a {
|
||||
color: var(--accent);
|
||||
text-decoration: none;
|
||||
transition: color 0.15s;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
text-underline-offset: 2px;
|
||||
}
|
||||
|
||||
/* Header */
|
||||
.site-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
max-width: var(--content-width);
|
||||
margin: 0 auto;
|
||||
padding: 2rem var(--spacing);
|
||||
}
|
||||
|
||||
.site-name {
|
||||
font-weight: 600;
|
||||
font-size: 1rem;
|
||||
color: var(--text);
|
||||
letter-spacing: -0.01em;
|
||||
}
|
||||
|
||||
.site-name:hover {
|
||||
text-decoration: none;
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.site-nav {
|
||||
display: flex;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
.site-nav a {
|
||||
color: var(--text-muted);
|
||||
font-size: 0.9375rem;
|
||||
font-weight: 450;
|
||||
}
|
||||
|
||||
.site-nav a:hover {
|
||||
color: var(--text);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
/* Main */
|
||||
main {
|
||||
max-width: var(--content-width);
|
||||
margin: 0 auto;
|
||||
padding: 0 var(--spacing) 3rem;
|
||||
}
|
||||
|
||||
/* Footer */
|
||||
.site-footer {
|
||||
max-width: var(--content-width);
|
||||
margin: 0 auto;
|
||||
padding: 2.5rem var(--spacing);
|
||||
text-align: center;
|
||||
color: var(--text-muted);
|
||||
font-size: 0.8125rem;
|
||||
border-top: 1px solid var(--border);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.powered-by {
|
||||
color: var(--text-muted);
|
||||
text-decoration: none;
|
||||
font-size: 0.75rem;
|
||||
opacity: 0.7;
|
||||
transition: opacity 0.2s;
|
||||
}
|
||||
|
||||
.powered-by:hover {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
/* Profile */
|
||||
.profile {
|
||||
text-align: center;
|
||||
margin-bottom: 3rem;
|
||||
}
|
||||
|
||||
.profile-avatar {
|
||||
width: 72px;
|
||||
height: 72px;
|
||||
border-radius: 50%;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.profile-name {
|
||||
font-size: 1.125rem;
|
||||
font-weight: 600;
|
||||
margin-bottom: 0.25rem;
|
||||
letter-spacing: -0.01em;
|
||||
}
|
||||
|
||||
.profile-bio {
|
||||
color: var(--text-muted);
|
||||
max-width: 380px;
|
||||
margin: 0 auto;
|
||||
font-size: 0.9375rem;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
/* Post List */
|
||||
.posts-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--content-spacing);
|
||||
}
|
||||
|
||||
.post-card a {
|
||||
display: block;
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
.post-card a:hover {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.post-card-date {
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-muted);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.post-card-title {
|
||||
margin: 0.375rem 0 0;
|
||||
font-size: 1.1875rem;
|
||||
font-weight: 600;
|
||||
letter-spacing: -0.015em;
|
||||
line-height: 1.35;
|
||||
}
|
||||
|
||||
.post-card a:hover .post-card-title {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.post-card-description {
|
||||
margin: 0.5rem 0 0;
|
||||
color: var(--text-muted);
|
||||
font-size: 0.9375rem;
|
||||
line-height: 1.55;
|
||||
}
|
||||
|
||||
.no-posts {
|
||||
color: var(--text-muted);
|
||||
text-align: center;
|
||||
padding: 3rem 0;
|
||||
}
|
||||
|
||||
/* Pagination */
|
||||
.pagination {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
margin-top: 3rem;
|
||||
padding-top: 1.5rem;
|
||||
border-top: 1px solid var(--border);
|
||||
font-size: 0.9375rem;
|
||||
}
|
||||
|
||||
/* Post Header */
|
||||
.post-header {
|
||||
margin-bottom: 2.5rem;
|
||||
}
|
||||
|
||||
.post-date {
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-muted);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.post-title {
|
||||
margin: 0.5rem 0 0;
|
||||
font-size: 2.25rem;
|
||||
font-weight: 700;
|
||||
line-height: 1.2;
|
||||
letter-spacing: -0.025em;
|
||||
}
|
||||
|
||||
.post-description {
|
||||
font-size: 1.125rem;
|
||||
color: var(--text-muted);
|
||||
margin: 0.75rem 0 0;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.post-tags {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem;
|
||||
margin-top: 1.25rem;
|
||||
}
|
||||
|
||||
.tag {
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-muted);
|
||||
background: var(--bg-secondary);
|
||||
padding: 0.25rem 0.625rem;
|
||||
border-radius: 0.25rem;
|
||||
}
|
||||
|
||||
.post-cover {
|
||||
margin: 1.75rem 0 0;
|
||||
}
|
||||
|
||||
.post-cover img {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
border-radius: 0.5rem;
|
||||
aspect-ratio: 16 / 9;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
/* Prose */
|
||||
.prose {
|
||||
line-height: var(--line-height);
|
||||
font-size: 1.0625rem;
|
||||
}
|
||||
|
||||
.prose > *:first-child {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.prose h2 {
|
||||
margin-top: var(--heading-spacing);
|
||||
margin-bottom: var(--paragraph-spacing);
|
||||
font-size: 1.5rem;
|
||||
font-weight: 650;
|
||||
letter-spacing: -0.02em;
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
.prose h3 {
|
||||
margin-top: calc(var(--heading-spacing) * 0.8);
|
||||
margin-bottom: calc(var(--paragraph-spacing) * 0.8);
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
letter-spacing: -0.015em;
|
||||
line-height: 1.35;
|
||||
}
|
||||
|
||||
.prose h4 {
|
||||
margin-top: calc(var(--heading-spacing) * 0.65);
|
||||
margin-bottom: calc(var(--paragraph-spacing) * 0.65);
|
||||
font-size: 1.0625rem;
|
||||
font-weight: 600;
|
||||
letter-spacing: -0.01em;
|
||||
}
|
||||
|
||||
.prose p {
|
||||
margin: var(--paragraph-spacing) 0;
|
||||
}
|
||||
|
||||
.prose ul, .prose ol {
|
||||
margin: var(--paragraph-spacing) 0;
|
||||
padding-left: 1.375rem;
|
||||
}
|
||||
|
||||
.prose li {
|
||||
margin: calc(var(--paragraph-spacing) * 0.4) 0;
|
||||
padding-left: 0.25rem;
|
||||
}
|
||||
|
||||
.prose li::marker {
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.prose blockquote {
|
||||
margin: var(--content-spacing) 0;
|
||||
padding: 0 0 0 1.25rem;
|
||||
border-left: 2px solid var(--border);
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.prose blockquote p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.prose pre:not(.chroma) {
|
||||
margin: var(--content-spacing) 0;
|
||||
padding: 1.125rem 1.25rem;
|
||||
background: var(--bg-secondary);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 0.375rem;
|
||||
overflow-x: auto;
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.prose pre.chroma {
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.6;
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
.prose code {
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.875em;
|
||||
background: var(--bg-secondary);
|
||||
padding: 0.175rem 0.375rem;
|
||||
border-radius: 0.25rem;
|
||||
font-variant-ligatures: none;
|
||||
}
|
||||
|
||||
.prose pre code {
|
||||
background: none;
|
||||
padding: 0;
|
||||
font-size: inherit;
|
||||
}
|
||||
|
||||
/* Enhanced Code Block */
|
||||
.code-block {
|
||||
margin: var(--content-spacing) 0;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 0.5rem;
|
||||
overflow: hidden;
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
|
||||
.code-block pre {
|
||||
margin: 0;
|
||||
border: none;
|
||||
border-radius: 0;
|
||||
padding: 1rem 1.25rem;
|
||||
}
|
||||
|
||||
.code-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.625rem 1rem;
|
||||
background: var(--bg);
|
||||
border-bottom: 1px solid var(--border);
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.code-icon {
|
||||
width: 1rem;
|
||||
height: 1rem;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.code-title {
|
||||
flex: 1;
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.8125rem;
|
||||
}
|
||||
|
||||
.code-copy {
|
||||
padding: 0.25rem 0.625rem;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
color: var(--text-muted);
|
||||
background: var(--bg-secondary);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 0.25rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.15s;
|
||||
font-family: var(--font-body);
|
||||
}
|
||||
|
||||
.code-copy:hover {
|
||||
color: var(--text);
|
||||
background: var(--bg);
|
||||
border-color: var(--text-muted);
|
||||
}
|
||||
|
||||
.prose img {
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
border-radius: 0.375rem;
|
||||
margin: var(--content-spacing) 0;
|
||||
}
|
||||
|
||||
.prose a {
|
||||
text-decoration: underline;
|
||||
text-underline-offset: 2px;
|
||||
text-decoration-color: color-mix(in srgb, var(--accent) 40%, transparent);
|
||||
}
|
||||
|
||||
.prose a:hover {
|
||||
text-decoration-color: var(--accent);
|
||||
}
|
||||
|
||||
.prose hr {
|
||||
margin: var(--heading-spacing) 0;
|
||||
border: none;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.prose strong {
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.prose table {
|
||||
width: 100%;
|
||||
margin: var(--content-spacing) 0;
|
||||
border-collapse: collapse;
|
||||
font-size: 0.9375rem;
|
||||
}
|
||||
|
||||
.prose th, .prose td {
|
||||
padding: 0.625rem 0.75rem;
|
||||
border: 1px solid var(--border);
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.prose th {
|
||||
background: var(--bg-secondary);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
/* Reactions & Comments */
|
||||
.reactions, .comments {
|
||||
margin-top: 3.5rem;
|
||||
padding-top: 2rem;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.comments-title {
|
||||
margin: 0 0 1.25rem;
|
||||
font-size: 1.125rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.reaction-buttons {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.reaction-btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.375rem;
|
||||
padding: 0.5rem 0.875rem;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 2rem;
|
||||
background: var(--bg);
|
||||
cursor: pointer;
|
||||
font-size: 1rem;
|
||||
transition: all 0.15s;
|
||||
}
|
||||
|
||||
.reaction-btn:hover {
|
||||
background: var(--bg-secondary);
|
||||
border-color: color-mix(in srgb, var(--accent) 50%, var(--border));
|
||||
}
|
||||
|
||||
.reaction-btn.active {
|
||||
background: var(--accent);
|
||||
border-color: var(--accent);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.reaction-btn .count {
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
/* Comment Form */
|
||||
.comment-form textarea {
|
||||
width: 100%;
|
||||
padding: 0.875rem;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 0.375rem;
|
||||
font-family: inherit;
|
||||
font-size: 0.9375rem;
|
||||
resize: vertical;
|
||||
min-height: 100px;
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
transition: border-color 0.15s;
|
||||
}
|
||||
|
||||
.comment-form textarea:focus {
|
||||
outline: none;
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.comment-form button {
|
||||
margin-top: 0.75rem;
|
||||
padding: 0.5rem 1rem;
|
||||
background: var(--accent);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 0.375rem;
|
||||
cursor: pointer;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
transition: opacity 0.15s;
|
||||
}
|
||||
|
||||
.comment-form button:hover {
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
/* Comments List */
|
||||
.comments-list {
|
||||
margin-top: 1.5rem;
|
||||
}
|
||||
|
||||
.comment {
|
||||
padding: 1.25rem 0;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.comment:first-child {
|
||||
padding-top: 0;
|
||||
}
|
||||
|
||||
.comment:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.comment-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.625rem;
|
||||
margin-bottom: 0.625rem;
|
||||
}
|
||||
|
||||
.comment-avatar {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
.comment-author {
|
||||
font-weight: 500;
|
||||
font-size: 0.9375rem;
|
||||
}
|
||||
|
||||
.comment-date {
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.comment-content {
|
||||
font-size: 0.9375rem;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
/* Auth Prompt */
|
||||
.auth-prompt {
|
||||
padding: 1.25rem;
|
||||
background: var(--bg-secondary);
|
||||
border-radius: 0.375rem;
|
||||
text-align: center;
|
||||
font-size: 0.9375rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.auth-prompt a {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
/* Mobile */
|
||||
@media (max-width: 640px) {
|
||||
:root {
|
||||
--spacing: 1.25rem;
|
||||
}
|
||||
|
||||
.site-header {
|
||||
padding: 1.5rem var(--spacing);
|
||||
}
|
||||
|
||||
.post-title {
|
||||
font-size: 1.75rem;
|
||||
}
|
||||
|
||||
.prose {
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.prose h2 {
|
||||
font-size: 1.375rem;
|
||||
}
|
||||
|
||||
.prose h3 {
|
||||
font-size: 1.125rem;
|
||||
}
|
||||
}
|
||||
10
internal/tenant/assets/js/tenant-blog.js
Normal file
10
internal/tenant/assets/js/tenant-blog.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(function() {
|
||||
'use strict';
|
||||
|
||||
const channel = new BroadcastChannel('writekit-studio');
|
||||
channel.onmessage = function(event) {
|
||||
if (event.data.type === 'settings-changed') {
|
||||
location.reload();
|
||||
}
|
||||
};
|
||||
})();
|
||||
197
internal/tenant/assets/js/tenant-post.js
Normal file
197
internal/tenant/assets/js/tenant-post.js
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
var WriteKit = (function() {
|
||||
'use strict';
|
||||
|
||||
let config = {};
|
||||
let user = null;
|
||||
|
||||
async function init(opts) {
|
||||
config = opts;
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/reader/me');
|
||||
const data = await res.json();
|
||||
if (data.logged_in) {
|
||||
user = data.user;
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
if (config.reactions) initReactions();
|
||||
if (config.comments) initComments();
|
||||
}
|
||||
|
||||
async function initReactions() {
|
||||
const container = document.querySelector('.reactions-container');
|
||||
if (!container) return;
|
||||
|
||||
const res = await fetch(`/api/reader/posts/${config.slug}/reactions`);
|
||||
const data = await res.json();
|
||||
|
||||
const counts = data.counts || {};
|
||||
const userReactions = data.user || [];
|
||||
|
||||
if (config.reactionMode === 'upvote') {
|
||||
const emoji = config.reactionEmojis[0] || '👍';
|
||||
const count = counts[emoji] || 0;
|
||||
const active = userReactions.includes(emoji);
|
||||
|
||||
container.innerHTML = `
|
||||
<button class="reaction-btn ${active ? 'active' : ''}" data-emoji="${emoji}">
|
||||
<span class="emoji">${emoji}</span>
|
||||
<span class="count">${count}</span>
|
||||
</button>
|
||||
`;
|
||||
} else {
|
||||
container.innerHTML = config.reactionEmojis.map(emoji => {
|
||||
const count = counts[emoji] || 0;
|
||||
const active = userReactions.includes(emoji);
|
||||
return `
|
||||
<button class="reaction-btn ${active ? 'active' : ''}" data-emoji="${emoji}">
|
||||
<span class="emoji">${emoji}</span>
|
||||
<span class="count">${count}</span>
|
||||
</button>
|
||||
`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
container.querySelectorAll('.reaction-btn').forEach(btn => {
|
||||
btn.addEventListener('click', () => toggleReaction(btn));
|
||||
});
|
||||
}
|
||||
|
||||
async function toggleReaction(btn) {
|
||||
if (config.requireAuth && !user) {
|
||||
showAuthPrompt('reactions');
|
||||
return;
|
||||
}
|
||||
|
||||
const emoji = btn.dataset.emoji;
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/reader/posts/${config.slug}/reactions`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ emoji })
|
||||
});
|
||||
|
||||
if (res.status === 401) {
|
||||
showAuthPrompt('reactions');
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
const countEl = btn.querySelector('.count');
|
||||
const current = parseInt(countEl.textContent) || 0;
|
||||
|
||||
if (data.added) {
|
||||
btn.classList.add('active');
|
||||
countEl.textContent = current + 1;
|
||||
} else {
|
||||
btn.classList.remove('active');
|
||||
countEl.textContent = Math.max(0, current - 1);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to toggle reaction', e);
|
||||
}
|
||||
}
|
||||
|
||||
async function initComments() {
|
||||
const section = document.querySelector('.comments');
|
||||
if (!section) return;
|
||||
|
||||
const list = section.querySelector('.comments-list');
|
||||
const formContainer = section.querySelector('.comment-form-container');
|
||||
|
||||
const res = await fetch(`/api/reader/posts/${config.slug}/comments`);
|
||||
const comments = await res.json();
|
||||
|
||||
if (comments && comments.length > 0) {
|
||||
list.innerHTML = comments.map(renderComment).join('');
|
||||
} else {
|
||||
list.innerHTML = '<p class="no-comments">No comments yet. Be the first!</p>';
|
||||
}
|
||||
|
||||
if (user) {
|
||||
formContainer.innerHTML = `
|
||||
<form class="comment-form">
|
||||
<textarea placeholder="Write a comment..." rows="3" required></textarea>
|
||||
<button type="submit">Post Comment</button>
|
||||
</form>
|
||||
`;
|
||||
formContainer.querySelector('form').addEventListener('submit', submitComment);
|
||||
} else {
|
||||
formContainer.innerHTML = `
|
||||
<div class="auth-prompt">
|
||||
<a href="/api/reader/login/github?redirect=${encodeURIComponent(window.location.pathname)}">Sign in</a> to leave a comment
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
function renderComment(comment) {
|
||||
const date = new Date(comment.created_at).toLocaleDateString('en-US', {
|
||||
year: 'numeric', month: 'short', day: 'numeric'
|
||||
});
|
||||
|
||||
return `
|
||||
<div class="comment" data-id="${comment.id}">
|
||||
<div class="comment-header">
|
||||
${comment.avatar_url ? `<img src="${comment.avatar_url}" alt="" class="comment-avatar">` : ''}
|
||||
<span class="comment-author">${escapeHtml(comment.name || 'Anonymous')}</span>
|
||||
<span class="comment-date">${date}</span>
|
||||
</div>
|
||||
<div class="comment-content">${comment.content_html || escapeHtml(comment.content)}</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
async function submitComment(e) {
|
||||
e.preventDefault();
|
||||
const form = e.target;
|
||||
const textarea = form.querySelector('textarea');
|
||||
const content = textarea.value.trim();
|
||||
|
||||
if (!content) return;
|
||||
|
||||
const btn = form.querySelector('button');
|
||||
btn.disabled = true;
|
||||
btn.textContent = 'Posting...';
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/reader/posts/${config.slug}/comments`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ content })
|
||||
});
|
||||
|
||||
if (res.status === 401) {
|
||||
showAuthPrompt('comments');
|
||||
return;
|
||||
}
|
||||
|
||||
const comment = await res.json();
|
||||
const list = document.querySelector('.comments-list');
|
||||
const noComments = list.querySelector('.no-comments');
|
||||
if (noComments) noComments.remove();
|
||||
|
||||
list.insertAdjacentHTML('beforeend', renderComment(comment));
|
||||
textarea.value = '';
|
||||
} catch (e) {
|
||||
console.error('Failed to post comment', e);
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
btn.textContent = 'Post Comment';
|
||||
}
|
||||
}
|
||||
|
||||
function showAuthPrompt(feature) {
|
||||
alert(`Please sign in to use ${feature}`);
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
return { init };
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue