Browse Source

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 <[email protected]>
Le Tan 8 years ago
parent
commit
010f4245f6
1 changed files with 18 additions and 4 deletions
  1. 18 4
      src/vmdedit.cpp

+ 18 - 4
src/vmdedit.cpp

@@ -268,14 +268,28 @@ bool VMdEdit::isOrphanImagePreviewBlock(QTextBlock p_block)
 {
     if (isImagePreviewBlock(p_block)) {
         // 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();
         if (prevBlock.isValid()) {
-            if (fetchImageToPreview(prevBlock.text()).isEmpty()) {
+            QString imageLink = fetchImageToPreview(prevBlock.text());
+            if (imageLink.isEmpty()) {
                 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 {
             return true;
         }