This commit is contained in:
Rizky 2024-03-21 14:07:53 +07:00
parent d019c54776
commit 8b7422536c
3 changed files with 62 additions and 33 deletions

File diff suppressed because one or more lines are too long

View File

@ -16,6 +16,7 @@ import { declareScope } from "./scope/scope";
// @ts-ignore // @ts-ignore
import { FNCompDef } from "../../../../../utils/types/meta-fn"; import { FNCompDef } from "../../../../../utils/types/meta-fn";
import { editorLocalValue } from "../../../../vi/render/script/local";
const scriptEdit = { const scriptEdit = {
timeout: null as any, timeout: null as any,
@ -363,6 +364,8 @@ export const EdScriptMonaco: FC<{}> = () => {
scope = code_result; scope = code_result;
} }
} else { } else {
editorLocalValue[active.item_id] = null;
const code_result = await p.sync.code.edit({ const code_result = await p.sync.code.edit({
type: "adv", type: "adv",
mode: mode, mode: mode,

View File

@ -4,6 +4,8 @@ import { updatePropScope } from "./eval-prop";
import { modifyChild } from "./passprop"; import { modifyChild } from "./passprop";
import { VG } from "../global"; import { VG } from "../global";
export const editorLocalValue = {} as Record<string, any>;
export const createViLocal = ( export const createViLocal = (
vi: { vi: {
layout: VG["layout"]; layout: VG["layout"];
@ -23,10 +25,17 @@ export const createViLocal = (
hook?: (local: T) => void; hook?: (local: T) => void;
effect?: (local: T) => void | Promise<void>; effect?: (local: T) => void | Promise<void>;
}) => { }) => {
const isEditor =
["localhost", "prasi.avolut.com"].includes(location.hostname) &&
location.pathname.startsWith("/ed/");
let id = meta.item.id;
const { children } = arg; const { children } = arg;
const init_local_effect = vi.script?.init_local_effect; const init_local_effect = vi.script?.init_local_effect;
const metas = is_layout ? vi.layout?.meta : vi.meta; const metas = is_layout ? vi.layout?.meta : vi.meta;
const ref = useRef<any>(arg.value); const ref = useRef<any>(
editorLocalValue[id] ? editorLocalValue[id] : arg.value
);
const local = ref.current; const local = ref.current;
local.render = meta.render; local.render = meta.render;
@ -37,8 +46,6 @@ export const createViLocal = (
} }
useEffect(() => { useEffect(() => {
let id = meta.item.id;
if (meta.parent?.instance_id && metas) { if (meta.parent?.instance_id && metas) {
const parent_meta = metas[meta.parent?.instance_id]; const parent_meta = metas[meta.parent?.instance_id];
if (parent_meta && parent_meta.instances) { if (parent_meta && parent_meta.instances) {
@ -61,6 +68,9 @@ export const createViLocal = (
const fn = async () => { const fn = async () => {
if (arg.effect) { if (arg.effect) {
await arg.effect(local); await arg.effect(local);
if (isEditor) {
editorLocalValue[id] = local;
}
} }
}; };
@ -70,6 +80,22 @@ export const createViLocal = (
return () => {}; return () => {};
}, [location.pathname]); }, [location.pathname]);
useEffect(() => {
if (isEditor) {
if (editorLocalValue[id] === null) {
const fn = async () => {
if (arg.effect) {
await arg.effect(local);
if (isEditor) {
editorLocalValue[id] = local;
}
}
};
fn();
}
}
}, [editorLocalValue[id]]);
return modifyChild(children, { return modifyChild(children, {
...meta.script?.scope, ...meta.script?.scope,
[arg.name]: local, [arg.name]: local,