Browse Source

add enable_flash_anchor config

It controls whether we need to flash the heading when we scroll to it in
read mode.
Le Tan 7 years ago
parent
commit
2bb2910fec

+ 25 - 0
src/dialog/vsettingsdialog.cpp

@@ -464,6 +464,10 @@ VReadEditTab::VReadEditTab(QWidget *p_parent)
     zoomFactorLayout->addWidget(m_customWebZoom);
     zoomFactorLayout->addWidget(m_webZoomFactorSpin);
 
+    // Web flash anchor.
+    m_flashAnchor = new QCheckBox(tr("Flash current heading"));
+    m_flashAnchor->setToolTip(tr("Flash current heading on change"));
+
     // Swap file.
     m_swapFile = new QCheckBox(tr("Swap file"));
     m_swapFile->setToolTip(tr("Automatically save changes to a swap file for backup"));
@@ -478,6 +482,7 @@ VReadEditTab::VReadEditTab(QWidget *p_parent)
 
     QVBoxLayout *readLayout = new QVBoxLayout();
     readLayout->addLayout(zoomFactorLayout);
+    readLayout->addWidget(m_flashAnchor);
     m_readBox->setLayout(readLayout);
 
     QFormLayout *editLayout = new QFormLayout();
@@ -509,6 +514,10 @@ bool VReadEditTab::loadConfiguration()
         return false;
     }
 
+    if (!loadFlashAnchor()) {
+        return false;
+    }
+
     if (!loadSwapFile()) {
         return false;
     }
@@ -526,6 +535,10 @@ bool VReadEditTab::saveConfiguration()
         return false;
     }
 
+    if (!saveFlashAnchor()) {
+        return false;
+    }
+
     if (!saveSwapFile()) {
         return false;
     }
@@ -567,6 +580,18 @@ bool VReadEditTab::saveWebZoomFactor()
     return true;
 }
 
+bool VReadEditTab::loadFlashAnchor()
+{
+    m_flashAnchor->setChecked(g_config->getEnableFlashAnchor());
+    return true;
+}
+
+bool VReadEditTab::saveFlashAnchor()
+{
+    g_config->setEnableFlashAnchor(m_flashAnchor->isChecked());
+    return true;
+}
+
 bool VReadEditTab::loadSwapFile()
 {
     m_swapFile->setChecked(g_config->getEnableBackupFile());

+ 6 - 0
src/dialog/vsettingsdialog.h

@@ -74,10 +74,16 @@ private:
 
     void showTipsAboutAutoSave();
 
+    bool loadFlashAnchor();
+    bool saveFlashAnchor();
+
     // Web zoom factor.
     QCheckBox *m_customWebZoom;
     QDoubleSpinBox *m_webZoomFactorSpin;
 
+    // Web flash anchor.
+    QCheckBox *m_flashAnchor;
+
     // Swap file.
     QCheckBox *m_swapFile;
 

+ 11 - 3
src/resources/markdown_template.js

@@ -40,6 +40,10 @@ if (typeof VEnableImageCaption == 'undefined') {
     VEnableImageCaption = false;
 }
 
+if (typeof VEnableFlashAnchor == 'undefined') {
+    VEnableFlashAnchor = false;
+}
+
 var getUrlScheme = function(url) {
     var idx = url.indexOf(':');
     if (idx > -1) {
@@ -132,7 +136,11 @@ var clearHighlightedAnchor = function() {
     }
 };
 
-var highlightAnchor = function(anchor) {
+var flashAnchor = function(anchor) {
+    if (!VEnableFlashAnchor) {
+        return;
+    }
+
     clearHighlightedAnchor();
     anchor.classList.add(VHighlightedAnchorClass);
 };
@@ -151,7 +159,7 @@ var scrollToAnchor = function(anchor) {
     var anc = document.getElementById(anchor);
     if (anc != null) {
         anc.scrollIntoView();
-        highlightAnchor(anc);
+        flashAnchor(anc);
 
         var headers = document.querySelectorAll("h1, h2, h3, h4, h5, h6");
         for (var i = 0; i < headers.length; ++i) {
@@ -935,7 +943,7 @@ var jumpTitle = function(forward, relativeLevel, repeat) {
     // Disable scroll temporarily.
     g_muteScroll = true;
     headers[targetIdx].scrollIntoView();
-    highlightAnchor(headers[targetIdx]);
+    flashAnchor(headers[targetIdx]);
     currentHeaderIdx = targetIdx;
     content.setHeader(headers[targetIdx].getAttribute("id"));
     setTimeout("g_muteScroll = false", 100);

+ 2 - 0
src/resources/vnote.ini

@@ -246,6 +246,8 @@ styles_to_inline_when_copied=all$border:color:display:font-family:font-size:font
 ; j - fix XHTML tag like <img> and <br>
 copy_targets="Without Background"$s:b(mark):c:i:x,Evernote$e:p:b(mark|pre):c(pre):g:m:a:x:n:j,OneNote$e:b(mark):c:i:m:a:x,"Microsoft Word"$s:p:b(mark|pre):c(pre):i:m:a:x,"WeChat Public Account"$s:p:b(mark|pre):c(pre):g:m:x:n:f,"Web Editor"$s:p:b(mark|pre):c(pre):m:x:n,"Raw HTML"$r:x
 
+enable_flash_anchor=true
+
 [shortcuts]
 ; Define shortcuts here, with each item in the form "operation=keysequence".
 ; Leave keysequence empty to disable the shortcut of an operation.

+ 4 - 0
src/utils/vutils.cpp

@@ -695,6 +695,10 @@ QString VUtils::generateHtmlTemplate(const QString &p_template,
                      "<script>var VEnableHighlightLineNumber = true;</script>\n";
     }
 
+    if (g_config->getEnableFlashAnchor()) {
+        extraFile += "<script>var VEnableFlashAnchor = true;</script>\n";
+    }
+
     extraFile += "<script>var VStylesToInline = '" + g_config->getStylesToInlineWhenCopied() + "';</script>\n";
 
     QString htmlTemplate(p_template);

+ 3 - 0
src/vconfigmanager.cpp

@@ -289,6 +289,9 @@ void VConfigManager::initialize()
 
     m_singleClickClosePreviousTab = getConfigFromSettings("global",
                                                           "single_click_close_previous_tab").toBool();
+
+    m_enableFlashAnchor = getConfigFromSettings("web",
+                                                "enable_flash_anchor").toBool();
 }
 
 void VConfigManager::initSettings()

+ 21 - 0
src/vconfigmanager.h

@@ -460,6 +460,9 @@ public:
     QString getWkhtmltopdfArgs() const;
     void setWkhtmltopdfArgs(const QString &p_args);
 
+    bool getEnableFlashAnchor() const;
+    void setEnableFlashAnchor(bool p_enabled);
+
 private:
     // Look up a config from user and default settings.
     QVariant getConfigFromSettings(const QString &section, const QString &key) const;
@@ -857,6 +860,9 @@ private:
     // Single click to open file and then close previous tab.
     bool m_singleClickClosePreviousTab;
 
+    // Whether flash anchor in read mode.
+    bool m_enableFlashAnchor;
+
     // The name of the config file in each directory, obsolete.
     // Use c_dirConfigFile instead.
     static const QString c_obsoleteDirConfigFile;
@@ -2111,4 +2117,19 @@ inline void VConfigManager::setWkhtmltopdfArgs(const QString &p_file)
 {
     setConfigToSettings("export", "wkhtmltopdfArgs", p_file);
 }
+
+inline bool VConfigManager::getEnableFlashAnchor() const
+{
+    return m_enableFlashAnchor;
+}
+
+inline void VConfigManager::setEnableFlashAnchor(bool p_enabled)
+{
+    if (p_enabled == m_enableFlashAnchor) {
+        return;
+    }
+
+    m_enableFlashAnchor = p_enabled;
+    setConfigToSettings("web", "enable_flash_anchor", m_enableFlashAnchor);
+}
 #endif // VCONFIGMANAGER_H