refactor(page): route editor access through an adapter
This commit is contained in:
@@ -61,6 +61,10 @@
|
||||
var ZOOM_WHEEL_THROTTLE_MS = 90;
|
||||
var zoomWheelAt = 0;
|
||||
var zoomPercent = 100;
|
||||
// All source-text access goes through this adapter. The CodeMirror
|
||||
// implementation replaces the returned object; callers never touch ta.
|
||||
var ed = createTextareaEditor(ta);
|
||||
ed.bind();
|
||||
|
||||
btnToggle.innerHTML = ICON_EDIT;
|
||||
btnZoom.innerHTML = ICON_ZOOM;
|
||||
@@ -97,7 +101,7 @@
|
||||
document.documentElement.style.setProperty('--content-scale', String(zoomPercent / 100));
|
||||
btnZoomReset.textContent = zoomPercent + '%';
|
||||
if (persist) queueZoomSave();
|
||||
if (inEdit()) autoResize();
|
||||
if (inEdit()) ed.resize();
|
||||
}
|
||||
function changeZoom(delta) {
|
||||
applyZoom(zoomPercent + delta, true);
|
||||
@@ -141,7 +145,7 @@
|
||||
window.addEventListener('scroll', reportScroll, { passive: true });
|
||||
applyZoom(100, false);
|
||||
window.__mdPreviewSetZoom = function(percent) { applyZoom(percent, false); };
|
||||
updateDocumentStats(ta.value);
|
||||
updateDocumentStats(ed.value());
|
||||
function shortenPath(path, max) {
|
||||
if (!path || path.length <= max) return path || '';
|
||||
var head = Math.ceil((max - 3) / 2);
|
||||
@@ -208,7 +212,7 @@
|
||||
return;
|
||||
}
|
||||
hideTabMenu();
|
||||
if (!inEdit() || e.target !== ta) e.preventDefault();
|
||||
if (!inEdit() || !ed.isTarget(e.target)) e.preventDefault();
|
||||
});
|
||||
function setDirty(d) {
|
||||
if (dirty === d) return;
|
||||
@@ -223,7 +227,7 @@
|
||||
function save() {
|
||||
cancelPendingAutosave();
|
||||
if (!dirty) return;
|
||||
window.ipc.postMessage('save:' + ta.value);
|
||||
window.ipc.postMessage('save:' + ed.value());
|
||||
}
|
||||
function scheduleAutosave() {
|
||||
cancelPendingAutosave();
|
||||
@@ -241,7 +245,7 @@
|
||||
if (saveBlocked) {
|
||||
if (!window.confirm(window.__mdPreviewCannotSave || 'Discard unsaved changes?')) return;
|
||||
} else {
|
||||
message += '\n' + ta.value;
|
||||
message += '\n' + ed.value();
|
||||
}
|
||||
}
|
||||
window.ipc.postMessage(message);
|
||||
@@ -552,7 +556,7 @@
|
||||
var next = start + text.length;
|
||||
replaceEditorRange(text, start, end, next, next);
|
||||
}
|
||||
ta.addEventListener('keydown', function(e) {
|
||||
function editorKeydown(e) {
|
||||
if (e.isComposing || e.keyCode === 229) return;
|
||||
if (e.key === 'Tab' && !e.ctrlKey && !e.metaKey && !e.altKey) {
|
||||
e.preventDefault();
|
||||
@@ -563,7 +567,28 @@
|
||||
e.preventDefault();
|
||||
insertNewlineWithIndent();
|
||||
}
|
||||
});
|
||||
}
|
||||
function createTextareaEditor(ta) {
|
||||
return {
|
||||
value: function() { return ta.value; },
|
||||
setValue: function(text) { ta.value = text; },
|
||||
focus: function() {
|
||||
try { ta.focus({ preventScroll: true }); } catch (_) { ta.focus(); }
|
||||
},
|
||||
selection: function() { return { start: ta.selectionStart, end: ta.selectionEnd }; },
|
||||
setSelection: function(start, end) { try { ta.setSelectionRange(start, end); } catch (_) {} },
|
||||
replaceRange: replaceEditorRange,
|
||||
indent: indentEditor,
|
||||
outdent: outdentEditor,
|
||||
newlineWithIndent: insertNewlineWithIndent,
|
||||
resize: autoResize,
|
||||
isTarget: function(node) { return node === ta; },
|
||||
bind: function() {
|
||||
ta.addEventListener('keydown', editorKeydown);
|
||||
ta.addEventListener('input', editorInput);
|
||||
}
|
||||
};
|
||||
}
|
||||
// Edit mode is per tab and owned by Rust; this only paints it.
|
||||
function setEditMode(on) {
|
||||
if (on) {
|
||||
@@ -577,17 +602,13 @@
|
||||
btnToggle.title = L_EDIT;
|
||||
btnToggle.setAttribute('aria-label', L_EDIT);
|
||||
}
|
||||
autoResize();
|
||||
ed.resize();
|
||||
}
|
||||
function enterEdit() {
|
||||
if (document.body.classList.contains('home')) return;
|
||||
var progress = currentScrollProgress();
|
||||
setEditMode(true);
|
||||
try {
|
||||
ta.focus({ preventScroll: true });
|
||||
} catch (_) {
|
||||
ta.focus();
|
||||
}
|
||||
ed.focus();
|
||||
restoreScrollProgress(progress);
|
||||
if (window.ipc) window.ipc.postMessage('edit-mode:' + activeTabId + ':1');
|
||||
}
|
||||
@@ -744,8 +765,8 @@
|
||||
changeZoom(e.deltaY < 0 ? ZOOM_STEP : -ZOOM_STEP);
|
||||
}
|
||||
window.addEventListener('wheel', onContentWheel, { passive: false });
|
||||
ta.addEventListener('input', function() { setDirty(true); scheduleAutosave(); autoResize(); updateDocumentStats(ta.value); });
|
||||
window.addEventListener('resize', function() { if (inEdit()) autoResize(); });
|
||||
function editorInput() { setDirty(true); scheduleAutosave(); autoResize(); updateDocumentStats(ta.value); }
|
||||
window.addEventListener('resize', function() { if (inEdit()) ed.resize(); });
|
||||
document.addEventListener('click', function(e) {
|
||||
if (!zoomControl.contains(e.target)) zoomControl.classList.remove('open');
|
||||
});
|
||||
@@ -841,7 +862,7 @@
|
||||
else base.removeAttribute('href');
|
||||
};
|
||||
window.__markSaved = function(savedRaw) {
|
||||
if (typeof savedRaw === 'string' && ta.value !== savedRaw) {
|
||||
if (typeof savedRaw === 'string' && ed.value() !== savedRaw) {
|
||||
window.ipc.postMessage('dirty:1');
|
||||
return;
|
||||
}
|
||||
@@ -918,7 +939,7 @@
|
||||
window.__setPreview(previewHtml, needsMath, needsMermaid);
|
||||
if (!inEdit() || !dirty) {
|
||||
autosavePaused = false;
|
||||
ta.value = rawMd;
|
||||
ed.setValue(rawMd);
|
||||
updateDocumentStats(rawMd);
|
||||
setDirty(false);
|
||||
}
|
||||
@@ -934,7 +955,7 @@
|
||||
hideFind();
|
||||
window.__setBaseHref('');
|
||||
document.getElementById('preview').innerHTML = previewHtml;
|
||||
ta.value = '';
|
||||
ed.setValue('');
|
||||
updateDocumentStats('');
|
||||
setDirty(false);
|
||||
window.scrollTo(0, 0);
|
||||
@@ -948,7 +969,7 @@
|
||||
hideFind();
|
||||
window.__setBaseHref('');
|
||||
document.getElementById('preview').innerHTML = previewHtml;
|
||||
ta.value = '';
|
||||
ed.setValue('');
|
||||
updateDocumentStats('');
|
||||
setDirty(false);
|
||||
window.scrollTo(0, 0);
|
||||
|
||||
Reference in New Issue
Block a user