Skip to content

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

:art: Refine document-bottom backlinks https://github.com/siyuan-note/siyuan/issues/18319

Danielab15722b032a
diff --git a/app/src/assets/scss/protyle/_protyle.scss b/app/src/assets/scss/protyle/_protyle.scss
index adc734f81c..f781ccd3db 100644
--- a/app/src/assets/scss/protyle/_protyle.scss
+++ b/app/src/assets/scss/protyle/_protyle.scss
@@ -366,6 +366,23 @@
> .block__icons {
border-top: 1px solid var(--b3-border-color);
flex-shrink: 0;
+ position: relative;
+
+ > .block__icon.backlinkList__toggle {
+ display: flex;
+ margin-left: -8px;
+ opacity: 1;
+
+ + .block__logo {
+ margin-left: 4px;
+ }
+ }
+
+ > .search__label {
+ top: 7px;
+ max-width: calc(100% - 16px);
+ box-sizing: border-box;
+ }
}
> .backlinkList,
@@ -374,6 +391,17 @@
min-height: auto;
overflow: visible;
}
+
+ .backlinkList__loading {
+ align-items: center;
+ display: flex;
+ height: 40px;
+ justify-content: center;
+ }
+
+ > .backlinkList--divider-spacing:not(.fn__none) + .block__icons {
+ margin-top: 8px;
+ }
}
.protyle-top:hover .protyle-background--enable {
diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.ts
index 35eedfd173..2a31dd26d5 100644
--- a/app/src/boot/globalEvent/keydown.ts
+++ b/app/src/boot/globalEvent/keydown.ts
@@ -1022,12 +1022,16 @@ const panelTreeKeydown = (app: App, event: KeyboardEvent) => {
if (matchCommand) {
return true;
}
- if (!matchHotKey(window.siyuan.config.keymap.editor.general.collapse.custom, event) &&
- !matchHotKey(window.siyuan.config.keymap.editor.general.expand.custom, event) &&
+ const matchCollapse = matchHotKey(window.siyuan.config.keymap.editor.general.collapse.custom, event);
+ const matchExpand = matchHotKey(window.siyuan.config.keymap.editor.general.expand.custom, event);
+ if (bottomBacklink && (matchCollapse || matchExpand)) {
+ return false;
+ }
+ if (!matchCollapse && !matchExpand &&
!event.key.startsWith("Arrow") && event.key !== "Enter") {
return false;
}
- if (!event.repeat && matchHotKey(window.siyuan.config.keymap.editor.general.collapse.custom, event)) {
+ if (!event.repeat && matchCollapse) {
const collapseElement = activePanelElement.querySelector('.block__icon[data-type="collapse"]');
if (collapseElement) {
collapseElement.dispatchEvent(new CustomEvent("click"));
@@ -1035,7 +1039,7 @@ const panelTreeKeydown = (app: App, event: KeyboardEvent) => {
return true;
}
}
- if (!event.repeat && matchHotKey(window.siyuan.config.keymap.editor.general.expand.custom, event)) {
+ if (!event.repeat && matchExpand) {
const expandElement = activePanelElement.querySelector('.block__icon[data-type="expand"]');
if (expandElement) {
expandElement.dispatchEvent(new CustomEvent("click"));
diff --git a/app/src/editor/index.ts b/app/src/editor/index.ts
index 5b75683e2e..2ea163c20d 100644
--- a/app/src/editor/index.ts
+++ b/app/src/editor/index.ts
@@ -162,7 +162,8 @@ export class Editor extends Model {
return;
}
const lastElement = this.editor.protyle.wysiwyg.element.lastElementChild;
- const hidden = lastElement?.getAttribute("data-eof") !== "2";
+ const hidden = !this.editor.protyle.block.showAll && this.editor.protyle.block.scroll &&
+ lastElement?.getAttribute("data-eof") !== "2";
if (this.backlinkElement.classList.contains("fn__none") !== hidden) {
this.backlinkElement.classList.toggle("fn__none", hidden);
setPadding(this.editor.protyle);
diff --git a/app/src/editor/util.ts b/app/src/editor/util.ts
index 3dd678175f..69eba8a109 100644
--- a/app/src/editor/util.ts
+++ b/app/src/editor/util.ts
@@ -716,7 +716,7 @@ export const updateBacklinkGraph = (models: IModels, protyle: IProtyle) => {
item.inputsElement[0].value = backlinkKeyword;
item.inputsElement[1].value = backmentionKeyword;
item.markDirty();
- item.refreshIfVisible();
+ item.refreshIfVisible(true);
return;
}
if (item.type === "local" && item.rootId !== protyle?.block?.rootID) {
diff --git a/app/src/layout/dock/Backlink.ts b/app/src/layout/dock/Backlink.ts
index bf644fe8e5..5650f009f6 100644
--- a/app/src/layout/dock/Backlink.ts
+++ b/app/src/layout/dock/Backlink.ts
@@ -40,6 +40,7 @@ export class Backlink extends Model {
} = {};
private dirty = false;
private destroyed = false;
+ private ownerFocusoutListener?: (event: FocusEvent) => void;
constructor(options: {
app: App,
@@ -70,13 +71,17 @@ export class Backlink extends Model {
if (this.type === "bottom") {
this.element.classList.add("sy__backlink--bottom");
this.element.tabIndex = -1;
- this.element.addEventListener("focusout", () => {
- window.setTimeout(() => this.refreshIfVisible());
- });
+ this.ownerFocusoutListener = (event: FocusEvent) => {
+ if (!event.relatedTarget || !this.ownerProtyle.element.contains(event.relatedTarget as Node)) {
+ this.refreshIfVisible(true);
+ }
+ };
+ this.ownerProtyle.element.addEventListener("focusout", this.ownerFocusoutListener);
}
const backlinkSort = window.siyuan.config.editor.backlinkSort;
const backmentionSort = window.siyuan.config.editor.backmentionSort;
this.element.innerHTML = `<div class="block__icons">
+ ${this.type === "bottom" ? `<span data-type="bLayout" class="block__icon block__icon--show fn__flex-center backlinkList__toggle ariaLabel" data-position="north" aria-label="${window.siyuan.languages.collapse}"><svg><use xlink:href="#iconDown"></use></svg></span>` : ""}
<div class="block__logo fn__flex-1${this.type === "bottom" ? " fn__pointer" : ""}"${this.type === "bottom" ? ' data-type="backlink"' : ""}>${window.siyuan.languages.backlinks}</div>
<span class="counter listCount" style="margin-left: 0"></span>
<span class="fn__space"></span>
@@ -87,20 +92,19 @@ export class Backlink extends Model {
<span class="fn__space"></span>
<span data-type="sort" data-sort="${backlinkSort}" class="block__icon ariaLabel" data-position="north" aria-label="${window.siyuan.languages.sort}"><svg><use xlink:href='#iconSort'></use></svg></span>
<span class="fn__space"></span>
- <span data-type="expand" class="block__icon ariaLabel" data-position="north" aria-label="${window.siyuan.languages.expand}${updateHotkeyAfterTip(window.siyuan.config.keymap.editor.general.expand.custom)}">
+ <span data-type="expand" class="block__icon ariaLabel" data-position="north" aria-label="${window.siyuan.languages.expand}${this.type === "bottom" ? "" : updateHotkeyAfterTip(window.siyuan.config.keymap.editor.general.expand.custom)}">
<svg><use xlink:href="#iconExpand"></use></svg>
</span>
<span class="fn__space"></span>
- <span data-type="collapse" class="block__icon ariaLabel" data-position="north" aria-label="${window.siyuan.languages.collapse}${updateHotkeyAfterTip(window.siyuan.config.keymap.editor.general.collapse.custom)}">
+ <span data-type="collapse" class="block__icon ariaLabel" data-position="north" aria-label="${window.siyuan.languages.collapse}${this.type === "bottom" ? "" : updateHotkeyAfterTip(window.siyuan.config.keymap.editor.general.collapse.custom)}">
<svg><use xlink:href="#iconContract"></use></svg>
</span>
- <span class="${this.type === "bottom" ? "" : "fn__none "}fn__space"></span>
- <span data-type="bLayout" class="${this.type === "bottom" ? "" : "fn__none "}block__icon ariaLabel" data-position="north" aria-label="${window.siyuan.languages.collapse}"><svg><use xlink:href='#iconDown'></use></svg></span>
<span class="${this.type !== "pin" ? "fn__none " : ""}fn__space"></span>
<span data-type="min" class="${this.type !== "pin" ? "fn__none " : ""}block__icon ariaLabel" data-position="north" aria-label="${window.siyuan.languages.min}${updateHotkeyAfterTip(window.siyuan.config.keymap.general.closeTab.custom)}"><svg><use xlink:href='#iconMin'></use></svg></span>
</div>
<div class="backlinkList fn__flex-1"></div>
<div class="block__icons">
+ ${this.type === "bottom" ? `<span data-type="layout" class="block__icon block__icon--show fn__flex-center backlinkList__toggle ariaLabel" data-position="north" aria-label="${window.siyuan.languages.collapse}"><svg><use xlink:href="#iconDown"></use></svg></span>` : ""}
<div class="block__logo fn__flex-1 fn__pointer" data-type="mention">${window.siyuan.languages.mentions}</div>
<span class="counter listMCount" style="margin-left: 0;"></span>
<span class="fn__space"></span>
@@ -116,10 +120,10 @@ export class Backlink extends Model {
<span data-type="mCollapse" class="block__icon b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages.collapse}">
<svg><use xlink:href="#iconContract"></use></svg>
</span>
- <span class="fn__space"></span>
+ ${this.type === "bottom" ? "" : `<span class="fn__space"></span>
<span data-type="layout" class="block__icon b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages.down}">
<svg><use xlink:href="#iconDown"></use></svg>
- </span>
+ </span>`}
</div>
<div class="backlinkMList fn__flex-1"></div>`;
@@ -143,6 +147,14 @@ export class Backlink extends Model {
}
});
});
+ this.element.querySelectorAll('[data-type="search"]').forEach((item, index) => {
+ item.addEventListener("click", (event) => {
+ event.stopPropagation();
+ const inputElement = this.inputsElement[index];
+ inputElement.classList.remove("fn__none");
+ inputElement.select();
+ });
+ });
this.tree = new Tree({
element: this.element.querySelector(".backlinkList") as HTMLElement,
data: null,
@@ -250,6 +262,7 @@ export class Backlink extends Model {
this.tree.element.querySelectorAll(".b3-list-item__arrow").forEach(item => {
item.classList.remove("b3-list-item__arrow--open");
});
+ this.updateBottomBacklinkSpacing();
});
this.element.querySelector('[data-type="expand"]').addEventListener("click", () => {
Array.from(this.tree.element.firstElementChild.children).forEach((item: HTMLElement) => {
@@ -294,11 +307,6 @@ export class Backlink extends Model {
getDockByType("backlink").toggleModel("backlink", false, true);
event.stopPropagation();
break;
- case "search":
- target.previousElementSibling.classList.remove("fn__none");
- (target.previousElementSibling as HTMLInputElement).select();
- event.stopPropagation();
- break;
case "sort":
case "mSort":
this.showSortMenu(type, target.getAttribute("data-sort"));
@@ -337,6 +345,7 @@ export class Backlink extends Model {
}
});
+ this.showBottomLoading();
this.searchBacklinks(true);
}
@@ -527,6 +536,7 @@ export class Backlink extends Model {
}
});
svgElement.removeAttribute("disabled");
+ this.updateBottomBacklinkSpacing();
} else {
const keyword = isMention ? this.inputsElement[1].value : this.inputsElement[0].value;
const blockId = this.blockId;
@@ -541,6 +551,7 @@ export class Backlink extends Model {
}
svgElement.removeAttribute("disabled");
svgElement.classList.add("b3-list-item__arrow--open");
+ this.updateBottomBacklinkSpacing();
const editorElement = document.createElement("div");
editorElement.style.minHeight = "auto";
editorElement.setAttribute("data-defid", blockId);
@@ -569,6 +580,25 @@ export class Backlink extends Model {
}
}
+ private updateBottomBacklinkSpacing() {
+ if (this.type !== "bottom") {
+ return;
+ }
+ const lastItem = this.tree.element.querySelector(":scope > .b3-list > .b3-list-item:last-of-type");
+ this.tree.element.classList.toggle("backlinkList--divider-spacing",
+ !lastItem || !lastItem.querySelector(".b3-list-item__arrow--open"));
+ }
+
+ private showBottomLoading() {
+ if (this.type !== "bottom") {
+ return;
+ }
+ const loadingHTML = '<div class="backlinkList__loading"><img width="32px" height="32px" src="/stage/loading-pure.svg"></div>';
+ this.tree.element.innerHTML = loadingHTML;
+ this.mTree.element.innerHTML = loadingHTML;
+ this.updateBottomBacklinkSpacing();
+ }
+
public refresh() {
const element = this.element.querySelector('.block__icon[data-type="refresh"] svg');
if (!this.blockId || element.classList.contains("fn__rotate")) {
@@ -693,6 +723,7 @@ export class Backlink extends Model {
this.inputsElement[1].value = data.mk;
this.tree.updateData(data.backlinks);
this.mTree.updateData(data.backmentions);
+ this.updateBottomBacklinkSpacing();
const countElement = this.element.querySelector(".listCount");
if (data.linkRefsCount === 0 && this.type !== "bottom") {
@@ -810,12 +841,11 @@ export class Backlink extends Model {
this.dirty = true;
}
- public refreshIfVisible() {
+ public refreshIfVisible(ignoreFocus = false) {
if (this.type !== "bottom" || !this.dirty) {
return;
}
- const activeProtyleElement = document.activeElement?.closest(".protyle");
- if (activeProtyleElement && this.element.contains(activeProtyleElement)) {
+ if (!ignoreFocus && this.ownerProtyle.element.contains(document.activeElement)) {
return;
}
if (this.element.classList.contains("fn__none") ||
@@ -839,6 +869,9 @@ export class Backlink extends Model {
public destroy() {
this.destroyed = true;
+ if (this.ownerFocusoutListener) {
+ this.ownerProtyle.element.removeEventListener("focusout", this.ownerFocusoutListener);
+ }
this.editors.forEach(item => item.destroy());
this.editors = [];
if (this.ws) {
diff --git a/app/src/protyle/ui/initUI.ts b/app/src/protyle/ui/initUI.ts
index bff92c66ea..3b2ecac0cd 100644
--- a/app/src/protyle/ui/initUI.ts
+++ b/app/src/protyle/ui/initUI.ts
@@ -333,8 +333,8 @@ export const setPadding = (protyle: IProtyle) => {
};
}
const padding = getPadding(protyle);
- const paddingLeft = padding.left;
- const paddingRight = padding.right;
+ const paddingLeft = protyle.options.backlinkData ? 24 : padding.left;
+ const paddingRight = protyle.options.backlinkData ? 16 : padding.right;
const backlinkBottomElement = protyle.contentElement.querySelector(".sy__backlink--bottom") as HTMLElement;
const backlinkBottomVisible = backlinkBottomElement && !backlinkBottomElement.classList.contains("fn__none");