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: Stabilize PDF navigation history restoration https://github.com/siyuan-note/siyuan/issues/11750
Daniel98e854941224
diff --git a/app/src/asset/pdf/navigationHistory.test.ts b/app/src/asset/pdf/navigationHistory.test.tsindex 51179a0e51..471bb58571 100644--- a/app/src/asset/pdf/navigationHistory.test.ts+++ b/app/src/asset/pdf/navigationHistory.test.ts@@ -4,6 +4,7 @@ import {PDFNavigationHistory, PDFNavigationStack} from "./navigationHistory";class TestEventBus {private listeners = new Map<string, Array<(event: any) => void>>();+ public lastHistoryState: {canGoBack: boolean, canGoForward: boolean};public _on(eventName: string, listener: (event: any) => void, options?: {signal?: AbortSignal}) {const listeners = this.listeners.get(eventName) || [];@@ -15,15 +16,23 @@ class TestEventBus {}public dispatch(eventName: string, data: unknown) {+ if (eventName === "pdfhistorystatechanged") {+ this.lastHistoryState = data as {canGoBack: boolean, canGoForward: boolean};+ }(this.listeners.get(eventName) || []).forEach((listener) => listener(data));}- public update(page: number, top = 0) {+ public update(page: number, top = 0, rotation = 0) {+ this.updateHash(`page=${page}&zoom=100,0,${top}`, rotation);+ }++ public updateHash(hash: string, rotation = 0) {+ const page = Number.parseInt(new URLSearchParams(hash).get("page"), 10);this.dispatch("updateviewarea", {location: {pageNumber: page,- pdfOpenParams: `#page=${page}&zoom=100,0,${top}`,- rotation: 0,+ pdfOpenParams: `#${hash}`,+ rotation,},});}@@ -32,32 +41,57 @@ class TestEventBus {class TestLinkService {public page = 1;public pagesCount = 20;- public rotation = 0;+ public autoUpdate = true;public lastHash = "";+ public lastDestination: string | unknown[];+ public operations: string[] = [];private readonly eventBus: TestEventBus;+ private currentRotation = 0;constructor(eventBus: TestEventBus) {this.eventBus = eventBus;}- public async goToDestination() {- this.eventBus.update(this.page);+ public get rotation() {+ return this.currentRotation;+ }++ public set rotation(rotation: number) {+ this.currentRotation = rotation;+ this.operations.push(`rotation:${rotation}`);+ }++ public async goToDestination(destination: string | unknown[]) {+ this.lastDestination = destination;+ this.operations.push("destination");+ if (this.autoUpdate) {+ this.eventBus.update(this.page, 0, this.rotation);+ }}public setHash(hash: string) {this.lastHash = hash;+ this.operations.push(`hash:${hash}`);const page = Number.parseInt(new URLSearchParams(hash).get("page"), 10);if (Number.isInteger(page)) {this.page = page;}- this.eventBus.update(this.page);+ if (this.autoUpdate) {+ this.eventBus.updateHash(hash, this.rotation);+ }}}-const createHistory = () => {+const createHistory = (options: {autoUpdate?: boolean, navigationTimeoutMs?: number} = {}) => {const eventBus = new TestEventBus();const linkService = new TestLinkService(eventBus);- const history = new PDFNavigationHistory({eventBus, linkService, limit: 64});+ linkService.autoUpdate = options.autoUpdate ?? true;+ const history = new PDFNavigationHistory({+ eventBus,+ linkService,+ limit: 64,+ navigationTimeoutMs: options.navigationTimeoutMs,+ });history.initialize();return {eventBus, history, linkService};};@@ -182,4 +216,82 @@ describe("PDFNavigationHistory", () => {first.history.reset();second.history.reset();});++ it("ignores stale view updates while restoring an entry", async () => {+ const {eventBus, history, linkService} = createHistory({autoUpdate: false});+ eventBus.update(1);+ history.pushCurrentPosition();+ history.pushPage(8);+ linkService.page = 8;+ eventBus.update(8);++ await history.back();+ assert.equal(eventBus.lastHistoryState.canGoForward, false);+ eventBus.update(8);+ assert.equal(eventBus.lastHistoryState.canGoForward, false);++ eventBus.updateHash("page=1&zoom=100,0,0");+ assert.equal(eventBus.lastHistoryState.canGoForward, true);+ await history.forward();+ assert.equal(linkService.lastHash, "page=8&zoom=100,0,0");+ history.reset();+ });++ it("preserves an explicit destination after recording its landing position", async () => {+ const {eventBus, history, linkService} = createHistory();+ const destination = [{num: 12, gen: 0}, {name: "XYZ"}, 0, 200, null];+ eventBus.update(1);+ history.pushCurrentPosition();+ history.push({explicitDest: destination, pageNumber: 8});+ linkService.page = 8;+ eventBus.update(8);++ await history.back();+ linkService.autoUpdate = false;+ await history.forward();+ assert.deepEqual(linkService.lastDestination, destination);+ history.reset();+ });++ it("restores rotation before coordinates", async () => {+ const {eventBus, history, linkService} = createHistory();+ history.seedPreviousPosition({+ page: 2,+ rotation: 90,+ scrollLeft: 10,+ scrollTop: 200,+ zoom: 125,+ });+ linkService.page = 8;+ eventBus.update(8);+ linkService.operations = [];++ await history.back();+ assert.deepEqual(linkService.operations.slice(0, 2), [+ "rotation:90",+ "hash:page=2&zoom=125,10,200",+ ]);+ history.reset();+ });++ it("does not replace an entry when navigation times out", async () => {+ const {eventBus, history, linkService} = createHistory({+ autoUpdate: false,+ navigationTimeoutMs: 5,+ });+ eventBus.update(1);+ history.pushCurrentPosition();+ history.pushPage(8);+ linkService.page = 8;+ eventBus.update(8);++ await history.back();+ eventBus.update(8);+ await new Promise((resolve) => setTimeout(resolve, 20));+ await history.forward();+ eventBus.updateHash("page=8&zoom=100,0,0");+ await history.back();+ assert.equal(linkService.lastHash, "page=1&zoom=100,0,0");+ history.reset();+ });});diff --git a/app/src/asset/pdf/navigationHistory.ts b/app/src/asset/pdf/navigationHistory.tsindex 848c821334..d2e96e599d 100644--- a/app/src/asset/pdf/navigationHistory.ts+++ b/app/src/asset/pdf/navigationHistory.ts@@ -122,7 +122,9 @@ export class PDFNavigationHistory {private appendInitialPosition = false;private awaitingLanding = false;private navigationStartHash: string;+ private navigationTarget: PDFNavigationEntry;private navigationTimeout: ReturnType<typeof setTimeout>;+ private readonly navigationTimeoutMs: number;private position: PDFNavigationEntry;private restoring = false;@@ -130,10 +132,12 @@ export class PDFNavigationHistory {eventBus: PDFNavigationEventBus,linkService: PDFNavigationLinkService,limit: number,+ navigationTimeoutMs?: number,}) {this.eventBus = options.eventBus;this.linkService = options.linkService;this.stack = new PDFNavigationStack(options.limit);+ this.navigationTimeoutMs = options.navigationTimeoutMs ?? 1000;}public get initialBookmark(): string | null {@@ -168,6 +172,7 @@ export class PDFNavigationHistory {this.appendInitialPosition = false;this.awaitingLanding = false;this.navigationStartHash = undefined;+ this.navigationTarget = undefined;this.restoring = false;this.dispatchState();}@@ -200,6 +205,7 @@ export class PDFNavigationHistory {}const current = this.stack.current;this.stack.replace({+ ...current,...this.position,key: current?.key,landingHash: current?.landingHash,@@ -265,6 +271,7 @@ export class PDFNavigationHistory {this.stack.push(entry, () => false);this.awaitingLanding = true;this.navigationStartHash = this.position?.hash;+ this.navigationTarget = entry;this.startNavigationTimeout();this.dispatchState();}@@ -275,6 +282,7 @@ export class PDFNavigationHistory {}const current = this.stack.current;this.stack.replace({+ ...current,...this.position,key: current?.key,landingHash: current?.landingHash,@@ -284,8 +292,12 @@ export class PDFNavigationHistory {private async restore(entry: PDFNavigationEntry) {this.restoring = true;this.navigationStartHash = this.position?.hash;+ this.navigationTarget = entry;this.dispatchState();try {+ if (Number.isInteger(entry.rotation)) {+ this.linkService.rotation = entry.rotation;+ }if (entry.dest) {await this.linkService.goToDestination(entry.dest);} else if (entry.hash) {@@ -293,9 +305,6 @@ export class PDFNavigationHistory {} else if (entry.page) {this.linkService.page = entry.page;}- if (Number.isInteger(entry.rotation)) {- this.linkService.rotation = entry.rotation;- }} finally {if (this.restoring) {this.startNavigationTimeout();@@ -308,14 +317,19 @@ export class PDFNavigationHistory {if (!hash) {return;}- this.position = {+ const position = {hash,page: this.linkService.page || event.location.pageNumber,rotation: event.location.rotation,};if (!this.initialized) {+ this.position = position;+ return;+ }+ if ((this.awaitingLanding || this.restoring) && !this.isNavigationTarget(position)) {return;}+ this.position = position;const current = this.stack.current;if (!current) {@@ -325,39 +339,48 @@ export class PDFNavigationHistory {this.appendInitialPosition = false;} else if (this.awaitingLanding || this.restoring) {this.stack.replace({+ ...current,...this.position,key: current.key,landingHash: this.awaitingLanding ? hash : current.landingHash,});- }-- if (this.awaitingLanding || this.restoring) {this.awaitingLanding = false;this.restoring = false;this.navigationStartHash = undefined;+ this.navigationTarget = undefined;this.clearNavigationTimeout();}this.dispatchState();}+ private isNavigationTarget(position: PDFNavigationEntry) {+ if (!this.navigationTarget) {+ return false;+ }+ if (Number.isInteger(this.navigationTarget.rotation) &&+ position.rotation !== this.navigationTarget.rotation) {+ return false;+ }+ if (this.navigationTarget.hash?.includes("&zoom=")) {+ return position.hash === this.navigationTarget.hash;+ }+ if (this.navigationTarget.page && position.page !== this.navigationTarget.page) {+ return false;+ }+ return position.hash !== this.navigationStartHash;+ }+private startNavigationTimeout() {this.clearNavigationTimeout();this.navigationTimeout = setTimeout(() => {- if (this.position && this.position.hash !== this.navigationStartHash &&- (this.awaitingLanding || this.restoring)) {- const current = this.stack.current;- this.stack.replace({- ...this.position,- key: current?.key,- landingHash: this.awaitingLanding ? this.position.hash : current?.landingHash,- });- }+ this.position = undefined;this.awaitingLanding = false;this.restoring = false;this.navigationStartHash = undefined;+ this.navigationTarget = undefined;this.navigationTimeout = undefined;this.dispatchState();- }, 1000);+ }, this.navigationTimeoutMs);}private clearNavigationTimeout() {