-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdownDisplayLine.tsx
145 lines (135 loc) · 4.16 KB
/
MarkdownDisplayLine.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React, { useState } from "react";
import styled from "styled-components";
import { Core } from "../tree/types";
import deleteFileFromPath from "../utils/deleteFileFromPath/deleteFileFromPath";
import setCommentForPath from "../utils/setCommentForPath/setCommentForPath";
import extractString from "../utils/extractString";
import CommentSection from "./CommentSection";
const LightBGColor = styled.div`
display: flex;
justify-content: space-between;
padding: 0.25rem;
background: #2b303b;
background: rgba(85, 73, 89, 0.2);
font-family: "Source Code Pro", monospace;
`;
const DarkBGColor = styled.div`
display: flex;
justify-content: space-between;
padding: 0.25rem;
background: #212428;
font-family: "Source Code Pro", monospace;
`;
const DeletionButton = styled.button`
background: none;
border: none;
font-family: "Source Code Pro", monospace;
color: #b3c3d3;
`;
const EditButton = styled.button`
background: none;
border: none;
font-family: "Source Code Pro", monospace;
color: #b3c3d3;
`;
interface Props {
isOddNumberedLine: boolean;
content: string;
onChange: Function;
treeCore: Core[];
}
const EDIT_ICON = "✏";
const RIGHT_BEFORE_COMMENT_STARTS = "<span># ";
const RIGHT_AFTER_COMMENT_ENDS = "</span>";
const RIGHT_BEFORE_FILENAME_STARTS = '">';
const RIGHT_AFTER_FILENAME_ENDS = "</a>";
const RIGHT_BEFORE_PATH_STARTS = '<a href="./';
const RIGHT_AFTER_PATH_ENDS = '">';
const MarkdownDisplayLine: React.FC<Props> = ({
isOddNumberedLine,
content,
treeCore,
onChange = (): void => {},
}) => {
/** Given a The string rips out the path inside The string
* @param {string} content - The string
* @returns {string} - Returns the path from hyperlink
*/
const getPath = (content: string): string => {
return extractString(
content,
RIGHT_BEFORE_PATH_STARTS,
RIGHT_AFTER_PATH_ENDS
);
};
/** Given a string, returns the comment inside the string
* @param {string} content - The string
* @returns {string} - Returns the path inside the hyperlink
*/
const getComment = (content: string): string => {
return extractString(
content,
RIGHT_BEFORE_COMMENT_STARTS,
RIGHT_AFTER_COMMENT_ENDS
);
};
const [comment, setComment] = useState(getComment(content));
const [commentVisibilty, setCommentVisibility] = useState(false);
const changeComment = (event: React.ChangeEvent<HTMLInputElement>): void => {
setComment(event.currentTarget.value);
};
const setNewComment = (): void => {
setCommentForPath(treeCore, getPath(content), comment);
setCommentVisibility(!commentVisibilty);
onChange(treeCore);
};
const handleDeletion = () => {
deleteFileFromPath(treeCore, getPath(content));
onChange(treeCore);
};
if (isOddNumberedLine) {
return (
<DarkBGColor>
<div style={{ width: "100%" }}>
{content}
<div style={{ width: "100%", position: "relative", left: "-12px" }}>
<CommentSection
isVisible={commentVisibilty}
value={comment}
onChange={(e) => changeComment(e)}
onClick={() => setNewComment()}
></CommentSection>
</div>
</div>
<div style={{ display: "flex" }}>
<DeletionButton onClick={() => handleDeletion()}>X</DeletionButton>
<EditButton onClick={() => setCommentVisibility(!commentVisibilty)}>
{EDIT_ICON}
</EditButton>
</div>
</DarkBGColor>
);
}
return (
<LightBGColor>
<div style={{ width: "100%" }}>
{content}
<div style={{ width: "100%", position: "relative", left: "-12px" }}>
<CommentSection
isVisible={commentVisibilty}
value={comment}
onChange={(e) => changeComment(e)}
onClick={() => setNewComment()}
></CommentSection>
</div>
</div>
<div style={{ display: "flex" }}>
<DeletionButton onClick={() => handleDeletion()}>X</DeletionButton>
<EditButton onClick={() => setCommentVisibility(!commentVisibilty)}>
{EDIT_ICON}
</EditButton>
</div>
</LightBGColor>
);
};
export default MarkdownDisplayLine;