mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-06 14:29:54 +08:00
hide image preview block when selected
We do not want user to copy the image preview block (a special char). Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
059e8dacd2
commit
385abb3041
@ -263,3 +263,9 @@ void HGMarkdownHighlighter::timerTimeout()
|
|||||||
rehighlight();
|
rehighlight();
|
||||||
emit highlightCompleted();
|
emit highlightCompleted();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HGMarkdownHighlighter::updateHighlight()
|
||||||
|
{
|
||||||
|
timer->stop();
|
||||||
|
timerTimeout();
|
||||||
|
}
|
||||||
|
@ -56,6 +56,8 @@ public:
|
|||||||
QTextDocument *parent = 0);
|
QTextDocument *parent = 0);
|
||||||
~HGMarkdownHighlighter();
|
~HGMarkdownHighlighter();
|
||||||
void setStyles(const QVector<HighlightingStyle> &styles);
|
void setStyles(const QVector<HighlightingStyle> &styles);
|
||||||
|
// Request to update highlihgt (re-parse and re-highlight)
|
||||||
|
void updateHighlight();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void highlightCompleted();
|
void highlightCompleted();
|
||||||
|
@ -16,7 +16,7 @@ const QString VMdEdit::c_cursorLineColor = "Indigo1";
|
|||||||
const QString VMdEdit::c_cursorLineColorVim = "Green2";
|
const QString VMdEdit::c_cursorLineColorVim = "Green2";
|
||||||
|
|
||||||
VMdEdit::VMdEdit(VFile *p_file, QWidget *p_parent)
|
VMdEdit::VMdEdit(VFile *p_file, QWidget *p_parent)
|
||||||
: VEdit(p_file, p_parent), m_mdHighlighter(NULL)
|
: VEdit(p_file, p_parent), m_mdHighlighter(NULL), m_previewImage(true)
|
||||||
{
|
{
|
||||||
Q_ASSERT(p_file->getDocType() == DocType::Markdown);
|
Q_ASSERT(p_file->getDocType() == DocType::Markdown);
|
||||||
|
|
||||||
@ -40,6 +40,9 @@ VMdEdit::VMdEdit(VFile *p_file, QWidget *p_parent)
|
|||||||
this, &VMdEdit::highlightCurrentLine);
|
this, &VMdEdit::highlightCurrentLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connect(this, &VMdEdit::selectionChanged,
|
||||||
|
this, &VMdEdit::handleSelectionChanged);
|
||||||
|
|
||||||
m_editOps->updateTabSettings();
|
m_editOps->updateTabSettings();
|
||||||
updateFontAndPalette();
|
updateFontAndPalette();
|
||||||
}
|
}
|
||||||
@ -229,6 +232,9 @@ void VMdEdit::scrollToHeader(int p_headerIndex)
|
|||||||
|
|
||||||
void VMdEdit::updateImageBlocks(QSet<int> p_imageBlocks)
|
void VMdEdit::updateImageBlocks(QSet<int> p_imageBlocks)
|
||||||
{
|
{
|
||||||
|
if (!m_previewImage) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// We need to handle blocks backward to avoid shifting all the following blocks.
|
// We need to handle blocks backward to avoid shifting all the following blocks.
|
||||||
// Inserting the preview image block may cause highlighter to emit signal again.
|
// Inserting the preview image block may cause highlighter to emit signal again.
|
||||||
QList<int> blockList = p_imageBlocks.toList();
|
QList<int> blockList = p_imageBlocks.toList();
|
||||||
@ -310,6 +316,25 @@ void VMdEdit::clearCorruptedImagePreviewBlock(QTextBlock p_block)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VMdEdit::clearAllImagePreviewBlocks()
|
||||||
|
{
|
||||||
|
QTextDocument *doc = document();
|
||||||
|
QTextBlock block = doc->begin();
|
||||||
|
bool modified = isModified();
|
||||||
|
while (block.isValid()) {
|
||||||
|
if (isImagePreviewBlock(block)) {
|
||||||
|
QTextBlock nextBlock = block.next();
|
||||||
|
removeBlock(block);
|
||||||
|
block = nextBlock;
|
||||||
|
} else {
|
||||||
|
clearCorruptedImagePreviewBlock(block);
|
||||||
|
block = block.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setModified(modified);
|
||||||
|
emit statusChanged();
|
||||||
|
}
|
||||||
|
|
||||||
QString VMdEdit::fetchImageToPreview(const QString &p_text)
|
QString VMdEdit::fetchImageToPreview(const QString &p_text)
|
||||||
{
|
{
|
||||||
QRegExp regExp("\\!\\[[^\\]]*\\]\\((images/[^/\\)]+)\\)");
|
QRegExp regExp("\\!\\[[^\\]]*\\]\\((images/[^/\\)]+)\\)");
|
||||||
@ -491,3 +516,21 @@ void VMdEdit::handleEditStateChanged(KeyState p_state)
|
|||||||
}
|
}
|
||||||
highlightCurrentLine();
|
highlightCurrentLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VMdEdit::handleSelectionChanged()
|
||||||
|
{
|
||||||
|
QString text = textCursor().selectedText();
|
||||||
|
if (text.isEmpty() && !m_previewImage) {
|
||||||
|
m_previewImage = true;
|
||||||
|
m_mdHighlighter->updateHighlight();
|
||||||
|
} else if (m_previewImage) {
|
||||||
|
if (text.trimmed() == QString(QChar::ObjectReplacementCharacter)) {
|
||||||
|
// Select the image and some whitespaces.
|
||||||
|
// We can let the user copy the image.
|
||||||
|
return;
|
||||||
|
} else if (text.contains(QChar::ObjectReplacementCharacter)) {
|
||||||
|
m_previewImage = false;
|
||||||
|
clearAllImagePreviewBlocks();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -40,6 +40,7 @@ private slots:
|
|||||||
void updateImageBlocks(QSet<int> p_imageBlocks);
|
void updateImageBlocks(QSet<int> p_imageBlocks);
|
||||||
void highlightCurrentLine();
|
void highlightCurrentLine();
|
||||||
void handleEditStateChanged(KeyState p_state);
|
void handleEditStateChanged(KeyState p_state);
|
||||||
|
void handleSelectionChanged();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
|
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
|
||||||
@ -69,12 +70,14 @@ private:
|
|||||||
// Returns the image relative path (image/xxx.png) only when
|
// Returns the image relative path (image/xxx.png) only when
|
||||||
// there is one and only one image link.
|
// there is one and only one image link.
|
||||||
QString fetchImageToPreview(const QString &p_text);
|
QString fetchImageToPreview(const QString &p_text);
|
||||||
|
void clearAllImagePreviewBlocks();
|
||||||
|
|
||||||
HGMarkdownHighlighter *m_mdHighlighter;
|
HGMarkdownHighlighter *m_mdHighlighter;
|
||||||
QVector<QString> m_insertedImages;
|
QVector<QString> m_insertedImages;
|
||||||
QVector<QString> m_initImages;
|
QVector<QString> m_initImages;
|
||||||
QVector<VHeader> m_headers;
|
QVector<VHeader> m_headers;
|
||||||
QColor m_cursorLineColor;
|
QColor m_cursorLineColor;
|
||||||
|
bool m_previewImage;
|
||||||
|
|
||||||
static const QString c_cursorLineColor;
|
static const QString c_cursorLineColor;
|
||||||
static const QString c_cursorLineColorVim;
|
static const QString c_cursorLineColorVim;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user