mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
clear corrupted image preview block
Users may add some texts in the image preview block around the image. In this case, we should just delete the image and insert another preview block. Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
26016dd44a
commit
3ce9f02007
@ -244,6 +244,7 @@ void VMdEdit::clearOrphanImagePreviewBlock()
|
||||
removeBlock(block);
|
||||
block = nextBlock;
|
||||
} else {
|
||||
clearCorruptedImagePreviewBlock(block);
|
||||
block = block.next();
|
||||
}
|
||||
}
|
||||
@ -268,6 +269,35 @@ bool VMdEdit::isOrphanImagePreviewBlock(QTextBlock p_block)
|
||||
return false;
|
||||
}
|
||||
|
||||
void VMdEdit::clearCorruptedImagePreviewBlock(QTextBlock p_block)
|
||||
{
|
||||
if (!p_block.isValid()) {
|
||||
return;
|
||||
}
|
||||
QString text = p_block.text();
|
||||
QVector<int> replacementChars;
|
||||
bool onlySpaces = true;
|
||||
for (int i = 0; i < text.size(); ++i) {
|
||||
if (text[i] == QChar::ObjectReplacementCharacter) {
|
||||
replacementChars.append(i);
|
||||
} else if (!text[i].isSpace()) {
|
||||
onlySpaces = false;
|
||||
}
|
||||
}
|
||||
if (!onlySpaces && !replacementChars.isEmpty()) {
|
||||
// ObjectReplacementCharacter mixed with other non-space texts.
|
||||
// Users corrupt the image preview block. Just remove the char.
|
||||
QTextCursor cursor(p_block);
|
||||
int blockPos = p_block.position();
|
||||
for (int i = replacementChars.size() - 1; i >= 0; --i) {
|
||||
int pos = replacementChars[i];
|
||||
cursor.setPosition(blockPos + pos);
|
||||
cursor.deleteChar();
|
||||
}
|
||||
Q_ASSERT(text.remove(QChar::ObjectReplacementCharacter) == p_block.text());
|
||||
}
|
||||
}
|
||||
|
||||
QString VMdEdit::fetchImageToPreview(const QString &p_text)
|
||||
{
|
||||
QRegExp regExp("\\!\\[[^\\]]*\\]\\((images/[^/\\)]+)\\)");
|
||||
|
@ -60,6 +60,8 @@ private:
|
||||
void clearOrphanImagePreviewBlock();
|
||||
void removeBlock(QTextBlock p_block);
|
||||
bool isOrphanImagePreviewBlock(QTextBlock p_block);
|
||||
// Block that has the QChar::ObjectReplacementCharacter as well as some non-space characters.
|
||||
void clearCorruptedImagePreviewBlock(QTextBlock p_block);
|
||||
// Returns the image relative path (image/xxx.png) only when
|
||||
// there is one and only one image link.
|
||||
QString fetchImageToPreview(const QString &p_text);
|
||||
|
Loading…
x
Reference in New Issue
Block a user