This commit is contained in:
Rizky 2023-11-20 16:37:32 +07:00
parent f9e5f9389d
commit 0b0494c9a4
8 changed files with 139 additions and 9 deletions

View File

@ -8,6 +8,8 @@
"@paralleldrive/cuid2": "^2.2.2",
"@types/mime-types": "^2.1.2",
"esbuild": "^0.19.4",
"@swc/core": "^1.3.96",
"woodpile": "^0.0.5",
"lmdb": "^2.8.5",
"mime-types": "^2.1.35",
"msgpackr": "^1.9.9",

View File

@ -1,9 +1,5 @@
import { component, page } from "dbgen";
import {
EComp,
EPage,
ESite,
} from "../../../web/src/nova/ed/logic/ed-global";
import { EComp, EPage, ESite } from "../../../web/src/nova/ed/logic/ed-global";
import { IItem } from "../../../web/src/utils/types/item";
import { site_group } from "./actions/site_group";
import { activity } from "./entity/activity";
@ -92,4 +88,11 @@ export const SyncActions = {
info: async (client_ids: string[]) =>
({}) as Record<string, { id: string; username: string }>,
},
swc: {
parse: async (
arg:
| { page_id: string; item_id?: string }
| { comp_id: string; item_id?: string }
) => ({}) as Record<string, string>,
},
};

View File

@ -15,7 +15,6 @@
"@parcel/packager-wasm": "^2.10.1",
"@parcel/service-worker": "^2.10.1",
"@qiwi/deep-proxy": "^2.0.3",
"@swc/wasm-web": "1.3.96-nightly-20231025.1",
"algoliasearch": "^4.20.0",
"date-fns": "^2.30.0",
"dbgen": "workspace:*",

View File

@ -88,6 +88,7 @@ export const EDGlobal = {
| "ready",
sync: null as unknown as Awaited<ReturnType<typeof clientStartSync>>,
site: deepClone(EmptySite),
site_dts: "",
script: {
siteTypes: {} as Record<string, string>,
loaded: false,

View File

@ -1,13 +1,84 @@
import type { Editor as MonacoEditor, OnMount } from "@monaco-editor/react";
import type { OnMount } from "@monaco-editor/react";
import { createStore } from "idb-keyval";
import trim from "lodash.trim";
import { useEffect } from "react";
import { useGlobal, useLocal } from "web-utils";
import { jscript } from "../../../../../utils/script/jscript";
import { EDGlobal, active } from "../../../logic/ed-global";
import { jsMount } from "../../../../../utils/script/mount";
import { monacoTypings } from "../../../../../utils/script/typings";
export type MonacoEditor = Parameters<OnMount>[0];
export const ScriptMonaco = () => {
const p = useGlobal(EDGlobal, "EDITOR");
const local = useLocal({
editor: null as null | MonacoEditor,
reloading: false,
changeTimeout: 0 as any,
historyOpen: false,
idbstore: createStore(`prasi-page-${p.page.cur.id}`, "script-history"),
});
const Editor = jscript.editor;
if (!Editor) return null;
const meta = p.page.meta[active.item_id];
if (!meta) return null;
const item = meta.item;
const adv = item.adv || {};
const val: string = (
typeof adv[p.ui.popup.script.mode] === "string"
? adv[p.ui.popup.script.mode]
: ""
) as any;
const doEdit = async (newval: string, all?: boolean) => {
if (local.editor && jscript.prettier.standalone) {
const text = trim(
await jscript.prettier.standalone.format(
all
? newval
: local.editor?.getValue().replace(/\{\s*children\s*\}/gi, newval)
),
"; \n"
);
local.editor.executeEdits(null, [
{
range: {
startLineNumber: 0,
startColumn: 0,
endColumn: Number.MAX_SAFE_INTEGER,
endLineNumber: Number.MAX_SAFE_INTEGER,
},
text,
},
]);
}
};
let mitem = meta.mitem;
if (!mitem) {
active.item_id = "";
return <div>no mitem</div>;
} else if (
item.type === "item" &&
item.component?.id &&
meta.parent_comp?.mitem
) {
mitem = meta.parent_comp?.mitem;
if (!mitem) {
active.item_id = "";
return <div>no mitem</div>;
}
}
return (
<Editor
value={val}
options={{
minimap: { enabled: false },
wordWrap: "wordWrapColumn",
@ -18,6 +89,55 @@ export const ScriptMonaco = () => {
tabSize: 2,
useTabStops: true,
}}
onMount={async (editor, monaco) => {
local.editor = editor;
editor.focus();
setTimeout(() => {
editor.focus();
}, 300);
const value = editor.getValue();
if (p.ui.popup.script.mode === "js") {
monaco.editor.getModels().forEach((model) => {
if (model.uri.toString().startsWith("inmemory://model")) {
model.dispose();
}
});
let model = monaco.editor.createModel(
value,
"typescript",
monaco.Uri.parse(
`ts:${
p.comp.cur.id
? `comp-${p.comp.cur.id}`
: `page-${p.page.cur.id}`
}-${active.item_id}.tsx`
)
);
editor.setModel(model);
}
monaco.editor.registerEditorOpener({
openCodeEditor(source, resource, selectionOrPosition) {
// https://github.com/microsoft/vscode/pull/177064#issue-1623100628
console.log(source, resource, selectionOrPosition);
return false;
},
});
await jsMount(editor, monaco);
await monacoTypings(
{
site_dts: p.site_dts,
script: {
siteTypes: {},
},
site: p.site.config,
},
monaco,
{ types: {}, values: {} }
);
}}
/>
);
};

View File

@ -15,6 +15,7 @@ export const ScriptWorkbench = () => {
].map((e) => {
return (
<div
key={e.type}
className={cx(
css`
color: ${e.color};

View File

@ -13,7 +13,7 @@ export const EdTreeAction = ({
const item = node.data?.item;
if (!item) return null;
return (
<div className="flex items-center pr-2">
<div className="flex items-center pr-1">
<div
className={cx(
"border rounded-sm text-[9px] flex w-[20px] h-[15px] items-center cursor-pointer justify-center uppercase",
@ -41,7 +41,11 @@ export const EdTreeAction = ({
p.render();
}}
>
{"</>"}
<div
dangerouslySetInnerHTML={{
__html: `<svg width="12px" height="12px" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M9.96424 2.68571C10.0668 2.42931 9.94209 2.13833 9.6857 2.03577C9.4293 1.93322 9.13832 2.05792 9.03576 2.31432L5.03576 12.3143C4.9332 12.5707 5.05791 12.8617 5.3143 12.9642C5.5707 13.0668 5.86168 12.9421 5.96424 12.6857L9.96424 2.68571ZM3.85355 5.14646C4.04882 5.34172 4.04882 5.6583 3.85355 5.85356L2.20711 7.50001L3.85355 9.14646C4.04882 9.34172 4.04882 9.6583 3.85355 9.85356C3.65829 10.0488 3.34171 10.0488 3.14645 9.85356L1.14645 7.85356C0.951184 7.6583 0.951184 7.34172 1.14645 7.14646L3.14645 5.14646C3.34171 4.9512 3.65829 4.9512 3.85355 5.14646ZM11.1464 5.14646C11.3417 4.9512 11.6583 4.9512 11.8536 5.14646L13.8536 7.14646C14.0488 7.34172 14.0488 7.6583 13.8536 7.85356L11.8536 9.85356C11.6583 10.0488 11.3417 10.0488 11.1464 9.85356C10.9512 9.6583 10.9512 9.34172 11.1464 9.14646L12.7929 7.50001L11.1464 5.85356C10.9512 5.6583 10.9512 5.34172 11.1464 5.14646Z" fill="currentColor" fill-rule="evenodd" clip-rule="evenodd"></path></svg>`,
}}
></div>
</div>
</div>
);

BIN
bun.lockb

Binary file not shown.