| 1 |
import { BLOCK_ID_SEL } from '@agent/lib/block-el'; |
| 2 |
import { UpdatePostConfirm } from '@agent/workflows/content/components/UpdatePostConfirm'; |
| 3 |
|
| 4 |
const { context, abilities } = window.extAgentData; |
| 5 |
|
| 6 |
// A wrapper carries its children's text, so counting it repeats every string. |
| 7 |
const ownText = (el) => { |
| 8 |
let text = ''; |
| 9 |
for (const node of el.childNodes) { |
| 10 |
if (node.nodeType === Node.TEXT_NODE) text += node.nodeValue; |
| 11 |
} |
| 12 |
return text.replace(/\s+/g, ' ').trim(); |
| 13 |
}; |
| 14 |
|
| 15 |
// One string is select-block's job; this earns its place on several. |
| 16 |
const hasSeveralStrings = () => { |
| 17 |
const strings = new Set(); |
| 18 |
for (const el of document.querySelectorAll(BLOCK_ID_SEL)) { |
| 19 |
const text = ownText(el); |
| 20 |
if (text) strings.add(text); |
| 21 |
if (strings.size > 1) return true; |
| 22 |
} |
| 23 |
return false; |
| 24 |
}; |
| 25 |
|
| 26 |
export default { |
| 27 |
available: () => |
| 28 |
abilities?.canEditPosts && |
| 29 |
!context?.adminPage && |
| 30 |
context?.postId && |
| 31 |
!context?.isBlogPage && |
| 32 |
context.usingBlockEditor && |
| 33 |
hasSeveralStrings(), |
| 34 |
id: 'edit-post-strings', |
| 35 |
whenFinished: { component: UpdatePostConfirm }, |
| 36 |
}; |
| 37 |
|