fix(editor): preserve BOM, load tabs reliably and cache fence state
This commit is contained in:
@@ -384,7 +384,7 @@ body.editing #app { max-width: none; padding: 0; }
|
||||
#cm-editor { display: none; }
|
||||
body.editing.cm-active { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
|
||||
body.editing.cm-active .tabbar { flex: none; }
|
||||
body.editing.cm-active #app { flex: 1; min-height: 0; width: 100%; max-width: none; padding: 0; overflow: hidden; }
|
||||
body.editing.cm-active #app { flex: 1; min-height: 0; width: 100%; max-width: none; padding: 0; overflow: hidden; scrollbar-gutter: auto; }
|
||||
body.editing.cm-active #editor { display: none; }
|
||||
body.editing.cm-active #cm-editor { display: block; height: 100%; padding: 10px 0 0 14px; box-sizing: border-box; }
|
||||
#cm-editor .CodeMirror { height: 100%; background: transparent; color: inherit; font: 14px/1.6 "SF Mono","Menlo","Consolas",monospace; }
|
||||
@@ -435,6 +435,7 @@ body.editing.cm-active #cm-editor { display: block; height: 100%; padding: 10px
|
||||
|
||||
@media print {
|
||||
#cm-editor { display: none !important; }
|
||||
.findbar { display: none !important; }
|
||||
html, body { height: auto !important; overflow: visible !important; display: block !important; }
|
||||
#app { flex: none !important; height: auto !important; min-height: 0 !important; overflow: visible !important; scrollbar-gutter: auto; }
|
||||
}
|
||||
|
||||
@@ -86,6 +86,8 @@
|
||||
var applyingRemote = false;
|
||||
var pendingScrollRatio = 0;
|
||||
var cmTabLines = {};
|
||||
var fileHasBom = false;
|
||||
var pendingEditFind = null;
|
||||
// All source-text access goes through this adapter. The CodeMirror
|
||||
// implementation replaces the returned object; callers never touch ta.
|
||||
var ed = createTextareaEditor(ta);
|
||||
@@ -306,10 +308,13 @@
|
||||
clearTimeout(pendingAutosaveTimer);
|
||||
pendingAutosaveTimer = 0;
|
||||
}
|
||||
function fileContentForSave() {
|
||||
return (fileHasBom ? '\uFEFF' : '') + ed.value();
|
||||
}
|
||||
function save() {
|
||||
cancelPendingAutosave();
|
||||
if (!dirty) return;
|
||||
window.ipc.postMessage('save:' + ed.value());
|
||||
window.ipc.postMessage('save:' + fileContentForSave());
|
||||
}
|
||||
function scheduleAutosave() {
|
||||
cancelPendingAutosave();
|
||||
@@ -328,7 +333,7 @@
|
||||
if (saveBlocked) {
|
||||
if (!window.confirm(window.__mdPreviewCannotSave || 'Discard unsaved changes?')) return;
|
||||
} else {
|
||||
message += '\n' + ed.value();
|
||||
message += '\n' + fileContentForSave();
|
||||
}
|
||||
}
|
||||
window.ipc.postMessage(message);
|
||||
@@ -741,7 +746,10 @@
|
||||
return;
|
||||
}
|
||||
ed = createCodeMirrorEditor(cm);
|
||||
cm.on('change', function() {
|
||||
cm.on('change', function(change) {
|
||||
if (cm.__mdpFenceCache) {
|
||||
cm.__mdpFenceCache.validTo = Math.min(cm.__mdpFenceCache.validTo, change.from.line);
|
||||
}
|
||||
if (applyingRemote) return;
|
||||
setDirty(true);
|
||||
scheduleAutosave();
|
||||
@@ -749,6 +757,11 @@
|
||||
});
|
||||
cm.on('scroll', reportScroll);
|
||||
updateEditorZoom();
|
||||
if (pendingEditFind !== null) {
|
||||
var pendingMode = pendingEditFind;
|
||||
pendingEditFind = null;
|
||||
openEditFind(pendingMode);
|
||||
}
|
||||
document.body.classList.toggle('cm-active', inEdit());
|
||||
document.documentElement.classList.toggle('cm-editing', inEdit());
|
||||
if (inEdit()) {
|
||||
@@ -759,6 +772,7 @@
|
||||
function createCodeMirrorEditor(instance) {
|
||||
function setValue(text) {
|
||||
applyingRemote = true;
|
||||
instance.__mdpFenceCache = null;
|
||||
try { instance.setValue(text); } finally { applyingRemote = false; }
|
||||
}
|
||||
return {
|
||||
@@ -785,23 +799,40 @@
|
||||
bind: function() {}
|
||||
};
|
||||
}
|
||||
// Fence state is cached per document and extended on demand: foldgutter
|
||||
// asks for every visible line, so rescanning from the top each time would be
|
||||
// O(n^2) on long files.
|
||||
function fenceCharAt(instance, lineNo) {
|
||||
var cache = instance.__mdpFenceCache;
|
||||
if (!cache) {
|
||||
cache = { validTo: 0, states: [null] };
|
||||
instance.__mdpFenceCache = cache;
|
||||
}
|
||||
while (cache.validTo < lineNo) {
|
||||
var state = cache.states[cache.validTo];
|
||||
var text = instance.getLine(cache.validTo) || '';
|
||||
var match = /^\s*(```+|~~~+)/.exec(text);
|
||||
var next = state;
|
||||
if (match) {
|
||||
var ch = match[1].charAt(0);
|
||||
if (!state) next = ch;
|
||||
else if (state === ch) next = null;
|
||||
}
|
||||
cache.validTo += 1;
|
||||
cache.states[cache.validTo] = next;
|
||||
}
|
||||
return cache.states[lineNo] || null;
|
||||
}
|
||||
function markdownFoldRange(instance, line) {
|
||||
// CodeMirror passes a Pos here; accept a plain line number too.
|
||||
var lineNo = typeof line === 'number' ? line : line.line;
|
||||
var text = instance.getLine(lineNo);
|
||||
if (!text) return null;
|
||||
// Lines inside a fenced block (including its closing fence) only fold as
|
||||
// part of that block.
|
||||
if (fenceCharAt(instance, lineNo)) 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) {
|
||||
@@ -828,6 +859,8 @@
|
||||
var endLine = lineNo;
|
||||
for (var i = lineNo + 1; i <= instance.lastLine(); i += 1) {
|
||||
var bodyLine = instance.getLine(i) || '';
|
||||
if (fenceCharAt(instance, i)) { endLine = i; continue; }
|
||||
if (/^\s*(```+|~~~+)/.test(bodyLine)) { endLine = i; continue; }
|
||||
var nextHeading = /^(#{1,6})\s/.exec(bodyLine);
|
||||
var nextLevel = nextHeading ? nextHeading[1].length : 0;
|
||||
if (!nextHeading && bodyLine.trim()) {
|
||||
@@ -874,7 +907,11 @@
|
||||
}
|
||||
}
|
||||
function openEditFind(replaceMode) {
|
||||
if (!cm || !inEdit()) return;
|
||||
if (!inEdit()) return;
|
||||
if (!cm) {
|
||||
if (cmLoading) pendingEditFind = replaceMode;
|
||||
return;
|
||||
}
|
||||
editReplaceGroup.hidden = !replaceMode;
|
||||
editFindVisible(true);
|
||||
var selected = cm.getSelection();
|
||||
@@ -967,6 +1004,7 @@
|
||||
}
|
||||
// Edit mode is per tab and owned by Rust; this only paints it.
|
||||
function setEditMode(on) {
|
||||
if (!on) pendingEditFind = null;
|
||||
if (!on && editFinding) closeEditFind();
|
||||
if (on && !cm && !cmDisabled) requestEditorSource();
|
||||
if (on) {
|
||||
@@ -991,6 +1029,7 @@
|
||||
}
|
||||
function enterEdit() {
|
||||
if (document.body.classList.contains('home')) return;
|
||||
if (document.body.classList.contains('finding')) hideFind();
|
||||
var progress = currentScrollProgress();
|
||||
pendingScrollRatio = progress;
|
||||
setEditMode(true);
|
||||
@@ -1270,7 +1309,8 @@
|
||||
else base.removeAttribute('href');
|
||||
};
|
||||
window.__markSaved = function(savedRaw) {
|
||||
if (typeof savedRaw === 'string' && ed.value() !== savedRaw) {
|
||||
var savedText = typeof savedRaw === 'string' ? savedRaw.replace(/^\uFEFF/, '') : savedRaw;
|
||||
if (typeof savedText === 'string' && ed.value().replace(/^\uFEFF/, '') !== savedText) {
|
||||
window.ipc.postMessage('dirty:1');
|
||||
return;
|
||||
}
|
||||
@@ -1346,6 +1386,7 @@
|
||||
});
|
||||
window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio, tabId) {
|
||||
var previousTabId = activeTabId;
|
||||
var switched = typeof tabId === 'number' && tabId !== previousTabId;
|
||||
if (cm && inEdit() && previousTabId) cmTabLines[previousTabId] = cmTopLine();
|
||||
if (typeof tabId === 'number' && tabId) activeTabId = tabId;
|
||||
if (editFinding) closeEditFind();
|
||||
@@ -1354,10 +1395,13 @@
|
||||
hideFind();
|
||||
window.__setBaseHref(baseHref);
|
||||
window.__setPreview(previewHtml, needsMath, needsMermaid);
|
||||
if (!inEdit() || !dirty) {
|
||||
if (switched || !inEdit() || !dirty) {
|
||||
autosavePaused = false;
|
||||
ed.setValue(rawMd);
|
||||
updateDocumentStats(rawMd);
|
||||
var loaded = typeof rawMd === 'string' ? rawMd : '';
|
||||
fileHasBom = loaded.charAt(0) === '\uFEFF';
|
||||
var editorText = fileHasBom ? loaded.slice(1) : loaded;
|
||||
ed.setValue(editorText);
|
||||
updateDocumentStats(editorText);
|
||||
setDirty(false);
|
||||
}
|
||||
setEditMode(!!editing);
|
||||
@@ -1371,6 +1415,7 @@
|
||||
cancelPendingAutosave();
|
||||
autosavePaused = false;
|
||||
hideFind();
|
||||
fileHasBom = false;
|
||||
window.__setBaseHref('');
|
||||
document.getElementById('preview').innerHTML = previewHtml;
|
||||
ed.setValue('');
|
||||
@@ -1386,6 +1431,7 @@
|
||||
cancelPendingAutosave();
|
||||
autosavePaused = false;
|
||||
hideFind();
|
||||
fileHasBom = false;
|
||||
window.__setBaseHref('');
|
||||
document.getElementById('preview').innerHTML = previewHtml;
|
||||
ed.setValue('');
|
||||
|
||||
@@ -251,6 +251,12 @@ mod tests {
|
||||
assert!(page.contains("CodeMirror-linenumbers"));
|
||||
assert!(page.contains("rangeFinder: markdownFoldRange"));
|
||||
assert!(page.contains("body.finding .findbar:not(#edit-findbar)"));
|
||||
assert!(page.contains("var fileHasBom = false"));
|
||||
assert!(page.contains("function fileContentForSave()"));
|
||||
assert!(page.contains("pendingEditFind"));
|
||||
assert!(page.contains("var switched ="));
|
||||
assert!(page.contains("function fenceCharAt"));
|
||||
assert!(page.contains("__mdpFenceCache"));
|
||||
assert!(page.contains("specialChars: /["));
|
||||
assert!(!page.contains("\\ufeff"));
|
||||
assert!(page.contains("openEditFind(false)"));
|
||||
@@ -288,7 +294,6 @@ mod tests {
|
||||
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