Toggle DatoCMS block labels on preview mode#309
Conversation
… item type IDs and generate model API keys
…ality. Updated documentation
…o go to, no fallback. Update docs
…ieldPath assignment
Deploying head-start with
|
| Latest commit: |
d636d75
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://a76bfebc.head-start.pages.dev |
| Branch Preview URL: | https://feat-show-blocks.head-start.pages.dev |
jbmoelker
left a comment
There was a problem hiding this comment.
Really nice PR @anareyna! Thanks for the elaborate PR body (don't forget to check the boxes at the bottom ;)). Really great feature to be able to showcase during sales as well :). I made some suggestions. I would approve already except for one thing which is about possible nested anchors. I suggested a workaround there. An alternative could be not wrapping but using adjacent elements after the blocks that lay over their blocks.
|
|
||
| When clicking on a block label in preview mode, the system automatically generates a field path to focus the correct field in DatoCMS. Block labels are only clickable when `PreviewModeSubscription` receives a `record` prop (see above). This is done by detecting the "focus field" for each block type. | ||
|
|
||
| ### How it works |
There was a problem hiding this comment.
I love the effort you put into the docs ❤️. Maybe nice to reference why this works on the DatoCMS site by referencing their content link feature?
scripts/download-item-types.ts
Outdated
| }); | ||
| dotenv.config({ allowEmptyValues: Boolean(process.env.CI) }); | ||
|
|
||
| const FILE_PATH = './src/lib/datocms/itemTypes.json'; |
There was a problem hiding this comment.
We don't use all uppercase const values elsewhere in the code base (other than for env variables). So I wouldn't start here. Just filePath etc.
scripts/download-item-types.ts
Outdated
| .split(/[_-]/) | ||
| function convertApiKeyToTypename(apiKey: string): string { | ||
| const words = apiKey.split(/[_-]/); | ||
| const pascalCase = words |
There was a problem hiding this comment.
https://www.npmjs.com/package/change-case ? or does DatoCMS maybe expose a method for this in general? I imagine they do the same thing in their packages.
There was a problem hiding this comment.
if not provided I'd go with https://github.com/unjs/scule/
unjs packages are well maintained in general
scripts/download-item-types.ts
Outdated
| } | ||
|
|
||
| // Run async work with a small concurrency cap to avoid spiking the api | ||
| async function processItemsConcurrently<T>( |
There was a problem hiding this comment.
I'm okay with these methods, but always find them a bit hard to read. So maybe easier to use helper packages.
Use async parallelLimit? https://www.npmjs.com/package/async
src/blocks/Blocks.astro
Outdated
| import VideoBlock from './VideoBlock/VideoBlock.astro'; | ||
| import VideoEmbedBlock from './VideoEmbedBlock/VideoEmbedBlock.astro'; | ||
| import GroupingBlock from './GroupingBlock/GroupingBlock.astro'; | ||
| import { getDebugPaths } from './block-debug-utils'; |
There was a problem hiding this comment.
I'm not sure if Debug is the best word. Is it Preview or Edit(or)?
There was a problem hiding this comment.
makes sense, renamed debug prefixes and attributes to editor
| <div class="preview-mode-actions"> | ||
| {/* eslint-disable-next-line astro/jsx-a11y/anchor-is-valid */} | ||
| <a data-edit-record target="_blank" rel="noreferrer noopener">edit in CMS</a> | ||
| <a data-debug-edit-record target="_blank" rel="noreferrer noopener">edit in CMS</a> |
| } | ||
|
|
||
| a[data-edit-record]:not([href]) { | ||
| a[data-debug-edit-record]:not([href]) { |
There was a problem hiding this comment.
I'm wondering if we should hide the other labels when one has focus. Now I tried out some pages that have a nested or overlapping blocks and my highlighted block contents are still not entirely visible as other labels are over it.
| } | ||
| :global([data-debug-blocks-visible="true"]) { | ||
| --block-label-z-index: 9999; | ||
| --block-label-color: #ff593d; |
There was a problem hiding this comment.
We've managed to only use primary colours up until now. So maybe use blue, similar to the Vercel content link?
| } | ||
| } | ||
|
|
||
| #buildRecordEditUrl(itemTypeId: string, recordId: string) { |
There was a problem hiding this comment.
Does this belong here or in lib/datocms or block-debug-utils?
There was a problem hiding this comment.
I'd keep it here since it uses these other instances (#datocmsProject, #datocmsEnvironment) and is only called in this class. We can extract it if it's ever needed elsewhere.
| return `https://${this.#datocmsProject}.admin.datocms.com/environments/${this.#datocmsEnvironment}/editor/item_types/${itemTypeId}/items/${recordId}`; | ||
| } | ||
|
|
||
| #applyBlockEditLinks(itemTypeId: string) { |
There was a problem hiding this comment.
If we use clickable divs instead of anchors, this only needs to be executed on click.
There was a problem hiding this comment.
I would also consider adding examples of these paths to a comment on this functions to make it easier to understand what goes in and out.
thanks!, not sure if I'm missing something about nesting anchors but currently the |
jbmoelker
left a comment
There was a problem hiding this comment.
sorry, no nested anchors. so approved. the rest are just suggestions for minor improvements.
…nstead of debug...

Changes
Adds visual block labels in preview mode that allow users to quickly identify and edit blocks in DatoCMS.

What changed
Technical details
block-debug-utils.tsfor field path building and API key conversiondownload-item-types.tsto detect focus fields (rich text, media, JSON, links, etc.)Blocks.astroto render debug labels with correct field pathsPreviewMode.client.tsto handle label positioning, hover effects, and edit link generationhideDebugLabelsprop to skip labels in specific contexts (PagePartialBlock, TextBlock inline blocks)Usage
Block labels appear automatically in preview mode when:
PreviewModeSubscriptionreceives arecordpropLabels are hidden for:
Documentation
Updated
docs/preview-mode.mdhttps://github.com/voorhoede/head-start/blob/feat/show-blocks/docs/preview-mode.md#block-field-path-detectionHow to test
Test in other head-start based projects:

Checklist