import React, { useEffect } from 'react' import { __, _x } from '@wordpress/i18n' import { addQueryArgs } from '@wordpress/url' import classnames from 'classnames' import { SNIPPET_TYPES, SNIPPET_TYPE_SCOPES } from '../../../types/Snippet' import '../../../editor' import { isLicensed } from '../../../utils/general' import { getSnippetType, isProType } from '../../../utils/snippets' import { useSnippetForm } from '../../../hooks/useSnippetForm' import { CodeEditor } from './CodeEditor' import type { SnippetScope, SnippetType } from '../../../types/Snippet' import type { Editor, EditorConfiguration } from 'codemirror' interface SnippetTypeTabProps { tabType: SnippetType label: string currentType: SnippetType updateScope: (scope: SnippetScope) => void openUpgradeDialog: VoidFunction } const SnippetTypeTab: React.FC = ({ tabType, label, currentType, updateScope, openUpgradeDialog }) => { event.preventDefault() openUpgradeDialog() } } : { href: addQueryArgs(window.location.href, { type: tabType }), onClick: event => { event.preventDefault() const scope = SNIPPET_TYPE_SCOPES[tabType][0] updateScope(scope) } } }> {`${label} `} {tabType} export const TYPE_LABELS: Record = { php: __('Functions', 'code-snippets'), html: __('Content', 'code-snippets'), css: __('Styles', 'code-snippets'), js: __('Scripts', 'code-snippets') } const EDITOR_MODES: Partial> = { css: 'text/css', js: 'javascript', php: 'text/x-php', html: 'application/x-httpd-php' } interface SnippetTypeTabsProps { codeEditor: Editor snippetType: SnippetType updateScope: (scope: SnippetScope) => void openUpgradeDialog: VoidFunction } const SnippetTypeTabs: React.FC = ({ codeEditor, updateScope, snippetType, openUpgradeDialog }) => { useEffect(() => { codeEditor.setOption('lint' as keyof EditorConfiguration, 'php' === snippetType || 'css' === snippetType) if (snippetType in EDITOR_MODES) { codeEditor.setOption('mode', EDITOR_MODES[snippetType]) codeEditor.refresh() } }, [codeEditor, snippetType]) return (

{SNIPPET_TYPES.map(type => )} {!isLicensed() ? { event.preventDefault() openUpgradeDialog() }} > {_x('Upgrade to ', 'Upgrade to Pro', 'code-snippets')} {_x('Pro', 'Upgrade to Pro', 'code-snippets')} : null}

) } export interface SnippetEditorProps { openUpgradeDialog: VoidFunction } export const SnippetEditor: React.FC = ({ openUpgradeDialog }) => { const { snippet, setSnippet, codeEditorInstance } = useSnippetForm() const snippetType = getSnippetType(snippet) return (

{snippet.id || window.CODE_SNIPPETS_EDIT?.isPreview || !codeEditorInstance ? '' : { setSnippet(previous => ({ ...previous, scope })) }} />}
) }