This commit is contained in:
Rizky 2023-12-29 15:54:21 +07:00
parent f32bdf7d62
commit 624758dc69
5 changed files with 105 additions and 46 deletions

View File

@ -7,6 +7,8 @@ import { EDGlobal, PG, active } from "../../../logic/ed-global";
import { EdCompPreviewTree } from "./comp-preview-tree"; import { EdCompPreviewTree } from "./comp-preview-tree";
import { compPicker, reloadCompPicker } from "./comp-reload"; import { compPicker, reloadCompPicker } from "./comp-reload";
import { loadComponent } from "../../../logic/comp/load"; import { loadComponent } from "../../../logic/comp/load";
import { NodeModel } from "@minoru/react-dnd-treeview";
import { CompItem } from "./comp-tree";
export const EdCompPreview = () => { export const EdCompPreview = () => {
const p = useGlobal(EDGlobal, "EDITOR"); const p = useGlobal(EDGlobal, "EDITOR");
@ -23,6 +25,23 @@ export const EdCompPreview = () => {
p.render(); p.render();
}); });
} }
let found = compPicker.tree.find((e) => e.id === comp_id);
if (!found) {
found = compPicker.trash.find((e) => e.id === comp_id);
}
if (found) {
const root = ref.tree.find((e) => e.parent === "root");
if (root) {
if (root.text !== found.text) {
found.text = root.text;
db.component.update({
where: { id: comp_id },
data: { name: found.text },
});
}
}
}
}, [comp_id]); }, [comp_id]);
const isTrashed = !!compPicker.trash.find((e) => e.id === comp_id); const isTrashed = !!compPicker.trash.find((e) => e.id === comp_id);

View File

@ -1,5 +1,5 @@
import { NodeModel } from "@minoru/react-dnd-treeview"; import { NodeModel } from "@minoru/react-dnd-treeview";
import { PG } from "../../../logic/ed-global"; import { IMeta, PG } from "../../../logic/ed-global";
import { CompItem } from "./comp-tree"; import { CompItem } from "./comp-tree";
export const compPicker = { export const compPicker = {
@ -57,6 +57,24 @@ export const reloadCompPicker = async (p: PG) => {
? compPicker.tree ? compPicker.tree
: compPicker.trash; : compPicker.trash;
if (p.comp.list[comp.id]) {
const tree = p.comp.list[comp.id].tree;
if (tree) {
const root = tree.find(
(e) => e.parent === "root"
) as NodeModel<IMeta>;
if (root) {
if (root.data?.item.name && comp.name !== root.data?.item.name) {
comp.name = root.data.item.name;
db.component.update({
where: { id: comp.id },
data: { name: comp.name },
});
}
}
}
}
tree.push({ tree.push({
id: comp.id, id: comp.id,
parent: comp.id_component_group, parent: comp.id_component_group,

View File

@ -55,7 +55,7 @@ export const EdTreeCtxMenu = ({
const comp = (item as IItem).component as FNComponent | undefined; const comp = (item as IItem).component as FNComponent | undefined;
const isComponent = comp?.id; const isComponent = comp?.id;
const isActiveComponent = active.comp_id === comp?.id; const isActiveComponent = active.comp_id === comp?.id;
const isJSXProp = node.data?.jsx_prop?.is_root;
if (!item) { if (!item) {
return ( return (
<Menu mouseEvent={event} onClose={onClose}> <Menu mouseEvent={event} onClose={onClose}>
@ -69,19 +69,22 @@ export const EdTreeCtxMenu = ({
return ( return (
<Menu mouseEvent={event} onClose={onClose}> <Menu mouseEvent={event} onClose={onClose}>
{type === "item" && !isActiveComponent && !item.component?.id && ( {type === "item" &&
<MenuItem !isActiveComponent &&
label="Attach Component" !isJSXProp &&
onClick={() => edActionAttach(p, item)} !item.component?.id && (
/> <MenuItem
)} label="Attach Component"
onClick={() => edActionAttach(p, item)}
/>
)}
{type === "item" && comp?.id && !isActiveComponent && ( {type === "item" && comp?.id && !isActiveComponent && (
<MenuItem <MenuItem
label="Detach Component" label="Detach Component"
onClick={() => edActionDetach(p, item)} onClick={() => edActionDetach(p, item)}
/> />
)} )}
{type === "item" && !comp?.id && ( {type === "item" && !comp?.id && !isJSXProp && (
<MenuItem <MenuItem
label="Create Component" label="Create Component"
onClick={(e) => edActionNewComp(p, item, e)} onClick={(e) => edActionNewComp(p, item, e)}
@ -91,35 +94,46 @@ export const EdTreeCtxMenu = ({
label={item.hidden ? "Unhide" : "Hide"} label={item.hidden ? "Unhide" : "Hide"}
onClick={() => edActionHide(p, item)} onClick={() => edActionHide(p, item)}
/> />
<MenuItem {!isJSXProp && (
label="Rename" <MenuItem
hotKey={"↵"} label="Rename"
onClick={() => edActionRename(p, item)} hotKey={"↵"}
/> onClick={() => edActionRename(p, item)}
<MenuItem />
label="Cut" )}
// hotKey={<HotKey shortcut={"X"} />} {!isJSXProp && (
onClick={() => edActionCut(p, item)} <MenuItem
/> label="Cut"
{!isActiveComponent && ( // hotKey={<HotKey shortcut={"X"} />}
onClick={() => edActionCut(p, item)}
/>
)}
{!isActiveComponent && !isJSXProp && (
<MenuItem <MenuItem
label="Delete" label="Delete"
hotKey="⌫" hotKey="⌫"
onClick={() => edActionDelete(p, item)} onClick={() => edActionDelete(p, item)}
/> />
)} )}
<MenuItem label="Clone" onClick={() => edActionClone(p, item)} />
<MenuItem label="Copy" onClick={() => edActionCopy(p, item)} /> {!isJSXProp && (
<MenuItem label="Clone" onClick={() => edActionClone(p, item)} />
)}
{!isJSXProp && (
<MenuItem label="Copy" onClick={() => edActionCopy(p, item)} />
)}
{local.allowCopy && {local.allowCopy &&
local.allowPaste && local.allowPaste &&
!isComponent && !isComponent &&
item.type !== "text" && ( item.type !== "text" && (
<MenuItem label="Paste" onClick={() => edActionPaste(p, item)} /> <MenuItem label="Paste" onClick={() => edActionPaste(p, item)} />
)} )}
{["text", "item"].includes(item.type) && ( {["text", "item"].includes(item.type) && !isJSXProp && (
<MenuItem label="Wrap" onClick={() => edActionWrap(p, item as IItem)} /> <MenuItem label="Wrap" onClick={() => edActionWrap(p, item as IItem)} />
)} )}
{["item"].includes(item.type) && !isComponent && ( {["item"].includes(item.type) && !isJSXProp && !isComponent && (
<MenuItem <MenuItem
label="Unwrap" label="Unwrap"
onClick={() => edActionUnwrap(p, item as IItem)} onClick={() => edActionUnwrap(p, item as IItem)}

View File

@ -3,6 +3,7 @@ import { FC, useEffect } from "react";
import { useGlobal, useLocal } from "web-utils"; import { useGlobal, useLocal } from "web-utils";
import { EDGlobal, IMeta, active } from "../../../../logic/ed-global"; import { EDGlobal, IMeta, active } from "../../../../logic/ed-global";
import { Tooltip } from "../../../../../../utils/ui/tooltip"; import { Tooltip } from "../../../../../../utils/ui/tooltip";
import { treeRebuild } from "../../../../logic/tree/build";
export const EdTreeName = ({ export const EdTreeName = ({
node, node,
@ -38,9 +39,17 @@ export const EdTreeName = ({
spellCheck={false} spellCheck={false}
defaultValue={local.rename} defaultValue={local.rename}
onFocus={(e) => { onFocus={(e) => {
e.currentTarget.select(); if (node.data?.jsx_prop?.is_root) {
p.ui.tree.rename_id = "";
p.render();
} else {
e.currentTarget.select();
}
}} }}
onBlur={() => { onBlur={() => {
if (node.data?.jsx_prop?.is_root) {
return;
}
item.name = local.rename; item.name = local.rename;
let mitem = node.data?.mitem; let mitem = node.data?.mitem;
@ -50,25 +59,22 @@ export const EdTreeName = ({
} }
if (mitem) { if (mitem) {
mitem.doc?.transact(() => { mitem.set("name", item.name);
if (mitem) { }
mitem.set("name", item.name);
if (active.comp_id === item.component?.id) { if (active.comp_id === item.component?.id) {
db.component.update({ db.component.update({
where: { where: {
id: active.comp_id, id: active.comp_id,
}, },
data: { data: {
name: item.name, name: item.name,
}, },
});
}
}
}); });
} }
p.ui.tree.rename_id = ""; p.ui.tree.rename_id = "";
treeRebuild(p);
p.render(); p.render();
}} }}
onKeyDown={(e) => { onKeyDown={(e) => {
@ -98,9 +104,9 @@ export const EdTreeName = ({
) : ( ) : (
<div className="flex flex-col"> <div className="flex flex-col">
<Name name={node.text} is_jsx_prop={is_jsx_prop} /> <Name name={node.text} is_jsx_prop={is_jsx_prop} />
<div className={"text-[9px] text-gray-500 -mt-1"}> {/* <div className={"text-[9px] text-gray-500 -mt-1"}>
{node.id} - {item.originalId} {node.id} - {item.originalId}
</div> </div> */}
</div> </div>
)} )}
</div> </div>

View File

@ -45,11 +45,13 @@ export const nodeOnDrop: (
toChilds.insert(relativeIndex, [map]); toChilds.insert(relativeIndex, [map]);
} }
from.parent.forEach((e, idx) => { if (!fromMeta?.jsx_prop?.is_root) {
if (from && e.get("id") === from.get("id")) { from.parent.forEach((e, idx) => {
from.parent.delete(idx); if (from && !!e && !!e.get && e.get("id") === from.get("id")) {
} from.parent.delete(idx);
}); }
});
}
} }
}); });
treeRebuild(p); treeRebuild(p);