1
0
mirror of https://github.com/PaddiM8/kalker.git synced 2025-03-01 06:41:15 +01:00

Fixed kalk_web browser compatibility problems

This commit is contained in:
PaddiM8 2021-01-05 18:38:22 +01:00
parent 693a53ddc7
commit e31f983c21
6 changed files with 103 additions and 70 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@paddim8/kalk-component", "name": "@paddim8/kalk-component",
"version": "1.0.0", "version": "1.0.6",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -7289,6 +7289,11 @@
"safe-buffer": "^5.0.1" "safe-buffer": "^5.0.1"
} }
}, },
"shadow-selection-polyfill": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/shadow-selection-polyfill/-/shadow-selection-polyfill-1.1.0.tgz",
"integrity": "sha512-ntz8P6DLEFpx7gikeXZ4gSi3APE2D+BP0rKnnaBzED+Lm8je8nkNcayy6kGWPEDWMFbtm+Yvd1ONFaXcsVWn2w=="
},
"shebang-command": { "shebang-command": {
"version": "1.2.0", "version": "1.2.0",
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz",
@ -7915,6 +7920,11 @@
"strip-indent": "^3.0.0" "strip-indent": "^3.0.0"
} }
}, },
"svelte-tag": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/svelte-tag/-/svelte-tag-1.0.1.tgz",
"integrity": "sha512-d3+bXGCZ/aSfFc1QBBGJFFeiQ+sukks+GrBQbk1YTYxA6OhlfFbWfFcyBaz7ONhspOBN9gbRq4+AZYhORALQ+A=="
},
"svgo": { "svgo": {
"version": "1.3.2", "version": "1.3.2",
"resolved": "https://registry.npmjs.org/svgo/-/svgo-1.3.2.tgz", "resolved": "https://registry.npmjs.org/svgo/-/svgo-1.3.2.tgz",

View File

@ -1,6 +1,6 @@
{ {
"name": "@paddim8/kalk-component", "name": "@paddim8/kalk-component",
"version": "1.0.6", "version": "1.0.7",
"description": "A Svelte component for kalk, a calculator that supports user-defined functions and variables.", "description": "A Svelte component for kalk, a calculator that supports user-defined functions and variables.",
"svelte": "src/main.ts", "svelte": "src/main.ts",
"main": "public/build/bundle.js", "main": "public/build/bundle.js",
@ -46,6 +46,7 @@
"svelte-check": "^1.1.15", "svelte-check": "^1.1.15",
"svelte-loader-hot": "^0.3.1", "svelte-loader-hot": "^0.3.1",
"svelte-preprocess": "^4.6.1", "svelte-preprocess": "^4.6.1",
"svelte-tag": "^1.0.1",
"terser-webpack-plugin": "^4.2.3", "terser-webpack-plugin": "^4.2.3",
"ts-loader": "^8.0.2", "ts-loader": "^8.0.2",
"ts-node": "^9.0.0", "ts-node": "^9.0.0",

View File

@ -1,20 +1,19 @@
<script lang="ts"> <script lang="ts">
export let byUser = false; export let byuser = false;
</script> </script>
<style> <style lang="scss">
p { .console-line-tg638d3 {
margin-top: 0; margin-top: 0;
margin-bottom: 5px; margin-bottom: 5px;
word-wrap: break-word; word-wrap: break-word;
} }
.result { .console-line-tg638d3.result {
margin-bottom: 10px; margin-bottom: 10px;
} }
</style> </style>
<svelte:options tag="console-line" /> <p class="console-line-tg638d3" class:result={!byuser}>
<p class:result={!byUser}>
<slot /> <slot />
</p> </p>

View File

@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import { afterUpdate } from "svelte"; import { afterUpdate } from "svelte";
import type { Context } from "@paddim8/kalk"; import type { Context } from "@paddim8/kalk";
import ConsoleLine from "./ConsoleLine.svelte";
// Props, HTML doesn't recognise them if they're not like this // Props, HTML doesn't recognise them if they're not like this
export let identifiercolor = "cornflowerblue"; export let identifiercolor = "cornflowerblue";
@ -57,7 +58,10 @@
href="https://kalk.netlify.app/#usage" href="https://kalk.netlify.app/#usage"
target="blank">Link to usage guide</a>`; target="blank">Link to usage guide</a>`;
} else { } else {
const [result, success] = calculate(kalk, input); const [result, success] = calculate(
kalk,
input.replace(/\s+/g, "") // Temporary fix, since it for some reason complains about spaces on chrome
);
output = success output = success
? highlight(result)[0] ? highlight(result)[0]
: `<span style="color: ${errorcolor}">${result}</span>`; : `<span style="color: ${errorcolor}">${result}</span>`;
@ -119,16 +123,13 @@
} }
function getCursorPos(element: HTMLInputElement): number { function getCursorPos(element: HTMLInputElement): number {
const selection = window.getSelection(); const selection = document.getSelection();
if (selection.rangeCount !== 0) {
const range = selection.getRangeAt(0); const range = selection.getRangeAt(0);
const preCaretRange = range.cloneRange(); const preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(element); preCaretRange.selectNodeContents(element);
preCaretRange.setEnd(range.endContainer, range.endOffset); preCaretRange.setEnd(range.endContainer, range.endOffset);
return preCaretRange.toString().length;
}
return 0; return preCaretRange.toString().length;
} }
function setCursorPos(element: HTMLElement, indexToSelect: number) { function setCursorPos(element: HTMLElement, indexToSelect: number) {
@ -187,7 +188,7 @@
if (!input) return ["", 0]; if (!input) return ["", 0];
let result = input; let result = input;
let offset = 0; let offset = 0;
result = result.replaceAll( result = result.replace(
/(?<identifier>[^!-@\s_|^⌊⌋⌈⌉]+(_\d+)?)|(?<op>[+\-/*%^!])/g, /(?<identifier>[^!-@\s_|^⌊⌋⌈⌉]+(_\d+)?)|(?<op>[+\-/*%^!])/g,
(substring, identifier, _, op) => { (substring, identifier, _, op) => {
if (identifier) { if (identifier) {
@ -239,7 +240,7 @@
</script> </script>
<style lang="scss"> <style lang="scss">
.calculator { .calculator-tg638d3 {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
width: 100%; width: 100%;
@ -247,7 +248,6 @@
box-sizing: border-box; box-sizing: border-box;
background-color: inherit; background-color: inherit;
color: inherit; color: inherit;
}
.output { .output {
display: flex; display: flex;
@ -296,19 +296,19 @@
color: gray; color: gray;
background-color: transparent; background-color: transparent;
} }
}
</style> </style>
<svelte:options tag="kalk-calculator" /> <div class="calculator-tg638d3">
<div class="calculator">
<div class="output" bind:this={outputElement}> <div class="output" bind:this={outputElement}>
<slot /> <slot />
{#each outputLines as line} {#each outputLines as line}
<console-line byUser={line[1]}> <ConsoleLine byuser={line[1]}>
{#if line[1]} {#if line[1]}
<span style="color: {promptcolor}">&gt;&gt;</span> <span style="color: {promptcolor}">&gt;&gt;</span>
{/if} {/if}
{@html line[0]} {@html line[0]}
</console-line> </ConsoleLine>
{/each} {/each}
</div> </div>
<div class="input-area"> <div class="input-area">

View File

@ -1,6 +1,29 @@
import "./public-path.js"; import "./public-path.js";
import KalkCalculator from './KalkCalculator.svelte'; import KalkCalculator from './KalkCalculator.svelte';
import ConsoleLine from './ConsoleLine.svelte';
import component from "svelte-tag";
new component({
component: KalkCalculator,
tagname: "kalk-calculator",
attributes: [
"identifiercolor",
"operatorcolor",
"promptcolor",
"errorcolor",
"linkcolor",
"hinttext",
"autofocus"
]
});
new component({
component: ConsoleLine,
tagname: "console-line",
attributes: ["byuser"]
});
export { export {
KalkCalculator, KalkCalculator,
ConsoleLine,
} }

View File

@ -61,7 +61,7 @@ const config: webpack.Configuration & WebpackDevServer.Configuration = {
use: { use: {
loader: 'svelte-loader-hot', loader: 'svelte-loader-hot',
options: { options: {
customElement: true, //customElement: true,
dev: !prod, dev: !prod,
emitCss: prod, emitCss: prod,
hotReload: !prod, hotReload: !prod,