export function render(buffer, cursor, selection) {
const range = selectedRange(selection);
const html = buffer.map((line, i) => {
const chars = [];
for (let c = 0; c <= line.length; c++) {
if (i === cursor.line && c === cursor.col)
chars.push(``);
if (c < line.length) {
const inSel = range &&
(i > range.start.line || (i === range.start.line && c >= range.start.col)) &&
(i < range.end.line || (i === range.end.line && c < range.end.col));
const ch = escapeChar(line[c]);
chars.push(inSel ? `${ch}` : ch);
}
}
if (i === cursor.line && cursor.col >= line.length)
chars.push(``);
return `${chars.join("")}`;
});
document.getElementById("text").innerHTML = html.join("
");
}
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 } };
}
function escapeChar(c) {
if (c === "&") return "&";
if (c === "<") return "<";
if (c === ">") return ">";
return c;
}