Browse Source

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 <[email protected]>
Le Tan 9 years ago
parent
commit
8a214831e3
5 changed files with 27 additions and 4 deletions
  1. 9 1
      src/resources/pre_template.html
  2. 1 1
      src/resources/template.html
  3. 9 0
      src/vdocument.cpp
  4. 4 0
      src/vdocument.h
  5. 4 2
      src/vedittab.cpp

+ 9 - 1
src/resources/pre_template.html

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

+ 1 - 1
src/resources/template.html

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

+ 9 - 0
src/vdocument.cpp

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

+ 4 - 0
src/vdocument.h

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

+ 4 - 2
src/vedittab.cpp

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