1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-15 23:00:36 +02:00

First pass at linting lib dir

This commit is contained in:
Laurent Cozic
2019-07-29 15:43:53 +02:00
parent 64b7bc3d62
commit 86dc72b204
170 changed files with 4140 additions and 3119 deletions

View File

@ -64,21 +64,25 @@ const cacheEnabledOutput = (key, output) => {
derivedStateCache_[key] = output;
return derivedStateCache_[key];
}
};
stateUtils.notesOrder = function(stateSettings) {
return cacheEnabledOutput('notesOrder', [{
by: stateSettings['notes.sortOrder.field'],
dir: stateSettings['notes.sortOrder.reverse'] ? 'DESC' : 'ASC',
}]);
}
return cacheEnabledOutput('notesOrder', [
{
by: stateSettings['notes.sortOrder.field'],
dir: stateSettings['notes.sortOrder.reverse'] ? 'DESC' : 'ASC',
},
]);
};
stateUtils.foldersOrder = function(stateSettings) {
return cacheEnabledOutput('foldersOrder', [{
by: stateSettings['folders.sortOrder.field'],
dir: stateSettings['folders.sortOrder.reverse'] ? 'DESC' : 'ASC',
}]);
}
return cacheEnabledOutput('foldersOrder', [
{
by: stateSettings['folders.sortOrder.field'],
dir: stateSettings['folders.sortOrder.reverse'] ? 'DESC' : 'ASC',
},
]);
};
stateUtils.parentItem = function(state) {
const t = state.notesParentType;
@ -88,20 +92,20 @@ stateUtils.parentItem = function(state) {
if (t === 'Search') id = state.selectedSearchId;
if (!t || !id) return null;
return { type: t, id: id };
}
};
stateUtils.lastSelectedNoteIds = function(state) {
const parent = stateUtils.parentItem(state);
if (!parent) return [];
const output = state.lastSelectedNotesIds[parent.type][parent.id];
return output ? output : [];
}
};
function arrayHasEncryptedItems(array) {
for (let i = 0; i < array.length; i++) {
if (!!array[i].encryption_applied) return true;
if (array[i].encryption_applied) return true;
}
return false
return false;
}
function stateHasEncryptedItems(state) {
@ -133,10 +137,10 @@ function handleItemDelete(state, action) {
let newState = Object.assign({}, state);
const map = {
'FOLDER_DELETE': ['folders', 'selectedFolderId'],
'NOTE_DELETE': ['notes', 'selectedNoteIds'],
'TAG_DELETE': ['tags', 'selectedTagId'],
'SEARCH_DELETE': ['searches', 'selectedSearchId'],
FOLDER_DELETE: ['folders', 'selectedFolderId'],
NOTE_DELETE: ['notes', 'selectedNoteIds'],
TAG_DELETE: ['tags', 'selectedTagId'],
SEARCH_DELETE: ['searches', 'selectedSearchId'],
};
const listKey = map[action.type][0];
@ -291,7 +295,7 @@ function changeSelectedNotes(state, action, options = null) {
}
newState.newNote = null;
} else {
} else {
throw new Error('Unreachable');
}
@ -299,7 +303,7 @@ function changeSelectedNotes(state, action, options = null) {
if (options.clearNoteHistory) newState.historyNotes = [];
return newState;
return newState;
}
function removeItemFromArray(array, property, value) {
@ -318,17 +322,14 @@ const reducer = (state = defaultState, action) => {
try {
switch (action.type) {
case 'NOTE_SELECT':
case 'NOTE_SELECT_ADD':
case 'NOTE_SELECT_REMOVE':
case 'NOTE_SELECT_TOGGLE':
newState = changeSelectedNotes(state, action);
break;
case 'NOTE_SELECT_EXTEND':
newState = Object.assign({}, state);
if (!newState.selectedNoteIds.length) {
@ -361,19 +362,17 @@ const reducer = (state = defaultState, action) => {
break;
case 'FOLDER_SELECT':
newState = changeSelectedFolder(state, action, { clearSelectedNoteIds: true });
break;
case 'FOLDER_AND_NOTE_SELECT':
newState = changeSelectedFolder(state, action, { clearNoteHistory: false });
const noteSelectAction = Object.assign({}, action, { type: 'NOTE_SELECT'});
const noteSelectAction = Object.assign({}, action, { type: 'NOTE_SELECT' });
newState = changeSelectedNotes(newState, noteSelectAction, { clearNoteHistory: false });
if (action.historyNoteAction) {
const historyNotes = newState.historyNotes.slice();
if (typeof action.historyNoteAction === 'object') {
if (typeof action.historyNoteAction === 'object') {
historyNotes.push(Object.assign({}, action.historyNoteAction));
} else if (action.historyNoteAction === 'pop') {
historyNotes.pop();
@ -385,17 +384,15 @@ const reducer = (state = defaultState, action) => {
// previous note wll stay.
newState.historyNotes = [];
}
break;
case 'SETTING_UPDATE_ALL':
newState = Object.assign({}, state);
newState.settings = action.settings;
break;
case 'SETTING_UPDATE_ONE':
newState = Object.assign({}, state);
let newSettings = Object.assign({}, state.settings);
newSettings[action.key] = action.value;
@ -404,7 +401,6 @@ const reducer = (state = defaultState, action) => {
// Replace all the notes with the provided array
case 'NOTE_UPDATE_ALL':
newState = Object.assign({}, state);
newState.notes = action.notes;
newState.notesSource = action.notesSource;
@ -413,14 +409,13 @@ const reducer = (state = defaultState, action) => {
// Insert the note into the note list if it's new, or
// update it within the note array if it already exists.
case 'NOTE_UPDATE_ONE':
const modNote = action.note;
const noteIsInFolder = function(note, folderId) {
if (note.is_conflict) return folderId === Folder.conflictFolderId();
if (!('parent_id' in modNote) || note.parent_id == folderId) return true;
return false;
}
};
let movedNotePreviousIndex = 0;
let noteFolderHasChanged = false;
@ -429,7 +424,6 @@ const reducer = (state = defaultState, action) => {
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)) {
// Merge the properties that have changed (in modNote) into
@ -440,8 +434,8 @@ const reducer = (state = defaultState, action) => {
if (!modNote.hasOwnProperty(n)) continue;
newNotes[i][n] = modNote[n];
}
} else { // Note has moved to a different folder
} else {
// Note has moved to a different folder
newNotes.splice(i, 1);
noteFolderHasChanged = true;
movedNotePreviousIndex = i;
@ -466,36 +460,31 @@ const reducer = (state = defaultState, action) => {
if (noteFolderHasChanged) {
let newIndex = movedNotePreviousIndex;
if (newIndex >= newNotes.length) newIndex = newNotes.length - 1;
if (newIndex >= newNotes.length) newIndex = newNotes.length - 1;
if (!newNotes.length) newIndex = -1;
newState.selectedNoteIds = newIndex >= 0 ? [newNotes[newIndex].id] : [];
}
break;
case 'NOTE_DELETE':
newState = handleItemDelete(state, action);
break;
case 'TAG_DELETE':
newState = handleItemDelete(state, action);
newState.selectedNoteTags = removeItemFromArray(newState.selectedNoteTags.splice(0), 'id', action.id);
break;
case 'FOLDER_UPDATE_ALL':
newState = Object.assign({}, state);
newState.folders = action.items;
break;
case 'FOLDER_SET_COLLAPSED':
newState = folderSetCollapsed(state, action);
break;
case 'FOLDER_TOGGLE':
if (state.collapsedFolderIds.indexOf(action.id) >= 0) {
newState = folderSetCollapsed(state, Object.assign({ collapsed: false }, action));
} else {
@ -504,19 +493,16 @@ const reducer = (state = defaultState, action) => {
break;
case 'FOLDER_SET_COLLAPSED_ALL':
newState = Object.assign({}, state);
newState.collapsedFolderIds = action.ids.slice();
break;
case 'TAG_UPDATE_ALL':
case 'TAG_UPDATE_ALL':
newState = Object.assign({}, state);
newState.tags = action.items;
break;
case 'TAG_SELECT':
newState = Object.assign({}, state);
newState.selectedTagId = action.id;
if (!action.id) {
@ -527,43 +513,36 @@ const reducer = (state = defaultState, action) => {
break;
case 'TAG_UPDATE_ONE':
newState = updateOneItem(state, action);
newState = updateOneItem(newState, action, 'selectedNoteTags');
break;
case 'NOTE_TAG_REMOVE':
newState = updateOneItem(state, action, 'tags');
let tagRemoved = action.item;
newState.selectedNoteTags = removeItemFromArray(newState.selectedNoteTags.splice(0), 'id', tagRemoved.id);;
newState.selectedNoteTags = removeItemFromArray(newState.selectedNoteTags.splice(0), 'id', tagRemoved.id);
break;
case 'FOLDER_UPDATE_ONE':
case 'MASTERKEY_UPDATE_ONE':
newState = updateOneItem(state, action);
break;
case 'FOLDER_DELETE':
newState = handleItemDelete(state, action);
break;
case 'MASTERKEY_UPDATE_ALL':
newState = Object.assign({}, state);
newState.masterKeys = action.items;
break;
case 'MASTERKEY_SET_NOT_LOADED':
newState = Object.assign({}, state);
newState.notLoadedMasterKeys = action.ids;
break;
case 'MASTERKEY_ADD_NOT_LOADED':
if (state.notLoadedMasterKeys.indexOf(action.id) < 0) {
newState = Object.assign({}, state);
const keys = newState.notLoadedMasterKeys.slice();
@ -573,7 +552,6 @@ const reducer = (state = defaultState, action) => {
break;
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];
@ -588,31 +566,26 @@ const reducer = (state = defaultState, action) => {
break;
case 'SYNC_STARTED':
newState = Object.assign({}, state);
newState.syncStarted = true;
break;
case 'SYNC_COMPLETED':
newState = Object.assign({}, state);
newState.syncStarted = false;
break;
case 'SYNC_REPORT_UPDATE':
newState = Object.assign({}, state);
newState.syncReport = action.report;
break;
case 'SEARCH_QUERY':
newState = Object.assign({}, state);
newState.searchQuery = action.query.trim();
break;
case 'SEARCH_ADD':
newState = Object.assign({}, state);
var searches = newState.searches.slice();
searches.push(action.search);
@ -620,7 +593,6 @@ const reducer = (state = defaultState, action) => {
break;
case 'SEARCH_UPDATE':
newState = Object.assign({}, state);
var searches = newState.searches.slice();
var found = false;
@ -644,12 +616,10 @@ const reducer = (state = defaultState, action) => {
break;
case 'SEARCH_DELETE':
newState = handleItemDelete(state, action);
break;
case 'SEARCH_SELECT':
newState = Object.assign({}, state);
newState.selectedSearchId = action.id;
if (!action.id) {
@ -660,19 +630,16 @@ const reducer = (state = defaultState, action) => {
break;
case 'APP_STATE_SET':
newState = Object.assign({}, state);
newState.appState = action.state;
break;
case 'SYNC_HAS_DISABLED_SYNC_ITEMS':
newState = Object.assign({}, state);
newState.hasDisabledSyncItems = true;
break;
case 'NOTE_SET_NEW_ONE':
newState = Object.assign({}, state);
newState.newNote = action.item;
if (newState.selectedNoteIds.length > 1) {
@ -682,7 +649,6 @@ const reducer = (state = defaultState, action) => {
break;
case 'CLIPPER_SERVER_SET':
newState = Object.assign({}, state);
const clipperServer = Object.assign({}, newState.clipperServer);
if ('startState' in action) clipperServer.startState = action.startState;
@ -691,7 +657,6 @@ const reducer = (state = defaultState, action) => {
break;
case 'DECRYPTION_WORKER_SET':
newState = Object.assign({}, state);
const decryptionWorker = Object.assign({}, newState.decryptionWorker);
for (var n in action) {
@ -702,7 +667,6 @@ const reducer = (state = defaultState, action) => {
break;
case 'RESOURCE_FETCHER_SET':
newState = Object.assign({}, state);
const rf = Object.assign({}, action);
delete rf.type;
@ -710,24 +674,21 @@ const reducer = (state = defaultState, action) => {
break;
case 'LOAD_CUSTOM_CSS':
newState = Object.assign({}, state);
newState.customCss = action.css;
break;
case 'TEMPLATE_UPDATE_ALL':
newState = Object.assign({}, state);
newState.templates = action.templates;
break;
case 'SET_NOTE_TAGS':
newState = Object.assign({}, state);
newState.selectedNoteTags = action.items;
break;
case 'PLUGIN_DIALOG_SET':
if (!action.pluginName) throw new Error('action.pluginName not specified');
newState = Object.assign({}, state);
const newPlugins = Object.assign({}, newState.plugins);
@ -736,7 +697,6 @@ const reducer = (state = defaultState, action) => {
newPlugins[action.pluginName] = newPlugin;
newState.plugins = newPlugins;
break;
}
} catch (error) {
error.message = 'In reducer: ' + error.message + ' Action: ' + JSON.stringify(action);
@ -749,6 +709,6 @@ const reducer = (state = defaultState, action) => {
}
return newState;
}
};
module.exports = { reducer, defaultState, stateUtils };