fix(editor): fold fence parity, search bar layout and per-tab scroll memory
This commit is contained in:
@@ -441,12 +441,15 @@ body.editing.cm-active #cm-editor { display: block; height: 100%; padding: 10px
|
||||
|
||||
/* Edit-mode find/replace bar: same look as the preview findbar. */
|
||||
html.cm-editing { overflow: hidden; scrollbar-gutter: auto; }
|
||||
#edit-findbar { top: calc(var(--chrome-top) + 8px); z-index: 130; flex-wrap: wrap; }
|
||||
body.edit-finding #edit-findbar { display: flex; }
|
||||
#edit-findbar .edit-replace-group { display: flex; flex: 1 0 100%; align-items: center; gap: 6px; justify-content: flex-start; border-top: 1px solid rgba(0,0,0,.08); padding-top: 6px; margin-top: 2px; }
|
||||
#edit-findbar .edit-replace-group[hidden] { display: none; }
|
||||
#edit-findbar #edit-replace-input { width: 160px; }
|
||||
#edit-findbar button.edit-replace-btn { width: auto; min-width: 54px; padding: 0 8px; font-size: 12px; }
|
||||
#edit-findbar { top: calc(var(--chrome-top) + 8px); z-index: 130; width: min(560px, calc(100vw - 32px)); padding: 8px 10px; }
|
||||
body.edit-finding #edit-findbar { display: flex; flex-direction: column; align-items: stretch; gap: 6px; }
|
||||
#edit-findbar .edit-find-row { display: flex; align-items: center; gap: 6px; width: 100%; }
|
||||
#edit-findbar .edit-find-row input { flex: 1 1 auto; width: auto; min-width: 0; }
|
||||
#edit-findbar .edit-replace-row { display: flex; align-items: center; gap: 6px; width: 100%; border-top: 1px solid rgba(0,0,0,.08); padding-top: 6px; }
|
||||
#edit-findbar .edit-replace-row[hidden] { display: none; }
|
||||
#edit-findbar #edit-replace-input { flex: 1 1 auto; width: auto; min-width: 0; }
|
||||
#edit-findbar button.edit-replace-btn { width: auto; height: 28px; min-width: 64px; padding: 0 10px; border: 1px solid #e2e2e2; border-radius: 7px; background: #f2f2f2; color: #333; font-size: 12px; }
|
||||
#edit-findbar button.edit-replace-btn:hover { background: #e7e7e7; color: #111; }
|
||||
#cm-editor .cm-edit-find-hit { background: rgba(255, 213, 0, .38); }
|
||||
|
||||
/* Use the same thin scrollbar inside CodeMirror as the preview container. */
|
||||
@@ -459,5 +462,7 @@ body.edit-finding #edit-findbar { display: flex; }
|
||||
#app::-webkit-scrollbar-thumb:hover { background: rgba(200,200,200,.55); }
|
||||
#cm-editor .CodeMirror-vscrollbar::-webkit-scrollbar-thumb { background: rgba(200,200,200,.35); }
|
||||
#cm-editor .CodeMirror-vscrollbar::-webkit-scrollbar-thumb:hover { background: rgba(200,200,200,.55); }
|
||||
#edit-findbar .edit-replace-group { border-top-color: rgba(255,255,255,.12); }
|
||||
#edit-findbar .edit-replace-row { border-top-color: rgba(255,255,255,.12); }
|
||||
#edit-findbar button.edit-replace-btn { background: #333; border-color: #4a4a4a; color: #ddd; }
|
||||
#edit-findbar button.edit-replace-btn:hover { background: #3d3d3d; color: #fff; }
|
||||
}
|
||||
|
||||
@@ -26,16 +26,18 @@ __MDP_PAGE_CSS__
|
||||
<button id="find-close" title="Close" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="findbar" id="edit-findbar" role="search" hidden>
|
||||
<div class="edit-find-row">
|
||||
<input id="edit-find-input" type="search" placeholder="__MDP_SEARCH_PLACEHOLDER__" aria-label="__MDP_SEARCH_PLACEHOLDER__">
|
||||
<span id="edit-find-state"></span>
|
||||
<button id="edit-find-prev" title="Previous" aria-label="Previous"></button>
|
||||
<button id="edit-find-next" title="Next" aria-label="Next"></button>
|
||||
<button id="edit-find-close" title="Close" aria-label="Close"></button>
|
||||
<span class="edit-replace-group" id="edit-replace-group" hidden>
|
||||
</div>
|
||||
<div class="edit-replace-row" id="edit-replace-group" hidden>
|
||||
<input id="edit-replace-input" type="text" placeholder="Replace with" aria-label="Replace with">
|
||||
<button id="edit-replace-one" class="edit-replace-btn" type="button">Replace</button>
|
||||
<button id="edit-replace-all" class="edit-replace-btn" type="button">All</button>
|
||||
</span>
|
||||
<button id="edit-replace-all" class="edit-replace-btn" type="button">Replace All</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="app">
|
||||
<div id="preview">__MDP_PREVIEW_HTML__</div>
|
||||
|
||||
@@ -85,6 +85,7 @@
|
||||
var cmDisabled = false;
|
||||
var applyingRemote = false;
|
||||
var pendingScrollRatio = 0;
|
||||
var cmTabLines = {};
|
||||
// All source-text access goes through this adapter. The CodeMirror
|
||||
// implementation replaces the returned object; callers never touch ta.
|
||||
var ed = createTextareaEditor(ta);
|
||||
@@ -134,13 +135,27 @@
|
||||
function changeZoom(delta) {
|
||||
applyZoom(zoomPercent + delta, true);
|
||||
}
|
||||
function cmTopLine() {
|
||||
if (!cm) return 0;
|
||||
var info = cm.getScrollInfo();
|
||||
var line = cm.lineAtHeight(info.top, 'local');
|
||||
var top = cm.heightAtLine(line, 'local');
|
||||
while (line > 0 && top > info.top + 0.5) {
|
||||
line -= 1;
|
||||
top = cm.heightAtLine(line, 'local');
|
||||
}
|
||||
return line;
|
||||
}
|
||||
function cmRememberLine() {
|
||||
if (!cm || !inEdit() || !activeTabId) return;
|
||||
cmTabLines[activeTabId] = cmTopLine();
|
||||
}
|
||||
function currentScrollProgress() {
|
||||
if (cm && inEdit()) {
|
||||
var info = cm.getScrollInfo();
|
||||
var cmMax = info.height - info.clientHeight;
|
||||
if (cmMax <= 0) return 0;
|
||||
var line = cm.lineAtHeight(info.top, 'local');
|
||||
var lineTop = cm.heightAtLine(line, 'local');
|
||||
var lineTop = cm.heightAtLine(cmTopLine(), 'local');
|
||||
return Math.min(1, Math.max(0, lineTop / cmMax));
|
||||
}
|
||||
var max = Math.max(appEl.scrollHeight - appEl.clientHeight, 0);
|
||||
@@ -149,18 +164,21 @@
|
||||
}
|
||||
// A newer restore supersedes any callback the previous one left behind.
|
||||
var scrollRestoreToken = 0;
|
||||
function restoreScrollProgress(progress) {
|
||||
function restoreScrollProgress(progress, useRemembered) {
|
||||
if (cm && inEdit()) {
|
||||
pendingScrollRatio = progress;
|
||||
var cmToken = ++scrollRestoreToken;
|
||||
var applyCm = function() {
|
||||
if (cmToken !== scrollRestoreToken) return;
|
||||
var remembered = useRemembered ? cmTabLines[activeTabId] : undefined;
|
||||
if (typeof remembered === 'number') {
|
||||
cm.scrollTo(null, cm.heightAtLine(remembered, 'local'));
|
||||
return;
|
||||
}
|
||||
var info = cm.getScrollInfo();
|
||||
var cmMax = Math.max(info.height - info.clientHeight, 0);
|
||||
cm.scrollTo(null, cmMax * progress);
|
||||
// Land on a line boundary so repeated switches do not drift.
|
||||
var topLine = cm.lineAtHeight(cm.getScrollInfo().top, 'local');
|
||||
cm.scrollTo(null, cm.heightAtLine(topLine, 'local'));
|
||||
cm.scrollTo(null, cm.heightAtLine(cmTopLine(), 'local'));
|
||||
};
|
||||
requestAnimationFrame(function() {
|
||||
if (cmToken !== scrollRestoreToken) return;
|
||||
@@ -296,6 +314,7 @@
|
||||
}
|
||||
window.__mdPreviewSave = save;
|
||||
function requestTabAction(action, id) {
|
||||
cmRememberLine();
|
||||
cancelPendingAutosave();
|
||||
var message = 'tab-action:' + action + ':' + id + ':' + currentScrollProgress().toFixed(4);
|
||||
if (dirty) {
|
||||
@@ -678,7 +697,7 @@
|
||||
lineWrapping: true,
|
||||
foldGutter: { rangeFinder: markdownFoldRange, minFoldSize: 0 },
|
||||
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
|
||||
foldOptions: { rangeFinder: markdownFoldRange, minFoldSize: 0 },
|
||||
foldOptions: { rangeFinder: markdownFoldRange, minFoldSize: 0, widget: '⋯' },
|
||||
indentUnit: 2,
|
||||
tabSize: 2,
|
||||
indentWithTabs: false,
|
||||
@@ -763,6 +782,16 @@
|
||||
if (!text) return null;
|
||||
var fence = /^\s*(```+|~~~+)/.exec(text);
|
||||
if (fence) {
|
||||
// Only an opening fence gets a fold marker. A closing fence is inside the
|
||||
// block that started above it, so scanning up tells the two apart.
|
||||
var openFence = null;
|
||||
for (var b = 0; b < lineNo; b += 1) {
|
||||
var priorFence = /^\s*(```+|~~~+)/.exec(instance.getLine(b) || '');
|
||||
if (!priorFence) continue;
|
||||
if (!openFence) openFence = priorFence[1].charAt(0);
|
||||
else if (priorFence[1].charAt(0) === openFence) openFence = null;
|
||||
}
|
||||
if (openFence) return null;
|
||||
var marker = fence[1];
|
||||
var fenceEnd = -1;
|
||||
for (var f = lineNo + 1; f <= instance.lastLine(); f += 1) {
|
||||
@@ -960,6 +989,7 @@
|
||||
if (window.ipc) window.ipc.postMessage('edit-mode:' + activeTabId + ':1');
|
||||
}
|
||||
function leaveEdit() {
|
||||
cmRememberLine();
|
||||
var progress = currentScrollProgress();
|
||||
if (dirty) save();
|
||||
setEditMode(false);
|
||||
@@ -1300,6 +1330,7 @@
|
||||
requestTabAction('activate', tab.getAttribute('data-tab-id'));
|
||||
});
|
||||
window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio) {
|
||||
cmRememberLine();
|
||||
if (editFinding) closeEditFind();
|
||||
document.body.classList.remove('home');
|
||||
document.body.classList.remove('missing');
|
||||
@@ -1313,7 +1344,7 @@
|
||||
setDirty(false);
|
||||
}
|
||||
setEditMode(!!editing);
|
||||
restoreScrollProgress(typeof scrollRatio === 'number' ? scrollRatio : 0);
|
||||
restoreScrollProgress(typeof scrollRatio === 'number' ? scrollRatio : 0, true);
|
||||
};
|
||||
window.__setEmptyPreview = function(previewHtml) {
|
||||
if (editFinding) closeEditFind();
|
||||
|
||||
@@ -281,7 +281,10 @@ mod tests {
|
||||
assert!(page.contains("var lineNo = typeof line === 'number'"));
|
||||
assert!(page.contains("appEl.addEventListener('scroll', reportScroll"));
|
||||
assert!(page.contains("cm.lineAtHeight(info.top, 'local')"));
|
||||
assert!(page.contains("flex: 1 0 100%"));
|
||||
assert!(page.contains("width: min(560px, calc(100vw - 32px))"));
|
||||
assert!(page.contains("edit-find-row"));
|
||||
assert!(page.contains("widget: '⋯'"));
|
||||
assert!(page.contains("priorFence"));
|
||||
// Escape closes the find bar; it must not leave edit mode anymore.
|
||||
assert!(!page.contains("if (e.key === 'Escape' && inEdit()) { leaveEdit(); }"));
|
||||
assert!(!page.contains("'Esc': function() { leaveEdit(); }"));
|
||||
|
||||
Reference in New Issue
Block a user