fix prettier

This commit is contained in:
Rizky 2023-10-15 12:41:35 +07:00
parent a295b2ead8
commit 5f1855780b
10 changed files with 178 additions and 38 deletions

View File

@ -3,7 +3,7 @@ import { page } from "web-utils";
import { Loading } from "../../utils/ui/loading"; import { Loading } from "../../utils/ui/loading";
export default page({ export default page({
url: "*", url: "**",
component: ({}) => { component: ({}) => {
useEffect(() => { useEffect(() => {
if (localStorage.getItem("prasi-session")) { if (localStorage.getItem("prasi-session")) {

View File

@ -1,10 +1,131 @@
import { page } from "web-utils"; import { FC, useEffect } from "react";
import { page, useGlobal, useLocal } from "web-utils";
import { EditorGlobal } from "../../render/editor/logic/global";
import { Loading } from "../../utils/ui/loading"; import { Loading } from "../../utils/ui/loading";
export default page({ export default page({
url: "/ed/:site_id/:page_id", url: "/editor/**",
component: ({}) => { component: ({}) => {
location.href = `/editor/${params.site_id}/${params.page_id}`; const p = useGlobal(EditorGlobal, "EDITOR");
return <Loading />;
const local = useLocal({
loading: true,
session: null as any,
notfound: false,
init: false,
});
const site_id = params.site_id === "_" ? "" : params.site_id;
const page_id = params.page_id === "_" ? "" : params.page_id;
useEffect(() => {
if (!local.init) {
(async () => {
let ses: any = null;
try {
ses = JSON.parse(localStorage.getItem("prasi-session") || "");
} catch (e) {}
await new Promise<void>(async (done) => {
try {
if (!!ses) {
done();
}
let e = await api.session();
if (!e) {
(window as any).redirectTo = location.pathname;
console.log("session not found");
// navigate("/login");
localStorage.removeItem("prasi-session");
} else {
localStorage.setItem("prasi-session", JSON.stringify(e));
}
if (!ses) {
ses = e;
done();
}
} catch (e) {
console.error(e);
}
});
if (ses) {
local.session = ses;
if (!site_id) {
const res = await db.site.findFirst({
where: {
is_deleted: false,
org: {
org_user: {
some: { id_user: ses.data.user.id },
},
},
},
select: {
id: true,
},
});
if (res) {
const page = await db.page.findFirst({
where: {
id_site: res.id,
is_deleted: false,
},
select: {
id: true,
},
});
if (page) {
local.loading = false;
local.render();
navigate(`/editor/${res.id}/${page.id}`);
return;
}
} else {
local.loading = false;
local.render();
return;
}
} else if (!page_id) {
let res = await db.page.findFirst({
where: {
id_site: site_id,
is_deleted: false,
},
select: {
id: true,
},
});
if (!res) {
res = await db.page.create({
data: {
content_tree: {
childs: [],
id: "root",
type: "root",
},
name: "home",
url: "/",
id_site: site_id,
},
});
}
if (res) {
local.loading = false;
local.render();
navigate(`/editor/${site_id}/${res.id}`);
return;
}
}
}
navigate(`/editor/${site_id}/${page_id}`);
})();
}
}, [local.init]);
return <Loading note="base-page" />;
}, },
}); });

View File

@ -11,11 +11,11 @@ export const auth_register = {
page: () => import("./page/auth/register"), page: () => import("./page/auth/register"),
}; };
export const all = { export const all = {
url: "*", url: "**",
page: () => import("./page/all"), page: () => import("./page/all"),
}; };
export const ed = { export const ed = {
url: "/ed/:site_id/:page_id", url: "/editor/**",
page: () => import("./page/ed"), page: () => import("./page/ed"),
}; };
export const editor = { export const editor = {

View File

@ -8,7 +8,7 @@ export const Root: FC<{}> = ({}) => {
const local = useLocal( const local = useLocal(
{ {
router: createRouter<{ url: string; Page: FC<any> }>({ router: createRouter<{ url: string; Page: FC<any> }>({
strictTrailingSlash: false, strictTrailingSlash: true,
}), }),
Page: null as any, Page: null as any,
}, },
@ -31,7 +31,6 @@ export const Root: FC<{}> = ({}) => {
const Provider = GlobalContext.Provider as FC<{ value: any; children: any }>; const Provider = GlobalContext.Provider as FC<{ value: any; children: any }>;
const found = local.router.lookup(location.pathname); const found = local.router.lookup(location.pathname);
console.log(found)
if (found) { if (found) {
w.params = found.params; w.params = found.params;

View File

@ -32,7 +32,6 @@ export const w = window as unknown as {
}; };
export const initEditor = async (p: PG, site_id: string) => { export const initEditor = async (p: PG, site_id: string) => {
w.isEditor = true; w.isEditor = true;
if (typeof w.isLayout === "undefined") { if (typeof w.isLayout === "undefined") {
w.isLayout = false; w.isLayout = false;
@ -77,6 +76,7 @@ export const initEditor = async (p: PG, site_id: string) => {
return site; return site;
}; };
const processSite = async (site: LSite) => { const processSite = async (site: LSite) => {
if (!site || (site && !site.id)) return;
if (!w.exports) { if (!w.exports) {
w.exports = {}; w.exports = {};
} }
@ -149,7 +149,7 @@ export const initEditor = async (p: PG, site_id: string) => {
}; };
export const execSiteJS = (p: PG) => { export const execSiteJS = (p: PG) => {
if (p) { if (p && p.site.api_url) {
p.script.siteTypes = {}; p.script.siteTypes = {};
const scope: any = { const scope: any = {
types: p.script.siteTypes, types: p.script.siteTypes,

View File

@ -38,6 +38,7 @@ const w = window as unknown as {
importCache: { importCache: {
prettier: any; prettier: any;
prettier_parser: any; prettier_parser: any;
prettier_estree: any;
}; };
}; };
@ -87,18 +88,28 @@ export const ScriptMonacoElement: FC<{
const doEdit = async (newval: string, all?: boolean) => { const doEdit = async (newval: string, all?: boolean) => {
if (local.editor) { if (local.editor) {
if (!w.importCache) { if (!w.importCache) {
w.importCache = { prettier_parser: "", prettier: "" }; w.importCache = {
prettier_parser: "",
prettier: "",
prettier_estree: "",
};
} }
if (!w.importCache.prettier)
w.importCache.prettier = (await import("prettier/standalone")).default;
if (!w.importCache.prettier_parser) if (!w.importCache.prettier_parser)
w.importCache.prettier_parser = await import( w.importCache.prettier_parser = await import(
"prettier/plugins/typescript" "prettier/plugins/typescript"
); );
if (!w.importCache.prettier) if (!w.importCache.prettier_estree)
w.importCache.prettier = await import("prettier/standalone"); w.importCache.prettier_estree = await import("prettier/plugins/estree");
const prettier = w.importCache.prettier; const prettier = w.importCache.prettier;
const prettier_parser = w.importCache.prettier_parser; const prettier_parser = w.importCache.prettier_parser;
const prettier_estree = w.importCache.prettier_estree;
const text = trim( const text = trim(
prettier.format( prettier.format(
all all
@ -106,7 +117,7 @@ export const ScriptMonacoElement: FC<{
: local.editor?.getValue().replace(/\{\s*children\s*\}/gi, newval), : local.editor?.getValue().replace(/\{\s*children\s*\}/gi, newval),
{ {
parser: "typescript", parser: "typescript",
plugins: [prettier_parser], plugins: [prettier_parser, prettier_estree],
} }
), ),
"; \n" "; \n"

View File

@ -17,6 +17,7 @@ const w = window as unknown as {
importCache: { importCache: {
prettier: any; prettier: any;
prettier_parser: any; prettier_parser: any;
prettier_estree: any;
}; };
}; };
@ -52,22 +53,34 @@ export const jsMount = async (p: PG, editor: MonacoEditor, monaco: Monaco) => {
monaco.languages.registerDocumentFormattingEditProvider("typescript", { monaco.languages.registerDocumentFormattingEditProvider("typescript", {
async provideDocumentFormattingEdits(model, options, token) { async provideDocumentFormattingEdits(model, options, token) {
if (!w.importCache) { if (!w.importCache) {
w.importCache = { prettier_parser: "", prettier: "" }; w.importCache = {
prettier_parser: "",
prettier: "",
prettier_estree: "",
};
} }
if (!w.importCache.prettier_parser)
w.importCache.prettier_parser = await import(
"prettier/plugins/typescript"
);
if (!w.importCache.prettier) if (!w.importCache.prettier)
w.importCache.prettier = await import("prettier/standalone"); w.importCache.prettier = (await import("prettier/standalone")).default;
if (!w.importCache.prettier_parser)
(w.importCache.prettier_parser = await import(
"prettier/plugins/typescript"
)).default;
if (!w.importCache.prettier_estree)
w.importCache.prettier_estree = (
await import("prettier/plugins/estree")
).default;
const prettier = w.importCache.prettier; const prettier = w.importCache.prettier;
const prettier_parser = w.importCache.prettier_parser; const prettier_parser = w.importCache.prettier_parser;
const prettier_estree = w.importCache.prettier_estree;
const text = trim( const text = trim(
prettier.format(model.getValue(), { await prettier.format(model.getValue(), {
parser: "typescript", parser: "typescript",
plugins: [prettier_parser], plugins: [prettier_parser, prettier_estree],
}), }),
"; \n" "; \n"
); );

View File

@ -19,7 +19,9 @@ export const createAPI = (url: string) => {
if (!w.prasiApi) { if (!w.prasiApi) {
w.prasiApi = {}; w.prasiApi = {};
} }
if (!url) {
throw new Error("s")
}
return w.apiClient(w.prasiApi[url]?.apiEntry, url); return w.apiClient(w.prasiApi[url]?.apiEntry, url);
}; };

View File

@ -30,17 +30,7 @@ export const _ = {
await genPages(); await genPages();
watch(pagedir, async (event, filename) => { watch(pagedir, async (event, filename) => {
const s = file(dir.path(`${pagedir}/${filename}`)); const s = file(dir.path(`${pagedir}/${filename}`));
if (s.size > 0) { genPages();
// await Bun.write(
// `app/srv/api/${filename}`,
// `\
// export const all = {
// url: "*",
// page: () => import("./page/all"),
// };
// `
// );
}
}); });
}; };

View File

@ -190,10 +190,14 @@ export const fetchSendApi = async (
if (!frm) { if (!frm) {
await waitUntil(() => { await waitUntil(() => {
frm = win.frmapi[w.serverurl]; frm = win.frmapi[base];
return frm; return frm;
}); });
} }
if (url.pathname.startsWith("//")) {
url.pathname = url.pathname.substring(1);
}
return await frm.send(url.pathname, params, win.apiHeaders); return await frm.send(url.pathname, params, win.apiHeaders);
}; };