-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
198 lines (154 loc) · 5 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
198
// 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');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "css-units" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('css-units.helloWorld', function () {
// The code you place here will be executed every time your command is executed
const editor = vscode.window.activeTextEditor;
if(!editor) {
vscode.window.showInformationMessage('Please select a CSS Unit!');
return;
}
const text = editor.document.getText(editor.selection);
if (containsUnits(text)) {
// vscode.window.showInformationMessage(`Selected Text: ${text}`);
const quickPick = vscode.window.createQuickPick();
let responseOne = getRelativeResponse(text);
let responseTwo = getAbsoluteResponse(text);
responseOne = Object.keys(responseOne).map(key => responseOne[key]);
responseTwo = Object.keys(responseTwo).map(key => responseTwo[key]);
quickPick.items = [...responseOne, ...responseTwo];
quickPick.items = quickPick.items.map((x) => (
x.indexOf('absolute') === 0
? {label: x.substring(x.indexOf(' ') + 1), detail: "Absolute Unit"}
: {label: x, detail: "Relative Unit"}
));
quickPick.onDidChangeSelection((item) => {
if(item) {
editor.edit((edit) => {
edit.replace(editor.selection, item[0].label);
})
quickPick.dispose();
}
})
quickPick.onDidHide(() => quickPick.dispose());
quickPick.show();
}
else {
vscode.window.showInformationMessage('Selected input does not contain px, em, rem, ex or vmin unit!');
}
});
context.subscriptions.push(disposable);
}
function containsUnits (text) {
return text.indexOf('px') > -1 || text.indexOf('em') > -1 || text.indexOf('rem') > -1 || text.indexOf('ex') > -1 || text.indexOf('vmin') > -1;
}
function getRelativeResponse (value) {
let units;
if (value.indexOf('px') > -1) {
let number = parseFloat(value.substring(0, value.indexOf('px')));
units = convertFromPixel(number);
}
else {
let number;
if (value.indexOf('em') > -1) {
number = value.substring(0, value.indexOf('em'));
}
else if (value.indexOf('rem') > -1) {
number = value.substring(0, value.indexOf('rem'));
}
else if (value.indexOf('ex') > -1) {
number = value.substring(0, value.indexOf('ex'));
}
else if (value.indexOf('vmin') > -1) {
number = value.substring(0, value.indexOf('vmin'));
}
number = parseFloat(number);
units = convertFromRemOrEm(number);
}
return units;
}
function getAbsoluteResponse (value) {
let units;
if (value.indexOf('px') > -1) {
let number = parseFloat(value.substring(0, value.indexOf('px')));
units = convertFromPixelToAbsolute(number);
}
else {
let number;
if (value.indexOf('em') > -1) {
number = value.substring(0, value.indexOf('em'));
}
else if (value.indexOf('rem') > -1) {
number = value.substring(0, value.indexOf('rem'));
}
else if (value.indexOf('ex') > -1) {
number = value.substring(0, value.indexOf('ex'));
}
else if (value.indexOf('vmin') > -1) {
number = value.substring(0, value.indexOf('vmin'));
}
number = parseFloat(number);
units = convertFromEmToAbsolute(number);
}
return units;
}
function convertFromPixel(pixels) {
let units = {
em: pixels / 16 + " em",
ex: pixels / 16 + " ex",
ch: (pixels / 2.54).toFixed(3) + " ch",
rem: pixels / 16 + " rem",
vh: pixels / 100 + " vh",
vmin: pixels / 16 + " vmin",
vmax: pixels / 2 + " vmax",
percent: pixels / 100 + " %"
}
return units;
}
function convertFromRemOrEm(em) {
let units = {
px: "absolute " + em * 16 + " px",
em: em + " em",
ex: em + " ex",
ch: (em * 16) / 2.54 + " ch",
rem: em + " rem",
vh: (em * 16) / 100 + " vh",
vmin: em + " vmin",
vmax: (em * 16) / 2 + " vmax",
percent: (em * 16) / 100 + " %"
}
return units;
}
function convertFromPixelToAbsolute (pixel) {
let units = {
cm: "absolute " + pixel * 0.02 + " cm",
mm: "absolute " + pixel * 0.26 + " mm",
in: "absolute " + pixel * 2.54 + " in",
pt: "absolute " + pixel * 0.03 + " pt",
pc: "absolute " + pixel * 0.35 + " pc",
}
return units;
}
function convertFromEmToAbsolute (em) {
let pixel = em * 16;
let units = convertFromPixelToAbsolute(pixel);
return units;
}
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
}