Skip to content

Commit

Permalink
add comman unattach. #10
Browse files Browse the repository at this point in the history
  • Loading branch information
michalyao committed Apr 10, 2017
1 parent ccecd3e commit 500700b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
{
"command": "extension.openNoteInBrowser",
"title": "ever browse"
},
{
"command": "extension.removeAttachment",
"title": "ever unattach"
}
],
"configuration": {
Expand Down
24 changes: 21 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ notebook: %s
---
`

// notesMap -- notebook - notes.
// notesMap -- [notebookguid:[notes]].
let notebooks, notesMap, selectedNotebook;
const localNote = {};
let showTips;
Expand Down Expand Up @@ -150,12 +150,29 @@ async function attachToNote() {
const cache = {};
cache[filepath] = attachment;
attachmentsCache[doc.fileName].push(cache);
vscode.window.setStatusBarMessage("Add attachment to current file succeeded", 2000);
vscode.window.showInformationMessage(util.format("%s has been attched to current note.", fileName));
} catch (err) {
wrapError(err);
}
}

// remove a local attachment.
async function removeAttachment() {
const editor = await vscode.window.activeTextEditor;
let doc = editor.document;
// Can only remove an attachment from a cache file
if (attachmentsCache[doc.fileName]) {
let localAttachments = attachmentsCache[doc.fileName].map(cache => _.values(cache)[0]);
const selectedAttachment = await vscode.window.showQuickPick(localAttachments.map(attachment => attachment.attributes.fileName));
if (!selectedAttachment) {
throw "";
}
let attachmentToRemove = localAttachments.find(attachment => attachment.attributes.fileName === selectedAttachment);
_.remove(attachmentsCache[doc.fileName], cache => _.values(cache)[0].attributes.fileName === selectedAttachment);
vscode.window.showInformationMessage(util.format("%s has been removed from current note.", selectedAttachment));
}
}

// list current file attachment.
async function listResources() {
try {
Expand Down Expand Up @@ -614,7 +631,7 @@ function activate(context) {
let attachToNoteCmd = vscode.commands.registerCommand("extension.attachToNote", attachToNote);
let listResourcesCmd = vscode.commands.registerCommand("extension.listResouces", listResources);
let openNoteInBrowserCmd = vscode.commands.registerCommand("extension.openNoteInBrowser", openNoteInBrowser);

let removeAttachmentCmd = vscode.commands.registerCommand("extension.removeAttachment", removeAttachment);

context.subscriptions.push(listAllNotebooksCmd);
context.subscriptions.push(publishNoteCmd);
Expand All @@ -627,6 +644,7 @@ function activate(context) {
context.subscriptions.push(attachToNoteCmd);
context.subscriptions.push(listResourcesCmd);
context.subscriptions.push(openNoteInBrowserCmd);
context.subscriptions.push(removeAttachmentCmd);

}
exports.activate = activate;
Expand Down

0 comments on commit 500700b

Please sign in to comment.