Skip to content

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

:bug: Apply bottom backlink expansion settings immediately https://github.com/siyuan-note/siyuan/issues/18436

Danielfd24a4a37397
diff --git a/app/src/config/tabs/editorRuntime.ts b/app/src/config/tabs/editorRuntime.ts
index b73d7225c7..fabdaf8499 100644
--- a/app/src/config/tabs/editorRuntime.ts
+++ b/app/src/config/tabs/editorRuntime.ts
@@ -4,10 +4,12 @@ import {refreshHeadingNumberMeasurements, setInlineStyle} from "../../util/asset
import {reloadProtyle} from "../../protyle/util/reload";
import {resize} from "../../protyle/util/resize";
import {createConfigNamespaceApi} from "../util/namespaceApi";
+import {shouldResetBottomBacklinkPanel} from "./editorRuntimeState";
const applyEditorConfig = (data: Config.IEditor) => {
const refreshKeepLoadedContent = window.siyuan.config.editor.keepLoadedContent !== data.keepLoadedContent;
const refreshDatabaseRowLayout = window.siyuan.config.editor.fullWidth !== data.fullWidth;
+ const resetBottomBacklinkPanel = shouldResetBottomBacklinkPanel(window.siyuan.config.editor, data);
const refreshHeadingNumbers = window.siyuan.config.editor.headingNumber !== data.headingNumber ||
window.siyuan.config.editor.headingNumberFormat !== data.headingNumberFormat;
const remeasureHeadingNumbers = window.siyuan.config.editor.fontSize !== data.fontSize ||
@@ -15,7 +17,7 @@ const applyEditorConfig = (data: Config.IEditor) => {
window.siyuan.config.editor.fontWeight !== data.fontWeight;
window.siyuan.config.editor = data;
const models = getAllModels();
- models.editor.forEach(item => item.updateBacklinkPanel());
+ models.editor.forEach(item => item.updateBacklinkPanel(resetBottomBacklinkPanel));
if (refreshDatabaseRowLayout) {
models.custom.forEach(item => {
if (item.type === "siyuan-database-row") {
diff --git a/app/src/config/tabs/editorRuntimeState.test.ts b/app/src/config/tabs/editorRuntimeState.test.ts
new file mode 100644
index 0000000000..a2d8d61e16
--- /dev/null
+++ b/app/src/config/tabs/editorRuntimeState.test.ts
@@ -0,0 +1,26 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {shouldResetBottomBacklinkPanel} from "./editorRuntimeState";
+
+describe("shouldResetBottomBacklinkPanel", () => {
+ it("keeps the panel when expand counts do not change", () => {
+ assert.equal(shouldResetBottomBacklinkPanel(
+ {backlinkExpandCount: 8, backmentionExpandCount: -1},
+ {backlinkExpandCount: 8, backmentionExpandCount: -1},
+ ), false);
+ });
+
+ it("resets the panel when the backlink expand count changes", () => {
+ assert.equal(shouldResetBottomBacklinkPanel(
+ {backlinkExpandCount: 8, backmentionExpandCount: -1},
+ {backlinkExpandCount: 10, backmentionExpandCount: -1},
+ ), true);
+ });
+
+ it("resets the panel when the mention expand count changes", () => {
+ assert.equal(shouldResetBottomBacklinkPanel(
+ {backlinkExpandCount: 8, backmentionExpandCount: 8},
+ {backlinkExpandCount: 8, backmentionExpandCount: 0},
+ ), true);
+ });
+});
diff --git a/app/src/config/tabs/editorRuntimeState.ts b/app/src/config/tabs/editorRuntimeState.ts
new file mode 100644
index 0000000000..1f2736fde3
--- /dev/null
+++ b/app/src/config/tabs/editorRuntimeState.ts
@@ -0,0 +1,6 @@
+type BacklinkExpandConfig = Pick<Config.IEditor, "backlinkExpandCount" | "backmentionExpandCount">;
+
+export const shouldResetBottomBacklinkPanel = (current: BacklinkExpandConfig, next: BacklinkExpandConfig) => {
+ return current.backlinkExpandCount !== next.backlinkExpandCount ||
+ current.backmentionExpandCount !== next.backmentionExpandCount;
+};
diff --git a/app/src/editor/index.ts b/app/src/editor/index.ts
index aed4433bb0..c0fe89914e 100644
--- a/app/src/editor/index.ts
+++ b/app/src/editor/index.ts
@@ -87,7 +87,10 @@ export class Editor extends Model {
this.editor.protyle.model = this;
}
- public updateBacklinkPanel() {
+ public updateBacklinkPanel(reset = false) {
+ if (reset) {
+ this.destroyBacklinkPanel();
+ }
if (!window.siyuan.config.editor.backlinkShowBottom) {
this.destroyBacklinkPanel();
return;
diff --git a/kernel/conf/editor.go b/kernel/conf/editor.go
index 956f9fb7d4..8d406390f9 100644
--- a/kernel/conf/editor.go
+++ b/kernel/conf/editor.go
@@ -79,6 +79,10 @@ const (
DefaultHeadingNumberFormat = "decimal-hierarchical"
)
+func NormalizeBacklinkExpandCount(count int) int {
+ return max(-1, count)
+}
+
func NewEditor() *Editor {
return &Editor{
FontSize: 16,
diff --git a/kernel/conf/editor_test.go b/kernel/conf/editor_test.go
new file mode 100644
index 0000000000..3596670a0d
--- /dev/null
+++ b/kernel/conf/editor_test.go
@@ -0,0 +1,40 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package conf
+
+import "testing"
+
+func TestNormalizeBacklinkExpandCount(t *testing.T) {
+ tests := []struct {
+ name string
+ count int
+ expected int
+ }{
+ {"preserves collapsed panel", -1, -1},
+ {"clamps unsupported negative values", -2, -1},
+ {"preserves folded contexts", 0, 0},
+ {"preserves expanded contexts", 10, 10},
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ if actual := NormalizeBacklinkExpandCount(test.count); actual != test.expected {
+ t.Fatalf("expected %d, got %d", test.expected, actual)
+ }
+ })
+ }
+}
diff --git a/kernel/model/conf.go b/kernel/model/conf.go
index 80400770fd..ddb87f789f 100644
--- a/kernel/model/conf.go
+++ b/kernel/model/conf.go
@@ -500,12 +500,8 @@ func InitConf() {
if 1 > len(Conf.Editor.SpellcheckLanguages) {
Conf.Editor.SpellcheckLanguages = []string{"en-US"}
}
- if 0 > Conf.Editor.BacklinkExpandCount {
- Conf.Editor.BacklinkExpandCount = 0
- }
- if -1 > Conf.Editor.BackmentionExpandCount {
- Conf.Editor.BackmentionExpandCount = -1
- }
+ Conf.Editor.BacklinkExpandCount = conf.NormalizeBacklinkExpandCount(Conf.Editor.BacklinkExpandCount)
+ Conf.Editor.BackmentionExpandCount = conf.NormalizeBacklinkExpandCount(Conf.Editor.BackmentionExpandCount)
if "" == Conf.Editor.HeadingNumberFormat {
Conf.Editor.HeadingNumberFormat = conf.DefaultHeadingNumberFormat
}