Adds a default template to resolver, adds "last" variable

This commit is contained in:
Emmaline Autumn 2024-08-21 15:43:59 -06:00
parent 9c9edd9e90
commit 1799c8da79
2 changed files with 10 additions and 5 deletions

View File

@ -34,8 +34,8 @@ export function OnDemandResolver({
const res = useRef(new TTCQueryResolver(parser)); const res = useRef(new TTCQueryResolver(parser));
const [content, setContent] = useState<ReactNode>(""); const [content, setContent] = useState<ReactNode>("");
const generateContent = useCallback(() => { const generateContent = useCallback(() => {
let content = template; let content = template || "$x";
const stackIdxs = Array.from(new Set(template.match(/\$\d/g))); const stackIdxs = Array.from(new Set(content.match(/\$(?:\d+|x)/g)));
for (const idx of stackIdxs) { for (const idx of stackIdxs) {
let thing = res.current.getFromStack(idx); let thing = res.current.getFromStack(idx);
if (Array.isArray(thing)) thing = thing.at(0); if (Array.isArray(thing)) thing = thing.at(0);
@ -52,12 +52,16 @@ export function OnDemandResolver({
}, [res, resolver, generateContent]); }, [res, resolver, generateContent]);
return ( return (
<> <div className="my-2 rounded-md p-1 bg-black/20 w-min">
<button onMouseDown={() => setContent("")} onClick={resolve}> <button
className="text-primary-600"
onMouseDown={() => setContent("")}
onClick={resolve}
>
{title ?? "Resolve"} {title ?? "Resolve"}
</button> </button>
<br /> <br />
{!!content && <span>{content}</span>} {!!content && <span>{content}</span>}
</> </div>
); );
} }

View File

@ -165,6 +165,7 @@ export class TTCQueryResolver {
} }
public getFromStack(stackIndex: string): StackItem { public getFromStack(stackIndex: string): StackItem {
if (stackIndex === "$x") return this.stack.at(-1)!;
const i = Number(stackIndex.replace("$", "")); const i = Number(stackIndex.replace("$", ""));
const val = this.stack[i]; const val = this.stack[i];
return val; return val;