| 1 |
// Wrapper / dynamic-content blocks that aren't editable on the front end. |
| 2 |
const KNOWN_UNSUPPORTED = new Set([ |
| 3 |
'wp-block-post-title', |
| 4 |
'wp-block-post-author', |
| 5 |
'wp-block-post-date', |
| 6 |
'wp-block-post-terms', |
| 7 |
// core/navigation wraps individual links; clicks route to the child link instead. |
| 8 |
'wp-block-navigation', |
| 9 |
]); |
| 10 |
|
| 11 |
// BEM children and helper classes, not block types. |
| 12 |
const WP_BLOCK_CLASS_BLOCKLIST = /__|^wp-block-(post|theme|root|preset)-/; |
| 13 |
|
| 14 |
// Unknown wp-block-* slugs would fabricate a wrong block type, so only known core slugs derive one. |
| 15 |
const CORE_BLOCK_SLUGS = new Set([ |
| 16 |
'paragraph', |
| 17 |
'heading', |
| 18 |
'list', |
| 19 |
'list-item', |
| 20 |
'quote', |
| 21 |
'pullquote', |
| 22 |
'code', |
| 23 |
'preformatted', |
| 24 |
'verse', |
| 25 |
'details', |
| 26 |
'footnotes', |
| 27 |
'table', |
| 28 |
'table-of-contents', |
| 29 |
'image', |
| 30 |
'gallery', |
| 31 |
'audio', |
| 32 |
'video', |
| 33 |
'file', |
| 34 |
'cover', |
| 35 |
'media-text', |
| 36 |
'embed', |
| 37 |
'buttons', |
| 38 |
'button', |
| 39 |
'columns', |
| 40 |
'column', |
| 41 |
'group', |
| 42 |
'separator', |
| 43 |
'spacer', |
| 44 |
'more', |
| 45 |
'nextpage', |
| 46 |
'social-links', |
| 47 |
'social-link', |
| 48 |
'search', |
| 49 |
'html', |
| 50 |
'shortcode', |
| 51 |
'page-list', |
| 52 |
'page-list-item', |
| 53 |
'navigation-link', |
| 54 |
'navigation-submenu', |
| 55 |
'home-link', |
| 56 |
'loginout', |
| 57 |
'site-logo', |
| 58 |
'site-title', |
| 59 |
'site-tagline', |
| 60 |
'archives', |
| 61 |
'calendar', |
| 62 |
'categories', |
| 63 |
'latest-posts', |
| 64 |
'latest-comments', |
| 65 |
'rss', |
| 66 |
'tag-cloud', |
| 67 |
'avatar', |
| 68 |
'read-more', |
| 69 |
'term-description', |
| 70 |
]); |
| 71 |
|
| 72 |
export const detectBlockType = (el) => { |
| 73 |
let firstWpBlockClass = null; |
| 74 |
for (const cls of el.classList) { |
| 75 |
if (KNOWN_UNSUPPORTED.has(cls)) return null; |
| 76 |
if (!firstWpBlockClass && cls.startsWith('wp-block-')) { |
| 77 |
if (!WP_BLOCK_CLASS_BLOCKLIST.test(cls)) firstWpBlockClass = cls; |
| 78 |
} |
| 79 |
} |
| 80 |
// Nav links/submenus don't always carry the -link/-submenu class, so match -item too. |
| 81 |
if ( |
| 82 |
el.classList.contains('wp-block-navigation-item') || |
| 83 |
el.classList.contains('wp-block-navigation-link') || |
| 84 |
el.classList.contains('wp-block-navigation-submenu') |
| 85 |
) { |
| 86 |
return 'core/navigation-link'; |
| 87 |
} |
| 88 |
if (firstWpBlockClass) { |
| 89 |
const slug = firstWpBlockClass.slice('wp-block-'.length); |
| 90 |
return CORE_BLOCK_SLUGS.has(slug) ? `core/${slug}` : null; |
| 91 |
} |
| 92 |
// Tag fallback for class-less <p>/<h1-6>/<li>; nav <li>s carry a class and resolve above. |
| 93 |
const tag = el.tagName.toLowerCase(); |
| 94 |
if (tag === 'p') return 'core/paragraph'; |
| 95 |
if (/^h[1-6]$/.test(tag)) return 'core/heading'; |
| 96 |
if (tag === 'li') return 'core/list-item'; |
| 97 |
return null; |
| 98 |
}; |
| 99 |
|