-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditDocumentPage.tsx
127 lines (116 loc) · 3.89 KB
/
EditDocumentPage.tsx
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { useEffect, useRef, useState } from "react";
import "./index.css";
import { RootState } from "../../store/store";
import { useDispatch, useSelector } from "react-redux";
import Tiptap from "./components/EditorContent";
import { Editor } from "@tiptap/react";
import { BlockType, IContentBlock } from "../../store/interfaces/block";
import ImageBlockComponent from "./components/ImageBlock";
import DropDownMenu from "./components/DropDownMenu";
import TableDataBase from "./components/DataBaseTable";
import { TrelloDataBase } from "./components/TrelloDataBase";
import TitleDocumentPage from "./components/TitleDocumentPage";
import { useParams } from "react-router-dom";
import { getPageByid, updatePageContent } from "../../store/API/Page";
import { setStoreState } from "../../store/slices/blockSlice";
import { handleKeyEventRefs } from "./composables/handleEventKeyDown";
import SubPageBlockComponent from "./components/SubPage";
const EditDocumentPage = () => {
const refs = useRef<(Editor | null)[]>([]);
const params = useParams();
const [updateDataBaseBlock, setUpdateDataBaseBlock] = useState(false);
const blocksPage: IContentBlock[] = useSelector(
(state: RootState) => state.blocks.content
);
const dispatch = useDispatch();
// Avant de rendre la page on vérifie que l'id est bien présent dans l'url
useEffect(() => {
(async () => {
if (!params.id) return;
const pageContent = await getPageByid(params.id);
const store = {
isPublic: pageContent.isPublic,
isEditable: pageContent.isEditable,
content: pageContent.content,
title: pageContent.title,
};
dispatch(setStoreState(store));
})();
}, [params.id, dispatch]);
useEffect(() => {
(async () => {
if (updateDataBaseBlock) {
if (!params.id) return;
await updatePageContent(params.id, blocksPage);
setUpdateDataBaseBlock(false);
}
})();
}, [updateDataBaseBlock, blocksPage, params.id]);
const getNewFocus = (newIndex: number) => {
refs.current[newIndex]?.chain().focus().run();
};
return (
<>
<TitleDocumentPage />
{blocksPage.map((item, index) => (
<div
key={item.id}
className="block"
style={{ display: "flex", alignItems: "center" }}
>
<DropDownMenu
setUpdateData={setUpdateDataBaseBlock}
editor={refs.current[index]}
item={item}
/>
{item.type === BlockType.TIPTAP ? (
<Tiptap
isEditable={true}
blockState={item}
ref={(ref) => {
refs.current[index] = ref;
}}
handleKeyEventRefs={(event) => {
handleKeyEventRefs(
event,
index,
item,
refs,
dispatch,
getNewFocus
);
}}
/>
) : null}
{item.type === BlockType.IMAGE && typeof item.content === "string" ? (
<ImageBlockComponent
isEditable={true}
blockState={item}
imageUrl={item.content}
/>
) : null}
{item.type === BlockType.DATABASE ? (
<TableDataBase
isSubPage={false}
isEditable={true}
dataSource={item}
index={index}
/>
) : null}
{item.type === BlockType.TRELLO && typeof item.content != "string" ? (
<TrelloDataBase
isSubPage={false}
isEditable={true}
dataSource={item}
index={index}
/>
) : null}
{item.type === BlockType.SUBPAGE ? (
<SubPageBlockComponent dataSource={item} index={index} />
) : null}
</div>
))}
</>
);
};
export default EditDocumentPage;