| 1 |
import { store as blockEditorStore } from '@wordpress/block-editor'; |
| 2 |
import { useSelect } from '@wordpress/data'; |
| 3 |
import { useCallback } from '@wordpress/element'; |
| 4 |
|
| 5 |
export const useSelectedText = () => { |
| 6 |
const { getSelectedBlockClientIds, getBlocksByClientId } = useSelect( |
| 7 |
(select) => select(blockEditorStore), |
| 8 |
[], |
| 9 |
); |
| 10 |
|
| 11 |
const selectedBlockId = getSelectedBlockClientIds(); |
| 12 |
|
| 13 |
const getSelectedContent = useCallback(() => { |
| 14 |
const selectedBlocks = getBlocksByClientId(selectedBlockId); |
| 15 |
return selectedBlocks |
| 16 |
.map(({ attributes }) => attributes.content) |
| 17 |
.join('\n\n'); |
| 18 |
}, [getBlocksByClientId, selectedBlockId]); |
| 19 |
|
| 20 |
return { |
| 21 |
selectedText: getSelectedContent(), |
| 22 |
}; |
| 23 |
}; |
| 24 |
|