This commit is contained in:
Li1304553726 2025-02-27 22:39:31 +08:00
parent eb7206d987
commit d2877d1a85
3 changed files with 16 additions and 5 deletions

View File

@ -19,6 +19,8 @@ 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"; import { useAuth } from "@web/src/providers/auth-provider";
// 定义 MindElixir 插件 的配置
const MIND_OPTIONS = { const MIND_OPTIONS = {
direction: MindElixir.SIDE, direction: MindElixir.SIDE,
draggable: true, draggable: true,
@ -53,6 +55,7 @@ const MIND_OPTIONS = {
}, },
}; };
export default function MindEditor({ id }: { id?: string }) { export default function MindEditor({ id }: { id?: string }) {
//containerRef 容器ref instance 实例
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 { isAuthenticated, user, hasSomePermissions } = useAuth();
@ -71,14 +74,18 @@ export default function MindEditor({ id }: { id?: string }) {
}, [user]) }, [user])
const navigate = useNavigate(); const navigate = useNavigate();
const { create, update } = usePost(); const { create, update } = usePost();
//拿到slug数组
const { data: taxonomies } = api.taxonomy.getAll.useQuery({ const { data: taxonomies } = api.taxonomy.getAll.useQuery({
type: ObjectType.COURSE, type: ObjectType.COURSE,
}); });
const { handleFileUpload } = useTusUpload(); const { handleFileUpload } = useTusUpload();
//antd表单自带钩子
const [form] = Form.useForm(); const [form] = Form.useForm();
//post 信息 //instance 实例
useEffect(() => { useEffect(() => {
if (post && form && instance && id) { if (post && form && instance && id) {
console.log(post); console.log('你好',taxonomies);
instance.refresh((post as any).meta); instance.refresh((post as any).meta);
const deptIds = (post?.depts || [])?.map((dept) => dept.id); const deptIds = (post?.depts || [])?.map((dept) => dept.id);
const formData = { const formData = {
@ -86,8 +93,8 @@ export default function MindEditor({ id }: { id?: string }) {
deptIds: deptIds, deptIds: deptIds,
}; };
post.terms?.forEach((term) => { post.terms?.forEach((term) => {
formData[term.taxonomyId] = term.id; // 假设 taxonomyName 是您在 Form.Item 中使用的 name formData[term.taxonomyId] = term.id // 假设 taxonomyName是您在 Form.Item 中使用的name
}); })
form.setFieldsValue(formData); form.setFieldsValue(formData);
} }
}, [post, form, instance, id]); }, [post, form, instance, id]);
@ -108,8 +115,10 @@ export default function MindEditor({ id }: { id?: string }) {
nodeMenu: canEdit, // 禁用节点右键菜单 nodeMenu: canEdit, // 禁用节点右键菜单
keypress: canEdit, // 禁用键盘快捷键 keypress: canEdit, // 禁用键盘快捷键
}); });
//初始化
mind.init(MindElixir.new("新学习路径")); mind.init(MindElixir.new("新学习路径"));
containerRef.current.hidden = true; containerRef.current.hidden = true;
//挂载实例
setInstance(mind); setInstance(mind);
}, [canEdit]); }, [canEdit]);
useEffect(() => { useEffect(() => {
@ -119,6 +128,7 @@ export default function MindEditor({ id }: { id?: string }) {
instance.refresh((post as any)?.meta); instance.refresh((post as any)?.meta);
} }
}, [id, post, instance]); }, [id, post, instance]);
//保存 按钮 函数
const handleSave = async () => { const handleSave = async () => {
if (!instance) return; if (!instance) return;
const values = form.getFieldsValue(); const values = form.getFieldsValue();

View File

@ -82,7 +82,7 @@ export function CourseFormProvider({
}, [course, form]); }, [course, form]);
const onSubmit = async (values: any) => { const onSubmit = async (values: any) => {
console.log(values);
const sections = values?.sections || []; const sections = values?.sections || [];
const deptIds = values?.deptIds || []; const deptIds = values?.deptIds || [];
const termIds = taxonomies const termIds = taxonomies
@ -149,6 +149,7 @@ export function CourseFormProvider({
} }
}; };
return ( return (
<CourseEditorContext.Provider <CourseEditorContext.Provider
value={{ value={{

View File

@ -9,7 +9,7 @@ import DepartmentSelect from "../../../department/department-select";
const { TextArea } = Input; const { TextArea } = Input;
export function CourseBasicForm() { export function CourseBasicForm() {
// 将 CourseLevelLabel 转换为 Ant Design Select 需要的选项格式 // 将 CourseLevelLabel 使用 Object.entries 将 CourseLevelLabel 对象转换为键值对数组。
const levelOptions = Object.entries(CourseLevelLabel).map( const levelOptions = Object.entries(CourseLevelLabel).map(
([key, value]) => ({ ([key, value]) => ({
label: value, label: value,