1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ReactNativeClient/lib/reducer.js

455 lines
12 KiB
JavaScript
Raw Normal View History

2018-03-09 19:49:35 +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 19:49:35 +02:00
notesSource: "",
2017-10-07 18:30:27 +02:00
notesParentType: null,
folders: [],
tags: [],
masterKeys: [],
notLoadedMasterKeys: [],
searches: [],
2017-11-22 20:35:31 +02:00
selectedNoteIds: [],
2017-10-07 18:30:27 +02:00
selectedFolderId: null,
selectedTagId: null,
selectedSearchId: null,
2018-03-09 19:49:35 +02:00
selectedItemType: "note",
2017-10-07 18:30:27 +02:00
showSideMenu: false,
screens: {},
historyCanGoBack: false,
syncStarted: false,
syncReport: {},
2018-03-09 19:49:35 +02:00
searchQuery: "",
2017-10-07 18:30:27 +02:00
settings: {},
2018-03-09 19:49:35 +02:00
appState: "starting",
2017-12-05 20:56:39 +02:00
hasDisabledSyncItems: false,
newNote: null,
2017-10-07 18:30:27 +02:00
};
const stateUtils = {};
stateUtils.notesOrder = function(stateSettings) {
2018-03-09 19:49:35 +02:00
return [
{
by: stateSettings["notes.sortOrder.field"],
dir: stateSettings["notes.sortOrder.reverse"] ? "DESC" : "ASC",
},
];
};
function arrayHasEncryptedItems(array) {
for (let i = 0; i < array.length; i++) {
if (!!array[i].encryption_applied) return true;
}
2018-03-09 19:49:35 +02:00
return false;
}
function stateHasEncryptedItems(state) {
if (arrayHasEncryptedItems(state.notes)) return true;
if (arrayHasEncryptedItems(state.folders)) return true;
if (arrayHasEncryptedItems(state.tags)) return true;
return false;
}
2017-11-08 23:39:07 +02:00
// When deleting a note, tag or folder
function handleItemDelete(state, action) {
let newState = Object.assign({}, state);
2017-11-08 23:39:07 +02:00
const map = {
2018-03-09 19:49:35 +02:00
FOLDER_DELETE: ["folders", "selectedFolderId"],
NOTE_DELETE: ["notes", "selectedNoteIds"],
TAG_DELETE: ["tags", "selectedTagId"],
SEARCH_DELETE: ["searches", "selectedSearchId"],
2017-11-08 23:39:07 +02:00
};
const listKey = map[action.type][0];
const selectedItemKey = map[action.type][1];
let previousIndex = 0;
let newItems = [];
const items = state[listKey];
for (let i = 0; i < items.length; i++) {
2017-11-08 23:39:07 +02:00
let item = items[i];
if (item.id == action.id) {
previousIndex = i;
continue;
}
2017-11-08 23:39:07 +02:00
newItems.push(item);
}
newState = Object.assign({}, state);
newState[listKey] = newItems;
if (previousIndex >= newItems.length) {
previousIndex = newItems.length - 1;
}
2017-11-22 20:35:31 +02:00
const newId = previousIndex >= 0 ? newItems[previousIndex].id : null;
2018-03-09 19:49:35 +02:00
newState[selectedItemKey] = action.type === "NOTE_DELETE" ? [newId] : newId;
2018-03-09 19:49:35 +02:00
if (!newId && newState.notesParentType !== "Folder") {
newState.notesParentType = "Folder";
}
return newState;
}
function updateOneItem(state, action) {
let itemsKey = null;
2018-03-09 19:49:35 +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";
let newItems = state[itemsKey].splice(0);
let item = action.item;
2017-10-22 19:12:16 +02:00
var found = false;
for (let i = 0; i < newItems.length; i++) {
let n = newItems[i];
if (n.id == item.id) {
newItems[i] = Object.assign(newItems[i], item);
found = true;
break;
}
}
if (!found) newItems.push(item);
let newState = Object.assign({}, state);
newState[itemsKey] = newItems;
2017-10-22 19:12:16 +02:00
return newState;
}
function defaultNotesParentType(state, exclusion) {
let newNotesParentType = null;
2018-03-09 19:49:35 +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";
}
return newNotesParentType;
}
2017-11-22 20:35:31 +02:00
function changeSelectedNotes(state, action) {
2018-03-09 19:49:35 +02:00
const noteIds = "id" in action ? (action.id ? [action.id] : []) : action.ids;
2017-11-22 20:35:31 +02:00
let newState = Object.assign({}, state);
2018-03-09 19:49:35 +02:00
if (action.type === "NOTE_SELECT") {
2017-11-22 20:35:31 +02:00
newState.selectedNoteIds = noteIds;
newState.newNote = null;
2017-11-22 20:35:31 +02:00
return newState;
}
2018-03-09 19:49:35 +02:00
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));
newState.newNote = null;
2017-11-22 20:35:31 +02:00
return newState;
}
2018-03-09 19:49:35 +02:00
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
let newSelectedNoteIds = [];
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;
newState.newNote = null;
2017-11-22 20:35:31 +02:00
return newState;
}
2018-03-09 19:49:35 +02:00
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 19:49:35 +02:00
newState = changeSelectedNotes(state, { type: "NOTE_SELECT_REMOVE", id: noteIds[0] });
2017-11-22 20:35:31 +02:00
} else {
2018-03-09 19:49:35 +02:00
newState = changeSelectedNotes(state, { type: "NOTE_SELECT_ADD", id: noteIds[0] });
2017-11-22 20:35:31 +02:00
}
newState.newNote = null;
2017-11-22 20:35:31 +02:00
return newState;
}
2018-03-09 19:49:35 +02:00
throw new Error("Unreachable");
2017-11-22 20:35:31 +02:00
}
2017-10-07 18:30:27 +02:00
const reducer = (state = defaultState, action) => {
let newState = state;
try {
switch (action.type) {
2018-03-09 19:49:35 +02:00
case "NOTE_SELECT":
case "NOTE_SELECT_ADD":
case "NOTE_SELECT_REMOVE":
case "NOTE_SELECT_TOGGLE":
2017-11-22 20:35:31 +02:00
newState = changeSelectedNotes(state, action);
break;
2018-03-09 19:49:35 +02:00
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;
let newSelectedNoteIds = state.selectedNoteIds.slice();
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;
}
2017-10-07 23:01:03 +02:00
break;
2018-03-09 19:49:35 +02:00
case "FOLDER_SELECT":
2017-10-08 00:17:10 +02:00
newState = Object.assign({}, state);
newState.selectedFolderId = action.id;
if (!action.id) {
2018-03-09 19:49:35 +02:00
newState.notesParentType = defaultNotesParentType(state, "Folder");
} else {
2018-03-09 19:49:35 +02:00
newState.notesParentType = "Folder";
}
2017-10-08 00:17:10 +02:00
break;
2018-03-09 19:49:35 +02:00
case "SETTING_UPDATE_ALL":
2017-10-07 18:30:27 +02:00
newState = Object.assign({}, state);
newState.settings = action.settings;
break;
2018-03-09 19:49:35 +02:00
case "SETTING_UPDATE_ONE":
2017-10-07 18:30:27 +02:00
newState = Object.assign({}, state);
let newSettings = Object.assign({}, state.settings);
newSettings[action.key] = action.value;
newState.settings = newSettings;
break;
// Replace all the notes with the provided array
2018-03-09 19:49:35 +02:00
case "NOTE_UPDATE_ALL":
2017-10-07 18:30:27 +02:00
newState = Object.assign({}, state);
newState.notes = action.notes;
newState.notesSource = action.notesSource;
break;
// Insert the note into the note list if it's new, or
// update it within the note array if it already exists.
2018-03-09 19:49:35 +02:00
case "NOTE_UPDATE_ONE":
2017-10-07 18:30:27 +02:00
const modNote = action.note;
const noteIsInFolder = function(note, folderId) {
if (note.is_conflict) return folderId === Folder.conflictFolderId();
2018-03-09 19:49:35 +02:00
if (!("parent_id" in modNote) || note.parent_id == folderId) return true;
return false;
2018-03-09 19:49:35 +02:00
};
let noteFolderHasChanged = false;
2017-10-07 18:30:27 +02:00
let newNotes = state.notes.slice();
var found = false;
for (let i = 0; i < newNotes.length; i++) {
let n = newNotes[i];
if (n.id == modNote.id) {
// Note is still in the same folder
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]);
for (let n in modNote) {
if (!modNote.hasOwnProperty(n)) continue;
newNotes[i][n] = modNote[n];
}
2018-03-09 19:49:35 +02:00
} else {
// Note has moved to a different folder
2017-10-07 18:30:27 +02:00
newNotes.splice(i, 1);
noteFolderHasChanged = true;
2017-10-07 18:30:27 +02:00
}
found = true;
break;
}
}
// 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
//newNotes = Note.sortNotes(newNotes, state.notesOrder, newState.settings.uncompletedTodosOnTop);
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;
if (noteFolderHasChanged) {
2017-11-30 02:25:52 +02:00
newState.selectedNoteIds = newNotes.length ? [newNotes[0].id] : [];
}
2017-10-07 18:30:27 +02:00
break;
2018-03-09 19:49:35 +02:00
case "NOTE_DELETE":
2017-11-08 23:39:07 +02:00
newState = handleItemDelete(state, action);
break;
2018-03-09 19:49:35 +02:00
case "TAG_DELETE":
2017-11-08 23:39:07 +02:00
newState = handleItemDelete(state, action);
2017-10-07 18:30:27 +02:00
break;
2018-03-09 19:49:35 +02:00
case "FOLDER_UPDATE_ALL":
2017-10-07 18:30:27 +02:00
newState = Object.assign({}, state);
newState.folders = action.items;
2017-10-07 18:30:27 +02:00
break;
2018-03-09 19:49:35 +02:00
case "TAG_UPDATE_ALL":
2017-10-07 18:30:27 +02:00
newState = Object.assign({}, state);
newState.tags = action.items;
break;
2017-10-07 18:30:27 +02:00
2018-03-09 19:49:35 +02:00
case "TAG_SELECT":
2017-10-22 19:12:16 +02:00
newState = Object.assign({}, state);
newState.selectedTagId = action.id;
if (!action.id) {
2018-03-09 19:49:35 +02:00
newState.notesParentType = defaultNotesParentType(state, "Tag");
} else {
2018-03-09 19:49:35 +02:00
newState.notesParentType = "Tag";
}
2017-10-22 19:12:16 +02:00
break;
2017-10-07 18:30:27 +02:00
2018-03-09 19:49:35 +02:00
case "TAG_UPDATE_ONE":
case "FOLDER_UPDATE_ONE":
case "MASTERKEY_UPDATE_ONE":
newState = updateOneItem(state, action);
2017-10-07 18:30:27 +02:00
break;
2018-03-09 19:49:35 +02:00
case "FOLDER_DELETE":
2017-11-08 23:39:07 +02:00
newState = handleItemDelete(state, action);
2017-10-07 18:30:27 +02:00
break;
2018-03-09 19:49:35 +02:00
case "MASTERKEY_UPDATE_ALL":
newState = Object.assign({}, state);
newState.masterKeys = action.items;
break;
2018-03-09 19:49:35 +02:00
case "MASTERKEY_ADD_NOT_LOADED":
if (state.notLoadedMasterKeys.indexOf(action.id) < 0) {
newState = Object.assign({}, state);
const keys = newState.notLoadedMasterKeys.slice();
keys.push(action.id);
newState.notLoadedMasterKeys = keys;
}
break;
2018-03-09 19:49:35 +02:00
case "MASTERKEY_REMOVE_NOT_LOADED":
const ids = action.id ? [action.id] : action.ids;
for (let i = 0; i < ids.length; i++) {
const id = ids[i];
const index = state.notLoadedMasterKeys.indexOf(id);
if (index >= 0) {
newState = Object.assign({}, state);
const keys = newState.notLoadedMasterKeys.slice();
keys.splice(index, 1);
newState.notLoadedMasterKeys = keys;
}
}
break;
2018-03-09 19:49:35 +02:00
case "SYNC_STARTED":
2017-10-07 18:30:27 +02:00
newState = Object.assign({}, state);
newState.syncStarted = true;
break;
2018-03-09 19:49:35 +02:00
case "SYNC_COMPLETED":
2017-10-07 18:30:27 +02:00
newState = Object.assign({}, state);
newState.syncStarted = false;
break;
2018-03-09 19:49:35 +02:00
case "SYNC_REPORT_UPDATE":
2017-10-07 18:30:27 +02:00
newState = Object.assign({}, state);
newState.syncReport = action.report;
break;
2018-03-09 19:49:35 +02:00
case "SEARCH_QUERY":
2017-10-07 18:30:27 +02:00
newState = Object.assign({}, state);
newState.searchQuery = action.query.trim();
break;
2018-03-09 19:49:35 +02:00
case "SEARCH_ADD":
newState = Object.assign({}, state);
let searches = newState.searches.slice();
searches.push(action.search);
newState.searches = searches;
break;
2018-03-09 19:49:35 +02:00
case "SEARCH_DELETE":
2017-11-17 20:57:27 +02:00
newState = handleItemDelete(state, action);
2018-03-09 19:49:35 +02:00
break;
2018-03-09 19:49:35 +02:00
case "SEARCH_SELECT":
newState = Object.assign({}, state);
newState.selectedSearchId = action.id;
if (!action.id) {
2018-03-09 19:49:35 +02:00
newState.notesParentType = defaultNotesParentType(state, "Search");
} else {
2018-03-09 19:49:35 +02:00
newState.notesParentType = "Search";
}
break;
2018-03-09 19:49:35 +02:00
case "APP_STATE_SET":
2017-10-07 18:30:27 +02:00
newState = Object.assign({}, state);
newState.appState = action.state;
break;
2018-03-09 19:49:35 +02:00
case "SYNC_HAS_DISABLED_SYNC_ITEMS":
2017-12-05 20:56:39 +02:00
newState = Object.assign({}, state);
newState.hasDisabledSyncItems = true;
break;
2018-03-09 19:49:35 +02:00
case "NOTE_SET_NEW_ONE":
newState = Object.assign({}, state);
newState.newNote = action.item;
2018-03-09 19:49:35 +02:00
break;
2017-10-07 18:30:27 +02:00
}
} catch (error) {
2018-03-09 19:49:35 +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 19:49:35 +02:00
if (action.type.indexOf("NOTE_UPDATE") === 0 || action.type.indexOf("FOLDER_UPDATE") === 0 || action.type.indexOf("TAG_UPDATE") === 0) {
newState = Object.assign({}, newState);
newState.hasEncryptedItems = stateHasEncryptedItems(newState);
}
2017-10-07 18:30:27 +02:00
return newState;
2018-03-09 19:49:35 +02:00
};
2017-10-07 18:30:27 +02:00
2018-03-09 19:49:35 +02:00
module.exports = { reducer, defaultState, stateUtils };