From dd56ae85d0a4bf5d3325f4e527ca13ffd9cb48d7 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Sat, 28 May 2022 02:33:27 +0100 Subject: [PATCH] feat: copy id or document to clipboard --- package.json | 26 ++++++++++++++++++++++++++ src/commands/index.ts | 2 ++ src/mdbExtensionController.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/package.json b/package.json index 087390bca..fb86283e6 100644 --- a/package.json +++ b/package.json @@ -314,6 +314,14 @@ "dark": "images/dark/search-regular.svg" } }, + { + "command": "mdb.copyDocumentIdFromTree", + "title": "Copy Document ID" + }, + { + "command": "mdb.copyDocumentFromTree", + "title": "Copy Document" + }, { "command": "mdb.openPlaygroundFromTreeItem", "title": "Open Playground" @@ -559,6 +567,16 @@ "when": "view == mongoDBConnectionExplorer && viewItem == documentListTreeItem", "group": "2@1" }, + { + "command": "mdb.copyDocumentIdFromTree", + "when": "view == mongoDBConnectionExplorer && viewItem == documentTreeItem", + "group": "1@1" + }, + { + "command": "mdb.copyDocumentFromTree", + "when": "view == mongoDBConnectionExplorer && viewItem == documentTreeItem", + "group": "1@2" + }, { "command": "mdb.refreshSchema", "when": "view == mongoDBConnectionExplorer && viewItem == schemaTreeItem" @@ -625,6 +643,14 @@ "command": "mdb.searchForDocuments", "when": "false" }, + { + "command": "mdb.copyDocumentIdFromTree", + "when": "false" + }, + { + "command": "mdb.copyDocumentFromTree", + "when": "false" + }, { "command": "mdb.addConnection", "when": "false" diff --git a/src/commands/index.ts b/src/commands/index.ts index 56090c3a8..3ee9f6741 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -45,6 +45,8 @@ enum EXTENSION_COMMANDS { MDB_RENAME_CONNECTION = 'mdb.renameConnection', MDB_ADD_DATABASE = 'mdb.addDatabase', MDB_SEARCH_FOR_DOCUMENTS = 'mdb.searchForDocuments', + MDB_COPY_DOCUMENT_ID_FROM_TREE = 'mdb.copyDocumentIdFromTree', + MDB_COPY_DOCUMENT_FROM_TREE = 'mdb.copyDocumentFromTree', MDB_COPY_DATABASE_NAME = 'mdb.copyDatabaseName', MDB_DROP_DATABASE = 'mdb.dropDatabase', MDB_REFRESH_DATABASE = 'mdb.refreshDatabase', diff --git a/src/mdbExtensionController.ts b/src/mdbExtensionController.ts index 4935be458..9c3b0abdc 100644 --- a/src/mdbExtensionController.ts +++ b/src/mdbExtensionController.ts @@ -487,6 +487,38 @@ export default class MDBExtensionController implements vscode.Disposable { return Promise.resolve(true); } ); + this.registerCommand( + EXTENSION_COMMANDS.MDB_COPY_DOCUMENT_ID_FROM_TREE, + async (element: DocumentTreeItem): Promise => { + const { documentId } = element; + + if (!documentId) { + void vscode.window.showWarningMessage('Can\'t copy ID from document that has no ID.'); + return false; + } + + let body = JSON.stringify(documentId); + + if (body.startsWith('"')) { + body = body.slice(1, body.length - 1); + } + + await vscode.env.clipboard.writeText(body); + return true; + } + ); + this.registerCommand( + EXTENSION_COMMANDS.MDB_COPY_DOCUMENT_FROM_TREE, + async (element: DocumentTreeItem): Promise => { + const { document } = element; + const tabSize = vscode.workspace + .getConfiguration('editor') + .get('tabSize', 2); + const body = JSON.stringify(document, undefined, tabSize); + await vscode.env.clipboard.writeText(body); + return true; + } + ); this.registerCommand( EXTENSION_COMMANDS.MDB_REFRESH_SCHEMA, (schemaTreeItem: SchemaTreeItem): Promise => {