kalk_web: Adjusted for piecewise and comparison operators

This commit is contained in:
bakk 2021-06-01 12:30:21 +02:00
parent 6697a37faa
commit 349aeb7f0b
3 changed files with 48 additions and 23 deletions

View File

@ -213,7 +213,6 @@ mod tests {
KalkNum::new_with_imaginary(KalkNum::from(2f64).value, "", KalkNum::from(3f64).value), KalkNum::new_with_imaginary(KalkNum::from(2f64).value, "", KalkNum::from(3f64).value),
) )
.unwrap(); .unwrap();
println!("{}", result.to_f64());
assert!(cmp(result.to_f64(), -4.5f64) || cmp(result.to_f64(), -4.499999f64)); assert!(cmp(result.to_f64(), -4.5f64) || cmp(result.to_f64(), -4.499999f64));
assert!(cmp(result.imaginary_to_f64(), 18f64)); assert!(cmp(result.imaginary_to_f64(), 18f64));
} }

View File

@ -1,15 +1,15 @@
{ {
"name": "@paddim8/kalk-component", "name": "@paddim8/kalk-component",
"version": "1.1.3", "version": "1.2.3",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@paddim8/kalk-component", "name": "@paddim8/kalk-component",
"version": "1.1.3", "version": "1.2.3",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@paddim8/kalk": "^2.0.2", "@paddim8/kalk": "../kalk/pkg",
"shadow-selection-polyfill": "^1.1.0" "shadow-selection-polyfill": "^1.1.0"
}, },
"devDependencies": { "devDependencies": {
@ -49,7 +49,6 @@
"../kalk/pkg": { "../kalk/pkg": {
"name": "@paddim8/kalk", "name": "@paddim8/kalk",
"version": "2.0.0", "version": "2.0.0",
"extraneous": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/@babel/code-frame": { "node_modules/@babel/code-frame": {
@ -1129,9 +1128,8 @@
} }
}, },
"node_modules/@paddim8/kalk": { "node_modules/@paddim8/kalk": {
"version": "2.0.2", "resolved": "../kalk/pkg",
"resolved": "https://registry.npmjs.org/@paddim8/kalk/-/kalk-2.0.2.tgz", "link": true
"integrity": "sha512-okiy4eUMzZZXRaE3hEQSv6BQ+w56M4IWw7iAr4F+UMZKY0omzAMPyDaL53PtDrT0A1mDYvMRYpV3GzlykVPs+Q=="
}, },
"node_modules/@trysound/sax": { "node_modules/@trysound/sax": {
"version": "0.1.1", "version": "0.1.1",
@ -12308,9 +12306,7 @@
} }
}, },
"@paddim8/kalk": { "@paddim8/kalk": {
"version": "2.0.2", "version": "file:../kalk/pkg"
"resolved": "https://registry.npmjs.org/@paddim8/kalk/-/kalk-2.0.2.tgz",
"integrity": "sha512-okiy4eUMzZZXRaE3hEQSv6BQ+w56M4IWw7iAr4F+UMZKY0omzAMPyDaL53PtDrT0A1mDYvMRYpV3GzlykVPs+Q=="
}, },
"@trysound/sax": { "@trysound/sax": {
"version": "0.1.1", "version": "0.1.1",

View File

@ -48,14 +48,13 @@
let hasBeenInteractedWith = false; let hasBeenInteractedWith = false;
function setText(text: string) { function setText(text: string) {
inputElement.value = text;
const highlighted = highlight(text); const highlighted = highlight(text);
setHtml(highlighted); setHtml(highlighted);
} }
function setHtml(html: string) { function setHtml(html: string) {
highlightedTextElement.innerHTML = html; highlightedTextElement.innerHTML = html;
inputElement.value = highlightedTextElement.textContent; inputElement.value = highlightedTextElement.innerText;
if (!html) { if (!html) {
highlightedTextElement.innerHTML = `<span class='placeholder'>${hinttext}</div>`; highlightedTextElement.innerHTML = `<span class='placeholder'>${hinttext}</div>`;
@ -72,7 +71,7 @@
): [result: string, success: boolean] { ): [result: string, success: boolean] {
try { try {
if (!kalkContext) kalkContext = new kalk.Context(); if (!kalkContext) kalkContext = new kalk.Context();
const result = kalkContext.evaluate(input); const result = kalkContext.evaluate(input.replaceAll(/\s+/g, " "));
return [result?.toPrettyString(), true]; return [result?.toPrettyString(), true];
} catch (err) { } catch (err) {
@ -80,9 +79,28 @@
} }
} }
function hasUnevenAmountOfBraces(input: string): boolean {
let openCount = 0;
let closedCount = 0;
for (const char of input) {
if (char == "{") openCount++;
if (char == "}") closedCount++;
}
return openCount > closedCount;
}
function handleKeyDown(event: KeyboardEvent, kalk: Kalk) { function handleKeyDown(event: KeyboardEvent, kalk: Kalk) {
hasBeenInteractedWith = true; hasBeenInteractedWith = true;
if (event.key == "Enter") { if (event.key == "Enter") {
if (
hasUnevenAmountOfBraces(
(event.target as HTMLTextAreaElement).value
)
) {
return;
}
selectedLineOffset = 0; selectedLineOffset = 0;
const input = inputElement.value; const input = inputElement.value;
let output: string; let output: string;
@ -157,13 +175,7 @@
function handleInput(event: Event) { function handleInput(event: Event) {
const target = event.target as HTMLInputElement; const target = event.target as HTMLInputElement;
// Make sure it doesn't mess with the HTML. setText(target.value == "\n" ? "" : target.value);
target.value = target.value
.replaceAll("\n", "")
.replaceAll(" ", " ")
.replaceAll("&", "")
.replaceAll("<", "");
setText(target.value);
} }
function handleTouchLine(event: Event) { function handleTouchLine(event: Event) {
@ -236,8 +248,22 @@
if (!input) return ""; if (!input) return "";
let result = input; let result = input;
result = result.replace( result = result.replace(
/(?<identifier>[^!-@\s_|^⌊⌋⌈⌉≈]+(_\d+)?)|(?<op>[+\-/*%^!≈])/g, /(?<html>[<>&]|(\n\s*\}?|\s+))|(?<op>([+\-/*%^!≈]|if|otherwise)|(?<identifier>[^!-@\s_|^⌊⌋⌈⌉≈\[\]\{\}≠≥≤]+(_\d+)?))/g,
(substring, identifier, _, op) => { (substring, _, html, _2, op, identifier) => {
if (html) {
if (substring == "<") return "&lt;";
if (substring == ">") return "&gt;";
if (substring == "&") return "&amp;";
if (substring.match(/\s+/)) return "&nbsp;";
if (substring.startsWith("\n")) {
if (substring.endsWith("}")) {
return "<br />}";
} else {
return "<br />&nbsp;&nbsp;";
}
}
}
if (identifier) { if (identifier) {
let newSubstring: string = substring; let newSubstring: string = substring;
switch (substring) { switch (substring) {
@ -412,6 +438,7 @@
width: 100%; width: 100%;
color: white; color: white;
word-wrap: anywhere; word-wrap: anywhere;
line-height: 1;
z-index: 1; z-index: 1;
} }
@ -420,11 +447,14 @@
position: absolute; position: absolute;
top: 0; top: 0;
left: 0; left: 0;
height: 100%;
width: 100%; width: 100%;
border: 0; border: 0;
font-size: inherit; font-size: inherit;
font-family: inherit; font-family: inherit;
line-height: 1;
overflow: hidden;
cursor: text; cursor: text;
color: transparent; color: transparent;
background: transparent; background: transparent;