1
- import type { EditorProps } from 'prosemirror-view ' ;
1
+ import dd from 'ts-dedent ' ;
2
2
3
- import { DataTransferType , isVSCode , tryParseVSCodeData } from '../../../utils/clipboard' ;
3
+ import { getLoggerFromState } from '#core' ;
4
+ import { Fragment } from '#pm/model' ;
5
+ import type { EditorProps } from '#pm/view' ;
6
+ import { DataTransferType , isVSCode , tryParseVSCodeData } from 'src/utils/clipboard' ;
4
7
5
8
import { CodeBlockNodeAttr } from './CodeBlockSpecs' ;
6
9
import { codeBlockType } from './const' ;
@@ -9,13 +12,26 @@ export const handlePaste: NonNullable<EditorProps['handlePaste']> = (view, e) =>
9
12
if ( ! e . clipboardData || view . state . selection . $from . parent . type . spec . code ) return false ;
10
13
const code = getCodeData ( e . clipboardData ) ;
11
14
if ( ! code ) return false ;
12
- let tr = view . state . tr ;
13
- const { schema} = tr . doc . type ;
14
- const codeBlockNode = codeBlockType ( schema ) . create (
15
- { [ CodeBlockNodeAttr . Lang ] : code . mode } ,
16
- schema . text ( code . value ) ,
17
- ) ;
18
- tr = tr . replaceSelectionWith ( codeBlockNode ) ;
15
+
16
+ getLoggerFromState ( view . state ) . event ( {
17
+ domEvent : 'paste' ,
18
+ event : 'paste-from-code-editor' ,
19
+ editor : code . editor ,
20
+ editorMode : code . mode ,
21
+ empty : ! code . value ,
22
+ dataTypes : e . clipboardData . types ,
23
+ } ) ;
24
+
25
+ const { tr, schema} = view . state ;
26
+ if ( code . value ) {
27
+ const codeBlockNode = codeBlockType ( schema ) . create (
28
+ { [ CodeBlockNodeAttr . Lang ] : code . mode } ,
29
+ schema . text ( code . value ) ,
30
+ ) ;
31
+ tr . replaceSelectionWith ( codeBlockNode ) ;
32
+ } else {
33
+ tr . replaceWith ( tr . selection . from , tr . selection . to , Fragment . empty ) ;
34
+ }
19
35
view . dispatch ( tr . scrollIntoView ( ) ) ;
20
36
return true ;
21
37
} ;
@@ -30,7 +46,7 @@ function getCodeData(data: DataTransfer): null | {editor: string; mode?: string;
30
46
mode = tryParseVSCodeData ( data ) ?. mode ;
31
47
} else return null ;
32
48
33
- return { editor, mode, value : data . getData ( DataTransferType . Text ) } ;
49
+ return { editor, mode, value : dd ( data . getData ( DataTransferType . Text ) ) } ;
34
50
}
35
51
return null ;
36
52
}
0 commit comments