From 85dd8f63f707f8bc3b4a3daad83b5b0b1e4f0685 Mon Sep 17 00:00:00 2001 From: Rizky Date: Mon, 5 Feb 2024 10:04:58 +0700 Subject: [PATCH] wip fix generator --- .../ed/panel/popup/script/default-val.tsx | 11 ++- .../nova/ed/panel/popup/script/prop-gen.tsx | 70 +++++++++++++++++-- 2 files changed, 72 insertions(+), 9 deletions(-) diff --git a/app/web/src/nova/ed/panel/popup/script/default-val.tsx b/app/web/src/nova/ed/panel/popup/script/default-val.tsx index 14265a8e..a627cafb 100644 --- a/app/web/src/nova/ed/panel/popup/script/default-val.tsx +++ b/app/web/src/nova/ed/panel/popup/script/default-val.tsx @@ -41,15 +41,20 @@ export const edMonacoDefaultVal = (p: PG, adv: FNAdv, mitem: MItem) => { `\ [ // suggeestion - async () => { }, + async () => { + return { + label: '', + list: [], + } + }, // generate - async () => { + async (opt: string) => { return \`""\`; }, ]`; } else if (kind === "visible") { val = mprop.get("visible") || "true"; - } else if (kind === "typings") { + } else if (kind === "typings") { val = mprop.get("typings") || "const typings = {}"; } else if (kind === "option") { val = diff --git a/app/web/src/nova/ed/panel/popup/script/prop-gen.tsx b/app/web/src/nova/ed/panel/popup/script/prop-gen.tsx index 8362835d..0f7a91ea 100644 --- a/app/web/src/nova/ed/panel/popup/script/prop-gen.tsx +++ b/app/web/src/nova/ed/panel/popup/script/prop-gen.tsx @@ -1,10 +1,16 @@ -import { useGlobal } from "web-utils"; +import { useGlobal, useLocal } from "web-utils"; import { EDGlobal, IMeta, active } from "../../../logic/ed-global"; import { Button } from "../../../../../utils/ui/form/Button"; import { IItem } from "../../../../../utils/types/item"; export const EdPropGen = () => { const p = useGlobal(EDGlobal, "EDITOR"); + const local = useLocal({ + options: [] as string[], + selected: "", + suggest_label: "", + suggest_status: "init" as "init" | "loading" | "ready", + }); let item = null as unknown as IItem; @@ -26,10 +32,12 @@ export const EdPropGen = () => { const prop = props[prop_name]; const gen = { - fn: undefined as undefined | (() => string | Promise), + fn: undefined as undefined | ((opt: string) => string | Promise), suggestions: undefined as | undefined - | (() => Promise | string[]), + | (() => + | Promise<{ list: string[]; label: string }> + | { list: string[]; label: string }), }; if (prop && prop.gen && prop.genBuilt) { @@ -63,14 +71,63 @@ export const EdPropGen = () => { return; } + if (gen.suggestions && local.suggest_status === "init") { + const res = gen.suggestions(); + if (typeof res === "object" && res instanceof Promise) { + local.suggest_status = "loading"; + res.then((val) => { + local.suggest_status = "ready"; + local.options = val.list; + local.suggest_label = val.label; + local.render(); + }); + } else if (res && Array.isArray(res.list)) { + local.suggest_status = "ready"; + local.suggest_label = res.label; + local.options = res.list; + } + } + if (gen.fn) { return (
+ {gen.suggestions && ( + <> + {local.suggest_status !== "ready" ? ( +
+ ) : ( + + )} + + )}
{ if (gen.fn) { - const fn_result = gen.fn(); + const fn_result = gen.fn(local.selected); let src = ""; if ( typeof fn_result === "object" && @@ -80,14 +137,15 @@ export const EdPropGen = () => { } else { src = fn_result; } - if (p.script.do_edit) p.script.do_edit(src); + if (p.script.do_edit) p.script.do_edit(src, true); + p.render(); } }} >
Generate
`, + __html: ``, }} >