1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 11:37:32 +03:00

GH-339 Do forward search

This commit is contained in:
Tae Won Ha 2017-01-06 16:06:07 +01:00
parent a13f348d6a
commit 656399d78d
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
2 changed files with 73 additions and 13 deletions

View File

@ -331,7 +331,14 @@ extension MarkdownRenderer {
}
func forwardSearchAction(_: Any?) {
NSLog("\(#function) for \(self.neoVimInfoProvider?.currentLine()) x \(self.neoVimInfoProvider?.currentColumn())")
guard let row = self.neoVimInfoProvider?.currentLine(),
let column = self.neoVimInfoProvider?.currentColumn()
else {
return
}
// NSLog("\(#function) for \(row) x \(column)")
self.webview.evaluateJavaScript("scrollToPosition(\(row), \(column));")
}
func reverseSearchAction(_: Any?) {

View File

@ -4,6 +4,7 @@
<meta charset="UTF-8">
<link rel="stylesheet" href="github-markdown.css">
<script>
// Scrolling
const sourceposRegex = /(\d+):(\d+)-(\d+):(\d+)/;
function isTopLeftInsideViewport(el) {
@ -20,6 +21,21 @@
);
}
function positionOfMarkdownElement(element) {
const regexResult = element.dataset.sourcepos.match(sourceposRegex);
if (regexResult.length != 5) {
return null;
}
return {
lineBegin: parseInt(regexResult[1], 10),
columnBegin: parseInt(regexResult[2], 10),
lineEnd: parseInt(regexResult[3], 10),
columnEnd: parseInt(regexResult[4], 10)
};
}
function toArray(nodeList) {
return Array.prototype.slice.call(nodeList)
}
@ -32,18 +48,7 @@
return;
}
const regexResult = candidate.dataset.sourcepos.match(sourceposRegex);
if (regexResult.length != 5) {
return;
}
const result = {
lineBegin: parseInt(regexResult[1], 10),
columnBegin: parseInt(regexResult[2], 10),
lineEnd: parseInt(regexResult[3], 10),
columnEnd: parseInt(regexResult[4], 10)
};
const result = positionOfMarkdownElement(candidate);
window.webkit.messageHandlers.com_vimr_preview_markdown.postMessage(result);
}
@ -63,6 +68,54 @@
ticking = true;
});
</script>
<script>
// Forward search
function scrollToPosition(row, column) {
const entries = toArray(document.querySelectorAll("[data-sourcepos]"))
.map(element => {
const position = positionOfMarkdownElement(element);
return {
element: element,
position: position,
rowDistance: Math.abs(row - position.lineBegin),
columnDistance: Math.abs(column - position.columnBegin)
};
});
const minRowDistance = entries.reduce((result, entry) => {
return Math.min(result, entry.rowDistance)
}, Number.MAX_SAFE_INTEGER);
const entriesWithMinRowDistance = entries.filter(entry => entry.rowDistance == minRowDistance);
const minColumnDistance = entriesWithMinRowDistance.reduce((result, entry) => {
return Math.min(result, entry.columnDistance)
}, Number.MAX_SAFE_INTEGER);
const candidateEntry = entriesWithMinRowDistance.find(entry => entry.columnDistance == minColumnDistance);
if (!candidateEntry) {
return;
}
let {x, y} = scrollPosition(candidateEntry.element);
window.scrollTo(x, y);
}
function scrollPosition(element) {
let {x, y} = { x: 0, y: 0 };
let curEl = element;
while (curEl) {
x += curEl.offsetLeft;
y += curEl.offsetTop;
curEl = curEl.offsetParent;
}
return { x: x, y: y };
}
</script>
<title>{{ title }}</title>
</head>
<body class="markdown-body">