2023-04-26 02:37:29 +00:00
|
|
|
import type { JSX } from 'solid-js'
|
|
|
|
import { Accessor, createContext, createSignal, useContext } from 'solid-js'
|
2023-05-04 19:57:02 +00:00
|
|
|
import { createStore, SetStoreFunction } from 'solid-js/store'
|
|
|
|
import { Topic } from '../graphql/types.gen'
|
2023-04-26 02:37:29 +00:00
|
|
|
|
|
|
|
type WordCounter = {
|
|
|
|
characters: number
|
|
|
|
words: number
|
|
|
|
}
|
|
|
|
|
2023-05-04 19:57:02 +00:00
|
|
|
type ShoutForm = {
|
|
|
|
slug: string
|
|
|
|
title: string
|
|
|
|
subtitle: string
|
|
|
|
selectedTopics: Topic[]
|
|
|
|
mainTopic: string
|
|
|
|
body: string
|
|
|
|
coverImageUrl: string
|
|
|
|
}
|
|
|
|
|
2023-04-26 02:37:29 +00:00
|
|
|
type EditorContextType = {
|
|
|
|
isEditorPanelVisible: Accessor<boolean>
|
|
|
|
wordCounter: Accessor<WordCounter>
|
2023-05-04 19:57:02 +00:00
|
|
|
form: ShoutForm
|
2023-04-26 02:37:29 +00:00
|
|
|
actions: {
|
|
|
|
toggleEditorPanel: () => void
|
|
|
|
countWords: (value: WordCounter) => void
|
2023-05-04 19:57:02 +00:00
|
|
|
setForm: SetStoreFunction<ShoutForm>
|
2023-04-26 02:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const EditorContext = createContext<EditorContextType>()
|
|
|
|
|
|
|
|
export function useEditorContext() {
|
|
|
|
return useContext(EditorContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const EditorProvider = (props: { children: JSX.Element }) => {
|
2023-05-03 16:13:48 +00:00
|
|
|
const [isEditorPanelVisible, setIsEditorPanelVisible] = createSignal<boolean>(false)
|
2023-05-04 19:57:02 +00:00
|
|
|
|
|
|
|
const [form, setForm] = createStore<ShoutForm>(null)
|
|
|
|
|
2023-04-26 02:37:29 +00:00
|
|
|
const [wordCounter, setWordCounter] = createSignal<WordCounter>({
|
|
|
|
characters: 0,
|
|
|
|
words: 0
|
|
|
|
})
|
2023-05-03 16:13:48 +00:00
|
|
|
const toggleEditorPanel = () => setIsEditorPanelVisible((value) => !value)
|
2023-04-26 02:37:29 +00:00
|
|
|
const countWords = (value) => setWordCounter(value)
|
|
|
|
const actions = {
|
|
|
|
toggleEditorPanel,
|
2023-05-04 19:57:02 +00:00
|
|
|
countWords,
|
|
|
|
setForm
|
2023-04-26 02:37:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 19:57:02 +00:00
|
|
|
const value: EditorContextType = { actions, form, isEditorPanelVisible, wordCounter }
|
2023-04-26 02:37:29 +00:00
|
|
|
|
|
|
|
return <EditorContext.Provider value={value}>{props.children}</EditorContext.Provider>
|
|
|
|
}
|