fix(MemoEditor): restore focus mode functionality
- Add useFocusMode hook to lock body scroll in focus mode - Add 'f' key keyboard shortcut to toggle focus mode - Add FocusModeExitButton inside editor - Import and wire up all focus mode components and hooks - Update useKeyboard to handle focus mode toggle
This commit is contained in:
parent
ca84241b8a
commit
201a0466e9
2 changed files with 20 additions and 3 deletions
|
|
@ -1,8 +1,10 @@
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
import { FOCUS_MODE_TOGGLE_KEY } from "../constants";
|
||||||
import type { EditorRefActions } from "../Editor";
|
import type { EditorRefActions } from "../Editor";
|
||||||
|
|
||||||
interface UseKeyboardOptions {
|
interface UseKeyboardOptions {
|
||||||
onSave: () => void;
|
onSave: () => void;
|
||||||
|
onToggleFocusMode?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useKeyboard = (editorRef: React.RefObject<EditorRefActions | null>, options: UseKeyboardOptions) => {
|
export const useKeyboard = (editorRef: React.RefObject<EditorRefActions | null>, options: UseKeyboardOptions) => {
|
||||||
|
|
@ -12,6 +14,17 @@ export const useKeyboard = (editorRef: React.RefObject<EditorRefActions | null>,
|
||||||
if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
|
if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
options.onSave();
|
options.onSave();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 'f' key to toggle focus mode (only if no modifiers and not in input)
|
||||||
|
if (event.key === FOCUS_MODE_TOGGLE_KEY && !event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey) {
|
||||||
|
const target = event.target as HTMLElement;
|
||||||
|
// Don't trigger if user is typing in an input/textarea
|
||||||
|
if (target.tagName !== "INPUT" && target.tagName !== "TEXTAREA" && !target.isContentEditable) {
|
||||||
|
event.preventDefault();
|
||||||
|
options.onToggleFocusMode?.();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@ import { toast } from "react-hot-toast";
|
||||||
import useCurrentUser from "@/hooks/useCurrentUser";
|
import useCurrentUser from "@/hooks/useCurrentUser";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { useTranslate } from "@/utils/i18n";
|
import { useTranslate } from "@/utils/i18n";
|
||||||
import { EditorContent, EditorMetadata, EditorToolbar, FocusModeOverlay } from "./components";
|
import { EditorContent, EditorMetadata, EditorToolbar, FocusModeExitButton, FocusModeOverlay } from "./components";
|
||||||
import { FOCUS_MODE_STYLES } from "./constants";
|
import { FOCUS_MODE_STYLES } from "./constants";
|
||||||
import type { EditorRefActions } from "./Editor";
|
import type { EditorRefActions } from "./Editor";
|
||||||
import { useAutoSave, useKeyboard, useMemoInit } from "./hooks";
|
import { useAutoSave, useFocusMode, useKeyboard, useMemoInit } from "./hooks";
|
||||||
import { cacheService, errorService, memoService, validationService } from "./services";
|
import { cacheService, errorService, memoService, validationService } from "./services";
|
||||||
import { EditorProvider, useEditorContext } from "./state";
|
import { EditorProvider, useEditorContext } from "./state";
|
||||||
import { MemoEditorContext } from "./types";
|
import { MemoEditorContext } from "./types";
|
||||||
|
|
@ -80,8 +80,11 @@ const MemoEditorImpl: React.FC<Props> = ({
|
||||||
// Auto-save content to localStorage
|
// Auto-save content to localStorage
|
||||||
useAutoSave(state.content, currentUser.name, cacheKey);
|
useAutoSave(state.content, currentUser.name, cacheKey);
|
||||||
|
|
||||||
|
// Focus mode management with body scroll lock
|
||||||
|
useFocusMode(state.ui.isFocusMode);
|
||||||
|
|
||||||
// Keyboard shortcuts
|
// Keyboard shortcuts
|
||||||
useKeyboard(editorRef, { onSave: handleSave });
|
useKeyboard(editorRef, { onSave: handleSave, onToggleFocusMode: actions.toggleFocusMode });
|
||||||
|
|
||||||
async function handleSave() {
|
async function handleSave() {
|
||||||
const { valid, reason } = validationService.canSave(state);
|
const { valid, reason } = validationService.canSave(state);
|
||||||
|
|
@ -131,6 +134,7 @@ const MemoEditorImpl: React.FC<Props> = ({
|
||||||
className,
|
className,
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
|
<FocusModeExitButton isActive={state.ui.isFocusMode} onToggle={actions.toggleFocusMode} title={t("editor.exit-focus-mode")} />
|
||||||
<EditorContent ref={editorRef} placeholder={placeholder} autoFocus={autoFocus} />
|
<EditorContent ref={editorRef} placeholder={placeholder} autoFocus={autoFocus} />
|
||||||
<EditorMetadata />
|
<EditorMetadata />
|
||||||
<EditorToolbar onSave={handleSave} onCancel={onCancel} />
|
<EditorToolbar onSave={handleSave} onCancel={onCancel} />
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue