@@ -4,6 +4,7 @@ import { store, getContext, getConfig } from '@wordpress/interactivity';
4
4
const domParser = new DOMParser ( ) ;
5
5
const d = document ;
6
6
const qs = d . querySelector . bind ( d ) as typeof document . querySelector ;
7
+ const dialog = qs < HTMLDialogElement > ( 'dialog[data-wp-interactive="nextgenthemes_arve_dialog"]' ) ;
7
8
8
9
setupInteractivityApi ( ) ;
9
10
setBodyBackgroundColorAsCssVar ( ) ;
@@ -17,6 +18,23 @@ function setBodyBackgroundColorAsCssVar() {
17
18
}
18
19
}
19
20
21
+ // ACF
22
+ window . jQuery ( document ) . on ( 'click' , '.arve-btn:not([data-editor="content"])' , ( e : Event ) => {
23
+ e . preventDefault ( ) ;
24
+
25
+ const openBtn = qs < HTMLButtonElement > (
26
+ '[data-wp-on--click="actions.openShortcodeDialog"][data-editor="content"]'
27
+ ) ;
28
+ const insertBtn = qs < HTMLButtonElement > ( '[data-wp-on--click="actions.insertShortcode"]' ) ;
29
+
30
+ if ( ! openBtn || ! insertBtn || ! dialog ) {
31
+ console . error ( 'Open btn, insert btn od dialog not found' ) ; // eslint-disable-line
32
+ return ;
33
+ }
34
+
35
+ openBtn . dispatchEvent ( new Event ( 'click' ) ) ;
36
+ } ) ;
37
+
20
38
function setupInteractivityApi ( ) {
21
39
const namespace = qs < HTMLElement > ( '[data-wp-interactive^="nextgenthemes"]' ) ?. dataset
22
40
?. wpInteractive ;
@@ -51,18 +69,43 @@ function setupInteractivityApi() {
51
69
toggleHelp : ( ) => {
52
70
state . help = ! state . help ;
53
71
} ,
54
- openShortcodeDialog : ( ) => {
55
- state . dialog = document . querySelector (
56
- 'dialog[data-wp-interactive="nextgenthemes_arve_dialog"]'
57
- ) ;
58
- state . dialog . showModal ( ) ;
72
+ openShortcodeDialog : ( event : Event ) => {
73
+ const editorId = event . target instanceof HTMLElement && event . target . dataset . editor ;
74
+
75
+ if ( ! dialog || ! editorId ) {
76
+ console . error ( 'Dialog or editorId not found' ) ; // eslint-disable-line
77
+ return ;
78
+ }
79
+
80
+ dialog . dataset . editor = editorId ;
81
+ dialog . showModal ( ) ;
59
82
} ,
60
83
insertShortcode : ( ) => {
61
- window . wp . media . editor . insert ( state . shortcode ) ;
62
- state . dialog . close ( ) ;
84
+ const editorId = dialog ?. dataset . editor ;
85
+
86
+ if ( ! editorId ) {
87
+ console . error ( 'Editor ID not found' ) ; // eslint-disable-line
88
+ } else if ( 'content' === editorId ) {
89
+ window . wp . media . editor . insert ( state . shortcode ) ;
90
+ } else {
91
+ // Ensure TinyMCE is loaded and the editor exists
92
+ if (
93
+ typeof window . tinymce === 'undefined' ||
94
+ ! window . tinymce . get ( editorId )
95
+ ) {
96
+ console . error ( 'TinyMCE not initialized for field: ' + editorId ) ; // eslint-disable-line
97
+ return ;
98
+ }
99
+
100
+ window . tinymce . get ( editorId ) . insertContent ( state . shortcode ) ;
101
+ }
102
+
103
+ actions . closeShortcodeDialog ( ) ;
63
104
} ,
64
105
closeShortcodeDialog : ( ) => {
65
- state . dialog . close ( ) ;
106
+ if ( dialog ) {
107
+ dialog . close ( ) ;
108
+ }
66
109
} ,
67
110
changeTab : ( ) => {
68
111
const context = getContext < optionContext > ( ) ;
@@ -101,8 +144,8 @@ function setupInteractivityApi() {
101
144
}
102
145
} ,
103
146
selectImage : ( ) => {
104
- if ( state . dialog ) {
105
- state . dialog . close ( ) ;
147
+ if ( dialog ) {
148
+ dialog . close ( ) ;
106
149
}
107
150
108
151
const context = getContext < optionContext > ( ) ;
@@ -118,13 +161,13 @@ function setupInteractivityApi() {
118
161
// We convert uploadedImage to a JSON object to make accessing it easier
119
162
const attachmentID = uploadedImage . toJSON ( ) . id ;
120
163
state . options [ context . option_key ] = attachmentID ;
121
- if ( state . dialog ) {
122
- state . dialog . showModal ( ) ;
164
+ if ( dialog ) {
165
+ dialog . showModal ( ) ;
123
166
}
124
167
} )
125
168
. on ( 'close' , function ( ) {
126
- if ( state . dialog ) {
127
- state . dialog . showModal ( ) ;
169
+ if ( dialog ) {
170
+ dialog . showModal ( ) ;
128
171
}
129
172
} ) ;
130
173
} ,
@@ -301,6 +344,10 @@ declare global {
301
344
wp : {
302
345
media : wpMedia ;
303
346
} ;
347
+ jQuery : any ;
348
+ tinymce : {
349
+ get : ( id : string ) => any ;
350
+ } ;
304
351
}
305
352
}
306
353
@@ -317,7 +364,6 @@ interface storeInterface {
317
364
state : {
318
365
options : Record < string , string | number | boolean > ;
319
366
help : boolean ;
320
- dialog : HTMLDialogElement ;
321
367
isSaving : boolean ;
322
368
message : string ;
323
369
shortcode : string ;
0 commit comments