2018-03-09 22:59:12 +02:00
|
|
|
const Note = require('lib/models/Note.js');
|
|
|
|
const Folder = require('lib/models/Folder.js');
|
|
|
|
const ArrayUtils = require('lib/ArrayUtils.js');
|
2017-10-07 18:30:27 +02:00
|
|
|
|
|
|
|
const defaultState = {
|
|
|
|
notes: [],
|
2018-03-09 22:59:12 +02:00
|
|
|
notesSource: '',
|
2017-10-07 18:30:27 +02:00
|
|
|
notesParentType: null,
|
|
|
|
folders: [],
|
|
|
|
tags: [],
|
2017-12-14 19:58:10 +02:00
|
|
|
masterKeys: [],
|
2017-12-22 20:50:27 +02:00
|
|
|
notLoadedMasterKeys: [],
|
2017-10-23 22:34:04 +02:00
|
|
|
searches: [],
|
2017-11-22 20:35:31 +02:00
|
|
|
selectedNoteIds: [],
|
2019-09-09 19:16:00 +02:00
|
|
|
selectedNoteHash: '',
|
2017-10-07 18:30:27 +02:00
|
|
|
selectedFolderId: null,
|
|
|
|
selectedTagId: null,
|
2017-10-23 22:34:04 +02:00
|
|
|
selectedSearchId: null,
|
2018-03-09 22:59:12 +02:00
|
|
|
selectedItemType: 'note',
|
2019-01-29 20:32:52 +02:00
|
|
|
lastSelectedNotesIds: {
|
|
|
|
Folder: {},
|
|
|
|
Tag: {},
|
|
|
|
Search: {},
|
|
|
|
},
|
2017-10-07 18:30:27 +02:00
|
|
|
showSideMenu: false,
|
|
|
|
screens: {},
|
|
|
|
historyCanGoBack: false,
|
|
|
|
syncStarted: false,
|
|
|
|
syncReport: {},
|
2018-03-09 22:59:12 +02:00
|
|
|
searchQuery: '',
|
2017-10-07 18:30:27 +02:00
|
|
|
settings: {},
|
2018-07-20 11:04:25 +02:00
|
|
|
sharedData: null,
|
2018-03-09 22:59:12 +02:00
|
|
|
appState: 'starting',
|
2017-12-05 20:56:39 +02:00
|
|
|
hasDisabledSyncItems: false,
|
2020-03-13 19:42:50 +02:00
|
|
|
hasDisabledEncryptionItems: false,
|
2018-11-08 00:52:31 +02:00
|
|
|
customCss: '',
|
2019-07-20 23:13:10 +02:00
|
|
|
templates: [],
|
2018-05-09 11:49:31 +02:00
|
|
|
collapsedFolderIds: [],
|
2018-05-25 14:30:27 +02:00
|
|
|
clipperServer: {
|
|
|
|
startState: 'idle',
|
|
|
|
port: null,
|
|
|
|
},
|
2018-06-10 18:43:24 +02:00
|
|
|
decryptionWorker: {
|
|
|
|
state: 'idle',
|
|
|
|
itemIndex: 0,
|
|
|
|
itemCount: 0,
|
|
|
|
},
|
2018-11-14 00:25:23 +02:00
|
|
|
selectedNoteTags: [],
|
|
|
|
resourceFetcher: {
|
|
|
|
toFetchCount: 0,
|
|
|
|
},
|
2020-03-15 11:40:57 +02:00
|
|
|
backwardHistoryNotes: [],
|
|
|
|
forwardHistoryNotes: [],
|
2019-04-01 21:43:13 +02:00
|
|
|
plugins: {},
|
2020-02-29 14:39:15 +02:00
|
|
|
provisionalNoteIds: [],
|
2020-03-10 01:24:57 +02:00
|
|
|
editorNoteStatuses: {},
|
2017-10-07 18:30:27 +02:00
|
|
|
};
|
|
|
|
|
2018-02-22 20:58:15 +02:00
|
|
|
const stateUtils = {};
|
|
|
|
|
2019-07-14 00:55:28 +02:00
|
|
|
const derivedStateCache_ = {};
|
|
|
|
|
|
|
|
// Allows, for a given state, to return the same derived
|
|
|
|
// objects, to prevent unecessary updates on calling components.
|
|
|
|
const cacheEnabledOutput = (key, output) => {
|
2019-09-19 23:51:18 +02:00
|
|
|
key = `${key}_${JSON.stringify(output)}`;
|
2019-07-14 00:55:28 +02:00
|
|
|
if (derivedStateCache_[key]) return derivedStateCache_[key];
|
|
|
|
|
|
|
|
derivedStateCache_[key] = output;
|
|
|
|
return derivedStateCache_[key];
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2019-07-14 00:55:28 +02:00
|
|
|
|
2018-02-22 20:58:15 +02:00
|
|
|
stateUtils.notesOrder = function(stateSettings) {
|
2019-07-29 15:43:53 +02:00
|
|
|
return cacheEnabledOutput('notesOrder', [
|
|
|
|
{
|
|
|
|
by: stateSettings['notes.sortOrder.field'],
|
|
|
|
dir: stateSettings['notes.sortOrder.reverse'] ? 'DESC' : 'ASC',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
};
|
2018-02-22 20:58:15 +02:00
|
|
|
|
2019-03-02 19:35:57 +02:00
|
|
|
stateUtils.foldersOrder = function(stateSettings) {
|
2019-07-29 15:43:53 +02:00
|
|
|
return cacheEnabledOutput('foldersOrder', [
|
|
|
|
{
|
|
|
|
by: stateSettings['folders.sortOrder.field'],
|
|
|
|
dir: stateSettings['folders.sortOrder.reverse'] ? 'DESC' : 'ASC',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
};
|
2019-03-02 19:35:57 +02:00
|
|
|
|
2019-01-29 20:32:52 +02:00
|
|
|
stateUtils.parentItem = function(state) {
|
|
|
|
const t = state.notesParentType;
|
|
|
|
let id = null;
|
|
|
|
if (t === 'Folder') id = state.selectedFolderId;
|
|
|
|
if (t === 'Tag') id = state.selectedTagId;
|
|
|
|
if (t === 'Search') id = state.selectedSearchId;
|
|
|
|
if (!t || !id) return null;
|
|
|
|
return { type: t, id: id };
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2019-01-29 20:32:52 +02:00
|
|
|
|
|
|
|
stateUtils.lastSelectedNoteIds = function(state) {
|
|
|
|
const parent = stateUtils.parentItem(state);
|
|
|
|
if (!parent) return [];
|
|
|
|
const output = state.lastSelectedNotesIds[parent.type][parent.id];
|
|
|
|
return output ? output : [];
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2019-01-29 20:32:52 +02:00
|
|
|
|
2020-03-15 11:40:57 +02:00
|
|
|
stateUtils.getLastSeenNote = function(state) {
|
|
|
|
const selectedNoteIds = state.selectedNoteIds;
|
|
|
|
const notes = state.notes;
|
|
|
|
if (selectedNoteIds != null && selectedNoteIds.length > 0) {
|
|
|
|
const currNote = notes.find(note => note.id === selectedNoteIds[0]);
|
|
|
|
if (currNote != null) {
|
|
|
|
return {
|
|
|
|
id: currNote.id,
|
|
|
|
parent_id: currNote.parent_id,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-14 19:58:10 +02:00
|
|
|
function arrayHasEncryptedItems(array) {
|
|
|
|
for (let i = 0; i < array.length; i++) {
|
2019-07-29 15:43:53 +02:00
|
|
|
if (array[i].encryption_applied) return true;
|
2017-12-14 19:58:10 +02:00
|
|
|
}
|
2019-07-29 15:43:53 +02:00
|
|
|
return false;
|
2017-12-14 19:58:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function stateHasEncryptedItems(state) {
|
|
|
|
if (arrayHasEncryptedItems(state.notes)) return true;
|
|
|
|
if (arrayHasEncryptedItems(state.folders)) return true;
|
|
|
|
if (arrayHasEncryptedItems(state.tags)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 11:49:31 +02:00
|
|
|
function folderSetCollapsed(state, action) {
|
|
|
|
const collapsedFolderIds = state.collapsedFolderIds.slice();
|
|
|
|
const idx = collapsedFolderIds.indexOf(action.id);
|
|
|
|
|
|
|
|
if (action.collapsed) {
|
|
|
|
if (idx >= 0) return state;
|
|
|
|
collapsedFolderIds.push(action.id);
|
|
|
|
} else {
|
|
|
|
if (idx < 0) return state;
|
|
|
|
collapsedFolderIds.splice(idx, 1);
|
|
|
|
}
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const newState = Object.assign({}, state);
|
2018-05-09 11:49:31 +02:00
|
|
|
newState.collapsedFolderIds = collapsedFolderIds;
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
2017-11-08 23:39:07 +02:00
|
|
|
// When deleting a note, tag or folder
|
|
|
|
function handleItemDelete(state, action) {
|
|
|
|
const map = {
|
2020-01-18 15:53:00 +02:00
|
|
|
FOLDER_DELETE: ['folders', 'selectedFolderId', true],
|
|
|
|
NOTE_DELETE: ['notes', 'selectedNoteIds', false],
|
|
|
|
TAG_DELETE: ['tags', 'selectedTagId', true],
|
|
|
|
SEARCH_DELETE: ['searches', 'selectedSearchId', true],
|
2017-11-08 23:39:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const listKey = map[action.type][0];
|
|
|
|
const selectedItemKey = map[action.type][1];
|
2020-01-18 15:53:00 +02:00
|
|
|
const isSingular = map[action.type][2];
|
|
|
|
|
|
|
|
const selectedItemKeys = isSingular ? [state[selectedItemKey]] : state[selectedItemKey];
|
|
|
|
const isSelected = selectedItemKeys.includes(action.id);
|
2017-10-29 18:22:53 +02:00
|
|
|
|
|
|
|
const items = state[listKey];
|
2020-03-14 01:46:14 +02:00
|
|
|
const newItems = [];
|
2020-01-18 15:53:00 +02:00
|
|
|
let newSelectedIndexes = [];
|
|
|
|
|
2017-10-29 18:22:53 +02:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const item = items[i];
|
2020-01-18 15:53:00 +02:00
|
|
|
if (isSelected) {
|
|
|
|
// the selected item is deleted so select the following item
|
|
|
|
// if multiple items are selected then just use the first one
|
|
|
|
if (selectedItemKeys[0] == item.id) {
|
|
|
|
newSelectedIndexes.push(newItems.length);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// the selected item/s is not deleted so keep it selected
|
|
|
|
if (selectedItemKeys.includes(item.id)) {
|
|
|
|
newSelectedIndexes.push(newItems.length);
|
|
|
|
}
|
|
|
|
}
|
2017-11-08 23:39:07 +02:00
|
|
|
if (item.id == action.id) {
|
2017-10-29 18:22:53 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-11-08 23:39:07 +02:00
|
|
|
newItems.push(item);
|
2017-10-29 18:22:53 +02:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:53:00 +02:00
|
|
|
if (newItems.length == 0) {
|
|
|
|
newSelectedIndexes = []; // no remaining items so no selection
|
|
|
|
|
|
|
|
} else if (newSelectedIndexes.length == 0) {
|
|
|
|
newSelectedIndexes.push(0); // no selection exists so select the top
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// when the items at end of list are deleted then select the end
|
|
|
|
for (let i = 0; i < newSelectedIndexes.length; i++) {
|
|
|
|
if (newSelectedIndexes[i] >= newItems.length) {
|
|
|
|
newSelectedIndexes = [newItems.length - 1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const newState = Object.assign({}, state);
|
2017-10-29 18:22:53 +02:00
|
|
|
newState[listKey] = newItems;
|
|
|
|
|
2020-03-15 11:40:57 +02:00
|
|
|
if (listKey === 'notes') {
|
|
|
|
newState.backwardHistoryNotes = newState.backwardHistoryNotes.filter(note => note.id != action.id);
|
|
|
|
newState.forwardHistoryNotes = newState.forwardHistoryNotes.filter(note => note.id != action.id);
|
|
|
|
}
|
|
|
|
if (listKey === 'folders') {
|
|
|
|
newState.backwardHistoryNotes = newState.backwardHistoryNotes.filter(note => note.parent_id != action.id);
|
|
|
|
newState.forwardHistoryNotes = newState.forwardHistoryNotes.filter(note => note.parent_id != action.id);
|
|
|
|
}
|
|
|
|
|
2020-01-18 15:53:00 +02:00
|
|
|
const newIds = [];
|
|
|
|
for (let i = 0; i < newSelectedIndexes.length; i++) {
|
|
|
|
newIds.push(newItems[newSelectedIndexes[i]].id);
|
2017-10-29 18:22:53 +02:00
|
|
|
}
|
2020-01-18 15:53:00 +02:00
|
|
|
newState[selectedItemKey] = isSingular ? newIds[0] : newIds;
|
2017-10-29 18:22:53 +02:00
|
|
|
|
2020-01-18 15:53:00 +02:00
|
|
|
if ((newIds.length == 0) && newState.notesParentType !== 'Folder') {
|
2018-03-09 22:59:12 +02:00
|
|
|
newState.notesParentType = 'Folder';
|
2017-11-12 19:37:04 +02:00
|
|
|
}
|
|
|
|
|
2017-10-29 18:22:53 +02:00
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
2018-11-08 00:16:05 +02:00
|
|
|
function updateOneItem(state, action, keyName = '') {
|
2017-12-14 19:58:10 +02:00
|
|
|
let itemsKey = null;
|
2020-03-14 01:57:34 +02:00
|
|
|
if (keyName) { itemsKey = keyName; } else {
|
2018-11-08 00:16:05 +02:00
|
|
|
if (action.type === 'TAG_UPDATE_ONE') itemsKey = 'tags';
|
|
|
|
if (action.type === 'FOLDER_UPDATE_ONE') itemsKey = 'folders';
|
|
|
|
if (action.type === 'MASTERKEY_UPDATE_ONE') itemsKey = 'masterKeys';
|
|
|
|
}
|
2017-12-14 19:58:10 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const newItems = state[itemsKey].splice(0);
|
|
|
|
const item = action.item;
|
2017-10-22 19:12:16 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
let found = false;
|
2017-10-22 19:12:16 +02:00
|
|
|
for (let i = 0; i < newItems.length; i++) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const n = newItems[i];
|
2017-10-22 19:12:16 +02:00
|
|
|
if (n.id == item.id) {
|
|
|
|
newItems[i] = Object.assign(newItems[i], item);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) newItems.push(item);
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const newState = Object.assign({}, state);
|
2017-10-22 19:12:16 +02:00
|
|
|
|
2017-12-14 19:58:10 +02:00
|
|
|
newState[itemsKey] = newItems;
|
|
|
|
|
2017-10-22 19:12:16 +02:00
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
2017-10-23 23:48:29 +02:00
|
|
|
function defaultNotesParentType(state, exclusion) {
|
|
|
|
let newNotesParentType = null;
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (exclusion !== 'Folder' && state.selectedFolderId) {
|
|
|
|
newNotesParentType = 'Folder';
|
|
|
|
} else if (exclusion !== 'Tag' && state.selectedTagId) {
|
|
|
|
newNotesParentType = 'Tag';
|
|
|
|
} else if (exclusion !== 'Search' && state.selectedSearchId) {
|
|
|
|
newNotesParentType = 'Search';
|
2017-10-23 23:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return newNotesParentType;
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:17:09 +02:00
|
|
|
function changeSelectedFolder(state, action, options = null) {
|
|
|
|
if (!options) options = {};
|
|
|
|
|
2020-03-15 11:40:12 +02:00
|
|
|
const newState = Object.assign({}, state);
|
2020-03-15 11:40:57 +02:00
|
|
|
|
|
|
|
// Save the last seen note so that back will return to it.
|
|
|
|
if (action.type === 'FOLDER_SELECT' && action.historyAction == 'goto') {
|
|
|
|
const backwardHistoryNotes = newState.backwardHistoryNotes.slice();
|
|
|
|
let forwardHistoryNotes = newState.forwardHistoryNotes.slice();
|
|
|
|
|
|
|
|
// Don't update history if going to the same note again.
|
|
|
|
const lastSeenNote = stateUtils.getLastSeenNote(state);
|
|
|
|
if (lastSeenNote != null && action.id != lastSeenNote.id) {
|
|
|
|
forwardHistoryNotes = [];
|
|
|
|
backwardHistoryNotes.push(Object.assign({}, lastSeenNote));
|
|
|
|
}
|
|
|
|
|
|
|
|
newState.backwardHistoryNotes = backwardHistoryNotes;
|
|
|
|
newState.forwardHistoryNotes = forwardHistoryNotes;
|
|
|
|
}
|
|
|
|
|
2018-11-08 02:58:06 +02:00
|
|
|
newState.selectedFolderId = 'folderId' in action ? action.folderId : action.id;
|
|
|
|
if (!newState.selectedFolderId) {
|
|
|
|
newState.notesParentType = defaultNotesParentType(state, 'Folder');
|
|
|
|
} else {
|
|
|
|
newState.notesParentType = 'Folder';
|
|
|
|
}
|
2019-02-07 20:17:09 +02:00
|
|
|
|
|
|
|
if (newState.selectedFolderId === state.selectedFolderId && newState.notesParentType === state.notesParentType) return state;
|
|
|
|
|
2019-04-04 08:48:33 +02:00
|
|
|
if (options.clearSelectedNoteIds) newState.selectedNoteIds = [];
|
2019-02-07 20:17:09 +02:00
|
|
|
|
2018-11-08 02:58:06 +02:00
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
2019-01-29 20:32:52 +02:00
|
|
|
function recordLastSelectedNoteIds(state, noteIds) {
|
|
|
|
const newOnes = Object.assign({}, state.lastSelectedNotesIds);
|
|
|
|
const parent = stateUtils.parentItem(state);
|
|
|
|
if (!parent) return state;
|
|
|
|
|
|
|
|
newOnes[parent.type][parent.id] = noteIds.slice();
|
|
|
|
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
lastSelectedNotesIds: newOnes,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:17:09 +02:00
|
|
|
function changeSelectedNotes(state, action, options = null) {
|
|
|
|
if (!options) options = {};
|
|
|
|
|
2018-11-08 02:58:06 +02:00
|
|
|
let noteIds = [];
|
|
|
|
if (action.id) noteIds = [action.id];
|
|
|
|
if (action.ids) noteIds = action.ids;
|
|
|
|
if (action.noteId) noteIds = [action.noteId];
|
|
|
|
|
2017-11-22 20:35:31 +02:00
|
|
|
let newState = Object.assign({}, state);
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (action.type === 'NOTE_SELECT') {
|
2017-11-22 20:35:31 +02:00
|
|
|
newState.selectedNoteIds = noteIds;
|
2019-09-09 19:16:00 +02:00
|
|
|
newState.selectedNoteHash = action.hash ? action.hash : '';
|
2020-03-15 11:40:57 +02:00
|
|
|
|
|
|
|
const backwardHistoryNotes = newState.backwardHistoryNotes.slice();
|
|
|
|
let forwardHistoryNotes = newState.forwardHistoryNotes.slice();
|
|
|
|
|
|
|
|
// The historyAction property is only used for user-initiated actions and tells how
|
|
|
|
// the history stack should be handled. That property should not be present for
|
|
|
|
// programmatic navigation. Possible values are:
|
|
|
|
// - "goto": When going to a note, but not via the back/forward arrows.
|
|
|
|
// - "pop": When clicking on the Back arrow
|
|
|
|
// - "push": When clicking on the Forward arrow
|
|
|
|
const lastSeenNote = stateUtils.getLastSeenNote(state);
|
|
|
|
if (action.historyAction == 'goto' && lastSeenNote != null && action.id != lastSeenNote.id) {
|
|
|
|
forwardHistoryNotes = [];
|
|
|
|
backwardHistoryNotes.push(Object.assign({}, lastSeenNote));
|
|
|
|
} else if (action.historyAction === 'pop' && lastSeenNote != null) {
|
|
|
|
if (forwardHistoryNotes.length === 0 || lastSeenNote.id != forwardHistoryNotes[forwardHistoryNotes.length - 1].id) {
|
|
|
|
forwardHistoryNotes.push(Object.assign({}, lastSeenNote));
|
|
|
|
}
|
|
|
|
backwardHistoryNotes.pop();
|
|
|
|
} else if (action.historyAction === 'push' && lastSeenNote != null) {
|
|
|
|
if (backwardHistoryNotes.length === 0 || lastSeenNote.id != backwardHistoryNotes[backwardHistoryNotes.length - 1].id) {
|
|
|
|
backwardHistoryNotes.push(Object.assign({}, lastSeenNote));
|
|
|
|
}
|
|
|
|
forwardHistoryNotes.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
newState.backwardHistoryNotes = backwardHistoryNotes;
|
|
|
|
newState.forwardHistoryNotes = forwardHistoryNotes;
|
|
|
|
|
|
|
|
return newState;
|
|
|
|
|
2019-01-29 20:32:52 +02:00
|
|
|
} else if (action.type === 'NOTE_SELECT_ADD') {
|
2017-11-22 20:35:31 +02:00
|
|
|
if (!noteIds.length) return state;
|
|
|
|
newState.selectedNoteIds = ArrayUtils.unique(newState.selectedNoteIds.concat(noteIds));
|
2019-01-29 20:32:52 +02:00
|
|
|
} else if (action.type === 'NOTE_SELECT_REMOVE') {
|
2017-11-22 20:35:31 +02:00
|
|
|
if (!noteIds.length) return state; // Nothing to unselect
|
|
|
|
if (state.selectedNoteIds.length <= 1) return state; // Cannot unselect the last note
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const newSelectedNoteIds = [];
|
2017-11-22 20:35:31 +02:00
|
|
|
for (let i = 0; i < newState.selectedNoteIds.length; i++) {
|
|
|
|
const id = newState.selectedNoteIds[i];
|
|
|
|
if (noteIds.indexOf(id) >= 0) continue;
|
|
|
|
newSelectedNoteIds.push(id);
|
|
|
|
}
|
|
|
|
newState.selectedNoteIds = newSelectedNoteIds;
|
2019-01-29 20:32:52 +02:00
|
|
|
} else if (action.type === 'NOTE_SELECT_TOGGLE') {
|
2017-11-22 20:35:31 +02:00
|
|
|
if (!noteIds.length) return state;
|
|
|
|
|
|
|
|
if (newState.selectedNoteIds.indexOf(noteIds[0]) >= 0) {
|
2018-03-09 22:59:12 +02:00
|
|
|
newState = changeSelectedNotes(state, { type: 'NOTE_SELECT_REMOVE', id: noteIds[0] });
|
2017-11-22 20:35:31 +02:00
|
|
|
} else {
|
2018-03-09 22:59:12 +02:00
|
|
|
newState = changeSelectedNotes(state, { type: 'NOTE_SELECT_ADD', id: noteIds[0] });
|
2017-11-22 20:35:31 +02:00
|
|
|
}
|
2019-07-29 15:43:53 +02:00
|
|
|
} else {
|
2019-01-29 20:32:52 +02:00
|
|
|
throw new Error('Unreachable');
|
2017-11-22 20:35:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 20:32:52 +02:00
|
|
|
newState = recordLastSelectedNoteIds(newState, newState.selectedNoteIds);
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
return newState;
|
2017-11-22 20:35:31 +02:00
|
|
|
}
|
|
|
|
|
2018-11-08 00:16:05 +02:00
|
|
|
function removeItemFromArray(array, property, value) {
|
|
|
|
for (let i = 0; i !== array.length; ++i) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const currentItem = array[i];
|
2018-11-08 00:16:05 +02:00
|
|
|
if (currentItem[property] === value) {
|
|
|
|
array.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2017-10-07 18:30:27 +02:00
|
|
|
const reducer = (state = defaultState, action) => {
|
|
|
|
let newState = state;
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (action.type) {
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'NOTE_SELECT':
|
|
|
|
case 'NOTE_SELECT_ADD':
|
|
|
|
case 'NOTE_SELECT_REMOVE':
|
|
|
|
case 'NOTE_SELECT_TOGGLE':
|
|
|
|
newState = changeSelectedNotes(state, action);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_SELECT_EXTEND':
|
|
|
|
{
|
2017-10-07 23:01:03 +02:00
|
|
|
newState = Object.assign({}, state);
|
2017-11-22 20:35:31 +02:00
|
|
|
|
|
|
|
if (!newState.selectedNoteIds.length) {
|
|
|
|
newState.selectedNoteIds = [action.id];
|
|
|
|
} else {
|
|
|
|
const selectRangeId1 = state.selectedNoteIds[state.selectedNoteIds.length - 1];
|
|
|
|
const selectRangeId2 = action.id;
|
|
|
|
if (selectRangeId1 === selectRangeId2) return state;
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const newSelectedNoteIds = state.selectedNoteIds.slice();
|
2017-11-22 20:35:31 +02:00
|
|
|
let selectionStarted = false;
|
|
|
|
for (let i = 0; i < state.notes.length; i++) {
|
|
|
|
const id = state.notes[i].id;
|
|
|
|
|
|
|
|
if (!selectionStarted && (id === selectRangeId1 || id === selectRangeId2)) {
|
|
|
|
selectionStarted = true;
|
|
|
|
if (newSelectedNoteIds.indexOf(id) < 0) newSelectedNoteIds.push(id);
|
|
|
|
continue;
|
|
|
|
} else if (selectionStarted && (id === selectRangeId1 || id === selectRangeId2)) {
|
|
|
|
if (newSelectedNoteIds.indexOf(id) < 0) newSelectedNoteIds.push(id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectionStarted && newSelectedNoteIds.indexOf(id) < 0) {
|
|
|
|
newSelectedNoteIds.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newState.selectedNoteIds = newSelectedNoteIds;
|
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-10-07 23:01:03 +02:00
|
|
|
|
2020-02-04 23:55:06 +02:00
|
|
|
case 'NOTE_SELECT_ALL':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.selectedNoteIds = newState.notes.map(n => n.id);
|
|
|
|
break;
|
|
|
|
|
2020-03-14 00:41:56 +02:00
|
|
|
case 'NOTE_SELECT_ALL_TOGGLE': {
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
const allSelected = state.notes.every(n => state.selectedNoteIds.includes(n.id));
|
2020-03-14 01:57:34 +02:00
|
|
|
if (allSelected) {
|
2020-03-14 00:41:56 +02:00
|
|
|
newState.selectedNoteIds = [];
|
2020-03-14 01:57:34 +02:00
|
|
|
} else {
|
2020-03-14 00:41:56 +02:00
|
|
|
newState.selectedNoteIds = newState.notes.map(n => n.id);
|
2020-03-14 01:57:34 +02:00
|
|
|
}
|
2020-03-14 00:41:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-22 13:25:16 +02:00
|
|
|
case 'SMART_FILTER_SELECT':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.notesParentType = 'SmartFilter';
|
|
|
|
newState.selectedSmartFilterId = action.id;
|
|
|
|
break;
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'FOLDER_SELECT':
|
|
|
|
newState = changeSelectedFolder(state, action, { clearSelectedNoteIds: true });
|
|
|
|
break;
|
2018-11-08 02:58:06 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'FOLDER_AND_NOTE_SELECT':
|
|
|
|
{
|
2020-03-15 11:40:57 +02:00
|
|
|
newState = changeSelectedFolder(state, action);
|
2019-07-29 15:43:53 +02:00
|
|
|
const noteSelectAction = Object.assign({}, action, { type: 'NOTE_SELECT' });
|
2020-03-15 11:40:57 +02:00
|
|
|
newState = changeSelectedNotes(newState, noteSelectAction);
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SETTING_UPDATE_ALL':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.settings = action.settings;
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SETTING_UPDATE_ONE':
|
|
|
|
{
|
2017-10-07 18:30:27 +02:00
|
|
|
newState = Object.assign({}, state);
|
2020-03-14 01:46:14 +02:00
|
|
|
const newSettings = Object.assign({}, state.settings);
|
2017-10-07 18:30:27 +02:00
|
|
|
newSettings[action.key] = action.value;
|
|
|
|
newState.settings = newSettings;
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2020-03-10 01:24:57 +02:00
|
|
|
case 'NOTE_PROVISIONAL_FLAG_CLEAR':
|
|
|
|
{
|
|
|
|
const newIds = ArrayUtils.removeElement(state.provisionalNoteIds, action.id);
|
|
|
|
if (newIds !== state.provisionalNoteIds) {
|
|
|
|
newState = Object.assign({}, state, { provisionalNoteIds: newIds });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2017-10-07 18:30:27 +02:00
|
|
|
// Replace all the notes with the provided array
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'NOTE_UPDATE_ALL':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.notes = action.notes;
|
|
|
|
newState.notesSource = action.notesSource;
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
|
|
|
// Insert the note into the note list if it's new, or
|
|
|
|
// update it within the note array if it already exists.
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'NOTE_UPDATE_ONE':
|
|
|
|
{
|
2017-10-07 18:30:27 +02:00
|
|
|
const modNote = action.note;
|
|
|
|
|
2017-11-12 20:57:59 +02:00
|
|
|
const noteIsInFolder = function(note, folderId) {
|
|
|
|
if (note.is_conflict) return folderId === Folder.conflictFolderId();
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!('parent_id' in modNote) || note.parent_id == folderId) return true;
|
2017-11-12 20:57:59 +02:00
|
|
|
return false;
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-12 20:57:59 +02:00
|
|
|
|
2019-07-13 18:40:09 +02:00
|
|
|
let movedNotePreviousIndex = 0;
|
2017-10-15 13:38:22 +02:00
|
|
|
let noteFolderHasChanged = false;
|
2017-10-07 18:30:27 +02:00
|
|
|
let newNotes = state.notes.slice();
|
2020-03-14 01:46:14 +02:00
|
|
|
let found = false;
|
2017-10-07 18:30:27 +02:00
|
|
|
for (let i = 0; i < newNotes.length; i++) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const n = newNotes[i];
|
2017-10-07 18:30:27 +02:00
|
|
|
if (n.id == modNote.id) {
|
2017-10-15 13:38:22 +02:00
|
|
|
// Note is still in the same folder
|
2017-11-12 20:57:59 +02:00
|
|
|
if (noteIsInFolder(modNote, n.parent_id)) {
|
2017-10-07 18:30:27 +02:00
|
|
|
// Merge the properties that have changed (in modNote) into
|
|
|
|
// the object we already have.
|
|
|
|
newNotes[i] = Object.assign({}, newNotes[i]);
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
for (const n in modNote) {
|
2017-10-07 18:30:27 +02:00
|
|
|
if (!modNote.hasOwnProperty(n)) continue;
|
|
|
|
newNotes[i][n] = modNote[n];
|
|
|
|
}
|
2019-07-29 15:43:53 +02:00
|
|
|
} else {
|
|
|
|
// Note has moved to a different folder
|
2017-10-07 18:30:27 +02:00
|
|
|
newNotes.splice(i, 1);
|
2017-10-15 13:38:22 +02:00
|
|
|
noteFolderHasChanged = true;
|
2019-07-13 18:40:09 +02:00
|
|
|
movedNotePreviousIndex = i;
|
2017-10-07 18:30:27 +02:00
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 20:57:59 +02:00
|
|
|
// Note was not found - if the current folder is the same as the note folder,
|
|
|
|
// add it to it.
|
|
|
|
if (!found) {
|
|
|
|
if (noteIsInFolder(modNote, state.selectedFolderId)) {
|
|
|
|
newNotes.push(modNote);
|
|
|
|
}
|
|
|
|
}
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-10-09 21:35:13 +02:00
|
|
|
// newNotes = Note.sortNotes(newNotes, state.notesOrder, newState.settings.uncompletedTodosOnTop);
|
2018-02-22 20:58:15 +02:00
|
|
|
newNotes = Note.sortNotes(newNotes, stateUtils.notesOrder(state.settings), newState.settings.uncompletedTodosOnTop);
|
2017-10-07 18:30:27 +02:00
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.notes = newNotes;
|
2017-10-15 13:38:22 +02:00
|
|
|
|
|
|
|
if (noteFolderHasChanged) {
|
2019-07-13 18:40:09 +02:00
|
|
|
let newIndex = movedNotePreviousIndex;
|
2019-07-29 15:43:53 +02:00
|
|
|
if (newIndex >= newNotes.length) newIndex = newNotes.length - 1;
|
2019-07-13 18:40:09 +02:00
|
|
|
if (!newNotes.length) newIndex = -1;
|
|
|
|
newState.selectedNoteIds = newIndex >= 0 ? [newNotes[newIndex].id] : [];
|
2017-10-15 13:38:22 +02:00
|
|
|
}
|
2020-02-29 14:39:15 +02:00
|
|
|
|
|
|
|
if (action.provisional) {
|
|
|
|
newState.provisionalNoteIds.push(modNote.id);
|
|
|
|
} else {
|
|
|
|
const idx = newState.provisionalNoteIds.indexOf(modNote.id);
|
|
|
|
if (idx >= 0) {
|
|
|
|
const t = newState.provisionalNoteIds.slice();
|
|
|
|
t.splice(idx, 1);
|
|
|
|
newState.provisionalNoteIds = t;
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'NOTE_DELETE':
|
2020-02-29 14:39:15 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
newState = handleItemDelete(state, action);
|
|
|
|
|
|
|
|
const idx = newState.provisionalNoteIds.indexOf(action.id);
|
|
|
|
if (idx >= 0) {
|
|
|
|
const t = newState.provisionalNoteIds.slice();
|
|
|
|
t.splice(idx, 1);
|
|
|
|
newState.provisionalNoteIds = t;
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
break;
|
2017-11-08 23:39:07 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'TAG_DELETE':
|
|
|
|
newState = handleItemDelete(state, action);
|
|
|
|
newState.selectedNoteTags = removeItemFromArray(newState.selectedNoteTags.splice(0), 'id', action.id);
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'FOLDER_UPDATE_ALL':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.folders = action.items;
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'FOLDER_SET_COLLAPSED':
|
|
|
|
newState = folderSetCollapsed(state, action);
|
|
|
|
break;
|
2018-05-09 11:49:31 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'FOLDER_TOGGLE':
|
|
|
|
if (state.collapsedFolderIds.indexOf(action.id) >= 0) {
|
|
|
|
newState = folderSetCollapsed(state, Object.assign({ collapsed: false }, action));
|
|
|
|
} else {
|
|
|
|
newState = folderSetCollapsed(state, Object.assign({ collapsed: true }, action));
|
|
|
|
}
|
|
|
|
break;
|
2018-05-09 11:49:31 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'FOLDER_SET_COLLAPSED_ALL':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.collapsedFolderIds = action.ids.slice();
|
|
|
|
break;
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'TAG_UPDATE_ALL':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.tags = action.items;
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'TAG_SELECT':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.selectedTagId = action.id;
|
|
|
|
if (!action.id) {
|
|
|
|
newState.notesParentType = defaultNotesParentType(state, 'Tag');
|
|
|
|
} else {
|
|
|
|
newState.notesParentType = 'Tag';
|
|
|
|
}
|
2020-01-26 19:46:19 +02:00
|
|
|
newState.selectedNoteIds = [];
|
2019-07-30 09:35:42 +02:00
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'TAG_UPDATE_ONE':
|
2020-03-15 11:47:47 +02:00
|
|
|
{
|
|
|
|
// We only want to update the selected note tags if the tag belongs to the currently open note
|
|
|
|
const selectedNoteHasTag = !!state.selectedNoteTags.find(tag => tag.id === action.item.id);
|
|
|
|
newState = updateOneItem(state, action);
|
|
|
|
if (selectedNoteHasTag) newState = updateOneItem(newState, action, 'selectedNoteTags');
|
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
break;
|
2018-11-08 00:16:05 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'NOTE_TAG_REMOVE':
|
|
|
|
{
|
2018-11-08 00:16:05 +02:00
|
|
|
newState = updateOneItem(state, action, 'tags');
|
2020-03-14 01:46:14 +02:00
|
|
|
const tagRemoved = action.item;
|
2019-07-29 15:43:53 +02:00
|
|
|
newState.selectedNoteTags = removeItemFromArray(newState.selectedNoteTags.splice(0), 'id', tagRemoved.id);
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-11-08 00:16:05 +02:00
|
|
|
|
2020-03-10 01:24:57 +02:00
|
|
|
case 'EDITOR_NOTE_STATUS_SET':
|
|
|
|
|
|
|
|
{
|
|
|
|
const newStatuses = Object.assign({}, state.editorNoteStatuses);
|
|
|
|
newStatuses[action.id] = action.status;
|
|
|
|
newState = Object.assign({}, state, { editorNoteStatuses: newStatuses });
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'EDITOR_NOTE_STATUS_REMOVE':
|
|
|
|
|
|
|
|
{
|
|
|
|
const newStatuses = Object.assign({}, state.editorNoteStatuses);
|
|
|
|
delete newStatuses[action.id];
|
|
|
|
newState = Object.assign({}, state, { editorNoteStatuses: newStatuses });
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'FOLDER_UPDATE_ONE':
|
|
|
|
case 'MASTERKEY_UPDATE_ONE':
|
|
|
|
newState = updateOneItem(state, action);
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'FOLDER_DELETE':
|
|
|
|
newState = handleItemDelete(state, action);
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'MASTERKEY_UPDATE_ALL':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.masterKeys = action.items;
|
|
|
|
break;
|
2017-12-14 19:58:10 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'MASTERKEY_SET_NOT_LOADED':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.notLoadedMasterKeys = action.ids;
|
|
|
|
break;
|
2019-05-28 23:05:11 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'MASTERKEY_ADD_NOT_LOADED':
|
|
|
|
{
|
2017-12-22 20:50:27 +02:00
|
|
|
if (state.notLoadedMasterKeys.indexOf(action.id) < 0) {
|
2017-12-14 20:53:08 +02:00
|
|
|
newState = Object.assign({}, state);
|
2017-12-22 20:50:27 +02:00
|
|
|
const keys = newState.notLoadedMasterKeys.slice();
|
2017-12-14 21:39:13 +02:00
|
|
|
keys.push(action.id);
|
2017-12-22 20:50:27 +02:00
|
|
|
newState.notLoadedMasterKeys = keys;
|
2017-12-14 21:39:13 +02:00
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-12-14 21:39:13 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'MASTERKEY_REMOVE_NOT_LOADED':
|
|
|
|
{
|
2017-12-14 21:39:13 +02:00
|
|
|
const ids = action.id ? [action.id] : action.ids;
|
|
|
|
for (let i = 0; i < ids.length; i++) {
|
|
|
|
const id = ids[i];
|
2017-12-22 20:50:27 +02:00
|
|
|
const index = state.notLoadedMasterKeys.indexOf(id);
|
2017-12-14 21:39:13 +02:00
|
|
|
if (index >= 0) {
|
|
|
|
newState = Object.assign({}, state);
|
2017-12-22 20:50:27 +02:00
|
|
|
const keys = newState.notLoadedMasterKeys.slice();
|
2017-12-14 21:39:13 +02:00
|
|
|
keys.splice(index, 1);
|
2017-12-22 20:50:27 +02:00
|
|
|
newState.notLoadedMasterKeys = keys;
|
2017-12-14 21:39:13 +02:00
|
|
|
}
|
2017-12-14 20:53:08 +02:00
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-12-14 20:53:08 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SYNC_STARTED':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.syncStarted = true;
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SYNC_COMPLETED':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.syncStarted = false;
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SYNC_REPORT_UPDATE':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.syncReport = action.report;
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SEARCH_QUERY':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.searchQuery = action.query.trim();
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SEARCH_ADD':
|
|
|
|
{
|
2017-10-23 22:34:04 +02:00
|
|
|
newState = Object.assign({}, state);
|
2020-03-14 01:46:14 +02:00
|
|
|
const searches = newState.searches.slice();
|
2017-10-23 22:34:04 +02:00
|
|
|
searches.push(action.search);
|
|
|
|
newState.searches = searches;
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-10-23 22:34:04 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SEARCH_UPDATE':
|
|
|
|
{
|
2018-03-20 01:04:48 +02:00
|
|
|
newState = Object.assign({}, state);
|
2020-03-14 01:46:14 +02:00
|
|
|
const searches = newState.searches.slice();
|
2019-07-30 09:35:42 +02:00
|
|
|
let found = false;
|
2018-03-20 01:04:48 +02:00
|
|
|
for (let i = 0; i < searches.length; i++) {
|
|
|
|
if (searches[i].id === action.search.id) {
|
|
|
|
searches[i] = Object.assign({}, action.search);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) searches.push(action.search);
|
|
|
|
|
|
|
|
if (!action.search.query_pattern) {
|
|
|
|
newState.notesParentType = defaultNotesParentType(state, 'Search');
|
|
|
|
} else {
|
|
|
|
newState.notesParentType = 'Search';
|
|
|
|
}
|
|
|
|
|
|
|
|
newState.searches = searches;
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-03-20 01:04:48 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SEARCH_DELETE':
|
|
|
|
newState = handleItemDelete(state, action);
|
|
|
|
break;
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SEARCH_SELECT':
|
2020-03-15 11:40:57 +02:00
|
|
|
{
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.selectedSearchId = action.id;
|
|
|
|
if (!action.id) {
|
|
|
|
newState.notesParentType = defaultNotesParentType(state, 'Search');
|
|
|
|
} else {
|
|
|
|
newState.notesParentType = 'Search';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update history when searching
|
|
|
|
const lastSeenNote = stateUtils.getLastSeenNote(state);
|
|
|
|
if (lastSeenNote != null && (state.backwardHistoryNotes.length === 0 ||
|
|
|
|
state.backwardHistoryNotes[state.backwardHistoryNotes.length - 1].id != lastSeenNote.id)) {
|
|
|
|
newState.forwardHistoryNotes = [];
|
|
|
|
newState.backwardHistoryNotes.push(Object.assign({},lastSeenNote));
|
|
|
|
}
|
|
|
|
|
|
|
|
newState.selectedNoteIds = [];
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-10-23 22:34:04 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'APP_STATE_SET':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.appState = action.state;
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SYNC_HAS_DISABLED_SYNC_ITEMS':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.hasDisabledSyncItems = true;
|
|
|
|
break;
|
2017-12-05 20:56:39 +02:00
|
|
|
|
2020-03-13 19:42:50 +02:00
|
|
|
case 'ENCRYPTION_HAS_DISABLED_ITEMS':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.hasDisabledEncryptionItems = action.value;
|
|
|
|
break;
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'CLIPPER_SERVER_SET':
|
|
|
|
{
|
2018-05-25 14:30:27 +02:00
|
|
|
newState = Object.assign({}, state);
|
|
|
|
const clipperServer = Object.assign({}, newState.clipperServer);
|
|
|
|
if ('startState' in action) clipperServer.startState = action.startState;
|
|
|
|
if ('port' in action) clipperServer.port = action.port;
|
|
|
|
newState.clipperServer = clipperServer;
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-06-10 18:43:24 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'DECRYPTION_WORKER_SET':
|
|
|
|
{
|
2018-06-10 18:43:24 +02:00
|
|
|
newState = Object.assign({}, state);
|
|
|
|
const decryptionWorker = Object.assign({}, newState.decryptionWorker);
|
2020-03-14 01:46:14 +02:00
|
|
|
for (const n in action) {
|
2018-06-10 18:43:24 +02:00
|
|
|
if (!action.hasOwnProperty(n) || n === 'type') continue;
|
|
|
|
decryptionWorker[n] = action[n];
|
|
|
|
}
|
|
|
|
newState.decryptionWorker = decryptionWorker;
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'RESOURCE_FETCHER_SET':
|
|
|
|
{
|
2018-11-14 00:25:23 +02:00
|
|
|
newState = Object.assign({}, state);
|
|
|
|
const rf = Object.assign({}, action);
|
|
|
|
delete rf.type;
|
|
|
|
newState.resourceFetcher = rf;
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-11-14 00:25:23 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'LOAD_CUSTOM_CSS':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.customCss = action.css;
|
|
|
|
break;
|
2019-07-20 23:13:10 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'TEMPLATE_UPDATE_ALL':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.templates = action.templates;
|
|
|
|
break;
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'SET_NOTE_TAGS':
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
newState.selectedNoteTags = action.items;
|
|
|
|
break;
|
2018-11-08 00:16:05 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
case 'PLUGIN_DIALOG_SET':
|
|
|
|
{
|
2019-04-01 21:43:13 +02:00
|
|
|
if (!action.pluginName) throw new Error('action.pluginName not specified');
|
|
|
|
newState = Object.assign({}, state);
|
|
|
|
const newPlugins = Object.assign({}, newState.plugins);
|
|
|
|
const newPlugin = newState.plugins[action.pluginName] ? Object.assign({}, newState.plugins[action.pluginName]) : {};
|
|
|
|
if ('open' in action) newPlugin.dialogOpen = action.open;
|
|
|
|
newPlugins[action.pluginName] = newPlugin;
|
|
|
|
newState.plugins = newPlugins;
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-10-07 18:30:27 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2019-09-19 23:51:18 +02:00
|
|
|
error.message = `In reducer: ${error.message} Action: ${JSON.stringify(action)}`;
|
2017-10-07 18:30:27 +02:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (action.type.indexOf('NOTE_UPDATE') === 0 || action.type.indexOf('FOLDER_UPDATE') === 0 || action.type.indexOf('TAG_UPDATE') === 0) {
|
2017-12-14 19:58:10 +02:00
|
|
|
newState = Object.assign({}, newState);
|
|
|
|
newState.hasEncryptedItems = stateHasEncryptedItems(newState);
|
|
|
|
}
|
|
|
|
|
2017-10-07 18:30:27 +02:00
|
|
|
return newState;
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-10-07 18:30:27 +02:00
|
|
|
|
2018-07-20 11:04:25 +02:00
|
|
|
module.exports = { reducer, defaultState, stateUtils };
|