mirror of https://github.com/laurent22/joplin.git
handle saving notes and folders in a uniform way
parent
56dc74c4cc
commit
a88e729812
|
@ -90,6 +90,17 @@ class BaseModel {
|
|||
return model;
|
||||
}
|
||||
|
||||
static diffObjects(oldModel, newModel) {
|
||||
let output = {};
|
||||
for (let n in newModel) {
|
||||
if (!newModel.hasOwnProperty(n)) continue;
|
||||
if (!(n in oldModel) || newModel[n] !== oldModel[n]) {
|
||||
output[n] = newModel[n];
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
static saveQuery(o, isNew = 'auto') {
|
||||
if (isNew == 'auto') isNew = !o.id;
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import { Log } from 'src/log.js'
|
|||
import { Note } from 'src/models/note.js'
|
||||
import { Registry } from 'src/registry.js'
|
||||
import { ScreenHeader } from 'src/components/screen-header.js';
|
||||
import { NoteFolderService } from 'src/services/note-folder-service.js';
|
||||
|
||||
class NoteScreenComponent extends React.Component {
|
||||
|
||||
|
@ -15,6 +16,7 @@ class NoteScreenComponent extends React.Component {
|
|||
constructor() {
|
||||
super();
|
||||
this.state = { note: Note.new() }
|
||||
this.originalNote = null;
|
||||
}
|
||||
|
||||
componentWillMount() {
|
||||
|
@ -22,6 +24,7 @@ class NoteScreenComponent extends React.Component {
|
|||
this.setState({ note: Note.new(this.props.folderId) });
|
||||
} else {
|
||||
Note.load(this.props.noteId).then((note) => {
|
||||
this.originalNote = Object.assign({}, note);
|
||||
this.setState({ note: note });
|
||||
});
|
||||
}
|
||||
|
@ -44,11 +47,9 @@ class NoteScreenComponent extends React.Component {
|
|||
}
|
||||
|
||||
saveNoteButton_press = () => {
|
||||
let isNew = !this.state.note.id;
|
||||
Note.save(this.state.note).then((note) => {
|
||||
if (isNew) return Note.updateGeolocation(note.id);
|
||||
}).then(() => {
|
||||
Registry.synchronizer().start();
|
||||
NoteFolderService.save('note', this.state.note, this.originalNote).then((note) => {
|
||||
this.originalNote = Object.assign({}, note);
|
||||
this.setState({ note: note });
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
// A service that handle notes and folders in a uniform way
|
||||
|
||||
import { BaseService } from 'src/base-service.js';
|
||||
import { BaseModel } from 'src/base-model.js';
|
||||
import { Note } from 'src/models/note.js';
|
||||
import { Folder } from 'src/models/folder.js';
|
||||
import { Log } from 'src/log.js';
|
||||
import { Registry } from 'src/registry.js';
|
||||
|
||||
class NoteFolderService extends BaseService {
|
||||
|
||||
static save(type, item, oldItem) {
|
||||
if (oldItem) {
|
||||
let diff = BaseModel.diffObjects(oldItem, item);
|
||||
if (!Object.getOwnPropertyNames(diff).length) {
|
||||
Log.info('Item not changed - not saved');
|
||||
return Promise.resolve(item);
|
||||
}
|
||||
}
|
||||
|
||||
let ItemClass = null;
|
||||
if (type == 'note') {
|
||||
ItemClass = Note;
|
||||
} else if (type == 'folder') {
|
||||
ItemClass = Folder;
|
||||
}
|
||||
|
||||
let isNew = !item.id;
|
||||
let output = null;
|
||||
return ItemClass.save(item).then((item) => {
|
||||
output = item;
|
||||
if (isNew && type == 'note') return Note.updateGeolocation(item.id);
|
||||
}).then(() => {
|
||||
Registry.synchronizer().start();
|
||||
return output;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export { NoteFolderService };
|
Loading…
Reference in New Issue