@brilo/base-blocks 0.1.38 → 0.1.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,16 @@
1
- import {FIELD_SELECT_MODE_VALUES, FIELD_TEXT_TYPES, FIELD_TEXT_TYPE_VALUES, FIELD_TYPE_VALUES} from './constants'
1
+ import {
2
+ FIELD_SELECT_MODE_VALUES,
3
+ FIELD_TEXT_TYPES,
4
+ FIELD_TEXT_TYPE_VALUES,
5
+ FIELD_TYPE_VALUES,
6
+ FILE_BUTTON_VARIANT_VALUES,
7
+ } from './constants'
2
8
  import type {BriloBlockType} from '~/types'
3
9
 
4
10
  export type FormFieldType = (typeof FIELD_TYPE_VALUES)[number]
5
11
  export type FormFieldTextType = (typeof FIELD_TEXT_TYPE_VALUES)[number]
6
12
  export type FormFieldSelectMode = (typeof FIELD_SELECT_MODE_VALUES)[number]
13
+ export type FormFieldFileButtonVariant = (typeof FILE_BUTTON_VARIANT_VALUES)[number]
7
14
 
8
15
  export type FormFieldOption = string | {label?: string; value?: string; email?: string}
9
16
 
@@ -19,6 +26,14 @@ export interface FormFieldAttributes extends Record<string, unknown> {
19
26
  selectMode: FormFieldSelectMode
20
27
  defaultValue: string
21
28
  value: string
29
+ isReplyTo: boolean
30
+ gtmEnabled: boolean
31
+ multiple: boolean
32
+ extensionsPreset: string[]
33
+ extensionsCustom: string
34
+ buttonVariant: FormFieldFileButtonVariant
35
+ buttonOutline: boolean
36
+ buttonLabel: string
22
37
  }
23
38
 
24
39
  export interface FormFieldOptions extends BriloBlockType {}
@@ -1,10 +1,10 @@
1
1
  import {InspectorControls, RichText} from '@wordpress/block-editor'
2
2
  import {FormTokenField, Notice, PanelBody, SelectControl, TextControl, ToggleControl} from '@wordpress/components'
3
- import {useDispatch} from '@wordpress/data'
3
+ import {useDispatch, useSelect} from '@wordpress/data'
4
4
  import {__} from '@wordpress/i18n'
5
5
  import {cleanForSlug} from '@wordpress/url'
6
6
  import clsx from 'clsx'
7
- import {useEffect} from 'react'
7
+ import {useEffect, useMemo} from 'react'
8
8
  import {useGroupBlock} from '~/hooks/useGroupBlock'
9
9
  import {EXTERNAL_METHODS, INTERNAL_SUBMIT_BEHAVIORS, PROCESSING_MODES} from './constants'
10
10
  import type {ExternalMethod, FormGroupAttributes, FormGroupOptions, InternalSubmitBehavior, ProcessingMode} from './types'
@@ -18,6 +18,41 @@ function generateFormId(formName: string): string {
18
18
  return `form-${slug}-${hash}`
19
19
  }
20
20
 
21
+ function slugifyFieldName(raw: string): string {
22
+ return (raw ?? '')
23
+ .normalize('NFD')
24
+ .replace(/\p{Diacritic}/gu, '')
25
+ .replace(/[\s_]+/g, '-')
26
+ .replace(/[^a-zA-Z0-9_-]/g, '')
27
+ .replace(/[-_]{2,}/g, '-')
28
+ .replace(/^[-_]+/g, '')
29
+ .toLowerCase()
30
+ }
31
+
32
+ function gtmPlaceholderValue(type?: string, textType?: string): string {
33
+ if (type === 'select') {
34
+ return '<vybraná možnost>'
35
+ }
36
+
37
+ if (type === 'textarea') {
38
+ return '<vyplněný text>'
39
+ }
40
+
41
+ if (textType === 'email') {
42
+ return '<vyplněný email>'
43
+ }
44
+
45
+ if (textType === 'tel') {
46
+ return '<vyplněné telefonní číslo>'
47
+ }
48
+
49
+ if (textType === 'number') {
50
+ return '<vyplněné číslo>'
51
+ }
52
+
53
+ return '<vyplněná hodnota>'
54
+ }
55
+
21
56
  export function createFormGroupEdit(options: FormGroupOptions) {
22
57
  return function Edit({
23
58
  attributes,
@@ -40,17 +75,59 @@ export function createFormGroupEdit(options: FormGroupOptions) {
40
75
  const isRecipientsMissing = isInternal && recipients.length === 0
41
76
  const isExternalUrlMissing = isExternal && (attributes.externalUrl ?? '').trim() === ''
42
77
 
78
+ type ChildFieldAttributes = {
79
+ type?: string
80
+ selectMode?: string
81
+ textType?: string
82
+ isReplyTo?: boolean
83
+ gtmEnabled?: boolean
84
+ name?: string
85
+ label?: string
86
+ }
87
+
88
+ const {replyToCount, gtmFields} = useSelect(
89
+ (select) => {
90
+ const editor = select('core/block-editor') as unknown as {
91
+ getBlocks: (rootClientId: string) => Array<{attributes: ChildFieldAttributes}>
92
+ }
93
+ const blocks = editor.getBlocks(clientId) || []
94
+ const replyToCount = blocks.filter(
95
+ (block) => block.attributes.type === 'text' && block.attributes.textType === 'email' && block.attributes.isReplyTo === true,
96
+ ).length
97
+ const gtmFields = blocks.filter((block) => block.attributes.gtmEnabled).map((block) => block.attributes)
98
+
99
+ return {replyToCount, gtmFields}
100
+ },
101
+ [clientId],
102
+ )
103
+
104
+ const hasMultipleReplyTo = replyToCount > 1
105
+
106
+ const gtmPreview = useMemo(() => {
107
+ const preview: Record<string, string> = {
108
+ event: (attributes.gtmEventName ?? '').trim() || '<název eventu>',
109
+ url: '<URL stránky, odkud bylo odesláno>',
110
+ }
111
+
112
+ gtmFields.forEach((field) => {
113
+ const name = (field.name && field.name.trim() !== '' ? field.name : slugifyFieldName(field.label ?? '')) || '<pole-bez-nazvu>'
114
+ preview[name] = gtmPlaceholderValue(field.type, field.textType)
115
+ })
116
+
117
+ return preview
118
+ }, [attributes.gtmEventName, gtmFields])
119
+
43
120
  useEffect(() => {
44
121
  const lockName = `brilo-form-group-${clientId}`
45
122
 
46
- if (isRecipientsMissing || isExternalUrlMissing) {
123
+ if (isRecipientsMissing || isExternalUrlMissing || hasMultipleReplyTo) {
47
124
  lockPostSaving(lockName)
48
125
  } else {
49
126
  unlockPostSaving(lockName)
50
127
  }
51
128
 
52
129
  return () => unlockPostSaving(lockName)
53
- }, [clientId, isRecipientsMissing, isExternalUrlMissing, lockPostSaving, unlockPostSaving])
130
+ }, [clientId, isRecipientsMissing, isExternalUrlMissing, hasMultipleReplyTo, lockPostSaving, unlockPostSaving])
54
131
 
55
132
  // Generated once and persisted into the block's attributes so it stays stable across edits.
56
133
  // biome-ignore lint/correctness/useExhaustiveDependencies: must run once on mount only
@@ -177,12 +254,67 @@ export function createFormGroupEdit(options: FormGroupOptions) {
177
254
  </>
178
255
  )}
179
256
 
257
+ {hasMultipleReplyTo && (
258
+ <Notice status="warning" isDismissible={false}>
259
+ {__('Formulář obsahuje více polí označených jako Reply-To. Podporováno je pouze jedno takové pole.', 'brilo-blocks')}
260
+ </Notice>
261
+ )}
262
+
180
263
  <TextControl
181
264
  label={__('Titulek tlačítka odeslání', 'brilo-blocks')}
182
265
  value={attributes.submit?.label ?? 'Odeslat'}
183
266
  onChange={(label) => setAttributes({submit: {label}})}
184
267
  />
185
268
  </PanelBody>
269
+
270
+ {isInternal && (
271
+ <PanelBody title={__('Google Tag Manager', 'brilo-blocks')} initialOpen={false}>
272
+ <ToggleControl
273
+ label={__('Odesílat data do dataLayer', 'brilo-blocks')}
274
+ help={__(
275
+ 'Po úspěšném odeslání formuláře se pushne event do window.dataLayer. Zbytek (cíle, tagy) se nastavuje v GTM.',
276
+ 'brilo-blocks',
277
+ )}
278
+ checked={attributes.gtmEnabled ?? false}
279
+ onChange={(gtmEnabled) => setAttributes({gtmEnabled})}
280
+ />
281
+
282
+ {(attributes.gtmEnabled ?? false) && (
283
+ <>
284
+ <TextControl
285
+ label={__('Název GTM eventu', 'brilo-blocks')}
286
+ placeholder={__('napr. website_form', 'brilo-blocks')}
287
+ value={attributes.gtmEventName ?? ''}
288
+ onChange={(gtmEventName) => setAttributes({gtmEventName})}
289
+ help={__(
290
+ 'Pushne se { event: <tento název>, url: <stránka, odkud bylo odesláno>, ...pole s GTM }. Jednotlivá pole si zapnete u nich ("Odesílat do GTM").',
291
+ 'brilo-blocks',
292
+ )}
293
+ />
294
+
295
+ {(attributes.gtmEventName ?? '').trim() === '' && (
296
+ <Notice status="warning" isDismissible={false}>
297
+ {__('Bez názvu eventu se dataLayer push neodešle.', 'brilo-blocks')}
298
+ </Notice>
299
+ )}
300
+
301
+ <p>
302
+ <strong>{__('Náhled objektu při úspěšném odeslání:', 'brilo-blocks')}</strong>
303
+ </p>
304
+ <pre className="g-form__gtm-preview">{JSON.stringify(gtmPreview, null, 2)}</pre>
305
+
306
+ {gtmFields.length === 0 && (
307
+ <Notice status="info" isDismissible={false}>
308
+ {__(
309
+ 'Žádné pole formuláře nemá zapnuté "Odesílat do GTM" - do dataLayer půjde jen event a url. Zapněte to u polí, jejichž hodnotu chcete v dataLayer mít.',
310
+ 'brilo-blocks',
311
+ )}
312
+ </Notice>
313
+ )}
314
+ </>
315
+ )}
316
+ </PanelBody>
317
+ )}
186
318
  </InspectorControls>
187
319
 
188
320
  <div {...blockProps}>
@@ -1,41 +1,54 @@
1
1
  .g-form {
2
2
  width: 100%;
3
+ }
3
4
 
4
- &__fields {
5
- display: grid;
6
- grid-template-columns: repeat(12, minmax(0, 1fr));
7
- gap: 16px;
8
- padding: 16px;
9
- padding-bottom: 56px;
10
- outline: 1px dashed #ddd;
5
+ .g-form__fields {
6
+ display: grid;
7
+ grid-template-columns: repeat(12, minmax(0, 1fr));
8
+ gap: 16px;
9
+ padding: 16px;
10
+ padding-bottom: 56px;
11
+ outline: 1px dashed #ddd;
12
+ }
11
13
 
12
- > * {
13
- max-width: 100%;
14
- }
15
- }
14
+ .g-form__fields > * {
15
+ max-width: 100%;
16
+ }
16
17
 
17
- &__gdpr {
18
- margin-top: 16px;
19
- padding: 16px;
20
- border: 1px solid #ddd;
21
- border-radius: 4px;
22
- background: #fff;
23
- }
18
+ .g-form__gdpr {
19
+ margin-top: 16px;
20
+ padding: 16px;
21
+ border: 1px solid #ddd;
22
+ border-radius: 4px;
23
+ background: #fff;
24
+ }
24
25
 
25
- &__gdpr-header {
26
- display: flex;
27
- flex-wrap: wrap;
28
- justify-content: space-between;
29
- align-items: center;
30
- gap: 12px;
31
- margin-bottom: 12px;
32
- }
26
+ .g-form__gdpr-header {
27
+ display: flex;
28
+ flex-wrap: wrap;
29
+ justify-content: space-between;
30
+ align-items: center;
31
+ gap: 12px;
32
+ margin-bottom: 12px;
33
+ }
34
+
35
+ .g-form__gdpr-content {
36
+ min-height: 56px;
37
+ padding: 10px 12px;
38
+ border: 1px solid #ddd;
39
+ border-radius: 4px;
40
+ background: #fff;
41
+ }
33
42
 
34
- &__gdpr-content {
35
- min-height: 56px;
36
- padding: 10px 12px;
37
- border: 1px solid #ddd;
38
- border-radius: 4px;
39
- background: #fff;
40
- }
43
+ // Rendered inside InspectorControls, not inside the block content area itself.
44
+ .g-form__gtm-preview {
45
+ max-width: 100%;
46
+ padding: 8px;
47
+ overflow-x: auto;
48
+ border: 1px solid #ddd;
49
+ border-radius: 4px;
50
+ background: #f0f0f0;
51
+ font-size: 11px;
52
+ white-space: pre-wrap;
53
+ word-break: break-word;
41
54
  }
@@ -29,6 +29,8 @@ export function createFormGroupBlock(options: FormGroupOptions): BlockConfigurat
29
29
  submit: {type: 'object', default: {label: 'Odeslat'}},
30
30
  gdpr: {type: 'object', default: {content: '', checkbox: true}},
31
31
  successMessage: {type: 'string', default: 'Formulář byl úspěšně odeslán.'},
32
+ gtmEnabled: {type: 'boolean', default: false},
33
+ gtmEventName: {type: 'string', default: ''},
32
34
  },
33
35
  edit: createFormGroupEdit(options),
34
36
  save: () => <InnerBlocks.Content />,
@@ -18,6 +18,8 @@ export interface FormGroupAttributes extends Record<string, unknown> {
18
18
  submit: {label: string}
19
19
  gdpr: {content: string; checkbox: boolean}
20
20
  successMessage: string
21
+ gtmEnabled: boolean
22
+ gtmEventName: string
21
23
  }
22
24
 
23
25
  export interface FormGroupOptions extends BriloBlockType {}