mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-01-31 17:09:13 +01:00
kalk_web history
This commit is contained in:
parent
bd625fdfde
commit
d301e52391
@ -2,7 +2,7 @@
|
|||||||
"name": "@paddim8/kalk-component",
|
"name": "@paddim8/kalk-component",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"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/index.ts",
|
"svelte": "src/main.ts",
|
||||||
"main": "public/build/bundle.js",
|
"main": "public/build/bundle.js",
|
||||||
"module": "public/build/bundle.js",
|
"module": "public/build/bundle.js",
|
||||||
"author": "PaddiM8",
|
"author": "PaddiM8",
|
||||||
|
@ -10,9 +10,10 @@
|
|||||||
export let backgroundColor = "#424242";
|
export let backgroundColor = "#424242";
|
||||||
export let textColor = "white";
|
export let textColor = "white";
|
||||||
|
|
||||||
let outputLines: string[] = [];
|
let outputLines: [value: string, byUser: boolean][] = [];
|
||||||
let outputElement: HTMLElement;
|
let outputElement: HTMLElement;
|
||||||
let kalkContext;
|
let kalkContext;
|
||||||
|
let selectedLineOffset: number = 0;
|
||||||
|
|
||||||
afterUpdate(() => {
|
afterUpdate(() => {
|
||||||
// Scroll to bottom
|
// Scroll to bottom
|
||||||
@ -23,6 +24,7 @@
|
|||||||
|
|
||||||
function handleKeyDown(event: KeyboardEvent, kalk) {
|
function handleKeyDown(event: KeyboardEvent, kalk) {
|
||||||
if (event.key == "Enter") {
|
if (event.key == "Enter") {
|
||||||
|
selectedLineOffset = 0;
|
||||||
const target = event.target as HTMLInputElement;
|
const target = event.target as HTMLInputElement;
|
||||||
const input = target.textContent;
|
const input = target.textContent;
|
||||||
let output: string;
|
let output: string;
|
||||||
@ -42,15 +44,49 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const inputHTML = `<span style="color: ${promptColor}">>> </span>${target.innerHTML}`;
|
|
||||||
outputLines = output
|
outputLines = output
|
||||||
? [...outputLines, inputHTML, output]
|
? [...outputLines, [target.innerHTML, true], [output, false]]
|
||||||
: [...outputLines, inputHTML];
|
: [...outputLines, [target.innerHTML, true]];
|
||||||
|
|
||||||
target.innerHTML = "";
|
target.innerHTML = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleKeyUp(event: KeyboardEvent) {
|
||||||
|
// on keyup, since pressing the arrow up key makes the cursor go to the start
|
||||||
|
// of the input field. This piece of code will put the cursor at the end,
|
||||||
|
// which therefore will need to be done afterwards, so that it doesn't just get moved back again.
|
||||||
|
if (event.key == "ArrowUp" || event.key == "ArrowDown") {
|
||||||
|
const target = event.target as HTMLInputElement;
|
||||||
|
const change = event.key == "ArrowUp" ? 1 : -1;
|
||||||
|
selectedLineOffset += change;
|
||||||
|
|
||||||
|
if (selectedLineOffset < 0) {
|
||||||
|
target.innerHTML = "";
|
||||||
|
selectedLineOffset = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const index = outputLines.length - selectedLineOffset - 1;
|
||||||
|
let line = outputLines[index];
|
||||||
|
|
||||||
|
// If it was just a response, get the one above that instead
|
||||||
|
while (line && !line[1]) {
|
||||||
|
line = outputLines[index - change];
|
||||||
|
selectedLineOffset += change;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line) {
|
||||||
|
target.innerHTML = line[0];
|
||||||
|
setCursorPosEnd(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selectedLineOffset >= outputLines.length) {
|
||||||
|
selectedLineOffset = outputLines.length - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
@ -99,6 +135,15 @@
|
|||||||
selection.addRange(range);
|
selection.addRange(range);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setCursorPosEnd(element: HTMLElement) {
|
||||||
|
const range = document.createRange();
|
||||||
|
const selection = window.getSelection();
|
||||||
|
range.selectNodeContents(element);
|
||||||
|
range.setStart(element, range.endOffset);
|
||||||
|
selection.removeAllRanges();
|
||||||
|
selection.addRange(range);
|
||||||
|
}
|
||||||
|
|
||||||
function getTextNodesIn(node: Node): Text[] {
|
function getTextNodesIn(node: Node): Text[] {
|
||||||
const textNodes: Text[] = [];
|
const textNodes: Text[] = [];
|
||||||
|
|
||||||
@ -225,7 +270,10 @@
|
|||||||
</p>
|
</p>
|
||||||
{#each outputLines as line}
|
{#each outputLines as line}
|
||||||
<p class="consoleLine">
|
<p class="consoleLine">
|
||||||
{@html line}
|
{#if line[1]}
|
||||||
|
<span style="color: {promptColor}">>></span>
|
||||||
|
{/if}
|
||||||
|
{@html line[0]}
|
||||||
</p>
|
</p>
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
@ -238,6 +286,7 @@
|
|||||||
contenteditable="true"
|
contenteditable="true"
|
||||||
class="input"
|
class="input"
|
||||||
on:keydown={(event) => handleKeyDown(event, kalk)}
|
on:keydown={(event) => handleKeyDown(event, kalk)}
|
||||||
|
on:keyup={handleKeyUp}
|
||||||
on:input={handleInput}
|
on:input={handleInput}
|
||||||
role="textbox" />
|
role="textbox" />
|
||||||
{:catch error}
|
{:catch error}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// https://github.com/webpack/webpack/issues/7968#issuecomment-421058484
|
// https://github.com/webpack/webpack/issues/7968#issuecomment-421058484
|
||||||
const url = new URL(document.currentScript.src);
|
const url = new URL(document.currentScript.src);
|
||||||
const widgetLink = url.href.substring(0, url.href.lastIndexOf('/') + 1);
|
const widgetLink = url.href.substring(0, url.href.lastIndexOf('/') + 1);
|
||||||
__webpack_public_path__ = widgetLink;
|
if (!__webpack_public_path__) __webpack_public_path__ = widgetLink;
|
@ -46,6 +46,7 @@ const config: webpack.Configuration & WebpackDevServer.Configuration = {
|
|||||||
mainFields: ['svelte', 'browser', 'module', 'main'],
|
mainFields: ['svelte', 'browser', 'module', 'main'],
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
|
publicPath: prod ? '' : '/build/',
|
||||||
path: __dirname + '/public/build',
|
path: __dirname + '/public/build',
|
||||||
filename: '[name].js',
|
filename: '[name].js',
|
||||||
chunkFilename: '[name].[id].js',
|
chunkFilename: '[name].[id].js',
|
||||||
@ -60,7 +61,7 @@ const config: webpack.Configuration & WebpackDevServer.Configuration = {
|
|||||||
use: {
|
use: {
|
||||||
loader: 'svelte-loader-hot',
|
loader: 'svelte-loader-hot',
|
||||||
options: {
|
options: {
|
||||||
customElement: prod,
|
customElement: true,
|
||||||
dev: !prod,
|
dev: !prod,
|
||||||
emitCss: prod,
|
emitCss: prod,
|
||||||
hotReload: !prod,
|
hotReload: !prod,
|
||||||
|
Loading…
Reference in New Issue
Block a user