Browse Source

support copy as Web Editor

Le Tan 8 years ago
parent
commit
632a007808
4 changed files with 98 additions and 6 deletions
  1. 4 2
      src/resources/vnote.ini
  2. 86 2
      src/utils/vwebutils.cpp
  3. 6 0
      src/utils/vwebutils.h
  4. 2 2
      src/vwebview.cpp

+ 4 - 2
src/resources/vnote.ini

@@ -203,7 +203,7 @@ style_of_span_for_mark="background-color: #FFFF00;"
 ; CSS properties to embed as inline styles when copied in edit mode
 ; tag1:tag2:tag3$property1:property2:property3,tag4:tag5$property2:property3
 ; "all" for all tags not specified explicitly
-styles_to_inline_when_copied=all$border:color:display:font-family:font-size:font-style:white-space:word-spacing:line-height:text-align:text-indent:padding-top:padding-bottom:margin-top:margin-bottom:background-color,code$font-family:font-size:line-height:color:display:overfow-x:background-color,li$line-height:background-color,a$color:vertical-align:background-color,pre$display:overflow-y:overflow-x:color:font-size:font-style:font-weight:letter-spacing:text-align:text-indent:word-spacing:background-color
+styles_to_inline_when_copied=all$border:color:display:font-family:font-size:font-style:white-space:word-spacing:line-height:text-align:text-indent:padding-top:padding-bottom:margin-top:margin-bottom:background-color:font-weight,code$font-family:font-size:line-height:color:display:overfow-x:background-color:font-weight,li$line-height:background-color:font-weight,a$color:vertical-align:background-color:font-weight,pre$display:overflow-y:overflow-x:color:font-size:font-style:font-weight:letter-spacing:text-align:text-indent:word-spacing:background-color
 
 ; Define targets the copied content will be pasted into
 ; target_name$action1:action2:action3,targeet_name2$action2:action3
@@ -217,7 +217,9 @@ styles_to_inline_when_copied=all$border:color:display:font-family:font-size:font
 ; a - transform <mark> to <span>
 ; x(tag1|tag2) - remove styles specified in [styles_to_inline_when_copied] of all tags except tag1 and tag2
 ; p - replace the background color of <pre> with that of its child <code>
-copy_targets=WithoutBackground$s:b(mark):c:i:x,OneNote$s:b(mark):c:i:m:a:x,MicroSoftWord$s:p:b(mark|pre):c(pre):i:m:a:x,RawHTML$r
+; n - replace the \n in <pre> with <br>
+; g - replace local relative/absolute <img> tag with a warning label
+copy_targets="Without Background"$s:b(mark):c:i:x,OneNote$s: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,"Web Editor"$s:p:b(mark|pre):c(pre):m:x:n,"Raw HTML"$r:x
 
 [shortcuts]
 ; Define shortcuts here, with each item in the form "operation=keysequence".

+ 86 - 2
src/utils/vwebutils.cpp

@@ -2,6 +2,7 @@
 
 #include <QRegExp>
 #include <QFileInfo>
+#include <QObject>
 #include <QDebug>
 
 #include "vpalette.h"
@@ -87,7 +88,7 @@ bool VWebUtils::fixImageSrc(const QUrl &p_baseUrl, QString &p_html)
     QUrl::ComponentFormattingOption strOpt = QUrl::FullyEncoded;
 #endif
 
-    QRegExp reg("(<img src=\")([^\"]+)\"");
+    QRegExp reg("<img src=\"([^\"]+)\"");
 
     int pos = 0;
     while (pos < p_html.size()) {
@@ -96,7 +97,7 @@ bool VWebUtils::fixImageSrc(const QUrl &p_baseUrl, QString &p_html)
             break;
         }
 
-        QString urlStr = reg.cap(2);
+        QString urlStr = reg.cap(1);
         QUrl imgUrl(urlStr);
 
         QString fixedStr;
@@ -212,6 +213,14 @@ bool VWebUtils::alterHtmlByTargetAction(const QUrl &p_baseUrl, QString &p_html,
         altered = replacePreBackgroundColorWithCode(p_html);
         break;
 
+    case 'n':
+        altered = replaceNewLineWithBR(p_html);
+        break;
+
+    case 'g':
+        altered = replaceLocalImgWithWarningLabel(p_html);
+        break;
+
     default:
         break;
     }
@@ -591,3 +600,78 @@ VWebUtils::HtmlTag VWebUtils::readNextTag(const QString &p_html, int p_pos)
     tag.m_style = m_styleTagReg.cap(3);
     return tag;
 }
+
+bool VWebUtils::replaceNewLineWithBR(QString &p_html)
+{
+    if (p_html.isEmpty()) {
+        return false;
+    }
+
+    bool altered = false;
+    int pos = 0;
+
+    while (pos < p_html.size()) {
+        int tagIdx = p_html.indexOf(m_tagReg, pos);
+        if (tagIdx == -1) {
+            break;
+        }
+
+        QString tagName = m_tagReg.cap(1);
+        pos = tagIdx + m_tagReg.matchedLength();
+        if (tagName.toLower() != "pre") {
+            continue;
+        }
+
+        int preEnd = skipToTagEnd(p_html, pos, tagName);
+
+        // Replace '\n' in [pos, preEnd).
+        while (pos < preEnd) {
+            int idx = p_html.indexOf('\n', pos);
+            if (idx == -1 || idx >= preEnd) {
+                break;
+            }
+
+            QString br("<br>");
+            p_html.replace(idx, 1, br);
+            pos = idx + br.size() - 1;
+            preEnd = preEnd + br.size() - 1;
+
+            altered = true;
+        }
+
+        pos = preEnd;
+    }
+
+    return altered;
+}
+
+bool VWebUtils::replaceLocalImgWithWarningLabel(QString &p_html)
+{
+    bool altered = false;
+
+    QRegExp reg("<img src=\"([^\"]+)\"[^>]*>");
+
+    QString label = QString("<span style=\"font-weight: bold; color: #FFFFFF; background-color: #EE0000;\">%1</span>")
+                           .arg(QObject::tr("Insert_Image_HERE"));
+
+    int pos = 0;
+    while (pos < p_html.size()) {
+        int idx = p_html.indexOf(reg, pos);
+        if (idx == -1) {
+            break;
+        }
+
+        QString urlStr = reg.cap(1);
+        QUrl imgUrl(urlStr);
+
+        if (imgUrl.scheme() == "https" || imgUrl.scheme() == "http") {
+            pos = idx + reg.matchedLength();
+            continue;
+        }
+
+        p_html.replace(idx, reg.matchedLength(), label);
+        pos = idx + label.size();
+    }
+
+    return altered;
+}

+ 6 - 0
src/utils/vwebutils.h

@@ -91,6 +91,12 @@ private:
 
     VWebUtils::HtmlTag readNextTag(const QString &p_html, int p_pos);
 
+    // Replace \n with <br> in <pre>.
+    bool replaceNewLineWithBR(QString &p_html);
+
+    // Replace local absolute/relative <img> tag with a warning label.
+    bool replaceLocalImgWithWarningLabel(QString &p_html);
+
     QVector<CopyTarget> m_copyTargets;
 
     // Custom styles to remove when copied.

+ 2 - 2
src/vwebview.cpp

@@ -111,8 +111,6 @@ void VWebView::handleEditAction()
 
 void VWebView::copyImage()
 {
-    m_afterCopyImage = true;
-
 #if defined(Q_OS_WIN)
     Q_ASSERT(m_copyImageUrlActionHooked);
     // triggerPageAction(QWebEnginePage::CopyImageUrlToClipboard) will not really
@@ -145,6 +143,8 @@ void VWebView::copyImage()
     }
 #endif
 
+    m_afterCopyImage = true;
+
     // Fall back.
     triggerPageAction(QWebEnginePage::CopyImageToClipboard);
 }