-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
54 lines (49 loc) · 1.57 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
const vscode = require('vscode');
const axios = require('axios').default;
const ncp = require('copy-paste');
const kebabCase = require('lodash/kebabCase');
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.commands.registerCommand(
'extension.getColorName',
function() {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('editor does not exist');
return;
}
const selectedText = editor.document.getText(editor.selection); // EXAMPLE: #112233
if (selectedText[0] === '#') {
axios
.get(`https://www.thecolorapi.com/id?hex=${selectedText.substr(1)}`)
.then(({ data }) => {
if (data.name.value) {
const colorName = kebabCase(data.name.value);
ncp.copy(colorName, () => {
vscode.window.showInformationMessage(
`Color Name has been copied to your clipboard 🎉`
);
});
}
})
.catch(error => {
console.log(error);
vscode.window.showInformationMessage(
`An error occured while getting the name of the color, make sure that you're selecting a color HEX value and try again.`
);
});
} else {
vscode.window.showInformationMessage(
`Select the HEX color with the # symbol`
);
}
}
);
context.subscriptions.push(disposable);
}
exports.activate = activate;
module.exports = {
activate
};