mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
clear unused images when finishing editing markdown
1. Clear existing images deleted by this edit; 2. Clear newly inserted images which were deleted before saving the file; Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
0298b8bfe1
commit
ac2552603b
@ -6,6 +6,7 @@
|
|||||||
#include "hgmarkdownhighlighter.h"
|
#include "hgmarkdownhighlighter.h"
|
||||||
#include "vmdeditoperations.h"
|
#include "vmdeditoperations.h"
|
||||||
#include "vtoc.h"
|
#include "vtoc.h"
|
||||||
|
#include "utils/vutils.h"
|
||||||
|
|
||||||
extern VConfigManager vconfig;
|
extern VConfigManager vconfig;
|
||||||
|
|
||||||
@ -93,12 +94,21 @@ void VEdit::beginEdit()
|
|||||||
case DocType::Markdown:
|
case DocType::Markdown:
|
||||||
setFont(vconfig.getMdEditFont());
|
setFont(vconfig.getMdEditFont());
|
||||||
setPlainText(noteFile->content);
|
setPlainText(noteFile->content);
|
||||||
|
initInitImages();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
qWarning() << "error: unknown doc type" << int(noteFile->docType);
|
qWarning() << "error: unknown doc type" << int(noteFile->docType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VEdit::endEdit()
|
||||||
|
{
|
||||||
|
setReadOnly(true);
|
||||||
|
if (noteFile->docType == DocType::Markdown) {
|
||||||
|
clearUnusedImages();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void VEdit::saveFile()
|
void VEdit::saveFile()
|
||||||
{
|
{
|
||||||
if (!document()->isModified()) {
|
if (!document()->isModified()) {
|
||||||
@ -218,3 +228,61 @@ void VEdit::updateCurHeader()
|
|||||||
}
|
}
|
||||||
emit curHeaderChanged(curHeader);
|
emit curHeaderChanged(curHeader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VEdit::insertImage(const QString &name)
|
||||||
|
{
|
||||||
|
m_insertedImages.append(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VEdit::initInitImages()
|
||||||
|
{
|
||||||
|
m_initImages = VUtils::imagesFromMarkdownFile(QDir(noteFile->basePath).filePath(noteFile->fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
void VEdit::clearUnusedImages()
|
||||||
|
{
|
||||||
|
QVector<QString> images = VUtils::imagesFromMarkdownFile(QDir(noteFile->basePath).filePath(noteFile->fileName));
|
||||||
|
|
||||||
|
if (!m_insertedImages.isEmpty()) {
|
||||||
|
QVector<QString> imageNames(images.size());
|
||||||
|
for (int i = 0; i < imageNames.size(); ++i) {
|
||||||
|
imageNames[i] = VUtils::fileNameFromPath(images[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
QDir dir = QDir(QDir(noteFile->basePath).filePath("images"));
|
||||||
|
for (int i = 0; i < m_insertedImages.size(); ++i) {
|
||||||
|
QString name = m_insertedImages[i];
|
||||||
|
int j;
|
||||||
|
for (j = 0; j < imageNames.size(); ++j) {
|
||||||
|
if (name == imageNames[j]) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete it
|
||||||
|
if (j == imageNames.size()) {
|
||||||
|
QString imagePath = dir.filePath(name);
|
||||||
|
QFile(imagePath).remove();
|
||||||
|
qDebug() << "delete inserted image" << imagePath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_insertedImages.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < m_initImages.size(); ++i) {
|
||||||
|
QString imagePath = m_initImages[i];
|
||||||
|
int j;
|
||||||
|
for (j = 0; j < images.size(); ++j) {
|
||||||
|
if (imagePath == images[j]) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete it
|
||||||
|
if (j == images.size()) {
|
||||||
|
QFile(imagePath).remove();
|
||||||
|
qDebug() << "delete existing image" << imagePath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_initImages.clear();
|
||||||
|
}
|
||||||
|
@ -17,6 +17,7 @@ public:
|
|||||||
VEdit(VNoteFile *noteFile, QWidget *parent = 0);
|
VEdit(VNoteFile *noteFile, QWidget *parent = 0);
|
||||||
~VEdit();
|
~VEdit();
|
||||||
void beginEdit();
|
void beginEdit();
|
||||||
|
void endEdit();
|
||||||
|
|
||||||
// Save buffer content to noteFile->content.
|
// Save buffer content to noteFile->content.
|
||||||
void saveFile();
|
void saveFile();
|
||||||
@ -26,6 +27,7 @@ public:
|
|||||||
|
|
||||||
void reloadFile();
|
void reloadFile();
|
||||||
void scrollToLine(int lineNumber);
|
void scrollToLine(int lineNumber);
|
||||||
|
void insertImage(const QString &name);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void headersChanged(const QVector<VHeader> &headers);
|
void headersChanged(const QVector<VHeader> &headers);
|
||||||
@ -43,6 +45,8 @@ private slots:
|
|||||||
private:
|
private:
|
||||||
void updateTabSettings();
|
void updateTabSettings();
|
||||||
void updateFontAndPalette();
|
void updateFontAndPalette();
|
||||||
|
void initInitImages();
|
||||||
|
void clearUnusedImages();
|
||||||
|
|
||||||
bool isExpandTab;
|
bool isExpandTab;
|
||||||
QString tabSpaces;
|
QString tabSpaces;
|
||||||
@ -50,6 +54,8 @@ private:
|
|||||||
HGMarkdownHighlighter *mdHighlighter;
|
HGMarkdownHighlighter *mdHighlighter;
|
||||||
VEditOperations *editOps;
|
VEditOperations *editOps;
|
||||||
QVector<VHeader> headers;
|
QVector<VHeader> headers;
|
||||||
|
QVector<QString> m_insertedImages;
|
||||||
|
QVector<QString> m_initImages;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ void VEditTab::readFile()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
textEditor->setReadOnly(true);
|
textEditor->endEdit();
|
||||||
showFileReadMode();
|
showFileReadMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,6 +55,8 @@ void VMdEditOperations::insertImageFromQImage(const QString &title, const QStrin
|
|||||||
|
|
||||||
QString md = QString("").arg(title).arg(fileName);
|
QString md = QString("").arg(title).arg(fileName);
|
||||||
insertTextAtCurPos(md);
|
insertTextAtCurPos(md);
|
||||||
|
|
||||||
|
editor->insertImage(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VMdEditOperations::insertImageFromPath(const QString &title,
|
void VMdEditOperations::insertImageFromPath(const QString &title,
|
||||||
@ -76,6 +78,8 @@ void VMdEditOperations::insertImageFromPath(const QString &title,
|
|||||||
|
|
||||||
QString md = QString("").arg(title).arg(fileName);
|
QString md = QString("").arg(title).arg(fileName);
|
||||||
insertTextAtCurPos(md);
|
insertTextAtCurPos(md);
|
||||||
|
|
||||||
|
editor->insertImage(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VMdEditOperations::insertImageFromURL(const QUrl &imageUrl)
|
bool VMdEditOperations::insertImageFromURL(const QUrl &imageUrl)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user