-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
183 lines (139 loc) · 6.64 KB
/
main.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
Zepto(function($){
setTimeout( wrapTextareas, 500);
$.get(chrome.extension.getURL ("/template.html"), function(response){
$(document.body).append(response)
});
var lightTheme = 'sj-light';
var darkTheme = 'sj-dark';
var currentTheme = lightTheme; //default
var errors = [];
function setTheme(theme){
currentTheme = theme;
var overlay = $('.remodal-overlay');
var cm = $('#simplejson_body .CodeMirror');
if(currentTheme == lightTheme){
overlay.addClass(lightTheme);
overlay.removeClass(darkTheme);
cm.addClass('cm-s-'+lightTheme);
cm.removeClass('cm-s-'+darkTheme);
cm.addClass('cm-s-mdn-like');
cm.removeClass('cm-s-monokai');
$('#simplejson_theme_light').addClass('activetheme');
$('#simplejson_theme_dark').removeClass('activetheme');
}
if(currentTheme == darkTheme){
overlay.removeClass(lightTheme);
overlay.addClass(darkTheme);
cm.removeClass('cm-s-'+lightTheme);
cm.addClass('cm-s-'+darkTheme);
cm.removeClass('cm-s-mdn-like');
cm.addClass('cm-s-monokai');
$('#simplejson_theme_light').removeClass('activetheme');
$('#simplejson_theme_dark').addClass('activetheme');
}
}
function wrapTextareas(){
loadOptions();
$.each($('textarea'), function(index, item){
var text = $(item).val();
if(text != null && text.length > 0){
var m = text.charAt(0);
if(m == "{"){
// Create our new link
$(item).after('<a class="simplejson" href="#">JSON detected</a>');
// Enable modal editor on click
$(item).next('a.simplejson').on('click', function(event){
var options = {
"hashTracking": false
};
var inst = $('[data-remodal-id=modal]').remodal(options);
inst.open();
$('.remodal-overlay').addClass(currentTheme);
$('#simplejson_errors').hide();
$('#simplejson_textarea').val($(item).val());
var cm_theme = (currentTheme == lightTheme) ? 'mdn-like' : 'monokai';
var editor = CodeMirror.fromTextArea($('#simplejson_textarea')[0], {
lineNumbers: true,
matchBrackets: true,
autoCloseBrackets: true,
lineWrapping: true,
mode: "application/json",
foldGutter: true,
gutters: ["CodeMirror-lint-markers", "CodeMirror-foldgutter"],
theme: cm_theme + ' ' + currentTheme,
lint: {
getAnnotations: CodeMirror.lint.json,
onUpdateLinting: function(annotationsNotSorted, annotations, cm){
errors = annotationsNotSorted;
if(errors.length > 0) {
$('#simplejson_errors').show();
$('#simplejson_format').addClass('skdisable');
} else {
$('#simplejson_errors').hide();
$('#simplejson_format').removeClass('skdisable');
}
}
}
});
// Button events
$('#simplejson_saveandclose').on('click', function(){
$(item).val(editor.getValue());
inst.close();
});
$('#simplejson_cancel').on('click', function(){
inst.close();
});
$('#simplejson_format').on('click', function(){
if(errors.length == 0) {
editor.setValue(JSON.stringify(JSON.parse(editor.getValue()), null, 4));
}
});
$('#simplejson_theme_light').on('click', function(){
if(currentTheme != lightTheme) {
setTheme(lightTheme);
saveOption('theme', lightTheme);
}
return false;
});
$('#simplejson_theme_dark').on('click', function(){
if(currentTheme != darkTheme) {
setTheme(darkTheme);
saveOption('theme', darkTheme);
}
return false;
});
$('#simplejson_errors').on('click', function(){
if(errors.length > 0){
var ln = errors[0].from.line;
var ch = errors[0].from.ch;
editor.scrollIntoView(ln,ch);
} else {
//nothing
}
});
$(document).on('closed', '.remodal', function (e) {
editor.toTextArea();
$('.remodal-overlay').removeClass(lightTheme).removeClass(darkTheme);
});
return false; // don't follow the URL
});
}
}
});
};
function saveOption(key, val) {
var option = {};
option[key] = val;
chrome.storage.sync.set(option, function() {
// Do nothing
});
}
function loadOptions() {
// Use default value of lightTheme
chrome.storage.sync.get({
theme: lightTheme
}, function(items) {
setTheme(items.theme);
});
}
});