diff --git a/app/web/src/nova/ed/panel/header/mid/add-text.tsx b/app/web/src/nova/ed/panel/header/mid/add-text.tsx index 01b532e0..062230a0 100644 --- a/app/web/src/nova/ed/panel/header/mid/add-text.tsx +++ b/app/web/src/nova/ed/panel/header/mid/add-text.tsx @@ -7,6 +7,7 @@ import { IText } from "../../../../../utils/types/text"; import { MContent } from "../../../../../utils/types/general"; import { fillID } from "../../../logic/tree/fill-id"; import { prepSection } from "./prep-section"; +import { IItem } from "../../../../../utils/types/item"; export const EdAddText = () => { const p = useGlobal(EDGlobal, "EDITOR"); @@ -37,7 +38,7 @@ export const EdAddText = () => { }, } as IText; - let mitem = meta.mitem; + let mitem = meta.mitem as MContent; if (mitem) { if ( meta.item.type === "text" || @@ -51,11 +52,40 @@ export const EdAddText = () => { if (!parent) { alert("Failed to add text!"); } else { - mitem = parent.mitem; + mitem = parent.mitem as MContent; } } if (mitem) { + if (mitem.get("type") === "section") { + const json = { + id: createId(), + name: `New Item`, + type: "item", + dim: { w: "full", h: "full" }, + childs: [], + adv: { + css: "", + }, + } as IItem; + + if (mitem) { + const childs = mitem.get("childs"); + if (childs) { + const map = new Y.Map() as MContent; + syncronize(map as any, fillID(json)); + const childs = mitem.get("childs"); + if (childs) { + childs.push([map]); + } + + active.item_id = map.get("id") || ""; + mitem = map; + p.render(); + } + } + } + const childs = mitem.get("childs"); if (childs) { const map = new Y.Map() as MContent; diff --git a/app/web/src/nova/ed/panel/tree/body.tsx b/app/web/src/nova/ed/panel/tree/body.tsx index e9e154b7..4aeb909d 100644 --- a/app/web/src/nova/ed/panel/tree/body.tsx +++ b/app/web/src/nova/ed/panel/tree/body.tsx @@ -22,7 +22,6 @@ export const EdTreeBody = () => { expandTreeHook(p, local); - if (active.comp_id && local.comp_id !== active.comp_id) { local.comp_id = active.comp_id; const ref = p.comp.list[active.comp_id]; @@ -42,7 +41,6 @@ export const EdTreeBody = () => { } } - if (tree.length === 0) return (
@@ -78,7 +76,6 @@ export const EdTreeBody = () => {
); - return ( <> { sort={false} dropTargetOffset={10} render={nodeRender} - onDrop={nodeOnDrop} + onDrop={(tree, options) => nodeOnDrop(p, tree, options)} canDrop={(_, args) => { if (!args.dragSource?.data?.item) return false; return canDrop(p, args); diff --git a/app/web/src/nova/ed/panel/tree/node/on-drop.tsx b/app/web/src/nova/ed/panel/tree/node/on-drop.tsx index 1212bbef..df441a77 100644 --- a/app/web/src/nova/ed/panel/tree/node/on-drop.tsx +++ b/app/web/src/nova/ed/panel/tree/node/on-drop.tsx @@ -3,11 +3,49 @@ import get from "lodash.get"; import { MContent } from "../../../../../utils/types/general"; import { EdMeta, PG, active } from "../../../logic/ed-global"; import { getMetaById } from "../../../logic/tree/build"; +import { fillID } from "../../../logic/tree/fill-id"; export const nodeOnDrop: ( + p: PG, tree: NodeModel[], options: DropOptions -) => void = () => {}; +) => void = (p, tree, options) => { + const { dragSource, dropTarget, relativeIndex, dragSourceId, dropTargetId } = + options; + + if ( + dragSource?.data && + dropTarget && + typeof dragSourceId === "string" && + typeof dropTargetId === "string" + ) { + let fromMeta = getMetaById(p, dragSourceId); + let toMeta = getMetaById(p, dropTargetId); + if (fromMeta && toMeta) { + let to = toMeta.parent_mcomp ? toMeta.parent_mcomp.mcomp : toMeta.mitem; + let from = fromMeta.mitem; + + if (to) { + to.doc?.transact(() => { + if (to && from && typeof relativeIndex === "number") { + const toChilds = to.get("childs"); + if (toChilds) { + const map = new Y.Map(); + syncronize(map, fillID(from.toJSON() as any)); + toChilds.insert(relativeIndex, [map]); + } + + from.parent.forEach((e, idx) => { + if (from && e.get("id") === from.get("id")) { + from.parent.delete(idx); + } + }); + } + }); + } + } + } +}; export const canDrop = (p: PG, arg: DropOptions) => { const { dragSource, dragSourceId, dropTargetId, dropTarget } = arg;