@@ -77,78 +77,86 @@ export class EdgelessCopilotWidget extends WidgetComponent<RootBlockModel> {
77
77
this . _visible = visible ;
78
78
}
79
79
80
- private _showCopilotPanel ( ) {
80
+ private _showCopilotInput ( ) {
81
81
requestConnectedFrame ( ( ) => {
82
- if ( ! this . _copilotPanel ) {
83
- const panel = new EdgelessCopilotPanel ( ) ;
84
- panel . host = this . host ;
85
- panel . groups = this . groups ;
86
- this . renderRoot . append ( panel ) ;
87
- this . _copilotPanel = panel ;
88
- }
89
-
90
82
const referenceElement = this . selectionElem ;
91
- const panel = this . _copilotPanel ;
92
- // @TODO : optimize
93
- const viewport = this . gfx . viewport ;
94
-
95
83
if ( ! referenceElement || ! referenceElement . isConnected ) return ;
96
84
97
85
// show ai input
98
86
const rootBlockId = this . host . doc . root ?. id ;
99
- if ( rootBlockId ) {
100
- const aiPanel = this . host . view . getWidget (
101
- AFFINE_AI_PANEL_WIDGET ,
102
- rootBlockId
103
- ) ;
104
- if ( aiPanel instanceof AffineAIPanelWidget && aiPanel . config ) {
105
- aiPanel . setState ( 'input' , referenceElement ) ;
106
- }
87
+ if ( ! rootBlockId ) return ;
88
+
89
+ const input = this . host . view . getWidget (
90
+ AFFINE_AI_PANEL_WIDGET ,
91
+ rootBlockId
92
+ ) ;
93
+
94
+ if ( input instanceof AffineAIPanelWidget ) {
95
+ input . setState ( 'input' , referenceElement ) ;
96
+ requestAnimationFrame ( ( ) => {
97
+ this . _createCopilotPanel ( ) ;
98
+ this . _updateCopilotPanel ( input ) ;
99
+ } ) ;
107
100
}
101
+ } , this ) ;
102
+ }
108
103
109
- autoUpdate ( referenceElement , panel , ( ) => {
110
- computePosition ( referenceElement , panel , {
111
- placement : 'right-start' ,
112
- middleware : [
113
- offset ( {
114
- mainAxis : 16 ,
115
- crossAxis : 45 ,
116
- } ) ,
117
- flip ( {
118
- mainAxis : true ,
104
+ private _createCopilotPanel ( ) {
105
+ if ( ! this . _copilotPanel ) {
106
+ const panel = new EdgelessCopilotPanel ( ) ;
107
+ panel . host = this . host ;
108
+ panel . groups = this . groups ;
109
+ this . renderRoot . append ( panel ) ;
110
+ this . _copilotPanel = panel ;
111
+ }
112
+ }
113
+
114
+ private _updateCopilotPanel ( referenceElement : HTMLElement ) {
115
+ // @TODO : optimize
116
+ const viewport = this . gfx . viewport ;
117
+ const panel = this . _copilotPanel ;
118
+ if ( ! panel ) return ;
119
+ autoUpdate ( referenceElement , panel , ( ) => {
120
+ computePosition ( referenceElement , panel , {
121
+ placement : 'bottom-start' ,
122
+ middleware : [
123
+ offset ( {
124
+ mainAxis : 4 ,
125
+ } ) ,
126
+ flip ( {
127
+ mainAxis : true ,
128
+ crossAxis : true ,
129
+ flipAlignment : true ,
130
+ } ) ,
131
+ shift ( ( ) => {
132
+ const { left, top, width, height } = viewport ;
133
+ return {
134
+ padding : 20 ,
119
135
crossAxis : true ,
120
- flipAlignment : true ,
121
- } ) ,
122
- shift ( ( ) => {
123
- const { left, top, width, height } = viewport ;
124
- return {
125
- padding : 20 ,
126
- crossAxis : true ,
127
- rootBoundary : {
128
- x : left ,
129
- y : top ,
130
- width,
131
- height : height - 100 ,
132
- } ,
133
- } ;
134
- } ) ,
135
- size ( {
136
- apply : ( { elements } ) => {
137
- const { height } = viewport ;
138
- elements . floating . style . maxHeight = `${ height - 140 } px` ;
136
+ rootBoundary : {
137
+ x : left ,
138
+ y : top ,
139
+ width,
140
+ height : height - 100 ,
139
141
} ,
140
- } ) ,
141
- ] ,
142
+ } ;
143
+ } ) ,
144
+ size ( {
145
+ apply : ( { elements } ) => {
146
+ const { height } = viewport ;
147
+ elements . floating . style . maxHeight = `${ height - 140 } px` ;
148
+ } ,
149
+ } ) ,
150
+ ] ,
151
+ } )
152
+ . then ( ( { x, y } ) => {
153
+ panel . style . left = `${ x } px` ;
154
+ panel . style . top = `${ y } px` ;
142
155
} )
143
- . then ( ( { x, y } ) => {
144
- panel . style . left = `${ x } px` ;
145
- panel . style . top = `${ y } px` ;
146
- } )
147
- . catch ( e => {
148
- console . warn ( "Can't compute EdgelessCopilotPanel position" , e ) ;
149
- } ) ;
150
- } ) ;
151
- } , this ) ;
156
+ . catch ( e => {
157
+ console . warn ( "Can't compute EdgelessCopilotPanel position" , e ) ;
158
+ } ) ;
159
+ } ) ;
152
160
}
153
161
154
162
private _updateSelection ( rect : DOMRect ) {
@@ -201,7 +209,7 @@ export class EdgelessCopilotWidget extends WidgetComponent<RootBlockModel> {
201
209
this . _visible = true ;
202
210
this . _updateSelection ( CopilotSelectionTool . area ) ;
203
211
if ( shouldShowPanel ) {
204
- this . _showCopilotPanel ( ) ;
212
+ this . _showCopilotInput ( ) ;
205
213
this . _watchClickOutside ( ) ;
206
214
} else {
207
215
this . hideCopilotPanel ( ) ;
0 commit comments