silverbullet/web/reducer.ts

242 lines
6.7 KiB
TypeScript
Raw Normal View History

import { PageMeta } from "../plug-api/types.ts";
2024-02-09 04:12:23 +08:00
import { Action, AppViewState } from "../type/web.ts";
import { PageState } from "./navigator.ts";
export default function reducer(
state: AppViewState,
action: Action,
): AppViewState {
// console.log("Got action", action);
switch (action.type) {
case "page-loading":
return {
...state,
isLoading: true,
currentPage: action.name,
2023-12-22 01:37:50 +08:00
panels: state.currentPage === action.name ? state.panels : {
2023-12-20 00:20:47 +08:00
...state.panels,
// Hide these by default to avoid flickering
top: {},
bottom: {},
},
};
case "page-loaded": {
const mouseDetected = window.matchMedia("(pointer:fine)").matches;
const pageMeta = state.allPages.find(p => p.name == action.meta.name);
const decor = state.settings.decorations?.filter(d => pageMeta?.tags?.some(t => d.tag === t));
if (decor && decor.length > 0) {
const mergedDecorations = decor.reduceRight((accumulator, el) => {
accumulator = {...accumulator, ...el};
return accumulator;
});
if (mergedDecorations) {
const { tag, ...currPageDecorations } = mergedDecorations;
action.meta.pageDecorations = currPageDecorations;
}
}
return {
...state,
isLoading: false,
isMobile: !mouseDetected,
allPages: state.allPages.map((pageMeta) =>
pageMeta.name === action.meta.name
? { ...pageMeta, lastOpened: Date.now() }
: pageMeta
),
2022-05-17 17:53:17 +08:00
currentPage: action.meta.name,
2023-01-16 18:28:59 +08:00
currentPageMeta: action.meta,
};
}
case "page-changed":
return {
...state,
unsavedChanges: true,
};
case "page-saved":
return {
...state,
unsavedChanges: false,
};
case "sync-change":
return {
...state,
2023-08-16 17:40:31 +08:00
syncFailures: action.syncSuccess ? 0 : state.syncFailures + 1,
};
2024-01-25 18:42:36 +08:00
case "settings-loaded":
return {
...state,
settings: action.settings,
};
2023-12-22 22:55:50 +08:00
case "update-page-list": {
// Let's move over any "lastOpened" times to the "allPages" list
2022-10-16 01:02:56 +08:00
const oldPageMeta = new Map(
[...state.allPages].map((pm) => [pm.name, pm]),
);
let currPageMeta = oldPageMeta.get(state.currentPage!);
if (currPageMeta === undefined) {
currPageMeta = {} as PageMeta;
}
2023-12-22 22:55:50 +08:00
for (const pageMeta of action.allPages) {
2022-10-16 01:02:56 +08:00
const oldPageMetaItem = oldPageMeta.get(pageMeta.name);
if (oldPageMetaItem && oldPageMetaItem.lastOpened) {
pageMeta.lastOpened = oldPageMetaItem.lastOpened;
}
const decor = state.settings.decorations?.filter(d => pageMeta.tags?.some((t: any) => d.tag === t));
// Page can have multiple decorations applied via different tags, accumulate them.
// The decorations higher in the decorations list defined in SETTINGS gets
// higher precedence.
if (decor && decor.length > 0) {
const mergedDecorations = decor.reduceRight((accumulator, el) => {
accumulator = {...accumulator, ...el};
return accumulator;
});
if (mergedDecorations) {
const { tag, ...currPageDecorations} = mergedDecorations;
pageMeta.pageDecorations = currPageDecorations;
if (pageMeta.name === state.currentPage) {
currPageMeta!.pageDecorations = currPageDecorations;
}
}
}
}
2023-12-22 22:55:50 +08:00
return {
...state,
allPages: action.allPages,
currentPageMeta: currPageMeta,
2023-12-22 22:55:50 +08:00
};
}
case "start-navigate": {
return {
...state,
2023-12-22 01:37:50 +08:00
showPageNavigator: true,
pageNavigatorMode: action.mode,
2023-12-22 01:37:50 +08:00
showCommandPalette: false,
showFilterBox: false,
};
2022-10-16 01:02:56 +08:00
}
2023-12-22 01:37:50 +08:00
case "stop-navigate":
return {
...state,
showPageNavigator: false,
};
2022-10-16 01:02:56 +08:00
case "show-palette": {
return {
...state,
showCommandPalette: true,
showPageNavigator: false,
showFilterBox: false,
showCommandPaletteContext: action.context,
};
2022-10-16 01:02:56 +08:00
}
case "hide-palette":
return {
...state,
showCommandPalette: false,
showCommandPaletteContext: undefined,
};
2022-05-16 21:09:36 +08:00
case "command-run":
return {
...state,
recentCommands: state.recentCommands.set(action.command, new Date()),
};
case "update-commands":
return {
...state,
commands: action.commands,
};
case "show-notification":
return {
...state,
2022-05-09 20:59:12 +08:00
notifications: [...state.notifications, action.notification],
};
case "dismiss-notification":
return {
...state,
notifications: state.notifications.filter((n) => n.id !== action.id),
};
case "show-panel":
2022-03-28 21:25:05 +08:00
return {
...state,
panels: {
...state.panels,
[action.id]: action.config,
},
2022-03-28 21:25:05 +08:00
};
case "hide-panel":
2022-03-28 21:25:05 +08:00
return {
...state,
panels: {
...state.panels,
[action.id]: {},
},
2022-04-27 01:04:36 +08:00
};
case "show-filterbox":
return {
...state,
showFilterBox: true,
filterBoxOnSelect: action.onSelect,
filterBoxPlaceHolder: action.placeHolder,
filterBoxOptions: action.options,
filterBoxLabel: action.label,
filterBoxHelpText: action.helpText,
};
case "hide-filterbox":
return {
...state,
showCommandPalette: false,
showPageNavigator: false,
showFilterBox: false,
filterBoxOnSelect: () => {},
filterBoxPlaceHolder: "",
filterBoxOptions: [],
filterBoxHelpText: "",
};
2022-12-21 23:08:51 +08:00
case "show-prompt":
return {
...state,
showPrompt: true,
promptDefaultValue: action.defaultValue,
promptMessage: action.message,
promptCallback: action.callback,
};
case "hide-prompt":
return {
...state,
showPrompt: false,
promptDefaultValue: undefined,
promptMessage: undefined,
promptCallback: undefined,
};
case "show-confirm":
return {
...state,
showConfirm: true,
confirmMessage: action.message,
confirmCallback: action.callback,
};
case "hide-confirm":
return {
...state,
showConfirm: false,
confirmMessage: undefined,
confirmCallback: undefined,
};
case "set-ui-option":
2022-09-16 20:26:47 +08:00
return {
...state,
uiOptions: {
...state.uiOptions,
[action.key]: action.value,
},
2022-09-16 20:26:47 +08:00
};
2023-06-15 02:58:08 +08:00
case "set-progress":
return {
...state,
progressPerc: action.progressPerc,
};
}
return state;
}