bugfix: clear image preview blocks having unmatched image path

The syntax highlighter could not recognize image links with spaces in
the URL. When clearing orphan image preview block, we need to clear
thoes blocks which having unmatched image path.

Fix issue: after inserting spaces in the URL of an image link, the
preview block still exists.

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2017-01-22 19:40:43 +08:00
parent 180ab46367
commit 010f4245f6

View File

@ -268,14 +268,28 @@ bool VMdEdit::isOrphanImagePreviewBlock(QTextBlock p_block)
{ {
if (isImagePreviewBlock(p_block)) { if (isImagePreviewBlock(p_block)) {
// It is an orphan image preview block if previous block is not // It is an orphan image preview block if previous block is not
// a block need to preview (containing exactly one image). // a block need to preview (containing exactly one image) or the image
// paths are not equal to each other.
QTextBlock prevBlock = p_block.previous(); QTextBlock prevBlock = p_block.previous();
if (prevBlock.isValid()) { if (prevBlock.isValid()) {
if (fetchImageToPreview(prevBlock.text()).isEmpty()) { QString imageLink = fetchImageToPreview(prevBlock.text());
if (imageLink.isEmpty()) {
return true; return true;
} else {
return false;
} }
QString imagePath = QDir(m_file->retriveBasePath()).filePath(imageLink);
// Get image preview block's image path.
QTextCursor cursor(p_block);
int shift = p_block.text().indexOf(QChar::ObjectReplacementCharacter);
if (shift > 0) {
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor,
shift + 1);
}
QTextImageFormat format = cursor.charFormat().toImageFormat();
Q_ASSERT(format.isValid());
QString curPath = format.property(ImagePath).toString();
return curPath != imagePath;
} else { } else {
return true; return true;
} }