mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 13:00:58 +00:00
refactor metadata into own fxn
This commit is contained in:
parent
880fda6ed9
commit
6f7189b7bb
|
@ -31,7 +31,7 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
|
|||
}
|
||||
|
||||
// get filename data from paste
|
||||
let pasteFilename = dataItem.asFile()?.name;
|
||||
const pasteFilename = dataItem.asFile()?.name;
|
||||
if (!pasteFilename) {
|
||||
return undefined;
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
// get notebook cell data
|
||||
// get notebook cell
|
||||
let notebookUri;
|
||||
let currentCell;
|
||||
for (const notebook of vscode.workspace.notebookDocuments) {
|
||||
|
@ -63,34 +63,12 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
|
|||
// create updated metadata for cell (prep for WorkspaceEdit)
|
||||
const b64string = encodeBase64(fileDataAsUint8);
|
||||
const startingAttachments = currentCell.metadata.custom?.attachments;
|
||||
let metadataNotebookEdit;
|
||||
if (!startingAttachments) {
|
||||
if (!currentCell.metadata.custom) {
|
||||
const initMetadata = { 'custom': { 'attachments': { [pasteFilename]: { 'image/png': b64string } } } };
|
||||
metadataNotebookEdit = vscode.NotebookEdit.updateCellMetadata(currentCell.index, initMetadata);
|
||||
} else {
|
||||
currentCell.metadata.custom['attachments'] = { [pasteFilename]: { 'image/png': b64string } };
|
||||
metadataNotebookEdit = vscode.NotebookEdit.updateCellMetadata(currentCell.index, currentCell.metadata);
|
||||
}
|
||||
|
||||
} else {
|
||||
for (let appendValue = 2; pasteFilename in startingAttachments; appendValue++) {
|
||||
const objEntries = Object.entries(startingAttachments[pasteFilename]);
|
||||
if (objEntries.length) { // check that mime:b64 are present
|
||||
const [, attachmentb64] = objEntries[0];
|
||||
if (attachmentb64 !== b64string) { // append a "-#" here. same name, diff data. this matches jupyter behavior
|
||||
pasteFilename = filename.concat(`-${appendValue}`) + filetype;
|
||||
}
|
||||
}
|
||||
}
|
||||
currentCell.metadata.custom.attachments[pasteFilename] = { 'image/png': b64string };
|
||||
metadataNotebookEdit = vscode.NotebookEdit.updateCellMetadata(currentCell.index, currentCell.metadata);
|
||||
}
|
||||
const newMetadata = buildMetadata(b64string, currentCell, pasteFilename, filetype, startingAttachments);
|
||||
|
||||
// build edits
|
||||
const nbEdit = vscode.NotebookEdit.updateCellMetadata(currentCell.index, newMetadata);
|
||||
const workspaceEdit = new vscode.WorkspaceEdit();
|
||||
if (metadataNotebookEdit) {
|
||||
workspaceEdit.set(notebookUri, [metadataNotebookEdit]);
|
||||
}
|
||||
workspaceEdit.set(notebookUri, [nbEdit]);
|
||||
|
||||
// create a snippet for paste
|
||||
const pasteSnippet = new vscode.SnippetString();
|
||||
|
@ -102,13 +80,6 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
|
|||
}
|
||||
}
|
||||
|
||||
export function imagePasteSetup() {
|
||||
const selector: vscode.DocumentSelector = { notebookType: 'jupyter-notebook', language: 'markdown' }; // this is correct provider
|
||||
return vscode.languages.registerDocumentPasteEditProvider(selector, new CopyPasteEditProvider(), {
|
||||
pasteMimeTypes: ['image/png'],
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Taken from https://github.com/microsoft/vscode/blob/743b016722db90df977feecde0a4b3b4f58c2a4c/src/vs/base/common/buffer.ts#L350-L387
|
||||
*/
|
||||
|
@ -149,3 +120,35 @@ function encodeBase64(buffer: Uint8Array, padded = true, urlSafe = false) {
|
|||
|
||||
return output;
|
||||
}
|
||||
|
||||
function buildMetadata(b64: string, cell: vscode.NotebookCell, filename: string, filetype: string, startingAttachments: any): { [key: string]: any } {
|
||||
const outputMetadata: { [key: string]: any } = cell.metadata;
|
||||
const customField = cell.metadata.custom;
|
||||
if (!customField) {
|
||||
return { 'custom': { 'attachments': { [filename]: { 'image/png': b64 } } } };
|
||||
}
|
||||
|
||||
const attachmentField = cell.metadata.custom.attachments;
|
||||
if (!attachmentField) {
|
||||
outputMetadata['attachments'] = { [filename]: { 'image/png': b64 } };
|
||||
} else {
|
||||
for (let appendValue = 2; filename in startingAttachments; appendValue++) {
|
||||
const objEntries = Object.entries(startingAttachments[filename]);
|
||||
if (objEntries.length) { // check that mime:b64 are present
|
||||
const [, attachmentb64] = objEntries[0];
|
||||
if (attachmentb64 !== b64) { // append a "-#" here. same name, diff data. this matches jupyter behavior
|
||||
filename = filename.concat(`-${appendValue}`) + filetype;
|
||||
}
|
||||
}
|
||||
}
|
||||
outputMetadata.custom.attachments[filename] = { 'image/png': b64 };
|
||||
}
|
||||
return outputMetadata;
|
||||
}
|
||||
|
||||
export function imagePasteSetup() {
|
||||
const selector: vscode.DocumentSelector = { notebookType: 'jupyter-notebook', language: 'markdown' }; // this is correct provider
|
||||
return vscode.languages.registerDocumentPasteEditProvider(selector, new CopyPasteEditProvider(), {
|
||||
pasteMimeTypes: ['image/png'],
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue