-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathEditorAddSources.tsx
More file actions
141 lines (131 loc) · 4.64 KB
/
EditorAddSources.tsx
File metadata and controls
141 lines (131 loc) · 4.64 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, { useContext, useState } from "react";
import AletheiaButton, { ButtonType } from "../../../Button";
import { uniqueId } from "remirror";
import SourceDialog from "../LinkToolBar/Dialog/SourceDialog";
import { VisualEditorContext } from "../../VisualEditorProvider";
import { useTranslation } from "next-i18next";
import AddIcon from '@mui/icons-material/Add';
import { URL_PATTERN } from "../../../../utils/ValidateFloatingLink";
import { HTTP_PROTOCOL_REGEX } from "../LinkToolBar/FloatingLinkToolbar";
import { useCommands } from "@remirror/react";
import { Node } from "@remirror/pm/model";
import { useAppSelector } from "../../../../store/store";
const EditorAddSources = ({
nodeFromJSON,
doc,
}: {
nodeFromJSON: (json: any) => Node;
doc: Node;
}) => {
const enableAddEditorSourcesWithoutSelecting = useAppSelector(
(state) => state?.enableAddEditorSourcesWithoutSelecting
);
const command = useCommands();
const { t } = useTranslation();
const [href, setHref] = useState("https://");
const [showDialog, setShowDialog] = useState(false);
const [error, setError] = useState(null);
const { setEditorSources } = useContext(VisualEditorContext);
const [isLoading, setIsLoading] = useState(false);
const validateFloatingLink = () => {
if (!URL_PATTERN.test(href)) {
throw new Error(t("sourceForm:errorMessageValidURL"));
}
};
const getNodeObject = (id, href) => ({
type: "text",
marks: [
{
type: "link",
attrs: {
id: id,
href: href,
target: null,
auto: true,
},
},
],
text: " ",
});
const submitHref = () => {
try {
setIsLoading(true);
const id = uniqueId();
const newSource = {
href,
props: {
field: null,
targetText: null,
id: id,
textRange: [0, 0],
},
};
validateFloatingLink();
setEditorSources((sources) => [...sources, newSource]);
setShowDialog(false);
command.insertNode(nodeFromJSON(getNodeObject(id, href)), {
selection: doc.content.size,
replaceEmptyParentBlock: true,
});
setError(null);
} catch (error) {
setError(error.message);
} finally {
setHref("https://");
setIsLoading(false);
}
};
const handleInputChange = ({ target: { value } }) => {
// Prevent href with double http protocol
const href = value.replace(HTTP_PROTOCOL_REGEX, "$1");
setHref(href);
};
return (
<div className="add-sources-container">
{!showDialog ? (
<>
<p className="empty-text">
{t(
`sourceForm:${
enableAddEditorSourcesWithoutSelecting
? "editorEmptySourcesWithButton"
: "editorEmptySources"
}`
)}
</p>
{enableAddEditorSourcesWithoutSelecting && (
<AletheiaButton
type={ButtonType.gray}
onClick={() => setShowDialog(true)}
data-cy="testAddEditorSources"
>
<AddIcon style={{ fontSize: "24px" }} />
</AletheiaButton>
)}
</>
) : (
<SourceDialog
autoFocus
placeholder={t("sourceForm:placeholder")}
value={href}
onChange={handleInputChange}
onKeyDown={(event) => {
const { code } = event;
if (code === "Enter") {
event.preventDefault();
submitHref();
}
if (code === "Escape") {
setShowDialog(false);
}
}}
handleClickButton={submitHref}
onCloseModal={() => setShowDialog(false)}
error={error}
isLoading={isLoading}
/>
)}
</div>
);
};
export default EditorAddSources;