LivePreview: smart live preview for online PlantUML

This commit is contained in:
Le Tan 2018-09-12 20:35:52 +08:00
parent 62c0f218df
commit 3b2852aa67
3 changed files with 43 additions and 7 deletions

View File

@ -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,

View File

@ -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;
}

View File

@ -266,6 +266,8 @@ private:
void performSmartLivePreview();
bool isOnlineLivePreview(const QString &p_lang) const;
// Sorted by m_startBlock in ascending order.
QVector<CodeBlockPreviewInfo> m_codeBlocks;