feat(ui): don't trigger preventLeave when opening a new tab (#11683)

### What?
Prevents the preventLeave dialog from showing if a clicked link is about
to open in a new tab.

### Why?
Currently, no external link can be clicked on the edit page if it was
modified, even if the link would not navigate the user away from that
page but open in a new tab instead.

### How?
We don't trigger the preventLeave dialog if
- the target of a clicked anchor is `_blank`
- the user pressed the command or ctrl key while clicking on a link
(which opens link in a new tab)
This commit is contained in:
Tobias Odendahl
2025-03-13 20:40:10 +01:00
committed by GitHub
parent 878dc54579
commit 398607fdc7
2 changed files with 33 additions and 2 deletions

View File

@@ -1008,7 +1008,7 @@ describe('General', () => {
const newTitle = 'new title'
await page.locator('#field-title').fill(newTitle)
await page.locator('header.app-header a[href="/admin/collections/posts"]').click()
await page.locator(`header.app-header a[href="/admin/collections/posts"]`).click()
// Locate the modal container
const modalContainer = page.locator('.payload__modal-container')
@@ -1024,6 +1024,36 @@ describe('General', () => {
})
})
test('should not open leave-without-saving modal if opening a new tab', async () => {
const title = 'title'
await page.goto(postsUrl.create)
await page.locator('#field-title').fill(title)
await expect(page.locator('#field-title')).toHaveValue(title)
const newTitle = 'new title'
await page.locator('#field-title').fill(newTitle)
// Open link in a new tab by holding down the Meta or Control key
const [newPage] = await Promise.all([
page.context().waitForEvent('page'),
page
.locator(`header.app-header a[href="/admin/collections/posts"]`)
.click({ modifiers: ['ControlOrMeta'] }),
])
// Wait for navigation to complete in the new tab and ensure correct URL
await expect(newPage.locator('.list-header')).toBeVisible()
// using contain here, because after load the lists view will add query params like "?limit=10"
expect(newPage.url()).toContain(postsUrl.list)
// Locate the modal container and ensure it is not visible
const modalContainer = page.locator('.payload__modal-container')
await expect(modalContainer).toBeHidden()
// Ensure the original page is the correct URL
expect(page.url()).toBe(postsUrl.create)
})
describe('preferences', () => {
test('should successfully reset prefs after clicking reset button', async () => {
await page.goto(`${serverURL}/admin/account`)