-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditModal.tsx
167 lines (144 loc) · 4.51 KB
/
EditModal.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// ReactJS stuff
import { ChangeEvent, FormEvent, useEffect } from 'react';
import { useContextMenu } from 'react-contexify';
// Our Imports
import { extensions } from '../utils/extensions';
import { BoardFile } from '../utils/types/board';
type FileSelectProps = {
fileName?: string;
setFileName?: Function;
currentFile?: BoardFile;
files?: BoardFile[];
setFiles?: Function;
};
const EditModal: React.FC<FileSelectProps> = ({
fileName,
setFileName,
currentFile,
files,
setFiles,
}) => {
const { show } = useContextMenu({
id: 'input',
});
function displayMenu(e) {
show({
event: e,
});
}
useEffect(() => {
const form = document.getElementsByClassName('editForm')[0];
document.getElementById('file-name').onkeydown = function (e) {
if (e.key == 'Enter') {
(form as HTMLFormElement).requestSubmit();
}
};
}, []);
function closeEdit() {
const div = document.querySelectorAll(`div.edit`);
const back = document.querySelector<HTMLElement>(`.backdrop`);
div.forEach((cls) => {
(cls as HTMLElement).style['display'] = 'none';
});
back.style['display'] = 'none';
}
function edit(event: FormEvent) {
event.preventDefault();
closeEdit();
try {
const input = document.querySelector<HTMLInputElement>(
`.edit-${fileName.replaceAll('.', '-')}.edit form input`
);
if (!input) return;
const name = input.value;
const file = files.find((a) => a.name == fileName);
const box = document.getElementsByClassName(`${fileName}-language`)[0];
if (!name) return;
if (files.find((a) => a.name === name))
return alert('Name already taken !');
else {
file.name = name;
file.language = (
(box as HTMLElement).innerText || box.textContent
).toLowerCase();
setFileName(name);
}
} catch {}
}
function updateEditLanguage(e: ChangeEvent<HTMLInputElement>, old) {
const value = e.target.value;
const box = document.getElementsByClassName(`${old}-language`)[0];
if (!box) return;
const language =
extensions.find((x) =>
x.key.includes('.' + value.replace('.', '^').split('^')[1])
)?.name ||
extensions.find((x) =>
x.key.includes('.' + value.split('.')[value.split('.').length - 1])
)?.name ||
'none';
box.innerHTML = language.charAt(0).toUpperCase() + language.slice(1);
}
function deleteFile(name: string) {
let text = `Are you sure to delete ${currentFile.name} ?\nThis is irreversible (Can't Undo)`;
if (confirm(text) == true) {
const removed = files.filter(function (item) {
return item.name !== name;
});
setFileName(removed[0].name);
setFiles(removed);
(
document.getElementsByClassName('editForm')[0] as HTMLFormElement
).requestSubmit();
} else
(
document.getElementsByClassName('editForm')[0] as HTMLFormElement
).requestSubmit();
}
return (
<div className={`edit-${currentFile.name.replaceAll('.', '-')} edit`}>
{' '}
<form className="editForm" onSubmit={(event) => edit(event)}>
<input
onContextMenu={displayMenu}
onChange={(event) => updateEditLanguage(event, currentFile.name)}
className={`file-name-${currentFile.name.replaceAll('.', '-')}`}
name="filename"
type="text"
id="file-name"
placeholder={currentFile.name}
autoComplete="off"></input>
<div style={{ padding: 0 }} className="tooltip">
<p>
<span
className={[
'language-show-edit',
currentFile.name + '-language',
].join(' ')}>
{currentFile.language.charAt(0).toUpperCase() +
currentFile.language.slice(1)}
</span>
</p>
<span
style={{ maxWidth: 'auto', left: '28%' }}
className="tooltiptext">
To change language, just have file extension with file name. Just
like you do with normal code editor
</span>
</div>
<button
title="Delete the file"
disabled={files.length == 1}
className="deletePrompt"
onClick={(e) => {
e.preventDefault();
setTimeout(() => deleteFile(currentFile.name), 400);
}}>
Delete
</button>
</form>
</div>
);
};
export default EditModal;
// Didnt use Million.block as it completely breaks apart