Browse Source

fix paste image issue

- Will detect whether there is only <img> in html;
- Detect local file when handling text;
Le Tan 7 years ago
parent
commit
d3a5642d06
3 changed files with 19 additions and 3 deletions
  1. 6 0
      src/utils/vutils.cpp
  2. 3 0
      src/utils/vutils.h
  3. 10 3
      src/vmdeditor.cpp

+ 6 - 0
src/utils/vutils.cpp

@@ -1580,3 +1580,9 @@ QString VUtils::promptForFileName(const QString &p_title,
 
     return name;
 }
+
+bool VUtils::onlyHasImgInHtml(const QString &p_html)
+{
+    // Tricky.
+    return !p_html.contains("<p ");
+}

+ 3 - 0
src/utils/vutils.h

@@ -339,6 +339,9 @@ public:
                                      const QString &p_dir,
                                      QWidget *p_parent = nullptr);
 
+    // Whether @p_html has only <img> content.
+    static bool onlyHasImgInHtml(const QString &p_html);
+
     // Regular expression for image link.
     // ![image title]( http://github.com/tamlok/vnote.jpg "alt text" =200x100)
     // Captured texts (need to be trimmed):

+ 10 - 3
src/vmdeditor.cpp

@@ -766,7 +766,8 @@ void VMdEditor::insertFromMimeData(const QMimeData *p_source)
     if (p_source->hasHtml()) {
         // Handle <img>.
         QRegExp reg("<img ([^>]*)src=\"([^\"]+)\"([^>]*)>");
-        if (reg.indexIn(p_source->html()) != -1) {
+        QString html(p_source->html());
+        if (reg.indexIn(html) != -1 && VUtils::onlyHasImgInHtml(html)) {
             if (p_source->hasImage()) {
                 // Both image data and URL are embedded.
                 VSelectDialog dialog(tr("Insert From Clipboard"), this);
@@ -858,9 +859,15 @@ void VMdEditor::insertFromMimeData(const QMimeData *p_source)
                 int selection = dialog.getSelection();
                 if (selection == 0) {
                     // Insert as image.
-                    QUrl url(text);
+                    QUrl url;
+                    if (QFileInfo::exists(text)) {
+                        url = QUrl::fromLocalFile(text);
+                    } else {
+                        url = QUrl(text);
+                    }
+
                     if (url.isValid()) {
-                        m_editOps->insertImageFromURL(QUrl(text));
+                        m_editOps->insertImageFromURL(url);
                     }
 
                     return;