Repository URL to install this package:
| 
          
        
        Version: 
           
    
          3.1.7  ▾
        
   | 
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAppDefaultState = exports.AppStateDialogName = void 0;
const immer_1 = require("immer");
const Setting_1 = require("@joplin/lib/models/Setting");
const reducer_1 = require("@joplin/lib/reducer");
const iterateItems_1 = require("./gui/ResizableLayout/utils/iterateItems");
const validateLayout_1 = require("./gui/ResizableLayout/utils/validateLayout");
const Logger_1 = require("@joplin/utils/Logger");
const logger = Logger_1.default.create('app.reducer');
var AppStateDialogName;
(function (AppStateDialogName) {
    AppStateDialogName["SyncWizard"] = "syncWizard";
    AppStateDialogName["MasterPassword"] = "masterPassword";
})(AppStateDialogName || (exports.AppStateDialogName = AppStateDialogName = {}));
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
function createAppDefaultState(windowContentSize, resourceEditWatcherDefaultState) {
    return Object.assign(Object.assign(Object.assign({}, reducer_1.defaultState), { route: {
            type: 'NAV_GO',
            routeName: 'Main',
            props: {},
        }, navHistory: [], noteVisiblePanes: ['editor', 'viewer'], windowContentSize, watchedNoteFiles: [], lastEditorScrollPercents: {}, devToolsVisible: false, visibleDialogs: {}, focusedField: null, layoutMoveMode: false, mainLayout: null, startupPluginsLoaded: false, dialogs: [], isResettingLayout: false }), resourceEditWatcherDefaultState);
}
exports.createAppDefaultState = createAppDefaultState;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
function default_1(state, action) {
    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;
                    newState = Object.assign({}, state);
                    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 = Object.assign(Object.assign({}, newState), { startupPluginsLoaded: action.value, mainLayout: JSON.parse(JSON.stringify(newState.mainLayout)) });
                }
                break;
            case 'WINDOW_CONTENT_SIZE_SET':
                newState = Object.assign({}, state);
                newState.windowContentSize = action.size;
                break;
            case 'NOTE_VISIBLE_PANES_TOGGLE':
                {
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
                    const getNextLayout = (currentLayout) => {
                        currentLayout = panes.length === 2 ? 'both' : currentLayout[0];
                        let paneOptions;
                        if (state.settings.layoutButtonSequence === Setting_1.default.LAYOUT_EDITOR_VIEWER) {
                            paneOptions = ['editor', 'viewer'];
                        }
                        else if (state.settings.layoutButtonSequence === Setting_1.default.LAYOUT_EDITOR_SPLIT) {
                            paneOptions = ['editor', 'both'];
                        }
                        else if (state.settings.layoutButtonSequence === Setting_1.default.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];
                    };
                    newState = Object.assign({}, state);
                    const panes = state.noteVisiblePanes.slice();
                    newState.noteVisiblePanes = getNextLayout(panes);
                }
                break;
            case 'NOTE_VISIBLE_PANES_SET':
                newState = Object.assign({}, state);
                newState.noteVisiblePanes = action.panes;
                break;
            case 'MAIN_LAYOUT_SET':
                newState = Object.assign(Object.assign({}, state), { mainLayout: action.value });
                break;
            case 'MAIN_LAYOUT_SET_ITEM_PROP':
                {
                    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 = (0, immer_1.default)(state.mainLayout, (draftLayout) => {
                            (0, iterateItems_1.default)(draftLayout, (_itemIndex, item, _parent) => {
                                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) {
                                        // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
                                        item[action.propName] = action.propValue;
                                        return false;
                                    }
                                }
                                return true;
                            });
                        });
                        if (newLayout !== state.mainLayout)
                            newLayout = (0, validateLayout_1.default)(newLayout);
                        newState = Object.assign(Object.assign({}, state), { mainLayout: newLayout });
                    }
                }
                break;
            case 'NOTE_FILE_WATCHER_ADD':
                if (newState.watchedNoteFiles.indexOf(action.id) < 0) {
                    newState = Object.assign({}, state);
                    const watchedNoteFiles = newState.watchedNoteFiles.slice();
                    watchedNoteFiles.push(action.id);
                    newState.watchedNoteFiles = watchedNoteFiles;
                }
                break;
            case 'NOTE_FILE_WATCHER_REMOVE':
                {
                    newState = Object.assign({}, state);
                    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) {
                    newState = Object.assign({}, state);
                    newState.watchedNoteFiles = [];
                }
                break;
            case 'EDITOR_SCROLL_PERCENT_SET':
                {
                    newState = Object.assign({}, state);
                    const newPercents = Object.assign({}, newState.lastEditorScrollPercents);
                    newPercents[action.noteId] = action.percent;
                    newState.lastEditorScrollPercents = newPercents;
                }
                break;
            case 'NOTE_DEVTOOLS_TOGGLE':
                newState = Object.assign({}, state);
                newState.devToolsVisible = !newState.devToolsVisible;
                break;
            case 'NOTE_DEVTOOLS_SET':
                newState = Object.assign({}, state);
                newState.devToolsVisible = action.value;
                break;
            case 'VISIBLE_DIALOGS_ADD':
                newState = Object.assign({}, state);
                newState.visibleDialogs = Object.assign({}, newState.visibleDialogs);
                newState.visibleDialogs[action.name] = true;
                break;
            case 'VISIBLE_DIALOGS_REMOVE':
                newState = Object.assign({}, state);
                newState.visibleDialogs = Object.assign({}, newState.visibleDialogs);
                delete newState.visibleDialogs[action.name];
                break;
            case 'FOCUS_SET':
                newState = Object.assign({}, state);
                newState.focusedField = action.field;
                break;
            case 'FOCUS_CLEAR':
                // A field can only clear its own state
                if (action.field === state.focusedField) {
                    newState = Object.assign({}, state);
                    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;
                    }
                    newState = Object.assign({}, state);
                    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,
                            props: action.props || {},
                        });
                        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 = Object.assign(Object.assign({}, state), { layoutMoveMode: action.value });
                break;
            case 'RESET_LAYOUT':
                newState = Object.assign(Object.assign({}, state), { isResettingLayout: action.value });
                break;
        }
    }
    catch (error) {
        error.message = `In reducer: ${error.message} Action: ${JSON.stringify(action)}`;
        throw error;
    }
    return newState;
}
exports.default = default_1;
//# sourceMappingURL=app.reducer.js.map