MdEditor: support Attach And Insert Link action

This commit is contained in:
Le Tan 2018-10-09 19:48:38 +08:00
parent 3779dd1b81
commit 33350e6bdf
4 changed files with 76 additions and 6 deletions

View File

@ -1884,7 +1884,16 @@ bool VMdEditor::processUrlFromMimeData(const QMimeData *p_source)
dialog.addSelection(tr("Insert As Link"), 2); dialog.addSelection(tr("Insert As Link"), 2);
if (isLocalFile) { if (isLocalFile) {
dialog.addSelection(tr("Insert As Relative Link"), 3); dialog.addSelection(tr("Insert As Relative Link"), 3);
// Attach as attachment.
if (m_file->getType() == FileType::Note) {
VNoteFile *note = static_cast<VNoteFile *>((VFile *)m_file);
if (-1 == note->findAttachmentByPath(url.toLocalFile(), false)) {
dialog.addSelection(tr("Attach And Insert Link"), 6);
} }
}
}
dialog.addSelection(tr("Insert As Text"), 4); dialog.addSelection(tr("Insert As Text"), 4);
if (!localTextFilePath.isEmpty()) { if (!localTextFilePath.isEmpty()) {
dialog.addSelection(tr("Insert File Content"), 5); dialog.addSelection(tr("Insert File Content"), 5);
@ -1909,6 +1918,37 @@ bool VMdEditor::processUrlFromMimeData(const QMimeData *p_source)
return true; return true;
} }
case 6:
{
// Attach And Insert Link.
QString file = url.toLocalFile();
Q_ASSERT(m_file->getType() == FileType::Note);
VNoteFile *note = static_cast<VNoteFile *>((VFile *)m_file);
QString destFile;
if (!note->addAttachment(file, &destFile)) {
VUtils::showMessage(QMessageBox::Warning,
tr("Warning"),
tr("Fail to add attachment %1 for note <span style=\"%2\">%3</span>.")
.arg(file)
.arg(g_config->c_dataTextStyle)
.arg(note->getName()),
"",
QMessageBox::Ok,
QMessageBox::Ok,
this);
return true;
}
emit m_object->statusMessage(tr("1 file added as attachment"));
// Update url to point to the attachment file.
Q_ASSERT(!destFile.isEmpty());
url = QUrl::fromLocalFile(destFile);
V_FALLTHROUGH;
}
case 3: case 3:
// Insert As Relative link. // Insert As Relative link.
relativeLink = true; relativeLink = true;
@ -2024,9 +2064,11 @@ void VMdEditor::replaceTextWithLocalImages(QString &p_text)
QString suffix = info.suffix(); QString suffix = info.suffix();
QScopedPointer<QTemporaryFile> tmpFile; QScopedPointer<QTemporaryFile> tmpFile;
if (info.exists() && info.isAbsolute()) { if (info.exists()) {
if (info.isAbsolute()) {
// Absolute local path. // Absolute local path.
srcImagePath = info.absoluteFilePath(); srcImagePath = info.absoluteFilePath();
}
} else { } else {
// Network path. // Network path.
QByteArray data = VDownloader::downloadSync(QUrl(imageUrl)); QByteArray data = VDownloader::downloadSync(QUrl(imageUrl));

View File

@ -235,7 +235,7 @@ bool VNoteFile::deleteInternalImages()
return deleted == images.size(); return deleted == images.size();
} }
bool VNoteFile::addAttachment(const QString &p_file) bool VNoteFile::addAttachment(const QString &p_file, QString *p_destFile)
{ {
if (p_file.isEmpty() || !QFileInfo::exists(p_file)) { if (p_file.isEmpty() || !QFileInfo::exists(p_file)) {
return false; return false;
@ -260,6 +260,10 @@ bool VNoteFile::addAttachment(const QString &p_file)
return false; return false;
} }
if (p_destFile) {
*p_destFile = destPath;
}
return true; return true;
} }
@ -360,6 +364,28 @@ int VNoteFile::findAttachment(const QString &p_name, bool p_caseSensitive)
return -1; return -1;
} }
int VNoteFile::findAttachmentByPath(const QString &p_file, bool p_caseSensitive)
{
QFileInfo fi(p_file);
int idx = findAttachment(fi.fileName(), p_caseSensitive);
if (idx == -1) {
return -1;
}
// Check path.
QString attPath = QDir(fetchAttachmentFolderPath()).filePath(m_attachments[idx].m_name);
bool equal = false;
if (p_caseSensitive) {
equal = VUtils::equalPath(attPath, fi.absoluteFilePath());
} else {
equal = VUtils::equalPath(attPath.toLower(),
fi.absoluteFilePath().toLower());
}
return equal ? idx : -1;
}
bool VNoteFile::sortAttachments(const QVector<int> &p_sortedIdx) bool VNoteFile::sortAttachments(const QVector<int> &p_sortedIdx)
{ {
V_ASSERT(m_opened); V_ASSERT(m_opened);

View File

@ -80,7 +80,7 @@ public:
void setAttachments(const QVector<VAttachment> &p_attas); void setAttachments(const QVector<VAttachment> &p_attas);
// Add @p_file as an attachment to this note. // Add @p_file as an attachment to this note.
bool addAttachment(const QString &p_file); bool addAttachment(const QString &p_file, QString *p_destFile = NULL);
// Fetch attachment folder path. // Fetch attachment folder path.
// Will create it if it does not exist. // Will create it if it does not exist.
@ -102,6 +102,8 @@ public:
// -1 if not found. // -1 if not found.
int findAttachment(const QString &p_name, bool p_caseSensitive = true); int findAttachment(const QString &p_name, bool p_caseSensitive = true);
int findAttachmentByPath(const QString &p_file, bool p_caseSensitive = true);
// Rename attachment @p_oldName to @p_newName. // Rename attachment @p_oldName to @p_newName.
bool renameAttachment(const QString &p_oldName, const QString &p_newName); bool renameAttachment(const QString &p_oldName, const QString &p_newName);