2022-06-08 11:33:53 +02:00
|
|
|
import produce, { Draft, original } from 'immer';
|
2020-11-05 18:58:23 +02:00
|
|
|
import pluginServiceReducer, { stateRootKey as pluginServiceStateRootKey, defaultState as pluginServiceDefaultState, State as PluginServiceState } from './services/plugins/reducer';
|
2021-05-13 18:57:37 +02:00
|
|
|
import shareServiceReducer, { stateRootKey as shareServiceStateRootKey, defaultState as shareServiceDefaultState, State as ShareServiceState } from './services/share/reducer';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Note from './models/Note';
|
|
|
|
import Folder from './models/Folder';
|
|
|
|
import BaseModel from './BaseModel';
|
2021-05-13 18:57:37 +02:00
|
|
|
import { Store } from 'redux';
|
2022-04-11 17:49:32 +02:00
|
|
|
import { ProfileConfig } from './services/profileConfig/types';
|
2022-05-26 16:57:44 +02:00
|
|
|
import * as ArrayUtils from './ArrayUtils';
|
2024-03-02 16:25:27 +02:00
|
|
|
import { FolderEntity, NoteEntity } from './services/database/types';
|
2023-09-18 18:40:36 +02:00
|
|
|
import { getListRendererIds } from './services/noteList/renderers';
|
2024-01-05 16:15:47 +02:00
|
|
|
import { ProcessResultsRow } from './services/search/SearchEngine';
|
2024-03-02 16:25:27 +02:00
|
|
|
import { getDisplayParentId } from './services/trash';
|
2022-06-08 11:33:53 +02:00
|
|
|
const fastDeepEqual = require('fast-deep-equal');
|
2020-11-05 18:58:23 +02:00
|
|
|
const { ALL_NOTES_FILTER_ID } = require('./reserved-ids');
|
2020-10-09 19:35:46 +02:00
|
|
|
const { createSelectorCreator, defaultMemoize } = require('reselect');
|
|
|
|
const { createCachedSelector } = require('re-reselect');
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const additionalReducers: any[] = [];
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
additionalReducers.push({
|
|
|
|
stateRootKey: pluginServiceStateRootKey,
|
|
|
|
defaultState: pluginServiceDefaultState,
|
|
|
|
reducer: pluginServiceReducer,
|
|
|
|
});
|
|
|
|
|
2021-05-13 18:57:37 +02:00
|
|
|
additionalReducers.push({
|
|
|
|
stateRootKey: shareServiceStateRootKey,
|
|
|
|
defaultState: shareServiceDefaultState,
|
|
|
|
reducer: shareServiceReducer,
|
|
|
|
});
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
interface StateLastSelectedNotesIds {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
Folder: any;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
Tag: any;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
Search: any;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface StateClipperServer {
|
2020-11-12 21:29:22 +02:00
|
|
|
startState: string;
|
|
|
|
port: number;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 19:15:18 +02:00
|
|
|
export interface StateDecryptionWorker {
|
2020-11-12 21:29:22 +02:00
|
|
|
state: string;
|
|
|
|
itemIndex: number;
|
|
|
|
itemCount: number;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
decryptedItemCounts: any;
|
|
|
|
decryptedItemCount: number;
|
|
|
|
skippedItemCount: number;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 19:15:18 +02:00
|
|
|
export interface StateResourceFetcher {
|
2020-11-12 21:29:22 +02:00
|
|
|
toFetchCount: number;
|
2024-04-15 19:15:18 +02:00
|
|
|
fetchingCount: number;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
export interface StateLastDeletion {
|
|
|
|
noteIds: string[];
|
|
|
|
folderIds: string[];
|
|
|
|
timestamp: number;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
export interface State {
|
2024-03-02 16:25:27 +02:00
|
|
|
notes: NoteEntity[];
|
2022-08-27 14:36:59 +02:00
|
|
|
noteSelectionEnabled?: boolean;
|
2020-11-12 21:29:22 +02:00
|
|
|
notesSource: string;
|
|
|
|
notesParentType: string;
|
2023-01-08 14:22:41 +02:00
|
|
|
folders: FolderEntity[];
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
tags: any[];
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
masterKeys: any[];
|
2021-08-17 13:03:19 +02:00
|
|
|
notLoadedMasterKeys: string[];
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
searches: any[];
|
|
|
|
highlightedWords: string[];
|
|
|
|
selectedNoteIds: string[];
|
|
|
|
selectedNoteHash: string;
|
|
|
|
selectedFolderId: string;
|
|
|
|
selectedTagId: string;
|
|
|
|
selectedSearchId: string;
|
|
|
|
selectedItemType: string;
|
|
|
|
selectedSmartFilterId: string;
|
|
|
|
lastSelectedNotesIds: StateLastSelectedNotesIds;
|
|
|
|
showSideMenu: boolean;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
screens: any;
|
|
|
|
historyCanGoBack: boolean;
|
|
|
|
syncStarted: boolean;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
syncReport: any;
|
|
|
|
searchQuery: string;
|
2023-12-13 21:24:58 +02:00
|
|
|
searchResults: ProcessResultsRow[];
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-11-09 21:19:08 +02:00
|
|
|
settings: Record<string, any>;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
sharedData: any;
|
|
|
|
appState: string;
|
2023-02-10 18:12:14 +02:00
|
|
|
biometricsDone: boolean;
|
2020-11-12 21:29:22 +02:00
|
|
|
hasDisabledSyncItems: boolean;
|
|
|
|
hasDisabledEncryptionItems: boolean;
|
|
|
|
customCss: string;
|
|
|
|
collapsedFolderIds: string[];
|
|
|
|
clipperServer: StateClipperServer;
|
|
|
|
decryptionWorker: StateDecryptionWorker;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
selectedNoteTags: any[];
|
|
|
|
resourceFetcher: StateResourceFetcher;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
backwardHistoryNotes: any[];
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
forwardHistoryNotes: any[];
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
pluginsLegacy: any;
|
|
|
|
provisionalNoteIds: string[];
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
editorNoteStatuses: any;
|
|
|
|
isInsertingNotes: boolean;
|
|
|
|
hasEncryptedItems: boolean;
|
2021-06-22 20:57:04 +02:00
|
|
|
needApiAuth: boolean;
|
2022-04-11 17:49:32 +02:00
|
|
|
profileConfig: ProfileConfig;
|
2023-09-18 18:40:36 +02:00
|
|
|
noteListRendererIds: string[];
|
2024-01-03 20:02:05 +02:00
|
|
|
noteListLastSortTime: number;
|
2024-03-02 16:25:27 +02:00
|
|
|
lastDeletion: StateLastDeletion;
|
|
|
|
lastDeletionNotificationTime: number;
|
2024-01-26 12:32:35 +02:00
|
|
|
mustUpgradeAppMessage: string;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
// Extra reducer keys go here:
|
2020-11-12 21:29:22 +02:00
|
|
|
pluginService: PluginServiceState;
|
2021-05-13 18:57:37 +02:00
|
|
|
shareService: ShareServiceState;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const defaultState: State = {
|
2020-10-09 19:35:46 +02:00
|
|
|
notes: [],
|
|
|
|
notesSource: '',
|
|
|
|
notesParentType: null,
|
|
|
|
folders: [],
|
|
|
|
tags: [],
|
|
|
|
masterKeys: [],
|
|
|
|
notLoadedMasterKeys: [],
|
|
|
|
searches: [],
|
|
|
|
highlightedWords: [],
|
|
|
|
selectedNoteIds: [],
|
|
|
|
selectedNoteHash: '',
|
|
|
|
selectedFolderId: null,
|
|
|
|
selectedTagId: null,
|
|
|
|
selectedSearchId: null,
|
|
|
|
selectedSmartFilterId: null,
|
|
|
|
selectedItemType: 'note',
|
|
|
|
lastSelectedNotesIds: {
|
|
|
|
Folder: {},
|
|
|
|
Tag: {},
|
|
|
|
Search: {},
|
|
|
|
},
|
|
|
|
showSideMenu: false,
|
|
|
|
screens: {},
|
|
|
|
historyCanGoBack: false,
|
|
|
|
syncStarted: false,
|
|
|
|
syncReport: {},
|
|
|
|
searchQuery: '',
|
2023-12-13 21:24:58 +02:00
|
|
|
searchResults: [],
|
2020-10-09 19:35:46 +02:00
|
|
|
settings: {},
|
|
|
|
sharedData: null,
|
|
|
|
appState: 'starting',
|
2023-02-10 18:12:14 +02:00
|
|
|
biometricsDone: false,
|
2020-10-09 19:35:46 +02:00
|
|
|
hasDisabledSyncItems: false,
|
|
|
|
hasDisabledEncryptionItems: false,
|
|
|
|
customCss: '',
|
|
|
|
collapsedFolderIds: [],
|
|
|
|
clipperServer: {
|
|
|
|
startState: 'idle',
|
|
|
|
port: null,
|
|
|
|
},
|
|
|
|
decryptionWorker: {
|
|
|
|
state: 'idle',
|
|
|
|
itemIndex: 0,
|
|
|
|
itemCount: 0,
|
|
|
|
decryptedItemCounts: {},
|
|
|
|
decryptedItemCount: 0,
|
|
|
|
skippedItemCount: 0,
|
|
|
|
},
|
|
|
|
selectedNoteTags: [],
|
|
|
|
resourceFetcher: {
|
|
|
|
toFetchCount: 0,
|
2024-04-15 19:15:18 +02:00
|
|
|
fetchingCount: 0,
|
2020-10-09 19:35:46 +02:00
|
|
|
},
|
|
|
|
backwardHistoryNotes: [],
|
|
|
|
forwardHistoryNotes: [],
|
|
|
|
// pluginsLegacy is the original plugin system, which eventually was used only for GotoAnything.
|
|
|
|
// GotoAnything should be refactored to part of core and when it's done the pluginsLegacy key can
|
|
|
|
// be removed. It was originally named "plugins", then renamed "pluginsLegacy" so as not to conflict
|
|
|
|
// with the new "plugins" key used for the new plugin system.
|
|
|
|
pluginsLegacy: {},
|
|
|
|
provisionalNoteIds: [],
|
|
|
|
editorNoteStatuses: {},
|
|
|
|
isInsertingNotes: false,
|
|
|
|
hasEncryptedItems: false,
|
2021-06-22 20:57:04 +02:00
|
|
|
needApiAuth: false,
|
2022-04-11 17:49:32 +02:00
|
|
|
profileConfig: null,
|
2023-09-18 18:40:36 +02:00
|
|
|
noteListRendererIds: getListRendererIds(),
|
2024-01-03 20:02:05 +02:00
|
|
|
noteListLastSortTime: 0,
|
2024-03-02 16:25:27 +02:00
|
|
|
lastDeletion: {
|
|
|
|
noteIds: [],
|
|
|
|
folderIds: [],
|
|
|
|
timestamp: 0,
|
|
|
|
},
|
|
|
|
lastDeletionNotificationTime: 0,
|
2024-01-26 12:32:35 +02:00
|
|
|
mustUpgradeAppMessage: '',
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
pluginService: pluginServiceDefaultState,
|
2021-05-13 18:57:37 +02:00
|
|
|
shareService: shareServiceDefaultState,
|
2020-10-09 19:35:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const additionalReducer of additionalReducers) {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-10-09 19:35:46 +02:00
|
|
|
(defaultState as any)[additionalReducer.stateRootKey] = additionalReducer.defaultState;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-05-13 18:57:37 +02:00
|
|
|
let store_: Store<any> = null;
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-05-13 18:57:37 +02:00
|
|
|
export function setStore(v: Store<any>) {
|
|
|
|
store_ = v;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-05-13 18:57:37 +02:00
|
|
|
export function store(): Store<any> {
|
|
|
|
return store_;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
export const MAX_HISTORY = 200;
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const derivedStateCache_: any = {};
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
// Allows, for a given state, to return the same derived
|
2024-02-26 12:16:23 +02:00
|
|
|
// objects, to prevent unnecessary updates on calling components.
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const cacheEnabledOutput = (key: string, output: any) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
key = `${key}_${JSON.stringify(output)}`;
|
|
|
|
if (derivedStateCache_[key]) return derivedStateCache_[key];
|
|
|
|
|
|
|
|
derivedStateCache_[key] = output;
|
|
|
|
return derivedStateCache_[key];
|
|
|
|
};
|
|
|
|
|
|
|
|
const createShallowArrayEqualSelector = createSelectorCreator(
|
|
|
|
defaultMemoize,
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
(prev: any[], next: any[]) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (prev.length !== next.length) return false;
|
|
|
|
for (let i = 0; i < prev.length; i++) {
|
|
|
|
if (prev[i] !== next[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2023-08-22 12:58:53 +02:00
|
|
|
},
|
2020-10-09 19:35:46 +02:00
|
|
|
);
|
|
|
|
|
2020-10-18 22:52:10 +02:00
|
|
|
const selectArrayShallow = createCachedSelector(
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
(state: any) => state.array,
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-08-22 12:58:53 +02:00
|
|
|
(array: any[]) => array,
|
2020-10-09 19:35:46 +02:00
|
|
|
)({
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
keySelector: (_state: any, cacheKey: any) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
return cacheKey;
|
|
|
|
},
|
|
|
|
selectorCreator: createShallowArrayEqualSelector,
|
|
|
|
});
|
|
|
|
|
2020-10-18 22:52:10 +02:00
|
|
|
class StateUtils {
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-10-18 22:52:10 +02:00
|
|
|
// Given an input array, this selector ensures that the same array is returned
|
|
|
|
// if its content hasn't changed.
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
public selectArrayShallow(props: any, cacheKey: any) {
|
2020-10-18 22:52:10 +02:00
|
|
|
return selectArrayShallow(props, cacheKey);
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public oneNoteSelected(state: State): boolean {
|
2020-10-18 22:52:10 +02:00
|
|
|
return state.selectedNoteIds.length === 1;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
public notesOrder(stateSettings: any) {
|
2020-10-18 22:52:10 +02:00
|
|
|
if (stateSettings['notes.sortOrder.field'] === 'order') {
|
|
|
|
return cacheEnabledOutput('notesOrder', [
|
|
|
|
{
|
|
|
|
by: 'order',
|
|
|
|
dir: 'DESC',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
by: 'user_created_time',
|
|
|
|
dir: 'DESC',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
return cacheEnabledOutput('notesOrder', [
|
|
|
|
{
|
|
|
|
by: stateSettings['notes.sortOrder.field'],
|
|
|
|
dir: stateSettings['notes.sortOrder.reverse'] ? 'DESC' : 'ASC',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
public foldersOrder(stateSettings: any) {
|
2020-10-18 22:52:10 +02:00
|
|
|
return cacheEnabledOutput('foldersOrder', [
|
2020-10-09 19:35:46 +02:00
|
|
|
{
|
2020-10-18 22:52:10 +02:00
|
|
|
by: stateSettings['folders.sortOrder.field'],
|
|
|
|
dir: stateSettings['folders.sortOrder.reverse'] ? 'DESC' : 'ASC',
|
2020-10-09 19:35:46 +02:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public hasNotesBeingSaved(state: State): boolean {
|
2020-10-18 22:52:10 +02:00
|
|
|
for (const id in state.editorNoteStatuses) {
|
|
|
|
if (state.editorNoteStatuses[id] === 'saving') return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public parentItem(state: State) {
|
2020-10-18 22:52:10 +02:00
|
|
|
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 };
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public lastSelectedNoteIds(state: State): string[] {
|
2020-10-18 22:52:10 +02:00
|
|
|
const parent = this.parentItem(state);
|
|
|
|
if (!parent) return [];
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-10-18 22:52:10 +02:00
|
|
|
const output = (state.lastSelectedNotesIds as any)[parent.type][parent.id];
|
|
|
|
return output ? output : [];
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
public selectedNote(state: State): any {
|
2020-10-18 22:52:10 +02:00
|
|
|
const noteId = this.selectedNoteId(state);
|
|
|
|
return noteId ? BaseModel.byId(state.notes, noteId) : null;
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
public selectedNoteId(state: State): any {
|
2020-10-18 22:52:10 +02:00
|
|
|
return state.selectedNoteIds.length ? state.selectedNoteIds[0] : null;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
2020-10-18 22:52:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const stateUtils: StateUtils = new StateUtils();
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function arrayHasEncryptedItems(array: any[]) {
|
2020-10-09 19:35:46 +02:00
|
|
|
for (let i = 0; i < array.length; i++) {
|
|
|
|
if (array[i].encryption_applied) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function stateHasEncryptedItems(state: State) {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (arrayHasEncryptedItems(state.notes)) return true;
|
|
|
|
if (arrayHasEncryptedItems(state.folders)) return true;
|
|
|
|
if (arrayHasEncryptedItems(state.tags)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function folderSetCollapsed(draft: Draft<State>, action: any) {
|
2020-10-09 19:35:46 +02:00
|
|
|
const collapsedFolderIds = draft.collapsedFolderIds.slice();
|
|
|
|
const idx = collapsedFolderIds.indexOf(action.id);
|
|
|
|
|
|
|
|
if (action.collapsed) {
|
|
|
|
if (idx >= 0) return;
|
|
|
|
collapsedFolderIds.push(action.id);
|
|
|
|
} else {
|
|
|
|
if (idx < 0) return;
|
|
|
|
collapsedFolderIds.splice(idx, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
draft.collapsedFolderIds = collapsedFolderIds;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function removeAdjacentDuplicates(items: any[]) {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
return items.filter((item: any, idx: number) => (idx >= 1) ? items[idx - 1].id !== item.id : true);
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// When deleting a note, tag or folder
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function handleItemDelete(draft: Draft<State>, action: any) {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const map: any = {
|
2020-10-09 19:35:46 +02:00
|
|
|
FOLDER_DELETE: ['folders', 'selectedFolderId', true],
|
|
|
|
NOTE_DELETE: ['notes', 'selectedNoteIds', false],
|
|
|
|
TAG_DELETE: ['tags', 'selectedTagId', true],
|
|
|
|
SEARCH_DELETE: ['searches', 'selectedSearchId', true],
|
|
|
|
};
|
|
|
|
|
|
|
|
const listKey = map[action.type][0];
|
|
|
|
const selectedItemKey = map[action.type][1];
|
|
|
|
const isSingular = map[action.type][2];
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-10-09 19:35:46 +02:00
|
|
|
const selectedItemKeys = isSingular ? [(draft as any)[selectedItemKey]] : (draft as any)[selectedItemKey];
|
|
|
|
const isSelected = selectedItemKeys.includes(action.id);
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-10-09 19:35:46 +02:00
|
|
|
const items = (draft as any)[listKey];
|
|
|
|
const newItems = [];
|
2020-11-12 21:13:28 +02:00
|
|
|
let newSelectedIndexes: number[] = [];
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const item = items[i];
|
|
|
|
if (isSelected) {
|
|
|
|
// the selected item is deleted so select the following item
|
|
|
|
// if multiple items are selected then just use the first one
|
2022-07-23 09:31:32 +02:00
|
|
|
if (selectedItemKeys[0] === item.id) {
|
2020-10-09 19:35:46 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 09:31:32 +02:00
|
|
|
if (item.id === action.id) {
|
2020-10-09 19:35:46 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
newItems.push(item);
|
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (newItems.length === 0) {
|
2020-10-09 19:35:46 +02:00
|
|
|
newSelectedIndexes = []; // no remaining items so no selection
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
} else if (newSelectedIndexes.length === 0) {
|
2020-10-09 19:35:46 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-10-09 19:35:46 +02:00
|
|
|
(draft as any)[listKey] = newItems;
|
|
|
|
|
|
|
|
const newIds = [];
|
|
|
|
for (let i = 0; i < newSelectedIndexes.length; i++) {
|
|
|
|
newIds.push(newItems[newSelectedIndexes[i]].id);
|
|
|
|
}
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-10-09 19:35:46 +02:00
|
|
|
(draft as any)[selectedItemKey] = isSingular ? newIds[0] : newIds;
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if ((newIds.length === 0) && draft.notesParentType !== 'Folder') {
|
2020-10-09 19:35:46 +02:00
|
|
|
draft.notesParentType = 'Folder';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-06-30 10:11:26 +02:00
|
|
|
function updateOneItem(draft: Draft<State>, action: any, keyName = '') {
|
2020-10-09 19:35:46 +02:00
|
|
|
let itemsKey = null;
|
|
|
|
if (keyName) { itemsKey = keyName; } else {
|
|
|
|
if (action.type === 'TAG_UPDATE_ONE') itemsKey = 'tags';
|
|
|
|
if (action.type === 'FOLDER_UPDATE_ONE') itemsKey = 'folders';
|
|
|
|
if (action.type === 'MASTERKEY_UPDATE_ONE') itemsKey = 'masterKeys';
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-10-09 19:35:46 +02:00
|
|
|
const newItems = (draft as any)[itemsKey].slice();
|
|
|
|
const item = action.item;
|
|
|
|
|
|
|
|
let found = false;
|
|
|
|
for (let i = 0; i < newItems.length; i++) {
|
|
|
|
const n = newItems[i];
|
2022-07-23 09:31:32 +02:00
|
|
|
if (n.id === item.id) {
|
2023-06-01 13:02:36 +02:00
|
|
|
newItems[i] = { ...newItems[i], ...item };
|
2020-10-09 19:35:46 +02:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) newItems.push(item);
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-10-09 19:35:46 +02:00
|
|
|
(draft as any)[itemsKey] = newItems;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function updateSelectedNotesFromExistingNotes(draft: Draft<State>) {
|
2020-10-09 19:35:46 +02:00
|
|
|
const newSelectedNoteIds = [];
|
|
|
|
for (const selectedNoteId of draft.selectedNoteIds) {
|
|
|
|
for (const n of draft.notes) {
|
|
|
|
if (n.id === selectedNoteId) {
|
|
|
|
newSelectedNoteIds.push(n.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-12 12:40:14 +02:00
|
|
|
if (JSON.stringify(draft.selectedNoteIds) === JSON.stringify(newSelectedNoteIds)) return;
|
2020-10-09 19:35:46 +02:00
|
|
|
draft.selectedNoteIds = newSelectedNoteIds;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function defaultNotesParentType(draft: Draft<State>, exclusion: string) {
|
2020-10-09 19:35:46 +02:00
|
|
|
let newNotesParentType = null;
|
|
|
|
|
|
|
|
if (exclusion !== 'SmartFilter' && draft.selectedSmartFilterId) {
|
|
|
|
newNotesParentType = 'SmartFilter';
|
|
|
|
} else if (exclusion !== 'Folder' && draft.selectedFolderId) {
|
|
|
|
newNotesParentType = 'Folder';
|
|
|
|
} else if (exclusion !== 'Tag' && draft.selectedTagId) {
|
|
|
|
newNotesParentType = 'Tag';
|
|
|
|
} else if (exclusion !== 'Search' && draft.selectedSearchId) {
|
|
|
|
newNotesParentType = 'Search';
|
|
|
|
}
|
|
|
|
|
|
|
|
return newNotesParentType;
|
|
|
|
}
|
|
|
|
|
2023-09-24 21:21:58 +02:00
|
|
|
export type NotesParentType = 'Folder' | 'Tag' | 'SmartFilter';
|
|
|
|
|
|
|
|
export interface NotesParent {
|
|
|
|
type: NotesParentType;
|
|
|
|
selectedItemId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const serializeNotesParent = (n: NotesParent) => {
|
|
|
|
return JSON.stringify(n);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const parseNotesParent = (s: string, activeFolderId: string): NotesParent => {
|
|
|
|
const defaultValue: NotesParent = {
|
|
|
|
type: 'Folder',
|
|
|
|
selectedItemId: activeFolderId,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!s) return defaultValue;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const parsed = JSON.parse(s);
|
|
|
|
return parsed;
|
|
|
|
} catch (error) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getNotesParent = (state: State): NotesParent => {
|
|
|
|
let type = state.notesParentType as NotesParentType;
|
|
|
|
let selectedItemId = '';
|
|
|
|
|
|
|
|
if (type === 'Folder') {
|
|
|
|
selectedItemId = state.selectedFolderId;
|
|
|
|
} else if (type === 'Tag') {
|
|
|
|
selectedItemId = state.selectedTagId;
|
|
|
|
} else if (type === 'SmartFilter') {
|
|
|
|
selectedItemId = state.selectedSmartFilterId;
|
|
|
|
} else {
|
|
|
|
type = 'Folder';
|
|
|
|
selectedItemId = state.selectedFolderId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { type, selectedItemId };
|
|
|
|
};
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function changeSelectedFolder(draft: Draft<State>, action: any, options: any = null) {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!options) options = {};
|
|
|
|
draft.selectedFolderId = 'folderId' in action ? action.folderId : action.id;
|
|
|
|
if (!draft.selectedFolderId) {
|
|
|
|
draft.notesParentType = defaultNotesParentType(draft, 'Folder');
|
|
|
|
} else {
|
|
|
|
draft.notesParentType = 'Folder';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.clearSelectedNoteIds) draft.selectedNoteIds = [];
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function recordLastSelectedNoteIds(draft: Draft<State>, noteIds: string[]) {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-06-01 13:02:36 +02:00
|
|
|
const newOnes: any = { ...draft.lastSelectedNotesIds };
|
2020-10-09 19:35:46 +02:00
|
|
|
const parent = stateUtils.parentItem(draft);
|
|
|
|
if (!parent) return;
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
newOnes[parent.type] = { ...newOnes[parent.type] };
|
2020-10-09 19:35:46 +02:00
|
|
|
newOnes[parent.type][parent.id] = noteIds.slice();
|
|
|
|
|
|
|
|
draft.lastSelectedNotesIds = newOnes;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function changeSelectedNotes(draft: Draft<State>, action: any, options: any = null) {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!options) options = {};
|
|
|
|
|
|
|
|
let noteIds = [];
|
|
|
|
if (action.id) noteIds = [action.id];
|
|
|
|
if (action.ids) noteIds = action.ids;
|
|
|
|
if (action.noteId) noteIds = [action.noteId];
|
2023-08-21 17:01:20 +02:00
|
|
|
if (action.index) noteIds = [draft.notes[action.index].id];
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
if (action.type === 'NOTE_SELECT') {
|
|
|
|
if (JSON.stringify(draft.selectedNoteIds) === JSON.stringify(noteIds)) return;
|
|
|
|
draft.selectedNoteIds = noteIds;
|
|
|
|
draft.selectedNoteHash = action.hash ? action.hash : '';
|
|
|
|
} else if (action.type === 'NOTE_SELECT_ADD') {
|
|
|
|
if (!noteIds.length) return;
|
|
|
|
draft.selectedNoteIds = ArrayUtils.unique(draft.selectedNoteIds.concat(noteIds));
|
|
|
|
} else if (action.type === 'NOTE_SELECT_REMOVE') {
|
|
|
|
if (!noteIds.length) return; // Nothing to unselect
|
|
|
|
if (draft.selectedNoteIds.length <= 1) return; // Cannot unselect the last note
|
|
|
|
|
|
|
|
const newSelectedNoteIds = [];
|
|
|
|
for (let i = 0; i < draft.selectedNoteIds.length; i++) {
|
|
|
|
const id = draft.selectedNoteIds[i];
|
|
|
|
if (noteIds.indexOf(id) >= 0) continue;
|
|
|
|
newSelectedNoteIds.push(id);
|
|
|
|
}
|
|
|
|
draft.selectedNoteIds = newSelectedNoteIds;
|
|
|
|
} else if (action.type === 'NOTE_SELECT_TOGGLE') {
|
|
|
|
if (!noteIds.length) return;
|
|
|
|
|
|
|
|
if (draft.selectedNoteIds.indexOf(noteIds[0]) >= 0) {
|
|
|
|
changeSelectedNotes(draft, { type: 'NOTE_SELECT_REMOVE', id: noteIds[0] });
|
|
|
|
} else {
|
|
|
|
changeSelectedNotes(draft, { type: 'NOTE_SELECT_ADD', id: noteIds[0] });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error('Unreachable');
|
|
|
|
}
|
|
|
|
|
|
|
|
recordLastSelectedNoteIds(draft, draft.selectedNoteIds);
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function removeItemFromArray(array: any[], property: any, value: any) {
|
2020-10-09 19:35:46 +02:00
|
|
|
for (let i = 0; i !== array.length; ++i) {
|
|
|
|
const currentItem = array[i];
|
|
|
|
if (currentItem[property] === value) {
|
|
|
|
array = array.slice();
|
|
|
|
array.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const getContextFromHistory = (ctx: any) => {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const result: any = {};
|
2020-10-09 19:35:46 +02:00
|
|
|
result.notesParentType = ctx.notesParentType;
|
|
|
|
if (result.notesParentType === 'Folder') {
|
|
|
|
result.selectedFolderId = ctx.selectedFolderId;
|
|
|
|
} else if (result.notesParentType === 'Tag') {
|
|
|
|
result.selectedTagId = ctx.selectedTagId;
|
|
|
|
} else if (result.notesParentType === 'Search') {
|
|
|
|
result.selectedSearchId = ctx.selectedSearchId;
|
|
|
|
result.searches = ctx.searches;
|
|
|
|
} else if (result.notesParentType === 'SmartFilter') {
|
|
|
|
result.selectedSmartFilterId = ctx.selectedSmartFilterId;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function getNoteHistoryInfo(state: State) {
|
2020-10-18 22:52:10 +02:00
|
|
|
const selectedNoteIds = state.selectedNoteIds;
|
|
|
|
const notes = state.notes;
|
2022-07-23 09:31:32 +02:00
|
|
|
if (selectedNoteIds && selectedNoteIds.length > 0) {
|
2020-10-18 22:52:10 +02:00
|
|
|
const currNote = notes.find(note => note.id === selectedNoteIds[0]);
|
2022-07-23 09:31:32 +02:00
|
|
|
if (currNote) {
|
2020-10-18 22:52:10 +02:00
|
|
|
return {
|
|
|
|
id: currNote.id,
|
|
|
|
parent_id: currNote.parent_id,
|
|
|
|
notesParentType: state.notesParentType,
|
|
|
|
selectedFolderId: state.selectedFolderId,
|
|
|
|
selectedTagId: state.selectedTagId,
|
|
|
|
selectedSearchId: state.selectedSearchId,
|
|
|
|
searches: state.searches,
|
|
|
|
selectedSmartFilterId: state.selectedSmartFilterId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function handleHistory(draft: Draft<State>, action: any) {
|
2020-10-18 22:52:10 +02:00
|
|
|
const currentNote = getNoteHistoryInfo(draft);
|
2020-10-09 19:35:46 +02:00
|
|
|
switch (action.type) {
|
|
|
|
case 'HISTORY_BACKWARD': {
|
|
|
|
const note = draft.backwardHistoryNotes[draft.backwardHistoryNotes.length - 1];
|
2022-07-23 09:31:32 +02:00
|
|
|
if (currentNote && (draft.forwardHistoryNotes.length === 0 || currentNote.id !== draft.forwardHistoryNotes[draft.forwardHistoryNotes.length - 1].id)) {
|
2020-10-09 19:35:46 +02:00
|
|
|
draft.forwardHistoryNotes = draft.forwardHistoryNotes.concat(currentNote).slice(-MAX_HISTORY);
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
changeSelectedFolder(draft, { ...action, type: 'FOLDER_SELECT', folderId: note.parent_id });
|
|
|
|
changeSelectedNotes(draft, { ...action, type: 'NOTE_SELECT', noteId: note.id });
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
const ctx = draft.backwardHistoryNotes[draft.backwardHistoryNotes.length - 1];
|
|
|
|
Object.assign(draft, getContextFromHistory(ctx));
|
|
|
|
|
|
|
|
draft.backwardHistoryNotes.pop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'HISTORY_FORWARD': {
|
|
|
|
const note = draft.forwardHistoryNotes[draft.forwardHistoryNotes.length - 1];
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (currentNote && (draft.backwardHistoryNotes.length === 0 || currentNote.id !== draft.backwardHistoryNotes[draft.backwardHistoryNotes.length - 1].id)) {
|
2020-10-09 19:35:46 +02:00
|
|
|
draft.backwardHistoryNotes = draft.backwardHistoryNotes.concat(currentNote).slice(-MAX_HISTORY);
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
changeSelectedFolder(draft, { ...action, type: 'FOLDER_SELECT', folderId: note.parent_id });
|
|
|
|
changeSelectedNotes(draft, { ...action, type: 'NOTE_SELECT', noteId: note.id });
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
const ctx = draft.forwardHistoryNotes[draft.forwardHistoryNotes.length - 1];
|
|
|
|
Object.assign(draft, getContextFromHistory(ctx));
|
|
|
|
|
|
|
|
|
|
|
|
draft.forwardHistoryNotes.pop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'NOTE_SELECT':
|
2022-07-23 09:31:32 +02:00
|
|
|
if (currentNote && action.id !== currentNote.id) {
|
2020-10-09 19:35:46 +02:00
|
|
|
draft.forwardHistoryNotes = [];
|
|
|
|
draft.backwardHistoryNotes = draft.backwardHistoryNotes.concat(currentNote).slice(-MAX_HISTORY);
|
|
|
|
}
|
|
|
|
// History should be free from duplicates.
|
2022-07-23 09:31:32 +02:00
|
|
|
if (draft.backwardHistoryNotes && draft.backwardHistoryNotes.length > 0 &&
|
2020-10-09 19:35:46 +02:00
|
|
|
action.id === draft.backwardHistoryNotes[draft.backwardHistoryNotes.length - 1].id) {
|
|
|
|
draft.backwardHistoryNotes.pop();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'TAG_SELECT':
|
|
|
|
case 'FOLDER_AND_NOTE_SELECT':
|
|
|
|
case 'FOLDER_SELECT':
|
2022-07-23 09:31:32 +02:00
|
|
|
if (currentNote) {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (draft.forwardHistoryNotes.length) draft.forwardHistoryNotes = [];
|
|
|
|
draft.backwardHistoryNotes = draft.backwardHistoryNotes.concat(currentNote).slice(-MAX_HISTORY);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'NOTE_UPDATE_ONE': {
|
|
|
|
const modNote = action.note;
|
|
|
|
|
|
|
|
draft.backwardHistoryNotes = draft.backwardHistoryNotes.map(note => {
|
|
|
|
if (note.id === modNote.id) {
|
2023-06-01 13:02:36 +02:00
|
|
|
return { ...note, parent_id: modNote.parent_id, selectedFolderId: modNote.parent_id };
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
return note;
|
|
|
|
});
|
|
|
|
|
|
|
|
draft.forwardHistoryNotes = draft.forwardHistoryNotes.map(note => {
|
|
|
|
if (note.id === modNote.id) {
|
2023-06-01 13:02:36 +02:00
|
|
|
return { ...note, parent_id: modNote.parent_id, selectedFolderId: modNote.parent_id };
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
return note;
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'SEARCH_UPDATE':
|
2022-07-23 09:31:32 +02:00
|
|
|
if (currentNote && (draft.backwardHistoryNotes.length === 0 ||
|
|
|
|
draft.backwardHistoryNotes[draft.backwardHistoryNotes.length - 1].id !== currentNote.id)) {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (draft.forwardHistoryNotes.length) draft.forwardHistoryNotes = [];
|
|
|
|
draft.backwardHistoryNotes = draft.backwardHistoryNotes.concat(currentNote).slice(-MAX_HISTORY);
|
|
|
|
}
|
|
|
|
break;
|
2023-12-13 21:24:58 +02:00
|
|
|
|
|
|
|
case 'SEARCH_RESULTS_SET':
|
|
|
|
draft.searchResults = action.value;
|
|
|
|
break;
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
case 'FOLDER_DELETE':
|
2022-07-23 09:31:32 +02:00
|
|
|
draft.backwardHistoryNotes = draft.backwardHistoryNotes.filter(note => note.parent_id !== action.id);
|
|
|
|
draft.forwardHistoryNotes = draft.forwardHistoryNotes.filter(note => note.parent_id !== action.id);
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
draft.backwardHistoryNotes = removeAdjacentDuplicates(draft.backwardHistoryNotes);
|
|
|
|
draft.forwardHistoryNotes = removeAdjacentDuplicates(draft.forwardHistoryNotes);
|
|
|
|
break;
|
|
|
|
case 'NOTE_DELETE': {
|
2022-07-23 09:31:32 +02:00
|
|
|
draft.backwardHistoryNotes = draft.backwardHistoryNotes.filter(note => note.id !== action.id);
|
|
|
|
draft.forwardHistoryNotes = draft.forwardHistoryNotes.filter(note => note.id !== action.id);
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
draft.backwardHistoryNotes = removeAdjacentDuplicates(draft.backwardHistoryNotes);
|
|
|
|
draft.forwardHistoryNotes = removeAdjacentDuplicates(draft.forwardHistoryNotes);
|
|
|
|
|
|
|
|
// Fix the case where after deletion the currently selected note is also the latest in history
|
|
|
|
const selectedNoteIds = draft.selectedNoteIds;
|
|
|
|
if (selectedNoteIds.length && draft.backwardHistoryNotes.length && draft.backwardHistoryNotes[draft.backwardHistoryNotes.length - 1].id === selectedNoteIds[0]) {
|
|
|
|
draft.backwardHistoryNotes = draft.backwardHistoryNotes.slice(0, draft.backwardHistoryNotes.length - 1);
|
|
|
|
}
|
|
|
|
if (selectedNoteIds.length && draft.forwardHistoryNotes.length && draft.forwardHistoryNotes[draft.forwardHistoryNotes.length - 1].id === selectedNoteIds[0]) {
|
|
|
|
draft.forwardHistoryNotes = draft.forwardHistoryNotes.slice(0, draft.forwardHistoryNotes.length - 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const reducer = produce((draft: Draft<State> = defaultState, action: any) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
// const reducer = (state:State = defaultState, action:any) => {
|
|
|
|
// if (!['SIDE_MENU_OPEN_PERCENT'].includes(action.type)) console.info('Action', action.type, action);
|
|
|
|
|
|
|
|
// let newState = state;
|
|
|
|
|
|
|
|
// NOTE_DELETE requires post processing
|
|
|
|
if (action.type !== 'NOTE_DELETE') {
|
|
|
|
handleHistory(draft, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (action.type) {
|
|
|
|
|
|
|
|
case 'NOTE_SELECT':
|
|
|
|
case 'NOTE_SELECT_ADD':
|
|
|
|
case 'NOTE_SELECT_REMOVE':
|
|
|
|
case 'NOTE_SELECT_TOGGLE':
|
|
|
|
changeSelectedNotes(draft, action);
|
|
|
|
break;
|
|
|
|
case 'NOTE_SELECT_EXTEND':
|
|
|
|
{
|
|
|
|
if (!draft.selectedNoteIds.length) {
|
|
|
|
draft.selectedNoteIds = [action.id];
|
|
|
|
} else {
|
|
|
|
const selectRangeId1 = draft.selectedNoteIds[draft.selectedNoteIds.length - 1];
|
|
|
|
const selectRangeId2 = action.id;
|
|
|
|
if (selectRangeId1 === selectRangeId2) {
|
|
|
|
// Nothing
|
|
|
|
} else {
|
|
|
|
const newSelectedNoteIds = draft.selectedNoteIds.slice();
|
|
|
|
let selectionStarted = false;
|
|
|
|
for (let i = 0; i < draft.notes.length; i++) {
|
|
|
|
const id = draft.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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
draft.selectedNoteIds = newSelectedNoteIds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_SELECT_ALL':
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
draft.selectedNoteIds = draft.notes.map((n: any) => n.id);
|
2020-10-09 19:35:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_SELECT_ALL_TOGGLE': {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const allSelected = draft.notes.every((n: any) => draft.selectedNoteIds.includes(n.id));
|
2020-10-09 19:35:46 +02:00
|
|
|
if (allSelected) {
|
|
|
|
draft.selectedNoteIds = [];
|
|
|
|
} else {
|
|
|
|
draft.selectedNoteIds = draft.notes.map(n => n.id);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'SMART_FILTER_SELECT':
|
|
|
|
draft.notesParentType = 'SmartFilter';
|
|
|
|
draft.selectedSmartFilterId = action.id;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FOLDER_SELECT':
|
|
|
|
changeSelectedFolder(draft, action, { clearSelectedNoteIds: true });
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FOLDER_AND_NOTE_SELECT':
|
|
|
|
{
|
2022-03-03 15:33:52 +02:00
|
|
|
changeSelectedFolder(draft, action);
|
2023-06-01 13:02:36 +02:00
|
|
|
const noteSelectAction = { ...action, type: 'NOTE_SELECT' };
|
2022-03-03 15:33:52 +02:00
|
|
|
changeSelectedNotes(draft, noteSelectAction);
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SETTING_UPDATE_ALL':
|
|
|
|
draft.settings = action.settings;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SETTING_UPDATE_ONE':
|
|
|
|
{
|
2023-06-01 13:02:36 +02:00
|
|
|
const newSettings = { ...draft.settings };
|
2020-10-09 19:35:46 +02:00
|
|
|
newSettings[action.key] = action.value;
|
|
|
|
draft.settings = newSettings;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
case 'ITEMS_TRASHED':
|
|
|
|
|
|
|
|
draft.lastDeletion = {
|
|
|
|
...action.value,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'DELETION_NOTIFICATION_DONE':
|
|
|
|
|
|
|
|
draft.lastDeletionNotificationTime = Date.now();
|
|
|
|
break;
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
case 'NOTE_PROVISIONAL_FLAG_CLEAR':
|
|
|
|
{
|
|
|
|
const newIds = ArrayUtils.removeElement(draft.provisionalNoteIds, action.id);
|
|
|
|
if (newIds !== draft.provisionalNoteIds) {
|
|
|
|
draft.provisionalNoteIds = newIds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Replace all the notes with the provided array
|
|
|
|
case 'NOTE_UPDATE_ALL':
|
|
|
|
draft.notes = action.notes;
|
|
|
|
draft.notesSource = action.notesSource;
|
2024-01-03 20:02:05 +02:00
|
|
|
draft.noteListLastSortTime = Date.now(); // Notes are already sorted when they are set this way.
|
2020-10-09 19:35:46 +02:00
|
|
|
updateSelectedNotesFromExistingNotes(draft);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 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':
|
|
|
|
{
|
2024-03-02 16:25:27 +02:00
|
|
|
const modNote: NoteEntity = action.note;
|
2020-10-09 19:35:46 +02:00
|
|
|
const isViewingAllNotes = (draft.notesParentType === 'SmartFilter' && draft.selectedSmartFilterId === ALL_NOTES_FILTER_ID);
|
|
|
|
const isViewingConflictFolder = draft.notesParentType === 'Folder' && draft.selectedFolderId === Folder.conflictFolderId();
|
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
const noteIsInFolder = function(note: NoteEntity, folderId: string) {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (note.is_conflict && isViewingConflictFolder) return true;
|
2024-03-02 16:25:27 +02:00
|
|
|
const noteDisplayParentId = getDisplayParentId(note, draft.folders.find(f => f.id === note.parent_id));
|
|
|
|
return folderId === noteDisplayParentId;
|
2020-10-09 19:35:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let movedNotePreviousIndex = 0;
|
|
|
|
let noteFolderHasChanged = false;
|
2024-01-03 20:02:05 +02:00
|
|
|
const newNotes = draft.notes.slice();
|
2020-10-09 19:35:46 +02:00
|
|
|
let found = false;
|
|
|
|
for (let i = 0; i < newNotes.length; i++) {
|
|
|
|
const n = newNotes[i];
|
2022-07-23 09:31:32 +02:00
|
|
|
if (n.id === modNote.id) {
|
2020-11-19 18:21:24 +02:00
|
|
|
if (n.is_conflict && !modNote.is_conflict) {
|
|
|
|
// Note was a conflict but was moved outside of
|
|
|
|
// the conflict folder
|
|
|
|
newNotes.splice(i, 1);
|
|
|
|
noteFolderHasChanged = true;
|
|
|
|
movedNotePreviousIndex = i;
|
2024-03-02 16:25:27 +02:00
|
|
|
} else if (isViewingAllNotes || noteIsInFolder(modNote, draft.selectedFolderId)) {
|
2020-11-19 18:21:24 +02:00
|
|
|
// Note is still in the same folder
|
2020-10-09 19:35:46 +02:00
|
|
|
// Merge the properties that have changed (in modNote) into
|
|
|
|
// the object we already have.
|
2023-06-01 13:02:36 +02:00
|
|
|
newNotes[i] = { ...newNotes[i] };
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
for (const n in modNote) {
|
|
|
|
if (!modNote.hasOwnProperty(n)) continue;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-03-02 16:25:27 +02:00
|
|
|
(newNotes[i] as any)[n] = (modNote as any)[n];
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Note has moved to a different folder
|
|
|
|
newNotes.splice(i, 1);
|
|
|
|
noteFolderHasChanged = true;
|
|
|
|
movedNotePreviousIndex = i;
|
|
|
|
}
|
|
|
|
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 (isViewingAllNotes || noteIsInFolder(modNote, draft.selectedFolderId)) {
|
|
|
|
newNotes.push(modNote);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
draft.notes = newNotes;
|
|
|
|
|
|
|
|
if (noteFolderHasChanged) {
|
|
|
|
let newIndex = movedNotePreviousIndex;
|
|
|
|
if (newIndex >= newNotes.length) newIndex = newNotes.length - 1;
|
|
|
|
if (!newNotes.length) newIndex = -1;
|
|
|
|
draft.selectedNoteIds = newIndex >= 0 ? [newNotes[newIndex].id] : [];
|
|
|
|
}
|
|
|
|
|
2021-04-07 18:57:44 +02:00
|
|
|
if (!action.ignoreProvisionalFlag) {
|
|
|
|
let newProvisionalNoteIds = draft.provisionalNoteIds;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2021-04-07 18:57:44 +02:00
|
|
|
if (action.provisional) {
|
2020-10-09 19:35:46 +02:00
|
|
|
newProvisionalNoteIds = newProvisionalNoteIds.slice();
|
2021-04-07 18:57:44 +02:00
|
|
|
newProvisionalNoteIds.push(modNote.id);
|
|
|
|
} else {
|
|
|
|
const idx = newProvisionalNoteIds.indexOf(modNote.id);
|
|
|
|
if (idx >= 0) {
|
|
|
|
newProvisionalNoteIds = newProvisionalNoteIds.slice();
|
|
|
|
newProvisionalNoteIds.splice(idx, 1);
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 18:57:44 +02:00
|
|
|
draft.provisionalNoteIds = newProvisionalNoteIds;
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_DELETE':
|
|
|
|
|
|
|
|
{
|
|
|
|
handleItemDelete(draft, action);
|
|
|
|
|
|
|
|
const idx = draft.provisionalNoteIds.indexOf(action.id);
|
|
|
|
if (idx >= 0) {
|
|
|
|
const t = draft.provisionalNoteIds.slice();
|
|
|
|
t.splice(idx, 1);
|
|
|
|
draft.provisionalNoteIds = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2024-01-03 20:02:05 +02:00
|
|
|
case 'NOTE_SORT':
|
|
|
|
|
|
|
|
{
|
|
|
|
draft.notes = Note.sortNotes(draft.notes, stateUtils.notesOrder(draft.settings), draft.settings.uncompletedTodosOnTop);
|
|
|
|
draft.noteListLastSortTime = Date.now();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
case 'NOTE_IS_INSERTING_NOTES':
|
|
|
|
|
|
|
|
if (draft.isInsertingNotes !== action.value) {
|
|
|
|
draft.isInsertingNotes = action.value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'TAG_DELETE':
|
|
|
|
handleItemDelete(draft, action);
|
|
|
|
draft.selectedNoteTags = removeItemFromArray(draft.selectedNoteTags, 'id', action.id);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FOLDER_UPDATE_ALL':
|
|
|
|
draft.folders = action.items;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FOLDER_SET_COLLAPSED':
|
|
|
|
folderSetCollapsed(draft, action);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FOLDER_TOGGLE':
|
|
|
|
if (draft.collapsedFolderIds.indexOf(action.id) >= 0) {
|
2023-06-01 13:02:36 +02:00
|
|
|
folderSetCollapsed(draft, { collapsed: false, ...action });
|
2020-10-09 19:35:46 +02:00
|
|
|
} else {
|
2023-06-01 13:02:36 +02:00
|
|
|
folderSetCollapsed(draft, { collapsed: true, ...action });
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FOLDER_SET_COLLAPSED_ALL':
|
|
|
|
draft.collapsedFolderIds = action.ids.slice();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'TAG_UPDATE_ALL':
|
2022-06-08 11:33:53 +02:00
|
|
|
if (!fastDeepEqual(original(draft.tags), action.items)) {
|
|
|
|
draft.tags = action.items;
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'TAG_SELECT':
|
|
|
|
|
2020-11-09 15:33:12 +02:00
|
|
|
if (draft.selectedTagId !== action.id || draft.notesParentType !== 'Tag') {
|
2020-10-09 19:35:46 +02:00
|
|
|
draft.selectedTagId = action.id;
|
|
|
|
if (!action.id) {
|
|
|
|
draft.notesParentType = defaultNotesParentType(draft, 'Tag');
|
|
|
|
} else {
|
|
|
|
draft.notesParentType = 'Tag';
|
|
|
|
}
|
|
|
|
draft.selectedNoteIds = [];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'TAG_UPDATE_ONE':
|
|
|
|
{
|
|
|
|
// We only want to update the selected note tags if the tag belongs to the currently open note
|
|
|
|
const selectedNoteHasTag = !!draft.selectedNoteTags.find(tag => tag.id === action.item.id);
|
|
|
|
updateOneItem(draft, action);
|
|
|
|
if (selectedNoteHasTag) updateOneItem(draft, action, 'selectedNoteTags');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_TAG_REMOVE':
|
|
|
|
{
|
|
|
|
updateOneItem(draft, action, 'tags');
|
|
|
|
const tagRemoved = action.item;
|
|
|
|
draft.selectedNoteTags = removeItemFromArray(draft.selectedNoteTags, 'id', tagRemoved.id);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'EDITOR_NOTE_STATUS_SET':
|
|
|
|
|
|
|
|
{
|
|
|
|
draft.editorNoteStatuses[action.id] = action.status;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'EDITOR_NOTE_STATUS_REMOVE':
|
|
|
|
|
|
|
|
{
|
|
|
|
delete draft.editorNoteStatuses[action.id];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FOLDER_UPDATE_ONE':
|
|
|
|
case 'MASTERKEY_UPDATE_ONE':
|
|
|
|
updateOneItem(draft, action);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FOLDER_DELETE':
|
|
|
|
handleItemDelete(draft, action);
|
|
|
|
break;
|
|
|
|
|
2021-08-12 17:54:10 +02:00
|
|
|
// case 'MASTERKEY_UPDATE_ALL':
|
|
|
|
// draft.masterKeys = action.items;
|
|
|
|
// break;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
case 'MASTERKEY_SET_NOT_LOADED':
|
|
|
|
draft.notLoadedMasterKeys = action.ids;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'MASTERKEY_ADD_NOT_LOADED':
|
|
|
|
{
|
|
|
|
if (draft.notLoadedMasterKeys.indexOf(action.id) < 0) {
|
|
|
|
const keys = draft.notLoadedMasterKeys.slice();
|
|
|
|
keys.push(action.id);
|
|
|
|
draft.notLoadedMasterKeys = keys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
const index = draft.notLoadedMasterKeys.indexOf(id);
|
|
|
|
if (index >= 0) {
|
|
|
|
const keys = draft.notLoadedMasterKeys.slice();
|
|
|
|
keys.splice(index, 1);
|
|
|
|
draft.notLoadedMasterKeys = keys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SYNC_STARTED':
|
|
|
|
draft.syncStarted = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SYNC_COMPLETED':
|
|
|
|
draft.syncStarted = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SYNC_REPORT_UPDATE':
|
|
|
|
draft.syncReport = action.report;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SEARCH_QUERY':
|
|
|
|
draft.searchQuery = action.query.trim();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SEARCH_ADD':
|
|
|
|
{
|
|
|
|
const searches = draft.searches.slice();
|
|
|
|
searches.push(action.search);
|
|
|
|
draft.searches = searches;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SEARCH_UPDATE':
|
|
|
|
{
|
|
|
|
const searches = draft.searches.slice();
|
|
|
|
let found = false;
|
|
|
|
for (let i = 0; i < searches.length; i++) {
|
|
|
|
if (searches[i].id === action.search.id) {
|
2023-06-01 13:02:36 +02:00
|
|
|
searches[i] = { ...action.search };
|
2020-10-09 19:35:46 +02:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) searches.push(action.search);
|
|
|
|
|
2020-11-09 14:07:37 +02:00
|
|
|
draft.notesParentType = 'Search';
|
2020-10-09 19:35:46 +02:00
|
|
|
draft.searches = searches;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SEARCH_DELETE':
|
|
|
|
handleItemDelete(draft, action);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SEARCH_SELECT':
|
|
|
|
draft.selectedSearchId = action.id;
|
|
|
|
if (!action.id) {
|
|
|
|
draft.notesParentType = defaultNotesParentType(draft, 'Search');
|
|
|
|
} else {
|
|
|
|
draft.notesParentType = 'Search';
|
|
|
|
}
|
|
|
|
draft.selectedNoteIds = [];
|
|
|
|
break;
|
|
|
|
case 'SET_HIGHLIGHTED':
|
|
|
|
draft.highlightedWords = action.words;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'APP_STATE_SET':
|
|
|
|
draft.appState = action.state;
|
|
|
|
break;
|
|
|
|
|
2023-02-10 18:12:14 +02:00
|
|
|
case 'BIOMETRICS_DONE_SET':
|
|
|
|
draft.biometricsDone = action.value;
|
|
|
|
break;
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
case 'SYNC_HAS_DISABLED_SYNC_ITEMS':
|
2024-01-27 18:59:19 +02:00
|
|
|
draft.hasDisabledSyncItems = 'value' in action ? action.value : true;
|
2020-10-09 19:35:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ENCRYPTION_HAS_DISABLED_ITEMS':
|
|
|
|
draft.hasDisabledEncryptionItems = action.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'CLIPPER_SERVER_SET':
|
|
|
|
{
|
2023-06-01 13:02:36 +02:00
|
|
|
const clipperServer = { ...draft.clipperServer };
|
2020-10-09 19:35:46 +02:00
|
|
|
if ('startState' in action) clipperServer.startState = action.startState;
|
|
|
|
if ('port' in action) clipperServer.port = action.port;
|
|
|
|
draft.clipperServer = clipperServer;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'DECRYPTION_WORKER_SET':
|
|
|
|
{
|
2023-06-01 13:02:36 +02:00
|
|
|
const decryptionWorker = { ...draft.decryptionWorker };
|
2020-10-09 19:35:46 +02:00
|
|
|
for (const n in action) {
|
|
|
|
if (!action.hasOwnProperty(n) || n === 'type') continue;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-10-09 19:35:46 +02:00
|
|
|
(decryptionWorker as any)[n] = action[n];
|
|
|
|
}
|
|
|
|
draft.decryptionWorker = decryptionWorker;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'RESOURCE_FETCHER_SET':
|
|
|
|
{
|
2023-06-01 13:02:36 +02:00
|
|
|
const rf = { ...action };
|
2020-10-09 19:35:46 +02:00
|
|
|
delete rf.type;
|
|
|
|
draft.resourceFetcher = rf;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-07-23 12:05:21 +02:00
|
|
|
case 'CUSTOM_CSS_APPEND':
|
|
|
|
draft.customCss += action.css;
|
2020-10-09 19:35:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SET_NOTE_TAGS':
|
2022-06-08 11:34:08 +02:00
|
|
|
if (!fastDeepEqual(original(draft.selectedNoteTags), action.items)) {
|
|
|
|
draft.selectedNoteTags = action.items;
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'PLUGINLEGACY_DIALOG_SET':
|
|
|
|
{
|
|
|
|
if (!action.pluginName) throw new Error('action.pluginName not specified');
|
2023-06-01 13:02:36 +02:00
|
|
|
const newPluginsLegacy = { ...draft.pluginsLegacy };
|
|
|
|
const newPlugin = draft.pluginsLegacy[action.pluginName] ? { ...draft.pluginsLegacy[action.pluginName] } : {};
|
2020-10-09 19:35:46 +02:00
|
|
|
if ('open' in action) newPlugin.dialogOpen = action.open;
|
2020-10-18 22:52:10 +02:00
|
|
|
if ('userData' in action) newPlugin.userData = action.userData;
|
2020-10-09 19:35:46 +02:00
|
|
|
newPluginsLegacy[action.pluginName] = newPlugin;
|
|
|
|
draft.pluginsLegacy = newPluginsLegacy;
|
|
|
|
}
|
|
|
|
break;
|
2021-06-22 20:57:04 +02:00
|
|
|
|
|
|
|
case 'API_NEED_AUTH_SET':
|
|
|
|
draft.needApiAuth = action.value;
|
|
|
|
break;
|
|
|
|
|
2022-04-11 17:49:32 +02:00
|
|
|
case 'PROFILE_CONFIG_SET':
|
|
|
|
draft.profileConfig = action.value;
|
|
|
|
break;
|
|
|
|
|
2024-01-26 12:32:35 +02:00
|
|
|
case 'MUST_UPGRADE_APP':
|
|
|
|
draft.mustUpgradeAppMessage = action.message;
|
|
|
|
break;
|
|
|
|
|
2023-09-18 18:40:36 +02:00
|
|
|
case 'NOTE_LIST_RENDERER_ADD':
|
2023-09-19 11:13:13 +02:00
|
|
|
{
|
|
|
|
const noteListRendererIds = draft.noteListRendererIds.slice();
|
|
|
|
if (noteListRendererIds.includes(action.value)) throw new Error(`Note list renderer is already registered: ${action.value}`);
|
|
|
|
noteListRendererIds.push(action.value);
|
|
|
|
draft.noteListRendererIds = noteListRendererIds;
|
|
|
|
}
|
2023-09-18 18:40:36 +02:00
|
|
|
break;
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
error.message = `In reducer: ${error.message} Action: ${JSON.stringify(action)}`;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type.indexOf('NOTE_UPDATE') === 0 || action.type.indexOf('FOLDER_UPDATE') === 0 || action.type.indexOf('TAG_UPDATE') === 0) {
|
|
|
|
draft.hasEncryptedItems = stateHasEncryptedItems(draft);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'NOTE_DELETE') {
|
|
|
|
handleHistory(draft, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const additionalReducer of additionalReducers) {
|
|
|
|
additionalReducer.reducer(draft, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if (Setting.value('env') === 'dev') {
|
|
|
|
// return Object.freeze(newState);
|
|
|
|
// } else {
|
|
|
|
// return newState;
|
|
|
|
// }
|
|
|
|
});
|
|
|
|
|
|
|
|
export default reducer;
|