This commit is contained in:
Rizky 2023-12-21 15:54:59 +07:00
parent b4264b727e
commit 44759a8fb1
14 changed files with 172 additions and 52 deletions

View File

@ -75,7 +75,7 @@ export const code_edit: SAction["code"]["edit"] = async function (
} }
} }
} else { } else {
const { comp_id, prop_kind, prop_name, value } = arg; const { comp_id, prop_kind, prop_name } = arg;
if (comp_id) { if (comp_id) {
const ref = docs.comp[comp_id]; const ref = docs.comp[comp_id];
if (ref) { if (ref) {

View File

@ -15,7 +15,7 @@ export const apiProxy = (api_url: string) => {
const base_url = `${base.protocol}//${base.host}`; const base_url = `${base.protocol}//${base.host}`;
if (!w.prasiApi[base_url]) { if (!w.prasiApi[base_url]) {
if (!apiProxyPending[base_url]) { if (!apiProxyPending[base_url]) {
apiProxyPending[base_url] = loadApiProxyDef(base_url, false); throw new Error(`API Definition not found: ${base_url}`);
} }
} }

View File

@ -66,11 +66,13 @@ export const fetchViaProxy = async (
} else { } else {
const res = await fetch(`/_proxy`, { const res = await fetch(`/_proxy`, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify([
{
url, url,
body, body,
headers, headers,
}), },
]),
headers: { "content-type": "application/json" }, headers: { "content-type": "application/json" },
}); });
return res.json(); return res.json();

View File

@ -1,6 +1,7 @@
import { Root as ReactRoot, createRoot } from "react-dom/client"; import { Root as ReactRoot, createRoot } from "react-dom/client";
import { defineReact, defineWindow } from "web-utils"; import { defineReact, defineWindow } from "web-utils";
import { apiProxy } from "./base/load/api/api-proxy"; import { apiProxy } from "./base/load/api/api-proxy";
import { loadApiProxyDef } from "./base/load/api/api-proxy-def";
import { dbProxy } from "./base/load/db/db-proxy"; import { dbProxy } from "./base/load/db/db-proxy";
import { Root } from "./base/root"; import { Root } from "./base/root";
import "./index.css"; import "./index.css";
@ -18,6 +19,8 @@ const start = async () => {
const cur = new URL(location.href); const cur = new URL(location.href);
const base_url = `${cur.protocol}//${cur.host}`; const base_url = `${cur.protocol}//${cur.host}`;
w.db = dbProxy(base_url); w.db = dbProxy(base_url);
await loadApiProxyDef(base_url, false);
w.api = apiProxy(base_url); w.api = apiProxy(base_url);
w.serverurl = base; w.serverurl = base;

View File

@ -21,7 +21,11 @@ export const EmptySite = {
config: { api_url: "" }, config: { api_url: "" },
js: "", js: "",
js_compiled: "", js_compiled: "",
layout: { id: "", meta: {} as Record<string, IMeta>, entry: [] as string[] }, layout: {
id: "--",
meta: {} as Record<string, IMeta>,
entry: [] as string[],
},
}; };
export type ESite = typeof EmptySite; export type ESite = typeof EmptySite;

View File

@ -15,7 +15,7 @@ export const edRoute = async (p: PG) => {
return; return;
} }
await loadSite(p, site); await loadSite(p, site, "from-route");
} }
if ( if (

View File

@ -2,10 +2,12 @@ import { viLoadLegacy } from "../../vi/load/load-legacy";
import { ESite, PG } from "./ed-global"; import { ESite, PG } from "./ed-global";
import { reloadPage } from "./ed-route"; import { reloadPage } from "./ed-route";
export const loadSite = async (p: PG, site: ESite) => { export const loadSite = async (p: PG, site: ESite, note: string) => {
console.log("note", note);
const old_layout_id = p.site.layout.id; const old_layout_id = p.site.layout.id;
const layout_changed = p.site.layout.id !== site.layout.id; const layout_changed = p.site.layout.id !== site.layout.id;
p.site = site; p.site = site;
if (layout_changed) { if (layout_changed) {
const old_layout = p.page.list[old_layout_id]; const old_layout = p.page.list[old_layout_id];

View File

@ -31,12 +31,13 @@ export const edInitSync = (p: PG) => {
p.ui.popup.code.init = false; p.ui.popup.code.init = false;
p.sync.site.load(params.site_id).then(async (site) => { p.sync.site.load(params.site_id).then(async (site) => {
if (site) { if (site) {
await loadSite(p, site); await loadSite(p, site, "from-sync");
p.render(); p.render();
} else { } else {
alert("Site not found. redirecting..."); alert("Site not found. redirecting...");
location.href = `/ed/`; location.href = `/ed/`;
} }
return;
}); });
return false; return false;
} }

View File

@ -0,0 +1,118 @@
import { IMeta, PG } from "../../../../logic/ed-global";
export const extractExportImport = (p: PG, m: IMeta, imports: string[]) => {
let _export = {};
const def = m.scope.def;
if (def) {
if (def.local) {
const local = extractLocal(p, m, def, imports);
if (local) {
for (const [k, v] of Object.entries(local)) {
v.names.forEach((n) =>
imports.push(`import { ${n} } from "./${k}";`)
);
}
}
}
}
return _export;
};
const extractLocal = (
p: PG,
m: IMeta,
def: IMeta["scope"]["def"],
imports: string[]
) => {
if (def?.local) {
let loc = {
item_id: m.item.id,
comp_id: m.parent?.comp_id,
type: "item",
};
let filename = `ts:scope~${JSON.stringify(loc)}.d.ts`;
return {
[filename]: {
names: [def.local.name],
src: `\
${imports.join("\n")}
type _local = ${def.local.value};
export const ${def.local.name}: _local & { render: () =>void };
`,
},
};
}
};
const extractPassProp = (
p: PG,
m: IMeta,
def: IMeta["scope"]["def"],
imports: string[]
) => {
if (def?.passprop) {
let loc = {
item_id: m.item.id,
comp_id: m.parent?.comp_id,
type: "item",
};
let filename = `ts:scope~${JSON.stringify(loc)}.d.ts`;
const result = {
names: [] as string[],
src: "",
};
const exports: string[] = [];
for (const [e, v] of Object.entries(def.passprop)) {
if (e !== "idx" && e !== "key") {
result.names.push(e);
exports.push(`export const ${e} = ${v.value};`);
}
}
result.src = `\
${imports.join("\n")}
${exports.join("\n")}
`;
return { [filename]: result };
}
};
const extractProps = (
p: PG,
m: IMeta,
def: IMeta["scope"]["def"],
imports: string[]
) => {
if (def?.passprop) {
let loc = {
item_id: m.item.id,
comp_id: m.parent?.comp_id,
type: "item",
};
let filename = `ts:scope~${JSON.stringify(loc)}.d.ts`;
const result = {
names: [] as string[],
src: "",
};
const exports: string[] = [];
for (const [e, v] of Object.entries(def.passprop)) {
if (e !== "idx" && e !== "key") {
result.names.push(e);
exports.push(`export const ${e} = ${v.value};`);
}
}
result.src = `\
${imports.join("\n")}
${exports.join("\n")}
`;
return { [filename]: result };
}
};

View File

@ -1,3 +0,0 @@
export const genImport = () => {
return ``;
};

View File

@ -26,36 +26,12 @@ export const defineScopeParent = (p: PG, meta: IMeta, monaco: Monaco) => {
m.scope.def.props = meta.scope?.def?.props; m.scope.def.props = meta.scope?.def?.props;
} }
} }
const def = m.scope.def; const def = m.scope.def;
if (def) { if (def) {
if (def.local) { if (def.local) {
addScope(p, "local", {
monaco,
loc: {
item_id: m.item.id,
comp_id: m.parent?.comp_id,
type: "item",
},
source: `\
type _local = ${def.local.value};
export const ${def.local.name}: _local & { render: () =>void };
`,
});
} else if (def.passprop) { } else if (def.passprop) {
Object.entries(def.passprop).map(([e, v]) => { } else if (def.props) {
if (e !== "idx" && e !== "key") {
addScope(p, "passprop", {
monaco,
loc: {
item_id: m.item.id,
comp_id: m.parent?.comp_id,
type: "item",
},
source: `\
export const ${e} = ${v.value};`,
});
}
});
} }
} }
} }

View File

@ -1,3 +1,4 @@
import { loadApiProxyDef } from "../../../base/load/api/api-proxy-def";
import importModule from "../../../render/editor/tools/dynamic-import"; import importModule from "../../../render/editor/tools/dynamic-import";
import { viScriptArg } from "../render/script/arg"; import { viScriptArg } from "../render/script/arg";
@ -38,7 +39,7 @@ export const viLoadLegacy = async (vi: {
let api_url = vi.site.api_url; let api_url = vi.site.api_url;
if (!api_url) api_url = ((site.config as any) || {}).api_url || ""; if (!api_url) api_url = ((site.config as any) || {}).api_url || "";
// await initApi(site.config); await loadApiProxyDef(api_url, true);
const path = `/npm/site/${vi.site.id}/site.js`; const path = `/npm/site/${vi.site.id}/site.js`;
await importModule(path); await importModule(path);

View File

@ -8,11 +8,18 @@ export const _ = {
headers: any; headers: any;
body: any; body: any;
}) { }) {
const res = await fetch(arg.url, { const res = await fetch(
method: arg.method, arg.url,
arg.body
? {
method: arg.method || "POST",
headers: arg.headers, headers: arg.headers,
body: arg.body, body: arg.body,
}); }
: {
headers: arg.headers,
}
);
return res as any; return res as any;
}, },
}; };

View File

@ -11,20 +11,29 @@ export const serveAPI = {
}, },
serve: async (url: URL, req: Request) => { serve: async (url: URL, req: Request) => {
let found = g.router.lookup(url.pathname); let found = g.router.lookup(url.pathname);
let found_not_match = false;
if (!found?.url) { if (!found?.url) {
if (!url.pathname.endsWith("/")) { let pathname = url.pathname;
found = g.router.lookup(url.pathname + "/"); if (!pathname.endsWith("/")) {
pathname = pathname + "/";
found = g.router.lookup(pathname);
found_not_match = true;
}
if (!pathname.endsWith("_")) {
found = g.router.lookup(pathname + "_");
found_not_match = true;
} }
if (!found?.url) { if (!found?.url) {
found = null; found = null;
} }
} }
if (found) { if (found) {
const params = { ...found.params }; const params = { ...found.params };
let args = found.args.map((e) => { let args = found_not_match
? []
: found.args.map((e) => {
return params[e]; return params[e];
}); });