This commit is contained in:
Rizky 2023-10-23 17:48:00 +07:00
parent 9c4578e1da
commit e60325a729
7 changed files with 54 additions and 19 deletions

View File

@ -3,9 +3,8 @@ import { clientStartSync } from "../../../utils/sync/ws-client";
import { IContent, MContent } from "../../../utils/types/general"; import { IContent, MContent } from "../../../utils/types/general";
import { IItem, MItem } from "../../../utils/types/item"; import { IItem, MItem } from "../../../utils/types/item";
import { DComp, DPage, IRoot } from "../../../utils/types/root"; import { DComp, DPage, IRoot } from "../../../utils/types/root";
import { IText, MText } from "../../../utils/types/text";
import { FNCompDef } from "../../../utils/types/meta-fn";
import { ISection } from "../../../utils/types/section"; import { ISection } from "../../../utils/types/section";
import { IText, MText } from "../../../utils/types/text";
const EmptySite = { const EmptySite = {
id: "", id: "",
@ -31,6 +30,20 @@ const EmptyComp = {
snapshot: null as null | Uint8Array, snapshot: null as null | Uint8Array,
}; };
const target = { active_id: false as any };
export const active = {
get item_id() {
if (target.active_id === false) {
target.active_id = localStorage.getItem("prasi-active-id") || "";
}
return target.active_id;
},
set item_id(val: string) {
localStorage.setItem("prasi-active-id", val);
target.active_id = val;
},
};
export type EdMeta = { export type EdMeta = {
item: IItem | IText | ISection; item: IItem | IText | ISection;
mitem?: MItem | MText; mitem?: MItem | MText;
@ -59,7 +72,7 @@ export const EDGlobal = {
root: null as null | IRoot, root: null as null | IRoot,
entry: [] as string[], entry: [] as string[],
tree: [] as NodeModel<EdMeta>[], tree: [] as NodeModel<EdMeta>[],
meta: {} as Record<string, { item: IContent; mitem?: MContent }>, meta: {} as Record<string, EdMeta>,
list: {} as Record<string, EPage>, list: {} as Record<string, EPage>,
}, },
comp: { comp: {
@ -69,9 +82,6 @@ export const EDGlobal = {
list: {} as Record<string, { cur: EComp; doc: DComp }>, list: {} as Record<string, { cur: EComp; doc: DComp }>,
}, },
ui: { ui: {
select: {
id: "",
},
tree: { tree: {
open: {} as Record<string, string[]>, open: {} as Record<string, string[]>,
}, },

View File

@ -2,7 +2,6 @@ import { IItem } from "../../../../../../../utils/types/item";
import { PG } from "../../../../../logic/ed-global"; import { PG } from "../../../../../logic/ed-global";
export const edActionAttach = (p: PG, item: IItem) => { export const edActionAttach = (p: PG, item: IItem) => {
p.ui.select.id = item.id;
const pick = () => { const pick = () => {
p.ui.popup.comp = (comp_id) => {}; p.ui.popup.comp = (comp_id) => {};
p.render(); p.render();

View File

@ -1,7 +1,7 @@
import { syncronize } from "y-pojo"; import { syncronize } from "y-pojo";
import { IContent } from "../../../../../../../utils/types/general"; import { IContent } from "../../../../../../../utils/types/general";
import { MItem } from "../../../../../../../utils/types/item"; import { MItem } from "../../../../../../../utils/types/item";
import { PG } from "../../../../../logic/ed-global"; import { PG, active } from "../../../../../logic/ed-global";
import { treeRebuild } from "../../../../../logic/tree/build"; import { treeRebuild } from "../../../../../logic/tree/build";
import { fillID } from "../../../../../../editor/tools/fill-id"; import { fillID } from "../../../../../../editor/tools/fill-id";
@ -31,8 +31,10 @@ export const edActionPaste = async (p: PG, item: IContent) => {
} }
} }
const map = new Y.Map(); const map = new Y.Map();
syncronize(map, fillID(child)); const newchild = fillID(child);
syncronize(map, newchild);
mchilds.push([map]); mchilds.push([map]);
active.item_id = newchild.id;
} }
}); });
} }

View File

@ -1,6 +1,6 @@
import { TreeMethods } from "@minoru/react-dnd-treeview"; import { TreeMethods } from "@minoru/react-dnd-treeview";
import { useEffect } from "react"; import { useEffect } from "react";
import { PG } from "../../../../logic/ed-global"; import { PG, active } from "../../../../logic/ed-global";
export const indentHook = ( export const indentHook = (
p: PG, p: PG,
@ -9,9 +9,26 @@ export const indentHook = (
useEffect(() => { useEffect(() => {
const open = JSON.parse(localStorage.getItem("prasi-tree-open") || "{}"); const open = JSON.parse(localStorage.getItem("prasi-tree-open") || "{}");
p.ui.tree.open = open; p.ui.tree.open = open;
if (open[p.page.cur.id] && local.tree) {
local.tree.open(open[p.page.cur.id]); let shouldOpen = open[p.page.cur.id] || [];
let meta = p.page.meta[active.item_id];
while (meta) {
if (meta.item.id) shouldOpen.push(meta.item.id);
meta = p.page.meta[meta.parent_item.id];
}
if (shouldOpen.length > 0 && local.tree) {
local.tree.open(shouldOpen);
local.render(); local.render();
if (active.item_id) {
setTimeout(() => {
const el = document.getElementsByClassName(active.item_id);
if (el.length > 0) {
el[0].scrollIntoView({ behavior: "instant", block: "center" });
}
});
}
} }
}, [p.page.tree]); }, [p.page.tree]);
}; };

View File

@ -1,13 +1,13 @@
import { NodeRender } from "@minoru/react-dnd-treeview"; import { NodeRender } from "@minoru/react-dnd-treeview";
import { EdMeta } from "../../../logic/ed-global"; import { useGlobal, useLocal } from "web-utils";
import { EDGlobal, EdMeta, active } from "../../../logic/ed-global";
import { EdTreeAction } from "./item/action"; import { EdTreeAction } from "./item/action";
import { EdTreeCtxMenu } from "./item/ctx-menu"; import { EdTreeCtxMenu } from "./item/ctx-menu";
import { EdTreeIndent } from "./item/indent"; import { EdTreeIndent } from "./item/indent";
import { EdTreeName } from "./item/name"; import { EdTreeName } from "./item/name";
import { indentHook } from "./item/indent-hook";
import { useLocal } from "web-utils";
export const nodeRender: NodeRender<EdMeta> = (node, prm) => { export const nodeRender: NodeRender<EdMeta> = (node, prm) => {
const p = useGlobal(EDGlobal, "EDITOR");
const local = useLocal({ const local = useLocal({
rightClick: null as null | React.MouseEvent<HTMLDivElement, MouseEvent>, rightClick: null as null | React.MouseEvent<HTMLDivElement, MouseEvent>,
}); });
@ -18,14 +18,21 @@ export const nodeRender: NodeRender<EdMeta> = (node, prm) => {
return ( return (
<div <div
className={cx( className={cx(
"relative border-b flex items-stretch hover:bg-blue-50 min-h-[26px]", item.id,
isComponent && `bg-purple-50` "relative border-b flex items-stretch min-h-[26px]",
active.item_id === item.id
? ["bg-blue-100"]
: ["hover:bg-blue-50", isComponent && `bg-purple-50`]
)} )}
onContextMenu={(event) => { onContextMenu={(event) => {
event.preventDefault(); event.preventDefault();
local.rightClick = event; local.rightClick = event;
local.render(); local.render();
}} }}
onClick={() => {
active.item_id = item.id;
p.render();
}}
> >
{local.rightClick && ( {local.rightClick && (
<EdTreeCtxMenu <EdTreeCtxMenu

View File

@ -9,7 +9,7 @@ export const EdTree = () => {
return ( return (
<div className="flex flex-col min-w-[300px] relative border-r"> <div className="flex flex-col min-w-[300px] relative border-r">
<div className=""></div> <div className=""></div>
<div className="flex relative flex-1 overflow-y-auto overflow-x-hidden"> <div className="tree-body flex relative flex-1 overflow-y-auto overflow-x-hidden">
<div className="absolute inset-0"> <div className="absolute inset-0">
<DndProvider backend={MultiBackend} options={getBackendOptions()}> <DndProvider backend={MultiBackend} options={getBackendOptions()}>
<EdTreeBody /> <EdTreeBody />

View File

@ -43,7 +43,7 @@ export const useGlobal = <T extends object>(
const { global, render } = ctx; const { global, render } = ctx;
if (!global[_id]) { if (!global[_id]) {
global[_id] = deepClone(defaultValue); global[_id] = defaultValue;
} }
useEffect(() => { useEffect(() => {