@@ -2,6 +2,8 @@ import { createRoot } from '@wordpress/element';
2
2
import GenerateExcerptWithAI from './excerpt' ;
3
3
import GenerateFeaturedImageWithAI from './featured-image' ;
4
4
import { GenerateTextWithAi } from './text-with-ai' ;
5
+ import React from 'react' ;
6
+ import { EditTextWithAi } from './edit-text-with-ai' ;
5
7
6
8
( function ( ) {
7
9
'use strict' ;
@@ -48,14 +50,16 @@ import { GenerateTextWithAi } from './text-with-ai';
48
50
}
49
51
} ;
50
52
51
- const addTextWithAI = ( blockName ) => {
52
- const textPanel = document . querySelector ( '.block-editor-block-card__content' ) ;
53
- if ( textPanel && ! document . querySelector ( '.e-text-ai' ) ) {
53
+ const addTextWithAI = ( blockName , blockClientId ) => {
54
+ const textPanel = document . querySelector ( '.block-editor-block-card__description, .block-editor-block-card__content' ) ;
55
+ if ( textPanel && ! document . querySelector ( `.e-text-ai[data-client-id="${ blockClientId } "]` ) ) {
56
+ removeAiIndicator ( ) ;
54
57
const rootElement = document . createElement ( 'div' ) ;
55
58
rootElement . classList . add ( 'e-text-ai' ) ;
59
+ rootElement . setAttribute ( 'data-client-id' , blockClientId ) ;
56
60
textPanel . appendChild ( rootElement ) ;
57
61
const root = createRoot ( rootElement ) ;
58
- root . render ( < GenerateTextWithAi blockName = { blockName } /> ) ;
62
+ root . render ( < GenerateTextWithAi blockName = { blockName } blockClientId = { blockClientId } /> ) ;
59
63
}
60
64
} ;
61
65
@@ -78,7 +82,7 @@ import { GenerateTextWithAi } from './text-with-ai';
78
82
const addAiIndicatorToTextBlock = ( blockNames ) => {
79
83
const selectedBlock = wp . data . select ( 'core/block-editor' ) ?. getSelectedBlock ( ) ;
80
84
if ( selectedBlock && blockNames . some ( ( name ) => selectedBlock . name . includes ( name ) ) ) {
81
- addTextWithAI ( selectedBlock . name ) ;
85
+ addTextWithAI ( selectedBlock . name , selectedBlock . clientId ) ;
82
86
} else {
83
87
removeAiIndicator ( ) ;
84
88
}
@@ -89,5 +93,57 @@ import { GenerateTextWithAi } from './text-with-ai';
89
93
addAiIndicator ( 'featured-image' , addGenerateFeaturedImageWithAI ) ;
90
94
addAiIndicatorToTextBlock ( [ 'paragraph' , 'heading' ] ) ;
91
95
} ) ;
96
+
97
+ const observer = new MutationObserver ( ( mutationsList ) => {
98
+ for ( const mutation of mutationsList ) {
99
+ if ( 'childList' === mutation . type ) {
100
+ if ( document . querySelector ( '.editor-post-excerpt' ) ) {
101
+ addGenerateExcerptWithAI ( ) ;
102
+ }
103
+ }
104
+ }
105
+ } ) ;
106
+
107
+ observer . observe ( document . body , { childList : true , subtree : true } ) ;
108
+ window . addEventListener ( 'beforeunload' , ( ) => {
109
+ observer . disconnect ( ) ;
110
+ } ) ;
92
111
} ) ;
93
112
} ) ( jQuery ) ;
113
+
114
+ ( function ( wp ) {
115
+ const { addFilter } = wp . hooks ;
116
+
117
+ const addAiButtonToToolbar = ( BlockEdit ) => {
118
+ return ( props ) => {
119
+ return < EditTextWithAi { ...props } blockEdit = { BlockEdit } /> ;
120
+ } ;
121
+ } ;
122
+
123
+ addFilter ( 'editor.BlockEdit' , 'elementor-ai-toolbar-button' , addAiButtonToToolbar ) ;
124
+ } ) ( window . wp ) ;
125
+
126
+ ( function ( ) {
127
+ 'use strict' ;
128
+
129
+ const setElementorWpAiCurrentContext = ( ) => {
130
+ const selectedBlock = wp . data . select ( 'core/block-editor' ) . getSelectedBlock ( ) ;
131
+ if ( selectedBlock ) {
132
+ const blockName = 'core/heading' === selectedBlock . name ? 'heading' : selectedBlock . name ;
133
+ window . elementorWpAiCurrentContext = {
134
+ widgetType : blockName ,
135
+ controlName : blockName ,
136
+ } ;
137
+ } else {
138
+ window . elementorWpAiCurrentContext = null ;
139
+ }
140
+ } ;
141
+
142
+ wp . data . subscribe ( setElementorWpAiCurrentContext ) ;
143
+
144
+ const clearElementorAiCurrentContext = ( ) => {
145
+ window . elementorWpAiCurrentContext = null ;
146
+ } ;
147
+
148
+ window . addEventListener ( 'beforeunload' , clearElementorAiCurrentContext ) ;
149
+ } ) ( ) ;
0 commit comments