@@ -19,6 +19,7 @@ import { act, fakeTimers, render } from "../../test/render";
1919
2020import { flatToMarkdown , pmToFlat , serializeEditorDoc } from "~/composer-rich-text-doc" ;
2121import { isMacPlatform } from "~/lib/utils" ;
22+ import { registerCommands } from "~/shortcuts/dispatcher" ;
2223import {
2324 ComposerPromptEditorTiptap as ComposerPromptEditor ,
2425 type ComposerPromptEditorHandle ,
@@ -36,11 +37,13 @@ function Composer({
3637 richTextEnabled,
3738 skills,
3839 onChange,
40+ onCommandKeyDown = ( ) => false ,
3941} : {
4042 value : string ;
4143 richTextEnabled : boolean ;
4244 skills : ReadonlyArray < ServerProviderSkill > ;
4345 onChange ?: ComposerPromptEditorProps [ "onChange" ] | undefined ;
46+ onCommandKeyDown ?: ComposerPromptEditorProps [ "onCommandKeyDown" ] | undefined ;
4447} ) {
4548 const [ prompt , setPrompt ] = useState ( { value, cursor : value . length } ) ;
4649 return (
@@ -58,6 +61,7 @@ function Composer({
5861 onChange ?.( ...args ) ;
5962 } }
6063 onPaste = { ( ) => { } }
64+ onCommandKeyDown = { onCommandKeyDown }
6165 />
6266 ) ;
6367}
@@ -67,9 +71,16 @@ function composer(
6771 richTextEnabled = true ,
6872 skills : ReadonlyArray < ServerProviderSkill > = [ ] ,
6973 onChange ?: ComposerPromptEditorProps [ "onChange" ] ,
74+ onCommandKeyDown ?: ComposerPromptEditorProps [ "onCommandKeyDown" ] ,
7075) {
7176 return (
72- < Composer value = { value } richTextEnabled = { richTextEnabled } skills = { skills } onChange = { onChange } />
77+ < Composer
78+ value = { value }
79+ richTextEnabled = { richTextEnabled }
80+ skills = { skills }
81+ onChange = { onChange }
82+ onCommandKeyDown = { onCommandKeyDown }
83+ />
7384 ) ;
7485}
7586
@@ -254,6 +265,55 @@ it.each([
254265 } ,
255266) ;
256267
268+ it . each ( [
269+ [ "e" , false , false ] ,
270+ [ "i" , false , false ] ,
271+ [ "e" , true , false ] ,
272+ [ "e" , false , true ] ,
273+ ] ) (
274+ "leaves Mod+%s to an app command bound to it instead of formatting (composer command: %s, declined capture command: %s)" ,
275+ async ( key , composerClaimsKey , declinedCapture ) => {
276+ const openUsage = vi . fn ( ( event : KeyboardEvent ) => {
277+ if ( declinedCapture ) return false ;
278+ event . preventDefault ( ) ;
279+ } ) ;
280+ using _commands = {
281+ [ Symbol . dispose ] : registerCommands (
282+ { "usage.open" : openUsage } ,
283+ {
284+ keybindings : ( ) => [
285+ {
286+ command : "usage.open" ,
287+ shortcut : {
288+ key,
289+ metaKey : false ,
290+ ctrlKey : false ,
291+ shiftKey : false ,
292+ altKey : false ,
293+ modKey : true ,
294+ } ,
295+ } ,
296+ ] ,
297+ capture : declinedCapture ,
298+ } ,
299+ ) ,
300+ } ;
301+ const composerCommand = vi . fn ( ( ) => composerClaimsKey ) ;
302+ await using screen = await mountEditor ( "word" , true , [ ] , undefined , composerCommand ) ;
303+ const { editor } = screen ;
304+ await act ( async ( ) => {
305+ editor . commands . focus ( ) ;
306+ editor . commands . setTextSelection ( { from : 1 , to : 5 } ) ;
307+ } ) ;
308+ await vi . waitFor ( ( ) => expect ( document . activeElement ) . toBe ( editor . view . dom ) ) ;
309+ const modifier = isMacPlatform ( navigator . platform ) ? "Meta" : "Control" ;
310+ await userEvent . keyboard ( `{${ modifier } >}${ key } {/${ modifier } }` ) ;
311+ expect ( composerCommand ) . toHaveBeenCalled ( ) ;
312+ expect ( openUsage ) . toHaveBeenCalledTimes ( composerClaimsKey ? 0 : 1 ) ;
313+ expect ( serializeEditorDoc ( editor . state . doc ) . value ) . toBe ( declinedCapture ? "`word`" : "word" ) ;
314+ } ,
315+ ) ;
316+
257317it . each ( [ false , true ] ) (
258318 "composes an umlaut without wrapping the preedit text (rich text: %s)" ,
259319 async ( richTextEnabled ) => {
0 commit comments