rename Editor -> ExprBlock
This commit is contained in:
parent
fe83532261
commit
93f665ba8f
9 changed files with 55 additions and 56 deletions
200
src/Editor.tsx
200
src/Editor.tsx
|
|
@ -1,200 +0,0 @@
|
|||
import { useContext, useEffect, useRef, useState } from "react";
|
||||
|
||||
import { getSymbol, getType, symbolFunction } from "dope2";
|
||||
|
||||
import { CallBlock, type CallBlockState } from "./CallBlock";
|
||||
import { InputBlock, type InputBlockState, type SuggestionType } from "./InputBlock";
|
||||
import { Type } from "./Type";
|
||||
import { evalEditorBlock, type ResolvedType } from "./eval";
|
||||
import { GlobalContext } from "./GlobalContext";
|
||||
import "./Editor.css";
|
||||
import { EnvContext } from "./EnvContext";
|
||||
import { LambdaBlock, type LambdaBlockState } from "./LambdaBlock";
|
||||
import { LetInBlock, type LetInBlockState } from "./LetInBlock";
|
||||
import { initialEditorState } from "./configurations";
|
||||
import { focusNextElement, focusPrevElement } from "./util/dom_trickery";
|
||||
|
||||
export type EditorState =
|
||||
InputBlockState
|
||||
| CallBlockState
|
||||
| LetInBlockState
|
||||
| LambdaBlockState;
|
||||
|
||||
export type SetStateFn<InType = EditorState, OutType = InType> = (state: InType) => OutType;
|
||||
|
||||
export interface State2Props<InType, OutType = InType> {
|
||||
state: InType;
|
||||
setState: (callback: SetStateFn<InType, OutType>) => void;
|
||||
suggestionPriority: (suggestion: ResolvedType) => number;
|
||||
}
|
||||
|
||||
interface EditorProps extends State2Props<EditorState> {
|
||||
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, suggestionPriority}: EditorProps) {
|
||||
const env = useContext(EnvContext);
|
||||
const [needCommand, setNeedCommand] = useState(false);
|
||||
const commandInputRef = useRef<HTMLInputElement>(null);
|
||||
useEffect(() => {
|
||||
if (needCommand) {
|
||||
commandInputRef.current?.focus();
|
||||
}
|
||||
}, [needCommand]);
|
||||
|
||||
const globalContext = useContext(GlobalContext);
|
||||
const onCommand = (e: React.KeyboardEvent) => {
|
||||
const commands = ['e', 't', 'Enter', 'Backspace', 'ArrowLeft', 'ArrowRight', 'Tab', 'l', 'L', '=', '.', 'c', 'a'];
|
||||
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 === '=' && !e.shiftKey) {
|
||||
// we become LetInBlock
|
||||
setState(state => ({
|
||||
kind: "let",
|
||||
inner: removeFocus(initialEditorState),
|
||||
name: "",
|
||||
value: removeFocus(state),
|
||||
}));
|
||||
globalContext?.doHighlight.let();
|
||||
return;
|
||||
}
|
||||
if (e.key === 'L' || e.key === '=' && e.shiftKey) {
|
||||
setState(state => ({
|
||||
kind: "let",
|
||||
inner: removeFocus(state),
|
||||
name: "",
|
||||
value: removeFocus(initialEditorState),
|
||||
}));
|
||||
}
|
||||
// a -> lAmbdA
|
||||
if (e.key === "a") {
|
||||
setState(state => ({
|
||||
kind: "lambda",
|
||||
paramName: "",
|
||||
expr: removeFocus(state),
|
||||
}));
|
||||
globalContext?.doHighlight.lambda();
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
const renderBlock = () => {
|
||||
switch (state.kind) {
|
||||
case "input":
|
||||
return <InputBlock
|
||||
state={state}
|
||||
setState={setState as (callback:(p:InputBlockState)=>EditorState)=>void}
|
||||
suggestionPriority={suggestionPriority}
|
||||
onCancel={onCancel}
|
||||
/>;
|
||||
case "call":
|
||||
return <CallBlock
|
||||
state={state}
|
||||
setState={setState as (callback:(p:CallBlockState)=>EditorState)=>void}
|
||||
suggestionPriority={suggestionPriority}
|
||||
/>;
|
||||
case "let":
|
||||
return <LetInBlock
|
||||
state={state}
|
||||
setState={setState as (callback:(p:LetInBlockState)=>EditorState)=>void}
|
||||
suggestionPriority={suggestionPriority}
|
||||
/>;
|
||||
case "lambda":
|
||||
return <LambdaBlock
|
||||
state={state}
|
||||
setState={setState as (callback:(p:LambdaBlockState)=>EditorState)=>void}
|
||||
suggestionPriority={suggestionPriority}
|
||||
/>;
|
||||
}
|
||||
}
|
||||
const resolved = evalEditorBlock(state, env);
|
||||
return <span className={"editor" + ((resolved.kind!=="value") ? " "+resolved.kind : "")}>
|
||||
{renderBlock()}
|
||||
<div className="typeSignature">
|
||||
:: <Type type={getType(resolved)} />
|
||||
</div>
|
||||
<input
|
||||
ref={commandInputRef}
|
||||
spellCheck={false}
|
||||
className="editable commandInput"
|
||||
placeholder={`<c>`}
|
||||
onKeyDown={onCommand}
|
||||
value={""}
|
||||
onChange={() => {}} />
|
||||
</span>;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue