@@ -70,4 +70,83 @@ describe("registerComposerInlineTokenPaste", () => {
7070 "<mention:.changeset/improve-deploy-error-logging.md> " ,
7171 ) ;
7272 } ) ;
73+
74+ it . each ( [
75+ "yarn expo install @expo/ui" ,
76+ "npm install @jane/foo.js" ,
77+ "import '@scope/pkg/sub/path'" ,
78+ ] ) ( "leaves scoped package command %s to the plain-text paste fallback" , ( command ) => {
79+ vi . stubGlobal ( "ClipboardEvent" , TestClipboardEvent ) ;
80+ const editor = createEditor ( ) ;
81+ const plainTextFallback = vi . fn ( ( event : ClipboardEvent ) => {
82+ const selection = $getSelection ( ) ;
83+ if ( ! $isRangeSelection ( selection ) ) return false ;
84+ selection . insertText ( event . clipboardData ?. getData ( "text/plain" ) ?? "" ) ;
85+ return true ;
86+ } ) ;
87+
88+ editor . update (
89+ ( ) => {
90+ const paragraph = $createParagraphNode ( ) ;
91+ $getRoot ( ) . append ( paragraph ) ;
92+ paragraph . selectEnd ( ) ;
93+ } ,
94+ { discrete : true } ,
95+ ) ;
96+ registerComposerInlineTokenPaste ( editor , {
97+ createMentionNode : ( path ) => $createTextNode ( `<mention:${ path } >` ) ,
98+ getExpandedAbsoluteOffsetForPoint : ( ) => 0 ,
99+ } ) ;
100+ editor . registerCommand ( PASTE_COMMAND , plainTextFallback , COMMAND_PRIORITY_EDITOR ) ;
101+
102+ const event = new TestClipboardEvent ( command ) ;
103+ let handled = false ;
104+ editor . update (
105+ ( ) => {
106+ handled = editor . dispatchCommand ( PASTE_COMMAND , event as ClipboardEvent ) ;
107+ } ,
108+ { discrete : true } ,
109+ ) ;
110+
111+ expect ( handled ) . toBe ( true ) ;
112+ expect ( plainTextFallback ) . toHaveBeenCalledOnce ( ) ;
113+ expect ( editor . getEditorState ( ) . read ( ( ) => $getRoot ( ) . getTextContent ( ) ) ) . toBe ( command ) ;
114+ } ) ;
115+
116+ it ( "pastes a canonical scoped folder link as a mention" , ( ) => {
117+ vi . stubGlobal ( "ClipboardEvent" , TestClipboardEvent ) ;
118+ const editor = createEditor ( ) ;
119+ const mention = "[sub](@scope/pkg/sub)" ;
120+ const plainTextFallback = vi . fn ( ( ) => true ) ;
121+
122+ editor . update (
123+ ( ) => {
124+ const paragraph = $createParagraphNode ( ) ;
125+ $getRoot ( ) . append ( paragraph ) ;
126+ paragraph . selectEnd ( ) ;
127+ } ,
128+ { discrete : true } ,
129+ ) ;
130+ registerComposerInlineTokenPaste ( editor , {
131+ createMentionNode : ( path ) => $createTextNode ( `<mention:${ path } >` ) ,
132+ getExpandedAbsoluteOffsetForPoint : ( ) => 0 ,
133+ } ) ;
134+ editor . registerCommand ( PASTE_COMMAND , plainTextFallback , COMMAND_PRIORITY_EDITOR ) ;
135+
136+ const event = new TestClipboardEvent ( mention ) ;
137+ let handled = false ;
138+ editor . update (
139+ ( ) => {
140+ handled = editor . dispatchCommand ( PASTE_COMMAND , event as ClipboardEvent ) ;
141+ } ,
142+ { discrete : true } ,
143+ ) ;
144+
145+ expect ( handled ) . toBe ( true ) ;
146+ expect ( plainTextFallback ) . not . toHaveBeenCalled ( ) ;
147+ expect ( event . defaultPrevented ) . toBe ( true ) ;
148+ expect ( editor . getEditorState ( ) . read ( ( ) => $getRoot ( ) . getTextContent ( ) ) ) . toBe (
149+ "<mention:@scope/pkg/sub> " ,
150+ ) ;
151+ } ) ;
73152} ) ;
0 commit comments