rht
This commit is contained in:
parent
0fb3ca9c20
commit
490947e231
|
@ -7,22 +7,24 @@ import {
|
||||||
PostDto,
|
PostDto,
|
||||||
PostType,
|
PostType,
|
||||||
Prisma,
|
Prisma,
|
||||||
|
RolePerms,
|
||||||
Taxonomy,
|
Taxonomy,
|
||||||
} from "@nice/common";
|
} from "@nice/common";
|
||||||
import TermSelect from "../../models/term/term-select";
|
import TermSelect from "../../models/term/term-select";
|
||||||
import DepartmentSelect from "../../models/department/department-select";
|
import DepartmentSelect from "../../models/department/department-select";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useMemo, useRef, useState } from "react";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { MindElixirInstance } from "mind-elixir";
|
import { MindElixirInstance } from "mind-elixir";
|
||||||
import MindElixir from "mind-elixir";
|
import MindElixir from "mind-elixir";
|
||||||
import { useTusUpload } from "@web/src/hooks/useTusUpload";
|
import { useTusUpload } from "@web/src/hooks/useTusUpload";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { useAuth } from "@web/src/providers/auth-provider";
|
||||||
const MIND_OPTIONS = {
|
const MIND_OPTIONS = {
|
||||||
direction: MindElixir.SIDE,
|
direction: MindElixir.SIDE,
|
||||||
draggable: true,
|
draggable: true,
|
||||||
contextMenu: true,
|
contextMenu: true,
|
||||||
toolBar: true,
|
toolBar: true,
|
||||||
nodeMenu: true,
|
//nodeMenu: true,
|
||||||
keypress: true,
|
keypress: true,
|
||||||
locale: "zh_CN" as const,
|
locale: "zh_CN" as const,
|
||||||
theme: {
|
theme: {
|
||||||
|
@ -53,6 +55,7 @@ const MIND_OPTIONS = {
|
||||||
export default function MindEditor({ id }: { id?: string }) {
|
export default function MindEditor({ id }: { id?: string }) {
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
const [instance, setInstance] = useState<MindElixirInstance | null>(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: PostDto; isLoading: boolean } =
|
||||||
api.post.findFirst.useQuery({
|
api.post.findFirst.useQuery({
|
||||||
|
@ -61,6 +64,11 @@ export default function MindEditor({ id }: { id?: string }) {
|
||||||
},
|
},
|
||||||
select: postDetailSelect,
|
select: postDetailSelect,
|
||||||
});
|
});
|
||||||
|
const canEdit: boolean = useMemo(() => {
|
||||||
|
//登录了且是作者、超管、无id新建模式
|
||||||
|
const isAuth = isAuthenticated && user?.id == post?.authorId;
|
||||||
|
return !id || isAuth || hasSomePermissions(RolePerms.MANAGE_ANY_POST);
|
||||||
|
}, [user])
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { create, update } = usePost();
|
const { create, update } = usePost();
|
||||||
const { data: taxonomies } = api.taxonomy.getAll.useQuery({
|
const { data: taxonomies } = api.taxonomy.getAll.useQuery({
|
||||||
|
@ -89,11 +97,21 @@ export default function MindEditor({ id }: { id?: string }) {
|
||||||
const mind = new MindElixir({
|
const mind = new MindElixir({
|
||||||
...MIND_OPTIONS,
|
...MIND_OPTIONS,
|
||||||
el: containerRef.current,
|
el: containerRef.current,
|
||||||
|
before:{
|
||||||
|
beginEdit(){
|
||||||
|
return canEdit
|
||||||
|
}
|
||||||
|
},
|
||||||
|
draggable: canEdit, // 禁用拖拽
|
||||||
|
contextMenu: canEdit, // 禁用右键菜单
|
||||||
|
toolBar: canEdit, // 禁用工具栏
|
||||||
|
nodeMenu: canEdit, // 禁用节点右键菜单
|
||||||
|
keypress: canEdit, // 禁用键盘快捷键
|
||||||
});
|
});
|
||||||
mind.init(MindElixir.new("新学习路径"));
|
mind.init(MindElixir.new("新学习路径"));
|
||||||
containerRef.current.hidden = true;
|
containerRef.current.hidden = true;
|
||||||
setInstance(mind);
|
setInstance(mind);
|
||||||
}, []);
|
}, [canEdit]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if ((!id || post) && instance) {
|
if ((!id || post) && instance) {
|
||||||
containerRef.current.hidden = false;
|
containerRef.current.hidden = false;
|
||||||
|
@ -160,13 +178,13 @@ export default function MindEditor({ id }: { id?: string }) {
|
||||||
}
|
}
|
||||||
console.log(result);
|
console.log(result);
|
||||||
},
|
},
|
||||||
(error) => {},
|
(error) => { },
|
||||||
`mind-thumb-${new Date().toString()}`
|
`mind-thumb-${new Date().toString()}`
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
<div className=" flex flex-col border rounded-lg overflow-hidden">
|
<div className=" flex flex-col w-screen max-h-screen border rounded-lg overflow-hidden">
|
||||||
{taxonomies && (
|
{canEdit && taxonomies && (
|
||||||
<Form
|
<Form
|
||||||
onFinish={(values) => {
|
onFinish={(values) => {
|
||||||
console.log(values);
|
console.log(values);
|
||||||
|
@ -205,8 +223,8 @@ export default function MindEditor({ id }: { id?: string }) {
|
||||||
</div>
|
</div>
|
||||||
</Form>
|
</Form>
|
||||||
)}
|
)}
|
||||||
<div ref={containerRef} className="mind-editor min-h-screen" />
|
<div ref={containerRef} className="mind-editor min-h-screen" onContextMenu={(e)=>e.preventDefault()} />
|
||||||
{instance && <NodeMenu mind={instance} />}
|
{canEdit && instance && <NodeMenu mind={instance} />}
|
||||||
{isLoading && (
|
{isLoading && (
|
||||||
<div
|
<div
|
||||||
className="py-64 justify-center flex"
|
className="py-64 justify-center flex"
|
||||||
|
|
Loading…
Reference in New Issue