1
0
silverbullet/web/reducer.ts

149 lines
3.6 KiB
TypeScript
Raw Normal View History

import { Action, AppViewState } from "./types.ts";
2022-05-13 12:36:26 +00:00
let m = new Map();
m.size;
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,
};
case "page-loaded":
return {
...state,
isLoading: false,
allPages: new Set(
2022-03-31 15:25:34 +00:00
[...state.allPages].map((pageMeta) =>
2022-05-17 09:53:17 +00:00
pageMeta.name === action.meta.name
2022-03-31 15:25:34 +00:00
? { ...pageMeta, lastOpened: Date.now() }
: pageMeta
),
),
2022-05-17 09:53:17 +00:00
perm: action.meta.perm,
currentPage: action.meta.name,
};
case "page-changed":
return {
...state,
unsavedChanges: true,
};
case "page-saved":
return {
...state,
unsavedChanges: false,
};
case "start-navigate":
return {
...state,
showPageNavigator: true,
};
case "stop-navigate":
return {
...state,
showPageNavigator: false,
};
case "pages-listed":
// Let's move over any "lastOpened" times to the "allPages" list
let oldPageMeta = new Map([...state.allPages].map((pm) => [pm.name, pm]));
for (let pageMeta of action.pages) {
let oldPageMetaItem = oldPageMeta.get(pageMeta.name);
if (oldPageMetaItem && oldPageMetaItem.lastOpened) {
pageMeta.lastOpened = oldPageMetaItem.lastOpened;
}
}
return {
...state,
allPages: action.pages,
};
case "show-palette":
let commands = new Map(state.commands);
for (let [k, v] of state.commands.entries()) {
if (
v.command.contexts &&
(!action.context || !v.command.contexts.includes(action.context))
) {
commands.delete(k);
}
}
return {
...state,
commands,
showCommandPalette: true,
};
case "hide-palette":
return {
...state,
showCommandPalette: false,
};
2022-05-16 13:09:36 +00: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 12:59:12 +00: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 13:25:05 +00:00
return {
...state,
panels: {
...state.panels,
[action.id]: action.config,
},
2022-03-28 13:25:05 +00:00
};
case "hide-panel":
2022-03-28 13:25:05 +00:00
return {
...state,
panels: {
...state.panels,
[action.id]: {},
},
2022-04-26 17:04:36 +00: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,
showFilterBox: false,
filterBoxOnSelect: () => {},
filterBoxPlaceHolder: "",
filterBoxOptions: [],
filterBoxHelpText: "",
};
2022-09-16 12:26:47 +00:00
case "set-editor-ro":
return {
...state,
forcedROMode: action.enabled,
};
}
return state;
}