Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Fix the search box not responding to cut operation (#17898)

Jeffrey Chen4ff3482c177f
diff --git a/app/src/protyle/render/av/gallery/render.ts b/app/src/protyle/render/av/gallery/render.ts
index b3603b406d..c873471289 100644
--- a/app/src/protyle/render/av/gallery/render.ts
+++ b/app/src/protyle/render/av/gallery/render.ts
@@ -2,14 +2,10 @@ import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "../
import {Constants} from "../../../../constants";
import {fetchSyncPost} from "../../../../util/fetch";
import {focusBlock} from "../../../util/selection";
-import {electronUndo} from "../../../undo";
-import {addClearButton} from "../../../../util/addClearButton";
import {avRender, genTabHeaderHTML, getGroupTitleHTML, updateSearch} from "../render";
+import {bindAvSearch} from "../search";
import {processRender} from "../../../util/processCode";
import {getPageSize} from "../groups";
-/// #if MOBILE
-import {activeBlur} from "../../../../mobile/util/keyboardToolbar";
-/// #endif
import {renderKanban} from "../kanban/render";
import {initVirtualScroll} from "../virtualScroll";
import {getRowHTML} from "../row";
@@ -155,67 +151,11 @@ export const afterRenderGallery = (options: ITableOptions) => {
if (!options.renderAll) {
return;
}
- const viewsElement = options.blockElement.querySelector(".av__views") as HTMLElement;
- const searchInputElement = options.blockElement.querySelector('[data-type="av-search"]') as HTMLElement;
- searchInputElement.textContent = options.resetData.query || "";
- if (options.resetData.isSearching) {
- searchInputElement.focus();
- }
- searchInputElement.addEventListener("compositionstart", (event: KeyboardEvent) => {
- event.stopPropagation();
- });
- searchInputElement.addEventListener("keydown", (event: KeyboardEvent) => {
- if (event.isComposing) {
- return;
- }
- electronUndo(event);
- });
- searchInputElement.addEventListener("input", (event: KeyboardEvent) => {
- event.stopPropagation();
- if (event.isComposing) {
- return;
- }
- if (searchInputElement.textContent || document.activeElement === searchInputElement) {
- viewsElement.classList.add("av__views--show");
- } else {
- viewsElement.classList.remove("av__views--show");
- }
- updateSearch(options.blockElement, options.protyle);
- });
- searchInputElement.addEventListener("compositionend", () => {
- updateSearch(options.blockElement, options.protyle);
- });
- searchInputElement.addEventListener("blur", (event: KeyboardEvent) => {
- if (event.isComposing) {
- return;
- }
- if (!searchInputElement.textContent.trim()) {
- viewsElement.classList.remove("av__views--show");
- searchInputElement.style.width = "0";
- searchInputElement.style.paddingLeft = "0";
- searchInputElement.style.setProperty("margin-right", "0", "important");
- const clearElement = searchInputElement.nextElementSibling;
- if (clearElement) {
- clearElement.classList.add("fn__none");
- }
- }
- });
- addClearButton({
- inputElement: searchInputElement,
- right: 0,
- width: "1em",
- height: searchInputElement.clientHeight,
- clearCB() {
- viewsElement.classList.remove("av__views--show");
- searchInputElement.style.width = "0";
- searchInputElement.style.paddingLeft = "0";
- searchInputElement.style.setProperty("margin-right", "0", "important");
- focusBlock(options.blockElement);
- updateSearch(options.blockElement, options.protyle);
- /// #if MOBILE
- activeBlur();
- /// #endif
- }
+ bindAvSearch({
+ blockElement: options.blockElement,
+ query: options.resetData.query,
+ isSearching: options.resetData.isSearching,
+ onChange: () => updateSearch(options.blockElement, options.protyle),
});
initVirtualScroll(options);
};
diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.ts
index b07cda77e0..e7f43b0d6b 100644
--- a/app/src/protyle/render/av/render.ts
+++ b/app/src/protyle/render/av/render.ts
@@ -8,9 +8,7 @@ import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "../
import {getRowHTML, stickyRow, updateHeader} from "./row";
import {getCalcValue} from "./calc";
import {renderAVAttribute} from "./blockAttr";
-import {addClearButton} from "../../../util/addClearButton";
import {escapeAriaLabel, escapeAttr, escapeHtml} from "../../../util/escape";
-import {electronUndo} from "../../undo";
import {isInMobileApp} from "../../util/compatibility";
import {isMobile} from "../../../util/functions";
import {renderGallery} from "./gallery/render";
@@ -19,10 +17,8 @@ import {openMenuPanel} from "./openMenuPanel";
import {getPageSize} from "./groups";
import {clearSelect} from "../../util/clear";
import {showMessage} from "../../../dialog/message";
-/// #if MOBILE
-import {activeBlur} from "../../../mobile/util/keyboardToolbar";
-/// #endif
import {renderKanban} from "./kanban/render";
+import {bindAvSearch} from "./search";
import {initVirtualScroll} from "./virtualScroll";
interface IIds {
@@ -384,67 +380,11 @@ const afterRenderTable = (options: ITableOptions) => {
if (!options.renderAll) {
return;
}
- const viewsElement = options.blockElement.querySelector(".av__views") as HTMLElement;
- const searchInputElement = options.blockElement.querySelector('[data-type="av-search"]') as HTMLElement;
- searchInputElement.textContent = options.resetData.query || "";
- if (options.resetData.isSearching) {
- searchInputElement.focus();
- }
- searchInputElement.addEventListener("compositionstart", (event: KeyboardEvent) => {
- event.stopPropagation();
- });
- searchInputElement.addEventListener("keydown", (event: KeyboardEvent) => {
- if (event.isComposing) {
- return;
- }
- electronUndo(event);
- });
- searchInputElement.addEventListener("input", (event: KeyboardEvent) => {
- event.stopPropagation();
- if (event.isComposing) {
- return;
- }
- if (searchInputElement.textContent || document.activeElement === searchInputElement) {
- viewsElement.classList.add("av__views--show");
- } else {
- viewsElement.classList.remove("av__views--show");
- }
- updateSearch(options.blockElement, options.protyle);
- });
- searchInputElement.addEventListener("compositionend", () => {
- updateSearch(options.blockElement, options.protyle);
- });
- searchInputElement.addEventListener("blur", (event: KeyboardEvent) => {
- if (event.isComposing) {
- return;
- }
- if (!searchInputElement.textContent.trim()) {
- viewsElement.classList.remove("av__views--show");
- searchInputElement.style.width = "0";
- searchInputElement.style.paddingLeft = "0";
- searchInputElement.style.setProperty("margin-right", "0", "important");
- const clearElement = searchInputElement.nextElementSibling;
- if (clearElement) {
- clearElement.classList.add("fn__none");
- }
- }
- });
- addClearButton({
- inputElement: searchInputElement,
- right: 0,
- width: "1em",
- height: searchInputElement.clientHeight,
- clearCB() {
- viewsElement.classList.remove("av__views--show");
- searchInputElement.style.width = "0";
- searchInputElement.style.paddingLeft = "0";
- searchInputElement.style.setProperty("margin-right", "0", "important");
- focusBlock(options.blockElement);
- updateSearch(options.blockElement, options.protyle);
- /// #if MOBILE
- activeBlur();
- /// #endif
- }
+ bindAvSearch({
+ blockElement: options.blockElement,
+ query: options.resetData.query,
+ isSearching: options.resetData.isSearching,
+ onChange: () => updateSearch(options.blockElement, options.protyle),
});
initVirtualScroll(options);
};
diff --git a/app/src/protyle/render/av/search.ts b/app/src/protyle/render/av/search.ts
new file mode 100644
index 0000000000..16c2d22823
--- /dev/null
+++ b/app/src/protyle/render/av/search.ts
@@ -0,0 +1,80 @@
+import {addClearButton} from "../../../util/addClearButton";
+import {focusBlock} from "../../util/selection";
+import {electronUndo} from "../../undo";
+/// #if MOBILE
+import {activeBlur} from "../../../mobile/util/keyboardToolbar";
+/// #endif
+
+const collapseAvSearch = (searchInputElement: HTMLElement, viewsElement: HTMLElement) => {
+ viewsElement.classList.remove("av__views--show");
+ searchInputElement.style.width = "0";
+ searchInputElement.style.paddingLeft = "0";
+ searchInputElement.style.marginRight = "0";
+};
+
+export const bindAvSearch = (options: {
+ blockElement: HTMLElement,
+ query?: string,
+ isSearching?: boolean,
+ onChange: () => void,
+}) => {
+ const viewsElement = options.blockElement.querySelector(".av__views") as HTMLElement;
+ const searchInputElement = options.blockElement.querySelector('[data-type="av-search"]') as HTMLElement;
+ searchInputElement.textContent = options.query || "";
+ if (options.isSearching) {
+ searchInputElement.focus();
+ }
+ searchInputElement.addEventListener("compositionstart", (event: KeyboardEvent) => {
+ event.stopPropagation();
+ });
+ searchInputElement.addEventListener("keydown", (event: KeyboardEvent) => {
+ if (event.isComposing) {
+ return;
+ }
+ electronUndo(event);
+ });
+ const searchInputChange = (event: Event) => {
+ event.stopPropagation();
+ if ((event as KeyboardEvent).isComposing) {
+ return;
+ }
+ if (searchInputElement.textContent || document.activeElement === searchInputElement) {
+ viewsElement.classList.add("av__views--show");
+ } else {
+ viewsElement.classList.remove("av__views--show");
+ }
+ options.onChange();
+ };
+ searchInputElement.addEventListener("input", searchInputChange);
+ // 剪切不会触发 input
+ searchInputElement.addEventListener("cut", (event) => {
+ setTimeout(() => {
+ searchInputChange(event);
+ });
+ });
+ searchInputElement.addEventListener("compositionend", () => {
+ options.onChange();
+ });
+ searchInputElement.addEventListener("blur", (event: KeyboardEvent) => {
+ if (event.isComposing) {
+ return;
+ }
+ if (!searchInputElement.textContent) {
+ collapseAvSearch(searchInputElement, viewsElement);
+ }
+ });
+ addClearButton({
+ inputElement: searchInputElement,
+ right: 0,
+ width: "1em",
+ height: searchInputElement.clientHeight,
+ clearCB() {
+ collapseAvSearch(searchInputElement, viewsElement);
+ focusBlock(options.blockElement);
+ options.onChange();
+ /// #if MOBILE
+ activeBlur();
+ /// #endif
+ }
+ });
+};
diff --git a/app/src/util/addClearButton.ts b/app/src/util/addClearButton.ts
index 382d0eb799..dcfec386c9 100644
--- a/app/src/util/addClearButton.ts
+++ b/app/src/util/addClearButton.ts
@@ -1,7 +1,7 @@
-const update = (inputElement: HTMLElement, clearElement: Element, right: number) => {
+const update = (inputElement: HTMLElement, clearElement: Element, right: number, contenteditable: boolean) => {
let value = "";
- if (inputElement.tagName === "DIV") {
- value = inputElement.textContent.trim();
+ if (contenteditable) {
+ value = inputElement.textContent;
} else {
value = (inputElement as HTMLInputElement).value;
}
@@ -10,7 +10,7 @@ const update = (inputElement: HTMLElement, clearElement: Element, right: number)
clearElement.classList.add("fn__none");
if (typeof right === "number") {
inputElement.style.paddingRight = inputElement.dataset.oldPaddingRight;
- if (inputElement.getAttribute("contenteditable")) {
+ if (contenteditable) {
inputElement.style.removeProperty("margin-right");
}
}
@@ -18,7 +18,7 @@ const update = (inputElement: HTMLElement, clearElement: Element, right: number)
clearElement.classList.remove("fn__none");
if (typeof right === "number") {
// 数据库搜索需设置 margin
- inputElement.style.setProperty(inputElement.getAttribute("contenteditable") ? "margin-right" : "padding-right", `${right * 2 + clearElement.clientWidth}px`, "important");
+ inputElement.style.setProperty(contenteditable ? "margin-right" : "padding-right", `${right * 2 + clearElement.clientWidth}px`, "important");
}
}
};
@@ -30,25 +30,35 @@ export const addClearButton = (options: {
height?: number
className?: string
}) => {
+ const contenteditable = !!options.inputElement.getAttribute("contenteditable");
options.inputElement.dataset.oldPaddingRight = options.inputElement.style.paddingRight;
options.inputElement.insertAdjacentHTML("afterend",
`<svg class="${options.className || "b3-form__icon-clear"} ariaLabel" aria-label="${window.siyuan.languages.clear}" style="${options.right ? "right: " + options.right + "px;" : ""}${options.height ? "height:" + options.height + "px;" : ""}${options.width ? "width:" + options.width : ""}">
<use xlink:href="#iconCloseRound"></use></svg>`);
const clearElement = options.inputElement.nextElementSibling;
clearElement.addEventListener("click", () => {
- if (options.inputElement.tagName === "DIV") {
+ if (contenteditable) {
options.inputElement.textContent = "";
} else {
(options.inputElement as HTMLInputElement).value = "";
}
options.inputElement.focus();
- update(options.inputElement, clearElement, options.right);
+ update(options.inputElement, clearElement, options.right, contenteditable);
if (options.clearCB) {
options.clearCB();
}
});
options.inputElement.addEventListener("input", () => {
- update(options.inputElement, clearElement, options.right);
+ update(options.inputElement, clearElement, options.right, contenteditable);
});
- update(options.inputElement, clearElement, options.right);
+ // contenteditable 剪切不会触发 input
+ if (contenteditable) {
+ options.inputElement.addEventListener("cut", () => {
+ // cut 事件在删除选中文本之前触发,需要延迟执行
+ setTimeout(() => {
+ update(options.inputElement, clearElement, options.right, contenteditable);
+ });
+ });
+ }
+ update(options.inputElement, clearElement, options.right, contenteditable);
};