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; }
|
#cm-editor { display: none; }
|
||||||
body.editing.cm-active { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
|
body.editing.cm-active { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
|
||||||
body.editing.cm-active .tabbar { flex: none; }
|
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 #editor { display: none; }
|
||||||
body.editing.cm-active #cm-editor { display: block; height: 100%; padding: 10px 0 0 14px; box-sizing: border-box; }
|
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; }
|
#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 {
|
@media print {
|
||||||
#cm-editor { display: none !important; }
|
#cm-editor { display: none !important; }
|
||||||
|
.findbar { display: none !important; }
|
||||||
html, body { height: auto !important; overflow: visible !important; display: block !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; }
|
#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 applyingRemote = false;
|
||||||
var pendingScrollRatio = 0;
|
var pendingScrollRatio = 0;
|
||||||
var cmTabLines = {};
|
var cmTabLines = {};
|
||||||
|
var fileHasBom = false;
|
||||||
|
var pendingEditFind = null;
|
||||||
// All source-text access goes through this adapter. The CodeMirror
|
// All source-text access goes through this adapter. The CodeMirror
|
||||||
// implementation replaces the returned object; callers never touch ta.
|
// implementation replaces the returned object; callers never touch ta.
|
||||||
var ed = createTextareaEditor(ta);
|
var ed = createTextareaEditor(ta);
|
||||||
@@ -306,10 +308,13 @@
|
|||||||
clearTimeout(pendingAutosaveTimer);
|
clearTimeout(pendingAutosaveTimer);
|
||||||
pendingAutosaveTimer = 0;
|
pendingAutosaveTimer = 0;
|
||||||
}
|
}
|
||||||
|
function fileContentForSave() {
|
||||||
|
return (fileHasBom ? '\uFEFF' : '') + ed.value();
|
||||||
|
}
|
||||||
function save() {
|
function save() {
|
||||||
cancelPendingAutosave();
|
cancelPendingAutosave();
|
||||||
if (!dirty) return;
|
if (!dirty) return;
|
||||||
window.ipc.postMessage('save:' + ed.value());
|
window.ipc.postMessage('save:' + fileContentForSave());
|
||||||
}
|
}
|
||||||
function scheduleAutosave() {
|
function scheduleAutosave() {
|
||||||
cancelPendingAutosave();
|
cancelPendingAutosave();
|
||||||
@@ -328,7 +333,7 @@
|
|||||||
if (saveBlocked) {
|
if (saveBlocked) {
|
||||||
if (!window.confirm(window.__mdPreviewCannotSave || 'Discard unsaved changes?')) return;
|
if (!window.confirm(window.__mdPreviewCannotSave || 'Discard unsaved changes?')) return;
|
||||||
} else {
|
} else {
|
||||||
message += '\n' + ed.value();
|
message += '\n' + fileContentForSave();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window.ipc.postMessage(message);
|
window.ipc.postMessage(message);
|
||||||
@@ -741,7 +746,10 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ed = createCodeMirrorEditor(cm);
|
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;
|
if (applyingRemote) return;
|
||||||
setDirty(true);
|
setDirty(true);
|
||||||
scheduleAutosave();
|
scheduleAutosave();
|
||||||
@@ -749,6 +757,11 @@
|
|||||||
});
|
});
|
||||||
cm.on('scroll', reportScroll);
|
cm.on('scroll', reportScroll);
|
||||||
updateEditorZoom();
|
updateEditorZoom();
|
||||||
|
if (pendingEditFind !== null) {
|
||||||
|
var pendingMode = pendingEditFind;
|
||||||
|
pendingEditFind = null;
|
||||||
|
openEditFind(pendingMode);
|
||||||
|
}
|
||||||
document.body.classList.toggle('cm-active', inEdit());
|
document.body.classList.toggle('cm-active', inEdit());
|
||||||
document.documentElement.classList.toggle('cm-editing', inEdit());
|
document.documentElement.classList.toggle('cm-editing', inEdit());
|
||||||
if (inEdit()) {
|
if (inEdit()) {
|
||||||
@@ -759,6 +772,7 @@
|
|||||||
function createCodeMirrorEditor(instance) {
|
function createCodeMirrorEditor(instance) {
|
||||||
function setValue(text) {
|
function setValue(text) {
|
||||||
applyingRemote = true;
|
applyingRemote = true;
|
||||||
|
instance.__mdpFenceCache = null;
|
||||||
try { instance.setValue(text); } finally { applyingRemote = false; }
|
try { instance.setValue(text); } finally { applyingRemote = false; }
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
@@ -785,23 +799,40 @@
|
|||||||
bind: function() {}
|
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) {
|
function markdownFoldRange(instance, line) {
|
||||||
// CodeMirror passes a Pos here; accept a plain line number too.
|
// CodeMirror passes a Pos here; accept a plain line number too.
|
||||||
var lineNo = typeof line === 'number' ? line : line.line;
|
var lineNo = typeof line === 'number' ? line : line.line;
|
||||||
var text = instance.getLine(lineNo);
|
var text = instance.getLine(lineNo);
|
||||||
if (!text) return null;
|
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);
|
var fence = /^\s*(```+|~~~+)/.exec(text);
|
||||||
if (fence) {
|
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 marker = fence[1];
|
||||||
var fenceEnd = -1;
|
var fenceEnd = -1;
|
||||||
for (var f = lineNo + 1; f <= instance.lastLine(); f += 1) {
|
for (var f = lineNo + 1; f <= instance.lastLine(); f += 1) {
|
||||||
@@ -828,6 +859,8 @@
|
|||||||
var endLine = lineNo;
|
var endLine = lineNo;
|
||||||
for (var i = lineNo + 1; i <= instance.lastLine(); i += 1) {
|
for (var i = lineNo + 1; i <= instance.lastLine(); i += 1) {
|
||||||
var bodyLine = instance.getLine(i) || '';
|
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 nextHeading = /^(#{1,6})\s/.exec(bodyLine);
|
||||||
var nextLevel = nextHeading ? nextHeading[1].length : 0;
|
var nextLevel = nextHeading ? nextHeading[1].length : 0;
|
||||||
if (!nextHeading && bodyLine.trim()) {
|
if (!nextHeading && bodyLine.trim()) {
|
||||||
@@ -874,7 +907,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
function openEditFind(replaceMode) {
|
function openEditFind(replaceMode) {
|
||||||
if (!cm || !inEdit()) return;
|
if (!inEdit()) return;
|
||||||
|
if (!cm) {
|
||||||
|
if (cmLoading) pendingEditFind = replaceMode;
|
||||||
|
return;
|
||||||
|
}
|
||||||
editReplaceGroup.hidden = !replaceMode;
|
editReplaceGroup.hidden = !replaceMode;
|
||||||
editFindVisible(true);
|
editFindVisible(true);
|
||||||
var selected = cm.getSelection();
|
var selected = cm.getSelection();
|
||||||
@@ -967,6 +1004,7 @@
|
|||||||
}
|
}
|
||||||
// Edit mode is per tab and owned by Rust; this only paints it.
|
// Edit mode is per tab and owned by Rust; this only paints it.
|
||||||
function setEditMode(on) {
|
function setEditMode(on) {
|
||||||
|
if (!on) pendingEditFind = null;
|
||||||
if (!on && editFinding) closeEditFind();
|
if (!on && editFinding) closeEditFind();
|
||||||
if (on && !cm && !cmDisabled) requestEditorSource();
|
if (on && !cm && !cmDisabled) requestEditorSource();
|
||||||
if (on) {
|
if (on) {
|
||||||
@@ -991,6 +1029,7 @@
|
|||||||
}
|
}
|
||||||
function enterEdit() {
|
function enterEdit() {
|
||||||
if (document.body.classList.contains('home')) return;
|
if (document.body.classList.contains('home')) return;
|
||||||
|
if (document.body.classList.contains('finding')) hideFind();
|
||||||
var progress = currentScrollProgress();
|
var progress = currentScrollProgress();
|
||||||
pendingScrollRatio = progress;
|
pendingScrollRatio = progress;
|
||||||
setEditMode(true);
|
setEditMode(true);
|
||||||
@@ -1270,7 +1309,8 @@
|
|||||||
else base.removeAttribute('href');
|
else base.removeAttribute('href');
|
||||||
};
|
};
|
||||||
window.__markSaved = function(savedRaw) {
|
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');
|
window.ipc.postMessage('dirty:1');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1346,6 +1386,7 @@
|
|||||||
});
|
});
|
||||||
window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio, tabId) {
|
window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio, tabId) {
|
||||||
var previousTabId = activeTabId;
|
var previousTabId = activeTabId;
|
||||||
|
var switched = typeof tabId === 'number' && tabId !== previousTabId;
|
||||||
if (cm && inEdit() && previousTabId) cmTabLines[previousTabId] = cmTopLine();
|
if (cm && inEdit() && previousTabId) cmTabLines[previousTabId] = cmTopLine();
|
||||||
if (typeof tabId === 'number' && tabId) activeTabId = tabId;
|
if (typeof tabId === 'number' && tabId) activeTabId = tabId;
|
||||||
if (editFinding) closeEditFind();
|
if (editFinding) closeEditFind();
|
||||||
@@ -1354,10 +1395,13 @@
|
|||||||
hideFind();
|
hideFind();
|
||||||
window.__setBaseHref(baseHref);
|
window.__setBaseHref(baseHref);
|
||||||
window.__setPreview(previewHtml, needsMath, needsMermaid);
|
window.__setPreview(previewHtml, needsMath, needsMermaid);
|
||||||
if (!inEdit() || !dirty) {
|
if (switched || !inEdit() || !dirty) {
|
||||||
autosavePaused = false;
|
autosavePaused = false;
|
||||||
ed.setValue(rawMd);
|
var loaded = typeof rawMd === 'string' ? rawMd : '';
|
||||||
updateDocumentStats(rawMd);
|
fileHasBom = loaded.charAt(0) === '\uFEFF';
|
||||||
|
var editorText = fileHasBom ? loaded.slice(1) : loaded;
|
||||||
|
ed.setValue(editorText);
|
||||||
|
updateDocumentStats(editorText);
|
||||||
setDirty(false);
|
setDirty(false);
|
||||||
}
|
}
|
||||||
setEditMode(!!editing);
|
setEditMode(!!editing);
|
||||||
@@ -1371,6 +1415,7 @@
|
|||||||
cancelPendingAutosave();
|
cancelPendingAutosave();
|
||||||
autosavePaused = false;
|
autosavePaused = false;
|
||||||
hideFind();
|
hideFind();
|
||||||
|
fileHasBom = false;
|
||||||
window.__setBaseHref('');
|
window.__setBaseHref('');
|
||||||
document.getElementById('preview').innerHTML = previewHtml;
|
document.getElementById('preview').innerHTML = previewHtml;
|
||||||
ed.setValue('');
|
ed.setValue('');
|
||||||
@@ -1386,6 +1431,7 @@
|
|||||||
cancelPendingAutosave();
|
cancelPendingAutosave();
|
||||||
autosavePaused = false;
|
autosavePaused = false;
|
||||||
hideFind();
|
hideFind();
|
||||||
|
fileHasBom = false;
|
||||||
window.__setBaseHref('');
|
window.__setBaseHref('');
|
||||||
document.getElementById('preview').innerHTML = previewHtml;
|
document.getElementById('preview').innerHTML = previewHtml;
|
||||||
ed.setValue('');
|
ed.setValue('');
|
||||||
|
|||||||
@@ -251,6 +251,12 @@ mod tests {
|
|||||||
assert!(page.contains("CodeMirror-linenumbers"));
|
assert!(page.contains("CodeMirror-linenumbers"));
|
||||||
assert!(page.contains("rangeFinder: markdownFoldRange"));
|
assert!(page.contains("rangeFinder: markdownFoldRange"));
|
||||||
assert!(page.contains("body.finding .findbar:not(#edit-findbar)"));
|
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("specialChars: /["));
|
||||||
assert!(!page.contains("\\ufeff"));
|
assert!(!page.contains("\\ufeff"));
|
||||||
assert!(page.contains("openEditFind(false)"));
|
assert!(page.contains("openEditFind(false)"));
|
||||||
@@ -288,7 +294,6 @@ mod tests {
|
|||||||
assert!(page.contains("width: min(560px, calc(100vw - 32px))"));
|
assert!(page.contains("width: min(560px, calc(100vw - 32px))"));
|
||||||
assert!(page.contains("edit-find-row"));
|
assert!(page.contains("edit-find-row"));
|
||||||
assert!(page.contains("widget: '⋯'"));
|
assert!(page.contains("widget: '⋯'"));
|
||||||
assert!(page.contains("priorFence"));
|
|
||||||
// Escape closes the find bar; it must not leave edit mode anymore.
|
// Escape closes the find bar; it must not leave edit mode anymore.
|
||||||
assert!(!page.contains("if (e.key === 'Escape' && inEdit()) { leaveEdit(); }"));
|
assert!(!page.contains("if (e.key === 'Escape' && inEdit()) { leaveEdit(); }"));
|
||||||
assert!(!page.contains("'Esc': function() { leaveEdit(); }"));
|
assert!(!page.contains("'Esc': function() { leaveEdit(); }"));
|
||||||
|
|||||||
Reference in New Issue
Block a user