fix outline navigation bug using Hoedown

Use a fixed base html and update the body using VDocument.html like
Marked does.

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-11-09 21:43:11 +08:00
parent 454072c9ba
commit 8a214831e3
5 changed files with 27 additions and 4 deletions

View File

@ -12,17 +12,25 @@
<script>hljs.initHighlightingOnLoad();</script> <script>hljs.initHighlightingOnLoad();</script>
</head> </head>
<body> <body>
<div id="placeholder"></div>
<script> <script>
'use strict'; 'use strict';
var placeholder = document.getElementById('placeholder');
var content; var content;
var scrollToAnchor = function(anchor) { var scrollToAnchor = function(anchor) {
document.getElementById(anchor).scrollIntoView(); document.getElementById(anchor).scrollIntoView();
}; };
var updateHtml = function(html) {
placeholder.innerHTML = html;
}
new QWebChannel(qt.webChannelTransport, new QWebChannel(qt.webChannelTransport,
function(channel) { function(channel) {
content = channel.objects.content; content = channel.objects.content;
updateHtml(content.html);
content.htmlChanged.connect(updateHtml);
content.requestScrollToAnchor.connect(scrollToAnchor); content.requestScrollToAnchor.connect(scrollToAnchor);
} }
); );
@ -35,7 +43,7 @@
return; return;
} }
var curIdx = 0; var curIdx = 0;
var biaScrollTop = scrollTop + 20; var biaScrollTop = scrollTop + 50;
for (var i = 0; i < eles.length; ++i) { for (var i = 0; i < eles.length; ++i) {
if (biaScrollTop >= eles[i].offsetTop) { if (biaScrollTop >= eles[i].offsetTop) {
curIdx = i; curIdx = i;

View File

@ -166,7 +166,7 @@
return; return;
} }
var curIdx = 0; var curIdx = 0;
var biaScrollTop = scrollTop + 20; var biaScrollTop = scrollTop + 50;
for (var i = 0; i < eles.length; ++i) { for (var i = 0; i < eles.length; ++i) {
if (biaScrollTop >= eles[i].offsetTop) { if (biaScrollTop >= eles[i].offsetTop) {
curIdx = i; curIdx = i;

View File

@ -52,3 +52,12 @@ void VDocument::setHeader(const QString &anchor)
m_header = anchor; m_header = anchor;
emit headerChanged(m_header); emit headerChanged(m_header);
} }
void VDocument::setHtml(const QString &html)
{
if (html == m_html) {
return;
}
m_html = html;
emit htmlChanged(m_html);
}

View File

@ -9,6 +9,7 @@ class VDocument : public QObject
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged) Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
Q_PROPERTY(QString toc MEMBER m_toc NOTIFY tocChanged) Q_PROPERTY(QString toc MEMBER m_toc NOTIFY tocChanged)
Q_PROPERTY(QString html MEMBER m_html NOTIFY htmlChanged)
public: public:
explicit VDocument(QObject *parent = 0); explicit VDocument(QObject *parent = 0);
@ -17,6 +18,7 @@ public:
QString getText(); QString getText();
QString getToc(); QString getToc();
void scrollToAnchor(const QString &anchor); void scrollToAnchor(const QString &anchor);
void setHtml(const QString &html);
public slots: public slots:
// Will be called in the HTML side // Will be called in the HTML side
@ -28,11 +30,13 @@ signals:
void tocChanged(const QString &toc); void tocChanged(const QString &toc);
void requestScrollToAnchor(const QString &anchor); void requestScrollToAnchor(const QString &anchor);
void headerChanged(const QString &anchor); void headerChanged(const QString &anchor);
void htmlChanged(const QString &html);
private: private:
QString m_text; QString m_text;
QString m_toc; QString m_toc;
QString m_header; QString m_header;
QString m_html;
}; };
#endif // VDOCUMENT_H #endif // VDOCUMENT_H

View File

@ -117,8 +117,7 @@ void VEditTab::previewByConverter()
QRegularExpression tocExp("<p>\\[TOC\\]<\\/p>", QRegularExpression::CaseInsensitiveOption); QRegularExpression tocExp("<p>\\[TOC\\]<\\/p>", QRegularExpression::CaseInsensitiveOption);
QString toc = mdConverter.generateToc(content, vconfig.getMarkdownExtensions()); QString toc = mdConverter.generateToc(content, vconfig.getMarkdownExtensions());
html.replace(tocExp, toc); html.replace(tocExp, toc);
QString completeHtml = VNote::preTemplateHtml + html + VNote::postTemplateHtml; document.setHtml(html);
webPreviewer->setHtml(completeHtml, QUrl::fromLocalFile(noteFile->basePath + QDir::separator()));
// Hoedown will add '\n' while Marked does not // Hoedown will add '\n' while Marked does not
updateTocFromHtml(toc.replace("\n", "")); updateTocFromHtml(toc.replace("\n", ""));
} }
@ -228,6 +227,9 @@ void VEditTab::setupMarkdownPreview()
if (mdConverterType == MarkdownConverterType::Marked) { if (mdConverterType == MarkdownConverterType::Marked) {
webPreviewer->setHtml(VNote::templateHtml, webPreviewer->setHtml(VNote::templateHtml,
QUrl::fromLocalFile(noteFile->basePath + QDir::separator())); QUrl::fromLocalFile(noteFile->basePath + QDir::separator()));
} else {
webPreviewer->setHtml(VNote::preTemplateHtml + VNote::postTemplateHtml,
QUrl::fromLocalFile(noteFile->basePath + QDir::separator()));
} }
addWidget(webPreviewer); addWidget(webPreviewer);