File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1144,7 +1144,7 @@ function ComposerInlineTokenPastePlugin() {
11441144 if ( mentions . length === 0 ) {
11451145 return false ;
11461146 }
1147- event . preventDefault ( ) ;
1147+ let inserted = false ;
11481148 editor . update ( ( ) => {
11491149 const selection = $getSelection ( ) ;
11501150 if ( ! $isRangeSelection ( selection ) ) {
@@ -1163,6 +1163,19 @@ function ComposerInlineTokenPastePlugin() {
11631163 }
11641164 }
11651165 } ;
1166+ const firstMention = mentions [ 0 ] ;
1167+ if ( firstMention && firstMention . start === 0 ) {
1168+ const anchorOffset = getExpandedAbsoluteOffsetForPoint (
1169+ selection . anchor . getNode ( ) ,
1170+ selection . anchor . offset ,
1171+ ) ;
1172+ const precedingChar = $getRoot ( )
1173+ . getTextContent ( )
1174+ . slice ( anchorOffset - 1 , anchorOffset ) ;
1175+ if ( precedingChar . length > 0 && ! / \s / . test ( precedingChar ) ) {
1176+ nodes . push ( $createTextNode ( " " ) ) ;
1177+ }
1178+ }
11661179 let cursor = 0 ;
11671180 for ( const mention of mentions ) {
11681181 if ( mention . start < cursor ) {
@@ -1183,7 +1196,12 @@ function ComposerInlineTokenPastePlugin() {
11831196 nodes . push ( $createTextNode ( " " ) ) ;
11841197 }
11851198 selection . insertNodes ( nodes ) ;
1199+ inserted = true ;
11861200 } ) ;
1201+ if ( ! inserted ) {
1202+ return false ;
1203+ }
1204+ event . preventDefault ( ) ;
11871205 return true ;
11881206 } ,
11891207 COMMAND_PRIORITY_HIGH ,
Original file line number Diff line number Diff line change @@ -110,13 +110,24 @@ export default function FileBrowserPanel({
110110 return ;
111111 }
112112 if ( clicked === "add-to-chat" ) {
113- const inserted = composerRef ?. current ?. insertTextAtEnd ( ` ${ mention } ` ) ?? false ;
114- if ( ! inserted ) {
113+ const composer = composerRef ?. current ;
114+ if ( ! composer ) {
115115 toastManager . add ( {
116116 type : "error" ,
117117 title : "Unable to add to chat" ,
118118 description : "Open a chat for this project and try again." ,
119119 } ) ;
120+ return ;
121+ }
122+ const currentValue = composer . readSnapshot ( ) . value ;
123+ const needsLeadingSpace = currentValue . length > 0 && ! / \s $ / . test ( currentValue ) ;
124+ const inserted = composer . insertTextAtEnd ( `${ needsLeadingSpace ? " " : "" } ${ mention } ` ) ;
125+ if ( ! inserted ) {
126+ toastManager . add ( {
127+ type : "error" ,
128+ title : "Unable to add to chat" ,
129+ description : "The chat isn't ready to accept input right now." ,
130+ } ) ;
120131 }
121132 }
122133 } finally {
You can’t perform that action at this time.
0 commit comments