support navigating by outline using Hoedown

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-11-07 22:40:43 +08:00
parent ae0130aa26
commit d1a3a9c386
5 changed files with 58 additions and 17 deletions

View File

@ -7,7 +7,38 @@
</style>
<link rel="stylesheet" type="text/css" href="CSS_PLACE_HOLDER">
<link rel="stylesheet" type="text/css" href="qrc:/utils/highlightjs/styles/default.css">
<script src="qrc:/resources/qwebchannel.js"></script>
<script src="qrc:/utils/highlightjs/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<script>
'use strict';
var content;
// Type = 0, Hoedown, getElementById;
// Type = 1, Marked, getElementsByTagName;
var scrollToAnchor = function(anchor, type) {
switch (type) {
case 0:
document.getElementById(anchor).scrollIntoView();
break;
case 1:
var eles = document.getElementsByTagName('a');
for (var i = 0; i < eles.length; ++i) {
if (eles[i].name == anchor) {
eles[i].scrollIntoView();
break;
}
}
break;
}
};
new QWebChannel(qt.webChannelTransport,
function(channel) {
content = channel.objects.content;
content.requestScrollToAnchor.connect(scrollToAnchor);
}
);
</script>

View File

@ -148,7 +148,14 @@
handleToc(needToc);
};
var scrollToAnchor = function(anchor) {
// Type = 0, Hoedown, getElementById;
// Type = 1, Marked, getElementsByTagName;
var scrollToAnchor = function(anchor, type) {
switch (type) {
case 0:
document.getElementById(anchor).scrollIntoView();
break;
case 1:
var eles = document.getElementsByTagName('a');
for (var i = 0; i < eles.length; ++i) {
if (eles[i].name == anchor) {
@ -156,6 +163,8 @@
break;
}
}
break;
}
};
new QWebChannel(qt.webChannelTransport,

View File

@ -39,7 +39,7 @@ QString VDocument::getToc()
return m_toc;
}
void VDocument::scrollToAnchor(const QString &anchor)
void VDocument::scrollToAnchor(const QString &anchor, int type)
{
emit requestScrollToAnchor(anchor);
emit requestScrollToAnchor(anchor, type);
}

View File

@ -16,7 +16,7 @@ public:
void setText(const QString &text);
QString getText();
QString getToc();
void scrollToAnchor(const QString &anchor);
void scrollToAnchor(const QString &anchor, int type);
public slots:
// Will be called in the HTML side
@ -25,7 +25,7 @@ public slots:
signals:
void textChanged(const QString &text);
void tocChanged(const QString &toc);
void requestScrollToAnchor(const QString &anchor);
void requestScrollToAnchor(const QString &anchor, int type);
private:
QString m_text;

View File

@ -217,12 +217,13 @@ void VEditTab::setupMarkdownPreview()
VPreviewPage *page = new VPreviewPage(this);
webPreviewer->setPage(page);
if (mdConverterType == MarkdownConverterType::Marked) {
QWebChannel *channel = new QWebChannel(this);
channel->registerObject(QStringLiteral("content"), &document);
connect(&document, &VDocument::tocChanged,
this, &VEditTab::updateTocFromHtml);
page->setWebChannel(channel);
if (mdConverterType == MarkdownConverterType::Marked) {
webPreviewer->setHtml(VNote::templateHtml,
QUrl::fromLocalFile(noteFile->basePath + QDir::separator()));
}
@ -344,7 +345,7 @@ void VEditTab::scrollToAnchor(const VAnchor &anchor)
}
} else {
if (!anchor.anchor.isEmpty()) {
document.scrollToAnchor(anchor.anchor.mid(1));
document.scrollToAnchor(anchor.anchor.mid(1), mdConverterType);
}
}
}