Skip to content

Commit

Permalink
imp: Preserve scroll on links actions
Browse files Browse the repository at this point in the history
  • Loading branch information
marienfressinaud committed Sep 21, 2022
1 parent 7a63d9a commit 7124f45
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 15 deletions.
24 changes: 24 additions & 0 deletions src/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@ function adaptLayoutContentBorderRadius () {
}

document.addEventListener('turbo:load', adaptLayoutContentBorderRadius);

// Allow to disable scroll on form submission.
// Submitting forms with a `data-turbo-preserve-scroll` attribute will keep the
// scroll position at the current position.
let disableScroll = false;

addEventListener('turbo:submit-start', (event) => {
if (event.target.hasAttribute('data-turbo-preserve-scroll')) {
disableScroll = true;
}
});

addEventListener('turbo:before-render', (event) => {
if (disableScroll) {
// As explained on GitHub, `Turbo.navigator.currentVisit.scrolled`
// is internal and private attribute: we should NOT access it.
// Unfortunately, there is no good alternative yet to maintain the
// scroll position. This means we have to be pay double attention when
// upgrading Turbo.
// Reference: https://github.com/hotwired/turbo/issues/37#issuecomment-979466543
Turbo.navigator.currentVisit.scrolled = true;
disableScroll = false;
}
});
10 changes: 5 additions & 5 deletions src/views/links/_link.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
</div>

<?php if ($display_mark_as_read): ?>
<form method="post" action="<?= url('mark link as read', ['id' => $link->id, 'from' => $this->safe('from')]) ?>" role="menuitem">
<form data-turbo-preserve-scroll method="post" action="<?= url('mark link as read', ['id' => $link->id, 'from' => $this->safe('from')]) ?>" role="menuitem">
<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />

<button class="popup__item popup__item--button">
Expand All @@ -205,7 +205,7 @@
<?php endif; ?>

<?php if ($display_mark_as_unread): ?>
<form method="post" action="<?= url('mark link as unread', ['id' => $link->id, 'from' => $this->safe('from')]) ?>" role="menuitem">
<form data-turbo-preserve-scroll method="post" action="<?= url('mark link as unread', ['id' => $link->id, 'from' => $this->safe('from')]) ?>" role="menuitem">
<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />

<button class="popup__item popup__item--button">
Expand Down Expand Up @@ -270,7 +270,7 @@
<?= _('Remove from the news…') ?>
</div>

<form method="post" action="<?= url('mark link as read', ['id' => $link->id, 'from' => url('news')]) ?>" role="menuitem">
<form data-turbo-preserve-scroll method="post" action="<?= url('mark link as read', ['id' => $link->id, 'from' => url('news')]) ?>" role="menuitem">
<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />

<button class="popup__item popup__item--button">
Expand All @@ -279,7 +279,7 @@
</button>
</form>

<form method="post" action="<?= url('read link later', ['id' => $link->id, 'from' => url('news')]) ?>" role="menuitem">
<form data-turbo-preserve-scroll method="post" action="<?= url('read link later', ['id' => $link->id, 'from' => url('news')]) ?>" role="menuitem">
<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />

<button class="popup__item popup__item--button">
Expand All @@ -290,7 +290,7 @@

<div class="popup__separator"></div>

<form method="post" action="<?= url('mark link to never read', ['id' => $link->id, 'from' => url('news')]) ?>" role="menuitem">
<form data-turbo-preserve-scroll method="post" action="<?= url('mark link to never read', ['id' => $link->id, 'from' => url('news')]) ?>" role="menuitem">
<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />

<button class="popup__item popup__item--button">
Expand Down
2 changes: 1 addition & 1 deletion src/views/links/collections/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<?= protect($link->title) ?>
</p>

<form method="post" action="<?= url('update link collections', ['id' => $link->id]) ?>">
<form data-turbo-preserve-scroll method="post" action="<?= url('update link collections', ['id' => $link->id]) ?>">
<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />
<input type="hidden" name="from" value="<?= $from ?>" />

Expand Down
2 changes: 1 addition & 1 deletion src/views/links/collections/index_adding.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<?= protect($link->title) ?>
</p>

<form method="post" action="<?= url('update link collections', ['id' => $link->id]) ?>">
<form data-turbo-preserve-scroll method="post" action="<?= url('update link collections', ['id' => $link->id]) ?>">
<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />
<input type="hidden" name="from" value="<?= $from ?>" />
<input type="hidden" name="mode" value="adding" />
Expand Down
2 changes: 1 addition & 1 deletion src/views/links/collections/index_news.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<?= protect($link->title) ?>
</p>

<form method="post" action="<?= url('update link collections', ['id' => $link->id]) ?>">
<form data-turbo-preserve-scroll method="post" action="<?= url('update link collections', ['id' => $link->id]) ?>">
<?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>

<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />
Expand Down
3 changes: 2 additions & 1 deletion src/views/links/edit.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<?= protect($link->title) ?>
</p>

<form method="post" action="<?= url('update link', ['id' => $link->id]) ?>">
<form data-turbo-preserve-scroll method="post" action="<?= url('update link', ['id' => $link->id]) ?>">
<?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>

<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />
Expand Down Expand Up @@ -84,6 +84,7 @@
</form>

<form
data-turbo-preserve-scroll
class="form--danger"
method="post"
action="<?= url('delete link', ['id' => $link->id]) ?>"
Expand Down
2 changes: 1 addition & 1 deletion src/views/links/repairing/_new.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<?= _('You can change the <abbr>URL</abbr> of a link if it’s broken, or if you made a mistake in it.') ?>
</p>

<form method="post" action="<?= url('repair link', ['id' => $link->id]) ?>">
<form data-turbo-preserve-scroll method="post" action="<?= url('repair link', ['id' => $link->id]) ?>">
<?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>

<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />
Expand Down
5 changes: 2 additions & 3 deletions src/views/links/searches/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
required
value="<?= $url ?>"
autocomplete="off"
autofocus
aria-describedby="url-desc"
/>

Expand Down Expand Up @@ -201,7 +200,7 @@

<div class="card__footer card__footer--centered">
<?php if ($current_user->isFollowing($feed->id)): ?>
<form method="post" action="<?= url('unfollow collection', ['id' => $feed->id]) ?>">
<form data-turbo-preserve-scroll method="post" action="<?= url('unfollow collection', ['id' => $feed->id]) ?>">
<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />
<input type="hidden" name="from" value="<?= $from ?>" />
<button>
Expand All @@ -210,7 +209,7 @@
</button>
</form>
<?php else: ?>
<form method="post" action="<?= url('follow collection', ['id' => $feed->id]) ?>">
<form data-turbo-preserve-scroll method="post" action="<?= url('follow collection', ['id' => $feed->id]) ?>">
<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />
<input type="hidden" name="from" value="<?= $from ?>" />
<button>
Expand Down
3 changes: 2 additions & 1 deletion src/views/links/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
</button>

<form
data-turbo-preserve-scroll
method="post"
action="<?= url('delete message', ['id' => $message->id]) ?>"
data-controller="confirmation"
Expand Down Expand Up @@ -155,7 +156,7 @@
<?php endforeach; ?>

<?php if ($can_comment): ?>
<form method="post" action="<?= url('links/create message', ['link_id' => $link->id]) ?>">
<form data-turbo-preserve-scroll method="post" action="<?= url('links/create message', ['link_id' => $link->id]) ?>">
<?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>

<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />
Expand Down
2 changes: 1 addition & 1 deletion src/views/messages/edit.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<h1 id="modal-title"><?= _('Message edition') ?></h1>
</div>

<form method="post" action="<?= url('update message', ['id' => $message->id]) ?>">
<form data-turbo-preserve-scroll method="post" action="<?= url('update message', ['id' => $message->id]) ?>">
<?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>

<input type="hidden" name="csrf" value="<?= $csrf_token ?>" />
Expand Down

0 comments on commit 7124f45

Please sign in to comment.