| 1 |
import { __ } from '@wordpress/i18n' |
| 2 |
import { parseSnippetObject } from './objects' |
| 3 |
import type { Snippet, SnippetType } from '../../types/Snippet' |
| 4 |
|
| 5 |
const PRO_TYPES = new Set<SnippetType>(['css', 'js', 'cond']) |
| 6 |
|
| 7 |
export const createSnippetObject = (fields: unknown): Snippet => |
| 8 |
parseSnippetObject(fields) |
| 9 |
|
| 10 |
export const getSnippetType = ({ scope }: Pick<Snippet, 'scope'>): SnippetType => { |
| 11 |
switch (true) { |
| 12 |
case scope.endsWith('-css'): |
| 13 |
return 'css' |
| 14 |
|
| 15 |
case scope.endsWith('-js'): |
| 16 |
return 'js' |
| 17 |
|
| 18 |
case scope.endsWith('content'): |
| 19 |
return 'html' |
| 20 |
|
| 21 |
case 'condition' === scope: |
| 22 |
return 'cond' |
| 23 |
|
| 24 |
default: |
| 25 |
return 'php' |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
export const validateSnippet = (snippet: Snippet): undefined | string => { |
| 30 |
const missingTitle = '' === snippet.name.trim() |
| 31 |
const missingCode = '' === snippet.code.trim() |
| 32 |
|
| 33 |
switch (true) { |
| 34 |
case missingCode && missingTitle: |
| 35 |
return __('This snippet has no code or title.', 'code-snippets') |
| 36 |
|
| 37 |
case missingCode: |
| 38 |
return __('This snippet has no snippet code.', 'code-snippets') |
| 39 |
|
| 40 |
case missingTitle: |
| 41 |
return __('This snippet has no title.', 'code-snippets') |
| 42 |
|
| 43 |
default: |
| 44 |
return undefined |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
export const isCondition = (snippet: Pick<Snippet, 'scope'>): boolean => |
| 49 |
'condition' === snippet.scope |
| 50 |
|
| 51 |
export const isProSnippet = (snippet: Pick<Snippet, 'scope'>): boolean => |
| 52 |
PRO_TYPES.has(getSnippetType(snippet)) |
| 53 |
|
| 54 |
export const isProType = (type: SnippetType): boolean => |
| 55 |
PRO_TYPES.has(type) |
| 56 |
|