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> </style>
<link rel="stylesheet" type="text/css" href="CSS_PLACE_HOLDER"> <link rel="stylesheet" type="text/css" href="CSS_PLACE_HOLDER">
<link rel="stylesheet" type="text/css" href="qrc:/utils/highlightjs/styles/default.css"> <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 src="qrc:/utils/highlightjs/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script> <script>hljs.initHighlightingOnLoad();</script>
</head> </head>
<body> <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,13 +148,22 @@
handleToc(needToc); handleToc(needToc);
}; };
var scrollToAnchor = function(anchor) { // Type = 0, Hoedown, getElementById;
var eles = document.getElementsByTagName('a'); // Type = 1, Marked, getElementsByTagName;
for (var i = 0; i < eles.length; ++i) { var scrollToAnchor = function(anchor, type) {
if (eles[i].name == anchor) { switch (type) {
eles[i].scrollIntoView(); case 0:
break; 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;
} }
}; };

View File

@ -39,7 +39,7 @@ QString VDocument::getToc()
return m_toc; 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); void setText(const QString &text);
QString getText(); QString getText();
QString getToc(); QString getToc();
void scrollToAnchor(const QString &anchor); void scrollToAnchor(const QString &anchor, int type);
public slots: public slots:
// Will be called in the HTML side // Will be called in the HTML side
@ -25,7 +25,7 @@ public slots:
signals: signals:
void textChanged(const QString &text); void textChanged(const QString &text);
void tocChanged(const QString &toc); void tocChanged(const QString &toc);
void requestScrollToAnchor(const QString &anchor); void requestScrollToAnchor(const QString &anchor, int type);
private: private:
QString m_text; QString m_text;

View File

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