| 1 |
import { subscribe } from '@wordpress/data'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
|
| 4 |
export const debounce = <T extends (...args: unknown[]) => void>( |
| 5 |
fn: T, |
| 6 |
delay: number, |
| 7 |
) => { |
| 8 |
let timeout: ReturnType<typeof setTimeout> | null = null; |
| 9 |
return (...args: Parameters<T>) => { |
| 10 |
if (timeout) clearTimeout(timeout); |
| 11 |
timeout = setTimeout(() => fn(...args), delay); |
| 12 |
}; |
| 13 |
}; |
| 14 |
|
| 15 |
export const handleValidationErrors = () => { |
| 16 |
const debouncedUnsub = debounce(() => unsub(), 5000); |
| 17 |
|
| 18 |
const unsub = subscribe(() => { |
| 19 |
const editor = document.querySelector( |
| 20 |
'#editor, iframe[name="editor-canvas"]', |
| 21 |
); |
| 22 |
const blockErrors = editor?.querySelectorAll( |
| 23 |
'.wp-block-kevinbatdorf-code-block-pro .block-editor-warning', |
| 24 |
); |
| 25 |
if (!blockErrors?.length) return; |
| 26 |
blockErrors.forEach((blockError) => { |
| 27 |
const message = blockError.querySelector( |
| 28 |
'.block-editor-warning__message', |
| 29 |
); |
| 30 |
if (message) { |
| 31 |
message.textContent = __( |
| 32 |
'This block has been updated. Press update to refresh.', |
| 33 |
'code-block-pro', |
| 34 |
); |
| 35 |
} |
| 36 |
const button = blockError.querySelector( |
| 37 |
'.block-editor-warning__action button', |
| 38 |
); |
| 39 |
if (button) { |
| 40 |
button.textContent = __('Update', 'code-block-pro'); |
| 41 |
} |
| 42 |
}); |
| 43 |
debouncedUnsub(); |
| 44 |
}); |
| 45 |
}; |
| 46 |
|