dope2-webapp/src/Editor.tsx

188 lines
5.1 KiB
TypeScript

import { getSymbol, getType, symbolFunction } from "dope2";
import { useContext, useEffect, useReducer, useRef, useState } from "react";
import { CallBlock, type CallBlockState } from "./CallBlock";
import { InputBlock, type InputBlockState } from "./InputBlock";
import { Type } from "./Type";
import { DeepError, type Dynamic, type State2Props } from "./types";
import "./Editor.css";
import { LetInBlock, type LetInBlockState } from "./LetInBlock";
import { focusNextElement, focusPrevElement } from "./util/dom_trickery";
import type { LambdaBlockState } from "./LambdaBlock";
import { initialEditorState } from "./configurations";
import { CommandContext } from "./CommandContext";
export type EditorState =
InputBlockState
| CallBlockState
| LetInBlockState
| LambdaBlockState;
interface EditorProps extends State2Props<EditorState> {
filter: (suggestion: [string, Dynamic]) => boolean;
onCancel: () => void;
}
function getCommands(type) {
const commands = ['e', 't', 'Enter', 'Backspace', 'ArrowLeft', 'ArrowRight', 'Tab', 'l', '=', '.'];
if (getSymbol(type) === symbolFunction) {
commands.push('c');
}
return commands;
}
function getShortCommands(type) {
if (getSymbol(type) === symbolFunction) {
return 'c|Tab|.';
}
return 'Tab|.';
}
function removeFocus(state: EditorState): EditorState {
if (state.kind === "input") {
return {...state, focus: false};
}
if (state.kind === "call") {
return {...state,
fn: removeFocus(state.fn),
input: removeFocus(state.input),
};
}
return state;
}
export function Editor({state, setState, onCancel, filter}: EditorProps) {
const [needCommand, setNeedCommand] = useState(false);
const commandInputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (needCommand) {
commandInputRef.current?.focus();
}
}, [needCommand]);
// const onMyResolve = (editorState: EditorState) => {
// setState(editorState);
// onResolve(editorState);
// return;
// if (editorState.resolved) {
// setNeedCommand(true);
// }
// else {
// // unresolved
// setNeedCommand(false);
// onResolve(editorState); // pass up the fact that we're unresolved
// }
// }
const globalContext = useContext(CommandContext);
const onCommand = (e: React.KeyboardEvent) => {
// const type = getType(state.resolved);
// const commands = getCommands(type);
const commands = ['e', 't', 'Enter', 'Backspace', 'ArrowLeft', 'ArrowRight', 'Tab', 'l', '=', '.', 'c'];
if (!commands.includes(e.key)) {
return;
}
e.preventDefault();
setNeedCommand(false);
// u -> pass Up
if (e.key === "e" || e.key === "Enter" || e.key === "Tab" && !e.shiftKey) {
// onResolve(state);
globalContext?.doHighlight.eval();
return;
}
if (e.key === "Tab" && e.shiftKey) {
setNeedCommand(false);
focusPrevElement();
}
// c -> Call
if (e.key === "c") {
// we become CallBlock
setState(state => ({
kind: "call",
fn: removeFocus(state),
input: initialEditorState,
resolved: undefined,
}));
globalContext?.doHighlight.call();
// focusNextElement();
return;
}
// t -> Transform
if (e.key === "t" || e.key === ".") {
// we become CallBlock
setState(state => ({
kind: "call",
fn: initialEditorState,
input: removeFocus(state),
resolved: undefined,
}));
globalContext?.doHighlight.transform();
return;
}
if (e.key === "Backspace" || e.key === "ArrowLeft") {
focusPrevElement();
return;
}
if (e.key === "ArrowRight") {
focusNextElement();
return;
}
// l -> Let ... in ...
// = -> assign to name
if (e.key === 'l' || e.key === '=') {
// we become LetInBlock
setState(state => ({
kind: "let",
inner: removeFocus(initialEditorState),
name: "",
value: removeFocus(state),
resolved: undefined,
}));
globalContext?.doHighlight.let();
return;
}
};
const renderBlock = () => {
switch (state.kind) {
case "input":
return <InputBlock
state={state}
setState={setState as (callback:(p:InputBlockState)=>EditorState)=>void}
filter={filter}
onCancel={onCancel}
/>;
case "call":
return <CallBlock
state={state}
setState={setState as (callback:(p:CallBlockState)=>EditorState)=>void}
/>;
case "let":
return <LetInBlock
state={state}
setState={setState as (callback:(p:LetInBlockState)=>EditorState)=>void}
/>;
case "lambda":
return <></>;
}
}
return <>
{renderBlock()}
{
(state.resolved && !(state.resolved instanceof DeepError))
? <div className="typeSignature">
:: <Type type={getType(state.resolved)} />
</div>
: <></>
}
<input
ref={commandInputRef}
spellCheck={false}
className="editable commandInput"
placeholder={`<command>`}
onKeyDown={onCommand}
value={""}
onChange={() => {}} />
</>;
}