Skip to content

Commit

Permalink
Ticket #212 - Make sure if content is in the textarea on page or JS l…
Browse files Browse the repository at this point in the history
…oad to use

that as the initial content for the editor. Still needs tests for this use case!
  • Loading branch information
OscarGodson committed Apr 5, 2013
1 parent 84aac99 commit 49d21bf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
16 changes: 12 additions & 4 deletions epiceditor/js/epiceditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@
, keypressTimer
, mousePos = { y: -1, x: -1 }
, _elementStates
, _syncTextarea
, _isInEdit
, nativeFs = false
, nativeFsWebkit = false
Expand Down Expand Up @@ -925,21 +926,28 @@
self.save(true);
}, 100);

_syncTextarea = function () {
self._textareaElement.value = self.exportFile(textareaFileName, 'text', true);
}

textareaFileName = self.settings.file.name;

if (typeof self.settings.textarea == 'string') {
self._textareaElement = document.getElementById(self.settings.textarea);
}
else if (typeof self.settings.textarea == 'object') {
self._textareaElement = self.settings.textarea;
}

if (self._textareaElement.value !== '') {
self.importFile(textareaFileName, self._textareaElement.value);
}

// Update the textarea on load and pull from drafts
self._textareaElement.value = self.exportFile(textareaFileName, 'text', true);
_syncTextarea();

// Make sure to keep it updated
self.on('__update', function () {
self._textareaElement.value = self.exportFile(textareaFileName, 'text', true);
});
self.on('__update', _syncTextarea);
}

window.addEventListener('resize', function () {
Expand Down
2 changes: 1 addition & 1 deletion epiceditor/js/epiceditor.min.js

Large diffs are not rendered by default.

29 changes: 25 additions & 4 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@
, keypressTimer
, mousePos = { y: -1, x: -1 }
, _elementStates
, _syncTextarea
, _isInEdit
, nativeFs = false
, nativeFsWebkit = false
Expand Down Expand Up @@ -925,21 +926,41 @@
self.save(true);
}, 100);

_syncTextarea = function () {
self._textareaElement.value = self.exportFile(textareaFileName, 'text', true);
}

textareaFileName = self.settings.file.name;

if (typeof self.settings.textarea == 'string') {
self._textareaElement = document.getElementById(self.settings.textarea);
}
else if (typeof self.settings.textarea == 'object') {
self._textareaElement = self.settings.textarea;
}

// On page load, if there's content in the textarea that means one of two
// different things:
//
// 1. The editor didn't load and the user was writing in the textarea and
// now he refreshed the page or the JS loaded and the textarea now has
// content. If this is the case the user probably expects his content is
// moved into the editor and not lose what he typed.
//
// 2. The developer put content in the textarea from some server side
// code. In this case, the textarea will take precedence.
//
// If the developer wants drafts to be recoverable they should check if
// the local file in localStorage's modified date is newer than the server.
if (self._textareaElement.value !== '') {
self.importFile(textareaFileName, self._textareaElement.value);
}

// Update the textarea on load and pull from drafts
self._textareaElement.value = self.exportFile(textareaFileName, 'text', true);
_syncTextarea();

// Make sure to keep it updated
self.on('__update', function () {
self._textareaElement.value = self.exportFile(textareaFileName, 'text', true);
});
self.on('__update', _syncTextarea);
}

window.addEventListener('resize', function () {
Expand Down
3 changes: 3 additions & 0 deletions test/tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
.hidden {
display: none;
}
#mocha .test.fail code {
color: #fff;
}
</style>
<script src="/docs/js/jquery.min.js"></script>
<script src="../node_modules/expect.js/expect.js"></script>
Expand Down

0 comments on commit 49d21bf

Please sign in to comment.