-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell-properties.js
187 lines (163 loc) · 7.24 KB
/
cell-properties.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
// Storage
let collectedSheetDB = []; //Contains all SheetDB
let sheetDB = [];
{
let addSheetBtn = document.querySelector(".sheet-add-icon");
addSheetBtn.click();
}
// Selectors for cell properties
let bold = document.querySelector(".bold");
let italic = document.querySelector(".italic");
let underline = document.querySelector(".underline");
let fontSize = document.querySelector(".font-size-prop");
let fontFamily = document.querySelector(".font-family-prop");
let fontColor = document.querySelector(".font-color-prop");
let BGcolor = document.querySelector(".BGcolor-prop");
let alignment = document.querySelectorAll(".alignment");
let leftAlign = alignment[0];
let centerAlign = alignment[1];
let rightAlign = alignment[2];
let activeColorProp = "#d1d8e0";
let inactiveColorProp = "#ecf0f1";
// Application of two-way binding
// Attach property listeners
bold.addEventListener("click", (e) => {
let address = addressBar.value;
let [cell, cellProp] = getCellAndCellProp(address);
// Modification
cellProp.bold = !cellProp.bold; // Data change
cell.style.fontWeight = cellProp.bold ? "bold" : "normal"; // UI change (1)
bold.style.backgroundColor = cellProp.bold ? activeColorProp : inactiveColorProp; // UI change (2)
})
italic.addEventListener("click", (e) => {
let address = addressBar.value;
let [cell, cellProp] = getCellAndCellProp(address);
// Modification
cellProp.italic = !cellProp.italic; // Data change
cell.style.fontStyle = cellProp.italic ? "italic" : "normal"; // UI change (1)
italic.style.backgroundColor = cellProp.italic ? activeColorProp : inactiveColorProp; // UI change (2)
})
underline.addEventListener("click", (e) => {
let address = addressBar.value;
let [cell, cellProp] = getCellAndCellProp(address);
// Modification
cellProp.underline = !cellProp.underline; // Data change
cell.style.textDecoration = cellProp.underline ? "underline" : "none"; // UI change (1)
underline.style.backgroundColor = cellProp.underline ? activeColorProp : inactiveColorProp; // UI change (2)
})
fontSize.addEventListener("change", (e) => {
let address = addressBar.value;
let [cell, cellProp] = getCellAndCellProp(address);
cellProp.fontSize = fontSize.value; // Data change
cell.style.fontSize = cellProp.fontSize + "px";
fontSize.value = cellProp.fontSize;
})
fontFamily.addEventListener("change", (e) => {
let address = addressBar.value;
let [cell, cellProp] = getCellAndCellProp(address);
cellProp.fontFamily = fontFamily.value; // Data change
cell.style.fontFamily = cellProp.fontFamily;
fontFamily.value = cellProp.fontFamily;
})
fontColor.addEventListener("change", (e) => {
let address = addressBar.value;
let [cell, cellProp] = getCellAndCellProp(address);
cellProp.fontColor = fontColor.value; // Data change
cell.style.color = cellProp.fontColor;
fontColor.value = cellProp.fontColor;
})
BGcolor.addEventListener("change", (e) => {
let address = addressBar.value;
let [cell, cellProp] = getCellAndCellProp(address);
cellProp.BGcolor = BGcolor.value; // Data change
cell.style.backgroundColor = cellProp.BGcolor;
BGcolor.value = cellProp.BGcolor;
})
alignment.forEach((alignElem) => {
alignElem.addEventListener("click", (e) => {
let address = addressBar.value;
let [cell, cellProp] = getCellAndCellProp(address);
let alignValue = e.target.classList[0];
cellProp.alignment = alignValue; // Data change
cell.style.textAlign = cellProp.alignment; // UI change (1)
switch(alignValue) { // UI change (2)
case "left":
leftAlign.style.backgroundColor = activeColorProp;
centerAlign.style.backgroundColor = inactiveColorProp;
rightAlign.style.backgroundColor = inactiveColorProp;
break;
case "center":
leftAlign.style.backgroundColor = inactiveColorProp;
centerAlign.style.backgroundColor = activeColorProp;
rightAlign.style.backgroundColor = inactiveColorProp;
break;
case "right":
leftAlign.style.backgroundColor = inactiveColorProp;
centerAlign.style.backgroundColor = inactiveColorProp;
rightAlign.style.backgroundColor = activeColorProp;
break;
}
})
})
let allCells = document.querySelectorAll(".cell");
for (let i = 0;i < allCells.length;i++) {
addListenerToAttachCellProperties(allCells[i]);
}
function addListenerToAttachCellProperties(cell) {
// Work
cell.addEventListener("click", (e) => {
let address = addressBar.value;
let [rid, cid] = decodeRIDCIDFromAddress(address);
let cellProp = sheetDB[rid][cid];
// Apply cell Properties
cell.style.fontWeight = cellProp.bold ? "bold" : "normal";
cell.style.fontStyle = cellProp.italic ? "italic" : "normal";
cell.style.textDecoration = cellProp.underline ? "underline" : "none";
cell.style.fontSize = cellProp.fontSize + "px";
cell.style.fontFamily = cellProp.fontFamily;
cell.style.color = cellProp.fontColor;
cell.style.backgroundColor = cellProp.BGcolor === "#000000" ? "transparent" : cellProp.BGcolor;
cell.style.textAlign = cellProp.alignment;
// Apply properties UI Props container
bold.style.backgroundColor = cellProp.bold ? activeColorProp : inactiveColorProp;
italic.style.backgroundColor = cellProp.italic ? activeColorProp : inactiveColorProp;
underline.style.backgroundColor = cellProp.underline ? activeColorProp : inactiveColorProp;
fontColor.value = cellProp.fontColor;
BGcolor.value = cellProp.BGcolor;
fontSize.value = cellProp.fontSize;
fontFamily.value = cellProp.fontFamily;
switch(cellProp.alignment) { // UI change (2)
case "left":
leftAlign.style.backgroundColor = activeColorProp;
centerAlign.style.backgroundColor = inactiveColorProp;
rightAlign.style.backgroundColor = inactiveColorProp;
break;
case "center":
leftAlign.style.backgroundColor = inactiveColorProp;
centerAlign.style.backgroundColor = activeColorProp;
rightAlign.style.backgroundColor = inactiveColorProp;
break;
case "right":
leftAlign.style.backgroundColor = inactiveColorProp;
centerAlign.style.backgroundColor = inactiveColorProp;
rightAlign.style.backgroundColor = activeColorProp;
break;
}
let formulaBar = document.querySelector(".formula-bar");
formulaBar.value = cellProp.formula;
cell.innerText = cellProp.value;
})
}
function getCellAndCellProp(address) {
let [rid, cid] = decodeRIDCIDFromAddress(address);
// Access cell & storage object
let cell = document.querySelector(`.cell[rid="${rid}"][cid="${cid}"]`);
let cellProp = sheetDB[rid][cid];
return [cell, cellProp];
}
function decodeRIDCIDFromAddress(address) {
// address -> "A1"
let rid = Number(address.slice(1) - 1); // "1" -> 0
let cid = Number(address.charCodeAt(0)) - 65; // "A" -> 65
return [rid, cid];
}