-
Notifications
You must be signed in to change notification settings - Fork 7
/
extension.js
197 lines (154 loc) · 6.52 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//TODO notebook list & note list need pagination; or maybe search????
//TODO delete
//TODO see more about activation Commands
//TODO can not open new doc with title; maybe commented headers in doc ??
'use strict'
const util = require('util');
const vscode = require('vscode');
const open = require('open');
const adapter = require('./evernote-adapter.js');
const converter = require('./converter.js');
// const userSettingUri = vscode.Uri.parse('%APPDATA%\Code\User\settings.json'); URI malformed :(
const config = vscode.workspace.getConfiguration('evernote');
const createNotebookOption = "Create New Notebook....", createNoteOption = "Create New Note...."; // hope these not collide with your note & notebook names
const docToNoteMetas = {};
let showTips = config.showTips;
function showError(error) {
if (!error) {
return; //user dismiss
}
console.log(error);
let errMsg;
if (error.statusCode && error.statusMessage) {
errMsg = util.format("Http Error: %s - %s (Wrong noteStoreUrl??)", error.statusCode, error.statusMessage);
} else if (error.errorCode && error.parameter) {
errMsg = util.format("Evernote Error: %s - %s", error.errorCode, error.parameter);
} else {
errMsg = "Unexpected Error: " + error.toString();
}
vscode.window.showErrorMessage(errMsg);
}
function openDocWithContent(selectedNotebook, selectedMeta, content) {
console.log('note content:' + content);
return vscode.workspace.openTextDocument({language: 'markdown'}).then(doc => {
docToNoteMetas[doc] = selectedMeta;
return vscode.window.showTextDocument(doc);
}).then(editor => {
let startPos = new vscode.Position(1,0);
editor.edit(edit => {
let mdContent = converter.toMd(content);
let metaCommentContent = converter.metaToComment(selectedMeta, selectedNotebook.name);
edit.insert(startPos, metaCommentContent + mdContent);
});
});
}
function navToOneNote() {
if (!config.noteStoreUrl || !config.token) {
vscode.window.showInformationMessage('Check readme for details.')
vscode.window.showWarningMessage("Please set the token & API url first");
return;
}
let notebooks, selectedNotebook, noteMetas, selectedNoteMeta;
adapter.listNoteBooks().then(allNotebooks => {
notebooks = allNotebooks;
let allNoteBookNames = allNotebooks.map(notebook => notebook.name).concat(createNotebookOption);
return vscode.window.showQuickPick(allNoteBookNames);
}).then(selected => {
if (!selected) {
throw "";
}
if (selected === createNotebookOption) {
insertNewNotebook();
throw "";
}
selectedNotebook = notebooks.find(notebook => notebook.name === selected);
return adapter.listAllNoteMetas(selectedNotebook.guid);
}).then(metaList => {
noteMetas = metaList.notes;
let allNoteTitles = noteMetas.map(noteMeta => noteMeta.title).concat(createNoteOption);
return vscode.window.showQuickPick(allNoteTitles);
}).then(selected => {
if (!selected) {
throw "";
}
if (selected === createNoteOption) {
insertNewNote(selectedNotebook.guid);
throw "";
}
selectedNoteMeta = noteMetas.find(meta => meta.title === selected);
return adapter.getNoteContent(selectedNoteMeta.guid);
}).then(noteContent => {
return openDocWithContent(selectedNotebook, selectedNoteMeta, noteContent);
}).catch(showError);
vscode.window.setStatusBarMessage("Requesting notebooks .....", 2);
}
function updateNote() {
let activeDoc = vscode.window.activeTextEditor.document;
let meta = docToNoteMetas[activeDoc];
if (meta) {
let rawText = activeDoc.getText();
let convertedContent = converter.toEnml(rawText);
console.log('converted out:' + convertedContent);
adapter.updateNoteContent(meta.guid, meta.title, convertedContent).then(note => {
vscode.window.showInformationMessage('Note:' + note.title +' updated at: ' + new Date(note.updated));
}).catch(showError);
}
}
function insertNewNotebook() {
vscode.window.showInputBox({placeHolder: "Notebook Name"}).then(result => {
if (result) {
adapter.createNotebook(result).then(notebook => {
vscode.window.showInformationMessage('Notebook: ' + notebook.name + ' created at: ' + new Date(notebook.serviceCreated));
}).catch(showError);
};
});
}
function insertNewNote(notebookGuid) {
vscode.window.showInputBox({placeHolder: "Note Title"}).then(result => {
if (result) {
adapter.createNote(result, notebookGuid).then(note => {
vscode.window.showInformationMessage('Note:' + note.title +' created at: ' + new Date(note.updated));
//note includes notemeta
return openDocWithContent(note, note.content);
}).catch(showError);
}
});
}
function openDevPage() {
vscode.window.showQuickPick(["China", "Other"]).then(choice => {
if (!choice) {
return; // user dismiss
}
if (choice === "China") {
open("https://app.yinxiang.com/api/DeveloperToken.action");
} else {
open("https://www.evernote.com/api/DeveloperToken.action");
}
});
}
function alertToUpdate() {
if (!showTips) {
return;
}
let msg = "Saving to local won't sync the remote. Try Evernote: Update Note";
let option = "Ignore";
vscode.window.showWarningMessage(msg, option).then(result => {
if (result === option) {
showTips = false;
}
});
}
function activate(context) {
let navToOneNoteCmd = vscode.commands.registerCommand('extension.navToOneNote', navToOneNote);
let updateNoteCmd = vscode.commands.registerCommand('extension.updateNote', updateNote);
let openDevPageCmd = vscode.commands.registerCommand('extension.openDevPage', openDevPage);
context.subscriptions.push(navToOneNoteCmd);
context.subscriptions.push(updateNoteCmd);
context.subscriptions.push(openDevPageCmd);
// vscode.workspace.onWillSaveTextDocument(alertToUpdate);
vscode.workspace.onDidSaveTextDocument(alertToUpdate);
}
function deactivate() {
}
exports.activate = activate;
exports.deactivate = deactivate;