Skip to content

Commit

Permalink
Merge pull request #1246 from kiwix/Bugfix-toc-switch-tab
Browse files Browse the repository at this point in the history
BugFix: Prevent crashes from tab switches due to Table Of Content
  • Loading branch information
kelson42 authored Nov 14, 2024
2 parents 0beea2d + b5d3ff0 commit 1a0580d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions resources/js/headerAnchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function anchorHeaderElements(headers)
});
}

function getHeaders()
function getHeadersJSONStr()
{
const headerInfo = { url: window.location.href.replace(location.hash,""), headers: [] };

Expand All @@ -40,12 +40,12 @@ function getHeaders()
const headers = getDOMElementsPreorderDFS(document.body, isHeaderElement);
headerInfo.headers = anchorHeaderElements(headers);
}
return headerInfo;
return JSON.stringify(headerInfo);
}

new QWebChannel(qt.webChannelTransport, function(channel) {
var kiwixObj = channel.objects.kiwixChannelObj;
kiwixObj.sendHeaders(getHeaders());
kiwixObj.sendHeadersJSONStr(getHeadersJSONStr());
kiwixObj.navigationRequested.connect(function(url, anchor) {
if (window.location.href.replace(location.hash,"") == url)
document.getElementById(anchor).scrollIntoView();
Expand Down
4 changes: 2 additions & 2 deletions src/kiwixwebchannelobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class KiwixWebChannelObject : public QObject
public:
explicit KiwixWebChannelObject(QObject *parent = nullptr) : QObject(parent) {};

Q_INVOKABLE void sendHeaders(const QJsonObject& headers) { emit headersChanged(headers); };
Q_INVOKABLE void sendHeadersJSONStr(const QString& headersJSONStr) { emit headersChanged(headersJSONStr); };

signals:
void headersChanged(const QJsonObject& headers);
void headersChanged(const QString& headersJSONStr);
void navigationRequested(const QString& url, const QString& anchor);
};

Expand Down
5 changes: 4 additions & 1 deletion src/tableofcontentbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ void createSubTree(QTreeWidgetItem* parent, QString parentNo, QJsonArray& header

void TableOfContentBar::setupTree(const QJsonObject& headers)
{
const auto headerUrl = headers["url"].toString();
const auto webView = KiwixApp::instance()->getTabWidget()->currentWebView();
if (!webView)
return;

const auto headerUrl = headers["url"].toString();
const auto currentUrl = webView->url().url(QUrl::RemoveFragment);
if (headerUrl != currentUrl)
return;
Expand Down
4 changes: 2 additions & 2 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ void WebView::onCurrentTitleChanged()
emit headersChanged(m_headers);
}

void WebView::onHeadersReceived(const QJsonObject& headers)
void WebView::onHeadersReceived(const QString& headersJSONStr)
{
const auto tabbar = KiwixApp::instance()->getTabWidget();
m_headers = QJsonObject(headers);
m_headers = QJsonDocument::fromJson(headersJSONStr.toUtf8()).object();

if (tabbar->currentWebView() == this)
emit headersChanged(m_headers);
Expand Down
2 changes: 1 addition & 1 deletion src/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public slots:
private slots:
void gotoTriggeredHistoryItemAction();
void onCurrentTitleChanged();
void onHeadersReceived(const QJsonObject& headers);
void onHeadersReceived(const QString& headersJSONStr);
void onNavigationRequested(const QString& url, const QString& anchor);

private:
Expand Down

0 comments on commit 1a0580d

Please sign in to comment.