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 {
const { comp_id, prop_kind, prop_name, value } = arg;
const { comp_id, prop_kind, prop_name } = arg;
if (comp_id) {
const ref = docs.comp[comp_id];
if (ref) {

View File

@ -15,7 +15,7 @@ export const apiProxy = (api_url: string) => {
const base_url = `${base.protocol}//${base.host}`;
if (!w.prasiApi[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 {
const res = await fetch(`/_proxy`, {
method: "POST",
body: JSON.stringify({
body: JSON.stringify([
{
url,
body,
headers,
}),
},
]),
headers: { "content-type": "application/json" },
});
return res.json();

View File

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

View File

@ -21,7 +21,11 @@ export const EmptySite = {
config: { api_url: "" },
js: "",
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;

View File

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

View File

@ -2,10 +2,12 @@ import { viLoadLegacy } from "../../vi/load/load-legacy";
import { ESite, PG } from "./ed-global";
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 layout_changed = p.site.layout.id !== site.layout.id;
p.site = site;
if (layout_changed) {
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.sync.site.load(params.site_id).then(async (site) => {
if (site) {
await loadSite(p, site);
await loadSite(p, site, "from-sync");
p.render();
} else {
alert("Site not found. redirecting...");
location.href = `/ed/`;
}
return;
});
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;
}
}
const def = m.scope.def;
if (def) {
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) {
Object.entries(def.passprop).map(([e, v]) => {
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};`,
});
}
});
} else if (def.props) {
}
}
}

View File

@ -1,3 +1,4 @@
import { loadApiProxyDef } from "../../../base/load/api/api-proxy-def";
import importModule from "../../../render/editor/tools/dynamic-import";
import { viScriptArg } from "../render/script/arg";
@ -38,7 +39,7 @@ export const viLoadLegacy = async (vi: {
let api_url = vi.site.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`;
await importModule(path);

View File

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

View File

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