-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevmind-beautifier.js
93 lines (80 loc) · 2.27 KB
/
devmind-beautifier.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
"use babel";
import DevmindBeautifierView from "./devmind-beautifier-view";
import { CompositeDisposable } from "atom";
const qs = require("qs");
function request(ext, text, url) {
data = { extension: ext, content: text };
var http = new XMLHttpRequest();
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(qs.stringify(data));
changeState(http);
}
function changeState(http) {
http.onreadystatechange = function() {
if (http.readyState == 4 && http.status == 200) {
success(http);
}
if (http.status !== 200) {
fail();
}
};
}
function success(http) {
if (typeof http.responseText.status === "undefined") {
editor.setText(http.responseText);
atom.notifications.addSuccess("Now your code looks fancy");
panel.modalPanel.hide();
} else {
atom.notifications.addInfo("File extension not supported. Yet.");
}
}
function fail() {
panel.modalPanel.hide();
atom.notifications.addError(
"Something went wrong, please check your internet connection"
);
}
export default {
devmindBeautifierView: null,
modalPanel: null,
subscriptions: null,
activate(state) {
this.devmindBeautifierView = new DevmindBeautifierView(
state.devmindBeautifierViewState
);
this.modalPanel = atom.workspace.addModalPanel({
item: this.devmindBeautifierView.getElement(),
visible: false
});
// Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
this.subscriptions = new CompositeDisposable();
// Register command that toggles this view
this.subscriptions.add(
atom.commands.add("atom-workspace", {
"devmind-beautifier:toggle": () => this.toggle()
})
);
},
deactivate() {
this.modalPanel.destroy();
this.subscriptions.dispose();
this.devmindBeautifierView.destroy();
},
serialize() {
return {
devmindBeautifierViewState: this.devmindBeautifierView.serialize()
};
},
toggle() {
panel = this;
editor = atom.workspace.getActiveTextEditor();
text = editor.getText().trim();
fileName = editor.getTitle();
ext = fileName
.split(".")
.pop()
.trim();
request(ext, text, "https://beautifier.devmind.io/format");
}
};