-
Notifications
You must be signed in to change notification settings - Fork 1
/
cutCopyPaste.js
238 lines (206 loc) · 8.85 KB
/
cutCopyPaste.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
let ctrlKey;
document.addEventListener("keydown", (e) => {
ctrlKey = e.ctrlKey;
})
document.addEventListener("keyup", (e) => {
ctrlKey = e.ctrlKey;
})
let gridCont = document.querySelector(".grid-cont");
gridCont.addEventListener("keydown", e => {
if (e.key === "Escape") {
defaultSelectedCellsUI();
rangeStorage = [];
}
});
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
let cell = document.querySelector(`.cell[rid="${i}"][cid="${j}"]`);
handleSelectedCells(cell);
}
}
let copyBtn = document.querySelector(".copy");
let cutBtn = document.querySelector(".cut");
let pasteBtn = document.querySelector(".paste");
let rangeStorage = [];
function handleSelectedCells(cell) {
cell.addEventListener("click", (e) => {
// select cells range
if (!ctrlKey) {
return;
}
if (rangeStorage.length >= 2) {
defaultSelectedCellsUI()
rangeStorage = [];
};
// UI
// cell.style.border = "thin solid #218c74";
// box-shadow: 0 0 10px 5px rgba(188, 120, 174, 0.65);
// cell.style.outline = "none";
cell.style.boxShadow = "0 0 7px 5px rgb(64 59 63 / 31%)"
cell.style.animation = "pulse 900ms linear infinite";
cell.style.zIndex = "5";
let rid = Number(cell.getAttribute("rid"));
let cid = Number(cell.getAttribute("cid"));
rangeStorage.push([rid, cid]);
// console.log(rangeStorage);
})
}
function defaultSelectedCellsUI() {
for (let i = 0; i < rangeStorage.length; i++) {
let cell = document.querySelector(`.cell[rid="${rangeStorage[i][0]}"][cid="${rangeStorage[i][1]}"]`);
// cell.style.border = "thin solid #07070712";
cell.style.boxShadow = "none";
cell.style.animation = "none";
cell.style.zIndex = "auto";
}
}
let copyData = [];
copyBtn.addEventListener("click", (e) => {
copyData = [];
let startRow = rangeStorage[0][0];
let startCol = rangeStorage[0][1];
if (rangeStorage.length <= 1) {
copyData.push([sheetDB[startRow][startCol]]);
copyBtn.setAttribute("src", "https://img.icons8.com/fluency-systems-filled/96/000000/copy.png");
// console.log(copyData);
return;
}
let endRow = rangeStorage[1][0];
let endCol = rangeStorage[1][1];
if (startRow == endRow && startCol == endCol) {
copyData.push([sheetDB[startRow][startCol]]);
copyBtn.setAttribute("src", "https://img.icons8.com/fluency-systems-filled/96/000000/copy.png");
// console.log(copyData);
return;
}
for (let i = startRow; i <= endRow; i++) {
let copyRow = [];
for (let j = startCol; j <= endCol; j++) {
let { ...cellProp } = sheetDB[i][j];
copyRow.push(cellProp);
}
copyData.push(copyRow);
}
// console.log(copyData);
copyBtn.setAttribute("src", "https://img.icons8.com/fluency-systems-filled/96/000000/copy.png");
defaultSelectedCellsUI();
})
cutBtn.addEventListener("click", (e) => {
let startRow = rangeStorage[0][0];
let startCol = rangeStorage[0][1];
if (rangeStorage.length <= 1) {
copyData.push([sheetDB[startRow][startCol]]);
cutBtn.setAttribute("src", "https://img.icons8.com/external-febrian-hidayat-basic-outline-febrian-hidayat/96/000000/external-cut-user-interface-febrian-hidayat-basic-outline-febrian-hidayat.png");
// console.log(copyData);
return;
}
let endRow = rangeStorage[1][0];
let endCol = rangeStorage[1][1];
if (startRow == endRow && startCol == endCol) {
copyData.push([sheetDB[startRow][startCol]]);
cutBtn.setAttribute("src", "https://img.icons8.com/external-febrian-hidayat-basic-outline-febrian-hidayat/96/000000/external-cut-user-interface-febrian-hidayat-basic-outline-febrian-hidayat.png");
// console.log(copyData);
return;
}
// copyBtn.click();
// copyBtn.setAttribute("src", "https://img.icons8.com/fluency-systems-regular/96/000000/copy.png");
for (let i = startRow; i <= endRow; i++) {
let copyRow = [];
for (let j = startCol; j <= endCol; j++) {
let cell = document.querySelector(`.cell[rid="${i}"][cid="${j}"]`);
let { ...cellProp } = sheetDB[i][j];
copyRow.push(cellProp);
cellProp = sheetDB[i][j];
// DB
cellProp.bold = false;
cellProp.italic = false;
cellProp.underline = false;
cellProp.alignment = "left";
cellProp.fontFamily = "monospace";
cellProp.fontSize = "15";
cellProp.fontColor = "#000000";
cellProp.BGcolor = "#ffffff";
cellProp.value = "";
// UI
cell.click();
}
copyData.push(copyRow);
}
cutBtn.setAttribute("src", "https://img.icons8.com/external-febrian-hidayat-basic-outline-febrian-hidayat/96/000000/external-cut-user-interface-febrian-hidayat-basic-outline-febrian-hidayat.png");
defaultSelectedCellsUI();
})
pasteBtn.addEventListener("click", (e) => {
// paste cells data
// console.log(rangeStorage);
if (copyData.length) {
// target
let address = addressBar.value;
let [stRow, stCol] = decodeRIDCIDFromAddress(address);
if (rangeStorage.length <= 1) {
let cell = document.querySelector(`.cell[rid="${stRow}"][cid="${stCol}"]`);
// DB
let data = copyData[0][0];
let cellProp = sheetDB[stRow][stCol];
cellProp.value = data.value;
cellProp.bold = data.bold;
cellProp.italic = data.italic;
cellProp.underline = data.underline;
cellProp.fontSize = data.fontSize;
cellProp.fontFamily = data.fontFamily;
cellProp.fontColor = data.fontColor;
cellProp.BGcolor = data.BGcolor;
cellProp.alignment = data.alignment;
// UI
cell.click();
copyBtn.setAttribute("src", "https://img.icons8.com/fluency-systems-regular/96/000000/copy.png");
cutBtn.setAttribute("src", "https://img.icons8.com/external-compact-zufarizal-robiyanto/50/000000/external-cut-compact-ui-essential-vol2-compact-zufarizal-robiyanto.png");
defaultSelectedCellsUI();
return;
}
let rowDiff = Math.abs(rangeStorage[0][0] - rangeStorage[1][0]);
let colDiff = Math.abs(rangeStorage[0][1] - rangeStorage[1][1]);
// if (rowDiff == colDiff == 0) {
// let cell = document.querySelector(`.cell[rid="${stRow}"][cid="${stCol}"]`);
// // DB
// let data = copyData[0][0];
// let cellProp = sheetDB[stRow][stCol];
// cellProp.value = data.value;
// cellProp.bold = data.bold;
// cellProp.italic = data.italic;
// cellProp.underline = data.underline;
// cellProp.fontSize = data.fontSize;
// cellProp.fontFamily = data.fontFamily;
// cellProp.fontColor = data.fontColor;
// cellProp.BGcolor = data.BGcolor;
// cellProp.alignment = data.alignment;
// // UI
// cell.click();
// return;
// }
// r -> copyData row ; c -> copyData col
// since in copyData we stored elems based on 0-indexing
for (let i = stRow, r = 0; i <= stRow + rowDiff; i++, r++) {
for (let j = stCol, c = 0; j <= stCol + colDiff; j++, c++) {
let cell = document.querySelector(`.cell[rid="${i}"][cid="${j}"]`);
if (!cell) continue; // if cell doesnt exist don't throw err i.e if we paste on Z99 a data containing more than 1 cell
// DB
let data = copyData[r][c];
let cellProp = sheetDB[i][j];
cellProp.value = data.value;
cellProp.bold = data.bold;
cellProp.italic = data.italic;
cellProp.underline = data.underline;
cellProp.fontSize = data.fontSize;
cellProp.fontFamily = data.fontFamily;
cellProp.fontColor = data.fontColor;
cellProp.BGcolor = data.BGcolor;
cellProp.alignment = data.alignment;
// UI
cell.click();
}
}
copyBtn.setAttribute("src", "https://img.icons8.com/fluency-systems-regular/96/000000/copy.png");
cutBtn.setAttribute("src", "https://img.icons8.com/external-compact-zufarizal-robiyanto/50/000000/external-cut-compact-ui-essential-vol2-compact-zufarizal-robiyanto.png");
}
defaultSelectedCellsUI();
})