This commit is contained in:
longdayi 2025-02-27 22:37:39 +08:00
commit bc5dd8e9dc
4 changed files with 77 additions and 70 deletions

View File

@ -36,7 +36,6 @@
"@nice/iconer": "workspace:^",
"@nice/utils": "workspace:^",
"mind-elixir": "workspace:^",
"@mind-elixir/node-menu": "workspace:*",
"@nice/ui": "workspace:^",
"@tanstack/query-async-storage-persister": "^5.51.9",
"@tanstack/react-query": "^5.51.21",

View File

@ -3,12 +3,11 @@ import NodeMenu from "./NodeMenu";
import { api, usePost } from "@nice/client";
import {
ObjectType,
PathDto,
postDetailSelect,
PostDto,
PostType,
Prisma,
RolePerms,
Taxonomy,
} from "@nice/common";
import TermSelect from "../../models/term/term-select";
import DepartmentSelect from "../../models/department/department-select";
@ -19,44 +18,12 @@ import MindElixir from "mind-elixir";
import { useTusUpload } from "@web/src/hooks/useTusUpload";
import { useNavigate } from "react-router-dom";
import { useAuth } from "@web/src/providers/auth-provider";
const MIND_OPTIONS = {
direction: MindElixir.SIDE,
draggable: true,
contextMenu: true,
toolBar: true,
nodeMenu: true,
keypress: true,
locale: "zh_CN" as const,
theme: {
name: "Latte",
palette: [
"#dd7878",
"#ea76cb",
"#8839ef",
"#e64553",
"#fe640b",
"#df8e1d",
"#40a02b",
"#209fb5",
"#1e66f5",
"#7287fd",
],
cssVar: {
"--main-color": "#444446",
"--main-bgcolor": "#ffffff",
"--color": "#777777",
"--bgcolor": "#f6f6f6",
"--panel-color": "#444446",
"--panel-bgcolor": "#ffffff",
"--panel-border-color": "#eaeaea",
},
},
};
import { MIND_OPTIONS } from "./constant";
export default function MindEditor({ id }: { id?: string }) {
const containerRef = useRef<HTMLDivElement>(null);
const [instance, setInstance] = useState<MindElixirInstance | null>(null);
const { isAuthenticated, user, hasSomePermissions } = useAuth();
const { data: post, isLoading }: { data: PostDto; isLoading: boolean } =
const { data: post, isLoading }: { data: PathDto; isLoading: boolean } =
api.post.findFirst.useQuery({
where: {
id,
@ -65,9 +32,9 @@ export default function MindEditor({ id }: { id?: string }) {
});
const canEdit: boolean = useMemo(() => {
//登录了且是作者、超管、无id新建模式
const isAuth = isAuthenticated && user?.id == post?.author.id
return !Boolean(id) || isAuth || hasSomePermissions(RolePerms.MANAGE_ANY_POST);
}, [user])
const isAuth = isAuthenticated && user?.id === post?.author?.id;
return !!id || isAuth || hasSomePermissions(RolePerms.MANAGE_ANY_POST);
}, [user]);
const navigate = useNavigate();
const { create, update } = usePost();
const { data: taxonomies } = api.taxonomy.getAll.useQuery({
@ -90,7 +57,6 @@ export default function MindEditor({ id }: { id?: string }) {
form.setFieldsValue(formData);
}
}, [post, form, instance, id]);
useEffect(() => {
if (!containerRef.current) return;
const mind = new MindElixir({
@ -98,8 +64,8 @@ export default function MindEditor({ id }: { id?: string }) {
el: containerRef.current,
before: {
beginEdit() {
return canEdit
}
return canEdit;
},
},
draggable: canEdit, // 禁用拖拽
contextMenu: canEdit, // 禁用右键菜单
@ -115,7 +81,9 @@ export default function MindEditor({ id }: { id?: string }) {
if ((!id || post) && instance) {
containerRef.current.hidden = false;
instance.toCenter();
instance.refresh((post as any)?.meta);
if (post?.meta?.nodeData) {
instance.refresh(post?.meta);
}
}
}, [id, post, instance]);
const handleSave = async () => {
@ -182,17 +150,13 @@ export default function MindEditor({ id }: { id?: string }) {
);
};
useEffect(() => {
containerRef.current.style.height = `${Math.floor(window.innerHeight / 1.25)}px`
}, [])
containerRef.current.style.height = `${Math.floor(window.innerHeight / 1.25)}px`;
}, []);
return (
<div className="grid grid-cols-1 flex-col w-[90vw] my-5 h-[80vh] border rounded-lg mx-auto">
{canEdit && taxonomies && (
<Form
form={form}
className=" bg-white p-4 ">
<Form form={form} className=" bg-white p-4 ">
<div className="flex items-center justify-between gap-4">
<div className="flex items-center gap-4">
{taxonomies.map((tax, index) => (
@ -219,28 +183,40 @@ export default function MindEditor({ id }: { id?: string }) {
/>
</Form.Item>
</div>
<Button ghost type="primary" onSubmit={(e) => e.preventDefault()} onClick={handleSave}>
<Button
ghost
type="primary"
onSubmit={(e) => e.preventDefault()}
onClick={handleSave}>
{id ? "更新" : "保存"}
</Button>
</div>
</Form>
)}
<div ref={containerRef} className="w-full" onContextMenu={(e) => e.preventDefault()} />
<div
ref={containerRef}
className="w-full"
onContextMenu={(e) => e.preventDefault()}
/>
{canEdit && instance && <NodeMenu mind={instance} />}
{isLoading && (
{
isLoading && (
<div
className="py-64 justify-center flex"
style={{ height: "calc(100vh - 287px)" }}>
<Spin size="large"></Spin>
</div>
)}
{!post && id && !isLoading && (
)
}
{
!post && id && !isLoading && (
<div
className="py-64"
style={{ height: "calc(100vh - 287px)" }}>
<Empty></Empty>
</div>
)}
</div>
)
}
</div >
);
}

View File

@ -0,0 +1,34 @@
import MindElixir from "mind-elixir";
export const MIND_OPTIONS = {
direction: MindElixir.SIDE,
draggable: true,
contextMenu: true,
toolBar: true,
nodeMenu: true,
keypress: true,
locale: "zh_CN" as const,
theme: {
name: "Latte",
palette: [
"#dd7878",
"#ea76cb",
"#8839ef",
"#e64553",
"#fe640b",
"#df8e1d",
"#40a02b",
"#209fb5",
"#1e66f5",
"#7287fd",
],
cssVar: {
"--main-color": "#444446",
"--main-bgcolor": "#ffffff",
"--color": "#777777",
"--bgcolor": "#f6f6f6",
"--panel-color": "#444446",
"--panel-bgcolor": "#ffffff",
"--panel-border-color": "#eaeaea",
},
},
};

View File

@ -226,8 +226,6 @@ model Post {
ancestors PostAncestry[] @relation("DescendantPosts")
descendants PostAncestry[] @relation("AncestorPosts")
resources Resource[] // 附件列表
// watchableStaffs Staff[] @relation("post_watch_staff")
// watchableDepts Department[] @relation("post_watch_dept") // 可观看的部门列表,关联 Department 模型
meta Json? // 封面url 视频url objectives具体的学习目标 rating评分Int
// 索引