| 1 |
import { |
| 2 |
BaseControl, |
| 3 |
CheckboxControl, |
| 4 |
TextControl, |
| 5 |
} from '@wordpress/components'; |
| 6 |
import { useEffect, useRef } from '@wordpress/element'; |
| 7 |
import { sprintf, __ } from '@wordpress/i18n'; |
| 8 |
import { AttributesPropsAndSetter } from '../../types'; |
| 9 |
import { parseStringArrayWithSingleNestedArray } from '../../util/arrayHelpers'; |
| 10 |
|
| 11 |
export const HighlightingControl = ({ |
| 12 |
attributes, |
| 13 |
setAttributes, |
| 14 |
}: AttributesPropsAndSetter) => { |
| 15 |
const inputRef = useRef<HTMLInputElement>(null); |
| 16 |
useEffect(() => { |
| 17 |
try { |
| 18 |
parseStringArrayWithSingleNestedArray(attributes.lineHighlights); |
| 19 |
inputRef.current |
| 20 |
?.querySelector('input') |
| 21 |
?.classList.remove('cbp-input-error'); |
| 22 |
} catch (e) { |
| 23 |
console.error(e); |
| 24 |
inputRef.current |
| 25 |
?.querySelector('input') |
| 26 |
?.classList.add('cbp-input-error'); |
| 27 |
} |
| 28 |
}, [attributes.lineHighlights]); |
| 29 |
return ( |
| 30 |
<BaseControl id="code-block-pro-show-highlighting"> |
| 31 |
<CheckboxControl |
| 32 |
data-cy="enable-highlighting" |
| 33 |
label={__('Enable line highlighting', 'code-block-pro')} |
| 34 |
help={__( |
| 35 |
'Highlight individual lines to bring attention to specific code.', |
| 36 |
'code-block-pro', |
| 37 |
)} |
| 38 |
checked={attributes.enableHighlighting ?? false} |
| 39 |
onChange={(enableHighlighting) => { |
| 40 |
setAttributes({ enableHighlighting }); |
| 41 |
}} |
| 42 |
/> |
| 43 |
{attributes.enableHighlighting && ( |
| 44 |
<BaseControl id="code-block-pro-show-highlighted-lines"> |
| 45 |
<div ref={inputRef}> |
| 46 |
<TextControl |
| 47 |
id="code-block-pro-show-highlighted-lines" |
| 48 |
spellCheck={false} |
| 49 |
autoComplete="off" |
| 50 |
label={__( |
| 51 |
'Highlight the following', |
| 52 |
'code-block-pro', |
| 53 |
)} |
| 54 |
help={sprintf( |
| 55 |
__( |
| 56 |
'Try %1$s or %2$s for a range.', |
| 57 |
'code-block-pro', |
| 58 |
), |
| 59 |
'1,5', |
| 60 |
'1,[3,5]', |
| 61 |
)} |
| 62 |
onChange={(lineHighlights) => { |
| 63 |
setAttributes({ lineHighlights }); |
| 64 |
}} |
| 65 |
value={attributes?.lineHighlights ?? ''} |
| 66 |
/> |
| 67 |
</div> |
| 68 |
</BaseControl> |
| 69 |
)} |
| 70 |
</BaseControl> |
| 71 |
); |
| 72 |
}; |
| 73 |
|