visuddhinanda 2 лет назад
Родитель
Сommit
43f69d2389
1 измененных файлов с 25 добавлено и 1 удалено
  1. 25 1
      dashboard/src/components/article/PaliTextToc.tsx

+ 25 - 1
dashboard/src/components/article/PaliTextToc.tsx

@@ -14,6 +14,9 @@ interface IWidget {
 }
 const PaliTextTocWidget = ({ book, para, channel, onSelect }: IWidget) => {
   const [tocList, setTocList] = useState<ListNodeData[]>([]);
+  const [selectedKeys, setSelectedKeys] = useState<Key[]>();
+  const [expandedKeys, setExpandedKeys] = useState<Key[]>();
+
   useEffect(() => {
     get<IPaliTocListResponse>(
       `/v2/palitext?view=book-toc&book=${book}&para=${para}`
@@ -26,12 +29,33 @@ const PaliTextTocWidget = ({ book, para, channel, onSelect }: IWidget) => {
         };
       });
       setTocList(toc);
+      if (json.data.rows.length > 0) {
+        let currLevel = 0;
+        let path: string[] = [];
+        for (let index = json.data.rows.length - 1; index >= 0; index--) {
+          const element = json.data.rows[index];
+          if (element.book === book && element.paragraph === para) {
+            currLevel = parseInt(element.level);
+          }
+          if (
+            parseInt(element.level) === 1 ||
+            (element.book === book && parseInt(element.level) < currLevel)
+          ) {
+            currLevel = parseInt(element.level);
+            path.push(`${element.book}-${element.paragraph}`);
+          }
+        }
+        setExpandedKeys(path);
+      }
+      setSelectedKeys([`${book}-${para}`]);
     });
   }, [book, para]);
+
   return (
     <TocTree
       treeData={tocList}
-      expandedKey={[`${book}-${para}`]}
+      expandedKeys={expandedKeys}
+      selectedKeys={selectedKeys}
       onSelect={(selectedKeys: Key[]) => {
         if (typeof onSelect !== "undefined") {
           onSelect(selectedKeys);