| 1 |
import { detectBlockType } from './block-type'; |
| 2 |
|
| 3 |
const BLOCK_ID_ATTR = 'data-extendify-agent-block-id'; |
| 4 |
|
| 5 |
// Wrappers that are broken markup without their last item — the op takes the |
| 6 |
// wrapper. Not group/columns: an empty one still renders a box worth keeping. |
| 7 |
const LIST_WRAPPERS = new Set([ |
| 8 |
'core/buttons', |
| 9 |
'core/social-links', |
| 10 |
'core/list', |
| 11 |
'core/quote', |
| 12 |
]); |
| 13 |
|
| 14 |
// The wrapper never reaches the model (dropped from the manifest), so "delete this |
| 15 |
// button" can only name the button — walk up to the wrapper when it's the last item. |
| 16 |
export const resolveDeleteTarget = (blockId) => { |
| 17 |
const el = document.querySelector(`[${BLOCK_ID_ATTR}="${blockId}"]`); |
| 18 |
if (!el) return blockId; |
| 19 |
let targetId = blockId; |
| 20 |
let parent = el.parentElement?.closest(`[${BLOCK_ID_ATTR}]`); |
| 21 |
while ( |
| 22 |
parent && |
| 23 |
LIST_WRAPPERS.has(detectBlockType(parent)) && |
| 24 |
parent.querySelectorAll(`[${BLOCK_ID_ATTR}]`).length === 1 |
| 25 |
) { |
| 26 |
targetId = parent.getAttribute(BLOCK_ID_ATTR); |
| 27 |
parent = parent.parentElement?.closest(`[${BLOCK_ID_ATTR}]`); |
| 28 |
} |
| 29 |
return targetId; |
| 30 |
}; |
| 31 |
|