-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
74 lines (58 loc) · 1.73 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
const vscode = require('vscode');
const symbols = /[\r\n%#()<>?[\\\]^`{|}]/g;
let externalQuotesValue = "double";
let quotes = getQuotes();
function getResults(value){
const namespaced = addNameSpace(value);
const escaped = encodeSVG(namespaced);
const resultCss = `background-image: url(${quotes.level1}data:image/svg+xml,${escaped}${quotes.level1});`;
return resultCss;
}
function addNameSpace (data) {
if (data.indexOf(`http://www.w3.org/2000/svg`) < 0) {
data = data.replace(/<svg/g, `<svg xmlns=${quotes.level2}http://www.w3.org/2000/svg${quotes.level2}`);
}
return data;
}
function encodeSVG (data) {
if (externalQuotesValue === `double`) {
data = data.replace(/"/g, `'`);
} else {
data = data.replace(/'/g, `"`);
}
data = data.replace(/>\s{1,}</g, `><`);
data = data.replace(/\s{2,}/g, ` `);
return data.replace(symbols, encodeURIComponent);
}
function getQuotes () {
const double = `"`;
const single = `'`;
return {
level1: externalQuotesValue === `double` ? double : single,
level2: externalQuotesValue === `double` ? single : double
};
}
function activate(context) {
let disposable = vscode.commands.registerCommand('svg-to-background-image.plugins', function () {
const editor = vscode.window.activeTextEditor;
if(!editor){
return;
}
const text = editor.document.getText(editor.selection);
if(!text){
vscode.window.showInformationMessage("pls. select svg!");
return;
}
editor.edit((editBuilder) => {
editBuilder.replace(editor.selection, getResults(text));
}).then(success => {
vscode.window.showInformationMessage("Converted!");
})
});
context.subscriptions.push(disposable);
}
function deactivate() {}
module.exports = {
activate,
deactivate
}