From c3793a7288a21a34937f8e3597c7ad464dec47d6 Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Mon, 12 Jul 2021 22:35:29 -0400 Subject: [PATCH 1/3] Initial spike to support same-page anchor scrolling through the current visit --- turbo/src/main/assets/js/turbo_bridge.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/turbo/src/main/assets/js/turbo_bridge.js b/turbo/src/main/assets/js/turbo_bridge.js index 62d6450d..30c7bd3e 100644 --- a/turbo/src/main/assets/js/turbo_bridge.js +++ b/turbo/src/main/assets/js/turbo_bridge.js @@ -92,7 +92,11 @@ // Adapter interface visitProposedToLocation(location, options) { - TurboSession.visitProposedToLocation(location.toString(), JSON.stringify(options)) + if (window.Turbo && this.currentVisit?.locationIsSamePageAnchor(location)) { + this.currentVisit.scrollToAnchor(location) + } else { + TurboSession.visitProposedToLocation(location.toString(), JSON.stringify(options)) + } } // Turbolinks 5 From fad98f611b62471d045cfcc88f86079e63cb876f Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Wed, 14 Jul 2021 09:55:19 -0400 Subject: [PATCH 2/3] Check to see if the proposed visit is a same page anchor through Turbo.navigator --- turbo/src/main/assets/js/turbo_bridge.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/turbo/src/main/assets/js/turbo_bridge.js b/turbo/src/main/assets/js/turbo_bridge.js index 30c7bd3e..11211b99 100644 --- a/turbo/src/main/assets/js/turbo_bridge.js +++ b/turbo/src/main/assets/js/turbo_bridge.js @@ -92,8 +92,8 @@ // Adapter interface visitProposedToLocation(location, options) { - if (window.Turbo && this.currentVisit?.locationIsSamePageAnchor(location)) { - this.currentVisit.scrollToAnchor(location) + if (window.Turbo && Turbo.navigator.locationWithActionIsSamePage(location, options.action)) { + Turbo.navigator.view.scrollToAnchorFromLocation(location) } else { TurboSession.visitProposedToLocation(location.toString(), JSON.stringify(options)) } From b836b8d6e9ff2ef1f1a029eafbcff43f20ba1278 Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Tue, 7 Sep 2021 21:15:22 -0400 Subject: [PATCH 3/3] Add forward/backward support for same-page anchor scrolling --- turbo/src/main/assets/js/turbo_bridge.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/turbo/src/main/assets/js/turbo_bridge.js b/turbo/src/main/assets/js/turbo_bridge.js index 2e002e8a..1cdd237e 100644 --- a/turbo/src/main/assets/js/turbo_bridge.js +++ b/turbo/src/main/assets/js/turbo_bridge.js @@ -92,11 +92,14 @@ // Adapter interface visitProposedToLocation(location, options) { - if (window.Turbo && Turbo.navigator.locationWithActionIsSamePage(location, options.action)) { - Turbo.navigator.view.scrollToAnchorFromLocation(location) - } else { - TurboSession.visitProposedToLocation(location.toString(), JSON.stringify(options)) + if (window.Turbo && typeof Turbo.navigator.locationWithActionIsSamePage === "function") { + if (Turbo.navigator.locationWithActionIsSamePage(location, options.action)) { + Turbo.navigator.view.scrollToAnchorFromLocation(location) + return + } } + + TurboSession.visitProposedToLocation(location.toString(), JSON.stringify(options)) } // Turbolinks 5