Kalk web special symbol auto-completion

This commit is contained in:
PaddiM8 2021-01-02 13:43:49 +01:00
parent bd2992e16f
commit e19f8b2ff2

View File

@ -3,7 +3,7 @@
function handleKeyDown(event: KeyboardEvent) { function handleKeyDown(event: KeyboardEvent) {
if (event.key == "Enter") { if (event.key == "Enter") {
const target = event.target as HTMLInputElement; const target = event.target as HTMLInputElement;
outputLines = [...outputLines, target.innerText]; outputLines = [...outputLines, target.innerHTML];
target.innerHTML = ""; target.innerHTML = "";
} }
} }
@ -11,8 +11,10 @@
function handleInput(event: Event) { function handleInput(event: Event) {
const target = event.target as HTMLInputElement; const target = event.target as HTMLInputElement;
const cursorPos = getCursorPos(target); const cursorPos = getCursorPos(target);
target.innerHTML = highlight(target.textContent); const [highlighted, offset] = highlight(target.textContent);
setCursorPos(target, cursorPos); console.log(highlighted);
target.innerHTML = highlighted;
setCursorPos(target, cursorPos - offset);
} }
function getCursorPos(element: HTMLInputElement): number { function getCursorPos(element: HTMLInputElement): number {
@ -71,12 +73,46 @@
return textNodes; return textNodes;
} }
function highlight(input: string): string { function highlight(input: string): [string, number] {
let result = input; let result = input;
let offset = 0;
result = result.replaceAll( result = result.replaceAll(
/(?<identifier>[^!-@\s_|^⌊⌋⌈⌉]+(_\d+)?)|(?<op>[+\-/*%^!])/g, /(?<identifier>[^!-@\s_|^⌊⌋⌈⌉]+(_\d+)?)|(?<op>[+\-/*%^!])/g,
(substring, identifier, _, op) => { (substring, identifier, _, op) => {
if (identifier) { if (identifier) {
switch (substring) {
case "sqrt": {
substring = "√";
offset += 3;
break;
}
case "sum": {
substring = "Σ";
offset += 2;
break;
}
case "pi": {
substring = "π";
offset += 1;
break;
}
case "gamma": {
substring = "Γ";
offset += 4;
break;
}
case "floor": {
substring = "⌊⌋";
offset += 3;
break;
}
case "ceil": {
substring = "⌈⌉";
offset += 3;
break;
}
}
return `<span class="identifier">${substring}</span>`; return `<span class="identifier">${substring}</span>`;
} }
@ -90,7 +126,7 @@
if (result.endsWith(" ")) result = result.slice(0, -1) + "&nbsp"; if (result.endsWith(" ")) result = result.slice(0, -1) + "&nbsp";
return result; return [result, offset];
} }
</script> </script>
@ -146,7 +182,7 @@
{#each outputLines as line} {#each outputLines as line}
<p class="consoleLine"> <p class="consoleLine">
<span class="prompt">&gt;&gt;</span> <span class="prompt">&gt;&gt;</span>
{@html highlight(line)} {@html line}
</p> </p>
{/each} {/each}
</div> </div>