1- import type { EditorProps } from 'prosemirror-view ' ;
1+ import dd from 'ts-dedent ' ;
22
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' ;
47
58import { CodeBlockNodeAttr } from './CodeBlockSpecs' ;
69import { codeBlockType } from './const' ;
@@ -9,13 +12,26 @@ export const handlePaste: NonNullable<EditorProps['handlePaste']> = (view, e) =>
912 if ( ! e . clipboardData || view . state . selection . $from . parent . type . spec . code ) return false ;
1013 const code = getCodeData ( e . clipboardData ) ;
1114 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+ }
1935 view . dispatch ( tr . scrollIntoView ( ) ) ;
2036 return true ;
2137} ;
@@ -30,7 +46,7 @@ function getCodeData(data: DataTransfer): null | {editor: string; mode?: string;
3046 mode = tryParseVSCodeData ( data ) ?. mode ;
3147 } else return null ;
3248
33- return { editor, mode, value : data . getData ( DataTransferType . Text ) } ;
49+ return { editor, mode, value : dd ( data . getData ( DataTransferType . Text ) ) } ;
3450 }
3551 return null ;
3652}
0 commit comments