highlighter fix

This commit is contained in:
Le Tan 2018-04-20 19:55:28 +08:00
parent 67e40cbe87
commit 6fe82d3db0
6 changed files with 59 additions and 42 deletions

View File

@ -856,7 +856,7 @@ VMarkdownTab::VMarkdownTab(QWidget *p_parent)
{
// Default note open mode.
m_openModeCombo = VUtils::getComboBox();
m_openModeCombo->setToolTip(tr("Default mode to open a note"));
m_openModeCombo->setToolTip(tr("Default mode to open an internal note"));
m_openModeCombo->addItem(tr("Read Mode"), (int)OpenFileMode::Read);
m_openModeCombo->addItem(tr("Edit Mode"), (int)OpenFileMode::Edit);
@ -865,7 +865,7 @@ VMarkdownTab::VMarkdownTab(QWidget *p_parent)
m_headingSequenceTypeCombo->setToolTip(tr("Enable auto sequence for all headings (in the form like 1.2.3.4.)"));
m_headingSequenceTypeCombo->addItem(tr("Disabled"), (int)HeadingSequenceType::Disabled);
m_headingSequenceTypeCombo->addItem(tr("Enabled"), (int)HeadingSequenceType::Enabled);
m_headingSequenceTypeCombo->addItem(tr("Enabled for notes only"), (int)HeadingSequenceType::EnabledNoteOnly);
m_headingSequenceTypeCombo->addItem(tr("Enabled for intrenal notes only"), (int)HeadingSequenceType::EnabledNoteOnly);
m_headingSequenceLevelCombo = VUtils::getComboBox();
m_headingSequenceLevelCombo->setToolTip(tr("Base level to start heading sequence"));

View File

@ -36,6 +36,7 @@ HGMarkdownHighlighter::HGMarkdownHighlighter(const QVector<HighlightingStyle> &s
m_blockHLResultReady(false),
waitInterval(waitInterval),
m_enableMathjax(false),
m_signalOut(false),
content(NULL),
capacity(0),
result(NULL)
@ -82,15 +83,12 @@ HGMarkdownHighlighter::HGMarkdownHighlighter(const QVector<HighlightingStyle> &s
startParseAndHighlight(false);
});
static const int completeWaitTime = 500;
const int completeWaitTime = 400;
m_completeTimer = new QTimer(this);
m_completeTimer->setSingleShot(true);
m_completeTimer->setInterval(completeWaitTime);
connect(m_completeTimer, &QTimer::timeout,
this, [this]() {
updateMathjaxBlocks();
emit highlightCompleted();
});
this, &HGMarkdownHighlighter::completeHighlight);
connect(document, &QTextDocument::contentsChange,
this, &HGMarkdownHighlighter::handleContentChange);
@ -331,7 +329,6 @@ void HGMarkdownHighlighter::initImageRegionsFromResult()
m_imageRegions.clear();
if (!result) {
emit imageLinksUpdated(m_imageRegions);
return;
}
@ -345,10 +342,6 @@ void HGMarkdownHighlighter::initImageRegionsFromResult()
m_imageRegions.push_back(VElementRegion(elem->pos, elem->end));
elem = elem->next;
}
qDebug() << "highlighter: parse" << m_imageRegions.size() << "image regions";
emit imageLinksUpdated(m_imageRegions);
}
void HGMarkdownHighlighter::initVerbatimBlocksFromResult()
@ -383,7 +376,6 @@ void HGMarkdownHighlighter::initHeaderRegionsFromResult()
m_headerRegions.clear();
if (!result) {
emit headersUpdated(m_headerRegions);
return;
}
@ -410,10 +402,6 @@ void HGMarkdownHighlighter::initHeaderRegionsFromResult()
}
std::sort(m_headerRegions.begin(), m_headerRegions.end());
qDebug() << "highlighter: parse" << m_headerRegions.size() << "header regions";
emit headersUpdated(m_headerRegions);
}
void HGMarkdownHighlighter::initBlockHighlihgtOne(unsigned long pos,
@ -756,7 +744,7 @@ void HGMarkdownHighlighter::highlightLinkWithSpacesInURL(const QString &p_text)
}
}
void HGMarkdownHighlighter::parse(bool p_fast)
void HGMarkdownHighlighter::parse()
{
if (!parsing.testAndSetRelaxed(0, 1)) {
return;
@ -776,7 +764,6 @@ void HGMarkdownHighlighter::parse(bool p_fast)
m_blockHLResultReady = true;
if (!p_fast) {
initHtmlCommentRegionsFromResult();
initImageRegionsFromResult();
@ -790,7 +777,6 @@ void HGMarkdownHighlighter::parse(bool p_fast)
initBoldItalicRegionsFromResult();
initLinkRegionsFromResult();
}
if (result) {
pmh_free_elements(result);
@ -834,6 +820,8 @@ void HGMarkdownHighlighter::handleContentChange(int /* position */, int charsRem
return;
}
m_signalOut = false;
timer->stop();
timer->start();
}
@ -841,17 +829,15 @@ void HGMarkdownHighlighter::handleContentChange(int /* position */, int charsRem
void HGMarkdownHighlighter::startParseAndHighlight(bool p_fast)
{
qDebug() << "HGMarkdownHighlighter start a new parse (fast" << p_fast << ")";
parse(p_fast);
parse();
if (p_fast) {
rehighlight();
} else {
if (!updateCodeBlocks()) {
rehighlight();
m_signalOut = !p_fast;
if (!p_fast) {
updateCodeBlocks();
}
highlightChanged();
}
rehighlight();
}
void HGMarkdownHighlighter::updateHighlight()
@ -1341,3 +1327,15 @@ bool HGMarkdownHighlighter::isValidMathjaxRegion(int p_blockNumber,
return true;
}
void HGMarkdownHighlighter::completeHighlight()
{
if (m_signalOut) {
m_signalOut = false;
updateMathjaxBlocks();
emit imageLinksUpdated(m_imageRegions);
emit headersUpdated(m_headerRegions);
}
emit highlightCompleted();
}

View File

@ -241,6 +241,8 @@ private slots:
// @p_fast: if true, just parse and update styles.
void startParseAndHighlight(bool p_fast = false);
void completeHighlight();
private:
struct HeaderBlockInfo
{
@ -330,6 +332,9 @@ private:
// Comment regions for each block.
QHash<int, QVector<VElementRegion>> m_commentRegions;
// Whether need to signal out changes when highlight completes.
bool m_signalOut;
char *content;
int capacity;
pmh_element **result;
@ -347,7 +352,7 @@ private:
// intended to complement this.
void highlightLinkWithSpacesInURL(const QString &p_text);
void parse(bool p_fast = false);
void parse();
void parseInternal();

View File

@ -82,8 +82,16 @@ void VListWidget::resizeEvent(QResizeEvent *p_event)
void VListWidget::handleSearchModeTriggered(bool p_inSearchMode, bool p_focus)
{
if (p_inSearchMode) {
setSearchInputVisible(p_inSearchMode);
} else {
// Hiding search input will make QWebEngine get focus which will consume
// the Esc key sequence by mistake.
if (p_focus) {
setFocus();
}
setSearchInputVisible(p_inSearchMode);
if (!p_inSearchMode) {
clearItemsHighlight();
}

View File

@ -496,7 +496,7 @@ void VMdEditor::updateHeaders(const QVector<VElementRegion> &p_headerRegions)
<< block.text();
}
if ((block.userState() == HighlightBlockState::Normal)
if ((block.userState() == HighlightBlockState::Header)
&& headerReg.exactMatch(block.text())) {
int level = headerReg.cap(1).length();
VTableOfContentItem header(headerReg.cap(2).trimmed(),

View File

@ -323,6 +323,7 @@ int VPreviewManager::calculateBlockMargin(const QTextBlock &p_block, int p_tabSt
void VPreviewManager::updateBlockPreviewInfo(TS p_timeStamp,
const QVector<ImageLinkInfo> &p_imageLinks)
{
QSet<int> affectedBlocks;
for (auto const & link : p_imageLinks) {
QTextBlock block = m_document->findBlockByNumber(link.m_blockNumber);
if (!block.isValid()) {
@ -345,16 +346,21 @@ void VPreviewManager::updateBlockPreviewInfo(TS p_timeStamp,
!link.m_isBlock,
name,
m_editor->imageSize(name));
blockData->insertPreviewInfo(info);
bool tsUpdated = blockData->insertPreviewInfo(info);
imageCache(PreviewSource::ImageLink).insert(name, p_timeStamp);
if (!tsUpdated) {
// No need to relayout the block if only timestamp is updated.
affectedBlocks.insert(link.m_blockNumber);
m_highlighter->addPossiblePreviewBlock(link.m_blockNumber);
}
qDebug() << "block" << link.m_blockNumber
<< imageCache(PreviewSource::ImageLink).size()
<< blockData->toString();
}
// TODO: may need to call m_editor->update()?
m_editor->relayout(affectedBlocks);
m_editor->update();
}
void VPreviewManager::updateBlockPreviewInfo(TS p_timeStamp,