An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve reading position while moving split tabs
Danield720550297c1
diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.tsindex ba637da473..0360a59fa6 100644--- a/app/src/layout/Wnd.ts+++ b/app/src/layout/Wnd.ts@@ -49,7 +49,7 @@ import {closeWindow} from "../window/closeWin";import {newCenterEmptyTab, resizeTabs, setTabPosition} from "./tabUtil";import {setPosition} from "../util/setPosition";import {clearOBG} from "./dock/util";-import {recordBeforeResizeTop} from "../protyle/util/resize";+import {recordBeforeResizeTop, restoreBeforeResizeTop} from "../protyle/util/resize";import {isPhablet, sanitizeClosedTabs, setStorageVal} from "../protyle/util/compatibility";import {setTitle} from "../util/processTitle";import {dragOverScroll} from "../boot/globalEvent/dragover";@@ -1105,6 +1105,8 @@ export class Wnd {}public moveTab(tab: Tab, nextId?: string) {+ const protyle = tab.model instanceof Editor ? tab.model.editor.protyle : undefined;+ const scrollTop = protyle?.contentElement.scrollTop;let rangeData: {id: string,start: number,@@ -1177,6 +1179,12 @@ export class Wnd {tab.parent = this;hideAllElements(["toolbar"]);setTabPosition();+ if (protyle) {+ // 在浏览器绘制前恢复阅读位置,保留锚点供尺寸动画结束后再次校准。+ if (!restoreBeforeResizeTop(protyle, false)) {+ protyle.contentElement.scrollTop = scrollTop;+ }+ }}public split(direction: Config.TUILayoutDirection, after = true) {diff --git a/app/src/layout/moveTabScroll.test.ts b/app/src/layout/moveTabScroll.test.tsnew file mode 100644index 0000000000..12c4d1ff25--- /dev/null+++ b/app/src/layout/moveTabScroll.test.ts@@ -0,0 +1,80 @@+import {readFileSync} from "node:fs";+import {join} from "node:path";+import {runInNewContext} from "node:vm";+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import * as ts from "typescript";++const loadModule = (file: string, dependencies: Record<string, object>) => {+ const exports: Record<string, any> = {};+ const source = ts.transpileModule(readFileSync(join(__dirname, file), "utf8"), {+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},+ }).outputText;+ runInNewContext(source, {+ exports,+ window: {siyuan: {config: {fileTree: {maxOpenTabCount: 10}}}},+ require: (name: string) => dependencies[name] || {},+ });+ return exports;+};++const resize = loadModule("../protyle/util/resize.ts", {});++describe("moving an editor tab preserves its reading position", () => {+ for (const anchor of [true, false]) {+ it(`restores before returning after DOM movement and focus, with anchor=${anchor}`, () => {+ let marked = anchor;+ let blockTop = 1700;+ const content = {+ scrollTop: 1726.5,+ getBoundingClientRect: () => ({top: 80}),+ };+ const block = {+ getBoundingClientRect: () => ({top: 80 + blockTop - content.scrollTop}),+ getAttribute: () => "26.5",+ removeAttribute: () => { marked = false; },+ };+ const protyle = {+ contentElement: content,+ wysiwyg: {element: {querySelector: () => marked ? block : null}},+ toolbar: {},+ };+ class Editor {+ editor = {protyle};+ }+ const {Wnd} = loadModule("Wnd.ts", {+ "../editor": {Editor},+ "../protyle/util/resize": resize,+ "../protyle/ui/hideElements": {hideAllElements: () => {}},+ "./tabUtil": {setTabPosition: () => {}, resizeTabs: () => {}},+ });+ const tab = {+ model: new Editor(),+ parent: {+ children: [{}],+ remove: () => { blockTop = 1300; },+ },+ };+ const target = Object.create(Wnd.prototype);+ target.element = {querySelector: () => ({append: () => { content.scrollTop = 0; }})};+ target.children = [];+ target.switchTab = () => { content.scrollTop = 0; };++ target.moveTab(tab);++ assert.equal(content.scrollTop, anchor ? 1326.5 : 1726.5);+ assert.equal(tab.parent, target);+ assert.equal(target.children[0], tab);+ assert.equal(marked, anchor);+ if (anchor) {+ // 尺寸动画改变块位置后,延迟校准仍使用同一锚点,且不会重复叠加偏移。+ blockTop = 1400;+ assert.equal(resize.restoreBeforeResizeTop(protyle), true);+ assert.equal(content.scrollTop, 1426.5);+ assert.equal(marked, false);+ assert.equal(resize.restoreBeforeResizeTop(protyle), false);+ assert.equal(content.scrollTop, 1426.5);+ }+ });+ }+});diff --git a/app/src/protyle/util/resize.ts b/app/src/protyle/util/resize.tsindex 7ad6e92a88..87750951e1 100644--- a/app/src/protyle/util/resize.ts+++ b/app/src/protyle/util/resize.ts@@ -47,6 +47,20 @@ export const recordBeforeResizeTop = () => {/// #endif};+export const restoreBeforeResizeTop = (protyle: IProtyle, clear = true) => {+ const topElement = protyle.wysiwyg.element.querySelector("[data-resize-top]");+ if (!topElement) {+ return false;+ }+ // 只调整编辑器的滚动位置,避免滚动外层布局,并保留块内的小数偏移。+ protyle.contentElement.scrollTop += topElement.getBoundingClientRect().top -+ protyle.contentElement.getBoundingClientRect().top + parseFloat(topElement.getAttribute("data-resize-top"));+ if (clear) {+ topElement.removeAttribute("data-resize-top");+ }+ return true;+};+export const resize = (protyle: IProtyle) => {hideElements(["gutterOnly"], protyle);const abs = setPadding(protyle);@@ -75,11 +89,6 @@ export const resize = (protyle: IProtyle) => {lineNumberRender(item.parentElement);}});- const topElement = protyle.wysiwyg.element.querySelector("[data-resize-top]");- if (topElement) {- topElement.scrollIntoView();- protyle.contentElement.scrollTop += parseInt(topElement.getAttribute("data-resize-top"));- topElement.removeAttribute("data-resize-top");- }+ restoreBeforeResizeTop(protyle);}, Constants.TIMEOUT_TRANSITION + 100); // 等待 setPadding 动画结束};