feat: find from the selected preview text without scrolling

Ctrl+F fills the find box from the current preview selection, anchors the first hit to that selection, and suppresses scroll when the find box opens. Typed queries still start from the current viewport and next/previous still reveal their target.
This commit is contained in:
2026-10-30 18:48:48 +08:00
parent 8644966b10
commit 618ac47b83

View File

@@ -1752,11 +1752,38 @@ body.editing #app {{ max-width: none; padding: 0; }}
// Home tab replaces it.
requestTabAction('new-tab', 0);
}}
function showFind() {{
function selectionSeed() {{
var sel = window.getSelection && window.getSelection();
if (!sel || sel.rangeCount === 0) return null;
var anchor = sel.anchorNode;
var element = anchor && (anchor.nodeType === 3 ? anchor.parentElement : anchor);
if (!element || !element.closest || !element.closest('#preview')) return null;
var text = sel.toString();
if (!text) return null;
var top = null;
try {{
var rect = sel.getRangeAt(0).getBoundingClientRect();
if (rect && (rect.width || rect.height)) top = rect.top;
}} catch (_) {{}}
var lines = text.split(/\r?\n/);
for (var i = 0; i < lines.length; i += 1) {{
var line = lines[i].trim();
if (line) return {{ text: line, top: top }};
}}
return null;
}}
function showFind(seed, anchorTop) {{
if (document.body.classList.contains('home')) return;
if (inEdit()) return;
document.body.classList.add('finding');
setTimeout(function(){{ findInput.focus(); findInput.select(); }}, 0);
if (typeof seed === 'string' && seed) {{
findInput.value = seed;
runFindQuery(seed, anchorTop, false);
}}
setTimeout(function(){{
try {{ findInput.focus({{ preventScroll: true }}); }} catch (_) {{ findInput.focus(); }}
findInput.select();
}}, 0);
}}
window.__mdPreviewShowFind = showFind;
function hideFind() {{
@@ -1800,7 +1827,7 @@ body.editing #app {{ max-width: none; padding: 0; }}
setTimeout(function() {{ focusFindInput(selectionStart, selectionEnd); }}, 80);
}});
}}
function selectFindHit(index) {{
function selectFindHit(index, reveal) {{
if (!findHits.length) {{
currentFindHit = -1;
updateFindState();
@@ -1812,11 +1839,28 @@ body.editing #app {{ max-width: none; padding: 0; }}
currentFindHit = (index + findHits.length) % findHits.length;
var hit = findHits[currentFindHit];
hit.classList.add('current');
if (reveal !== false) {{
if (window.__mdPreviewExpandFold) window.__mdPreviewExpandFold(hit);
hit.scrollIntoView({{ block: 'center', inline: 'nearest' }});
}}
updateFindState();
}}
function runFindQuery(query) {{
function findStartIndex(anchorTop) {{
if (!findHits.length) return 0;
var startTop = typeof anchorTop === 'number' ? anchorTop : 0;
var lastAbove = -1;
for (var i = 0; i < findHits.length; i += 1) {{
var rect = findHits[i].getBoundingClientRect();
if (!rect.width && !rect.height) continue;
if (rect.bottom < startTop) {{
lastAbove = i;
continue;
}}
return i;
}}
return lastAbove >= 0 ? lastAbove : 0;
}}
function runFindQuery(query, anchorTop, reveal) {{
clearFindHits();
query = String(query || '').trim();
if (!query) return;
@@ -1855,7 +1899,7 @@ body.editing #app {{ max-width: none; padding: 0; }}
if (start < text.length) fragment.appendChild(document.createTextNode(text.slice(start)));
node.parentNode.replaceChild(fragment, node);
}});
selectFindHit(0);
selectFindHit(findStartIndex(anchorTop), reveal);
}}
function runFind(backward) {{
var q = findInput.value;
@@ -2234,8 +2278,9 @@ body.editing #app {{ max-width: none; padding: 0; }}
}}
if ((e.metaKey || e.ctrlKey) && (e.key === 'f' || e.key === 'F')) {{
if (inEdit()) return;
var seed = (document.body.classList.contains('finding') && document.activeElement === findInput) ? null : selectionSeed();
e.preventDefault();
showFind();
showFind(seed ? seed.text : '', seed ? seed.top : null);
return;
}}
if ((e.metaKey || e.ctrlKey) && (e.key === 'e' || e.key === 'E')) {{
@@ -2865,8 +2910,16 @@ mod tests {
assert!(page.contains("window.__mdPreviewOpenFile = openFile"));
assert!(page.contains("window.__mdPreviewShowFind = showFind"));
assert!(page.contains("window.__mdPreviewToggleEdit"));
assert!(page
.contains("if (inEdit()) return;\n\t e.preventDefault();\n\t showFind();"));
assert!(page.contains("function selectionSeed()"));
assert!(page.contains("showFind(seed ? seed.text : ''"));
assert!(page.contains("function findStartIndex(anchorTop)"));
assert!(page.contains("return lastAbove >= 0 ? lastAbove : 0"));
assert!(page.contains("findInput.focus({ preventScroll: true })"));
assert!(page.contains("selectFindHit(findStartIndex(anchorTop), reveal)"));
assert!(page.contains("runFindQuery(seed, anchorTop, false)"));
assert!(page.contains("var INDENT = ' ';"));
assert!(page.contains("insertNewlineWithIndent"));
assert!(page.contains("document.execCommand('insertText'"));
assert!(page.contains("id=\"hljs-code\""));
assert!(page.contains("#preview pre { background: #1e1e1e;"));
assert!(!page.contains("hljs-light"));