Files
2026-06-30 21:31:35 +02:00

39 lines
1.3 KiB
JavaScript

export function hasSelection(selection) {
return selection.anchor !== null &&
(selection.anchor.line !== selection.head.line ||
selection.anchor.col !== selection.head.col);
}
export function selectedRange(selection) {
if (selection.anchor === null || selection.head === null) return null;
const a = selection.anchor;
const h = selection.head;
if (a.line === h.line && a.col === h.col) return null;
if (a.line < h.line || (a.line === h.line && a.col < h.col))
return { start: { ...a }, end: { ...h } };
return { start: { ...h }, end: { ...a } };
}
export function clearSelection(selection) {
selection.anchor = null;
selection.head = null;
}
export function deleteSelection(buffer, cursor, selection) {
const range = selectedRange(selection);
if (!range) return;
const { start, end } = range;
if (start.line === end.line) {
const line = buffer[start.line];
buffer[start.line] = line.slice(0, start.col) + line.slice(end.col);
} else {
const before = buffer[start.line].slice(0, start.col);
const after = buffer[end.line].slice(end.col);
buffer[start.line] = before + after;
buffer.splice(start.line + 1, end.line - start.line);
}
cursor.line = start.line;
cursor.col = start.col;
clearSelection(selection);
}