Files
memos/web/src/components/MemoEditor/edit/state/reducer.ts
T
boojack bbd0d35a25 feat(markup): annotate images and unify canvas editing
Preserve source images and retain acknowledged uploads for retries.
Share the canvas between viewing and editing to preserve the viewport.
2026-11-15 14:43:33 +08:00

129 lines
2.8 KiB
TypeScript

import type { EditorAction, EditorState } from "./types";
import { createInitialState } from "./types";
export function editorReducer(state: EditorState, action: EditorAction): EditorState {
switch (action.type) {
case "INIT_MEMO":
return {
...state,
content: action.payload.content,
metadata: action.payload.metadata,
timestamps: action.payload.timestamps,
};
case "SET_DRAWING_SAVE_ERROR":
return { ...state, drawingSaveError: action.payload };
case "SET_DRAWING_MEMO_UPDATE":
return { ...state, drawingNeedsMemoUpdate: action.payload };
case "UPDATE_CONTENT": {
if (state.content === action.payload.content) {
return state;
}
return {
...state,
content: action.payload.content,
};
}
case "SET_METADATA":
return {
...state,
metadata: {
...state.metadata,
...action.payload,
},
};
case "ADD_LOCAL_FILE":
return {
...state,
localFiles: [...state.localFiles, action.payload],
};
case "REMOVE_LOCAL_FILE":
return {
...state,
localFiles: state.localFiles.filter((f) => f.previewUrl !== action.payload),
};
case "SET_LOCAL_FILES":
return {
...state,
localFiles: action.payload,
};
case "SET_ATTACHMENT_ORDER":
return {
...state,
attachmentOrder: action.payload,
};
case "TOGGLE_FOCUS_MODE":
return {
...state,
ui: {
...state.ui,
isFocusMode: !state.ui.isFocusMode,
},
};
case "SET_LOADING":
return {
...state,
ui: {
...state.ui,
isLoading: {
...state.ui.isLoading,
[action.payload.key]: action.payload.value,
},
},
};
case "SET_PENDING_INLINE_IMAGE_INSERTIONS":
return {
...state,
ui: {
...state.ui,
pendingInlineImageInsertions: action.payload,
},
};
case "SET_TIMESTAMPS":
return {
...state,
timestamps: {
...state.timestamps,
...action.payload,
},
};
case "SET_RECORDER_BUSY":
return {
...state,
recorderBusy: action.payload,
};
case "SET_JUST_SAVED":
return {
...state,
ui: {
...state.ui,
justSaved: action.payload,
},
};
case "RESET":
return createInitialState();
case "ACCEPT_SUGGESTION":
return state.acceptedSuggestionIds.includes(action.payload)
? state
: { ...state, acceptedSuggestionIds: [...state.acceptedSuggestionIds, action.payload] };
default:
return state;
}
}