浏览代码

MdEditor: support inserting file as relative link

Le Tan 7 年之前
父节点
当前提交
30a3a6e74a
共有 1 个文件被更改,包括 93 次插入79 次删除
  1. 93 79
      src/vmdeditor.cpp

+ 93 - 79
src/vmdeditor.cpp

@@ -1826,102 +1826,116 @@ bool VMdEditor::processUrlFromMimeData(const QMimeData *p_source)
 
     QList<QUrl> urls = p_source->urls();
     if (urls.size() == 1) {
-        if (VUtils::isImageURL(urls[0])) {
-            VSelectDialog dialog(tr("Insert From Clipboard"), this);
-            dialog.addSelection(tr("Insert As Image"), 0);
-            dialog.addSelection(tr("Insert As Text"), 1);
-            dialog.addSelection(tr("Insert As Image Link"), 2);
+        bool isImage = VUtils::isImageURL(urls[0]);
+        bool isLocalFile = urls[0].isLocalFile()
+                           && QFileInfo::exists(urls[0].toLocalFile());
+
+        QString localTextFilePath;
+        if (!isImage && isLocalFile) {
+            localTextFilePath = urls[0].toLocalFile();
+            QMimeDatabase mimeDatabase;
+            const QMimeType mimeType = mimeDatabase.mimeTypeForFile(localTextFilePath);
+            if (mimeType.isValid() && !mimeType.inherits(QStringLiteral("text/plain"))) {
+                localTextFilePath.clear();
+            }
+        }
 
-            if (dialog.exec() == QDialog::Accepted) {
-                // FIXME: After calling dialog.exec(), p_source->hasUrl() returns false.
-                int selection = dialog.getSelection();
-                if (selection == 0) {
-                    // Insert as image.
-                    m_editOps->insertImageFromURL(urls[0]);
-                    return true;
-                } else if (selection == 2) {
-                    // Insert as link.
-                    insertImageLink("", urls[0].toString(QUrl::FullyEncoded));
-                    return true;
-                }
+        VSelectDialog dialog(tr("Insert From Clipboard"), this);
+        if (isImage) {
+            dialog.addSelection(tr("Insert As Image"), 0);
+            dialog.addSelection(tr("Insert As Image Link"), 1);
+        }
 
-                QMimeData newSource;
-                newSource.setUrls(urls);
-                VTextEdit::insertFromMimeData(&newSource);
-            }
+        dialog.addSelection(tr("Insert As Link"), 2);
+        if (isLocalFile) {
+            dialog.addSelection(tr("Insert As Relative Link"), 3);
+        }
+        dialog.addSelection(tr("Insert As Text"), 4);
+        if (!localTextFilePath.isEmpty()) {
+            dialog.addSelection(tr("Insert File Content"), 5);
+        }
 
-            return true;
-        } else {
-            QString localTextFilePath;
-            if (urls[0].isLocalFile()) {
-                localTextFilePath = urls[0].toLocalFile();
-                if (!QFileInfo::exists(localTextFilePath)) {
-                    localTextFilePath.clear();
-                } else {
-                    QMimeDatabase mimeDatabase;
-                    const QMimeType mimeType = mimeDatabase.mimeTypeForFile(localTextFilePath);
-                    if (mimeType.isValid() && !mimeType.inherits(QStringLiteral("text/plain"))) {
-                        localTextFilePath.clear();
-                    }
-                }
+        // FIXME: After calling dialog.exec(), p_source->hasUrl() returns false.
+        if (dialog.exec() == QDialog::Accepted) {
+            bool relativeLink = false;
+            switch (dialog.getSelection()) {
+            case 0:
+            {
+                // Insert As Image.
+                m_editOps->insertImageFromURL(urls[0]);
+                return true;
             }
 
-            VSelectDialog dialog(tr("Insert From Clipboard"), this);
-            dialog.addSelection(tr("Insert As Link"), 0);
-            dialog.addSelection(tr("Insert As Text"), 1);
-            if (!localTextFilePath.isEmpty()) {
-                dialog.addSelection(tr("Insert File Content"), 2);
+            case 1:
+            {
+                // Insert As Image Link.
+                insertImageLink("", urls[0].isLocalFile() ? urls[0].toString(QUrl::EncodeSpaces)
+                                                          : urls[0].toString());
+                return true;
             }
 
-            if (dialog.exec() == QDialog::Accepted) {
-                switch (dialog.getSelection()) {
-                case 0:
-                {
-                    QString ut = urls[0].isLocalFile() ? urls[0].toString(QUrl::EncodeSpaces)
-                                                       : urls[0].toString();
-                    VInsertLinkDialog ld(QObject::tr("Insert Link"),
-                                         "",
-                                         "",
-                                         "",
-                                         ut,
-                                         false,
-                                         this);
-                    if (ld.exec() == QDialog::Accepted) {
-                        QString linkText = ld.getLinkText();
-                        QString linkUrl = ld.getLinkUrl();
-                        Q_ASSERT(!linkText.isEmpty() && !linkUrl.isEmpty());
-                        m_editOps->insertLink(linkText, linkUrl);
-                    }
-
-                    break;
+            case 3:
+                // Insert As Relative link.
+                relativeLink = true;
+                V_FALLTHROUGH;
+
+            case 2:
+            {
+                // Insert As Link.
+                QString ut;
+                if (relativeLink) {
+                    QDir dir(m_file->fetchBasePath());
+                    ut = dir.relativeFilePath(urls[0].toLocalFile());
+                    ut = QUrl(ut).toString(QUrl::EncodeSpaces);
+                } else {
+                    ut = urls[0].isLocalFile() ? urls[0].toString(QUrl::EncodeSpaces)
+                                               : urls[0].toString();
                 }
 
-                case 1:
-                    if (p_source->hasText()) {
-                        m_editOps->insertText(p_source->text());
-                    } else {
-                        m_editOps->insertText(urls[0].toString());
-                    }
-
-                    break;
-
-                case 2:
-                {
-                    Q_ASSERT(!localTextFilePath.isEmpty());
-                    m_editOps->insertText(VUtils::readFileFromDisk(localTextFilePath));
-                    break;
+                VInsertLinkDialog ld(QObject::tr("Insert Link"),
+                                     "",
+                                     "",
+                                     "",
+                                     ut,
+                                     false,
+                                     this);
+                if (ld.exec() == QDialog::Accepted) {
+                    QString linkText = ld.getLinkText();
+                    QString linkUrl = ld.getLinkUrl();
+                    Q_ASSERT(!linkText.isEmpty() && !linkUrl.isEmpty());
+                    m_editOps->insertLink(linkText, linkUrl);
                 }
 
-                default:
-                    Q_ASSERT(false);
-                    break;
+                return true;
+            }
+
+            case 4:
+            {
+                // Insert As Text.
+                if (p_source->hasText()) {
+                    m_editOps->insertText(p_source->text());
+                } else {
+                    m_editOps->insertText(urls[0].toString());
                 }
 
                 return true;
-            } else {
+            }
+
+            case 5:
+            {
+                // Insert File Content.
+                Q_ASSERT(!localTextFilePath.isEmpty());
+                m_editOps->insertText(VUtils::readFileFromDisk(localTextFilePath));
                 return true;
             }
+
+            default:
+                Q_ASSERT(false);
+                break;
+            }
         }
+
+        return true;
     }
 
     return false;