EditorPage.tsx
1.54 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
46
47
48
49
50
51
import React, { useRef, useEffect, useState } from "react";
import ToastEditor from "tui-image-editor";
import EditorHeader from "./EditorHeader";
import ImageResizer from "./ImageResizer";
import { editorOptions } from "utils/editorOptions";
import { SubmenuContext } from "context/Submenu";
import "tui-image-editor/dist/tui-image-editor.css";
import "./Editor.scss";
import "./EditorPage.scss";
// @ts-ignore
const initialEditor:ToastEditor = new ToastEditor(document.querySelector("#hidden-editor"), editorOptions);
function getEditorClass(size: number, subMenuVisible: boolean) {
return `my-editor tui-image-editor-container left ${!subMenuVisible ? "submenu-hidden" : ""} size-${size}`;
}
function EditorPage() {
const imageEditorRef = useRef<HTMLDivElement>(null);
const [editor, setEditor] = useState(initialEditor);
const [submenuVisible, setSubmenuVisible] = useState(true);
const [size, setSize] = useState(1);
useEffect(() => {
// @ts-ignore
setEditor(new ToastEditor(imageEditorRef.current, editorOptions));
return () => {
initialEditor.destroy();
};
}, []);
function onChangeSubmenuVisible() {
setSubmenuVisible(!submenuVisible);
}
return (
<>
<SubmenuContext.Provider value={{ visible: submenuVisible, onChange: onChangeSubmenuVisible }}>
<EditorHeader editor={editor} />
<div className={getEditorClass(size, submenuVisible)} ref={imageEditorRef} />
<ImageResizer size={size} setSize={setSize} />
</SubmenuContext.Provider>
</>
);
}
export default EditorPage;