Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion web/src/components/MemoEditor/components/EditorToolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Eye, EyeOff } from "lucide-react";
import type { FC } from "react";
import { Button } from "@/components/ui/button";
import { validationService } from "../services";
Expand All @@ -11,6 +12,7 @@ export const EditorToolbar: FC<EditorToolbarProps> = ({ onSave, onCancel, memoNa
const { valid } = validationService.canSave(state);

const isSaving = state.ui.isLoading.saving;
const is_preview_mode = state.ui.isPreviewMode;

const handleLocationChange = (location: typeof state.metadata.location) => {
dispatch(actions.setMetadata({ location }));
Expand All @@ -20,20 +22,27 @@ export const EditorToolbar: FC<EditorToolbarProps> = ({ onSave, onCancel, memoNa
dispatch(actions.toggleFocusMode());
};

const handleTogglePreviewMode = () => {
dispatch(actions.togglePreviewMode());
};

const handleVisibilityChange = (visibility: typeof state.metadata.visibility) => {
dispatch(actions.setMetadata({ visibility }));
};

return (
<div className="w-full flex flex-row justify-between items-center mb-2">
<div className="flex flex-row justify-start items-center">
<div className="flex flex-row justify-start items-center gap-1">
<InsertMenu
isUploading={state.ui.isLoading.uploading}
location={state.metadata.location}
onLocationChange={handleLocationChange}
onToggleFocusMode={handleToggleFocusMode}
memoName={memoName}
/>
<Button variant="ghost" size="sm" onClick={handleTogglePreviewMode} title={is_preview_mode ? "Edit" : "Preview"}>
{is_preview_mode ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
</Button>
</div>

<div className="flex flex-row justify-end items-center gap-2">
Expand Down
7 changes: 7 additions & 0 deletions web/src/components/MemoEditor/hooks/useKeyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ import type { EditorRefActions } from "../Editor";

interface UseKeyboardOptions {
onSave: () => void;
onTogglePreview?: () => void;
}

export const useKeyboard = (_editorRef: React.RefObject<EditorRefActions | null>, options: UseKeyboardOptions) => {
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
// Cmd/Ctrl + Enter to save
if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
event.preventDefault();
options.onSave();
}
// Cmd/Ctrl + Shift + P to toggle preview
if ((event.metaKey || event.ctrlKey) && event.shiftKey && event.key === "p") {
event.preventDefault();
options.onTogglePreview?.();
}
};

window.addEventListener("keydown", handleKeyDown);
Expand Down
29 changes: 27 additions & 2 deletions web/src/components/MemoEditor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useQueryClient } from "@tanstack/react-query";
import { useRef } from "react";
import { toast } from "react-hot-toast";
import MemoContent from "@/components/MemoContent";
import { MemoViewContext } from "@/components/MemoView/MemoViewContext";
import useCurrentUser from "@/hooks/useCurrentUser";
import { memoKeys } from "@/hooks/useMemoQueries";
import { userKeys } from "@/hooks/useUserQueries";
Expand Down Expand Up @@ -62,7 +64,11 @@ const MemoEditorImpl: React.FC<MemoEditorProps> = ({
dispatch(actions.toggleFocusMode());
};

useKeyboard(editorRef, { onSave: handleSave });
const handleTogglePreviewMode = () => {
dispatch(actions.togglePreviewMode());
};

useKeyboard(editorRef, { onSave: handleSave, onTogglePreview: handleTogglePreviewMode });

async function handleSave() {
// Validate before saving
Expand Down Expand Up @@ -136,7 +142,26 @@ const MemoEditorImpl: React.FC<MemoEditorProps> = ({
<FocusModeExitButton isActive={state.ui.isFocusMode} onToggle={handleToggleFocusMode} title={t("editor.exit-focus-mode")} />

{/* Editor content grows to fill available space in focus mode */}
<EditorContent ref={editorRef} placeholder={placeholder} autoFocus={autoFocus} />
{state.ui.isPreviewMode ? (
<div className="w-full flex-1 overflow-auto py-2">
<MemoViewContext.Provider
value={{
memo: { relations: [] } as never,
creator: undefined,
currentUser: undefined,
parentPage: "",
isArchived: false,
readonly: true,
showNSFWContent: false,
nsfw: false,
}}
>
<MemoContent content={state.content} />
</MemoViewContext.Provider>
</div>
) : (
<EditorContent ref={editorRef} placeholder={placeholder} autoFocus={autoFocus} />
)}

{/* Metadata and toolbar grouped together at bottom */}
<div className="w-full flex flex-col gap-2">
Expand Down
1 change: 1 addition & 0 deletions web/src/components/MemoEditor/services/memoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const memoService = {
},
ui: {
isFocusMode: false,
isPreviewMode: false,
isLoading: {
saving: false,
uploading: false,
Expand Down
4 changes: 4 additions & 0 deletions web/src/components/MemoEditor/state/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export const editorActions = {
type: "TOGGLE_FOCUS_MODE",
}),

togglePreviewMode: (): EditorAction => ({
type: "TOGGLE_PREVIEW_MODE",
}),

setLoading: (key: LoadingKey, value: boolean): EditorAction => ({
type: "SET_LOADING",
payload: { key, value },
Expand Down
9 changes: 9 additions & 0 deletions web/src/components/MemoEditor/state/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ export function editorReducer(state: EditorState, action: EditorAction): EditorS
},
};

case "TOGGLE_PREVIEW_MODE":
return {
...state,
ui: {
...state.ui,
isPreviewMode: !state.ui.isPreviewMode,
},
};

case "SET_LOADING":
return {
...state,
Expand Down
3 changes: 3 additions & 0 deletions web/src/components/MemoEditor/state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface EditorState {
};
ui: {
isFocusMode: boolean;
isPreviewMode: boolean;
isLoading: {
saving: boolean;
uploading: boolean;
Expand Down Expand Up @@ -42,6 +43,7 @@ export type EditorAction =
| { type: "REMOVE_LOCAL_FILE"; payload: string }
| { type: "CLEAR_LOCAL_FILES" }
| { type: "TOGGLE_FOCUS_MODE" }
| { type: "TOGGLE_PREVIEW_MODE" }
| { type: "SET_LOADING"; payload: { key: LoadingKey; value: boolean } }
| { type: "SET_DRAGGING"; payload: boolean }
| { type: "SET_COMPOSING"; payload: boolean }
Expand All @@ -57,6 +59,7 @@ export const initialState: EditorState = {
},
ui: {
isFocusMode: false,
isPreviewMode: false,
isLoading: {
saving: false,
uploading: false,
Expand Down