PluginProbe
Code Snippets / 3.5.0
Code Snippets v3.5.0
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / Edit / fields / DescriptionEditor.tsx

DescriptionEditor.tsx in Code Snippets 3.5.0, at js/Edit/fields/DescriptionEditor.tsx

83 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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