| 1 |
// Single source for the agent-block selection-descriptor shape so |
| 2 |
// DOMHighlighter's click commit and ask-ai's pill click can't drift on |
| 3 |
// which metadata flags exist or how they're detected. |
| 4 |
|
| 5 |
import { detectBlockType } from '@quick-edit/lib/dom'; |
| 6 |
|
| 7 |
const AGENT_ATTR = 'data-extendify-agent-block-id'; |
| 8 |
const PART_ID_ATTR = 'data-extendify-part-block-id'; |
| 9 |
const PART_ATTR = 'data-extendify-part'; |
| 10 |
const PART_SLUG_ATTR = 'data-extendify-part-slug'; |
| 11 |
|
| 12 |
export const buildAgentBlockDescriptor = (match) => { |
| 13 |
if (!match) return null; |
| 14 |
const templatePart = match.closest?.(`[${PART_ATTR}]`); |
| 15 |
const details = { |
| 16 |
id: match.getAttribute(AGENT_ATTR), |
| 17 |
target: AGENT_ATTR, |
| 18 |
// local-pick + classify-block-edit gate on this; without it every edit |
| 19 |
// falls through to find-agent. |
| 20 |
blockType: detectBlockType(match), |
| 21 |
hasNav: |
| 22 |
!!match.querySelector('.wp-block-navigation') || |
| 23 |
match.classList.contains('wp-block-navigation'), |
| 24 |
hasSiteTitle: |
| 25 |
match.classList.contains('wp-block-site-title') || |
| 26 |
!!match.querySelector('.wp-block-site-title'), |
| 27 |
hasSiteLogo: |
| 28 |
match.classList.contains('wp-block-site-logo') || |
| 29 |
!!match.querySelector('.wp-block-site-logo'), |
| 30 |
hasLinks: !!match.querySelector('a') || match.tagName === 'A', |
| 31 |
hasImages: |
| 32 |
!!match.querySelector('.wp-block-image') || |
| 33 |
match.classList.contains('wp-block-image') || |
| 34 |
!!match.querySelector('img'), |
| 35 |
hasText: /\S/.test((match.textContent || '').replace(//g, '')), |
| 36 |
}; |
| 37 |
if (templatePart) { |
| 38 |
details.id = templatePart.getAttribute(PART_ID_ATTR); |
| 39 |
details.target = PART_ID_ATTR; |
| 40 |
details.template = templatePart.getAttribute(PART_ATTR); |
| 41 |
// Part ids number their own space; without the scope the agent's read |
| 42 |
// and save land on the page post's block of the same number. |
| 43 |
details.source = { |
| 44 |
kind: 'template-part', |
| 45 |
partSlug: templatePart.getAttribute(PART_SLUG_ATTR) || '', |
| 46 |
}; |
| 47 |
} |
| 48 |
return details; |
| 49 |
}; |
| 50 |
|