-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1237 from kiwix/Issue#42-toc-qtree-alternative
Introduce Table of Content Without Intense JS Invasion
- Loading branch information
Showing
16 changed files
with
541 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<RCC> | ||
<qresource prefix="/"> | ||
<file>js/headerAnchor.js</file> | ||
</qresource> | ||
</RCC> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
function isHeaderElement(elem) | ||
{ | ||
return elem.nodeName.match(/^H\d+$/) && elem.textContent; | ||
} | ||
|
||
function getDOMElementsPreorderDFS(elem, pred) | ||
{ | ||
var result = []; | ||
if (pred(elem)) | ||
result.push(elem); | ||
|
||
for ( const child of elem.children) | ||
result = result.concat(getDOMElementsPreorderDFS(child, pred)); | ||
return result; | ||
} | ||
|
||
function anchorHeaderElements(headers) | ||
{ | ||
return Array.from(headers, function(elem, i) | ||
{ | ||
const text = elem.textContent.trim().replace(/"/g, '\\"'); | ||
const level = parseInt(elem.nodeName.substr(1)); | ||
const anchor = `kiwix-toc-${i}`; | ||
|
||
const anchorElem = document.createElement("a"); | ||
anchorElem.id = anchor; | ||
|
||
/* Mark header content with something we can reference. */ | ||
elem.insertAdjacentElement("afterbegin", anchorElem); | ||
return { text, level, anchor }; | ||
}); | ||
} | ||
|
||
function getHeaders() | ||
{ | ||
const headerInfo = { url: window.location.href.replace(location.hash,""), headers: [] }; | ||
|
||
if (document.body !== undefined) | ||
{ | ||
const headers = getDOMElementsPreorderDFS(document.body, isHeaderElement); | ||
headerInfo.headers = anchorHeaderElements(headers); | ||
} | ||
return headerInfo; | ||
} | ||
|
||
new QWebChannel(qt.webChannelTransport, function(channel) { | ||
var kiwixObj = channel.objects.kiwixChannelObj; | ||
kiwixObj.sendHeaders(getHeaders()); | ||
kiwixObj.navigationRequested.connect(function(url, anchor) { | ||
if (window.location.href.replace(location.hash,"") == url) | ||
document.getElementById(anchor).scrollIntoView(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef KIWIXWEBCHANNELOBJECT_H | ||
#define KIWIXWEBCHANNELOBJECT_H | ||
|
||
#include <QObject> | ||
|
||
class KiwixWebChannelObject : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit KiwixWebChannelObject(QObject *parent = nullptr) : QObject(parent) {}; | ||
|
||
Q_INVOKABLE void sendHeaders(const QJsonObject& headers) { emit headersChanged(headers); }; | ||
|
||
signals: | ||
void headersChanged(const QJsonObject& headers); | ||
void navigationRequested(const QString& url, const QString& anchor); | ||
}; | ||
|
||
#endif // KIWIXWEBCHANNELOBJECT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.