further improved keyboard handling

This commit is contained in:
Joeri Exelmans 2025-05-13 00:15:47 +02:00
parent 95eb8aef84
commit fa70d2f3f4
5 changed files with 38 additions and 13 deletions

View file

@ -1,6 +1,6 @@
import { useState } from "react";
import { apply, UnifyError, assignFn, getType } from "dope2";
import { apply, UnifyError, assignFn, getType, getSymbol, symbolFunction } from "dope2";
import { Editor, type EditorState } from "./Editor";
import { Value } from "./Value";
@ -97,7 +97,8 @@ export function CallBlock({ state, setState, onResolve }: CallBlockProps) {
<FunctionHeader
fn={state.fn}
setFn={setFn}
onFnResolve={onFnResolve} />
onFnResolve={onFnResolve}
input={state.input} />
<div className="functionParams">
<div className="outputParam">
{/* Sequence of input parameters */}
@ -116,7 +117,7 @@ export function CallBlock({ state, setState, onResolve }: CallBlockProps) {
</span>;
}
function FunctionHeader({ fn, setFn, onFnResolve }) {
function FunctionHeader({ fn, setFn, input, onFnResolve }) {
if (fn.kind === "call") {
// if the function we're calling is itself the result of a function call,
// then we are anonymous, and so we don't draw a function name
@ -129,9 +130,28 @@ function FunctionHeader({ fn, setFn, onFnResolve }) {
return <FunctionHeader
fn={fn.fn}
setFn={fnFn => setFn({...fn, fn: fnFn})}
onFnResolve={onFnFnResolve} />;
onFnResolve={onFnFnResolve}
input={fn.input} />;
}
else {
const filterCompatibleFns = ([_name, dynamic]: [string, Dynamic]) => {
if (input.resolved) {
try {
const type = getType(dynamic);
if (getSymbol(type) !== symbolFunction) {
return false;
}
assignFn(type, getType(input.resolved));
} catch (e) {
if (!(e instanceof UnifyError)) {
throw e;
}
return false;
}
}
return true;
}
// end of recursion - draw function name
return <div className="functionName">
&#119891;&#119899;&nbsp;
@ -141,7 +161,7 @@ function FunctionHeader({ fn, setFn, onFnResolve }) {
focus={false}
onResolve={onFnResolve}
onCancel={() => {/*todo*/}}
filter={() => true} />
filter={filterCompatibleFns} />
</div>;
}
}

View file

@ -75,10 +75,14 @@ export function Editor({state, setState, onResolve, onCancel, filter, focus}: Ed
e.preventDefault();
setNeedCommand(false);
// u -> pass Up
if (e.key === "u" || e.key === "Enter" || e.key === "Tab") {
if (e.key === "u" || e.key === "Enter" || e.key === "Tab" && !e.shiftKey) {
onResolve(state);
return;
}
if (e.key === "Tab" && e.shiftKey) {
setNeedCommand(false);
focusPrevElement();
}
// c -> Call
if (e.key === "c") {
// we become CallBlock

View file

@ -26,7 +26,7 @@
margin-left: 4px;
border: solid 1px dodgerblue;
cursor: pointer;
max-height: calc(100vh - 44px);
max-height: calc(100vh - 64px);
overflow: auto;
z-index: 10;
background-color: white;

View file

@ -2,7 +2,7 @@ import { Double, getType, Int, newDynamic, trie } from "dope2";
import { focusNextElement, focusPrevElement, setRightMostCaretPosition } from "./util/dom_trickery";
import { parseDouble, parseInt } from "./util/parse";
import { useEffect, useRef, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";
import { Type } from "./Type";
import "./InputBlock.css";
@ -29,12 +29,13 @@ const computeSuggestions = (text, env, filter) => {
const ls = [
... (asDouble ? [[asDouble.toString(), newDynamic(asDouble)(Double)]] : []),
... (asInt ? [[asInt.toString(), newDynamic(BigInt(asInt))(Int)]] : []),
... trie.suggest(env.name2dyn)(text)(10),
... trie.suggest(env.name2dyn)(text)(Infinity),
]
return [
...ls.filter(filter), // ones that match filter come first
...ls.filter(s => !filter(s)),
];
]
// .slice(0,30);
}
export function InputBlock({ state, setState, filter, onResolve, onCancel }: InputBlockProps) {
@ -48,7 +49,7 @@ export function InputBlock({ state, setState, filter, onResolve, onCancel }: Inp
}
const singleSuggestion = trie.growPrefix(env.name2dyn)(text);
const suggestions = computeSuggestions(text, env, filter);
const suggestions = useMemo(() => computeSuggestions(text, env, filter), [text]);
useEffect(() => {
setI(0); // reset
@ -186,7 +187,7 @@ function Suggestions({ suggestions, onSelect, i, setI }) {
{suggestions.map(([name, dynamic], j) =>
<div
key={`${j}_${name}`}
className={i === j ? "selected" : ""}
className={(i === j ? " selected" : "")}
onMouseEnter={onMouseEnter(j)}
onMouseDown={onMouseDown(j)}>
{name} :: <Type type={getType(dynamic)} />

View file

@ -58,7 +58,7 @@
.iteratorType {
border-style: dashed;
animation: flickerAnimation 500ms steps(1) normal infinite;
/* animation: flickerAnimation 500ms steps(1) normal infinite; */
}
/* Animations */