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