| 1 |
import { __ } from '@wordpress/i18n' |
| 2 |
import React, { useId } from 'react' |
| 3 |
import Select from 'react-select' |
| 4 |
import { useSnippetForm } from '../WithSnippetFormContext' |
| 5 |
import { SNIPPET_TYPE_SCOPES } from '../../../../types/Snippet' |
| 6 |
import { getSnippetType, isCondition } from '../../../../utils/snippets/snippets' |
| 7 |
import type { SnippetCodeScope } from '../../../../types/Snippet' |
| 8 |
import type { SelectOption } from '../../../../types/SelectOption' |
| 9 |
|
| 10 |
const SCOPE_ICONS: Record<SnippetCodeScope, string> = { |
| 11 |
'global': 'admin-site', |
| 12 |
'admin': 'admin-tools', |
| 13 |
'front-end': 'admin-appearance', |
| 14 |
'single-use': 'clock', |
| 15 |
'content': 'shortcode', |
| 16 |
'head-content': 'editor-code', |
| 17 |
'body-content': 'editor-code', |
| 18 |
'footer-content': 'editor-code', |
| 19 |
'admin-css': 'dashboard', |
| 20 |
'site-css': 'admin-customizer', |
| 21 |
'site-head-js': 'media-code', |
| 22 |
'site-footer-js': 'media-code' |
| 23 |
} |
| 24 |
|
| 25 |
const SCOPE_DESCRIPTIONS: Record<SnippetCodeScope, string> = { |
| 26 |
'global': __('Run everywhere', 'code-snippets'), |
| 27 |
'admin': __('Only run in administration area', 'code-snippets'), |
| 28 |
'front-end': __('Only run on site front-end', 'code-snippets'), |
| 29 |
'single-use': __('Only run once', 'code-snippets'), |
| 30 |
'content': __('Where inserted in editor', 'code-snippets'), |
| 31 |
'head-content': __('In site header (<head> section)', 'code-snippets'), |
| 32 |
'body-content': __('In site content (start of <body>)', 'code-snippets'), |
| 33 |
'footer-content': __('In site footer (end of <body>)', 'code-snippets'), |
| 34 |
'site-css': __('Site front-end', 'code-snippets'), |
| 35 |
'admin-css': __('Administration area', 'code-snippets'), |
| 36 |
'site-head-js': __('In site header (<head> section)', 'code-snippets'), |
| 37 |
'site-footer-js': __('In site footer (end of <body>)', 'code-snippets') |
| 38 |
} |
| 39 |
|
| 40 |
export const SnippetLocationInput: React.FC = () => { |
| 41 |
const { snippet, setSnippet, isReadOnly } = useSnippetForm() |
| 42 |
const locationId = useId() |
| 43 |
|
| 44 |
const options: SelectOption<SnippetCodeScope>[] = SNIPPET_TYPE_SCOPES[getSnippetType(snippet)] |
| 45 |
.filter(scope => 'condition' !== scope) |
| 46 |
.map(scope => ({ |
| 47 |
key: scope, |
| 48 |
value: scope, |
| 49 |
label: SCOPE_DESCRIPTIONS[scope] |
| 50 |
})) |
| 51 |
|
| 52 |
return isCondition(snippet) |
| 53 |
? null |
| 54 |
: <div className="block-form-field"> |
| 55 |
<label htmlFor={locationId}> |
| 56 |
{__('Location', 'code-snippets')} |
| 57 |
</label> |
| 58 |
|
| 59 |
<Select |
| 60 |
inputId={locationId} |
| 61 |
className="code-snippets-select code-snippets-select-location" |
| 62 |
options={options} |
| 63 |
isSearchable={false} |
| 64 |
isDisabled={isReadOnly} |
| 65 |
styles={{ |
| 66 |
menu: provided => ({ ...provided, zIndex: 9999 }), |
| 67 |
input: provided => ({ ...provided, ':focus': { boxShadow: 'none' } }) |
| 68 |
}} |
| 69 |
value={options.find(option => option.value === snippet.scope)} |
| 70 |
formatOptionLabel={({ label, value }) => |
| 71 |
<> |
| 72 |
<span className={`dashicons dashicons-${SCOPE_ICONS[value]}`} aria-hidden="true"></span>{` ${label}`} |
| 73 |
</> |
| 74 |
} |
| 75 |
onChange={option => |
| 76 |
option?.value && setSnippet(previous => ({ ...previous, scope: option.value }))} |
| 77 |
/> |
| 78 |
</div> |
| 79 |
} |
| 80 |
|