Skip to content

Commit

Permalink
Ticket #286 - textarea now syncs when file is changed
Browse files Browse the repository at this point in the history
This also changes the behavior of the create event. Before create would
fire _before_ the file was created meaning if you asked for the file
inside of create it would give you undefined. I've moved the create
event into save, rather than importFile, and it fires after the file is
created.
  • Loading branch information
OscarGodson committed Sep 6, 2013
1 parent ff9f43d commit b7ca05b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 40 deletions.
33 changes: 18 additions & 15 deletions epiceditor/js/epiceditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,6 @@

EpicEditor.prototype._setupTextareaSync = function () {
var self = this
, textareaFileName = self.settings.file.name
, _syncTextarea;

// Even if autoSave is false, we want to make sure to keep the textarea synced
Expand All @@ -1167,7 +1166,10 @@
// This only happens for draft files. Probably has something to do with
// the fact draft files haven't been saved by the time this is called.
// TODO: Add test for this case.
self._textareaElement.value = self.exportFile(textareaFileName, 'text', true) || self.settings.file.defaultContent;
// Get the file.name each time as it can change. DO NOT save this to a
// var outside of this closure or the editor will stop syncing when the
// file is changed with importFile or open.
self._textareaElement.value = self.exportFile(self.settings.file.name, 'text', true) || self.settings.file.defaultContent;
}

if (typeof self.settings.textarea == 'string') {
Expand All @@ -1191,7 +1193,7 @@
// 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);
self.importFile(self.settings.file.name, self._textareaElement.value);

// manually save draft after import so there is no delay between the
// import and exporting in _syncTextarea. Without this, _syncTextarea
Expand All @@ -1204,6 +1206,7 @@

// Make sure to keep it updated
self.on('__update', _syncTextarea);
self.on('__create', _syncTextarea);
}

/**
Expand Down Expand Up @@ -1504,6 +1507,7 @@
var self = this
, storage
, isUpdate = false
, isNew = false
, file = self.settings.file.name
, previewDraftName = ''
, data = this._storage[previewDraftName + self.settings.localStorageName]
Expand All @@ -1525,6 +1529,7 @@
// If the file doesn't exist we need to create it
if (storage[file] === undefined) {
storage[file] = self._defaultFileSchema();
isNew = true;
}

// If it does, we need to check if the content is different and
Expand All @@ -1541,10 +1546,17 @@
storage[file].content = content;
this._storage[previewDraftName + self.settings.localStorageName] = JSON.stringify(storage);

// After the content is actually changed, emit update so it emits the updated content
// If it's a new file, send a create event as well as a private one for
// use internally.
if (isNew) {
self.emit('create');
self.emit('__create');
}

// After the content is actually changed, emit update so it emits the
// updated content. Also send a private event for interal use.
if (isUpdate) {
self.emit('update');
// Emit a private update event so it can't get accidentally removed
self.emit('__update');
}

Expand Down Expand Up @@ -1606,26 +1618,17 @@
* @returns {object} EpicEditor will be returned
*/
EpicEditor.prototype.importFile = function (name, content, kind, meta) {
var self = this
, isNew = false;
var self = this;

name = name || self.settings.file.name;
content = content || '';
kind = kind || 'md';
meta = meta || {};

if (JSON.parse(this._storage[self.settings.localStorageName])[name] === undefined) {
isNew = true;
}

// Set our current file to the new file and update the content
self.settings.file.name = name;
_setText(self.editor, content);

if (isNew) {
self.emit('create');
}

self.save();

if (self.is('fullscreen')) {
Expand Down
4 changes: 2 additions & 2 deletions epiceditor/js/epiceditor.min.js

Large diffs are not rendered by default.

33 changes: 18 additions & 15 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,6 @@

EpicEditor.prototype._setupTextareaSync = function () {
var self = this
, textareaFileName = self.settings.file.name
, _syncTextarea;

// Even if autoSave is false, we want to make sure to keep the textarea synced
Expand All @@ -1167,7 +1166,10 @@
// This only happens for draft files. Probably has something to do with
// the fact draft files haven't been saved by the time this is called.
// TODO: Add test for this case.
self._textareaElement.value = self.exportFile(textareaFileName, 'text', true) || self.settings.file.defaultContent;
// Get the file.name each time as it can change. DO NOT save this to a
// var outside of this closure or the editor will stop syncing when the
// file is changed with importFile or open.
self._textareaElement.value = self.exportFile(self.settings.file.name, 'text', true) || self.settings.file.defaultContent;
}

if (typeof self.settings.textarea == 'string') {
Expand All @@ -1191,7 +1193,7 @@
// 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);
self.importFile(self.settings.file.name, self._textareaElement.value);

// manually save draft after import so there is no delay between the
// import and exporting in _syncTextarea. Without this, _syncTextarea
Expand All @@ -1204,6 +1206,7 @@

// Make sure to keep it updated
self.on('__update', _syncTextarea);
self.on('__create', _syncTextarea);
}

/**
Expand Down Expand Up @@ -1504,6 +1507,7 @@
var self = this
, storage
, isUpdate = false
, isNew = false
, file = self.settings.file.name
, previewDraftName = ''
, data = this._storage[previewDraftName + self.settings.localStorageName]
Expand All @@ -1525,6 +1529,7 @@
// If the file doesn't exist we need to create it
if (storage[file] === undefined) {
storage[file] = self._defaultFileSchema();
isNew = true;
}

// If it does, we need to check if the content is different and
Expand All @@ -1541,10 +1546,17 @@
storage[file].content = content;
this._storage[previewDraftName + self.settings.localStorageName] = JSON.stringify(storage);

// After the content is actually changed, emit update so it emits the updated content
// If it's a new file, send a create event as well as a private one for
// use internally.
if (isNew) {
self.emit('create');
self.emit('__create');
}

// After the content is actually changed, emit update so it emits the
// updated content. Also send a private event for interal use.
if (isUpdate) {
self.emit('update');
// Emit a private update event so it can't get accidentally removed
self.emit('__update');
}

Expand Down Expand Up @@ -1606,26 +1618,17 @@
* @returns {object} EpicEditor will be returned
*/
EpicEditor.prototype.importFile = function (name, content, kind, meta) {
var self = this
, isNew = false;
var self = this;

name = name || self.settings.file.name;
content = content || '';
kind = kind || 'md';
meta = meta || {};

if (JSON.parse(this._storage[self.settings.localStorageName])[name] === undefined) {
isNew = true;
}

// Set our current file to the new file and update the content
self.settings.file.name = name;
_setText(self.editor, content);

if (isNew) {
self.emit('create');
}

self.save();

if (self.is('fullscreen')) {
Expand Down
8 changes: 0 additions & 8 deletions test/test.importFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ describe('.importFile([fileName],[content])', function () {
expect(editor.exportFile(id)).to.be('foo');
});

it('should fire the create event when importing a new file', function () {
editor.on('create', function () {
eventFired = true;
});
editor.importFile(fooFile);
expect(eventFired).to.be(true);
});

it('should fire the update event when modifying an existing file', function () {
editor.on('update', function () {
eventFired = true;
Expand Down
10 changes: 10 additions & 0 deletions test/test.options.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,16 @@ describe('EpicEditor([options])', function () {
done();
}, 100)
});
it('should sync the content of the editor when content is imported with a different name', function (done) {
var editor = new EpicEditor(opts).load();
expect(textareaElement.value).to.be(id);

editor.importFile(id + 'blahblah', 'Imported');
setTimeout(function () {
expect(textareaElement.value).to.be('Imported');
done();
}, 100)
});
it('should sync the content of the editor when the editor is updated AND autoSave is OFF', function (done) {
opts.file.autoSave = false;
var editor = new EpicEditor(opts).load();
Expand Down
9 changes: 9 additions & 0 deletions test/test.save.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ describe('.save()', function () {
expect(JSON.parse(localStorage['epiceditor'])[id].content).to.be('bar');
});

it('should fire the create event when saving a new file', function () {
editor.on('create', function () {
eventFired = true;
});
editor.settings.file.name = id + 'LOL';
editor.save();
expect(eventFired).to.be(true);
});

it('should fire the save event but not the autosave event', function () {
editor.on('save', function () {
eventFired = true;
Expand Down

0 comments on commit b7ca05b

Please sign in to comment.