| 1 |
import domReady from '@wordpress/dom-ready' |
| 2 |
import React, { useCallback, useEffect } from 'react' |
| 3 |
import { __ } from '@wordpress/i18n' |
| 4 |
import { SnippetInputProps } from '../../types/SnippetInputProps' |
| 5 |
|
| 6 |
export const EDITOR_ID = 'snippet_description' |
| 7 |
|
| 8 |
const DEFAULT_ROWS = 5 |
| 9 |
|
| 10 |
const TOOLBAR_BUTTONS = [ |
| 11 |
[ |
| 12 |
'bold', |
| 13 |
'italic', |
| 14 |
'underline', |
| 15 |
'strikethrough', |
| 16 |
'blockquote', |
| 17 |
'bullist', |
| 18 |
'numlist', |
| 19 |
'alignleft', |
| 20 |
'aligncenter', |
| 21 |
'alignright', |
| 22 |
'link', |
| 23 |
'wp_adv', |
| 24 |
'code_snippets' |
| 25 |
], |
| 26 |
[ |
| 27 |
'formatselect', |
| 28 |
'forecolor', |
| 29 |
'pastetext', |
| 30 |
'removeformat', |
| 31 |
'charmap', |
| 32 |
'outdent', |
| 33 |
'indent', |
| 34 |
'undo', |
| 35 |
'redo', |
| 36 |
'spellchecker' |
| 37 |
] |
| 38 |
] |
| 39 |
|
| 40 |
const initializeEditor = (onChange: (content: string) => void) => { |
| 41 |
window.wp.editor?.initialize(EDITOR_ID, { |
| 42 |
mediaButtons: window.CODE_SNIPPETS_EDIT?.descEditorOptions.mediaButtons, |
| 43 |
quicktags: true, |
| 44 |
tinymce: { |
| 45 |
toolbar: TOOLBAR_BUTTONS.map(line => line.join(' ')), |
| 46 |
setup: editor => { |
| 47 |
editor.on('change', () => onChange(editor.getContent())) |
| 48 |
} |
| 49 |
} |
| 50 |
}) |
| 51 |
} |
| 52 |
|
| 53 |
export const DescriptionEditor: React.FC<SnippetInputProps> = ({ snippet, setSnippet, isReadOnly }) => { |
| 54 |
const onChange = useCallback( |
| 55 |
(desc: string) => setSnippet(previous => ({ ...previous, desc })), |
| 56 |
[setSnippet] |
| 57 |
) |
| 58 |
|
| 59 |
useEffect(() => { |
| 60 |
domReady(() => initializeEditor(onChange)) |
| 61 |
}, [onChange]) |
| 62 |
|
| 63 |
return window.CODE_SNIPPETS_EDIT?.enableDescription ? |
| 64 |
<div className="snippet-description-container"> |
| 65 |
<h2> |
| 66 |
<label htmlFor={EDITOR_ID}> |
| 67 |
{__('Description', 'code-snippets')} |
| 68 |
</label> |
| 69 |
</h2> |
| 70 |
|
| 71 |
<textarea |
| 72 |
id={EDITOR_ID} |
| 73 |
className="wp-editor-area" |
| 74 |
onChange={event => onChange(event.target.value)} |
| 75 |
autoComplete="off" |
| 76 |
disabled={isReadOnly} |
| 77 |
rows={window.CODE_SNIPPETS_EDIT?.descEditorOptions.rows ?? DEFAULT_ROWS} |
| 78 |
cols={40} |
| 79 |
>{snippet.desc}</textarea> |
| 80 |
</div> : |
| 81 |
null |
| 82 |
} |
| 83 |
|