Skip to content

Commit 60900c0

Browse files
committed
Scroll Top on Blog Entry Change
1 parent dc1ca74 commit 60900c0

2 files changed

Lines changed: 51 additions & 56 deletions

File tree

docs/js/BlogManager.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,15 @@
11
// Simple blog manager for my procstack blog
2+
// Update : Not so simple blog manager
3+
// Being added into ProcPages as a module for displaying blogs and comments
4+
// Still working out making the comments more universal, but it's newly added
25

36

47
import { blogEntries as procBlogEntries } from './blog/blogEntries.js';
58
import { blogEntry, ENTRY_MARK_ENUM } from './blog/blogEntryBase.js';
69
import { CommentManager } from './blog/CommentManager.js';
710

8-
/* comment manager -
9-
10-
const comments = new CommentManager({
11-
repo: "YOUR_NAME/YOUR_REPO",
12-
repoId: "YOUR_REPO_ID",
13-
category: "General",
14-
categoryId: "YOUR_CATEGORY_ID",
15-
containerId: "comments",
16-
});
17-
18-
comments.load(post.slug);
19-
comments.unload();
20-
*/
21-
2211

2312
export class BlogManager {
24-
/**
25-
* @param {HTMLElement} listParent - DOM parent for the blog listing (buttons/links)
26-
* @param {HTMLElement} contentParent - DOM parent where blog content entries will be built
27-
* @param {Object} [options] - Optional settings: { entryData: blogEntry[], listingContainer: String, entryContainer: String, spacerStyle: String[] }
28-
*/
2913
constructor( blockParent, blogData = {} ){
3014
this.blockParent = blockParent;
3115
this.listParent = blogData.listParent || null;
@@ -87,12 +71,15 @@ export class BlogManager {
8771
curEntry.subscribeToURLChange( this.entryURLChange.bind(this) )
8872
}
8973
}
74+
9075
setListParent( listParent ){
9176
this.listParent = listParent;
9277
}
78+
9379
setContentParent( contentParent ){
9480
this.contentParent = contentParent;
9581
}
82+
9683
addBlogEntry( title, date, tags, body ){
9784
// Ensure we pass a sensible parent to the blogEntry constructor
9885
let parentForEntry = this.contentParent || this.listParent || null;
@@ -103,6 +90,7 @@ export class BlogManager {
10390

10491
this.blogEntries.push( newBlogEntry );
10592
}
93+
10694
build( parent=null ){
10795
let parentObj = parent;
10896
if( parentObj == null ){
@@ -518,6 +506,11 @@ export class BlogManager {
518506
this.comments.containerId = commentId;
519507
this.comments.load( this.currentEntry.urlRoute );
520508

509+
// Scroll top baby!
510+
if( this.contentParent ){
511+
this.contentParent.scrollTo( 0, 0 );
512+
}
513+
521514
// Check for URL update
522515
// TODO : Update how entries buttons are added to the DOM,
523516
// This seems like incorrect behaviour to include here in `showEntry()`

docs/js/blog/CommentManager.js

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ const giscusThemeList = [
1414
"catppuccin_latte", "catppuccin_frappe", "catppuccin_macchiato", "catppuccin_mocha", "fro"
1515
];
1616
17+
18+
// comment manager -
19+
20+
const comments = new CommentManager({
21+
repo: "YOUR_NAME/YOUR_REPO",
22+
repoId: "YOUR_REPO_ID",
23+
category: "General",
24+
categoryId: "YOUR_CATEGORY_ID",
25+
containerId: "comments",
26+
});
27+
28+
comments.load(post.slug);
29+
comments.unload();
30+
1731
*/
1832

1933

@@ -26,7 +40,7 @@ export class CommentManager {
2640
containerId = "comments",
2741
theme = "cobalt",
2842
lang = "en",
29-
}) {
43+
}){
3044
this.booted = false;
3145
this.repo = repo;
3246
this.repoId = repoId;
@@ -35,54 +49,35 @@ export class CommentManager {
3549
this.containerId = containerId;
3650
this.theme = theme;
3751
this.lang = lang;
52+
this.script = null;
3853

3954
this.handleMessage = this.handleMessage.bind(this);
4055
window.addEventListener("message", this.handleMessage);
4156
}
4257

43-
handleMessage(event) {
44-
if (event.origin !== "https://giscus.app") return;
45-
if (!(event.data && typeof event.data === "object" && event.data.giscus)) return;
58+
handleMessage( event ){
59+
if( event.origin !== "https://giscus.app" ) return;
60+
if( !(event.data && typeof event.data === "object" && event.data.giscus) ) return;
4661

4762
const giscus = event.data.giscus;
48-
if (giscus.resizeHeight) {
63+
if( giscus.resizeHeight ) {
4964
const container = document.getElementById(this.containerId);
50-
if (container) {
65+
if( container ) {
5166
container.style.height = `${giscus.resizeHeight + 30}px`;
5267
}
5368
}
5469
}
5570

56-
load(postId) {
57-
71+
load( postId ){
5872
const container = document.getElementById(this.containerId);
59-
if (!container) {
73+
if( !container ){
6074
console.warn("[CommentManager] Container not found:", this.containerId);
6175
return;
6276
}
6377

6478
this.booted = true;
6579
container.innerHTML = "";
6680

67-
/* Generated from Giscus.app --
68-
<script src="https://giscus.app/client.js"
69-
data-repo="ProcStack/procDiscussions"
70-
data-repo-id="R_kgDORc_LJw"
71-
data-category="General"
72-
data-category-id="DIC_kwDORc_LJ84C3kiH"
73-
data-mapping="pathname"
74-
data-strict="0"
75-
data-reactions-enabled="1"
76-
data-emit-metadata="0"
77-
data-input-position="top"
78-
data-theme="cobalt"
79-
data-lang="en"
80-
data-loading="lazy"
81-
crossorigin="anonymous"
82-
async>
83-
</script>
84-
*/
85-
8681
const script = document.createElement("script");
8782
script.src = "https://giscus.app/client.js";
8883
script.async = true;
@@ -99,30 +94,37 @@ export class CommentManager {
9994
script.dataset.strict = "0";
10095

10196
script.dataset.reactionsEnabled = "1";
102-
script.dataset.emitMetadata = "1";
97+
script.dataset.emitMetadata = "0";
10398
script.dataset.inputPosition = "top";
10499
script.dataset.theme = this.theme;
105100
script.dataset.lang = this.lang;
106101
script.dataset.loading = "lazy";
107102

108-
109-
110-
container.appendChild(script);
103+
// Add and store script reference
104+
container.appendChild( script );
105+
this.script = script;
111106
}
112107

113-
unload() {
108+
unload(){
114109
const container = document.getElementById(this.containerId);
115-
if (!container) return;
110+
if( !container ) return;
116111

117112
container.innerHTML = "";
118113
this.booted = false;
114+
115+
// Clean up for easier pre-rendered procPages
116+
// Repo - ./packing/renderPages.jsx
117+
if( this.script ){
118+
this.script.remove();
119+
this.script = null;
120+
}
119121
}
120122

121-
setTheme(theme) {
123+
setTheme( theme ){
122124
this.theme = theme;
123125

124126
const iframe = document.querySelector("iframe.giscus-frame");
125-
if (!iframe) return;
127+
if( !iframe ) return;
126128

127129
iframe.contentWindow.postMessage(
128130
{
@@ -134,7 +136,7 @@ export class CommentManager {
134136
);
135137
}
136138

137-
isBooted() {
139+
isBooted(){
138140
return this.booted;
139141
}
140142
}

0 commit comments

Comments
 (0)