Skip to content

Commit 67730ef

Browse files
committed
Merge pull request #14 from PoliteJS/ISSUE-12_ace-editor-autosize
Issue 12 ace editor autosize
2 parents 885b7d9 + 2772f0f commit 67730ef

File tree

6 files changed

+115
-8
lines changed

6 files changed

+115
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsbox",
3-
"version": "3.2.1",
3+
"version": "3.2.2",
44
"description": "jquery playground plugin for javascript",
55
"main": "Gruntfile.js",
66
"directories": {

src/autosize.html

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!doctype html>
2+
<html lang="en-US">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6+
7+
<title>JSBox - editor auto size</title>
8+
9+
<style>
10+
body {margin: 2% 10%}
11+
.hint {border: 2px solid #218ace; margin: 20px 0;padding:0 20px 20px 20px;}
12+
.hint p:last-child {margin-bottom:0}
13+
.code {background:#eee;color#eee;padding: 10px 20px;border-radius:5px}
14+
div[data-jsbox] {position:relative;border:3px dashed #ddd;padding:20px;border-radius:10px;overflow: hidden}
15+
div[data-jsbox]:after {content:"Loading JSBox...";position: absolute;top:0;left:0;display:inline-block;width:100%;height:100%;padding-top: 20pt;background: rgba(255,255,255,.9);text-align: center;font-size: 20pt;color:#bbb;}
16+
</style>
17+
18+
</head>
19+
<body>
20+
21+
<h1>JSBox Auto Size</h1>
22+
23+
<div class="hint">
24+
<pre class="code">
25+
&lt;div data-jsbox data-autosize>
26+
&lt;div data-jsbox data-autosize minHeight=80 maxHeight=150>
27+
</pre>
28+
</div>
29+
30+
<h2>Try it you self:</h2>
31+
32+
<!-- JSBox -->
33+
<div data-jsbox data-autosize>
34+
<pre data-js></pre>
35+
<pre data-html></pre>
36+
<pre data-css></pre>
37+
</div>
38+
<!-- // JSBox -->
39+
40+
41+
<h2>More Info Online!</h2>
42+
43+
<p>
44+
<a href="http://politejs.com/jsbox">http://PoliteJS/jsbox</a>
45+
</p>
46+
47+
48+
49+
50+
<!-- Dependencies -->
51+
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
52+
<script src="//cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
53+
54+
<!-- JSBox -->
55+
<link rel="stylesheet" href="jsbox/jquery.jsbox.css">
56+
<script src="jsbox/jquery.jsbox.js"></script>
57+
58+
59+
60+
</body>
61+
</html>

src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ <h2>Available Examples</h2>
5555
<li><a href="async.html">Handle asynchronous code</a></li>
5656
<li><a href="code-errors.html">Code errors handling</a></li>
5757
<li><a href="chai.html">Use ChaiJS Assertions</a></li>
58+
<li><a href="autosize.html">Editor auto resize</a></li>
5859
</ul>
5960

6061
<hr>

src/js/editor-ace.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ var aceEditorEngine = {
2525
var AceEditorDefaults = {
2626
language: 'txt',
2727
source: '',
28-
sourceDelay: 0,
29-
autosizeDelay: 0
28+
29+
autosize: false,
30+
minHeight: false,
31+
maxHeight: false
3032
};
3133

3234
var AceEditor = {
@@ -72,10 +74,19 @@ var aceEditorEngine = {
7274
this.ace.on('focus', function() {
7375
publish(self, 'focus');
7476
});
77+
7578
this.ace.on('blur', function() {
7679
publish(self, 'blur');
7780
});
7881

82+
if (this.options.autosize === true) {
83+
setTimeout(function() {
84+
self.ace.on('change', editorAutosize.bind(self));
85+
self.ace.resize(true);
86+
editorAutosize.call(self);
87+
}, 0);
88+
}
89+
7990
this.setSource(this.options.source, true);
8091

8192
},
@@ -104,6 +115,18 @@ var aceEditorEngine = {
104115
};
105116

106117

118+
function editorAutosize() {
119+
var height = this.ace.getSession().getScreenLength() * this.ace.renderer.lineHeight + this.ace.renderer.scrollBar.getWidth();
120+
if (this.options.minHeight !== false && height < this.options.minHeight) {
121+
height = this.options.minHeight;
122+
}
123+
if (this.options.maxHeight !== false && height > this.options.maxHeight) {
124+
height = this.options.maxHeight;
125+
}
126+
this.el.style.height = height.toString() + 'px';
127+
this.ace.resize();
128+
}
129+
107130

108131
// Factory Method
109132
aceEditorEngine.create = function(options) {

src/js/jsbox.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,25 @@ function disposeSandbox(box) {
190190
// ------------------------------------- //
191191

192192
function initEditors(box) {
193-
Object.keys(box.options.editors).forEach(function(editorName) {
193+
var supportedEditors = ['js','css','html'];
194+
Object.keys(box.options.editors).filter(function(editorName) {
195+
return supportedEditors.indexOf(editorName) !== -1;
196+
}).forEach(function(editorName) {
194197
if (box.options.editors[editorName] === false) {
195198
return;
196199
}
197200
var source = '';
198201
if (typeof box.options.editors[editorName] === 'string') {
199202
source = box.options.editors[editorName];
200203
}
201-
box.editors[editorName] = box.options.engines.editor.create({
204+
var options = {
202205
language: editorName,
203-
source: source
204-
});
206+
source: source,
207+
autosize: box.options.editors.autosize,
208+
minHeight: box.options.editors.minHeight,
209+
maxHeight: box.options.editors.maxHeight
210+
};
211+
box.editors[editorName] = box.options.engines.editor.create(options);
205212
box.editors[editorName].on('cmd-execute', box.execute.bind(box));
206213
box.editors[editorName].on('cmd-reset', box.reset.bind(box));
207214
});

src/js/plugin.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,22 @@
167167
}
168168
}
169169

170-
// console.log(config);
170+
// autosize
171+
172+
if ($el.is('[data-autosize]')) {
173+
config.editors.autosize = true;
174+
config.editors.minHeight = 50;
175+
}
176+
177+
if ($el.is('[data-minHeight]')) {
178+
config.editors.minHeight = $el.attr('data-minHeight');
179+
}
180+
181+
if ($el.is('[data-maxHeight]')) {
182+
config.editors.maxHeight = $el.attr('data-maxHeight');
183+
}
184+
185+
//console.log(config);
171186
return config;
172187
}
173188

0 commit comments

Comments
 (0)