diff --git a/frontend/.eslintrc.json b/frontend/.eslintrc.json index 1037f1a3e4..4fc6500ac2 100644 --- a/frontend/.eslintrc.json +++ b/frontend/.eslintrc.json @@ -34,6 +34,7 @@ "no-class-assign": "warn", "no-unused-vars": "warn", "no-useless-escape": "off", + "no-inner-declarations": "off", "no-irregular-whitespace": "warn", "no-trailing-spaces": "warn", "react/jsx-indent": ["warn", 2], diff --git a/frontend/src/pages/wiki2/side-panel.js b/frontend/src/pages/wiki2/side-panel.js index b3acc2545a..2256e3506d 100644 --- a/frontend/src/pages/wiki2/side-panel.js +++ b/frontend/src/pages/wiki2/side-panel.js @@ -8,7 +8,7 @@ import Loading from '../../components/loading'; import WikiNav from './wiki-nav/index'; import PageUtils from './wiki-nav/page-utils'; import Page from './models/page'; -import { isObjectNotEmpty } from './utils'; +import { isObjectNotEmpty, isPageInSubtree } from './utils'; import wikiAPI from '../../utils/wiki-api'; import { Utils } from '../../utils/utils'; import WikiExternalOperations from './wiki-external-operations'; @@ -46,7 +46,7 @@ class SidePanel extends PureComponent { onDeletePage = (pageId) => { const config = deepCopy(this.props.config); - const { pages } = config; + const { pages, navigation } = config; const index = PageUtils.getPageIndexById(pageId, pages); const deletePageName = pages[index].name; config.pages.splice(index, 1); @@ -64,7 +64,8 @@ class SidePanel extends PureComponent { let errMessage = Utils.getErrorMsg(error); toaster.danger(errMessage); }); - if (this.props.getCurrentPageId() === pageId) { + const currentPageId = this.props.getCurrentPageId(); + if (currentPageId === pageId || isPageInSubtree(navigation, pageId, currentPageId)) { const newPageId = config.pages.length > 0 ? config.pages[0].id : ''; this.props.setCurrentPage(newPageId); } diff --git a/frontend/src/pages/wiki2/utils/index.js b/frontend/src/pages/wiki2/utils/index.js index 342dcd8f2c..920bd4ef26 100644 --- a/frontend/src/pages/wiki2/utils/index.js +++ b/frontend/src/pages/wiki2/utils/index.js @@ -108,7 +108,14 @@ const getPaths = (navigation, currentPageId, pages, isGetPathStr) => { paths: pathStr, curNode }; - return pathStr.split('-').map(id => idPageMap[id]); + const pathIds = pathStr.split('-'); + const result = []; + for (const id of pathIds) { + const page = idPageMap[id]; + if (!page) return result; + result.push(page); + } + return result; }; const getNamePaths = (config, pageId) => { @@ -134,6 +141,33 @@ const getNamePaths = (config, pageId) => { }; }; +const isPageInSubtree = (navigation, targetPageId, currentPageId) => { + let found = false; + function traverse(node) { + if (node.id === targetPageId) { + function checkSubtree(subNode) { + if (subNode.id === currentPageId) { + found = true; + return; + } + if (subNode.children) { + subNode.children.forEach(child => { + if (child) checkSubtree(child); + }); + } + } + checkSubtree(node); + return; + } + if (node.children) { + node.children.forEach(child => { + if (child) traverse(child); + }); + } + } + navigation.forEach(item => traverse(item)); + return found; +}; export { generatorBase64Code, @@ -144,5 +178,6 @@ export { getWikPageLink, throttle, getPaths, - getNamePaths + getNamePaths, + isPageInSubtree };