PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
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 / utils / snippets / snippets.ts

snippets.ts in Code Snippets 4.0.0-beta.1, at js/utils/snippets/snippets.ts

85 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __, sprintf } from '@wordpress/i18n'
2 import { buildUrl } from '../urls'
3 import { parseSnippetObject } from './objects'
4 import type { Snippet, SnippetScope, SnippetType } from '../../types/Snippet'
5
6 export const SNIPPET_TYPE_LABELS: Record<SnippetType, string> = {
7 php: __('Functions', 'code-snippets'),
8 html: __('Content', 'code-snippets'),
9 css: __('Styles', 'code-snippets'),
10 js: __('Scripts', 'code-snippets'),
11 cond: __('Conditions', 'code-snippets')
12 }
13
14 const PRO_TYPES = new Set<SnippetType>(['css', 'js', 'cond'])
15
16 export const createSnippetObject = (fields: unknown): Snippet =>
17 parseSnippetObject(fields)
18
19 export const cloneSnippetObject = (snippet: Snippet): Snippet =>
20 createSnippetObject({
21 ...snippet,
22 id: 0,
23 active: false,
24 // translators: %s: snippet title.
25 name: sprintf(__('%s [CLONE]', 'code-snippets'), snippet.name)
26 })
27
28 export const getSnippetType = ({ scope }: Pick<Snippet, 'scope'>): SnippetType => {
29 switch (true) {
30 case scope.endsWith('-css'):
31 return 'css'
32
33 case scope.endsWith('-js'):
34 return 'js'
35
36 case scope.endsWith('content'):
37 return 'html'
38
39 case 'condition' === scope:
40 return 'cond'
41
42 default:
43 return 'php'
44 }
45 }
46
47 export const getSnippetEditUrl = (snippet?: Pick<Snippet, 'id'>): string | undefined =>
48 snippet?.id
49 ? buildUrl(window.CODE_SNIPPETS?.urls.edit, { id: snippet.id })
50 : window.CODE_SNIPPETS?.urls.addNew
51
52 export const getSnippetDisplayName = (snippet: Pick<Snippet, 'name' | 'id' | 'scope'>): string =>
53 '' === snippet.name.trim()
54 // translators: %s: snippet identifier.
55 ? sprintf(isCondition(snippet) ? __('Condition #%d', 'code-snippets') : __('Snippet #%d', 'code-snippets'), snippet.id)
56 : snippet.name
57
58 export const validateSnippet = (snippet: Snippet): undefined | string => {
59 const missingTitle = '' === snippet.name.trim()
60 const missingCode = '' === snippet.code.trim()
61
62 switch (true) {
63 case missingCode && missingTitle:
64 return __('This snippet has no code or title.', 'code-snippets')
65
66 case missingCode:
67 return __('This snippet has no snippet code.', 'code-snippets')
68
69 case missingTitle:
70 return __('This snippet has no title.', 'code-snippets')
71
72 default:
73 return undefined
74 }
75 }
76
77 export const isCondition = (snippet: { scope?: SnippetScope }): boolean =>
78 'condition' === snippet.scope
79
80 export const isProSnippet = (snippet: Pick<Snippet, 'scope'>): boolean =>
81 PRO_TYPES.has(getSnippetType(snippet))
82
83 export const isProType = (type: SnippetType): boolean =>
84 PRO_TYPES.has(type)
85