This commit is contained in:
Rizky 2023-12-05 15:29:00 +07:00
parent 76700d7417
commit 4cfecbea6b
3 changed files with 72 additions and 7 deletions

View File

@ -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;

View File

@ -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 (
<div className="flex py-[100px] select-none justify-center flex-1">
@ -78,7 +76,6 @@ export const EdTreeBody = () => {
</div>
);
return (
<>
<TypedTree
@ -92,7 +89,7 @@ export const EdTreeBody = () => {
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);

View File

@ -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<EdMeta>[],
options: DropOptions<EdMeta>
) => 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<EdMeta>) => {
const { dragSource, dragSourceId, dropTargetId, dropTarget } = arg;