Kaynağa Gözat

MdTab: fix the synchronization between read and edit mode

Mute web view in edit mode.
Le Tan 7 yıl önce
ebeveyn
işleme
3e29a647ab
4 değiştirilmiş dosya ile 29 ekleme ve 2 silme
  1. 6 0
      src/resources/markdown_template.js
  2. 3 2
      src/vdocument.cpp
  3. 13 0
      src/vdocument.h
  4. 7 0
      src/vmdtab.cpp

+ 6 - 0
src/resources/markdown_template.js

@@ -154,12 +154,18 @@ var htmlContent = function() {
     content.htmlContentCB("", styleContent(), contentDiv.innerHTML);
 };
 
+var mute = function(muted) {
+    g_muteScroll = muted;
+};
+
 new QWebChannel(qt.webChannelTransport,
     function(channel) {
         content = channel.objects.content;
 
         content.requestScrollToAnchor.connect(scrollToAnchor);
 
+        content.requestMuted.connect(mute);
+
         if (typeof highlightText == "function") {
             content.requestHighlightText.connect(highlightText);
             content.noticeReadyToHighlightText();

+ 3 - 2
src/vdocument.cpp

@@ -12,7 +12,8 @@ VDocument::VDocument(const VFile *v_file, QObject *p_parent)
       m_readyToHighlight(false),
       m_plantUMLHelper(NULL),
       m_graphvizHelper(NULL),
-      m_nextID(0)
+      m_nextID(0),
+      m_webViewMuted(false)
 {
 }
 
@@ -46,7 +47,7 @@ void VDocument::scrollToAnchor(const QString &anchor)
 
 void VDocument::setHeader(const QString &anchor)
 {
-    if (anchor == m_header) {
+    if (m_webViewMuted || anchor == m_header) {
         return;
     }
 

+ 13 - 0
src/vdocument.h

@@ -67,6 +67,8 @@ public:
 
     int registerIdentifier();
 
+    void muteWebView(bool p_muted);
+
 public slots:
     // Will be called in the HTML side
 
@@ -173,6 +175,8 @@ signals:
 
     void codeBlockPreviewReady(int p_id, const QString &p_lang, const QString &p_html);
 
+    void requestMuted(bool p_muted);
+
 private:
     QString m_toc;
     QString m_header;
@@ -198,6 +202,9 @@ private:
     VGraphvizHelper *m_graphvizHelper;
 
     int m_nextID;
+
+    // Whether propogate signals from web view.
+    bool m_webViewMuted;
 };
 
 inline bool VDocument::isReadyToHighlight() const
@@ -219,4 +226,10 @@ inline int VDocument::registerIdentifier()
 {
     return ++m_nextID;
 }
+
+inline void VDocument::muteWebView(bool p_muted)
+{
+    m_webViewMuted = p_muted;
+    emit requestMuted(m_webViewMuted);
+}
 #endif // VDOCUMENT_H

+ 7 - 0
src/vmdtab.cpp

@@ -434,6 +434,7 @@ void VMdTab::setupMarkdownViewer()
                     // Recover header from edit mode.
                     scrollWebViewToHeader(m_headerFromEditMode);
                     m_headerFromEditMode.clear();
+                    m_document->muteWebView(false);
                     return;
                 }
 
@@ -1058,6 +1059,10 @@ void VMdTab::tabIsReady(TabReady p_mode)
                          || (!m_isEditMode && p_mode == TabReady::ReadMode);
 
     if (isCurrentMode) {
+        if (p_mode == TabReady::ReadMode) {
+            m_document->muteWebView(false);
+        }
+
         restoreFromTabInfo();
 
         if (m_enableBackupFile
@@ -1426,6 +1431,7 @@ void VMdTab::setCurrentMode(Mode p_mode)
         break;
 
     case Mode::Edit:
+        m_document->muteWebView(true);
         m_webViewer->hide();
         m_editor->show();
 
@@ -1435,6 +1441,7 @@ void VMdTab::setCurrentMode(Mode p_mode)
 
     case Mode::EditPreview:
         Q_ASSERT(m_editor);
+        m_document->muteWebView(true);
         m_webViewer->setInPreview(true);
         m_webViewer->show();
         m_editor->show();