-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutCopyPaste.js
146 lines (123 loc) · 4.28 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
let ctrlKey;
document.addEventListener('keydown', e => {
ctrlKey = e.ctrlKey; // the property will have a boolean value
});
document.addEventListener('keyup', e => {
ctrlKey = e.ctrlKey; // means 'ctrl' is not pressed
});
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 = []; // storage for cells to keep data of (cut, copy, paste). Has rid, cid of top-left to btm-right cell
function handleSelectedCells(cell) {
cell.addEventListener('click', e => {
// Select cell's range
if (!ctrlKey) return;
if (rangeStorage.length >= 2) {
defaultSelectedCellsUI();
rangeStorage = [];
}
// UI
cell.style.border = '3px solid #006266';
// Attributes of clicked cell
let rid = Number(cell.getAttribute('rid'));
let cid = Number(cell.getAttribute('cid'));
rangeStorage.push([rid, cid]);
console.log(rangeStorage);
});
}
function defaultSelectedCellsUI() {
// rangeStorage keeps data of two cells that were selected using CTRL+Click
for (let i = 0; i < rangeStorage.length; i++) {
let cell = document.querySelector(
`.cell[rid="${rangeStorage[i][0]}"][cid="${rangeStorage[i][1]}"]`,
);
cell.style.border = '1px solid #dfe4ea';
}
}
let copyData = []; // has data between left and right selected cell
copyBtn.addEventListener('click', e => {
if (rangeStorage.length < 2) return;
copyData = []; // Initializing empty becoz on every new cell click, new data should get copied to clipboard rmeoving previous data
let [startRow, startCol, endRow, endCol] = [
rangeStorage[0][0],
rangeStorage[0][1],
rangeStorage[1][0],
rangeStorage[1][1],
];
for (let i = startRow; i <= endRow; i++) {
let copyRow = [];
for (let j = startCol; j <= endCol; j++) {
let cellProp = sheetDB[i][j];
let cellDataProp = copyRow.push(cellProp);
console.log(cellDataProp);
}
copyData.push(copyRow);
}
defaultSelectedCellsUI(); // After copying, the selected cells are removed from the UI
});
cutBtn.addEventListener('click', e => {
if (rangeStorage.length < 2) return;
let [startRow, startCol, endRow, endCol] = [
rangeStorage[0][0],
rangeStorage[0][1],
rangeStorage[1][0],
rangeStorage[1][1],
];
for (let i = startRow; i <= endRow; i++) {
for (let j = startCol; j <= endCol; j++) {
let cell = document.querySelector(`.cell[rid="${i}"][cid="${j}"]`);
// DB
let cellProp = sheetDB[i][j];
cellProp.value = "";
cellProp.bold = false;
cellProp.italic = false;
cellProp.underline = false;
cellProp.fontSize = 14;
cellProp.fontFamily = "monospace";
cellProp.fontColor = "#000000";
cellProp.BGcolor = "#000000";
cellProp.alignment = "left";
// UI
cell.click();
}
}
defaultSelectedCellsUI();
});
pasteBtn.addEventListener('click', e => {
// Paste cell's data
if (rangeStorage.length < 2) return;
let rowDiff = Math.abs(rangeStorage[0][0] - rangeStorage[1][0]);
let colDiff = Math.abs(rangeStorage[0][1] - rangeStorage[1][1]);
// Target
let address = addressBar.value;
let [stRow, stCol] = decodeRidCidFromAddress(address);
// r - refers copyData's row
// c - refers copyData's col
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;
// DB : to copy paste data & properties (and not children & formula)
let data = copyData[r][c]; // has the cell's object
let cellProp = sheetDB[i][j]; // has cell object
cellProp.value = data.value; // assign value to targeted cell
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();
}
}
});