mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
MdEditor: support inserting file as relative link
This commit is contained in:
parent
b052fa0f05
commit
30a3a6e74a
@ -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());
|
||||
|
||||
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()) {
|
||||
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 (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 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;
|
||||
}
|
||||
|
||||
default:
|
||||
Q_ASSERT(false);
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return true;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
VSelectDialog dialog(tr("Insert From Clipboard"), this);
|
||||
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:
|
||||
{
|
||||
// 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"),
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user