2021-09-04 13:37:22 +02:00
|
|
|
import produce from 'immer';
|
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
2021-09-04 19:11:29 +02:00
|
|
|
import { defaultState, State } from '@joplin/lib/reducer';
|
2021-09-04 13:37:22 +02:00
|
|
|
import iterateItems from './gui/ResizableLayout/utils/iterateItems';
|
|
|
|
import { LayoutItem } from './gui/ResizableLayout/utils/types';
|
|
|
|
import validateLayout from './gui/ResizableLayout/utils/validateLayout';
|
2023-07-27 17:05:56 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
2023-06-06 17:31:31 +02:00
|
|
|
|
|
|
|
const logger = Logger.create('app.reducer');
|
2021-09-04 13:37:22 +02:00
|
|
|
|
2021-09-04 19:11:29 +02:00
|
|
|
export interface AppStateRoute {
|
|
|
|
type: string;
|
|
|
|
routeName: string;
|
|
|
|
props: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum AppStateDialogName {
|
|
|
|
SyncWizard = 'syncWizard',
|
|
|
|
MasterPassword = 'masterPassword',
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AppStateDialog {
|
|
|
|
name: AppStateDialogName;
|
2021-11-15 19:19:51 +02:00
|
|
|
props: Record<string, any>;
|
2021-09-04 19:11:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface AppState extends State {
|
|
|
|
route: AppStateRoute;
|
|
|
|
navHistory: any[];
|
|
|
|
noteVisiblePanes: string[];
|
|
|
|
windowContentSize: any;
|
|
|
|
watchedNoteFiles: string[];
|
|
|
|
lastEditorScrollPercents: any;
|
|
|
|
devToolsVisible: boolean;
|
|
|
|
visibleDialogs: any; // empty object if no dialog is visible. Otherwise contains the list of visible dialogs.
|
|
|
|
focusedField: string;
|
|
|
|
layoutMoveMode: boolean;
|
|
|
|
startupPluginsLoaded: boolean;
|
|
|
|
|
|
|
|
// Extra reducer keys go here
|
|
|
|
watchedResources: any;
|
|
|
|
mainLayout: LayoutItem;
|
|
|
|
dialogs: AppStateDialog[];
|
2023-02-17 15:07:18 +02:00
|
|
|
isResettingLayout: boolean;
|
2021-09-04 19:11:29 +02:00
|
|
|
}
|
|
|
|
|
2021-09-04 13:37:22 +02:00
|
|
|
export function createAppDefaultState(windowContentSize: any, resourceEditWatcherDefaultState: any): AppState {
|
|
|
|
return {
|
|
|
|
...defaultState,
|
|
|
|
route: {
|
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: 'Main',
|
|
|
|
props: {},
|
|
|
|
},
|
|
|
|
navHistory: [],
|
|
|
|
noteVisiblePanes: ['editor', 'viewer'],
|
|
|
|
windowContentSize, // bridge().windowContentSize(),
|
|
|
|
watchedNoteFiles: [],
|
|
|
|
lastEditorScrollPercents: {},
|
|
|
|
devToolsVisible: false,
|
|
|
|
visibleDialogs: {}, // empty object if no dialog is visible. Otherwise contains the list of visible dialogs.
|
|
|
|
focusedField: null,
|
|
|
|
layoutMoveMode: false,
|
|
|
|
mainLayout: null,
|
|
|
|
startupPluginsLoaded: false,
|
|
|
|
dialogs: [],
|
2023-02-17 15:07:18 +02:00
|
|
|
isResettingLayout: false,
|
2021-09-04 13:37:22 +02:00
|
|
|
...resourceEditWatcherDefaultState,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function(state: AppState, action: any) {
|
|
|
|
let newState = state;
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (action.type) {
|
|
|
|
|
|
|
|
case 'NAV_BACK':
|
|
|
|
case 'NAV_GO':
|
|
|
|
|
|
|
|
{
|
|
|
|
const goingBack = action.type === 'NAV_BACK';
|
|
|
|
|
|
|
|
if (goingBack && !state.navHistory.length) break;
|
|
|
|
|
|
|
|
const currentRoute = state.route;
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
const newNavHistory = state.navHistory.slice();
|
|
|
|
|
|
|
|
if (goingBack) {
|
|
|
|
let newAction = null;
|
|
|
|
while (newNavHistory.length) {
|
|
|
|
newAction = newNavHistory.pop();
|
|
|
|
if (newAction.routeName !== state.route.routeName) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newAction) break;
|
|
|
|
|
|
|
|
action = newAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!goingBack) newNavHistory.push(currentRoute);
|
|
|
|
newState.navHistory = newNavHistory;
|
|
|
|
newState.route = action;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'STARTUP_PLUGINS_LOADED':
|
|
|
|
|
|
|
|
// When all startup plugins have loaded, we also recreate the
|
|
|
|
// main layout to ensure that it is updated in the UI. There's
|
|
|
|
// probably a cleaner way to do this, but for now that will do.
|
|
|
|
if (state.startupPluginsLoaded !== action.value) {
|
|
|
|
newState = {
|
|
|
|
...newState,
|
|
|
|
startupPluginsLoaded: action.value,
|
|
|
|
mainLayout: JSON.parse(JSON.stringify(newState.mainLayout)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'WINDOW_CONTENT_SIZE_SET':
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
newState.windowContentSize = action.size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_VISIBLE_PANES_TOGGLE':
|
|
|
|
|
|
|
|
{
|
|
|
|
const getNextLayout = (currentLayout: any) => {
|
|
|
|
currentLayout = panes.length === 2 ? 'both' : currentLayout[0];
|
|
|
|
|
|
|
|
let paneOptions;
|
|
|
|
if (state.settings.layoutButtonSequence === Setting.LAYOUT_EDITOR_VIEWER) {
|
|
|
|
paneOptions = ['editor', 'viewer'];
|
|
|
|
} else if (state.settings.layoutButtonSequence === Setting.LAYOUT_EDITOR_SPLIT) {
|
|
|
|
paneOptions = ['editor', 'both'];
|
|
|
|
} else if (state.settings.layoutButtonSequence === Setting.LAYOUT_VIEWER_SPLIT) {
|
|
|
|
paneOptions = ['viewer', 'both'];
|
|
|
|
} else {
|
|
|
|
paneOptions = ['editor', 'viewer', 'both'];
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentLayoutIndex = paneOptions.indexOf(currentLayout);
|
|
|
|
const nextLayoutIndex = currentLayoutIndex === paneOptions.length - 1 ? 0 : currentLayoutIndex + 1;
|
|
|
|
|
|
|
|
const nextLayout = paneOptions[nextLayoutIndex];
|
|
|
|
return nextLayout === 'both' ? ['editor', 'viewer'] : [nextLayout];
|
|
|
|
};
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
|
|
|
|
const panes = state.noteVisiblePanes.slice();
|
|
|
|
newState.noteVisiblePanes = getNextLayout(panes);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_VISIBLE_PANES_SET':
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
newState.noteVisiblePanes = action.panes;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'MAIN_LAYOUT_SET':
|
|
|
|
|
|
|
|
newState = {
|
|
|
|
...state,
|
|
|
|
mainLayout: action.value,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'MAIN_LAYOUT_SET_ITEM_PROP':
|
|
|
|
|
|
|
|
{
|
2023-06-06 17:31:31 +02:00
|
|
|
if (!state.mainLayout) {
|
|
|
|
logger.warn('MAIN_LAYOUT_SET_ITEM_PROP: Trying to set an item prop on the layout, but layout is empty: ', JSON.stringify(action));
|
|
|
|
} else {
|
|
|
|
let newLayout = produce(state.mainLayout, (draftLayout: LayoutItem) => {
|
|
|
|
iterateItems(draftLayout, (_itemIndex: number, item: LayoutItem, _parent: LayoutItem) => {
|
|
|
|
if (!item) {
|
|
|
|
logger.warn('MAIN_LAYOUT_SET_ITEM_PROP: Found an empty item in layout: ', JSON.stringify(state.mainLayout));
|
|
|
|
} else {
|
|
|
|
if (item.key === action.itemKey) {
|
|
|
|
(item as any)[action.propName] = action.propValue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2021-09-04 13:37:22 +02:00
|
|
|
});
|
|
|
|
|
2023-06-06 17:31:31 +02:00
|
|
|
if (newLayout !== state.mainLayout) newLayout = validateLayout(newLayout);
|
2021-09-04 13:37:22 +02:00
|
|
|
|
2023-06-06 17:31:31 +02:00
|
|
|
newState = {
|
|
|
|
...state,
|
|
|
|
mainLayout: newLayout,
|
|
|
|
};
|
|
|
|
}
|
2021-09-04 13:37:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_FILE_WATCHER_ADD':
|
|
|
|
|
|
|
|
if (newState.watchedNoteFiles.indexOf(action.id) < 0) {
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
const watchedNoteFiles = newState.watchedNoteFiles.slice();
|
|
|
|
watchedNoteFiles.push(action.id);
|
|
|
|
newState.watchedNoteFiles = watchedNoteFiles;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_FILE_WATCHER_REMOVE':
|
|
|
|
|
|
|
|
{
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
const idx = newState.watchedNoteFiles.indexOf(action.id);
|
|
|
|
if (idx >= 0) {
|
|
|
|
const watchedNoteFiles = newState.watchedNoteFiles.slice();
|
|
|
|
watchedNoteFiles.splice(idx, 1);
|
|
|
|
newState.watchedNoteFiles = watchedNoteFiles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_FILE_WATCHER_CLEAR':
|
|
|
|
|
|
|
|
if (state.watchedNoteFiles.length) {
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
newState.watchedNoteFiles = [];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'EDITOR_SCROLL_PERCENT_SET':
|
|
|
|
|
|
|
|
{
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
|
|
|
const newPercents = { ...newState.lastEditorScrollPercents };
|
2021-09-04 13:37:22 +02:00
|
|
|
newPercents[action.noteId] = action.percent;
|
|
|
|
newState.lastEditorScrollPercents = newPercents;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_DEVTOOLS_TOGGLE':
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
newState.devToolsVisible = !newState.devToolsVisible;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NOTE_DEVTOOLS_SET':
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
newState.devToolsVisible = action.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'VISIBLE_DIALOGS_ADD':
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
|
|
|
newState.visibleDialogs = { ...newState.visibleDialogs };
|
2021-09-04 13:37:22 +02:00
|
|
|
newState.visibleDialogs[action.name] = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'VISIBLE_DIALOGS_REMOVE':
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
|
|
|
newState.visibleDialogs = { ...newState.visibleDialogs };
|
2021-09-04 13:37:22 +02:00
|
|
|
delete newState.visibleDialogs[action.name];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FOCUS_SET':
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
newState.focusedField = action.field;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FOCUS_CLEAR':
|
|
|
|
|
|
|
|
// A field can only clear its own state
|
|
|
|
if (action.field === state.focusedField) {
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
newState.focusedField = null;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'DIALOG_OPEN':
|
|
|
|
case 'DIALOG_CLOSE':
|
|
|
|
|
|
|
|
{
|
|
|
|
let isOpen = true;
|
|
|
|
|
|
|
|
if (action.type === 'DIALOG_CLOSE') {
|
|
|
|
isOpen = false;
|
|
|
|
} else { // DIALOG_OPEN
|
|
|
|
isOpen = action.isOpen !== false;
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
newState = { ...state };
|
2021-09-04 13:37:22 +02:00
|
|
|
|
|
|
|
if (isOpen) {
|
|
|
|
const newDialogs = newState.dialogs.slice();
|
|
|
|
|
|
|
|
if (newDialogs.find(d => d.name === action.name)) throw new Error(`Trying to open a dialog is already open: ${action.name}`);
|
|
|
|
|
|
|
|
newDialogs.push({
|
|
|
|
name: action.name,
|
2021-11-15 19:19:51 +02:00
|
|
|
props: action.props || {},
|
2021-09-04 13:37:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
newState.dialogs = newDialogs;
|
|
|
|
} else {
|
|
|
|
if (!newState.dialogs.find(d => d.name === action.name)) throw new Error(`Trying to close a dialog that is not open: ${action.name}`);
|
|
|
|
const newDialogs = newState.dialogs.slice().filter(d => d.name !== action.name);
|
|
|
|
newState.dialogs = newDialogs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'LAYOUT_MOVE_MODE_SET':
|
|
|
|
|
|
|
|
newState = {
|
|
|
|
...state,
|
|
|
|
layoutMoveMode: action.value,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
2023-02-17 15:07:18 +02:00
|
|
|
|
|
|
|
case 'RESET_LAYOUT':
|
|
|
|
newState = {
|
|
|
|
...state,
|
|
|
|
isResettingLayout: action.value,
|
|
|
|
};
|
|
|
|
break;
|
2021-09-04 13:37:22 +02:00
|
|
|
}
|
2023-02-17 15:07:18 +02:00
|
|
|
|
2021-09-04 13:37:22 +02:00
|
|
|
} catch (error) {
|
|
|
|
error.message = `In reducer: ${error.message} Action: ${JSON.stringify(action)}`;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newState;
|
|
|
|
}
|