Skip to content

Commit 6381435

Browse files
committed
Ticket #161 - Remember the edit or preview state on unloading so when you load again its where you left it. Also added a guard for calling load when its already loaded to save on memory and changed all _eeState checks internally to use is() instead
1 parent 0fa7d4b commit 6381435

File tree

4 files changed

+65
-34
lines changed

4 files changed

+65
-34
lines changed

epiceditor/js/epiceditor.js

+32-16
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@
420420
}
421421

422422
// It opens edit mode by default (for now);
423-
self._eeState.edit = true;
423+
if (!self.is('edit') && !self.is('preview')) {
424+
self._eeState.edit = true;
425+
}
424426

425427
callback = callback || function () {};
426428

@@ -547,7 +549,7 @@
547549
_elementStates = {}
548550
self._goFullscreen = function (el) {
549551

550-
if (self._eeState.fullscreen) {
552+
if (self.is('fullscreen')) {
551553
self._exitFullscreen(el);
552554
return;
553555
}
@@ -556,7 +558,7 @@
556558
el.webkitRequestFullScreen();
557559
}
558560

559-
_isInEdit = self._eeState.edit;
561+
_isInEdit = self.is('edit');
560562

561563
// Set the state of EE in fullscreen
562564
// We set edit and preview to true also because they're visible
@@ -674,7 +676,7 @@
674676
window.clearTimeout(keypressTimer);
675677
}
676678
keypressTimer = window.setTimeout(function () {
677-
if (self._eeState.fullscreen) {
679+
if (self.is('fullscreen')) {
678680
self.preview();
679681
}
680682
}, 250);
@@ -743,9 +745,9 @@
743745
if (e.keyCode == 17) { isCtrl = true } // check for ctrl/cmnd press, in order to catch ctrl/cmnd + s
744746

745747
// Check for alt+p and make sure were not in fullscreen - default shortcut to switch to preview
746-
if (isMod === true && e.keyCode == self.settings.shortcut.preview && !self._eeState.fullscreen) {
748+
if (isMod === true && e.keyCode == self.settings.shortcut.preview && !self.is('fullscreen')) {
747749
e.preventDefault();
748-
if (self._eeState.edit) {
750+
if (self.is('edit')) {
749751
self.preview();
750752
}
751753
else {
@@ -765,7 +767,7 @@
765767
}
766768

767769
// When a user presses "esc", revert everything!
768-
if (e.keyCode == 27 && self._eeState.fullscreen) {
770+
if (e.keyCode == 27 && self.is('fullscreen')) {
769771
self._exitFullscreen(fsElement);
770772
}
771773

@@ -820,7 +822,7 @@
820822
window.addEventListener('resize', function () {
821823
// If NOT webkit, and in fullscreen, we need to account for browser resizing
822824
// we don't care about webkit because you can't resize in webkit's fullscreen
823-
if (!self.iframe.webkitRequestFullScreen && self._eeState.fullscreen) {
825+
if (!self.iframe.webkitRequestFullScreen && self.is('fullscreen')) {
824826
_applyStyles(self.iframeElement, {
825827
'width': window.outerWidth + 'px'
826828
, 'height': window.innerHeight + 'px'
@@ -841,14 +843,23 @@
841843
});
842844
}
843845
// Makes the editor support fluid width when not in fullscreen mode
844-
else if (!self._eeState.fullscreen) {
846+
else if (!self.is('fullscreen')) {
845847
resetWidth(elementsToResize);
846848
}
847849
});
848850

849-
self.iframe.close();
851+
// Set states before flipping edit and preview modes
850852
self._eeState.loaded = true;
851853
self._eeState.unloaded = false;
854+
855+
if (self.is('preview')) {
856+
self.preview();
857+
}
858+
else {
859+
self.edit();
860+
}
861+
862+
self.iframe.close();
852863
// The callback and call are the same thing, but different ways to access them
853864
callback.call(this);
854865
this.emit('load');
@@ -862,7 +873,7 @@
862873
EpicEditor.prototype.unload = function (callback) {
863874

864875
// Make sure the editor isn't already unloaded.
865-
if (this._eeState.unloaded) {
876+
if (this.is('unloaded')) {
866877
throw new Error('Editor isn\'t loaded');
867878
}
868879

@@ -907,7 +918,7 @@
907918
self.previewer.innerHTML = self.exportFile(null, 'html');
908919

909920
// Hide the editor and display the previewer
910-
if (!self._eeState.fullscreen) {
921+
if (!self.is('fullscreen')) {
911922
self.editorIframe.style.display = 'none';
912923
self.previewerIframe.style.display = 'block';
913924
self._eeState.preview = true;
@@ -924,7 +935,7 @@
924935
* @returns {object} EpicEditor will be returned
925936
*/
926937
EpicEditor.prototype.enterFullscreen = function () {
927-
if (this._eeState.fullscreen) { return this; }
938+
if (this.is('fullscreen')) { return this; }
928939
this._goFullscreen(this.iframeElement);
929940
return this;
930941
}
@@ -934,7 +945,7 @@
934945
* @returns {object} EpicEditor will be returned
935946
*/
936947
EpicEditor.prototype.exitFullscreen = function () {
937-
if (!this._eeState.fullscreen) { return this; }
948+
if (!this.is('fullscreen')) { return this; }
938949
this._exitFullscreen(this.iframeElement);
939950
return this;
940951
}
@@ -973,14 +984,19 @@
973984

974985
// Check that the given string is a possible option and verify the editor isn't unloaded
975986
// without this, you'd be given a reference to an object that no longer exists in the DOM
976-
if (!available[name] || this._eeState.unloaded) {
987+
if (!available[name] || this.is('unloaded')) {
977988
return null;
978989
}
979990
else {
980991
return available[name];
981992
}
982993
}
983994

995+
/**
996+
* Returns a boolean of each "state" of the editor. For example "editor.is('loaded')" // returns true/false
997+
* @param {String} what the state you want to check for
998+
* @returns {Boolean}
999+
*/
9841000
EpicEditor.prototype.is = function (what) {
9851001
var self = this;
9861002
switch (what) {
@@ -1137,7 +1153,7 @@
11371153

11381154
self.save();
11391155

1140-
if (self._eeState.fullscreen) {
1156+
if (self.is('fullscreen')) {
11411157
self.preview();
11421158
}
11431159

0 commit comments

Comments
 (0)