feat(ui): add document link to drawer (#12036)

### What?
Adds an option to open the current document in a new tab when opened in
a drawer.

### Why?
There is currently no direct way to open a document when opened in a
drawer. However, sometimes editors want to edit one or multiple
documents from relationships independently of the current edit view and
need an easy option to open these separately.

### How?
Converts the document id to a link if in drawer context.


![image](https://github.com/user-attachments/assets/e448328f-f685-49b8-95c5-bd5d6aa60e35)

---------

Co-authored-by: Jacob Fletcher <jacobsfletch@gmail.com>
This commit is contained in:
Tobias Odendahl
2025-05-05 16:09:26 +02:00
committed by GitHub
parent 2628b43639
commit 292b462f34
3 changed files with 88 additions and 8 deletions

View File

@@ -2,7 +2,12 @@
import React from 'react' import React from 'react'
import './index.scss' import './index.scss'
import { Link } from '../../elements/Link/index.js'
import { useConfig } from '../../providers/Config/index.js'
import { useDocumentInfo } from '../../providers/DocumentInfo/index.js'
import { formatAdminURL } from '../../utilities/formatAdminURL.js'
import { sanitizeID } from '../../utilities/sanitizeID.js' import { sanitizeID } from '../../utilities/sanitizeID.js'
import { useDrawerDepth } from '../Drawer/index.js'
const baseClass = 'id-label' const baseClass = 'id-label'
@@ -10,10 +15,26 @@ export const IDLabel: React.FC<{ className?: string; id: string; prefix?: string
id, id,
className, className,
prefix = 'ID:', prefix = 'ID:',
}) => ( }) => {
const {
config: {
routes: { admin: adminRoute },
},
} = useConfig()
const { collectionSlug, globalSlug } = useDocumentInfo()
const drawerDepth = useDrawerDepth()
const docPath = formatAdminURL({
adminRoute,
path: `/${collectionSlug ? `collections/${collectionSlug}` : `globals/${globalSlug}`}/${id}`,
})
return (
<div className={[baseClass, className].filter(Boolean).join(' ')} title={id}> <div className={[baseClass, className].filter(Boolean).join(' ')} title={id}>
{prefix} {prefix}
&nbsp; &nbsp;
{sanitizeID(id)} {drawerDepth > 1 ? <Link href={docPath}>{sanitizeID(id)}</Link> : sanitizeID(id)}
</div> </div>
) )
}

View File

@@ -360,6 +360,65 @@ describe('Document View', () => {
await expect.poll(() => drawer2Left > drawerLeft).toBe(true) await expect.poll(() => drawer2Left > drawerLeft).toBe(true)
}) })
test('document drawer displays a link to document', async () => {
await navigateToDoc(page, postsUrl)
// change the relationship to a document which is a different one than the current one
await page.locator('#field-relationship').click()
await page.locator('#field-relationship .rs__option').nth(2).click()
await saveDocAndAssert(page)
// open relationship drawer
await page
.locator('.field-type.relationship .relationship--single-value__drawer-toggler')
.click()
const drawer1Content = page.locator('[id^=doc-drawer_posts_1_] .drawer__content')
await expect(drawer1Content).toBeVisible()
// modify the title to trigger the leave page modal
await page.locator('.drawer__content #field-title').fill('New Title')
// Open link in a new tab by holding down the Meta or Control key
const documentLink = page.locator('.id-label a')
const documentId = String(await documentLink.textContent())
await documentLink.click()
const leavePageModal = page.locator('#leave-without-saving #confirm-action').last()
await expect(leavePageModal).toBeVisible()
await leavePageModal.click()
await page.waitForURL(postsUrl.edit(documentId))
})
test('document can be opened in a new tab from within the drawer', async () => {
await navigateToDoc(page, postsUrl)
await page
.locator('.field-type.relationship .relationship--single-value__drawer-toggler')
.click()
await wait(500)
const drawer1Content = page.locator('[id^=doc-drawer_posts_1_] .drawer__content')
await expect(drawer1Content).toBeVisible()
const currentUrl = page.url()
// Open link in a new tab by holding down the Meta or Control key
const documentLink = page.locator('.id-label a')
const documentId = String(await documentLink.textContent())
const [newPage] = await Promise.all([
page.context().waitForEvent('page'),
documentLink.click({ modifiers: ['ControlOrMeta'] }),
])
// Wait for navigation to complete in the new tab and ensure correct URL
await expect(newPage.locator('.doc-header')).toBeVisible()
// using contain here, because after load the lists view will add query params like "?limit=10"
expect(newPage.url()).toContain(postsUrl.edit(documentId))
// Ensure the original page did not change
expect(page.url()).toBe(currentUrl)
})
}) })
describe('descriptions', () => { describe('descriptions', () => {

View File

@@ -31,7 +31,7 @@
} }
], ],
"paths": { "paths": {
"@payload-config": ["./test/fields/config.ts"], "@payload-config": ["./test/_community/config.ts"],
"@payloadcms/admin-bar": ["./packages/admin-bar/src"], "@payloadcms/admin-bar": ["./packages/admin-bar/src"],
"@payloadcms/live-preview": ["./packages/live-preview/src"], "@payloadcms/live-preview": ["./packages/live-preview/src"],
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"], "@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],