fix(editor): preserve BOM, load tabs reliably and cache fence state
This commit is contained in:
@@ -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('');
|
||||
|
||||
Reference in New Issue
Block a user