Skip to content

Commit

Permalink
Fixed hash
Browse files Browse the repository at this point in the history
  • Loading branch information
kuogi committed Sep 27, 2023
1 parent 9a6ad55 commit 43a3570
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
29 changes: 22 additions & 7 deletions src/app/main.content.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Content {
XML.addEventListener('readystatechange', (e) => {
if (e.target.readyState === 4 && e.target.status === 200) {
window.history.pushState('', '', pathFileName + (hash ?? ''));
this.#handleResponse(content, pageSections, e.target.response);
this.#handleResponse(content, pageSections, e.target.response, hash);
}
});
}
Expand All @@ -56,7 +56,7 @@ class Content {
return { content, pageSections };
}

#handleResponse(content, pageSections, response) {
#handleResponse(content, pageSections, response, hash) {
try {
const data = sourceParser.getContentByData(response);
content.classList.remove('is-loading');
Expand All @@ -69,11 +69,10 @@ class Content {
pageSections.textContent = '';
pageSections.appendChild(sectionsFragment);

//try {
// this.#handleHash(data, hash);
//} catch(e) {}
try {
this.#handleHash(data, hash);
} catch(e) {}
} catch(e) {
//this.#headerCompass.classList.remove('visible');
content.classList.add('is-loading');
content.textContent = 'Rendering failed';
pageSections.classList.add('is-loading');
Expand All @@ -83,6 +82,22 @@ class Content {
}
}

#handleHash(data, hash) {
if (typeof hash != 'undefined' && hash.length) {
if (hash.startsWith('#')) {
hash = hash.slice(1);
}

const selectedChild = data.querySelectorAll('.h1, .h2, .h3');
for(const child of selectedChild) {
if (utils.generateSectionRefByTextContent(child.textContent) == hash) {
child.scrollIntoView();
break;
}
}
}
}

#iterPageSectionsData(container, currentDom, childsLimit = Infinity) {
for(const [id, element] of container.childNodes.entries()) {
if (id > childsLimit) {
Expand Down Expand Up @@ -146,7 +161,7 @@ class Content {
}
}
}

updateMobileSectionsVisibilityState(forcedState) {
return this.#currentSectionsElement.classList.toggle('show', forcedState);
}
Expand Down
5 changes: 2 additions & 3 deletions src/app/main.home.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class HomePage {
#chooseRightTab(pathName, hash) {
config.getAvailableCategories().then((ids) => {
let found = false;

for(const id of ids) {
if (decodeURI(pathName).startsWith(utils.parseCategoryUrl(id))) {
found = true;
Expand All @@ -85,7 +85,6 @@ class HomePage {
this.#sidebarInstance.loadSidebar(id);

this.#tryToIndexFilePathFromId(id, pathName, hash);
//this.#contentInstance.loadFile(pathname);
}
}

Expand All @@ -102,7 +101,7 @@ class HomePage {
for(const file of files) {
if (utils.parseCategoryUrl(file) === decodeURI(pathName)) {
this.#sidebarInstance.updateActiveFile(file);
this.#contentInstance.loadFile(file);
this.#contentInstance.loadFile(file, hash);
break;
}
}
Expand Down

0 comments on commit 43a3570

Please sign in to comment.