-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
134 lines (121 loc) · 4.4 KB
/
extension.js
File metadata and controls
134 lines (121 loc) · 4.4 KB
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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode')
const loginHandler = require('./utils/loginHandler')
const snippets = require('./utils/snippets')
const config = require('./utils/config')
const languages = require('./utils/languages')
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
const handleSnippetCreation = async (editor, ctx) => {
const selection = editor.selection
const content = editor.document.getText(selection)
const syntax = editor.document.languageId === 'plaintext' ? 'plain' : editor.document.languageId
const filteredLanguages = languages.filter(l => l.value === syntax)
let topic = filteredLanguages.length ? filteredLanguages[0].name : syntax
let title = ''
if (syntax === 'plain') {
do {
topic = await vscode.window.showInputBox({
prompt: 'Topic',
placeHolder: 'This snippet is about...',
})
} while (typeof topic === 'string' && !topic.trim())
}
do {
title = await vscode.window.showInputBox({
prompt: 'Snippet Title (Required)',
placeHolder: 'I just learned how to...',
})
} while (typeof title === 'string' && !title.trim())
if (title) {
const source = await vscode.window.showInputBox({
prompt: 'Source (Optional)',
placeHolder: 'From',
})
try {
const accessToken = loginHandler.accessToken(ctx)
await snippets.create({ title, syntax, content, source, topic }, accessToken)
vscode.window
.showInformationMessage(
'Success! Your new snippet has been created!',
'Convert it to a flashcard',
)
.then(r => {
if (r === 'Convert it to a flashcard')
vscode.env.openExternal(vscode.Uri.parse(`${config.baseUrl}snippets/process`))
})
} catch (e) {
vscode.window.showErrorMessage('Oops. Looks like something went wrong. Please try again.')
}
}
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.commands.registerCommand('extension.createSnippet', async () => {
const editor = vscode.window.activeTextEditor
const selection = editor.selection
const content = editor.document.getText(selection)
if (!content) {
vscode.window.showErrorMessage('Please select the text that you want to create as a snippet')
return
}
let isLoggedIn = loginHandler.accessToken(context)
if (!isLoggedIn) {
const authToken = await vscode.window.showInputBox({
prompt: 'Please login to continue',
placeHolder: 'Paste your magic token here',
})
if (!authToken) {
vscode.window.showInformationMessage("Don't have a token?", 'Get your token').then(r => {
if (r === 'Get your token') vscode.env.openExternal(vscode.Uri.parse(`${config.baseUrl}?s=extension`))
})
return
}
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: 'Logging in...',
},
async (progress, token) => {
try {
await loginHandler.login(authToken, context)
progress.report({
increment: 100,
message: 'Logged in successfully!',
})
handleSnippetCreation(editor, context)
} catch (e) {
progress.report({
increment: 100,
message: 'Logging in failed. Please try again.',
})
vscode.window
.showInformationMessage("Logging in failed. Don't have a token?", 'Get your token')
.then(r => {
if (r === 'Get your token')
vscode.env.openExternal(vscode.Uri.parse(`${config.baseUrl}?s=extension`))
})
}
},
)
return
}
handleSnippetCreation(editor, context)
})
let logoutCommand = vscode.commands.registerCommand('extension.logout', async () => {
loginHandler.logout(context)
vscode.window.showInformationMessage("You've been logged out of mem.dev")
})
context.subscriptions.push(disposable)
context.subscriptions.push(logoutCommand)
}
exports.activate = activate
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate,
}