-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCell.svelte
More file actions
253 lines (239 loc) · 5.8 KB
/
Cell.svelte
File metadata and controls
253 lines (239 loc) · 5.8 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<style>
td {
/*
0.5px borders are rounded in many browsers, so instead we do 1px borders
on two sides.
*/
border-bottom: 1px solid var(--fg-color);
border-right: 1px solid var(--fg-color);
user-select: none;
-webkit-user-select: none;
background: transparent;
}
td,
td > * {
width: var(--width);
min-width: var(--width);
max-width: var(--width);
height: var(--height);
min-height: var(--height);
max-height: var(--height);
}
td .text,
td .element,
td textarea {
padding: 0.1em 0.2em;
}
td .text {
overflow: hidden;
white-space: pre;
text-overflow: ellipsis;
}
td .error {
/* Need to subtract 1 to prevent border overflow */
width: calc(var(--width) - 1px);
min-width: calc(var(--width) - 1px);
max-width: calc(var(--width) - 1px);
border: 1px solid var(--error-color);
}
td .element {
display: flex;
overflow: hidden;
}
textarea {
resize: none;
border: 0;
font-family: monospace, monospace;
line-height: 1;
white-space: pre;
/*
This is ncessary to prevent the switch to textarea from reflowing the
table cell (and by extension the row) a few pixels taller.
*/
display: block;
}
/* No scrollbar for text area */
textarea {
overflow: scroll;
scrollbar-width: none;
-ms-overflow-style: none;
}
textarea::-webkit-scrollbar {
width: 0;
height: 0;
}
/*
All these combinations are required to prevent the classes from overwriting
each other when multiple are set.
Including the four basic cases, and the case where none are selected at all,
there should be 2^4 == 16 total cases covered (the size of the power set of
the four possible classes).
*/
.left {
box-shadow: inset 1px 0 0 0 var(--fg-color);
}
.right {
box-shadow: inset -1px 0 0 0 var(--fg-color);
}
.top {
box-shadow: inset 0 1px 0 0 var(--fg-color);
}
.bottom {
box-shadow: inset 0 -1px 0 0 var(--fg-color);
}
.top.left {
box-shadow:
inset 0 1px 0 0 var(--fg-color),
inset 1px 0 0 0 var(--fg-color);
}
.top.right {
box-shadow:
inset 0 1px 0 0 var(--fg-color),
inset -1px 0 0 0 var(--fg-color);
}
.bottom.left {
box-shadow:
inset 0 -1px 0 0 var(--fg-color),
inset 1px 0 0 0 var(--fg-color);
}
.bottom.right {
box-shadow:
inset 0 -1px 0 0 var(--fg-color),
inset -1px 0 0 0 var(--fg-color);
}
.left.right {
box-shadow:
inset 1px 0 0 0 var(--fg-color),
inset -1px 0 0 0 var(--fg-color);
}
.bottom.top {
box-shadow:
inset 0 1px 0 0 var(--fg-color),
inset 0 -1px 0 0 var(--fg-color);
}
.top.left.bottom {
box-shadow:
inset 0 1px 0 0 var(--fg-color),
inset 1px 0 0 0 var(--fg-color),
inset 0 -1px 0 0 var(--fg-color);
}
.top.right.bottom {
box-shadow:
inset 0 1px 0 0 var(--fg-color),
inset -1px 0 0 0 var(--fg-color),
inset 0 -1px 0 0 var(--fg-color);
}
.left.bottom.right {
box-shadow:
inset 0 -1px 0 0 var(--fg-color),
inset 1px 0 0 0 var(--fg-color),
inset -1px 0 0 0 var(--fg-color);
}
.left.top.right {
box-shadow:
inset 0 1px 0 0 var(--fg-color),
inset 1px 0 0 0 var(--fg-color),
inset -1px 0 0 0 var(--fg-color);
}
.top.left.bottom.right {
box-shadow:
inset -1px -1px 0 0 var(--fg-color),
inset 1px 1px 0 0 var(--fg-color);
}
</style>
<script>
let { cell, row, col, width, height, globals = $bindable() } = $props();
let selected = $derived(globals.selected);
let value = $derived(cell.value);
let errorText = $derived(cell.errorText);
let errorStack = $derived(cell.errorStack);
let style = $derived(cell.style);
let element = $derived(cell.element);
let innerNode = $state(undefined);
$effect(() => {
if (element == null) {
return;
}
innerNode?.replaceChildren?.();
innerNode?.appendChild?.(element);
});
$effect(() => {
if (cell.editing) {
globals.mode = "insert";
}
});
function focus(e) {
e.focus();
e.select();
}
</script>
<td
bind:this={cell.td}
style:--width="{width}px"
style:--height="{height}px"
class:left={cell.leftBorder}
class:right={cell.rightBorder}
class:top={cell.topBorder}
class:bottom={cell.bottomBorder}
class:editing={cell.editing}
style={cell.editing ? undefined : style}
onfocus={() => {
/* TODO */
}}
onpointerover={(e) => {
if (e.buttons != 1) {
return;
}
if (selected.type == "cell") {
globals.setSelectionEnd({ x: col, y: row });
} else if (selected.type == "row") {
globals.setSelectionEnd(row);
} else if (selected.type == "col") {
globals.setSelectionEnd(col);
}
}}
onpointerdown={(e) => {
if (e.buttons != 1) {
return;
}
if (cell.editing) {
return;
}
if (e.shiftKey) {
globals.mode = "visual";
globals.setSelectionEnd({ x: col, y: row });
} else {
globals.mode = "normal";
globals.setSelectionStart("cell", { x: col, y: row });
}
}}
ondblclick={() => {
// Assumes that this cell is the only thing selected when it is
// double-clicked
cell.editing = selected.contains(row, col);
}}
>
{#if cell.editing}
<textarea
use:focus
bind:value={cell.formula}
onblur={() => {
cell.editing = false;
globals.mode = "normal";
}}
rows="1"
wrap="off"
autocorrect="off"
autocapitalize="none"
autocapitalization="none"
autocomplete="off"
spellcheck="false"
></textarea>
{:else if errorText != null}
<div class="text error" title={errorStack}>{errorText}</div>
{:else if element != null}
<div bind:this={innerNode} class="element"></div>
{:else}
<div class="text">{$value}</div>
{/if}
</td>