mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 22:09:52 +08:00
bugfix: VImagePreviewer to preview images with optional title
Do not preview images in code block.
This commit is contained in:
parent
bf55ba01ce
commit
7a4d86eca9
@ -24,6 +24,8 @@ extern VConfigManager vconfig;
|
|||||||
const QVector<QPair<QString, QString>> VUtils::c_availableLanguages = {QPair<QString, QString>("en_US", "Englisth(US)"),
|
const QVector<QPair<QString, QString>> VUtils::c_availableLanguages = {QPair<QString, QString>("en_US", "Englisth(US)"),
|
||||||
QPair<QString, QString>("zh_CN", "Chinese")};
|
QPair<QString, QString>("zh_CN", "Chinese")};
|
||||||
|
|
||||||
|
const QString VUtils::c_imageLinkRegExp = QString("\\!\\[([^\\]]*)\\]\\(([^\\)\"]+)\\s*(\".*\")?\\s*\\)");
|
||||||
|
|
||||||
VUtils::VUtils()
|
VUtils::VUtils()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,14 @@ public:
|
|||||||
static QChar keyToChar(int p_key);
|
static QChar keyToChar(int p_key);
|
||||||
static QString getLocale();
|
static QString getLocale();
|
||||||
|
|
||||||
|
// Regular expression for image link.
|
||||||
|
// 
|
||||||
|
// Captured texts (need to be trimmed):
|
||||||
|
// 1. Image Alt Text (Title);
|
||||||
|
// 2. Image URL;
|
||||||
|
// 3. Image Optional Title with double quotes;
|
||||||
|
static const QString c_imageLinkRegExp;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// <value, name>
|
// <value, name>
|
||||||
static const QVector<QPair<QString, QString>> c_availableLanguages;
|
static const QVector<QPair<QString, QString>> c_availableLanguages;
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include "utils/vutils.h"
|
#include "utils/vutils.h"
|
||||||
#include "vfile.h"
|
#include "vfile.h"
|
||||||
#include "vdownloader.h"
|
#include "vdownloader.h"
|
||||||
|
#include "hgmarkdownhighlighter.h"
|
||||||
|
|
||||||
extern VConfigManager vconfig;
|
extern VConfigManager vconfig;
|
||||||
|
|
||||||
@ -72,6 +73,11 @@ void VImagePreviewer::handleContentChange(int /* p_position */,
|
|||||||
m_timer->start();
|
m_timer->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool VImagePreviewer::isNormalBlock(const QTextBlock &p_block)
|
||||||
|
{
|
||||||
|
return p_block.userState() == HighlightBlockState::Normal;
|
||||||
|
}
|
||||||
|
|
||||||
void VImagePreviewer::previewImages()
|
void VImagePreviewer::previewImages()
|
||||||
{
|
{
|
||||||
if (m_isPreviewing) {
|
if (m_isPreviewing) {
|
||||||
@ -86,7 +92,7 @@ void VImagePreviewer::previewImages()
|
|||||||
while (block.isValid() && m_enablePreview) {
|
while (block.isValid() && m_enablePreview) {
|
||||||
if (isImagePreviewBlock(block)) {
|
if (isImagePreviewBlock(block)) {
|
||||||
// Image preview block. Check if it is parentless.
|
// Image preview block. Check if it is parentless.
|
||||||
if (!isValidImagePreviewBlock(block)) {
|
if (!isValidImagePreviewBlock(block) || !isNormalBlock(block)) {
|
||||||
QTextBlock nblock = block.next();
|
QTextBlock nblock = block.next();
|
||||||
removeBlock(block);
|
removeBlock(block);
|
||||||
block = nblock;
|
block = nblock;
|
||||||
@ -96,7 +102,11 @@ void VImagePreviewer::previewImages()
|
|||||||
} else {
|
} else {
|
||||||
clearCorruptedImagePreviewBlock(block);
|
clearCorruptedImagePreviewBlock(block);
|
||||||
|
|
||||||
|
if (isNormalBlock(block)) {
|
||||||
block = previewImageOfOneBlock(block);
|
block = previewImageOfOneBlock(block);
|
||||||
|
} else {
|
||||||
|
block = block.next();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,7 +168,8 @@ bool VImagePreviewer::isValidImagePreviewBlock(QTextBlock &p_block)
|
|||||||
|
|
||||||
QString VImagePreviewer::fetchImageUrlToPreview(const QString &p_text)
|
QString VImagePreviewer::fetchImageUrlToPreview(const QString &p_text)
|
||||||
{
|
{
|
||||||
QRegExp regExp("\\!\\[[^\\]]*\\]\\(([^\\)]+)\\)");
|
QRegExp regExp(VUtils::c_imageLinkRegExp);
|
||||||
|
|
||||||
int index = regExp.indexIn(p_text);
|
int index = regExp.indexIn(p_text);
|
||||||
if (index == -1) {
|
if (index == -1) {
|
||||||
return QString();
|
return QString();
|
||||||
@ -169,7 +180,7 @@ QString VImagePreviewer::fetchImageUrlToPreview(const QString &p_text)
|
|||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
return regExp.capturedTexts()[1];
|
return regExp.capturedTexts()[2].trimmed();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString VImagePreviewer::fetchImagePathToPreview(const QString &p_text)
|
QString VImagePreviewer::fetchImagePathToPreview(const QString &p_text)
|
||||||
|
@ -93,6 +93,9 @@ private:
|
|||||||
// Return true if and only if there is update.
|
// Return true if and only if there is update.
|
||||||
bool updateImageWidth(QTextImageFormat &p_format);
|
bool updateImageWidth(QTextImageFormat &p_format);
|
||||||
|
|
||||||
|
// Whether it is a normal block or not.
|
||||||
|
bool isNormalBlock(const QTextBlock &p_block);
|
||||||
|
|
||||||
VMdEdit *m_edit;
|
VMdEdit *m_edit;
|
||||||
QTextDocument *m_document;
|
QTextDocument *m_document;
|
||||||
VFile *m_file;
|
VFile *m_file;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user