wwf
2025-05-20 938c3e5a587ce950a94964ea509b9e7f8834dfae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {
  createContext,
  useContext,
  useRef,
} from 'react'
import { createVisualEditorStore } from './store'
import { useMitt } from '@/hooks/use-mitt'
import { noop } from 'lodash-es'
 
type VisualEditorStore = ReturnType<typeof createVisualEditorStore>
 
type VisualEditorContextType = VisualEditorStore | null
 
type VisualEditorProviderProps = {
  children: React.ReactNode
}
 
export const VisualEditorContext = createContext<VisualEditorContextType>(null)
 
export const VisualEditorContextProvider = ({ children }: VisualEditorProviderProps) => {
  const storeRef = useRef<VisualEditorStore>()
 
  if (!storeRef.current)
    storeRef.current = createVisualEditorStore()
 
  return (
    <VisualEditorContext.Provider value={storeRef.current}>
      {children}
    </VisualEditorContext.Provider>
  )
}
 
export const MittContext = createContext<ReturnType<typeof useMitt>>({
  emit: noop,
  useSubscribe: noop,
})
 
export const MittProvider = ({ children }: { children: React.ReactNode }) => {
  const mitt = useMitt()
 
  return (
    <MittContext.Provider value={mitt}>
      {children}
    </MittContext.Provider>
  )
}
 
export const useMittContext = () => {
  return useContext(MittContext)
}