-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathEditor.tsx
More file actions
120 lines (111 loc) · 3.66 KB
/
Editor.tsx
File metadata and controls
120 lines (111 loc) · 3.66 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
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
import React, { useCallback, useContext } from "react";
import { useHelpers, useCommands } from "@remirror/react";
import {
Summarize,
QuestionMark,
ReportProblem,
FactCheck
} from "@mui/icons-material";
import { IconButton } from "@mui/material";
import EditorStyle from "./Editor.style";
import useCardPresence from "../hooks/useCardPresence";
import { ReviewTaskMachineContext } from "../../../machines/reviewTask/ReviewTaskMachineProvider";
import {
ReportModelEnum,
ReviewTaskTypeEnum,
} from "../../../machines/reviewTask/enums";
import { getContentHtml } from "../Form/EditorCard";
import { EditorState } from "remirror";
/**
* @param editable enable or disable buttons
* @param state remirror state
* @returns An toolbar used for users to add a new input.
*/
const Editor = ({
editable,
state,
}: {
editable: boolean;
state: EditorState;
}) => {
const command = useCommands();
const { reportModel, reviewTaskType } = useContext(
ReviewTaskMachineContext
);
const { getJSON } = useHelpers();
const summaryDisabled = useCardPresence(getJSON, state, "summary", false);
const reportDisabled = useCardPresence(getJSON, state, "report", false);
const verificationDisabled = useCardPresence(
getJSON,
state,
"verification",
false
);
const handleInsertNode = useCallback(
(insertNode) => {
const { doc } = state;
command.insertHtml(insertNode, {
selection: doc.content.size,
replaceEmptyParentBlock: true,
});
},
[command, state]
);
const actions = [
{
onClick: () => handleInsertNode(getContentHtml("data-summary-id")),
disabled: editable || summaryDisabled,
"data-cy": "testClaimReviewsummarizeAdd",
icon: <Summarize className="toolbar-item-icon" />,
},
];
if (reportModel === ReportModelEnum.FactChecking) {
actions.push(
{
onClick: () =>
handleInsertNode(getContentHtml("data-verification-id")),
disabled: editable || verificationDisabled,
"data-cy": "testClaimReviewverificationAdd",
icon: <FactCheck className="toolbar-item-icon" />,
},
{
onClick: () =>
handleInsertNode(getContentHtml("data-report-id")),
disabled: editable || reportDisabled,
"data-cy": "testClaimReviewreportAdd",
icon: <ReportProblem className="toolbar-item-icon" />,
},
{
onClick: () =>
handleInsertNode(getContentHtml("data-question-id")),
disabled: editable,
"data-cy": "testClaimReviewquestionsAdd",
icon: <QuestionMark className="toolbar-item-icon" />,
}
);
}
if (reviewTaskType !== ReviewTaskTypeEnum.Claim) {
return <></>;
}
return (
<EditorStyle>
<div className="toolbar">
{actions ? (
actions.map(({ icon, ...props }) => (
<IconButton
key={props["data-cy"]}
className="toolbar-item"
style={{ outline: "none", border: "none" }}
{...props}
>
{icon}
</IconButton>
))
) : (
<></>
)}
</div>
</EditorStyle>
);
};
export default Editor;