@@ -31,13 +31,13 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
31
31
}
32
32
33
33
// get filename data from paste
34
- const pasteFilename = dataItem . asFile ( ) ?. name ;
35
- if ( ! pasteFilename ) {
34
+ const clipboardFilename = dataItem . asFile ( ) ?. name ;
35
+ if ( ! clipboardFilename ) {
36
36
return undefined ;
37
37
}
38
- const separatorIndex = pasteFilename ?. lastIndexOf ( '.' ) ;
39
- const filename = pasteFilename ?. slice ( 0 , separatorIndex ) ;
40
- const filetype = pasteFilename ?. slice ( separatorIndex ) ;
38
+ const separatorIndex = clipboardFilename ?. lastIndexOf ( '.' ) ;
39
+ const filename = clipboardFilename ?. slice ( 0 , separatorIndex ) ;
40
+ const filetype = clipboardFilename ?. slice ( separatorIndex ) ;
41
41
if ( ! filename || ! filetype ) {
42
42
return undefined ;
43
43
}
@@ -46,13 +46,12 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
46
46
if ( ! currentCell ) {
47
47
return undefined ;
48
48
}
49
-
50
49
const notebookUri = currentCell . notebook . uri ;
51
50
52
51
// create updated metadata for cell (prep for WorkspaceEdit)
53
52
const b64string = encodeBase64 ( fileDataAsUint8 ) ;
54
53
const startingAttachments = currentCell . metadata . custom ?. attachments ;
55
- const newMetadata = buildMetadata ( b64string , currentCell , pasteFilename , filetype , startingAttachments ) ;
54
+ const [ newMetadata , revisedFilename ] = buildMetadata ( b64string , currentCell , filename , filetype , startingAttachments ) ;
56
55
57
56
// build edits
58
57
const nbEdit = vscode . NotebookEdit . updateCellMetadata ( currentCell . index , newMetadata ) ;
@@ -62,8 +61,8 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
62
61
// create a snippet for paste
63
62
const pasteSnippet = new vscode . SnippetString ( ) ;
64
63
pasteSnippet . appendText ( '` ) ;
64
+ pasteSnippet . appendPlaceholder ( `${ revisedFilename } ` ) ;
65
+ pasteSnippet . appendText ( `](attachment:${ revisedFilename } )` ) ;
67
66
68
67
return { insertText : pasteSnippet , additionalEdit : workspaceEdit } ;
69
68
}
@@ -123,27 +122,27 @@ function encodeBase64(buffer: Uint8Array, padded = true, urlSafe = false) {
123
122
return output ;
124
123
}
125
124
126
- function buildMetadata ( b64 : string , cell : vscode . NotebookCell , filename : string , filetype : string , startingAttachments : any ) : { [ key : string ] : any } {
125
+ function buildMetadata ( b64 : string , cell : vscode . NotebookCell , filename : string , filetype : string , startingAttachments : any ) : [ { [ key : string ] : any } , string ] {
127
126
const outputMetadata = { ...cell . metadata } ;
127
+ let tempFilename = filename + filetype ;
128
128
129
129
if ( ! outputMetadata . custom ) {
130
- outputMetadata [ 'custom' ] = { 'attachments' : { [ filename ] : { 'image/png' : b64 } } } ;
130
+ outputMetadata [ 'custom' ] = { 'attachments' : { [ tempFilename ] : { 'image/png' : b64 } } } ;
131
131
} else if ( ! outputMetadata . custom . attachments ) {
132
- outputMetadata . custom [ 'attachments' ] = { [ filename ] : { 'image/png' : b64 } } ;
132
+ outputMetadata . custom [ 'attachments' ] = { [ tempFilename ] : { 'image/png' : b64 } } ;
133
133
} else {
134
- for ( let appendValue = 2 ; filename in startingAttachments ; appendValue ++ ) {
135
- const objEntries = Object . entries ( startingAttachments [ filename ] ) ;
134
+ for ( let appendValue = 2 ; tempFilename in startingAttachments ; appendValue ++ ) {
135
+ const objEntries = Object . entries ( startingAttachments [ tempFilename ] ) ;
136
136
if ( objEntries . length ) { // check that mime:b64 are present
137
137
const [ , attachmentb64 ] = objEntries [ 0 ] ;
138
138
if ( attachmentb64 !== b64 ) { // append a "-#" here. same name, diff data. this matches jupyter behavior
139
- filename = filename . concat ( `-${ appendValue } ` ) + filetype ;
139
+ tempFilename = filename . concat ( `-${ appendValue } ` ) + filetype ;
140
140
}
141
141
}
142
142
}
143
- outputMetadata . custom . attachments [ filename ] = { 'image/png' : b64 } ;
143
+ outputMetadata . custom . attachments [ tempFilename ] = { 'image/png' : b64 } ;
144
144
}
145
-
146
- return outputMetadata ;
145
+ return [ outputMetadata , tempFilename ] ;
147
146
}
148
147
149
148
export function imagePasteSetup ( ) {
0 commit comments