-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
210 lines (185 loc) · 6.73 KB
/
index.html
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
<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<meta name="google" value="notranslate">
<title>Kanji Sudoku</title>
<link rel="shortcut icon" type="image/png" href="./img/icon.png">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.css" />
<link rel="stylesheet" type="text/css" href="./fnt/font.css" />
<style type="text/css">
.stroke { font-family: "Kanji Stroke Order"; }
.select-container { margin: 1em auto; display: table; width: 75vmin; display: flex; }
.select-title { display: inline-block; margin-right: auto; }
.select-button {
display: inline-block;
border: 1px solid black;
margin-left: -1px;
margin-right: 0px;
border-radius: 0;
padding: 0em 1em;
cursor: pointer;
}
.select-button:hover { background-color: #eee; }
.selected { background-color: #fcc; }
.select-button:nth-child(2) { border-radius: 0.3em 0 0 0.3em; }
.select-button:last-child { border-radius: 0 0.3em 0.3em 0; }
.description { width: 75vmin; height-max: 50vmin; display: table; margin: 1em auto; line-height: 0; }
.card {
display: inline-block;
width: 25vmin; box-sizing: border-box;
border: 1px solid black; margin: -1px -1px 0 0;
line-height: initial;
overflow: hidden;
}
img.help { box-sixing: border-box; width: 100%; border: 1px solid silver; cursor: pointer; }
img.hidden { display: none; }
.cardcont { display: flex; width: 100%; }
.stroke { display: inline-block; font-size: 8vmin; }
.reading {
display: inline-block;
margin-top: 0.5em;
width: auto;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.kcont, table.sudoku {
display: table;
width: 75vmin; height: 75vmin; box-sizing: border-box;
border-collapse: collapse;
-webkit-border-horizontal-spacing: 0;
-webkit-border-vertical-spacing: 0;
margin-left: auto; margin-right: auto; margin-bottom: 1em; }
td { height: 1.6em; width: 1.6em; border: 1px solid; text-align: center; }
td, th:first-child, td:first-child, th:last-child, td:last-child { padding: 0.1em 0.1em 0.1em 0.1em; margin: 0 0 0 0; }
td:first-child { border-left:3px solid; }
td:nth-child(3n) { border-right:2px solid; }
td:last-child { border-right:3px solid; }
tr:first-child { border-top:3px solid; }
tr:nth-child(3n) td { border-bottom:solid ; }
tr:last-child { border-bottom: 3px solid; }
.kcont, table.sudoku { font-size: 4.5vmin; }
.select-container { font-size: 2vmin; }
.description { font-size: 2vmin; }
@media print {
.noprint { display: none; }
}
@media screen {
.printonly { display: none; }
}
</style>
</head>
<body>
<div id="container">
</div>
<script type="module">
import {html, Component, render, useState} from "https://unpkg.com/htm/preact/standalone.mjs";
import kanji from "./dat/kanji.js";
import readings from "./dat/reading.js";
import sudokus from "./dat/easy.js";
const sample = (text, n) => {
const bucket = [...Array(text.length)].map((_, i) => i)
function rand() {
const idx = Math.floor(Math.random() * bucket.length);
return bucket.splice(idx, 1)[0];
}
return [...Array(n)].map(() => text[rand()]);
}
const difficulties = ["easy", "medium", "hard"];
const sudoku_board = (diff="easy") => sudokus[Math.floor(Math.random() * sudokus.length)];
const HowTo = () => {
const [n, set] = useState(0);
const next = () => set((n+1)%3);
return html`
<details className="select-container">
<summary>How to use?</summary>
${[...new Array(3)].map((_, i) => html`
<img class=${ n===i?"help":"hidden" } src=${ `./img/howto${ i+1 }.png` } onClick=${ next }/>
`)}
</details>
`;
};
class Sudoku extends Component {
constructor(props) {
super(props);
this.state = {kanji: [], board: [], grade: "", diff: "easy"}
this.generate = this.generate.bind(this);
this.render_row = this.render_row.bind(this);
this.grade_button = this.grade_button.bind(this);
this.diff_button = this.diff_button.bind(this);
}
componentDidMount() { this.generate(Object.keys(kanji)[0], "easy"); }
componentWillUnmount() { this.setState({kanji: [], board: []}) }
generate(grade, diff) {
// real logic goes here
console.log("generate", grade);
const k = sample(kanji[grade], 9);
const read = Object.fromEntries(k.map(kk => [kk, readings[kk] || {kun: "", on: ""}]));
this.setState({kanji: k, readings: read, board: sudoku_board(), grade: grade, diff: diff})
}
render_row(r) {
return html`<tr>${ r.split("").map(c => this.state.kanji[c-1]).map(Cell) }</tr>`;
}
grade_button(name) {
const cb = (ev) => { ev.preventDefault(); this.generate(name, this.state.diff); };
return html`
<div className=${ "select-button" + (this.state.grade == name?" selected":"") }
onClick=${ cb } key=${ name }>${ name }</div>
`;
}
diff_button(name) {
const cb = (ev) => { ev.preventDefault(); this.generate(this.state.grade, name); };
return html`
<div className=${"select-button" + (this.state.diff == name?" selected":"")}
onClick=${cb} key=${name}>${name}</div>
`;
}
render() {
return html`
<div className="sudoku">
<div className="noprint">
<div className="select-container">
<div className="select-title">Kanji grade:</div>
${Object.keys(kanji).map(this.grade_button)}
</div>
<div className="select-container">
<div className="select-title">Sudoku difficulty:</div>
${difficulties.map(this.diff_button)}
</div>
<${ HowTo }/>
</div>
<div className="printonly select-container">
<div className="select-title">dimaqq.github.io/kanji-sudoku</div>
kanji grade: ${ this.state.grade }, sudoku: ${ this.state.diff }
</div>
<div className="description">
${ this.state.kanji.map(glyph => Card(glyph, this.state.readings[glyph])) }
</div>
<div className="tabcontainer">
${ Board(this.state.board, this.state.kanji) }
</div>
</div>
`;
}
}
const Board = (board, kanji) => html`
<table class="sudoku"><tbody>
${ board.map(line => Row(line, kanji)) }
</tbody></table>`;
// FIXME replace Row/Cell with flexbox or grid
const Row = (line, kanji) => html`<tr>${ line.split("").map(c => Cell(kanji[c-1])) }</tr>`;
const Cell = (glyph) => html`<td>${ glyph }</td>`;
const Card = (glyph, reading) => html`
<div className="card">
<div className="cardcont">
<div className="stroke">${ glyph }</div>
<div className="reading">
${ reading["kun"] }<br/>
${ reading["on"] }
</div>
</div>
</div>`;
render(html`<${ Sudoku }/>`, document.querySelector("#container"));
</script>
</body>
</html>