-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathMarkdownEditorContext.tsx
More file actions
46 lines (39 loc) · 1.27 KB
/
MarkdownEditorContext.tsx
File metadata and controls
46 lines (39 loc) · 1.27 KB
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
import { createContext, useContext, useState, ReactNode } from "react";
export interface MarkdownEditorCommands {
toggleBold: () => void;
toggleItalic: () => void;
insertLink: () => void;
toggleHeading1: () => void;
toggleHeading2: () => void;
toggleHeading3: () => void;
toggleUnorderedList: () => void;
toggleOrderedList: () => void;
insertImage: () => void;
}
interface MarkdownEditorContextType {
commands: MarkdownEditorCommands | undefined;
setCommands: (commands: MarkdownEditorCommands | undefined) => void;
}
const MarkdownEditorContext = createContext<MarkdownEditorContextType | undefined>(
undefined
);
export const MarkdownEditorProvider = ({ children }: { children: ReactNode }) => {
const [commands, setCommands] = useState<MarkdownEditorCommands | undefined>(
undefined
);
return (
<MarkdownEditorContext.Provider value={{ commands, setCommands }}>
{children}
</MarkdownEditorContext.Provider>
);
};
// eslint-disable-next-line react-refresh/only-export-components
export const useMarkdownEditorContext = () => {
const context = useContext(MarkdownEditorContext);
if (context === undefined) {
throw new Error(
"useMarkdownEditorContext must be used within a MarkdownEditorProvider"
);
}
return context;
};