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