fix(editor): add find/replace bar and tidy CodeMirror scrolling
This commit is contained in:
@@ -20,6 +20,16 @@
|
||||
var findPrev = document.getElementById('find-prev');
|
||||
var findNext = document.getElementById('find-next');
|
||||
var findClose = document.getElementById('find-close');
|
||||
var editFindBar = document.getElementById('edit-findbar');
|
||||
var editFindInput = document.getElementById('edit-find-input');
|
||||
var editFindState = document.getElementById('edit-find-state');
|
||||
var editFindPrev = document.getElementById('edit-find-prev');
|
||||
var editFindNextBtn = document.getElementById('edit-find-next');
|
||||
var editFindClose = document.getElementById('edit-find-close');
|
||||
var editReplaceGroup = document.getElementById('edit-replace-group');
|
||||
var editReplaceInput = document.getElementById('edit-replace-input');
|
||||
var editReplaceOne = document.getElementById('edit-replace-one');
|
||||
var editReplaceAll = document.getElementById('edit-replace-all');
|
||||
var tabsEl = document.getElementById('tabs');
|
||||
var docStats = document.getElementById('doc-stats');
|
||||
var tabOpen = document.getElementById('tab-open');
|
||||
@@ -49,6 +59,11 @@
|
||||
// original start-up page. Set to false to keep the tab bar always on.
|
||||
var HIDE_SINGLE_HOME_TAB = true;
|
||||
var findHits = [];
|
||||
var editFinding = false;
|
||||
var editFindMatches = [];
|
||||
var editFindIndex = -1;
|
||||
var editFindOverlay = null;
|
||||
var editFindTimer = 0;
|
||||
var currentFindHit = -1;
|
||||
var lastFindQuery = '';
|
||||
var STAT_WORDS = '__MDP_STAT_WORDS_JS__';
|
||||
@@ -79,6 +94,9 @@
|
||||
findPrev.innerHTML = ICON_UP;
|
||||
findNext.innerHTML = ICON_DOWN;
|
||||
findClose.innerHTML = ICON_CLOSE;
|
||||
editFindPrev.innerHTML = ICON_UP;
|
||||
editFindNextBtn.innerHTML = ICON_DOWN;
|
||||
editFindClose.innerHTML = ICON_CLOSE;
|
||||
|
||||
function inEdit() { return document.body.classList.contains('editing'); }
|
||||
function textSegments(text) {
|
||||
@@ -131,12 +149,15 @@
|
||||
function restoreScrollProgress(progress) {
|
||||
if (cm && inEdit()) {
|
||||
pendingScrollRatio = progress;
|
||||
var cmToken = ++scrollRestoreToken;
|
||||
var applyCm = function() {
|
||||
if (cmToken !== scrollRestoreToken) return;
|
||||
var info = cm.getScrollInfo();
|
||||
var cmMax = Math.max(info.height - info.clientHeight, 0);
|
||||
cm.scrollTo(null, cmMax * progress);
|
||||
};
|
||||
requestAnimationFrame(function() {
|
||||
if (cmToken !== scrollRestoreToken) return;
|
||||
cm.refresh();
|
||||
applyCm();
|
||||
requestAnimationFrame(applyCm);
|
||||
@@ -650,9 +671,9 @@
|
||||
mode: { name: 'markdown', highlightFormatting: true },
|
||||
lineNumbers: true,
|
||||
lineWrapping: true,
|
||||
foldGutter: { rangeFinder: markdownFoldRange, minFoldSize: 1 },
|
||||
foldGutter: { rangeFinder: markdownFoldRange, minFoldSize: 0 },
|
||||
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
|
||||
foldOptions: { rangeFinder: markdownFoldRange, minFoldSize: 1 },
|
||||
foldOptions: { rangeFinder: markdownFoldRange, minFoldSize: 0 },
|
||||
indentUnit: 2,
|
||||
tabSize: 2,
|
||||
indentWithTabs: false,
|
||||
@@ -671,13 +692,13 @@
|
||||
if (removed) instance.replaceRange('', { line: cursor.line, ch: 0 }, { line: cursor.line, ch: removed });
|
||||
},
|
||||
'Enter': function(instance) { instance.execCommand('newlineAndIndent'); },
|
||||
'Ctrl-F': function(instance) { instance.execCommand('find'); },
|
||||
'Cmd-F': function(instance) { instance.execCommand('find'); },
|
||||
'Ctrl-H': function(instance) { instance.execCommand('replace'); },
|
||||
'Cmd-H': function(instance) { instance.execCommand('replace'); },
|
||||
'Ctrl-F': function() { openEditFind(false); },
|
||||
'Cmd-F': function() { openEditFind(false); },
|
||||
'Ctrl-H': function() { openEditFind(true); },
|
||||
'Cmd-H': function() { openEditFind(true); },
|
||||
'Ctrl-S': function() { save(); },
|
||||
'Cmd-S': function() { save(); },
|
||||
'Esc': function() { leaveEdit(); }
|
||||
'Esc': function() { if (editFinding) closeEditFind(); }
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
@@ -695,6 +716,7 @@
|
||||
cm.on('scroll', reportScroll);
|
||||
updateEditorZoom();
|
||||
document.body.classList.toggle('cm-active', inEdit());
|
||||
document.documentElement.classList.toggle('cm-editing', inEdit());
|
||||
if (inEdit()) {
|
||||
ed.focus();
|
||||
restoreScrollProgress(pendingScrollRatio);
|
||||
@@ -774,8 +796,132 @@
|
||||
if (end <= line) return null;
|
||||
return { from: CodeMirror.Pos(line + 1, 0), to: CodeMirror.Pos(end, instance.getLine(end).length) };
|
||||
}
|
||||
// ---- Edit-mode find/replace ----------------------------------------------
|
||||
function editFindOverlayFor(query) {
|
||||
var escaped = query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
|
||||
var re = new RegExp(escaped, 'gi');
|
||||
return { token: function(stream) {
|
||||
re.lastIndex = stream.pos;
|
||||
var match = re.exec(stream.string);
|
||||
if (match && match.index === stream.pos) {
|
||||
stream.pos += match[0].length || 1;
|
||||
return 'edit-find-hit';
|
||||
}
|
||||
if (match) { stream.pos = match.index; } else { stream.skipToEnd(); }
|
||||
}};
|
||||
}
|
||||
function editFindClearOverlay() {
|
||||
if (cm && editFindOverlay) cm.removeOverlay(editFindOverlay);
|
||||
editFindOverlay = null;
|
||||
}
|
||||
function editFindVisible(on) {
|
||||
editFinding = on;
|
||||
document.body.classList.toggle('edit-finding', on);
|
||||
editFindBar.hidden = !on;
|
||||
if (!on) {
|
||||
if (editFindTimer) { clearTimeout(editFindTimer); editFindTimer = 0; }
|
||||
editFindClearOverlay();
|
||||
editFindMatches = [];
|
||||
editFindIndex = -1;
|
||||
editFindState.textContent = '';
|
||||
if (cm && inEdit()) cm.focus();
|
||||
}
|
||||
}
|
||||
function openEditFind(replaceMode) {
|
||||
if (!cm || !inEdit()) return;
|
||||
editReplaceGroup.hidden = !replaceMode;
|
||||
editFindVisible(true);
|
||||
var selected = cm.getSelection();
|
||||
if (selected && selected.indexOf('\n') < 0) editFindInput.value = selected;
|
||||
editFindInput.focus();
|
||||
editFindInput.select();
|
||||
editFindRun(true);
|
||||
}
|
||||
function closeEditFind() {
|
||||
editFindVisible(false);
|
||||
}
|
||||
function editFindSelect(index) {
|
||||
if (!editFindMatches.length) {
|
||||
editFindIndex = -1;
|
||||
editFindState.textContent = '';
|
||||
return;
|
||||
}
|
||||
editFindIndex = (index + editFindMatches.length) % editFindMatches.length;
|
||||
var match = editFindMatches[editFindIndex];
|
||||
editFindState.textContent = (editFindIndex + 1) + '/' + editFindMatches.length;
|
||||
cm.setSelection(match.from, match.to);
|
||||
try { cm.scrollIntoView({ from: match.from, to: match.to }, 40); } catch (_) {}
|
||||
}
|
||||
function editFindRun(reset) {
|
||||
if (!cm || !editFinding) return;
|
||||
var query = editFindInput.value;
|
||||
editFindClearOverlay();
|
||||
editFindMatches = [];
|
||||
editFindIndex = -1;
|
||||
if (!query) {
|
||||
editFindState.textContent = '';
|
||||
return;
|
||||
}
|
||||
editFindOverlay = editFindOverlayFor(query);
|
||||
cm.addOverlay(editFindOverlay);
|
||||
var cursor = cm.getSearchCursor(query, CodeMirror.Pos(cm.firstLine(), 0), { caseFold: true });
|
||||
while (cursor.findNext()) {
|
||||
editFindMatches.push({ from: cursor.from(), to: cursor.to() });
|
||||
if (editFindMatches.length >= 5000) break;
|
||||
}
|
||||
if (!editFindMatches.length) {
|
||||
editFindState.textContent = '0/0';
|
||||
return;
|
||||
}
|
||||
if (reset) {
|
||||
var current = cm.getCursor('from');
|
||||
var start = 0;
|
||||
for (var i = 0; i < editFindMatches.length; i += 1) {
|
||||
if (CodeMirror.cmpPos(editFindMatches[i].from, current) >= 0) { start = i; break; }
|
||||
}
|
||||
editFindSelect(start);
|
||||
} else {
|
||||
editFindSelect(0);
|
||||
}
|
||||
}
|
||||
function editFindStep(backward) {
|
||||
if (!editFinding) return;
|
||||
if (!editFindMatches.length) { editFindRun(true); return; }
|
||||
editFindSelect(editFindIndex + (backward ? -1 : 1));
|
||||
}
|
||||
function editFindReplaceOne() {
|
||||
if (!cm || !editFinding) return;
|
||||
var query = editFindInput.value;
|
||||
if (!query) return;
|
||||
if (!editFindMatches.length) editFindRun(true);
|
||||
var match = editFindMatches[editFindIndex];
|
||||
if (!match) return;
|
||||
var replacement = editReplaceInput.value;
|
||||
if (cm.getRange(match.from, match.to) !== cm.getSelection()) {
|
||||
editFindSelect(editFindIndex);
|
||||
return;
|
||||
}
|
||||
cm.replaceRange(replacement, match.from, match.to, '+input');
|
||||
editFindRun(true);
|
||||
}
|
||||
function editFindReplaceAll() {
|
||||
if (!cm || !editFinding) return;
|
||||
var query = editFindInput.value;
|
||||
if (!query) return;
|
||||
if (!editFindMatches.length) editFindRun(true);
|
||||
if (!editFindMatches.length) return;
|
||||
var replacement = editReplaceInput.value;
|
||||
var matches = editFindMatches.slice();
|
||||
cm.operation(function() {
|
||||
for (var i = matches.length - 1; i >= 0; i -= 1) {
|
||||
cm.replaceRange(replacement, matches[i].from, matches[i].to, '+input');
|
||||
}
|
||||
});
|
||||
editFindRun(true);
|
||||
}
|
||||
// Edit mode is per tab and owned by Rust; this only paints it.
|
||||
function setEditMode(on) {
|
||||
if (!on && editFinding) closeEditFind();
|
||||
if (on && !cm && !cmDisabled) requestEditorSource();
|
||||
if (on) {
|
||||
document.body.classList.add('editing');
|
||||
@@ -790,8 +936,10 @@
|
||||
}
|
||||
if (cm) {
|
||||
document.body.classList.toggle('cm-active', on);
|
||||
document.documentElement.classList.toggle('cm-editing', on);
|
||||
if (on) cm.refresh();
|
||||
} else {
|
||||
document.documentElement.classList.remove('cm-editing');
|
||||
ed.resize();
|
||||
}
|
||||
}
|
||||
@@ -936,6 +1084,22 @@
|
||||
findPrev.addEventListener('click', function() { runFind(true); });
|
||||
findNext.addEventListener('click', function() { runFind(false); });
|
||||
findClose.addEventListener('click', hideFind);
|
||||
editFindInput.addEventListener('input', function() {
|
||||
if (editFindTimer) clearTimeout(editFindTimer);
|
||||
editFindTimer = setTimeout(function() { editFindTimer = 0; editFindRun(true); }, 120);
|
||||
});
|
||||
editFindInput.addEventListener('keydown', function(e) {
|
||||
if (e.key === 'Enter') { e.preventDefault(); editFindStep(e.shiftKey); }
|
||||
else if (e.key === 'Escape') { e.preventDefault(); closeEditFind(); }
|
||||
});
|
||||
editFindPrev.addEventListener('click', function() { editFindStep(true); });
|
||||
editFindNextBtn.addEventListener('click', function() { editFindStep(false); });
|
||||
editFindClose.addEventListener('click', closeEditFind);
|
||||
editReplaceOne.addEventListener('click', editFindReplaceOne);
|
||||
editReplaceAll.addEventListener('click', editFindReplaceAll);
|
||||
editReplaceInput.addEventListener('keydown', function(e) {
|
||||
if (e.key === 'Escape') { e.preventDefault(); closeEditFind(); }
|
||||
});
|
||||
|
||||
btnToggle.addEventListener('click', function() {
|
||||
window.__mdPreviewToggleEdit();
|
||||
@@ -1007,12 +1171,17 @@
|
||||
return;
|
||||
}
|
||||
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();
|
||||
if (inEdit()) { openEditFind(false); return; }
|
||||
var seed = (document.body.classList.contains('finding') && document.activeElement === findInput) ? null : selectionSeed();
|
||||
showFind(seed ? seed.text : '', seed ? seed.top : null);
|
||||
return;
|
||||
}
|
||||
if ((e.metaKey || e.ctrlKey) && (e.key === 'h' || e.key === 'H') && inEdit()) {
|
||||
e.preventDefault();
|
||||
openEditFind(true);
|
||||
return;
|
||||
}
|
||||
if ((e.metaKey || e.ctrlKey) && (e.key === 'e' || e.key === 'E')) {
|
||||
e.preventDefault();
|
||||
if (inEdit()) leaveEdit(); else enterEdit();
|
||||
@@ -1029,7 +1198,6 @@
|
||||
return;
|
||||
}
|
||||
if (e.key === 'Escape' && document.body.classList.contains('finding')) { hideFind(); return; }
|
||||
if (e.key === 'Escape' && inEdit()) { leaveEdit(); }
|
||||
});
|
||||
|
||||
// Called by Rust after a save (only preview is refreshed) or after an
|
||||
@@ -1125,6 +1293,7 @@
|
||||
requestTabAction('activate', tab.getAttribute('data-tab-id'));
|
||||
});
|
||||
window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio) {
|
||||
if (editFinding) closeEditFind();
|
||||
document.body.classList.remove('home');
|
||||
document.body.classList.remove('missing');
|
||||
hideFind();
|
||||
@@ -1140,6 +1309,7 @@
|
||||
restoreScrollProgress(typeof scrollRatio === 'number' ? scrollRatio : 0);
|
||||
};
|
||||
window.__setEmptyPreview = function(previewHtml) {
|
||||
if (editFinding) closeEditFind();
|
||||
document.body.classList.add('home');
|
||||
document.body.classList.remove('missing');
|
||||
setEditMode(false);
|
||||
@@ -1154,6 +1324,7 @@
|
||||
window.scrollTo(0, 0);
|
||||
};
|
||||
window.__setMissing = function(previewHtml) {
|
||||
if (editFinding) closeEditFind();
|
||||
document.body.classList.remove('home');
|
||||
document.body.classList.add('missing');
|
||||
setEditMode(false);
|
||||
|
||||
Reference in New Issue
Block a user