-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-to-javascript.js
More file actions
146 lines (114 loc) · 4.04 KB
/
Copy pathhtml-to-javascript.js
File metadata and controls
146 lines (114 loc) · 4.04 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
135
136
137
138
139
140
141
142
143
144
145
146
'use babel';
var vm;
vm = require('vm');
import HtmlToJavascriptView from './html-to-javascript-view';
import { CompositeDisposable } from 'atom';
export default {
htmlEditor: null,
paneOnWillDestroy: null,
htmlToJavascriptView: null,
modalPanel: null,
subscriptions: null,
activate(state) {
this.htmlToJavascriptView = new HtmlToJavascriptView(state.htmlToJavascriptViewState);
this.modalPanel = atom.workspace.addModalPanel({
item: this.htmlToJavascriptView.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', {
'html-to-javascript:convert': () => this.convert(),
'html-to-javascript:js-to-html': () => this.go_edit(),
}));
},
deactivate() {
this.modalPanel.destroy();
this.subscriptions.dispose();
this.htmlToJavascriptView.destroy();
// this.paneOnWillDestroy.dispose();
},
serialize() {
return {
htmlToJavascriptViewState: this.htmlToJavascriptView.serialize()
};
},
go_edit(){
console.log('opening');
if (this.htmlView) {
// Already open, close and reopen
// console.log('already open');
// this.htmlView.close();
}
var editor = atom.workspace.getActiveTextEditor();
var selection = editor.getSelectedText();
//this.htmlView = atom.workspace.open('html-to-javascript://'+encodeURI('temp-preview-window')+'.html', {split:'right',searchAllPanes: true});
var p1 = atom.workspace.open('html-to-javascript://'+encodeURI('temp-preview-window')+'.html', {split:'right',searchAllPanes: true});
p1.then(function(val) {
// Editor = returned from promise
// console.log(val);
// this.htmlEditor = val;
// Our selected javascript-string will now be moved to another window for editor as html.
// If eval of string results in a string then it is assumed to be valid HTML, pass this to the view, remove the start and end characters, beautify (if package available) and let the user edit it.
// Replace '"' with '\"'
selection = selection.replace(/"/g , "\\\"").replace(/\t/g, "").replace(/\r\n/g,"");
// console.log(selection);
try {
var input = vm.runInThisContext('"' + selection + '"');
} catch(err) {
// console.log(err.message);
}
// console.log(input);
input = input.replace(/\'\+\'/g, "");
val.insertText(input.substring(1,input.length-1));
// If atom beautify is installed...
atom.commands.dispatch(atom.views.getView(editor), 'atom-beautify:beautify-editor');
}).catch(function(reason) {
// console.log('Handle rejected promise ('+reason+') here.');
});
},
convert(text) {
// this.go_edit();
var editor = atom.workspace.getActiveTextEditor();
if (editor){
var selection = (text)?text:editor.getSelectedText();
if (!!!selection.trim()) return;
// Replace all ' in file with \'
selection = selection.replace(/'/g , "\\'");
// Edit selection:
var lines = selection.split("\n");
var js_string = "''+\n";
// Wrap beginning and end of lines with '
// (for the beginning of a line, put it at the beginning of the first non space)
// (for the end of a line, trim the end putting '+ at the end of the line)
try {
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
num_leading_whitespace = line.search(/\S|$/);
var leading_indent = '';
if (num_leading_whitespace){
console.log(num_leading_whitespace);
for (var j = 0; j < num_leading_whitespace; j++) {
leading_indent += "\t";
}
// for (var i = 0; i < num_leading_whitespace; i++) {
// leading_indent += "\t";
// }
}
js_string += leading_indent + "'" + line.trim() + "'+\n";
}
}
catch(err) {
console.log(err.message);
}
if (text) {
return js_string.substring(0,js_string.length-2);
} else {
editor.insertText(js_string.substring(0,js_string.length-2), {
select: true
}); //# Ignores the last 2 symbols "+\n"
}
}
}
};