fix options jsx

This commit is contained in:
Rizky 2024-03-29 15:35:39 +07:00
parent ddad7fcbec
commit 7582371142
6 changed files with 52 additions and 79 deletions

File diff suppressed because one or more lines are too long

View File

@ -70,6 +70,7 @@ const target = {
export const active = { export const active = {
should_render_main: true, should_render_main: true,
hover: { id: "" }, hover: { id: "" },
scope: {} as any,
script_nav: { script_nav: {
list: [] as { list: [] as {
item_id: string; item_id: string;

View File

@ -1,4 +1,4 @@
import { FC, MouseEvent } from "react"; import { FC, MouseEvent, useEffect } from "react";
import { useGlobal, useLocal } from "web-utils"; import { useGlobal, useLocal } from "web-utils";
import { IItem } from "../../../../utils/types/item"; import { IItem } from "../../../../utils/types/item";
import { FMCompDef, FNCompDef } from "../../../../utils/types/meta-fn"; import { FMCompDef, FNCompDef } from "../../../../utils/types/meta-fn";
@ -13,6 +13,8 @@ import { EdPropInstanceText } from "./prop-instance/prop-text";
import { EdStyleAll } from "./style/side-all"; import { EdStyleAll } from "./style/side-all";
import { EdPropInstanceButton } from "./prop-instance/prop-button"; import { EdPropInstanceButton } from "./prop-instance/prop-button";
const w = window as any;
export const EdSidePropInstance: FC<{ meta: IMeta }> = ({ meta }) => { export const EdSidePropInstance: FC<{ meta: IMeta }> = ({ meta }) => {
const p = useGlobal(EDGlobal, "EDITOR"); const p = useGlobal(EDGlobal, "EDITOR");
const local = useLocal( const local = useLocal(
@ -71,7 +73,7 @@ export const EdSidePropInstance: FC<{ meta: IMeta }> = ({ meta }) => {
if (visible && visible !== "true") { if (visible && visible !== "true") {
try { try {
const arg: any = {}; const arg: any = { ...active.scope };
if (meta.item.script?.props) { if (meta.item.script?.props) {
for (const [k, v] of Object.entries(meta.item.script?.props)) { for (const [k, v] of Object.entries(meta.item.script?.props)) {
eval(`arg.${k} = ${v.value}`); eval(`arg.${k} = ${v.value}`);
@ -93,6 +95,7 @@ export const EdSidePropInstance: FC<{ meta: IMeta }> = ({ meta }) => {
return; return;
} }
} catch (e) { } catch (e) {
console.log(e);
return; return;
} }
} }

View File

@ -17,7 +17,6 @@ export const EdPropInstanceButton: FC<{
labelClick?: React.MouseEventHandler<HTMLDivElement> | undefined; labelClick?: React.MouseEventHandler<HTMLDivElement> | undefined;
}> = ({ label, name, cprop, mprop, meta }) => { }> = ({ label, name, cprop, mprop, meta }) => {
const p = useGlobal(EDGlobal, "EDITOR"); const p = useGlobal(EDGlobal, "EDITOR");
const val = cprop?.valueBuilt;
const local = useLocal({ const local = useLocal({
value: [] as { value: [] as {
@ -28,16 +27,6 @@ export const EdPropInstanceButton: FC<{
timeout: null as any, timeout: null as any,
}); });
if (!w.prop_buttons) {
w.prop_buttons = {};
}
if (!w.prop_buttons[name]) {
w.prop_buttons[name] = {
meta: meta,
local: local,
src: val,
};
}
useEffect(() => { useEffect(() => {
return () => { return () => {

View File

@ -3,7 +3,7 @@ import { useGlobal, useLocal } from "web-utils";
import { apiProxy } from "../../../../../base/load/api/api-proxy"; import { apiProxy } from "../../../../../base/load/api/api-proxy";
import { dbProxy } from "../../../../../base/load/db/db-proxy"; import { dbProxy } from "../../../../../base/load/db/db-proxy";
import { FMCompDef, FNCompDef } from "../../../../../utils/types/meta-fn"; import { FMCompDef, FNCompDef } from "../../../../../utils/types/meta-fn";
import { EDGlobal, IMeta } from "../../../logic/ed-global"; import { EDGlobal, IMeta, active } from "../../../logic/ed-global";
import { treeRebuild } from "../../../logic/tree/build"; import { treeRebuild } from "../../../logic/tree/build";
import { EdPropLabel } from "./prop-label"; import { EdPropLabel } from "./prop-label";
import { ChevronDown } from "../../tree/node/item/indent"; import { ChevronDown } from "../../tree/node/item/indent";
@ -62,6 +62,7 @@ export const EdPropInstanceOptions: FC<{
...window.exports, ...window.exports,
db: p.script.db, db: p.script.db,
api: p.script.api, api: p.script.api,
...active.scope,
}; };
if (meta.item.script?.props) { if (meta.item.script?.props) {
@ -78,7 +79,7 @@ ${Object.entries(arg)
.map((e) => `const ${e[0]} = arg["${e[0]}"]`) .map((e) => `const ${e[0]} = arg["${e[0]}"]`)
.join(";\n")} .join(";\n")}
const resOpt = ${cprop.meta.optionsBuilt || cprop.meta.options}; const resOpt = ${cprop.meta.optionsBuilt || cprop.meta.options};
if (typeof resOpt === 'function') local.metaFn = resOpt; if (typeof resOpt === 'function') local.metaFn = resOpt;
else metaOptions = resOpt; else metaOptions = resOpt;
`); `);
} catch (e) { } catch (e) {
@ -90,11 +91,14 @@ else metaOptions = resOpt;
if (local.metaFn && !local.loaded && !local.loading) { if (local.metaFn && !local.loaded && !local.loading) {
local.loading = true; local.loading = true;
local.metaFn().then((e) => { const res = local.metaFn();
const callback = (e: any) => {
local.loading = false; local.loading = false;
local.loaded = e; local.loaded = e;
local.render(); local.render();
}); };
if (res instanceof Promise) res.then(callback);
else callback(res);
} }
} }

View File

@ -1,6 +1,6 @@
import { createId } from "@paralleldrive/cuid2"; import { createId } from "@paralleldrive/cuid2";
import { FMCompDef } from "../../../../utils/types/meta-fn"; import { FMCompDef } from "../../../../utils/types/meta-fn";
import { IMeta } from "../../../ed/logic/ed-global"; import { IMeta, active } from "../../../ed/logic/ed-global";
import { VG } from "../global"; import { VG } from "../global";
import { ViRender } from "../render"; import { ViRender } from "../render";
import { viScriptArg } from "./arg"; import { viScriptArg } from "./arg";
@ -152,38 +152,14 @@ export const viEvalProps = (
} }
} }
if (w.prop_buttons) { if (location.pathname.startsWith("/ed/") && active.item_id) {
for (const [k, v] of Object.entries(w.prop_buttons)) { if (meta.item.id === active.item_id) {
const btn = v as { active.scope = {};
meta: IMeta; for (const [k, v] of Object.entries(passprop)) {
src: string; active.scope[k] = v;
local: { value: any; render: () => void };
};
let found = false;
let cur = btn.meta;
while (cur && cur.parent) {
if (
(btn.meta.item.id === cur.item.id,
btn.meta.item.originalId === cur.item.originalId)
) {
found = true;
break;
}
cur = vi.meta[cur.parent.id];
} }
if (found) { active.scope.self_props = _props;
const curprops = { ...passprop, _props };
const fn = new Function(
...Object.keys(curprops),
`return ${btn.src}`
);
btn.local.value = fn(...Object.values(curprops));
setTimeout(btn.local.render, 300);
setTimeout(btn.local.render, 1000);
}
} }
} }
} }