visuddhinanda 3 years ago
parent
commit
02edfa49c2

+ 20 - 8
dashboard/src/components/corpus/BookTreeList.tsx

@@ -30,17 +30,19 @@ interface IWidgetBookTreeList {
   path?: string[];
   path?: string[];
   tags?: string[];
   tags?: string[];
   onChange?: Function;
   onChange?: Function;
+  onTocLoad?: Function;
 }
 }
 const Widget = ({
 const Widget = ({
   root = "default",
   root = "default",
   path,
   path,
   tags,
   tags,
   onChange,
   onChange,
+  onTocLoad,
 }: IWidgetBookTreeList) => {
 }: IWidgetBookTreeList) => {
   const [tocData, setTocData] = useState<ITocTree[]>([]);
   const [tocData, setTocData] = useState<ITocTree[]>([]);
   const [currData, setCurrData] = useState<ITocTree[]>([]);
   const [currData, setCurrData] = useState<ITocTree[]>([]);
   const [bookPath, setBookPath] = useState<pathData[]>([]);
   const [bookPath, setBookPath] = useState<pathData[]>([]);
-  const [currRoot, setCurrRoot] = useState<string>(root);
+  const [currRoot, setCurrRoot] = useState<string>();
   const navigate = useNavigate();
   const navigate = useNavigate();
 
 
   useEffect(() => {
   useEffect(() => {
@@ -48,14 +50,18 @@ const Widget = ({
   }, [root]);
   }, [root]);
 
 
   useEffect(() => {
   useEffect(() => {
+    let mPath: string[] = [];
     const newPath: pathData[] = path
     const newPath: pathData[] = path
       ? path.map((item) => {
       ? path.map((item) => {
-          return { to: item, title: item };
+          mPath.push(item);
+          return { to: mPath.join("_"), title: item };
         })
         })
       : [];
       : [];
+
     setBookPath(newPath);
     setBookPath(newPath);
-    const currPath = getListCurrRoot(tocData, newPath);
-    setCurrData(currPath);
+    const currDir = getListCurrRoot(tocData, mPath);
+    console.log("currDir", currDir);
+    setCurrData(currDir);
   }, [path, tocData]);
   }, [path, tocData]);
 
 
   useEffect(() => {
   useEffect(() => {
@@ -75,18 +81,23 @@ const Widget = ({
         console.log("Book List ajax", json);
         console.log("Book List ajax", json);
         const treeData = json.map(treeMap);
         const treeData = json.map(treeMap);
         setTocData(treeData);
         setTocData(treeData);
+        if (typeof onTocLoad !== "undefined") {
+          onTocLoad(json);
+        }
       });
       });
     }
     }
   }, [currRoot]);
   }, [currRoot]);
 
 
   useEffect(() => {
   useEffect(() => {
-    const currPath = getListCurrRoot(tocData, bookPath);
-    setCurrData(currPath);
+    const currPath =
+      bookPath.length > 0 ? bookPath[bookPath.length - 1].to.split("_") : [];
+    const currDir = getListCurrRoot(tocData, currPath);
+    setCurrData(currDir);
   }, [bookPath, tocData]);
   }, [bookPath, tocData]);
 
 
   function getListCurrRoot(
   function getListCurrRoot(
     allTocData: ITocTree[],
     allTocData: ITocTree[],
-    currPath: pathData[]
+    currPath: string[]
   ): ITocTree[] {
   ): ITocTree[] {
     let curr: ITocTree[];
     let curr: ITocTree[];
     if (allTocData.length > 0) {
     if (allTocData.length > 0) {
@@ -98,7 +109,7 @@ const Widget = ({
     for (const itPath of currPath) {
     for (const itPath of currPath) {
       let isFound = false;
       let isFound = false;
       for (const itAll of curr) {
       for (const itAll of curr) {
-        if (itPath.to === itAll.dir) {
+        if (itPath === itAll.dir) {
           curr = itAll.children;
           curr = itAll.children;
           isFound = true;
           isFound = true;
           break;
           break;
@@ -118,6 +129,7 @@ const Widget = ({
       "_"
       "_"
     );
     );
     bookPath.push({ to: newPath, title: title });
     bookPath.push({ to: newPath, title: title });
+    console.log("newPath", newPath);
     console.log("book Path", bookPath);
     console.log("book Path", bookPath);
 
 
     setBookPath(bookPath);
     setBookPath(bookPath);

+ 45 - 5
dashboard/src/pages/library/palicanon/bypath.tsx

@@ -9,6 +9,7 @@ import type { IEventBookTreeOnchange } from "../../../components/corpus/BookTree
 import PaliChapterListByTag from "../../../components/corpus/PaliChapterListByTag";
 import PaliChapterListByTag from "../../../components/corpus/PaliChapterListByTag";
 import BookViewer from "../../../components/corpus/BookViewer";
 import BookViewer from "../../../components/corpus/BookViewer";
 import { IChapterClickEvent } from "../../../components/corpus/PaliChapterList";
 import { IChapterClickEvent } from "../../../components/corpus/PaliChapterList";
+import { IPaliBookListResponse } from "../../../components/api/Corpus";
 
 
 const Widget = () => {
 const Widget = () => {
   const { root, path, tag } = useParams();
   const { root, path, tag } = useParams();
@@ -20,6 +21,41 @@ const Widget = () => {
   const [isModalOpen, setIsModalOpen] = useState(false);
   const [isModalOpen, setIsModalOpen] = useState(false);
   const [openPara, setOpenPara] = useState({ book: 0, para: 0 });
   const [openPara, setOpenPara] = useState({ book: 0, para: 0 });
   const [drawerTitle, setDrawerTitle] = useState("");
   const [drawerTitle, setDrawerTitle] = useState("");
+  const [tocData, setTocData] = useState<IPaliBookListResponse[]>([]);
+
+  // 根据路径,遍历目录树,获取标签
+  const getTagByPath = (
+    _path?: string,
+    _tocData?: IPaliBookListResponse[]
+  ): string[] => {
+    if (typeof _path === "undefined" || _path === "") {
+      return [];
+    }
+    if (typeof _tocData === "undefined" || _tocData.length === 0) {
+      return [];
+    }
+    const arrPath = _path ? _path.split("_") : [];
+    let currToc = _tocData;
+    let nextToc: IPaliBookListResponse[] | undefined;
+    let found = false;
+    let tags: string[] = [];
+    for (const itPath of arrPath) {
+      for (const itToc of currToc) {
+        if (itPath === itToc.name.toLowerCase()) {
+          found = true;
+          nextToc = itToc.children;
+          tags = itToc.tag;
+          break;
+        }
+      }
+      if (found && nextToc) {
+        currToc = nextToc;
+      } else {
+        break;
+      }
+    }
+    return tags;
+  };
 
 
   useEffect(() => {
   useEffect(() => {
     let currRoot: string | null;
     let currRoot: string | null;
@@ -35,10 +71,11 @@ const Widget = () => {
     const arrPath = path ? path.split("_") : [];
     const arrPath = path ? path.split("_") : [];
     setBookPath(arrPath);
     setBookPath(arrPath);
     setBookRoot(currRoot);
     setBookRoot(currRoot);
-    console.log("index-load", root);
-  }, [root, path, navigate]);
+    const currTags = getTagByPath(path, tocData);
+    setBookTag(currTags);
+    console.log("index-load", root, path, currTags);
+  }, [root, path, navigate, tocData]);
 
 
-  // TODO
   return (
   return (
     <>
     <>
       <Row>
       <Row>
@@ -77,8 +114,11 @@ const Widget = () => {
                 root={bookRoot}
                 root={bookRoot}
                 path={bookPath}
                 path={bookPath}
                 onChange={(e: IEventBookTreeOnchange) => {
                 onChange={(e: IEventBookTreeOnchange) => {
+                  console.log("book tree list on change", e);
                   navigate(`/palicanon/list/${bookRoot}/${e.path.join("_")}`);
                   navigate(`/palicanon/list/${bookRoot}/${e.path.join("_")}`);
-                  setBookTag(e.tag);
+                }}
+                onTocLoad={(toc: IPaliBookListResponse[]) => {
+                  setTocData(toc);
                 }}
                 }}
               />
               />
               <PaliChapterListByTag
               <PaliChapterListByTag
@@ -98,7 +138,7 @@ const Widget = () => {
               />
               />
             </Col>
             </Col>
             <Col xs={0} sm={0} md={4}>
             <Col xs={0} sm={0} md={4}>
-              侧边栏 侧边栏 侧边栏 侧边栏 侧边栏
+              侧边栏
             </Col>
             </Col>
           </Row>
           </Row>
         </Col>
         </Col>