diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts index e92ea669b..a91dbf8e7 100644 --- a/app/src/protyle/toolbar/index.ts +++ b/app/src/protyle/toolbar/index.ts @@ -1449,8 +1449,7 @@ export class Toolbar { focusByRange(this.range); } }); - inputElement.addEventListener("input", (event) => { - event.stopPropagation(); + const genList = () => { fetchPost("/api/search/searchTemplate", { k: inputElement.value, }, (response) => { @@ -1468,13 +1467,33 @@ export class Toolbar { `; }); listElement.innerHTML = searchHTML || `
  • ${window.siyuan.languages.emptyContent}
  • `; - const currentPath = response.data.blocks[0]?.path; - if (previewPath === currentPath) { + + if (!previewPath) { + previewPath = response.data.blocks[0]?.path; + /// #if !MOBILE + const rangePosition = getSelectionPosition(nodeElement, range); + setPosition(this.subElement, rangePosition.left, rangePosition.top + 18, Constants.SIZE_TOOLBAR_HEIGHT); + (this.subElement.firstElementChild as HTMLElement).style.maxHeight = Math.min(window.innerHeight * 0.8, window.innerHeight - this.subElement.getBoundingClientRect().top) - 16 + "px"; + /// #else + setPosition(this.subElement, 0, 0); + /// #endif + } else if (response.data.blocks[0]?.path === previewPath) { return; + } else { + previewPath = response.data.blocks[0]?.path; } - previewPath = currentPath; previewTemplate(previewPath, previewElement, protyle.block.parentID); }); + }; + inputElement.addEventListener("compositionend", () => { + genList(); + }); + inputElement.addEventListener("input", (event: KeyboardEvent) => { + event.stopPropagation(); + if (event.isComposing) { + return; + } + genList(); }); this.subElement.lastElementChild.addEventListener("click", (event) => { const target = event.target as HTMLElement; @@ -1539,33 +1558,7 @@ export class Toolbar { this.subElementCloseCB = undefined; this.element.classList.add("fn__none"); inputElement.select(); - fetchPost("/api/search/searchTemplate", { - k: "", - }, (response) => { - let html = ""; - response.data.blocks.forEach((item: { path: string, content: string }, index: number) => { - html += `
    -${item.content}`; - /// #if !BROWSER - html += ` - -`; - /// #endif - html += ` - -
    `; - }); - this.subElement.querySelector(".b3-list--background").innerHTML = html || `
  • ${window.siyuan.languages.emptyContent}
  • `; - /// #if !MOBILE - const rangePosition = getSelectionPosition(nodeElement, range); - setPosition(this.subElement, rangePosition.left, rangePosition.top + 18, Constants.SIZE_TOOLBAR_HEIGHT); - (this.subElement.firstElementChild as HTMLElement).style.maxHeight = Math.min(window.innerHeight * 0.8, window.innerHeight - this.subElement.getBoundingClientRect().top) - 16 + "px"; - /// #else - setPosition(this.subElement, 0, 0); - /// #endif - previewPath = listElement.firstElementChild.getAttribute("data-value"); - previewTemplate(previewPath, previewElement, protyle.block.parentID); - }); + genList(); } public showWidget(protyle: IProtyle, nodeElement: HTMLElement, range: Range) { @@ -1595,8 +1588,7 @@ export class Toolbar { focusByRange(this.range); } }); - inputElement.addEventListener("input", (event) => { - event.stopPropagation(); + const genList = (init = false) => { fetchPost("/api/search/searchWidget", { k: inputElement.value, }, (response) => { @@ -1608,7 +1600,25 @@ export class Toolbar { `; }); listElement.innerHTML = searchHTML; + if (init) { + /// #if !MOBILE + const rangePosition = getSelectionPosition(nodeElement, range); + setPosition(this.subElement, rangePosition.left, rangePosition.top + 18, Constants.SIZE_TOOLBAR_HEIGHT); + /// #else + setPosition(this.subElement, 0, 0); + /// #endif + } }); + }; + inputElement.addEventListener("compositionend", () => { + genList(); + }); + inputElement.addEventListener("input", (event: KeyboardEvent) => { + event.stopPropagation(); + if (event.isComposing) { + return; + } + genList(); }); this.subElement.lastElementChild.addEventListener("click", (event) => { const target = event.target as HTMLElement; @@ -1623,24 +1633,7 @@ export class Toolbar { this.subElementCloseCB = undefined; this.element.classList.add("fn__none"); inputElement.select(); - fetchPost("/api/search/searchWidget", { - k: "", - }, (response) => { - let html = ""; - response.data.blocks.forEach((item: { content: string, name: string }, index: number) => { - html += `
    -${item.name} -${item.content} -
    `; - }); - this.subElement.querySelector(".b3-list--background").innerHTML = html; - /// #if !MOBILE - const rangePosition = getSelectionPosition(nodeElement, range); - setPosition(this.subElement, rangePosition.left, rangePosition.top + 18, Constants.SIZE_TOOLBAR_HEIGHT); - /// #else - setPosition(this.subElement, 0, 0); - /// #endif - }); + genList(true); } public showContent(protyle: IProtyle, range: Range, nodeElement: Element) {