Skip to content

Commit cf25288

Browse files
author
Alexis Beingessner
committed
Ticket OscarGodson#132 - autogrow support
1 parent 916e9c1 commit cf25288

File tree

3 files changed

+165
-4
lines changed

3 files changed

+165
-4
lines changed

docs/js/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ $(function () {
3333
{ container: 'example-1'
3434
, file: { defaultContent: "#EpicEditor\nThis is some default content. Go ahead, _change me_. " }
3535
, focusOnLoad: true
36+
, autogrow: true
3637
}
3738
, editor = new EpicEditor(opts).load()
3839
, example = new EpicEditor()

epiceditor/js/epiceditor.js

+82-2
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,13 @@
356356
}
357357
, parser: typeof marked == 'function' ? marked : null
358358
, button: { fullscreen: true, preview: true }
359+
, autogrow: false
359360
}
360-
, defaultStorage;
361+
, defaultStorage
362+
, autogrowDefaults = { minHeight: 80
363+
, maxHeight: false
364+
, scroll: true
365+
};
361366

362367
self.settings = _mergeObjs(true, defaults, opts);
363368

@@ -372,6 +377,16 @@
372377
}
373378
}
374379

380+
if (self.settings.autogrow) {
381+
if (self.settings.autogrow === true) {
382+
self.settings.autogrow = autogrowDefaults;
383+
}
384+
else {
385+
self.settings.autogrow = _mergeObjs(true, autogrowDefaults, self.settings.autogrow);
386+
}
387+
self._oldHeight = -1;
388+
}
389+
375390
// If you put an absolute link as the path of any of the themes ignore the basePath
376391
// preview theme
377392
if (!self.settings.theme.preview.match(/^https?:\/\//)) {
@@ -499,7 +514,8 @@
499514
, isMod = false
500515
, isCtrl = false
501516
, eventableIframes
502-
, i; // i is reused for loops
517+
, i // i is reused for loops
518+
, boundAutogrow;
503519

504520
// Startup is a way to check if this EpicEditor is starting up. Useful for
505521
// checking and doing certain things before EpicEditor emits a load event.
@@ -1048,6 +1064,18 @@
10481064

10491065
self.iframe.close();
10501066
self._eeState.startup = false;
1067+
1068+
if (self.settings.autogrow) {
1069+
boundAutogrow = function () {
1070+
self._autogrow();
1071+
}
1072+
1073+
self.on("update", boundAutogrow);
1074+
self.on("edit", boundAutogrow);
1075+
self.on("preview", boundAutogrow);
1076+
boundAutogrow();
1077+
}
1078+
10511079
// The callback and call are the same thing, but different ways to access them
10521080
callback.call(this);
10531081
this.emit('load');
@@ -1694,6 +1722,58 @@
16941722
return self;
16951723
}
16961724

1725+
/**
1726+
* Handles autogrowing the editor
1727+
*/
1728+
EpicEditor.prototype._autogrow = function () {
1729+
var editorHeight
1730+
, newHeight
1731+
, minHeight
1732+
, maxHeight;
1733+
1734+
//autogrow in fullscreen in nonsensical
1735+
if (!this.is("fullscreen")) {
1736+
if (this.is("edit")) {
1737+
editorHeight = this.getElement('editor').documentElement.scrollHeight;
1738+
}
1739+
else {
1740+
editorHeight = this.getElement('previewer').documentElement.scrollHeight;
1741+
}
1742+
1743+
newHeight = editorHeight;
1744+
1745+
//handle minimum
1746+
minHeight = this.settings.autogrow.minHeight;
1747+
if (typeof minHeight === "function") {
1748+
minHeight = minHeight(this);
1749+
}
1750+
1751+
if (minHeight && newHeight < minHeight) {
1752+
newHeight = minHeight;
1753+
}
1754+
1755+
//handle maximum
1756+
maxHeight = this.settings.autogrow.maxHeight;
1757+
if (typeof maxHeight === "function") {
1758+
maxHeight = maxHeight(this);
1759+
}
1760+
1761+
if (maxHeight && newHeight > maxHeight) {
1762+
newHeight = maxHeight;
1763+
}
1764+
1765+
//actual resize
1766+
if (newHeight != this.oldHeight) {
1767+
this.getElement("container").style.height = newHeight + "px";
1768+
this.reflow();
1769+
if (this.settings.autogrow.scroll) {
1770+
window.scrollBy(0, newHeight - this.oldHeight);
1771+
}
1772+
this.oldHeight = newHeight;
1773+
}
1774+
}
1775+
}
1776+
16971777
EpicEditor.version = '0.2.1';
16981778

16991779
// Used to store information to be shared across editors

src/editor.js

+82-2
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,13 @@
356356
}
357357
, parser: typeof marked == 'function' ? marked : null
358358
, button: { fullscreen: true, preview: true }
359+
, autogrow: false
359360
}
360-
, defaultStorage;
361+
, defaultStorage
362+
, autogrowDefaults = { minHeight: 80
363+
, maxHeight: false
364+
, scroll: true
365+
};
361366

362367
self.settings = _mergeObjs(true, defaults, opts);
363368

@@ -372,6 +377,16 @@
372377
}
373378
}
374379

380+
if (self.settings.autogrow) {
381+
if (self.settings.autogrow === true) {
382+
self.settings.autogrow = autogrowDefaults;
383+
}
384+
else {
385+
self.settings.autogrow = _mergeObjs(true, autogrowDefaults, self.settings.autogrow);
386+
}
387+
self._oldHeight = -1;
388+
}
389+
375390
// If you put an absolute link as the path of any of the themes ignore the basePath
376391
// preview theme
377392
if (!self.settings.theme.preview.match(/^https?:\/\//)) {
@@ -499,7 +514,8 @@
499514
, isMod = false
500515
, isCtrl = false
501516
, eventableIframes
502-
, i; // i is reused for loops
517+
, i // i is reused for loops
518+
, boundAutogrow;
503519

504520
// Startup is a way to check if this EpicEditor is starting up. Useful for
505521
// checking and doing certain things before EpicEditor emits a load event.
@@ -1048,6 +1064,18 @@
10481064

10491065
self.iframe.close();
10501066
self._eeState.startup = false;
1067+
1068+
if (self.settings.autogrow) {
1069+
boundAutogrow = function () {
1070+
self._autogrow();
1071+
}
1072+
1073+
self.on("update", boundAutogrow);
1074+
self.on("edit", boundAutogrow);
1075+
self.on("preview", boundAutogrow);
1076+
boundAutogrow();
1077+
}
1078+
10511079
// The callback and call are the same thing, but different ways to access them
10521080
callback.call(this);
10531081
this.emit('load');
@@ -1694,6 +1722,58 @@
16941722
return self;
16951723
}
16961724

1725+
/**
1726+
* Handles autogrowing the editor
1727+
*/
1728+
EpicEditor.prototype._autogrow = function () {
1729+
var editorHeight
1730+
, newHeight
1731+
, minHeight
1732+
, maxHeight;
1733+
1734+
//autogrow in fullscreen in nonsensical
1735+
if (!this.is("fullscreen")) {
1736+
if (this.is("edit")) {
1737+
editorHeight = this.getElement('editor').documentElement.scrollHeight;
1738+
}
1739+
else {
1740+
editorHeight = this.getElement('previewer').documentElement.scrollHeight;
1741+
}
1742+
1743+
newHeight = editorHeight;
1744+
1745+
//handle minimum
1746+
minHeight = this.settings.autogrow.minHeight;
1747+
if (typeof minHeight === "function") {
1748+
minHeight = minHeight(this);
1749+
}
1750+
1751+
if (minHeight && newHeight < minHeight) {
1752+
newHeight = minHeight;
1753+
}
1754+
1755+
//handle maximum
1756+
maxHeight = this.settings.autogrow.maxHeight;
1757+
if (typeof maxHeight === "function") {
1758+
maxHeight = maxHeight(this);
1759+
}
1760+
1761+
if (maxHeight && newHeight > maxHeight) {
1762+
newHeight = maxHeight;
1763+
}
1764+
1765+
//actual resize
1766+
if (newHeight != this.oldHeight) {
1767+
this.getElement("container").style.height = newHeight + "px";
1768+
this.reflow();
1769+
if (this.settings.autogrow.scroll) {
1770+
window.scrollBy(0, newHeight - this.oldHeight);
1771+
}
1772+
this.oldHeight = newHeight;
1773+
}
1774+
}
1775+
}
1776+
16971777
EpicEditor.version = '@VERSION';
16981778

16991779
// Used to store information to be shared across editors

0 commit comments

Comments
 (0)