import { Tree as DNDTree, DndProvider, TreeMethods, getBackendOptions, } from "@minoru/react-dnd-treeview"; import { HTML5Backend } from "react-dnd-html5-backend"; import { useEffect } from "react"; import { deepClone, useGlobal, useLocal } from "web-utils"; import { Loading } from "../../../../../utils/ui/loading"; import { Modal } from "../../../../../utils/ui/modal"; import { EDGlobal, active } from "../../../logic/ed-global"; import { compPicker, reloadCompPicker } from "./comp-reload"; import { CompItem, edPageTreeRender } from "./comp-tree"; import { EdCompPreview } from "./comp-preview"; import { fuzzy } from "../../../../../utils/ui/fuzzy"; export const EdPopComp = () => { const p = useGlobal(EDGlobal, "EDITOR"); const local = useLocal({ tree: null as TreeMethods | null, tab: "Components", }); const TypedTree = DNDTree; compPicker.render = local.render; useEffect(() => { local.tree?.openAll(); }, [p.ui.popup.comp.open, compPicker.site_id, local.tab]); if (!p.ui.popup.comp.open) return null; if (!compPicker.active_id && active.item_id) { compPicker.active_id = active.item_id; } if (p.site.id !== compPicker.site_id) { compPicker.site_id = p.site.id; reloadCompPicker(p); } let tree = compPicker.tree; if (local.tab === "Trash") { tree = compPicker.trash; } if (compPicker.search) { tree = fuzzy(tree, "text", compPicker.search); tree.forEach((e) => (e.parent = "comp-root")); tree = tree.filter((e) => e.data?.type === "component"); } return ( <> { if (!open) { p.ui.popup.comp.open = null; p.render(); } }} fade={false} >
{ if (ref) { compPicker.ref = ref; } }} className={cx("absolute inset-[5%] bg-white flex")} >
{compPicker.status === "loading" && ( )} {compPicker.status !== "loading" && ( <>
{["Components", "Trash"].map((e) => { return (
{ local.tab = e; p.render(); }} > {e}
); })}
{ compPicker.search = e.currentTarget.value; p.render(); }} />
.tree-root > .listitem:first-child > div { border-top: 0; } > .tree-root > .listitem > .container { display: flex; flex-direction: row; flex-wrap: wrap; position: relative; } .dropping { background: #efefff; } ` )} > {compPicker.ref && compPicker.status === "ready" && ( { if (local.tree !== ref) { local.tree = ref; } }} tree={tree} initialOpen={true} rootId={"comp-root"} onDrop={async (newTree, opt) => { compPicker.tree = newTree; p.render(); if ( typeof opt.dragSourceId === "string" && typeof opt.dropTargetId === "string" ) { db.component.update({ where: { id: opt.dragSourceId, }, data: { id_component_group: opt.dropTargetId, }, }); } }} dragPreviewRender={() => <>} canDrag={() => true} canDrop={(tree, opt) => { if (opt.dropTarget?.data?.type === "component") return false; if (opt.dropTargetId === "comp-root") return false; return true; }} classes={{ root: "tree-root flex-1", listItem: "listitem", container: "container", dropTarget: "dropping", }} render={edPageTreeRender} /> )}
)}
); };