wip fix constrain

This commit is contained in:
Rizky 2024-01-11 16:20:08 +07:00
parent 5522b5fa33
commit 5d64a656d8
7 changed files with 89 additions and 34 deletions

View File

@ -10,6 +10,7 @@
"@floating-ui/react": "^0.26.4", "@floating-ui/react": "^0.26.4",
"@leeoniya/ufuzzy": "^1.0.14", "@leeoniya/ufuzzy": "^1.0.14",
"@minoru/react-dnd-treeview": "^3.4.4", "@minoru/react-dnd-treeview": "^3.4.4",
"constrained-editor-plugin": "^1.3.0",
"@monaco-editor/react": "^4.6.0", "@monaco-editor/react": "^4.6.0",
"@paralleldrive/cuid2": "2.2.2", "@paralleldrive/cuid2": "2.2.2",
"react-contenteditable": "^3.3.7", "react-contenteditable": "^3.3.7",

View File

@ -32,6 +32,7 @@ export const edMonacoDefaultVal = (p: PG, adv: FNAdv, mitem: MItem) => {
const mprop = mprops.get(p.ui.popup.script.prop_name); const mprop = mprops.get(p.ui.popup.script.prop_name);
if (mprop) { if (mprop) {
const kind = p.ui.popup.script.prop_kind; const kind = p.ui.popup.script.prop_kind;
if (kind === "value") { if (kind === "value") {
val = mprop.get("value"); val = mprop.get("value");
} else if (kind === "gen") { } else if (kind === "gen") {

View File

@ -12,6 +12,9 @@ import { getActiveMeta } from "../../../logic/active/get-meta";
import { EDGlobal, IMeta, active } from "../../../logic/ed-global"; import { EDGlobal, IMeta, active } from "../../../logic/ed-global";
import { edMonacoDefaultVal } from "./default-val"; import { edMonacoDefaultVal } from "./default-val";
import { declareScope } from "./scope/scope"; import { declareScope } from "./scope/scope";
import { Loading } from "../../../../../utils/ui/loading";
// @ts-ignore
import { constrainedEditor } from "constrained-editor-plugin/dist/esm/constrainedEditor";
const scriptEdit = { const scriptEdit = {
timeout: null as any, timeout: null as any,
@ -57,6 +60,7 @@ export const EdScriptMonaco: FC<{}> = () => {
const monaco = local.monaco; const monaco = local.monaco;
if (monaco && editor) { if (monaco && editor) {
const type = p.ui.popup.script.type;
if (local.mode !== p.ui.popup.script.mode) { if (local.mode !== p.ui.popup.script.mode) {
local.init = false; local.init = false;
local.mode = p.ui.popup.script.mode; local.mode = p.ui.popup.script.mode;
@ -73,12 +77,14 @@ export const EdScriptMonaco: FC<{}> = () => {
}); });
if (meta) { if (meta) {
if (!meta.item.adv) { if (type === "item") {
meta.item.adv = {}; if (!meta.item.adv) {
} meta.item.adv = {};
}
if (!meta.item.adv[p.ui.popup.script.mode]) { if (!meta.item.adv[p.ui.popup.script.mode]) {
meta.item.adv[p.ui.popup.script.mode] = val; meta.item.adv[p.ui.popup.script.mode] = val;
}
} }
} }
@ -95,18 +101,33 @@ export const EdScriptMonaco: FC<{}> = () => {
{ types: {}, values: {} } { types: {}, values: {} }
); );
if (meta) { if (meta) {
if (p.ui.popup.script.type !== "prop-master") { let end_hide = 0;
if (type === "prop-master") {
const nmodel = monaco.editor.createModel(
val,
"typescript",
monaco.Uri.parse("file:///active.tsx")
);
editor.setModel(nmodel);
} else {
const imports = declareScope(p, meta, monaco); const imports = declareScope(p, meta, monaco);
let cur = active.comp_id ? active.comp_id : "page"; let cur = active.comp_id ? active.comp_id : "page";
monaco.editor.getModels().forEach((model) => { monaco.editor.getModels().forEach((model) => {
if ( if (
type === "item" &&
model.uri.path === `/${cur}_${active.item_id}_src_src.tsx` model.uri.path === `/${cur}_${active.item_id}_src_src.tsx`
) { ) {
editor.setModel(model); editor.setModel(model);
} }
});
if (
type === "prop-instance" &&
model.uri.path ===
`/${cur}_${active.item_id}_prop_${p.ui.popup.script.prop_name}.tsx`
) {
editor.setModel(model);
}
});
const model = editor.getModel(); const model = editor.getModel();
if (!model) { if (!model) {
const nmodel = monaco.editor.createModel( const nmodel = monaco.editor.createModel(
@ -116,27 +137,38 @@ export const EdScriptMonaco: FC<{}> = () => {
"typescript", "typescript",
monaco.Uri.parse("file:///active.tsx") monaco.Uri.parse("file:///active.tsx")
); );
editor.setModel(nmodel); editor.setModel(nmodel);
} }
if (imports) { if (imports) {
local.imports = imports; local.imports = imports;
const end = imports.split("\n").length + 1; end_hide = imports.split("\n").length + 1;
const range = new monaco.Range(1, 0, end, 0); const range = new monaco.Range(1, 0, end_hide, 0);
(editor as any).setHiddenAreas([range]); (editor as any).setHiddenAreas([range]);
} }
} else {
const nmodel = monaco.editor.createModel(
val,
"typescript",
monaco.Uri.parse("file:///active.tsx")
);
editor.setModel(nmodel);
} }
await jsMount(editor, monaco, p); await jsMount(editor, monaco, p);
if (type === "prop-instance") {
const constrainedInstance = constrainedEditor(monaco);
constrainedInstance.initializeIn(editor);
const model = editor.getModel();
constrainedInstance.removeRestrictionsIn(model);
const frange = model?.getFullModelRange();
if (frange) {
const ranges = [
{
range: [
end_hide + 1,
`export const ${p.ui.popup.script.prop_name} = `.length,
frange.getEndPosition().lineNumber,
frange.getEndPosition().column,
],
allowMultiline: true,
},
];
constrainedInstance.addRestrictionsTo(model, ranges);
}
}
} }
} else { } else {
const model = monaco.editor.createModel( const model = monaco.editor.createModel(
@ -219,9 +251,9 @@ export const EdScriptMonaco: FC<{}> = () => {
} }
val = edMonacoDefaultVal(p, adv, mitem); val = edMonacoDefaultVal(p, adv, mitem);
return ( return (
<Editor <Editor
loading={<Loading backdrop={false} note="monaco-loading" />}
options={{ options={{
minimap: { enabled: false }, minimap: { enabled: false },
wordWrap: "wordWrapColumn", wordWrap: "wordWrapColumn",
@ -236,20 +268,29 @@ export const EdScriptMonaco: FC<{}> = () => {
{ css: "scss", js: "typescript", html: "html" }[p.ui.popup.script.mode] { css: "scss", js: "typescript", html: "html" }[p.ui.popup.script.mode]
} }
onChange={(value) => { onChange={(value) => {
const stype = p.ui.popup.script.type;
if ((value || "").includes("/** IMPORT MODULE **/")) { if ((value || "").includes("/** IMPORT MODULE **/")) {
const valparts = (value || "").split("/** IMPORT MODULE **/\n"); const valparts = (value || "").split("/** IMPORT MODULE **/\n");
if (valparts.length === 2) local.value = valparts[1]; if (valparts.length === 2) local.value = valparts[1];
if (
stype === "prop-instance" &&
local.value.includes(
`export const ${p.ui.popup.script.prop_name} = `
)
) {
local.value = local.value.substring(
`export const ${p.ui.popup.script.prop_name} = `.length
);
}
} else { } else {
local.value = value || ""; local.value = value || "";
} }
local.render(); local.render();
clearTimeout(scriptEdit.timeout); clearTimeout(scriptEdit.timeout);
const applyChanges = async () => { const applyChanges = async () => {
const value = local.value; const value = local.value;
const meta = getActiveMeta(p); const meta = getActiveMeta(p);
const type = p.ui.popup.script.mode; const mode = p.ui.popup.script.mode;
if (meta && meta.mitem) { if (meta && meta.mitem) {
let arg = {} as any; let arg = {} as any;
if (active.comp_id) { if (active.comp_id) {
@ -257,9 +298,8 @@ export const EdScriptMonaco: FC<{}> = () => {
} else { } else {
arg.page_id = p.page.cur.id; arg.page_id = p.page.cur.id;
} }
let scope: boolean | ParsedScope = false; let scope: boolean | ParsedScope = false;
if (p.ui.popup.script.type === "prop-master") { if (stype === "prop-master") {
p.sync.code.edit({ p.sync.code.edit({
type: "prop-master", type: "prop-master",
prop_kind: p.ui.popup.script.prop_kind, prop_kind: p.ui.popup.script.prop_kind,
@ -267,10 +307,10 @@ export const EdScriptMonaco: FC<{}> = () => {
value: compress(encode.encode(value || "")), value: compress(encode.encode(value || "")),
...arg, ...arg,
}); });
} else if (p.ui.popup.script.type === "prop-instance") { } else if (stype === "prop-instance") {
scope = await p.sync.code.edit({ scope = await p.sync.code.edit({
type: "prop-instance", type: "prop-instance",
mode: type, mode: mode,
prop_name: p.ui.popup.script.prop_name, prop_name: p.ui.popup.script.prop_name,
item_id: active.item_id, item_id: active.item_id,
value: compress(encode.encode(value || "")), value: compress(encode.encode(value || "")),
@ -279,19 +319,17 @@ export const EdScriptMonaco: FC<{}> = () => {
} else { } else {
scope = await p.sync.code.edit({ scope = await p.sync.code.edit({
type: "adv", type: "adv",
mode: type, mode: mode,
item_id: active.item_id, item_id: active.item_id,
value: compress(encode.encode(value || "")), value: compress(encode.encode(value || "")),
...arg, ...arg,
}); });
} }
if (typeof scope === "object") { if (typeof scope === "object") {
meta.item.script = scope; meta.item.script = scope;
} }
} }
}; };
p.ui.popup.script.on_close = () => { p.ui.popup.script.on_close = () => {
clearTimeout(scriptEdit.timeout); clearTimeout(scriptEdit.timeout);
applyChanges(); applyChanges();

View File

@ -23,7 +23,7 @@ export const instantiate = (arg: {
newitem.component.props[k].value = item.component.props[k].value; newitem.component.props[k].value = item.component.props[k].value;
newitem.component.props[k].valueBuilt = newitem.component.props[k].valueBuilt =
item.component.props[k].valueBuilt; item.component.props[k].valueBuilt;
newitem.component.props[k].jsxCalledBy = newitem.component.props[k].jsxCalledBy =
item.component.props[k].jsxCalledBy; item.component.props[k].jsxCalledBy;
} }

View File

@ -51,6 +51,11 @@ export const viParts = (
props.children = children; props.children = children;
if (meta.item.adv?.js && !meta.item.adv.js.includes("children")) {
delete props.children;
delete props.dangerouslySetInnerHTML;
}
return { return {
props, props,
text_props, text_props,

View File

@ -69,7 +69,7 @@ export const viEvalScript = (
const fn = new Function( const fn = new Function(
...Object.keys(arg), ...Object.keys(arg),
`// ${meta.item.name}: ${meta.item.id} `// ${meta.item.name}: ${meta.item.id}
${meta.item.adv?.jsBuilt || ""} ${replaceWithObject(meta.item.adv?.jsBuilt || "", replacement) || ""}
` `
); );
fn(...Object.values(arg)); fn(...Object.values(arg));
@ -90,3 +90,13 @@ const JsxProp: FC<{
} }
return local.result; return local.result;
}; };
const replacement = {
"stroke-width": "strokeWidth",
};
const replaceWithObject = (tpl: string, data: any) => {
return tpl.replace(/\$\(([^\)]+)?\)/g, function ($1, $2) {
return data[$2];
});
};

BIN
bun.lockb

Binary file not shown.