-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutCopyPaste.js
133 lines (115 loc) · 3.96 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
let ctrlKey;
document.addEventListener("keydown", (e) => {
ctrlKey = e.ctrlKey;
})
document.addEventListener("keyup", (e) => {
ctrlKey = e.ctrlKey;
})
for(let r = 0; r < rows; r++){
for(let c = 0; c < cols; c++){
let cell = document.querySelector(`.cell[rid="${r}"][cid="${c}"]`);
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 = [];
}
cell.style.border = "3px solid seagreen";
let rid = Number(cell.getAttribute("rid"));
let cid = Number(cell.getAttribute("cid"));
rangeStorage.push([rid, cid]);
})
}
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 = "1px solid #dfe4ea";
}
}
let copyData = [];
copyBtn.addEventListener("click", (e) => {
if(rangeStorage.length < 2) return;
copyData = [];
let strRow = rangeStorage[0][0];
let endRow = rangeStorage[1][0];
let strCol = rangeStorage[0][1];
let endCol = rangeStorage[1][1];
for(let r = strRow ; r <= endRow; r++){
let copyRow = [];
for(let c = strCol; c <= endCol; c++){
let cellProp = sheetDB[r][c];
copyRow.push(cellProp);
}
copyData.push(copyRow);
}
defaultSelectedCellsUI();
})
cutBtn.addEventListener("click", (e) => {
if(rangeStorage.length < 2) return;
copyData = [];
let strRow = rangeStorage[0][0];
let endRow = rangeStorage[1][0];
let strCol = rangeStorage[0][1];
let endCol = rangeStorage[1][1];
for(let r = strRow ; r <= endRow; r++){
let copyRow = [];
for(let c = strCol; c <= endCol; c++){
let cell = document.querySelector(`.cell[rid="${r}"][cid="${c}"]`);
let cellProp = sheetDB[r][c];
let originalCellProp = cellProp
copyRow.push(originalCellProp);
//db change
cellProp.value = "";
cellProp.bold = false;
cellProp.italic = false;
cellProp.underline = false;
cellProp.fontSize = 14;
cellProp.fontFamily = "monospace";
cellProp.fontColor = "#000000";
cellProp.BGcolor = "#ecf0f1";
cellProp.alignment = "left";
//ui change
cell.click();
}
copyData.push(copyRow);
}
console.log(copyData);
defaultSelectedCellsUI();
})
pasteBtn.addEventListener("click", (e) => {
//paste cells 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]);
let address = addressBar.value;
let[strow, stcol] = decodeAddress(address);
for(let r = strow, i = 0; r <= strow + rowDiff; r++, i++){
for(let c = stcol, j = 0; c <= stcol + colDiff; c++, j++){
let cell = document.querySelector(`.cell[rid="${r}"][cid="${c}"]`);
if(!cell) continue;
//db change
let data = copyData[i][j];
let cellProp = sheetDB[r][c];
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 change
cell.click();
}
}
})