feat: add tab and auto-indent to the editor
Tab inserts two spaces, Shift+Tab outdents selected lines, and Enter carries the current line's leading whitespace. Native textarea undo is preserved and IME composition is left alone.
This commit is contained in:
124
src/main.rs
124
src/main.rs
@@ -1552,6 +1552,10 @@ body.editing #app {{ max-width: none; padding: 0; }}
|
|||||||
var composingFind = false;
|
var composingFind = false;
|
||||||
var pendingFindTimer = 0;
|
var pendingFindTimer = 0;
|
||||||
var FIND_DEBOUNCE_MS = 300;
|
var FIND_DEBOUNCE_MS = 300;
|
||||||
|
// Tab inserts spaces, never a literal tab. Two spaces matches the
|
||||||
|
// common Markdown/AI-generated document style and keeps list nesting
|
||||||
|
// compact; change this single constant to switch the width.
|
||||||
|
var INDENT = ' ';
|
||||||
// The strip stays hidden while the only tab is Home, matching the
|
// The strip stays hidden while the only tab is Home, matching the
|
||||||
// original start-up page. Set to false to keep the tab bar always on.
|
// original start-up page. Set to false to keep the tab bar always on.
|
||||||
var HIDE_SINGLE_HOME_TAB = true;
|
var HIDE_SINGLE_HOME_TAB = true;
|
||||||
@@ -1887,6 +1891,126 @@ body.editing #app {{ max-width: none; padding: 0; }}
|
|||||||
ta.style.height = h + 'px';
|
ta.style.height = h + 'px';
|
||||||
window.scrollTo(x, y);
|
window.scrollTo(x, y);
|
||||||
}}
|
}}
|
||||||
|
// ---- Source editor commands -------------------------------------------------
|
||||||
|
function editorLineStart(value, pos) {{
|
||||||
|
return value.lastIndexOf('\n', Math.max(0, pos - 1)) + 1;
|
||||||
|
}}
|
||||||
|
function editorLineEnd(value, pos) {{
|
||||||
|
var end = value.indexOf('\n', pos);
|
||||||
|
return end < 0 ? value.length : end;
|
||||||
|
}}
|
||||||
|
function replaceEditorRange(text, start, end, selectionStart, selectionEnd) {{
|
||||||
|
try {{
|
||||||
|
ta.focus({{ preventScroll: true }});
|
||||||
|
}} catch (_) {{
|
||||||
|
ta.focus();
|
||||||
|
}}
|
||||||
|
ta.setSelectionRange(start, end);
|
||||||
|
var inserted = false;
|
||||||
|
try {{
|
||||||
|
if (text === '') {{
|
||||||
|
inserted = document.execCommand('delete', false);
|
||||||
|
}} else {{
|
||||||
|
inserted = document.execCommand('insertText', false, text);
|
||||||
|
}}
|
||||||
|
}} catch (_) {{
|
||||||
|
inserted = false;
|
||||||
|
}}
|
||||||
|
if (!inserted) {{
|
||||||
|
ta.setRangeText(text, start, end, 'end');
|
||||||
|
ta.dispatchEvent(new Event('input', {{ bubbles: true }}));
|
||||||
|
}}
|
||||||
|
if (typeof selectionStart === 'number' && typeof selectionEnd === 'number') {{
|
||||||
|
try {{ ta.setSelectionRange(selectionStart, selectionEnd); }} catch (_) {{}}
|
||||||
|
}}
|
||||||
|
setDirty(true);
|
||||||
|
scheduleAutosave();
|
||||||
|
autoResize();
|
||||||
|
updateDocumentStats(ta.value);
|
||||||
|
}}
|
||||||
|
function transformSelectedEditorLines(transform) {{
|
||||||
|
var value = ta.value;
|
||||||
|
var start = ta.selectionStart;
|
||||||
|
var end = ta.selectionEnd;
|
||||||
|
if (start === end) return false;
|
||||||
|
var from = editorLineStart(value, start);
|
||||||
|
var last = end - 1;
|
||||||
|
if (last > start && value.charAt(last) === '\n') last -= 1;
|
||||||
|
var to = editorLineEnd(value, last);
|
||||||
|
var block = value.slice(from, to);
|
||||||
|
var lines = block.split('\n');
|
||||||
|
var transformed = lines.map(transform).join('\n');
|
||||||
|
if (transformed !== block) {{
|
||||||
|
replaceEditorRange(transformed, from, to, from, from + transformed.length);
|
||||||
|
}}
|
||||||
|
return true;
|
||||||
|
}}
|
||||||
|
function indentSelectedEditorLines() {{
|
||||||
|
transformSelectedEditorLines(function(line) {{
|
||||||
|
return INDENT + line;
|
||||||
|
}});
|
||||||
|
}}
|
||||||
|
function outdentSelectedEditorLines() {{
|
||||||
|
transformSelectedEditorLines(function(line) {{
|
||||||
|
if (line.charAt(0) === '\t') return line.slice(1);
|
||||||
|
var removed = 0;
|
||||||
|
while (removed < INDENT.length && line.charAt(removed) === ' ') removed += 1;
|
||||||
|
return line.slice(removed);
|
||||||
|
}});
|
||||||
|
}}
|
||||||
|
function indentEditor() {{
|
||||||
|
var start = ta.selectionStart;
|
||||||
|
var end = ta.selectionEnd;
|
||||||
|
if (start === end) {{
|
||||||
|
replaceEditorRange(INDENT, start, end, start + INDENT.length, start + INDENT.length);
|
||||||
|
return;
|
||||||
|
}}
|
||||||
|
indentSelectedEditorLines();
|
||||||
|
}}
|
||||||
|
function outdentEditor() {{
|
||||||
|
var value = ta.value;
|
||||||
|
var start = ta.selectionStart;
|
||||||
|
var end = ta.selectionEnd;
|
||||||
|
if (start !== end) {{
|
||||||
|
outdentSelectedEditorLines();
|
||||||
|
return;
|
||||||
|
}}
|
||||||
|
var from = editorLineStart(value, start);
|
||||||
|
var line = value.slice(from, editorLineEnd(value, start));
|
||||||
|
var removed = 0;
|
||||||
|
if (line.charAt(0) === '\t') {{
|
||||||
|
removed = 1;
|
||||||
|
}} else {{
|
||||||
|
while (removed < INDENT.length && line.charAt(removed) === ' ') removed += 1;
|
||||||
|
}}
|
||||||
|
if (!removed) return;
|
||||||
|
var next = Math.max(from, start - removed);
|
||||||
|
replaceEditorRange('', from, from + removed, next, next);
|
||||||
|
}}
|
||||||
|
function insertNewlineWithIndent() {{
|
||||||
|
var value = ta.value;
|
||||||
|
var start = ta.selectionStart;
|
||||||
|
var end = ta.selectionEnd;
|
||||||
|
var from = editorLineStart(value, start);
|
||||||
|
var prefix = value.slice(from, start);
|
||||||
|
var leading = /^[ \t]*/.exec(prefix)[0];
|
||||||
|
if (!/\S/.test(prefix)) leading = '';
|
||||||
|
var text = '\n' + leading;
|
||||||
|
var next = start + text.length;
|
||||||
|
replaceEditorRange(text, start, end, next, next);
|
||||||
|
}}
|
||||||
|
ta.addEventListener('keydown', function(e) {{
|
||||||
|
if (e.isComposing || e.keyCode === 229) return;
|
||||||
|
if (e.key === 'Tab' && !e.ctrlKey && !e.metaKey && !e.altKey) {{
|
||||||
|
e.preventDefault();
|
||||||
|
if (e.shiftKey) outdentEditor(); else indentEditor();
|
||||||
|
return;
|
||||||
|
}}
|
||||||
|
if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey && !e.metaKey && !e.altKey) {{
|
||||||
|
e.preventDefault();
|
||||||
|
insertNewlineWithIndent();
|
||||||
|
}}
|
||||||
|
}});
|
||||||
function enterEdit() {{
|
function enterEdit() {{
|
||||||
if (document.body.classList.contains('home')) return;
|
if (document.body.classList.contains('home')) return;
|
||||||
var progress = currentScrollProgress();
|
var progress = currentScrollProgress();
|
||||||
|
|||||||
Reference in New Issue
Block a user