1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Record last selected note IDs and restore it when opening notebook

This commit is contained in:
Laurent Cozic 2019-01-29 18:32:52 +00:00
parent 4d08b49578
commit a33f602f3b
3 changed files with 75 additions and 17 deletions

View File

@ -13,6 +13,7 @@ const eventManager = require('../eventManager');
const InteropService = require('lib/services/InteropService'); const InteropService = require('lib/services/InteropService');
const InteropServiceHelper = require('../InteropServiceHelper.js'); const InteropServiceHelper = require('../InteropServiceHelper.js');
const Search = require('lib/models/Search'); const Search = require('lib/models/Search');
const { stateUtils } = require('lib/reducer');
const Mark = require('mark.js/dist/mark.min.js'); const Mark = require('mark.js/dist/mark.min.js');
const SearchEngine = require('lib/services/SearchEngine'); const SearchEngine = require('lib/services/SearchEngine');
const NoteListUtils = require('./utils/NoteListUtils'); const NoteListUtils = require('./utils/NoteListUtils');
@ -271,6 +272,16 @@ class NoteListComponent extends React.Component {
if (prevProps.windowCommand !== this.props.windowCommand) { if (prevProps.windowCommand !== this.props.windowCommand) {
this.doCommand(this.props.windowCommand); this.doCommand(this.props.windowCommand);
} }
if (prevProps.selectedNoteIds !== this.props.selectedNoteIds && this.props.selectedNoteIds.length === 1) {
const id = this.props.selectedNoteIds[0];
for (let i = 0; i < this.props.notes.length; i++) {
if (this.props.notes[i].id === id) {
this.itemListRef.current.makeItemIndexVisible(i);
break;
}
}
}
} }
async onKeyDown(event) { async onKeyDown(event) {

View File

@ -237,9 +237,30 @@ class BaseApplication {
id: state.selectedNoteIds && state.selectedNoteIds.length ? state.selectedNoteIds[0] : null, id: state.selectedNoteIds && state.selectedNoteIds.length ? state.selectedNoteIds[0] : null,
}); });
} else { } else {
const lastSelectedNoteIds = stateUtils.lastSelectedNoteIds(state);
const foundIds = [];
for (let i = 0; i < lastSelectedNoteIds.length; i++) {
const noteId = lastSelectedNoteIds[i];
let found = false;
for (let j = 0; j < notes.length; j++) {
if (notes[j].id === noteId) {
found = true;
break;
}
}
if (found) foundIds.push(noteId);
}
let selectedNoteId = null;
if (foundIds.length) {
selectedNoteId = foundIds[0];
} else {
selectedNoteId = notes.length ? notes[0].id : null;
}
this.store().dispatch({ this.store().dispatch({
type: 'NOTE_SELECT', type: 'NOTE_SELECT',
id: notes.length ? notes[0].id : null, id: selectedNoteId,
}); });
} }
} }

View File

@ -16,6 +16,11 @@ const defaultState = {
selectedTagId: null, selectedTagId: null,
selectedSearchId: null, selectedSearchId: null,
selectedItemType: 'note', selectedItemType: 'note',
lastSelectedNotesIds: {
Folder: {},
Tag: {},
Search: {},
},
showSideMenu: false, showSideMenu: false,
screens: {}, screens: {},
historyCanGoBack: false, historyCanGoBack: false,
@ -53,6 +58,23 @@ stateUtils.notesOrder = function(stateSettings) {
}]; }];
} }
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 };
}
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) { function arrayHasEncryptedItems(array) {
for (let i = 0; i < array.length; i++) { for (let i = 0; i < array.length; i++) {
if (!!array[i].encryption_applied) return true; if (!!array[i].encryption_applied) return true;
@ -183,6 +205,18 @@ function changeSelectedFolder(state, action) {
return newState; return newState;
} }
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,
});
}
function changeSelectedNotes(state, action) { function changeSelectedNotes(state, action) {
let noteIds = []; let noteIds = [];
if (action.id) noteIds = [action.id]; if (action.id) noteIds = [action.id];
@ -196,17 +230,11 @@ function changeSelectedNotes(state, action) {
if (action.type === 'NOTE_SELECT') { if (action.type === 'NOTE_SELECT') {
newState.selectedNoteIds = noteIds; newState.selectedNoteIds = noteIds;
newState.newNote = null; newState.newNote = null;
return newState; } else if (action.type === 'NOTE_SELECT_ADD') {
}
if (action.type === 'NOTE_SELECT_ADD') {
if (!noteIds.length) return state; if (!noteIds.length) return state;
newState.selectedNoteIds = ArrayUtils.unique(newState.selectedNoteIds.concat(noteIds)); newState.selectedNoteIds = ArrayUtils.unique(newState.selectedNoteIds.concat(noteIds));
newState.newNote = null; newState.newNote = null;
return newState; } else if (action.type === 'NOTE_SELECT_REMOVE') {
}
if (action.type === 'NOTE_SELECT_REMOVE') {
if (!noteIds.length) return state; // Nothing to unselect if (!noteIds.length) return state; // Nothing to unselect
if (state.selectedNoteIds.length <= 1) return state; // Cannot unselect the last note if (state.selectedNoteIds.length <= 1) return state; // Cannot unselect the last note
@ -218,11 +246,7 @@ function changeSelectedNotes(state, action) {
} }
newState.selectedNoteIds = newSelectedNoteIds; newState.selectedNoteIds = newSelectedNoteIds;
newState.newNote = null; newState.newNote = null;
} else if (action.type === 'NOTE_SELECT_TOGGLE') {
return newState;
}
if (action.type === 'NOTE_SELECT_TOGGLE') {
if (!noteIds.length) return state; if (!noteIds.length) return state;
if (newState.selectedNoteIds.indexOf(noteIds[0]) >= 0) { if (newState.selectedNoteIds.indexOf(noteIds[0]) >= 0) {
@ -232,11 +256,13 @@ function changeSelectedNotes(state, action) {
} }
newState.newNote = null; newState.newNote = null;
} else {
return newState; throw new Error('Unreachable');
} }
throw new Error('Unreachable'); newState = recordLastSelectedNoteIds(newState, newState.selectedNoteIds);
return newState;
} }
function removeItemFromArray(array, property, value) { function removeItemFromArray(array, property, value) {