diff --git a/app/src/mobile/menu/search.ts b/app/src/mobile/menu/search.ts index 38af97c4a..00743fa6d 100644 --- a/app/src/mobile/menu/search.ts +++ b/app/src/mobile/menu/search.ts @@ -47,16 +47,17 @@ const replace = (element: Element, config: Config.IUILayoutTabSearchConfig, isAl return; } saveKeyList("replaceKeys", replaceInputElement.value); - let currentLiElement: HTMLElement = searchListElement.querySelector(".b3-list-item--focus"); + const currentLiElement: HTMLElement = searchListElement.querySelector(".b3-list-item--focus"); if (!currentLiElement) { return; } loadElement.classList.remove("fn__none"); loadElement.nextElementSibling.classList.add("fn__none"); + const currentId = currentLiElement.getAttribute("data-node-id"); fetchPost("/api/search/findReplace", { k: config.method === 0 ? getKeyByLiElement(currentLiElement) : (document.querySelector("#toolbarSearch") as HTMLInputElement).value, r: replaceInputElement.value, - ids: isAll ? [] : [currentLiElement.getAttribute("data-node-id")], + ids: isAll ? [] : [currentId], types: config.types, method: config.method, replaceTypes: config.replaceTypes, @@ -73,41 +74,27 @@ const replace = (element: Element, config: Config.IUILayoutTabSearchConfig, isAl return; } if (isAll) { - updateSearchResult(config, element); + updateSearchResult(config, element, false); return; } reloadProtyle(window.siyuan.mobile.editor.protyle, false); + let newId = currentLiElement.getAttribute("data-node-id"); if (currentLiElement.nextElementSibling) { - currentLiElement.nextElementSibling.classList.add("b3-list-item--focus"); + newId = currentLiElement.nextElementSibling.getAttribute("data-node-id"); } else if (currentLiElement.previousElementSibling) { - currentLiElement.previousElementSibling.classList.add("b3-list-item--focus"); + newId = currentLiElement.previousElementSibling.getAttribute("data-node-id"); } - if (config.group === 1) { - if (currentLiElement.nextElementSibling || currentLiElement.previousElementSibling) { - currentLiElement.remove(); - } else { - const nextDocElement = currentLiElement.parentElement.nextElementSibling || currentLiElement.parentElement.previousElementSibling.previousElementSibling?.previousElementSibling; - if (nextDocElement) { - nextDocElement.nextElementSibling.firstElementChild.classList.add("b3-list-item--focus"); - nextDocElement.nextElementSibling.classList.remove("fn__none"); - nextDocElement.firstElementChild.firstElementChild.classList.add("b3-list-item__arrow--open"); - } - currentLiElement.parentElement.previousElementSibling.remove(); - currentLiElement.parentElement.remove(); + if (config.group === 1 && !newId) { + const nextDocElement = currentLiElement.parentElement.nextElementSibling || currentLiElement.parentElement.previousElementSibling.previousElementSibling?.previousElementSibling; + if (nextDocElement) { + newId = nextDocElement.nextElementSibling.firstElementChild.getAttribute("data-node-id"); } - } else { - currentLiElement.remove(); - } - currentLiElement = searchListElement.querySelector(".b3-list-item--focus"); - if (!currentLiElement) { - searchListElement.innerHTML = `
${window.siyuan.languages.emptyContent}
`; - return; - } - if (searchListElement.scrollTop < currentLiElement.offsetTop - searchListElement.clientHeight + 30 || - searchListElement.scrollTop > currentLiElement.offsetTop) { - searchListElement.scrollTop = currentLiElement.offsetTop - searchListElement.clientHeight + 30; } + updateSearchResult(config, element, false, { + currentId, + newId + }); }); }; @@ -167,10 +154,16 @@ const updateConfig = (element: Element, newConfig: Config.IUILayoutTabSearchConf window.siyuan.menus.menu.remove(); }; -const onRecentBlocks = (data: IBlock[], config: Config.IUILayoutTabSearchConfig, response?: IWebSocketData) => { +const onRecentBlocks = (data: IBlock[], config: Config.IUILayoutTabSearchConfig, + response?: IWebSocketData, focusId?: { + currentId?: string, + newId?: string + }) => { const listElement = document.querySelector("#searchList"); let resultHTML = ""; - data.forEach((item: IBlock, index: number) => { + let currentData; + let newData; + data.forEach((item: IBlock) => { const title = getNotebookName(item.box) + getDisplayName(item.hPath, false); if (item.children) { resultHTML += `
@@ -180,8 +173,16 @@ const onRecentBlocks = (data: IBlock[], config: Config.IUILayoutTabSearchConfig, ${unicode2Emoji(getNotebookIcon(item.box) || window.siyuan.storage[Constants.LOCAL_IMAGES].note, "b3-list-item__graphic", true)} ${escapeGreat(title)}
`; - item.children.forEach((childItem, childIndex) => { - resultHTML += `
+ item.children.forEach((childItem) => { + if (focusId) { + if (childItem.id === focusId.currentId) { + currentData = childItem; + } + if (childItem.id === focusId.newId) { + newData = childItem; + } + } + resultHTML += `
${unicode2Emoji(childItem.ial.icon, "b3-list-item__graphic", true)} ${childItem.content} @@ -190,7 +191,15 @@ ${childItem.tag ? ` + if (focusId) { + if (item.id === focusId.currentId) { + currentData = item; + } + if (item.id === focusId.newId) { + newData = item; + } + } + resultHTML += `
${unicode2Emoji(item.ial.icon, "b3-list-item__graphic", true)} @@ -222,10 +231,31 @@ ${childItem.tag ? `${config.page}/${response.data.pageCount || 1}`; } listElement.previousElementSibling.querySelector('[data-type="result"]').innerHTML = countHTML; + if (!currentData) { + currentData = newData; + } + if (!currentData && data.length > 0) { + if (data[0].children) { + currentData = data[0].children[0]; + } else { + currentData = data[0]; + } + } + if (currentData) { + const currentList = listElement.querySelector(`[data-node-id="${currentData.id}"]`) as HTMLElement; + if (currentList) { + currentList.classList.add("b3-list-item--focus"); + currentList.scrollIntoView(); + } + } }; let toolbarSearchTimeout = 0; -export const updateSearchResult = (config: Config.IUILayoutTabSearchConfig, element: Element, rmCurrentCriteria = false) => { +export const updateSearchResult = (config: Config.IUILayoutTabSearchConfig, element: Element, rmCurrentCriteria = false, + focusId?: { + currentId?: string, + newId?: string + }) => { clearTimeout(toolbarSearchTimeout); toolbarSearchTimeout = window.setTimeout(() => { if (rmCurrentCriteria) { @@ -238,7 +268,7 @@ export const updateSearchResult = (config: Config.IUILayoutTabSearchConfig, elem const inputElement = document.getElementById("toolbarSearch") as HTMLInputElement; if (inputElement.value === "" && (!config.idPath || config.idPath.length === 0)) { fetchPost("/api/block/getRecentUpdatedBlocks", {}, (response) => { - onRecentBlocks(response.data, config); + onRecentBlocks(response.data, config, undefined, focusId); loadingElement.classList.add("fn__none"); previousElement.setAttribute("disabled", "true"); nextElement.setAttribute("disabled", "true"); @@ -261,7 +291,7 @@ export const updateSearchResult = (config: Config.IUILayoutTabSearchConfig, elem orderBy: config.sort, page: config.page, }, (response) => { - onRecentBlocks(response.data.blocks, config, response); + onRecentBlocks(response.data.blocks, config, response, focusId); loadingElement.classList.add("fn__none"); if (config.page < response.data.pageCount) { nextElement.removeAttribute("disabled"); diff --git a/app/src/search/util.ts b/app/src/search/util.ts index 03c5deb8d..10eb5dc92 100644 --- a/app/src/search/util.ts +++ b/app/src/search/util.ts @@ -1174,11 +1174,12 @@ export const replace = (element: Element, config: Config.IUILayoutTabSearchConfi return; } saveKeyList("replaceKeys", replaceInputElement.value); - let currentList: HTMLElement = searchPanelElement.querySelector(".b3-list-item--focus"); + const currentList: HTMLElement = searchPanelElement.querySelector(".b3-list-item--focus"); if (!currentList || currentList.dataset.type === "search-new") { return; } loadElement.classList.remove("fn__none"); + const currentId = currentList.getAttribute("data-node-id"); fetchPost("/api/search/findReplace", { k: config.method === 0 ? getKeyByLiElement(currentList) : searchInputElement.value, r: replaceInputElement.value, @@ -1188,7 +1189,7 @@ export const replace = (element: Element, config: Config.IUILayoutTabSearchConfi groupBy: config.group, orderBy: config.sort, page: config.page, - ids: isAll ? [] : [currentList.getAttribute("data-node-id")], + ids: isAll ? [] : [currentId], replaceTypes: config.replaceTypes }, (response) => { loadElement.classList.add("fn__none"); @@ -1197,7 +1198,7 @@ export const replace = (element: Element, config: Config.IUILayoutTabSearchConfi return; } if (isAll) { - inputEvent(element, config, edit, true); + inputEvent(element, config, edit, false); return; } const rootId = currentList.getAttribute("data-root-id"); @@ -1206,48 +1207,31 @@ export const replace = (element: Element, config: Config.IUILayoutTabSearchConfi reloadProtyle(item.editor.protyle, false); } }); + let newId = currentList.getAttribute("data-node-id"); if (currentList.nextElementSibling) { - currentList.nextElementSibling.classList.add("b3-list-item--focus"); + newId = currentList.nextElementSibling.getAttribute("data-node-id"); } else if (currentList.previousElementSibling) { - currentList.previousElementSibling.classList.add("b3-list-item--focus"); + newId = currentList.previousElementSibling.getAttribute("data-node-id"); } - if (config.group === 1) { - if (currentList.nextElementSibling || currentList.previousElementSibling) { - currentList.remove(); - } else { - const nextDocElement = currentList.parentElement.nextElementSibling || currentList.parentElement.previousElementSibling.previousElementSibling?.previousElementSibling; - if (nextDocElement) { - nextDocElement.nextElementSibling.firstElementChild.classList.add("b3-list-item--focus"); - nextDocElement.nextElementSibling.classList.remove("fn__none"); - nextDocElement.firstElementChild.firstElementChild.classList.add("b3-list-item__arrow--open"); - } - currentList.parentElement.previousElementSibling.remove(); - currentList.parentElement.remove(); + if (config.group === 1 && !newId) { + const nextDocElement = currentList.parentElement.nextElementSibling || currentList.parentElement.previousElementSibling.previousElementSibling?.previousElementSibling; + if (nextDocElement) { + newId = nextDocElement.nextElementSibling.firstElementChild.getAttribute("data-node-id"); } - } else { - currentList.remove(); } - currentList = searchPanelElement.querySelector(".b3-list-item--focus"); - if (!currentList) { - searchPanelElement.innerHTML = `
${window.siyuan.languages.emptyContent}
`; - edit.protyle.element.classList.add("fn__none"); - element.querySelector(".search__drag").classList.add("fn__none"); - return; - } - if (searchPanelElement.scrollTop < currentList.offsetTop - searchPanelElement.clientHeight + 30 || - searchPanelElement.scrollTop > currentList.offsetTop) { - searchPanelElement.scrollTop = currentList.offsetTop - searchPanelElement.clientHeight + 30; - } - getArticle({ - edit, - id: currentList.getAttribute("data-node-id"), - config, - value: searchInputElement.value, + inputEvent(element, config, edit, false, { + currentId, + newId }); }); }; -export const inputEvent = (element: Element, config: Config.IUILayoutTabSearchConfig, edit: Protyle, rmCurrentCriteria = false) => { +export const inputEvent = (element: Element, config: Config.IUILayoutTabSearchConfig, + edit: Protyle, rmCurrentCriteria = false, + focusId?: { + currentId?: string, + newId?: string + }) => { let inputTimeout = parseInt(element.getAttribute("data-timeout") || "0"); clearTimeout(inputTimeout); inputTimeout = window.setTimeout(() => { @@ -1308,7 +1292,7 @@ export const inputEvent = (element: Element, config: Config.IUILayoutTabSearchCo } else { nextElement.setAttribute("disabled", "disabled"); } - onSearch(response.data.blocks, edit, element, config); + onSearch(response.data.blocks, edit, element, config, focusId); let text = window.siyuan.languages.findInDoc.replace("${x}", response.data.matchedRootCount).replace("${y}", response.data.matchedBlockCount); if (response.data.docMode) { text = window.siyuan.languages.matchDoc.replace("${x}", response.data.matchedRootCount); @@ -1337,9 +1321,15 @@ export const getAttr = (block: IBlock) => { return attrHTML; }; -const onSearch = (data: IBlock[], edit: Protyle, element: Element, config: Config.IUILayoutTabSearchConfig) => { +const onSearch = (data: IBlock[], edit: Protyle, element: Element, config: Config.IUILayoutTabSearchConfig, + focusId?: { + currentId?: string, + newId?: string + }) => { let resultHTML = ""; - data.forEach((item, index) => { + let currentData; + let newData; + data.forEach((item) => { const title = getNotebookName(item.box) + getDisplayName(item.hPath, false); if (item.children) { resultHTML += `
@@ -1349,8 +1339,16 @@ const onSearch = (data: IBlock[], edit: Protyle, element: Element, config: Confi ${unicode2Emoji(getNotebookIcon(item.box) || window.siyuan.storage[Constants.LOCAL_IMAGES].note, "b3-list-item__graphic", true)} ${escapeGreat(title)}
`; - item.children.forEach((childItem, childIndex) => { - resultHTML += `
+ item.children.forEach((childItem) => { + if (focusId) { + if (childItem.id === focusId.currentId) { + currentData = childItem; + } + if (childItem.id === focusId.newId) { + newData = childItem; + } + } + resultHTML += `
${unicode2Emoji(childItem.ial.icon, "b3-list-item__graphic", true)} ${childItem.content} @@ -1360,7 +1358,15 @@ ${childItem.tag ? ` + if (focusId) { + if (item.id === focusId.currentId) { + currentData = item; + } + if (item.id === focusId.newId) { + newData = item; + } + } + resultHTML += `
${unicode2Emoji(item.ial.icon, "b3-list-item__graphic", true)} ${item.content} @@ -1370,26 +1376,25 @@ ${item.tag ? `${it
`; } }); - - if (data[0]) { + if (!currentData) { + currentData = newData; + } + if (!currentData && data.length > 0) { + if (data[0].children) { + currentData = data[0].children[0]; + } else { + currentData = data[0]; + } + } + if (currentData) { edit.protyle.element.classList.remove("fn__none"); element.querySelector(".search__drag").classList.remove("fn__none"); - const searchInputElement = element.querySelector("#searchInput") as HTMLInputElement; - if (data[0].children) { - getArticle({ - edit, - id: data[0].children[0].id, - config, - value: searchInputElement.value, - }); - } else { - getArticle({ - edit, - id: data[0].id, - config, - value: searchInputElement.value, - }); - } + getArticle({ + edit, + id: currentData.id, + config, + value: (element.querySelector("#searchInput") as HTMLInputElement).value, + }); } else { edit.protyle.element.classList.add("fn__none"); element.querySelector(".search__drag").classList.add("fn__none"); @@ -1409,4 +1414,11 @@ ${item.tag ? `${it ${window.siyuan.languages.emptyContent}
`); + if (currentData) { + const currentList = element.querySelector(`[data-node-id="${currentData.id}"]`) as HTMLElement; + if (currentList) { + currentList.classList.add("b3-list-item--focus"); + currentList.scrollIntoView(); + } + } };