dom.js
32 lines
| 1 | /** |
| 2 | * Programmatically set a React-controlled textarea's value. |
| 3 | * Uses the native setter so React's synthetic onChange fires. |
| 4 | * |
| 5 | * @param {HTMLTextAreaElement} textarea - The textarea element. |
| 6 | * @param {string} value - The new value. |
| 7 | */ |
| 8 | export function setTextareaValue( textarea, value ) { |
| 9 | const setter = Object.getOwnPropertyDescriptor( |
| 10 | window.HTMLTextAreaElement.prototype, |
| 11 | 'value' |
| 12 | ).set; |
| 13 | setter.call( textarea, value ); |
| 14 | textarea.dispatchEvent( new Event( 'input', { bubbles: true } ) ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Accept a block suggestion: write text to the modal textarea and clear the store. |
| 19 | * |
| 20 | * @param {HTMLElement} blockModal - The block guideline modal element. |
| 21 | * @param {string} blockName - Block name key in the store. |
| 22 | * @param {string} suggestion - Suggestion text to write. |
| 23 | * @param {Function} clearSuggestion - Store action to clear the suggestion. |
| 24 | */ |
| 25 | export function acceptBlockSuggestion( blockModal, blockName, suggestion, clearSuggestion ) { |
| 26 | const textarea = blockModal?.querySelector( '.components-textarea-control__input' ); |
| 27 | if ( textarea ) { |
| 28 | setTextareaValue( textarea, suggestion ); |
| 29 | } |
| 30 | clearSuggestion( blockName ); |
| 31 | } |
| 32 |