From 3b2852aa67534b9ae56820da1af54f47cf5ea1c4 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 12 Sep 2018 20:35:52 +0800 Subject: [PATCH] LivePreview: smart live preview for online PlantUML --- src/resources/markdown_template.js | 32 ++++++++++++++++++++++++++---- src/vlivepreviewhelper.cpp | 16 ++++++++++++--- src/vlivepreviewhelper.h | 2 ++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/resources/markdown_template.js b/src/resources/markdown_template.js index 72323c94..dc742447 100644 --- a/src/resources/markdown_template.js +++ b/src/resources/markdown_template.js @@ -1625,6 +1625,10 @@ var htmlToText = function(identifier, id, timeStamp, html) { content.htmlToTextCB(identifier, id, timeStamp, markdown); }; +var printRect = function(rect) { + content.setLog('rect ' + rect.left + ' ' + rect.top + ' ' + rect.width + ' ' + rect.height); +}; + var performSmartLivePreview = function(lang, text, hints, isRegex) { if (previewDiv.style.display == 'none' || document.getSelection().type == 'Range') { @@ -1635,11 +1639,25 @@ var performSmartLivePreview = function(lang, text, hints, isRegex) { return; } + var previewNode = previewDiv; + var trectOffset = null; + try { + var objs = previewNode.getElementsByTagName('object'); + if (objs.length > 0) { + var obj = objs[0]; + previewNode = obj.contentDocument.children[0]; + trectOffset = obj.getBoundingClientRect(); + } + } catch (err) { + content.setLog("err: " + err); + return; + } + // PlantUML. var targetNode = null; if (hints.indexOf('id') >= 0) { // isRegex is ignored. - var result = findNodeWithText(previewDiv, + var result = findNodeWithText(previewNode, text, function (node, text) { if (node.id && node.id == text) { @@ -1660,7 +1678,7 @@ var performSmartLivePreview = function(lang, text, hints, isRegex) { var result; if (isRegex) { var nodeReg = new RegExp(text); - result = findNodeWithText(previewDiv, + result = findNodeWithText(previewNode, text, function(node, text) { var se = nodeReg.exec(node.textContent); @@ -1677,7 +1695,7 @@ var performSmartLivePreview = function(lang, text, hints, isRegex) { return res; }); } else { - result = findNodeWithText(previewDiv, + result = findNodeWithText(previewNode, text, function(node, text) { var idx = node.textContent.indexOf(text); @@ -1704,7 +1722,13 @@ var performSmartLivePreview = function(lang, text, hints, isRegex) { // (left, top) is relative to the viewport. // Should add window.scrollX and window.scrollY to get the real content offset. - var trect = targetNode.getBoundingClientRect(); + var tbrect = targetNode.getBoundingClientRect(); + var trect = { + left: tbrect.left + (trectOffset ? trectOffset.left : 0), + top: tbrect.top + (trectOffset ? trectOffset.top : 0), + width: tbrect.width, + height: tbrect.height + }; var vrect = { left: document.documentElement.scrollLeft || document.body.scrollLeft || window.pageXOffset, diff --git a/src/vlivepreviewhelper.cpp b/src/vlivepreviewhelper.cpp index 72c5ab7f..522c4b82 100644 --- a/src/vlivepreviewhelper.cpp +++ b/src/vlivepreviewhelper.cpp @@ -540,11 +540,11 @@ void VLivePreviewHelper::performSmartLivePreview() } const CodeBlockPreviewInfo &cb = m_codeBlocks[m_cbIndex]; - if (!cb.hasImageData()) { + const VCodeBlock &vcb = cb.codeBlock(); + if (!cb.hasImageData() && !isOnlineLivePreview(vcb.m_lang)) { return; } - const VCodeBlock &vcb = cb.codeBlock(); const QTextBlock block = m_editor->textCursorW().block(); if (block.blockNumber() <= vcb.m_startBlock || block.blockNumber() >= vcb.m_endBlock) { @@ -553,7 +553,7 @@ void VLivePreviewHelper::performSmartLivePreview() QString keyword, hints; bool isRegex = false; - if (vcb.m_lang == "puml" && m_plantUMLMode == PlantUMLMode::LocalPlantUML) { + if (vcb.m_lang == "puml") { keyword = VPlantUMLHelper::keywordForSmartLivePreview(block.text(), hints, isRegex); @@ -561,3 +561,13 @@ void VLivePreviewHelper::performSmartLivePreview() m_document->performSmartLivePreview(vcb.m_lang, keyword, hints, isRegex); } + +bool VLivePreviewHelper::isOnlineLivePreview(const QString &p_lang) const +{ + if (p_lang == "dot" + || (p_lang == "puml" && m_plantUMLMode == PlantUMLMode::LocalPlantUML)) { + return false; + } + + return true; +} diff --git a/src/vlivepreviewhelper.h b/src/vlivepreviewhelper.h index c6be2cf3..e339ada6 100644 --- a/src/vlivepreviewhelper.h +++ b/src/vlivepreviewhelper.h @@ -266,6 +266,8 @@ private: void performSmartLivePreview(); + bool isOnlineLivePreview(const QString &p_lang) const; + // Sorted by m_startBlock in ascending order. QVector m_codeBlocks;