Skip to content

Commit 67a6025

Browse files
committed
improve cells example
1 parent 4d6f9cc commit 67a6025

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

src/examples/src/cells/App/style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ table {
99
}
1010

1111
th {
12-
background-color: lightgray;
12+
background-color: #eee;
1313
}
1414

1515
tr:first-of-type th {
@@ -21,7 +21,7 @@ tr:first-of-type th:first-of-type {
2121
}
2222

2323
td {
24-
border: 1px solid lightgray;
24+
border: 1px solid #ccc;
2525
height: 1.5em;
2626
overflow: scroll;
2727
}

src/examples/src/cells/Cell/style.css

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
.cell {
1+
.cell, .cell input {
2+
height: 1.5em;
3+
line-height: 1.5;
4+
font-size: 15px;
5+
}
6+
7+
.cell span {
28
padding: 0 6px;
3-
height: 1em;
49
}
510

611
.cell input {
7-
width: 80px;
8-
height: 1em;
9-
font-size: 15px;
12+
width: 100%;
13+
box-sizing: border-box;
1014
}

src/examples/src/cells/Cell/template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="cell" @click="editing = true">
1+
<div class="cell" :title="cells[c][r]" @click="editing = true">
22
<input
33
v-if="editing"
44
:value="cells[c][r]"

src/examples/src/cells/store.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
import { reactive } from 'vue'
22

3+
const COLS = 5
4+
const ROWS = 20
5+
36
export const cells = reactive(
4-
Array.from(Array(26).keys()).map((i) =>
5-
Array.from(Array(100).keys()).map((i) => '')
7+
Array.from(Array(COLS).keys()).map((i) =>
8+
Array.from(Array(ROWS).keys()).map((i) => '')
69
)
710
)
811

9-
// https://codesandbox.io/s/jotai-7guis-task7-cells-mzoit?file=/src/atoms.ts
12+
// adapted from https://codesandbox.io/s/jotai-7guis-task7-cells-mzoit?file=/src/atoms.ts
13+
// by @dai-shi
1014
export function evalCell(exp) {
1115
if (!exp.startsWith('=')) {
1216
return exp
1317
}
14-
try {
15-
const fn = Function(
16-
'get',
17-
`
18-
return ${exp
19-
.slice(1)
20-
.replace(/\b([A-Z])(\d{1,2})\b/g, (_, c, r) => `get('${c}', ${r})`)};
21-
`
18+
19+
// = A1 + B2 ---> get(0,1) + get(1,2)
20+
exp = exp
21+
.slice(1)
22+
.replace(
23+
/\b([A-Z])(\d{1,2})\b/g,
24+
(_, c, r) => `get(${c.charCodeAt(0) - 65},${r})`
2225
)
23-
return fn((c, r) => {
24-
c = c.charCodeAt(0) - 65
25-
const val = evalCell(cells[c][r])
26-
const num = Number(val)
27-
return Number.isFinite(num) ? num : val
28-
})
26+
27+
try {
28+
return new Function('get', `return ${exp}`)(getCellValue)
2929
} catch (e) {
3030
return `#ERROR ${e}`
3131
}
3232
}
33+
34+
function getCellValue(c, r) {
35+
const val = evalCell(cells[c][r])
36+
const num = Number(val)
37+
return Number.isFinite(num) ? num : val
38+
}

0 commit comments

Comments
 (0)