Files
tolaria/src/hooks/useRawMode.ts
Test fafa4e394b fix: sync raw editor (CodeMirror) content to BlockNote on mode switch
When toggling from raw mode back to BlockNote, the editor now correctly
re-parses content from tab.content instead of using stale cached blocks.

Key changes:
- useEditorTabSwap: detect rawMode true→false transition, invalidate
  block cache, and re-parse from tab.content. Added rawSwapPendingRef
  guard to prevent a second effect run from re-caching stale blocks
  before the deferred doSwap microtask runs.
- useRawMode: added onBeforeRawEnd callback to flush debounced raw
  editor content synchronously before toggling off.
- Editor.tsx: wired rawLatestContentRef and handleBeforeRawEnd to
  ensure the latest raw content reaches tab.content before the swap.
- RawEditorView: exposed latestContentRef so parent can read the
  latest keystroke content without waiting for the 500ms debounce.
- EditorContent: threaded rawLatestContentRef through to RawEditorView.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 22:14:20 +01:00

34 lines
1.2 KiB
TypeScript

import { useState, useCallback } from 'react'
interface UseRawModeParams {
activeTabPath: string | null
/** Flush pending WYSIWYG edits to disk before entering raw mode. */
onFlushPending?: () => Promise<boolean>
/** Called synchronously before raw mode is deactivated, so the caller can
* flush any debounced raw-editor content into tab state. */
onBeforeRawEnd?: () => void
}
/**
* Manages raw editor mode state.
* Raw mode is automatically inactive when the active tab changes,
* because rawMode is derived from whether the stored path matches the current tab.
*/
export function useRawMode({ activeTabPath, onFlushPending, onBeforeRawEnd }: UseRawModeParams) {
// Track which path has raw mode active — automatically deactivates on tab switch
const [rawActivePath, setRawActivePath] = useState<string | null>(null)
const rawMode = rawActivePath !== null && rawActivePath === activeTabPath
const handleToggleRaw = useCallback(async () => {
if (rawMode) {
onBeforeRawEnd?.()
setRawActivePath(null)
} else {
await onFlushPending?.()
setRawActivePath(activeTabPath)
}
}, [rawMode, activeTabPath, onFlushPending, onBeforeRawEnd])
return { rawMode, handleToggleRaw }
}