MdEditor: support inserting file as relative link

This commit is contained in:
Le Tan 2018-09-14 19:48:17 +08:00
parent b052fa0f05
commit 30a3a6e74a

View File

@ -1826,59 +1826,72 @@ 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());
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;
}
QMimeData newSource;
newSource.setUrls(urls);
VTextEdit::insertFromMimeData(&newSource);
}
return true;
} else {
QString localTextFilePath;
if (urls[0].isLocalFile()) {
if (!isImage && 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();
}
}
}
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);
if (isImage) {
dialog.addSelection(tr("Insert As Image"), 0);
dialog.addSelection(tr("Insert As Image Link"), 1);
}
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);
}
// FIXME: After calling dialog.exec(), p_source->hasUrl() returns false.
if (dialog.exec() == QDialog::Accepted) {
bool relativeLink = false;
switch (dialog.getSelection()) {
case 0:
{
QString ut = urls[0].isLocalFile() ? urls[0].toString(QUrl::EncodeSpaces)
// Insert As Image.
m_editOps->insertImageFromURL(urls[0]);
return true;
}
case 1:
{
// Insert As Image Link.
insertImageLink("", urls[0].isLocalFile() ? urls[0].toString(QUrl::EncodeSpaces)
: urls[0].toString());
return true;
}
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();
}
VInsertLinkDialog ld(QObject::tr("Insert Link"),
"",
"",
@ -1893,35 +1906,36 @@ bool VMdEditor::processUrlFromMimeData(const QMimeData *p_source)
m_editOps->insertLink(linkText, linkUrl);
}
break;
return true;
}
case 1:
case 4:
{
// Insert As Text.
if (p_source->hasText()) {
m_editOps->insertText(p_source->text());
} else {
m_editOps->insertText(urls[0].toString());
}
break;
return true;
}
case 2:
case 5:
{
// Insert File Content.
Q_ASSERT(!localTextFilePath.isEmpty());
m_editOps->insertText(VUtils::readFileFromDisk(localTextFilePath));
break;
return true;
}
default:
Q_ASSERT(false);
break;
}
}
return true;
} else {
return true;
}
}
}
return false;