/
editors.js
238 lines (195 loc) · 6.47 KB
/
editors.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//= require "codemirror"
//= require "mobileCodeMirror"
//= require "library"
//= require "unsaved"
//= require "panel"
var panels = {};
// evenly distribute the width of all the visible panels
Panel.prototype.distribute = function () {
var visible = [],
width = 100,
innerWidth = window.innerWidth,
left = 0,
right = 0;
for (var panel in panels.panels) {
if (panels.panels[panel].visible) visible.push(panels.panels[panel]);
}
if (visible.length) {
visible = visible.sort(function (a, b) {
return a.order < b.order ? -1 : 1;
});
console.log(visible);
width = 100 / visible.length;
for (var i = 0; i < visible.length; i++) {
right = 100 - (width * (i+1));
visible[i].$el.css({ left: left + '%', right: right + '%' });
visible[i].splitter.trigger('init', innerWidth * left/100);
console.log(visible[i].name, width, left, innerWidth * left/100)
left += width;
}
}
};
var editors = jsbin.panels = panels.panels = {
javascript: new Panel('javascript', { editor: true, nosplitter: true }),
css: new Panel('css', { editor: true }),
html: new Panel('html', { editor: true }),
console: new Panel('console'),
live: new Panel('live')
};
var editorsReady = setInterval(function () {
var ready = true;
for (var panel in panels.panels) {
if (!panels.panels[panel].ready) ready = false;
}
if (ready) {
clearInterval(editorsReady);
// jsbin.panels.ready = true;
// if (typeof editors.onReady == 'function') editors.onReady();
$(window).resize(function () {
setTimeout(function () {
$document.trigger('sizeeditors');
}, 100);
});
$document.trigger('sizeeditors');
$document.trigger('jsbinReady');
}
}, 100);
/** disabled - moving to reusable panels */
false && (function () {
var focusPanel = 'javascript';
var editors = {};
window.editors = editors;
editors.html = CodeMirror.fromTextArea(document.getElementById('html'), {
parserfile: [],
tabMode: 'shift',
mode: 'text/html',
onChange: changecontrol,
lineWrapping: true,
theme: jsbin.settings.theme || 'jsbin'
});
editors.javascript = CodeMirror.fromTextArea(document.getElementById('javascript'), {
mode: 'javascript',
tabMode: 'shift',
onChange: changecontrol,
lineWrapping: true,
theme: jsbin.settings.theme || 'jsbin'
});
setupEditor('javascript');
setupEditor('html');
var editorsReady = setInterval(function () {
if (editors.html.ready && editors.javascript.ready) {
clearInterval(editorsReady);
editors.ready = true;
if (typeof editors.onReady == 'function') editors.onReady();
var scrollers = {
html: $(editors.html.getScrollerElement()),
javascript: $(editors.javascript.getScrollerElement())
};
$document.bind('sizeeditors', function () {
var top = 0, //$el.offset().top,
height = $('#bin').height();
scrollers.html.height(height - top);
scrollers.javascript.height(height - top - $error.filter(':visible').height());
editors.javascript.refresh();
editors.html.refresh();
});
$(window).resize(function () {
setTimeout(function () {
$document.trigger('sizeeditors');
}, 100);
});
$document.trigger('sizeeditors');
$document.trigger('jsbinReady');
}
}, 100);
function focused(editor, event) {
focusPanel = editor.id;
}
function getFocusedPanel() {
return focusPanel;
}
function setupEditor(panel) {
var e = editors[panel],
focusedPanel = sessionStorage.getItem('panel');
// overhang from CodeMirror1
e.setCode = function (str) {
e.setValue(str);
};
e.getCode = function () {
return e.getValue();
};
e.currentLine = function () {
var pos = e.getCursor();
return pos.line;
};
e.setOption('onChange', changecontrol);
e.setOption('onKeyEvent', keycontrol);
e.setOption('onFocus', focused);
e.id = panel;
e.win = e.getWrapperElement();
e.scroller = $(e.getScrollerElement());
$(e.win).click(function () {
e.focus();
});
var $label = $('.code.' + panel + ' > .label');
if (document.body.className.indexOf('ie6') === -1) {
e.scroller.scroll(function (event) {
if (this.scrollTop > 10) {
$label.stop().animate({ opacity: 0 }, 50, function () {
$(this).hide();
});
} else {
$label.show().stop().animate({ opacity: 1 }, 250);
}
});
}
populateEditor(panel);
e.ready = true;
if (focusedPanel == panel || focusedPanel == null && panel == 'javascript') {
// e.selectLines(e.nthLine(sessionStorage.getItem('line')), sessionStorage.getItem('character'));
e.focus();
e.setCursor({ line: (sessionStorage.getItem('line') || 0) * 1, ch: (sessionStorage.getItem('character') || 0) * 1 });
}
}
function populateEditor(panel) {
// populate - should eventually use: session, saved data, local storage
var data = sessionStorage.getItem(panel), // session code
saved = localStorage.getItem('saved-' + panel), // user template
sessionURL = sessionStorage.getItem('url'),
changed = false;
if (data == template[panel]) { // restored from original saved
editors[panel].setCode(data);
} else if (data && sessionURL == template.url) { // try to restore the session first - only if it matches this url
editors[panel].setCode(data);
// tell the document that it's currently being edited, but check that it doesn't match the saved template
// because sessionStorage gets set on a reload
changed = data != saved;
} else if (saved !== null && !/edit/.test(window.location) && !window.location.search) { // then their saved preference
editors[panel].setCode(saved);
} else { // otherwise fall back on the JS Bin default
editors[panel].setCode(template[panel]);
}
if (changed) {
$(document).trigger('codeChange', [ /* revert triggered */ false, /* don't use fade */ true ]);
}
}
// work out the browser platform
var ua = navigator.userAgent;
if (/macintosh|mac os x/.test(ua)) {
$.browser.platform = 'mac';
} else if (/windows|win32/.test(ua)) {
$.browser.platform = 'win';
} else if (/linux/.test(ua)) {
$.browser.platform = 'linux';
} else {
$.browser.platform = '';
}
function changecontrol(event) {
// sends message to the document saying that a key has been pressed, we'll ignore the control keys
// if (! ({ 16:1, 17:1, 18:1, 20:1, 27:1, 37:1, 38:1, 39:1, 40:1, 91:1, 93:1 })[event.which] ) {
$(document).trigger('codeChange');
// }
return true;
}
}())
//= require "keycontrol"