Skip to content

Commit

Permalink
Ticket OscarGodson#204 - refactoring getFiles to only give metadata, …
Browse files Browse the repository at this point in the history
…fixing firefox whitespace bug
  • Loading branch information
Alexis Beingessner committed Jun 5, 2013
1 parent fa27bd6 commit c4d80ec
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 44 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,11 @@ importFileBtn.onclick = function () {

### exportFile([_filename_],[_type_])

Returns the raw content of the file by default, or if given a `type` will return the content converted into that type. If you leave both parameters `null` it will return the current document's raw content.
Returns the raw content of the file by default (warning: this may be browser-specific), or if given a `type` will return the content converted into that type. If you leave both parameters `null` it will return the current document's raw content.

```javascript
syncWithServerBtn.onclick = function () {
var theContent = editor.exportFile();
var theContent = editor.exportFile(null, 'text');
saveToServerAjaxCall('/save', {data:theContent}, function () {
console.log('Data was saved to the database.');
});
Expand Down Expand Up @@ -331,7 +331,7 @@ removeFileBtn.onclick = function () {

### getFiles([_name_])

If no `name` is given it returns an object containing all the file objects. If a `name` is specified it will return just that single file object.
If no `name` is given it returns an object containing the names and metadata of all file objects. If a `name` is specified it will return just the metadata of that single file object.

```javascript
var files = editor.getFiles();
Expand Down
66 changes: 50 additions & 16 deletions epiceditor/js/epiceditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,14 @@
content = content.replace(/</g, '&lt;');
content = content.replace(/>/g, '&gt;');
content = content.replace(/\n/g, '<br>');
// Make sure to look for TWO spaces and replace with a space and &nbsp;

// Make sure to there aren't two spaces in a row (replace one with &nbsp;)
// If you find and replace every space with a &nbsp; text will not wrap.
// Hence the name (Non-Breaking-SPace).
content = content.replace(/\s\s/g, ' &nbsp;')

content = content.replace(/<br>\s/g, '<br>&nbsp;')
content = content.replace(/\s\s\s/g, '&nbsp; &nbsp;')
content = content.replace(/\s\s/g, '&nbsp; ')
el.innerHTML = content;
}
return true;
Expand Down Expand Up @@ -418,7 +422,6 @@
if (localStorage && self.settings.clientSideStorage) {
this._storage = localStorage;
if (this._storage[self.settings.localStorageName] && self.getFiles(self.settings.file.name) === undefined) {
_defaultFile = self.getFiles(self.settings.file.name);
_defaultFile = self._defaultFileSchema();
_defaultFile.content = self.settings.file.defaultContent;
}
Expand Down Expand Up @@ -1333,9 +1336,9 @@
name = name || self.settings.file.name;
self.settings.file.name = name;
if (this._storage[self.settings.localStorageName]) {
fileObj = self.getFiles();
if (fileObj[name] !== undefined) {
_setText(self.editor, fileObj[name].content);
fileObj = self.exportFile(name);
if (fileObj !== undefined) {
_setText(self.editor, fileObj);
self.emit('read');
}
else {
Expand Down Expand Up @@ -1479,10 +1482,30 @@
return this;
};

/**
* Gets the local filestore
* @param {string} name Name of the file in the store
* @returns {object|undefined} the local filestore, or a specific file in the store, if a name is given
*/
EpicEditor.prototype._getFileStore = function (name, _isPreviewDraft) {
var previewDraftName = ''
, store;
if (_isPreviewDraft) {
previewDraftName = this._previewDraftLocation;
}
store = JSON.parse(this._storage[previewDraftName + this.settings.localStorageName]);
if (name) {
return store[name];
}
else {
return store;
}
}

/**
* Exports a file as a string in a supported format
* @param {string} name Name of the file you want to export (case sensitive)
* @param {string} kind Kind of file you want the content in (currently supports html and text)
* @param {string} kind Kind of file you want the content in (currently supports html and text, default is the format the browser "wants")
* @returns {string|undefined} The content of the file in the content given or undefined if it doesn't exist
*/
EpicEditor.prototype.exportFile = function (name, kind, _isPreviewDraft) {
Expand All @@ -1493,7 +1516,7 @@
name = name || self.settings.file.name;
kind = kind || 'text';

file = self.getFiles(name, _isPreviewDraft);
file = self._getFileStore(name, _isPreviewDraft);

// If the file doesn't exist just return early with undefined
if (file === undefined) {
Expand All @@ -1518,17 +1541,28 @@
}
}

EpicEditor.prototype.getFiles = function (name, _isPreviewDraft) {
var previewDraftName = '';
if (_isPreviewDraft) {
previewDraftName = this._previewDraftLocation;
}
var files = JSON.parse(this._storage[previewDraftName + this.settings.localStorageName]);
/**
* Gets the metadata for files
* @param {string} name Name of the file whose metadata you want (case sensitive)
* @returns {object} An object with the names and metadata of every file, or just the metadata of one file if a name was given
*/
EpicEditor.prototype.getFiles = function (name) {
var file
, data = this._getFileStore(name);

if (name) {
return files[name];
if (data !== undefined) {
delete data.content;
}
return data;
}
else {
return files;
for (file in data) {
if (data.hasOwnProperty(file)) {
delete data[file].content;
}
}
return data;
}
}

Expand Down
2 changes: 1 addition & 1 deletion epiceditor/js/epiceditor.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ <h3>EpicEditor([<em>options</em>])</h3>
basePath: &#39;epiceditor&#39;,
clientSideStorage: true,
localStorageName: &#39;epiceditor&#39;,
useNativeFullsreen: true,
useNativeFullscreen: true,
parser: marked,
file: {
name: &#39;epiceditor&#39;,
Expand Down Expand Up @@ -273,11 +273,11 @@ <h3>importFile([<em>filename</em>],[<em>content</em>])</h3>
editor.importFile(&#39;some-file&#39;,&quot;#Imported markdown\nFancy, huh?&quot;); //Imports a file when the user clicks this button
}</code></pre>
<h3>exportFile([<em>filename</em>],[<em>type</em>])</h3>
<p>Returns the raw content of the file by default, or if given a <code>type</code> will return the content converted into that type. If you leave both parameters <code>null</code> it will return the current document&#39;s raw content.
<p>Returns the raw content of the file by default (warning: this may be browser-specific), or if given a <code>type</code> will return the content converted into that type. If you leave both parameters <code>null</code> it will return the current document&#39;s raw content.

</p>
<pre><code class="lang-javascript">syncWithServerBtn.onclick = function () {
var theContent = editor.exportFile();
var theContent = editor.exportFile(null, &#39;text&#39;);
saveToServerAjaxCall(&#39;/save&#39;, {data:theContent}, function () {
console.log(&#39;Data was saved to the database.&#39;);
});
Expand Down Expand Up @@ -305,7 +305,7 @@ <h3>remove(<em>name</em>)</h3>
editor.remove(&#39;example.md&#39;);
}</code></pre>
<h3>getFiles([<em>name</em>])</h3>
<p>If no <code>name</code> is given it returns an object containing all the file objects. If a <code>name</code> is specified it will return just that single file object.
<p>If no <code>name</code> is given it returns an object containing the names and metadata of all file objects. If a <code>name</code> is specified it will return just the metadata of that single file object.

</p>
<pre><code class="lang-javascript">var files = editor.getFiles();
Expand Down
66 changes: 50 additions & 16 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,14 @@
content = content.replace(/</g, '&lt;');
content = content.replace(/>/g, '&gt;');
content = content.replace(/\n/g, '<br>');
// Make sure to look for TWO spaces and replace with a space and &nbsp;

// Make sure to there aren't two spaces in a row (replace one with &nbsp;)
// If you find and replace every space with a &nbsp; text will not wrap.
// Hence the name (Non-Breaking-SPace).
content = content.replace(/\s\s/g, ' &nbsp;')

content = content.replace(/<br>\s/g, '<br>&nbsp;')
content = content.replace(/\s\s\s/g, '&nbsp; &nbsp;')
content = content.replace(/\s\s/g, '&nbsp; ')
el.innerHTML = content;
}
return true;
Expand Down Expand Up @@ -418,7 +422,6 @@
if (localStorage && self.settings.clientSideStorage) {
this._storage = localStorage;
if (this._storage[self.settings.localStorageName] && self.getFiles(self.settings.file.name) === undefined) {
_defaultFile = self.getFiles(self.settings.file.name);
_defaultFile = self._defaultFileSchema();
_defaultFile.content = self.settings.file.defaultContent;
}
Expand Down Expand Up @@ -1333,9 +1336,9 @@
name = name || self.settings.file.name;
self.settings.file.name = name;
if (this._storage[self.settings.localStorageName]) {
fileObj = self.getFiles();
if (fileObj[name] !== undefined) {
_setText(self.editor, fileObj[name].content);
fileObj = self.exportFile(name);
if (fileObj !== undefined) {
_setText(self.editor, fileObj);
self.emit('read');
}
else {
Expand Down Expand Up @@ -1479,10 +1482,30 @@
return this;
};

/**
* Gets the local filestore
* @param {string} name Name of the file in the store
* @returns {object|undefined} the local filestore, or a specific file in the store, if a name is given
*/
EpicEditor.prototype._getFileStore = function (name, _isPreviewDraft) {
var previewDraftName = ''
, store;
if (_isPreviewDraft) {
previewDraftName = this._previewDraftLocation;
}
store = JSON.parse(this._storage[previewDraftName + this.settings.localStorageName]);
if (name) {
return store[name];
}
else {
return store;
}
}

/**
* Exports a file as a string in a supported format
* @param {string} name Name of the file you want to export (case sensitive)
* @param {string} kind Kind of file you want the content in (currently supports html and text)
* @param {string} kind Kind of file you want the content in (currently supports html and text, default is the format the browser "wants")
* @returns {string|undefined} The content of the file in the content given or undefined if it doesn't exist
*/
EpicEditor.prototype.exportFile = function (name, kind, _isPreviewDraft) {
Expand All @@ -1493,7 +1516,7 @@
name = name || self.settings.file.name;
kind = kind || 'text';

file = self.getFiles(name, _isPreviewDraft);
file = self._getFileStore(name, _isPreviewDraft);

// If the file doesn't exist just return early with undefined
if (file === undefined) {
Expand All @@ -1518,17 +1541,28 @@
}
}

EpicEditor.prototype.getFiles = function (name, _isPreviewDraft) {
var previewDraftName = '';
if (_isPreviewDraft) {
previewDraftName = this._previewDraftLocation;
}
var files = JSON.parse(this._storage[previewDraftName + this.settings.localStorageName]);
/**
* Gets the metadata for files
* @param {string} name Name of the file whose metadata you want (case sensitive)
* @returns {object} An object with the names and metadata of every file, or just the metadata of one file if a name was given
*/
EpicEditor.prototype.getFiles = function (name) {
var file
, data = this._getFileStore(name);

if (name) {
return files[name];
if (data !== undefined) {
delete data.content;
}
return data;
}
else {
return files;
for (file in data) {
if (data.hasOwnProperty(file)) {
delete data[file].content;
}
}
return data;
}
}

Expand Down
6 changes: 4 additions & 2 deletions test/test.getFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ describe('.getFiles([name])', function () {
expect(fileCount).to.be(3);
});

it('should return the correct file when the name is specified', function () {
expect(editor.getFiles(fooFile).content).to.be('foo');
it('should return just one file when the name is specified', function () {
var file = editor.getFiles(fooFile);
expect(file).not.to.be(undefined);
expect(file).to.have.property("modified");
});
});
4 changes: 2 additions & 2 deletions test/test.load.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe(".load([callback])", function () {

// blow away the stack trace. Hack for Mocha:
// https://github.com/visionmedia/mocha/issues/502
beforeEach(function(done){
beforeEach(function (done) {
setTimeout(done, 0);
});

Expand Down Expand Up @@ -113,7 +113,7 @@ describe(".load([callback])", function () {
expect(previewer.body.scrollTop).to.be.greaterThan(0);
previewer.body.scrollTop = 0;

anchor.href = window.location.hostname+'#test';
anchor.href = window.location.hostname + '#test';
// Again, this is for IE
anchor.hostname = originalHostname
anchor.click();
Expand Down

0 comments on commit c4d80ec

Please sign in to comment.