1
1
import { BlockControls } from '@wordpress/block-editor' ;
2
- import { Modal , ToolbarButton , ToolbarGroup } from '@wordpress/components' ;
2
+ import { ToolbarDropdownMenu , ToolbarGroup } from '@wordpress/components' ;
3
3
import { useEffect , useState } from '@wordpress/element' ;
4
4
import { __ } from '@wordpress/i18n' ;
5
5
import { RichTextFormat , insertObject , WPFormatEditProps } from '@wordpress/rich-text' ;
6
6
7
+ import { FieldShortcodeSelectExisting } from './FieldShortcodeSelectExisting' ;
8
+ import { FieldShortcodeSelectMeta } from './FieldShortcodeSelectMeta' ;
9
+ import { FieldShortcodeSelectNew } from './FieldShortcodeSelectNew' ;
10
+ import { useExistingRemoteData } from '../../hooks/useExistingRemoteData' ;
7
11
import {
8
12
formatName ,
9
13
formatTypeSettings ,
10
14
} from '@/blocks/remote-data-container/components/field-shortcode' ;
11
15
import { FieldShortcodeSelectFieldPopover } from '@/blocks/remote-data-container/components/field-shortcode/FieldShortcodeSelectFieldPopover' ;
12
- import { FieldShortcodeSelectTabs } from '@/blocks/remote-data-container/components/field-shortcode/FieldShortcodeSelectTabs' ;
13
- import { FieldShortcodeSelectField } from '@/blocks/remote-data-container/components/field-shortcode/FieldShortcodeSelection' ;
14
16
import { sendTracksEvent } from '@/blocks/remote-data-container/utils/tracks' ;
15
17
import { getBlockDataSourceType } from '@/utils/localized-block-data' ;
18
+ import './FieldShortcode.scss' ;
16
19
17
20
function parseDataQuery ( dataQuery ?: string ) : FieldSelection | null {
18
21
if ( ! dataQuery ) {
@@ -26,36 +29,18 @@ function parseDataQuery( dataQuery?: string ): FieldSelection | null {
26
29
}
27
30
}
28
31
29
- interface QueryInput {
30
- blockName : string ;
31
- queryInput : RemoteDataQueryInput ;
32
- }
33
-
34
32
export function FieldShortcodeButton ( props : WPFormatEditProps ) {
35
33
const { onChange, onFocus, value, isObjectActive, activeObjectAttributes, contentRef } = props ;
36
34
const fieldSelection = parseDataQuery ( activeObjectAttributes ?. [ 'data-query' ] ) ;
37
-
38
- const [ queryInput , setQueryInput ] = useState < QueryInput | null > ( null ) ;
39
35
const [ showUI , setShowUI ] = useState < boolean > ( false ) ;
40
36
41
- function onClick ( ) {
42
- setShowUI ( ! showUI ) ;
43
- sendTracksEvent ( 'remotedatablocks_field_shortcode' , { action : 'toolbar_icon_clicked' } ) ;
44
- }
45
-
46
- function onClose ( ) {
47
- setShowUI ( false ) ;
48
- onFocus ( ) ;
49
- }
50
-
51
- function onSelectItem ( config : BlockConfig , data : RemoteDataQueryInput ) {
52
- setQueryInput ( {
53
- blockName : config . name ,
54
- queryInput : data ,
55
- } ) ;
56
- }
37
+ useEffect ( ( ) => {
38
+ if ( isObjectActive ) {
39
+ setShowUI ( true ) ;
40
+ }
41
+ } , [ isObjectActive ] ) ;
57
42
58
- function updateOrInsertField ( data : FieldSelection | null , fieldValue : string ) {
43
+ const updateOrInsertField = ( data : FieldSelection | null , fieldValue : string ) => {
59
44
const format : RichTextFormat = {
60
45
attributes : {
61
46
...activeObjectAttributes ,
@@ -65,86 +50,79 @@ export function FieldShortcodeButton( props: WPFormatEditProps ) {
65
50
type : formatName ,
66
51
} ;
67
52
68
- if ( Object . keys ( activeObjectAttributes ) . length ) {
69
- const replacements = value . replacements . slice ( ) ;
70
- replacements [ value . start ] = format ;
71
-
72
- onChange ( { ...value , replacements } ) ;
73
- return ;
74
- }
75
-
76
- onChange ( insertObject ( value , format ) ) ;
77
- }
78
-
79
- function onSelectField ( data : FieldSelection , fieldValue : string ) {
53
+ onChange (
54
+ Object . keys ( activeObjectAttributes ) . length
55
+ ? {
56
+ ...value ,
57
+ replacements : value . replacements . map ( ( replacement , index ) =>
58
+ index === value . start ? format : replacement
59
+ ) ,
60
+ }
61
+ : insertObject ( value , format )
62
+ ) ;
63
+ } ;
64
+
65
+ const onSelectField = ( data : FieldSelection , fieldValue : string ) => {
80
66
updateOrInsertField ( data , fieldValue ) ;
81
- onClose ( ) ;
67
+ setShowUI ( false ) ;
68
+ onFocus ( ) ;
82
69
sendTracksEvent ( 'remotedatablocks_field_shortcode' , {
83
70
action : data . action ,
84
71
data_source_type : getBlockDataSourceType ( data . remoteData ?. blockName ) ,
85
72
selection_path : data . selectionPath ,
86
73
} ) ;
87
- }
74
+ } ;
88
75
89
- function resetField ( blockName ?: string ) : void {
76
+ const resetField = ( blockName ?: string ) : void => {
90
77
updateOrInsertField ( null , 'Unbound field' ) ;
91
- setQueryInput ( null ) ;
92
78
sendTracksEvent ( 'remotedatablocks_field_shortcode' , {
93
79
action : 'reset_field_shortcode' ,
94
80
data_source_type : getBlockDataSourceType ( blockName ) ,
95
81
} ) ;
96
- }
82
+ } ;
97
83
98
- useEffect ( ( ) => {
99
- if ( isObjectActive ) {
100
- setShowUI ( true ) ;
101
- }
102
- } , [ isObjectActive ] ) ;
84
+ const remoteData = useExistingRemoteData ( ) ;
103
85
104
86
return (
105
87
< >
106
88
< BlockControls >
107
89
< ToolbarGroup >
108
- < ToolbarButton
109
- icon = "shortcode"
110
- isActive = { isObjectActive }
111
- onClick = { onClick }
112
- title = "Field shortcode"
113
- />
114
- </ ToolbarGroup >
115
- </ BlockControls >
116
-
117
- { showUI && ! fieldSelection && (
118
- < Modal
119
- overlayClassName = "remote-data-blocks-pattern__selection-modal"
120
- title = { __ ( 'Field shortcode' ) }
121
- onRequestClose = { onClose }
122
- isFullScreen
123
- >
124
- { ! queryInput && (
125
- < FieldShortcodeSelectTabs
90
+ { remoteData . length > 0 ? (
91
+ < ToolbarDropdownMenu
92
+ className = "remote-data-blocks-select-new"
93
+ icon = "shortcode"
94
+ label = { __ ( 'Select block bindings' , 'remote-data-blocks' ) }
95
+ popoverProps = { { className : 'rdb-field-shortcode_dropdown' , offset : 8 } }
96
+ >
97
+ { ( ) => (
98
+ < ToolbarGroup >
99
+ < FieldShortcodeSelectNew onSelectField = { onSelectField } />
100
+ < FieldShortcodeSelectExisting onSelectField = { onSelectField } />
101
+ < FieldShortcodeSelectMeta onSelectField = { onSelectField } />
102
+ </ ToolbarGroup >
103
+ ) }
104
+ </ ToolbarDropdownMenu >
105
+ ) : (
106
+ < FieldShortcodeSelectNew
126
107
onSelectField = { onSelectField }
127
- onSelectItem = { onSelectItem }
128
- />
129
- ) }
130
- { queryInput && (
131
- < FieldShortcodeSelectField
132
- blockName = { queryInput . blockName }
133
- onSelectField = { ( data , fieldValue ) =>
134
- onSelectField ( { ...data , selectionPath : 'select_new_tab' } , fieldValue )
135
- }
136
- queryInput = { queryInput . queryInput }
137
- fieldType = "field"
108
+ icon = "shortcode"
109
+ label = { __ ( 'Select block bindings' , 'remote-data-blocks' ) }
110
+ popoverProps = { { offset : 8 , placement : 'bottom-start' } }
111
+ text = { undefined }
138
112
/>
139
113
) }
140
- </ Modal >
141
- ) }
114
+ </ ToolbarGroup >
115
+ </ BlockControls >
116
+
142
117
{ showUI && fieldSelection && (
143
118
< FieldShortcodeSelectFieldPopover
144
119
contentRef = { contentRef }
145
120
fieldSelection = { fieldSelection }
146
121
formatTypeSettings = { formatTypeSettings }
147
- onClose = { onClose }
122
+ onClose = { ( ) => {
123
+ setShowUI ( false ) ;
124
+ onFocus ( ) ;
125
+ } }
148
126
onSelectField = { ( data , fieldValue ) =>
149
127
onSelectField ( { ...data , selectionPath : 'popover' } , fieldValue )
150
128
}
0 commit comments