2020-11-05 18:58:23 +02:00
|
|
|
const { reg } = require('../../registry.js');
|
2021-01-22 19:41:11 +02:00
|
|
|
const Folder = require('../../models/Folder').default;
|
2020-11-05 18:58:23 +02:00
|
|
|
const BaseModel = require('../../BaseModel').default;
|
2021-01-22 19:41:11 +02:00
|
|
|
const Note = require('../../models/Note').default;
|
|
|
|
const Resource = require('../../models/Resource').default;
|
|
|
|
const ResourceFetcher = require('../../services/ResourceFetcher').default;
|
|
|
|
const DecryptionWorker = require('../../services/DecryptionWorker').default;
|
2020-11-05 18:58:23 +02:00
|
|
|
const Setting = require('../../models/Setting').default;
|
2019-01-29 20:02:34 +02:00
|
|
|
const Mutex = require('async-mutex').Mutex;
|
2017-11-05 02:49:23 +02:00
|
|
|
|
|
|
|
const shared = {};
|
|
|
|
|
2019-01-29 20:02:34 +02:00
|
|
|
// If saveNoteButton_press is called multiple times in short intervals, it might result in
|
|
|
|
// the same new note being created twice, so we need to a mutex to access this function.
|
|
|
|
const saveNoteMutex_ = new Mutex();
|
|
|
|
|
2017-11-05 02:49:23 +02:00
|
|
|
shared.noteExists = async function(noteId) {
|
|
|
|
const existingNote = await Note.load(noteId);
|
|
|
|
return !!existingNote;
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-05 02:49:23 +02:00
|
|
|
|
2020-03-06 20:49:30 +02:00
|
|
|
// Note has been deleted while user was modifying it. In that case, we
|
|
|
|
// just save a new note so that user can keep editing.
|
|
|
|
shared.handleNoteDeletedWhileEditing_ = async (note) => {
|
|
|
|
if (await shared.noteExists(note.id)) return null;
|
|
|
|
|
|
|
|
reg.logger().info('Note has been deleted while it was being edited - recreating it.');
|
|
|
|
|
|
|
|
let newNote = Object.assign({}, note);
|
|
|
|
delete newNote.id;
|
|
|
|
newNote = await Note.save(newNote);
|
|
|
|
|
|
|
|
return Note.load(newNote.id);
|
|
|
|
};
|
|
|
|
|
2019-06-13 09:48:19 +02:00
|
|
|
shared.saveNoteButton_press = async function(comp, folderId = null, options = null) {
|
2020-03-04 03:13:10 +02:00
|
|
|
options = Object.assign({}, {
|
|
|
|
autoTitle: true,
|
|
|
|
}, options);
|
2019-06-13 09:48:19 +02:00
|
|
|
|
2019-01-29 20:02:34 +02:00
|
|
|
const releaseMutex = await saveNoteMutex_.acquire();
|
|
|
|
|
2017-11-05 02:49:23 +02:00
|
|
|
let note = Object.assign({}, comp.state.note);
|
|
|
|
|
2020-03-06 20:49:30 +02:00
|
|
|
const recreatedNote = await shared.handleNoteDeletedWhileEditing_(note);
|
|
|
|
if (recreatedNote) note = recreatedNote;
|
2017-11-05 02:49:23 +02:00
|
|
|
|
2018-05-01 19:53:45 +02:00
|
|
|
if (folderId) {
|
|
|
|
note.parent_id = folderId;
|
|
|
|
} else if (!note.parent_id) {
|
2018-05-09 19:12:00 +02:00
|
|
|
const activeFolderId = Setting.value('activeFolderId');
|
|
|
|
let folder = await Folder.load(activeFolderId);
|
|
|
|
if (!folder) folder = await Folder.defaultFolder();
|
2019-01-29 20:02:34 +02:00
|
|
|
if (!folder) return releaseMutex();
|
2017-11-05 02:49:23 +02:00
|
|
|
note.parent_id = folder.id;
|
|
|
|
}
|
|
|
|
|
2020-03-04 03:13:10 +02:00
|
|
|
const isProvisionalNote = comp.props.provisionalNoteIds.includes(note.id);
|
2017-11-05 02:49:23 +02:00
|
|
|
|
2020-03-04 03:13:10 +02:00
|
|
|
const saveOptions = {
|
|
|
|
userSideValidation: true,
|
|
|
|
fields: BaseModel.diffObjectsFields(comp.state.lastSavedNote, note),
|
|
|
|
};
|
2017-11-28 00:50:46 +02:00
|
|
|
|
2020-03-04 03:13:10 +02:00
|
|
|
const hasAutoTitle = comp.state.newAndNoTitleChangeNoteId || (isProvisionalNote && !note.title);
|
2019-06-13 09:48:19 +02:00
|
|
|
if (hasAutoTitle && options.autoTitle) {
|
2020-05-02 17:41:07 +02:00
|
|
|
note.title = Note.defaultTitle(note.body);
|
2019-06-13 09:48:19 +02:00
|
|
|
if (saveOptions.fields && saveOptions.fields.indexOf('title') < 0) saveOptions.fields.push('title');
|
2018-01-12 21:58:01 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
const savedNote = 'fields' in saveOptions && !saveOptions.fields.length ? Object.assign({}, note) : await Note.save(note, saveOptions);
|
2017-11-28 00:50:46 +02:00
|
|
|
|
2017-12-01 02:00:18 +02:00
|
|
|
const stateNote = comp.state.note;
|
2018-01-12 21:58:01 +02:00
|
|
|
|
|
|
|
// Note was reloaded while being saved.
|
2020-03-06 20:49:30 +02:00
|
|
|
if (!recreatedNote && (!stateNote || stateNote.id !== savedNote.id)) return releaseMutex();
|
2018-01-12 21:58:01 +02:00
|
|
|
|
2017-11-28 00:50:46 +02:00
|
|
|
// Re-assign any property that might have changed during saving (updated_time, etc.)
|
|
|
|
note = Object.assign(note, savedNote);
|
|
|
|
|
2018-01-12 21:58:01 +02:00
|
|
|
if (stateNote.id === note.id) {
|
2017-12-01 02:00:18 +02:00
|
|
|
// But we preserve the current title and body because
|
|
|
|
// the user might have changed them between the time
|
|
|
|
// saveNoteButton_press was called and the note was
|
2017-12-07 23:32:22 +02:00
|
|
|
// saved (it's done asynchronously).
|
|
|
|
//
|
|
|
|
// If the title was auto-assigned above, we don't restore
|
|
|
|
// it from the state because it will be empty there.
|
2018-01-12 21:58:01 +02:00
|
|
|
if (!hasAutoTitle) note.title = stateNote.title;
|
2017-12-01 02:00:18 +02:00
|
|
|
note.body = stateNote.body;
|
|
|
|
}
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const newState = {
|
2017-11-05 02:49:23 +02:00
|
|
|
lastSavedNote: Object.assign({}, note),
|
|
|
|
note: note,
|
2018-01-12 21:58:01 +02:00
|
|
|
};
|
|
|
|
|
2020-03-04 03:13:10 +02:00
|
|
|
if (isProvisionalNote && hasAutoTitle) newState.newAndNoTitleChangeNoteId = note.id;
|
2018-01-12 21:58:01 +02:00
|
|
|
|
2019-06-13 09:48:19 +02:00
|
|
|
if (!options.autoTitle) newState.newAndNoTitleChangeNoteId = null;
|
|
|
|
|
2018-01-12 21:58:01 +02:00
|
|
|
comp.setState(newState);
|
2017-11-28 00:50:46 +02:00
|
|
|
|
2020-03-04 03:13:10 +02:00
|
|
|
if (isProvisionalNote) {
|
2020-05-21 10:14:33 +02:00
|
|
|
Note.updateGeolocation(note.id).then(geoNote => {
|
2018-01-28 20:42:43 +02:00
|
|
|
const stateNote = comp.state.note;
|
2018-01-30 23:49:22 +02:00
|
|
|
if (!stateNote || !geoNote) return;
|
2018-01-28 20:42:43 +02:00
|
|
|
if (stateNote.id !== geoNote.id) return; // Another note has been loaded while geoloc was being retrieved
|
|
|
|
|
|
|
|
// Geo-location for this note has been saved to the database however the properties
|
|
|
|
// are is not in the state so set them now.
|
|
|
|
|
|
|
|
const geoInfo = {
|
|
|
|
longitude: geoNote.longitude,
|
|
|
|
latitude: geoNote.latitude,
|
|
|
|
altitude: geoNote.altitude,
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-01-28 20:42:43 +02:00
|
|
|
|
|
|
|
const modNote = Object.assign({}, stateNote, geoInfo);
|
|
|
|
const modLastSavedNote = Object.assign({}, comp.state.lastSavedNote, geoInfo);
|
|
|
|
|
|
|
|
comp.setState({ note: modNote, lastSavedNote: modLastSavedNote });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-29 20:02:34 +02:00
|
|
|
releaseMutex();
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-05 02:49:23 +02:00
|
|
|
|
|
|
|
shared.saveOneProperty = async function(comp, name, value) {
|
|
|
|
let note = Object.assign({}, comp.state.note);
|
|
|
|
|
2020-03-06 20:49:30 +02:00
|
|
|
const recreatedNote = await shared.handleNoteDeletedWhileEditing_(note);
|
|
|
|
if (recreatedNote) note = recreatedNote;
|
2017-11-05 02:49:23 +02:00
|
|
|
|
2020-03-06 20:49:30 +02:00
|
|
|
let toSave = { id: note.id };
|
|
|
|
toSave[name] = value;
|
|
|
|
toSave = await Note.save(toSave);
|
|
|
|
note[name] = toSave[name];
|
2017-11-05 02:49:23 +02:00
|
|
|
|
2020-03-06 20:49:30 +02:00
|
|
|
comp.setState({
|
|
|
|
lastSavedNote: Object.assign({}, note),
|
|
|
|
note: note,
|
|
|
|
});
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-05 02:49:23 +02:00
|
|
|
|
|
|
|
shared.noteComponent_change = function(comp, propName, propValue) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const newState = {};
|
2018-01-12 21:58:01 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const note = Object.assign({}, comp.state.note);
|
2017-11-05 02:49:23 +02:00
|
|
|
note[propName] = propValue;
|
2018-01-12 21:58:01 +02:00
|
|
|
newState.note = note;
|
|
|
|
|
|
|
|
comp.setState(newState);
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-05 02:49:23 +02:00
|
|
|
|
2019-05-22 16:56:07 +02:00
|
|
|
let resourceCache_ = {};
|
|
|
|
|
|
|
|
shared.clearResourceCache = function() {
|
|
|
|
resourceCache_ = {};
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2019-03-08 19:14:17 +02:00
|
|
|
|
|
|
|
shared.attachedResources = async function(noteBody) {
|
|
|
|
if (!noteBody) return {};
|
|
|
|
const resourceIds = await Note.linkedItemIdsByType(BaseModel.TYPE_RESOURCE, noteBody);
|
|
|
|
|
|
|
|
const output = {};
|
|
|
|
for (let i = 0; i < resourceIds.length; i++) {
|
|
|
|
const id = resourceIds[i];
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2019-03-08 19:14:17 +02:00
|
|
|
if (resourceCache_[id]) {
|
|
|
|
output[id] = resourceCache_[id];
|
|
|
|
} else {
|
|
|
|
const resource = await Resource.load(id);
|
2019-05-22 16:56:07 +02:00
|
|
|
const localState = await Resource.localState(resource);
|
|
|
|
|
|
|
|
const o = {
|
|
|
|
item: resource,
|
|
|
|
localState: localState,
|
|
|
|
};
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
// eslint-disable-next-line require-atomic-updates
|
2019-05-22 16:56:07 +02:00
|
|
|
resourceCache_[id] = o;
|
|
|
|
output[id] = o;
|
2019-03-08 19:14:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2019-03-08 19:14:17 +02:00
|
|
|
|
2017-11-05 02:49:23 +02:00
|
|
|
shared.isModified = function(comp) {
|
|
|
|
if (!comp.state.note || !comp.state.lastSavedNote) return false;
|
2020-03-14 01:46:14 +02:00
|
|
|
const diff = BaseModel.diffObjects(comp.state.lastSavedNote, comp.state.note);
|
2017-11-05 02:49:23 +02:00
|
|
|
delete diff.type_;
|
|
|
|
return !!Object.getOwnPropertyNames(diff).length;
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-05 02:49:23 +02:00
|
|
|
|
|
|
|
shared.initState = async function(comp) {
|
2020-03-06 20:49:30 +02:00
|
|
|
const isProvisionalNote = comp.props.provisionalNoteIds.includes(comp.props.noteId);
|
|
|
|
|
|
|
|
const note = await Note.load(comp.props.noteId);
|
2017-11-05 02:49:23 +02:00
|
|
|
let mode = 'view';
|
2020-03-06 20:49:30 +02:00
|
|
|
|
2020-06-04 19:40:44 +02:00
|
|
|
if (isProvisionalNote && !comp.props.sharedData) {
|
2017-11-05 02:49:23 +02:00
|
|
|
mode = 'edit';
|
2020-01-08 19:42:28 +02:00
|
|
|
comp.scheduleFocusUpdate();
|
2017-11-05 02:49:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const folder = Folder.byId(comp.props.folders, note.parent_id);
|
|
|
|
|
|
|
|
comp.setState({
|
|
|
|
lastSavedNote: Object.assign({}, note),
|
|
|
|
note: note,
|
|
|
|
mode: mode,
|
|
|
|
folder: folder,
|
|
|
|
isLoading: false,
|
2018-07-20 11:04:25 +02:00
|
|
|
fromShare: comp.props.sharedData ? true : false,
|
2019-03-08 19:14:17 +02:00
|
|
|
noteResources: await shared.attachedResources(note ? note.body : ''),
|
2017-11-05 02:49:23 +02:00
|
|
|
});
|
2017-11-07 01:56:33 +02:00
|
|
|
|
2020-06-04 19:40:44 +02:00
|
|
|
|
2018-07-20 11:04:25 +02:00
|
|
|
if (comp.props.sharedData) {
|
2020-06-04 19:40:44 +02:00
|
|
|
if (comp.props.sharedData.title) {
|
|
|
|
this.noteComponent_change(comp, 'title', comp.props.sharedData.title);
|
|
|
|
}
|
|
|
|
if (comp.props.sharedData.text) {
|
|
|
|
this.noteComponent_change(comp, 'body', comp.props.sharedData.text);
|
|
|
|
}
|
|
|
|
if (comp.props.sharedData.resources) {
|
|
|
|
for (let i = 0; i < comp.props.sharedData.resources.length; i++) {
|
|
|
|
const resource = comp.props.sharedData.resources[i];
|
|
|
|
reg.logger().info(`about to attach resource ${JSON.stringify(resource)}`);
|
|
|
|
await comp.attachFile({
|
|
|
|
uri: resource.uri,
|
|
|
|
type: resource.mimeType,
|
|
|
|
fileName: resource.name,
|
|
|
|
}, null);
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 11:04:25 +02:00
|
|
|
}
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
// eslint-disable-next-line require-atomic-updates
|
2020-03-06 20:49:30 +02:00
|
|
|
comp.lastLoadedNoteId_ = note.id;
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-05 02:49:23 +02:00
|
|
|
|
|
|
|
shared.toggleIsTodo_onPress = function(comp) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const newNote = Note.toggleIsTodo(comp.state.note);
|
|
|
|
const newState = { note: newNote };
|
2017-11-05 02:49:23 +02:00
|
|
|
comp.setState(newState);
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-05 02:49:23 +02:00
|
|
|
|
2019-03-08 19:14:17 +02:00
|
|
|
shared.toggleCheckbox = function(ipcMessage, noteBody) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const newBody = noteBody.split('\n');
|
2019-03-08 19:14:17 +02:00
|
|
|
const p = ipcMessage.split(':');
|
|
|
|
const lineIndex = Number(p[p.length - 1]);
|
|
|
|
if (lineIndex >= newBody.length) {
|
2019-04-20 20:00:22 +02:00
|
|
|
reg.logger().warn('Checkbox line out of bounds: ', ipcMessage);
|
2019-03-08 19:14:17 +02:00
|
|
|
return newBody.join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
let line = newBody[lineIndex];
|
|
|
|
|
2019-04-20 20:00:22 +02:00
|
|
|
const noCrossIndex = line.trim().indexOf('- [ ] ');
|
|
|
|
let crossIndex = line.trim().indexOf('- [x] ');
|
|
|
|
if (crossIndex < 0) crossIndex = line.trim().indexOf('- [X] ');
|
|
|
|
|
|
|
|
if (noCrossIndex < 0 && crossIndex < 0) {
|
|
|
|
reg.logger().warn('Could not find matching checkbox for message: ', ipcMessage);
|
|
|
|
return newBody.join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
let isCrossLine = false;
|
|
|
|
|
|
|
|
if (noCrossIndex >= 0 && crossIndex >= 0) {
|
|
|
|
isCrossLine = crossIndex < noCrossIndex;
|
|
|
|
} else {
|
|
|
|
isCrossLine = crossIndex >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isCrossLine) {
|
2019-03-08 19:14:17 +02:00
|
|
|
line = line.replace(/- \[ \] /, '- [x] ');
|
2019-07-29 15:43:53 +02:00
|
|
|
} else {
|
2019-03-08 19:14:17 +02:00
|
|
|
line = line.replace(/- \[x\] /i, '- [ ] ');
|
|
|
|
}
|
|
|
|
|
|
|
|
newBody[lineIndex] = line;
|
2019-07-29 15:43:53 +02:00
|
|
|
return newBody.join('\n');
|
|
|
|
};
|
2019-03-08 19:14:17 +02:00
|
|
|
|
2019-05-22 16:56:07 +02:00
|
|
|
shared.installResourceHandling = function(refreshResourceHandler) {
|
|
|
|
ResourceFetcher.instance().on('downloadComplete', refreshResourceHandler);
|
|
|
|
ResourceFetcher.instance().on('downloadStarted', refreshResourceHandler);
|
|
|
|
DecryptionWorker.instance().on('resourceDecrypted', refreshResourceHandler);
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2019-05-22 16:56:07 +02:00
|
|
|
|
|
|
|
shared.uninstallResourceHandling = function(refreshResourceHandler) {
|
|
|
|
ResourceFetcher.instance().off('downloadComplete', refreshResourceHandler);
|
|
|
|
ResourceFetcher.instance().off('downloadStarted', refreshResourceHandler);
|
|
|
|
DecryptionWorker.instance().off('resourceDecrypted', refreshResourceHandler);
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2019-05-22 16:56:07 +02:00
|
|
|
|
2018-07-20 11:04:25 +02:00
|
|
|
module.exports = shared;
|