Skip to content

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

:recycle: Reuse panel search interactions https://github.com/siyuan-note/siyuan/issues/19388

Vanessa1857c96979d5
diff --git a/app/src/layout/dock/BacklinkContent.ts b/app/src/layout/dock/BacklinkContent.ts
index 9603a99612..58ecc55bea 100644
--- a/app/src/layout/dock/BacklinkContent.ts
+++ b/app/src/layout/dock/BacklinkContent.ts
@@ -1,3 +1,4 @@
+import {bindPanelSearch} from "./panelSearch";
import type {Tab} from "../Tab";
import {Model} from "../Model";
import {Tree} from "../../util/Tree";
@@ -289,31 +290,14 @@ export class BacklinkContent extends Model {
<div class="backlinkMList fn__flex-1"></div>`;
this.inputsElement = this.element.querySelectorAll("input");
- this.inputsElement.forEach((item) => {
- item.addEventListener("blur", (event: KeyboardEvent) => {
- const inputElement = event.target as HTMLInputElement;
- inputElement.classList.add("fn__none");
- const filterIconElement = inputElement.parentElement.querySelector('[data-type="search"]');
- if (inputElement.value) {
- filterIconElement.classList.add("block__icon--active");
- filterIconElement.setAttribute("aria-label", window.siyuan.languages.search + " " + inputElement.value);
- } else {
- filterIconElement.classList.remove("block__icon--active");
- filterIconElement.setAttribute("aria-label", window.siyuan.languages.search);
- }
+ this.inputsElement.forEach((inputElement) => {
+ const searchElement = inputElement.parentElement.querySelector('[data-type="search"]');
+ const showSearch = bindPanelSearch(inputElement, searchElement, () => this.searchBacklinks(), {
+ trim: false, trigger: "enter",
});
- item.addEventListener("keydown", (event: KeyboardEvent) => {
- if (!event.isComposing && event.key === "Enter") {
- this.searchBacklinks();
- }
- });
- });
- this.element.querySelectorAll('[data-type="search"]').forEach((item, index) => {
- item.addEventListener("click", (event) => {
+ searchElement.addEventListener("click", (event) => {
event.stopPropagation();
- const inputElement = this.inputsElement[index];
- inputElement.classList.remove("fn__none");
- inputElement.select();
+ showSearch();
});
});
this.tree = new Tree({
diff --git a/app/src/layout/dock/Bookmark.ts b/app/src/layout/dock/Bookmark.ts
index a2092c782c..fd696b415a 100644
--- a/app/src/layout/dock/Bookmark.ts
+++ b/app/src/layout/dock/Bookmark.ts
@@ -1,4 +1,5 @@
import {Tab} from "../Tab";
+import {bindPanelSearch} from "./panelSearch";
import {Model} from "../Model";
import {Tree} from "../../util/Tree";
import {setPanelFocus} from "../util";
@@ -53,24 +54,8 @@ export class Bookmark extends Model {
</div>
<div class="fn__flex-1" style="margin-bottom: 8px"></div>`;
const inputElement = this.element.querySelector("input.b3-text-field.search__label") as HTMLInputElement;
- inputElement.addEventListener("blur", () => {
- inputElement.classList.add("fn__none");
- const filterIconElement = inputElement.nextElementSibling as HTMLElement;
- const value = inputElement.value;
- if (value.trim()) {
- filterIconElement.classList.add("block__icon--active");
- filterIconElement.setAttribute("aria-label", window.siyuan.languages.search + " " + value);
- } else {
- filterIconElement.classList.remove("block__icon--active");
- filterIconElement.setAttribute("aria-label", window.siyuan.languages.search);
- }
- });
- inputElement.addEventListener("input", (event: InputEvent) => {
- if (!event.isComposing) {
- this.filter();
- }
- });
- inputElement.addEventListener("compositionend", () => this.filter());
+ const showSearch = bindPanelSearch(inputElement,
+ this.element.querySelector('[data-type="search"]') as HTMLElement, () => this.filter());
this.tree = new Tree({
element: this.element.lastElementChild as HTMLElement,
data: null,
@@ -185,8 +170,7 @@ export class Bookmark extends Model {
this.update();
break;
case "search":
- inputElement.classList.remove("fn__none");
- inputElement.select();
+ showSearch();
break;
}
}
diff --git a/app/src/layout/dock/Graph.ts b/app/src/layout/dock/Graph.ts
index 4911fa503a..2d93810438 100644
--- a/app/src/layout/dock/Graph.ts
+++ b/app/src/layout/dock/Graph.ts
@@ -1,3 +1,4 @@
+import {bindPanelSearch} from "./panelSearch";
import {Tab} from "../Tab";
import {getInstanceById, setPanelFocus} from "../util";
import {getDockByType} from "../tabUtil";
@@ -264,6 +265,8 @@ export class Graph extends Model {
this.graphElement = this.element.querySelector(".graph__svg");
this.ensureGraphEngine();
this.inputElement = this.element.querySelector("input");
+ const showSearch = bindPanelSearch(this.inputElement,
+ this.element.querySelector('[data-type="search"]'), () => this.scheduleGraphSearch());
this.panelElement = this.element.querySelector(".graph__panel") as HTMLElement;
this.element.addEventListener("click", (event) => {
if (this.type === "local") {
@@ -297,8 +300,7 @@ export class Graph extends Model {
this.panelElement.style.right = "0";
}
} else if (dataType === "search") {
- target.previousElementSibling.classList.remove("fn__none");
- (target.previousElementSibling as HTMLInputElement).select();
+ showSearch();
} else if (dataType === "refresh") {
this.searchGraph({refresh: true});
} else if (dataType === "fullscreen") {
@@ -316,7 +318,9 @@ export class Graph extends Model {
}
break;
} else if (target.classList.contains("graph__svg")) {
- this.element.querySelectorAll(".block__icon.block__icon--active").forEach(item => {
+ // 图谱阻止了指针按下的默认行为,需要主动让搜索框失焦。
+ this.inputElement.blur();
+ this.element.querySelectorAll('.block__icon[data-type="menu"].block__icon--active').forEach(item => {
item.classList.remove("block__icon--active");
});
this.panelElement.style.right = "";
@@ -325,19 +329,6 @@ export class Graph extends Model {
target = target.parentElement;
}
});
- this.inputElement.addEventListener("compositionend", () => {
- this.scheduleGraphSearch();
- });
- this.inputElement.addEventListener("blur", (event: InputEvent) => {
- const inputElement = event.target as HTMLInputElement;
- inputElement.classList.add("fn__none");
- });
- this.inputElement.addEventListener("input", (event: InputEvent) => {
- if (event.isComposing) {
- return;
- }
- this.scheduleGraphSearch();
- });
this.element.querySelectorAll(".b3-slider").forEach((item: HTMLInputElement) => {
item.addEventListener("input", () => {
item.setAttribute("aria-label", item.value);
@@ -419,6 +410,9 @@ export class Graph extends Model {
window.siyuan.config.graph.local = conf as IGraphCommon & { dailyNote: boolean };
}
this.inputElement.value = "";
+ const searchElement = this.element.querySelector('[data-type="search"]');
+ searchElement.classList.remove("block__icon--active");
+ searchElement.setAttribute("aria-label", window.siyuan.languages.search);
this.panelElement.querySelector("[data-type='nodeSize']").setAttribute("aria-label", conf.d3.nodeSize.toString());
this.panelElement.querySelector("[data-type='centerStrength']").setAttribute("aria-label", conf.d3.centerStrength.toString());
this.panelElement.querySelector("[data-type='collideRadius']").setAttribute("aria-label", conf.d3.collideRadius.toString());
diff --git a/app/src/layout/dock/Outline.ts b/app/src/layout/dock/Outline.ts
index 7918a050d3..8fcb669cc8 100644
--- a/app/src/layout/dock/Outline.ts
+++ b/app/src/layout/dock/Outline.ts
@@ -1,3 +1,4 @@
+import {bindPanelSearch} from "./panelSearch";
import {Tab} from "../Tab";
import {Model} from "../Model";
import {Tree} from "../../util/Tree";
@@ -16,7 +17,7 @@ import {
import {openFileById} from "../../editor/util";
import {Constants} from "../../constants";
import {MenuItem} from "../../menus/Menu";
-import {escapeAttr, escapeHtml} from "../../util/escape";
+import {escapeHtml} from "../../util/escape";
import {getFileTreeIconHTML} from "../../emoji/fileTreeIcon";
import {getPreviousBlock} from "../../protyle/wysiwyg/getBlock";
import type {App} from "../../index";
@@ -104,24 +105,10 @@ export class Outline extends Model {
this.element = options.tab.panelElement.lastElementChild as HTMLElement;
this.headerElement = options.tab.panelElement.firstElementChild as HTMLElement;
const inputElement = this.headerElement.querySelector("input.b3-text-field.search__label") as HTMLInputElement;
- inputElement.addEventListener("blur", () => {
- inputElement.classList.add("fn__none");
- const filterIconElement = inputElement.nextElementSibling as HTMLElement; // search 图标
- const value = inputElement.value;
- if (value) {
- filterIconElement.classList.add("block__icon--active");
- filterIconElement.setAttribute("aria-label", window.siyuan.languages.search + " " + escapeAttr(value));
- } else {
- filterIconElement.classList.remove("block__icon--active");
- filterIconElement.setAttribute("aria-label", window.siyuan.languages.search);
- }
- });
- inputElement.addEventListener("input", (event: InputEvent) => {
- if (!event.isComposing) {
- this.setFilter();
- }
- });
- inputElement.addEventListener("compositionend", () => this.setFilter());
+ const showSearch = bindPanelSearch(inputElement,
+ this.headerElement.querySelector('[data-type="search"]'), () => this.setFilter(), {
+ trim: false,
+ });
this.tree = new Tree({
element: this.element,
data: null,
@@ -271,8 +258,7 @@ export class Outline extends Model {
isFocus = false;
break;
case "search":
- inputElement.classList.remove("fn__none");
- inputElement.select();
+ showSearch();
break;
case "expandLevel":
this.showExpandLevelMenu(target);
diff --git a/app/src/layout/dock/Tag.ts b/app/src/layout/dock/Tag.ts
index 82ef167107..48f59bdf88 100644
--- a/app/src/layout/dock/Tag.ts
+++ b/app/src/layout/dock/Tag.ts
@@ -1,4 +1,5 @@
import {Tab} from "../Tab";
+import {bindPanelSearch} from "./panelSearch";
import {Model} from "../Model";
import {Tree} from "../../util/Tree";
import {setPanelFocus} from "../util";
@@ -59,24 +60,8 @@ export class Tag extends Model {
</div>
<div class="fn__flex-1" style="margin-bottom: 8px"></div>`;
const inputElement = this.element.querySelector("input.b3-text-field.search__label") as HTMLInputElement;
- inputElement.addEventListener("blur", () => {
- inputElement.classList.add("fn__none");
- const filterIconElement = inputElement.nextElementSibling as HTMLElement;
- const value = inputElement.value;
- if (value.trim()) {
- filterIconElement.classList.add("block__icon--active");
- filterIconElement.setAttribute("aria-label", window.siyuan.languages.search + " " + value);
- } else {
- filterIconElement.classList.remove("block__icon--active");
- filterIconElement.setAttribute("aria-label", window.siyuan.languages.search);
- }
- });
- inputElement.addEventListener("input", (event: InputEvent) => {
- if (!event.isComposing) {
- this.filter();
- }
- });
- inputElement.addEventListener("compositionend", () => this.filter());
+ const showSearch = bindPanelSearch(inputElement,
+ this.element.querySelector('[data-type="search"]') as HTMLElement, () => this.filter());
this.tree = new Tree({
element: this.element.lastElementChild as HTMLElement,
@@ -178,8 +163,7 @@ export class Tag extends Model {
this.update();
break;
case "search":
- inputElement.classList.remove("fn__none");
- inputElement.select();
+ showSearch();
break;
}
}
diff --git a/app/src/layout/dock/panelSearch.ts b/app/src/layout/dock/panelSearch.ts
new file mode 100644
index 0000000000..26ef8d4bf5
--- /dev/null
+++ b/app/src/layout/dock/panelSearch.ts
@@ -0,0 +1,38 @@
+export const bindPanelSearch = (inputElement: HTMLInputElement, searchElement: Element, onFilter: () => void, options: {
+ trim?: boolean;
+ activeClass?: string | false;
+ updateLabel?: boolean;
+ trigger?: "input" | "enter";
+} = {}) => {
+ inputElement.addEventListener("blur", () => {
+ inputElement.classList.add("fn__none");
+ const value = inputElement.value;
+ const hasKeyword = Boolean(options.trim === false ? value : value.trim());
+ if (options.activeClass !== false) {
+ searchElement.classList.toggle(options.activeClass || "block__icon--active", hasKeyword);
+ }
+ if (options.updateLabel !== false) {
+ searchElement.setAttribute("aria-label", window.siyuan.languages.search + (hasKeyword ? " " + value : ""));
+ }
+ });
+ if (options.trigger === "enter") {
+ inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
+ if (!event.isComposing && event.key === "Enter") {
+ onFilter();
+ }
+ });
+ } else {
+ inputElement.addEventListener("input", (event: InputEvent) => {
+ if (!event.isComposing) {
+ onFilter();
+ }
+ });
+ inputElement.addEventListener("compositionend", () => onFilter());
+ }
+
+ // 由面板在处理焦点后调用,保持搜索输入框的聚焦顺序。
+ return () => {
+ inputElement.classList.remove("fn__none");
+ inputElement.select();
+ };
+};
diff --git a/app/src/mobile/dock/MobileBookmarks.ts b/app/src/mobile/dock/MobileBookmarks.ts
index 8bd94cdc86..2030a6acd1 100644
--- a/app/src/mobile/dock/MobileBookmarks.ts
+++ b/app/src/mobile/dock/MobileBookmarks.ts
@@ -1,3 +1,4 @@
+import {bindPanelSearch} from "../../layout/dock/panelSearch";
import {Tree} from "../../util/Tree";
import {fetchPost} from "../../util/fetch";
import {Constants} from "../../constants";
@@ -35,22 +36,10 @@ export class MobileBookmarks {
<div class="fn__flex-1 bookmarkList"></div>
<img style="position: absolute;top: 0;left: 0;height: 100%;width: 100%;padding: 30vw;box-sizing: border-box;" src="/stage/loading-pure.svg">`;
const inputElement = this.element.querySelector("input.b3-text-field.search__label") as HTMLInputElement;
- inputElement.addEventListener("blur", () => {
- inputElement.classList.add("fn__none");
- const filterIconElement = inputElement.nextElementSibling as HTMLElement;
- const value = inputElement.value;
- if (value.trim()) {
- filterIconElement.classList.add("toolbar__icon--active");
- } else {
- filterIconElement.classList.remove("toolbar__icon--active");
- }
- });
- inputElement.addEventListener("input", (event: InputEvent) => {
- if (!event.isComposing) {
- this.filter();
- }
- });
- inputElement.addEventListener("compositionend", () => this.filter());
+ bindPanelSearch(inputElement,
+ this.element.querySelector('[data-type="search"]'), () => this.filter(), {
+ activeClass: "toolbar__icon--active", updateLabel: false,
+ });
this.tree = new Tree({
element: this.element.querySelector(".bookmarkList") as HTMLElement,
diff --git a/app/src/mobile/dock/MobileOutline.ts b/app/src/mobile/dock/MobileOutline.ts
index 309f7dfa1d..380ef4f9c9 100644
--- a/app/src/mobile/dock/MobileOutline.ts
+++ b/app/src/mobile/dock/MobileOutline.ts
@@ -1,3 +1,4 @@
+import {bindPanelSearch} from "../../layout/dock/panelSearch";
import {Tree} from "../../util/Tree";
import {fetchPost} from "../../util/fetch";
import {confirmBlockRef} from "../../util/checkBlockRef";
@@ -78,22 +79,10 @@ export class MobileOutline extends Model {
<div class="b3-list-item fn__none" data-type="doc-title"></div>
<div class="fn__flex-1" style="padding: 3px 0 calc(8px + env(safe-area-inset-bottom))"></div>`;
const inputElement = this.element.querySelector("input.b3-text-field.search__label") as HTMLInputElement;
- inputElement.addEventListener("blur", () => {
- inputElement.classList.add("fn__none");
- const filterIconElement = inputElement.nextElementSibling as HTMLElement; // search 图标
- const value = inputElement.value;
- if (value) {
- filterIconElement.classList.add("toolbar__icon--active");
- } else {
- filterIconElement.classList.remove("toolbar__icon--active");
- }
- });
- inputElement.addEventListener("input", (event: InputEvent) => {
- if (!event.isComposing) {
- this.setFilter();
- }
- });
- inputElement.addEventListener("compositionend", () => this.setFilter());
+ const showSearch = bindPanelSearch(inputElement,
+ this.element.querySelector('[data-type="search"]'), () => this.setFilter(), {
+ trim: false, activeClass: "toolbar__icon--active", updateLabel: false,
+ });
this.tree = new Tree({
element: this.element.lastElementChild as HTMLElement,
data: null,
@@ -192,8 +181,7 @@ export class MobileOutline extends Model {
const type = target.getAttribute("data-type");
switch (type) {
case "search":
- inputElement.classList.remove("fn__none");
- inputElement.select();
+ showSearch();
break;
case "expandLevel":
this.showExpandLevelMenu();
diff --git a/app/src/mobile/dock/MobileTags.ts b/app/src/mobile/dock/MobileTags.ts
index e6b1aaf24c..7b93022e97 100644
--- a/app/src/mobile/dock/MobileTags.ts
+++ b/app/src/mobile/dock/MobileTags.ts
@@ -1,3 +1,4 @@
+import {bindPanelSearch} from "../../layout/dock/panelSearch";
import {Tree} from "../../util/Tree";
import {fetchPost} from "../../util/fetch";
import {hasClosestByClassName} from "../../protyle/util/hasClosest";
@@ -40,22 +41,10 @@ export class MobileTags {
<div class="fn__flex-1 tagList"></div>
<img style="position: absolute;top: 0;left: 0;height: 100%;width: 100%;padding: 30vw;box-sizing: border-box;" src="/stage/loading-pure.svg">`;
const inputElement = this.element.querySelector("input.b3-text-field.search__label") as HTMLInputElement;
- inputElement.addEventListener("blur", () => {
- inputElement.classList.add("fn__none");
- const filterIconElement = inputElement.nextElementSibling as HTMLElement;
- const value = inputElement.value;
- if (value.trim()) {
- filterIconElement.classList.add("toolbar__icon--active");
- } else {
- filterIconElement.classList.remove("toolbar__icon--active");
- }
- });
- inputElement.addEventListener("input", (event: InputEvent) => {
- if (!event.isComposing) {
- this.filter();
- }
- });
- inputElement.addEventListener("compositionend", () => this.filter());
+ bindPanelSearch(inputElement,
+ this.element.querySelector('[data-type="search"]'), () => this.filter(), {
+ activeClass: "toolbar__icon--active", updateLabel: false,
+ });
this.tree = new Tree({
element: this.element.querySelector(".tagList") as HTMLElement,