-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.htm
More file actions
448 lines (428 loc) · 16.5 KB
/
index.htm
File metadata and controls
448 lines (428 loc) · 16.5 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<!DOCTYPE html>
<html lang="en">
<head>
<title>Langpad</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="manifest" href="manifest.json">
<link rel="stylesheet" href="tachyons.css" />
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<link rel="icon" type="image/png" href="icon.png">
<style>
.minw4 { min-width:8rem; }
.minh4 { min-height:8rem; }
.bw1-hover:hover, .bw1-hover:active { border-width:.125rem; }
.foreign { font-family: Candara, 'Noto Serif', serif; }
.local { font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; }
.vocab { max-height: 30rem; overflow-y: scroll; }
input { border: 1px solid transparent; border-radius: 4px; box-sizing: border-box; }
input:hover, input:active { border: 1px solid gray; }
button { border-style: solid; }
</style>
</head>
<body class="sans-serif">
<main v-scope @vue:mounted="mounted" class="mw-100 overflow-hidden">
<header>
<div class="bg-black-90 dt pa3 w-100 center white">
<h1 class="dtc white f5 b">
Langpad
</h1>
<p class="dn dtc-ns white f6 pl1 ">
Your personal language-learning glossary
</p>
<p class="dtc f6">
<button
class="ba bw1 b--white bg-black w-90 white tc b pointer grow pa1 mh2"
@click="dialog = 'EXPORT'"
>
Export
</button>
</p>
<p class="dtc f6">
<button
class="ba bw1 b--white bg-black w-90 white tc b pointer grow pa1 mh2"
@click="dialog = 'VOCAB'"
>
Vocab
</button>
</p>
<p class="dtc f6">
<button
class="ba bw1 b--white bg-gold w-90 black tc b pointer grow pa1 mh2"
@click="reset()"
>
{{ resetBtnTexts[resetClicks] }}
</button>
</p>
</div>
</header>
<div class="ma3">
<div v-for="el in orderedElements()" class="mv2">
<!-- Heading Element -->
<div v-if="el.elementType === elementFactory.h1">
<div class="flex justify-center">
<input
class="pa2 f3 mh1 mh2-ns w-40 minw4 local tc"
v-model="el.words[0].local"
placeholder="Heading"
@focus="select(el)"
@blur="blur()"
>
</div>
</div>
<!-- Gloss Element -->
<div v-if="el.elementType === elementFactory.gloss">
<div class="flex justify-center">
<input
class="pa2 f4 mh1 mh2-ns w-40 minw4 foreign tr"
v-model="el.words[0].foreign"
placeholder="A word to learn"
@focus="select(el)"
@blur="blur()"
>
<input
class="pa2 f4 mh1 mh2-ns w-40 minw4 local tl"
v-model="el.words[0].local"
placeholder="Meaning/gloss"
@focus="select(el)"
@blur="blur()"
>
</div>
</div>
<!-- A/B Element -->
<div v-if="el.elementType === elementFactory.ab">
<div class="flex flex-wrap justify-center items-center">
<div v-for="word in el.words" class="ba b--light-gray br1 mv2 mh1 mh2-ns w-25 minw4">
<input
class="db pa2 f4 foreign w-100 tc"
v-model="word.foreign"
placeholder="Vocab"
@focus="select(el, word)"
@blur="blur()"
>
<input
class="db pa2 f5 local w-100 tc"
v-model="word.local"
placeholder="Gloss"
@focus="select(el, word)"
@blur="blur()"
>
</div>
</div>
</div>
<!-- Usage Element -->
<div v-if="el.elementType === elementFactory.usage">
<div>
<input
class="pa2 f4 mh1 mh2-ns w-100 minw4 foreign tc"
v-model="el.words[0].phrase"
placeholder="An example sentence"
@focus="select(el)"
@blur="blur()"
>
</div>
<div>
<input
class="pa2 f5 mh1 mh2-ns w-100 minw4 local tc"
v-model="el.words[0].local"
placeholder="Meaning"
@focus="select(el)"
@blur="blur()"
>
</div>
</div>
<!-- Mnemonic Element -->
<div v-if="el.elementType === elementFactory.mnem">
<div>
<input
class="pa2 f4 mh1 mh2-ns w-100 minw4 foreign tc"
v-model="el.words[0].phrase"
placeholder="A helpful mnemonic"
@focus="select(el)"
@blur="blur()"
>
</div>
</div>
<!-- Buttons -->
<div class="flex flex-wrap justify-center items-center" v-if="isSel(el)">
<button
type="button"
class="pointer pa0 tc w2 h2 ma1 mr3 b--black bw1 br2 bg-white black"
v-if="el.order !== maxOrdinal()"
@click="toggleTools(el)">📚</button>
<button type="button" v-if="el.elementType === elementFactory.ab" class="pointer pa0 tc w2 h2 ma1 b--green bw1 br2 bg-white black" @click="addWord(el)">+</button>
<button type="button" v-if="el.elementType === elementFactory.ab" class="pointer pa0 tc w2 h2 ma1 mr3 b--blue bw1 br2 bg-white black" @click="removeWord(el, selectedWord)">−</button>
<button type="button" class="pointer pa0 tc w2 h2 ma1 b--black bw1 br2 bg-white black" @click="moveUp(el)">↑</button>
<button type="button" class="pointer pa0 tc w2 h2 ma1 b--red bw1 br2 bg-white red" @click="remove(el)">×</button>
<button type="button" class="pointer pa0 tc w2 h2 ma1 b--black bw1 br2 bg-white black" @click="moveDown(el)">↓</button>
</div>
<!-- New Element Tools -->
<div
class="flex flex-wrap justify-center mt1"
:class="{'mt1' : addToolLocation === 'ELEMENT', 'mt4' : addToolLocation === 'END'}"
v-if="showTools(el)">
<button type="button" class="pointer pa1 ma1 b--black bw1 br2 bg-white black minw4" @click="add(elementFactory.h1)">Heading 📰</button>
<button type="button" class="pointer pa1 ma1 b--black bw1 br2 bg-white black minw4" @click="add(elementFactory.gloss)">Gloss 📔</button>
<button type="button" class="pointer pa1 ma1 b--black bw1 br2 bg-white black minw4" @click="add(elementFactory.ab)">A/B 🆎</button>
<button type="button" class="pointer pa1 ma1 b--black bw1 br2 bg-white black minw4" @click="add(elementFactory.mnem)">Mnemonic 🧠</button>
<button type="button" class="pointer pa1 ma1 b--black bw1 br2 bg-white black minw4" @click="add(elementFactory.usage)">Usage 🦜</button>
</div>
</div>
<div class="flex justify-center mt4" v-if="addToolLocation !== 'END'">
<button
type="button"
class="pointer pa2 ma2 b--black bw1 br2 bg-white black minw4"
@click="toggleTools()">Add 📚</button>
</div>
</div>
<div class="ba ma3" v-if="dialog === 'VOCAB'">
<button type="button" @click="dialog = null">Close</button>
<label>Sort Local <input v-model="sortLocal" type="checkbox"></label>
<ul>
<li v-for="v in allVocab()">
<span v-if="sortLocal">{{v.local}} – {{v.foreign}}</span>
<span v-else>{{v.foreign}} – {{v.local}}</span>
<ul v-if="v.usages">
<li v-for="use in v.usages">{{ use.phrase }}</li>
</ul>
</li>
</ul>
</div>
<!-- Overlay background -->
<div
v-if="dialog"
class="overlay z-2 bg-dark-gray o-70 fixed absolute--fill"
></div>
<!-- Overlay contents -->
<div v-if="dialog" class="z-3 fixed top-1 left-1 right-1 ma1 pa2 black ba b--black bw2 bg-white">
<div class="flex justify-between mb3">
<div class="f3">{{dialog}}</div>
<div
class="z-3 f3 mr2 pointer"
@click="dialog = null"
>
×
</div>
</div>
<div v-if="dialog === 'VOCAB'">
<label><input v-model="sortLocal" type="checkbox"> Sort Local</label>
<ul class="vocab">
<li v-for="v in allVocab()">
<span v-if="sortLocal">{{v.local}} – {{v.foreign}}</span>
<span v-else>{{v.foreign}} – {{v.local}}</span>
<ul v-if="v.usages">
<li v-for="use in v.usages">{{ use.phrase }}</li>
</ul>
</li>
</ul>
</div>
<div v-if="dialog === 'EXPORT'" class="center">
<button type="button" class="pointer db pa2 ma1 center b--black bw1 br2 bg-white black minw4" @click="copyText()">
{{ copied === 'TEXT' ? "✔" : "📋" }} Copy all text
</button>
<button type="button" class="pointer db pa2 ma1 center b--black bw1 br2 bg-white black minw4" @click="copyVocab()">
{{ copied === 'VOCAB' ? "✔" : "📑" }} Copy all vocab
</button>
<button type="button" class="pointer db pa2 ma1 center b--black bw1 br2 bg-white black minw4" @click="copyJson()">
{{ copied === 'JSON' ? "✔" : "💾" }} Copy JSON
</button>
<button type="button" v-if="!importing" class="pointer db pa2 ma1 center b--black bw1 br2 bg-white black minw4" @click="importing = true">
📁 Import JSON
</button>
<button type="button" v-else class="pointer db pa2 ma1 center b--black bw1 br2 bg-white black minw4" @click="load()">
✅ Load it!
</button>
<textarea class="w-100 mt3 minh4 code f6" v-if="copied === false">{{ pasteFailContent }}</textarea>
<textarea class="w-100 mt3 minh4 code f6" v-if="importing" v-model="importContent"></textarea>
</div>
</div>
</main>
<script src="petite-vue.js"></script>
<script src="cuid.js"></script>
<script src="elements.js"></script>
<script>
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("sw.js");
}
const CONTENT = "savedElements";
const elementFactory = new ElementFactory();
PetiteVue.createApp({
message: "Welcome",
dialog: null,
copied: null,
resetClicks: 0,
resetBtnTexts: ["New", "Sure?", "Pew!"],
elements: elementFactory.initElements(),
selected: null,
selectedWord: null,
addPoint: 0,
addToolLocation: null,
sortLocal: false,
pasteFailContent: '',
importing: false,
importContent: '',
mounted() {
console.log("mounted");
// Load from LS
const json = window.localStorage.getItem(CONTENT);
if (json) {
const saved = JSON.parse(json);
if (saved && Array.isArray(saved)) {
this.elements = saved;
}
}
this.addPoint = this.nextOrdinal();
},
// Computed
maxOrdinal() {
const max = Math.max(...this.elements.map(e => e.order)) || 0;
return max;
},
nextOrdinal() {
const max = Math.max(...this.elements.map(e => e.order)) || 0;
console.log("nextOrdinal", max + 1);
return max + 1;
},
orderedElements() {
return this.elements.sort((a, b) => a.order - b.order);
},
allVocab() {
const all = [...this.elements].flatMap(e => e.words)
const allWords = all.filter(e => e.foreign > "");
const allUsages = all.filter(e => e.phrase > "");
if (this.sortLocal) allWords.sort((a, b) => a.local.localeCompare(b.local));
else allWords.sort((a, b) => a.foreign.localeCompare(b.foreign))
allWords.forEach((w) => {
w.usages = allUsages.filter(u => u.phrase.search(new RegExp(w.foreign, "i")) !== -1);
})
return allWords;
},
isSel(element) {
return element.id === this.selected;
},
showTools(element) {
const isOpenOnElement = (this.addToolLocation === 'ELEMENT' && this.isSel(element) && this.addPoint === element.order);
const isOpenAtEnd = (this.addToolLocation === 'END' && element.order === this.maxOrdinal());
return isOpenOnElement || isOpenAtEnd;
},
// Methods
select(element, word) {
const prevSelected = this.selected;
this.selected = element.id;
if (word) this.selectedWord = word.id;
if (this.selected !== prevSelected) this.hideElementTools();
},
hideElementTools() {
if (this.addToolLocation === 'ELEMENT')
this.addToolLocation = null;
console.log("hideElementTools", this.addToolLocation, this.addPoint);
},
toggleTools(element) {
const toolLocationTarget = element ? 'ELEMENT' : 'END';
this.addPoint = element ? element.order : this.nextOrdinal();
this.addToolLocation = this.addToolLocation === toolLocationTarget
? null
: toolLocationTarget;
console.log("toggleTools", this.addToolLocation, this.addPoint)
},
add(elementType) {
const order = this.addToolLocation === 'END'
? this.maxOrdinal()
: this.addPoint;
// Shunt down existing elements
const newOrdinal = order + 1;
this.elements.filter(e => e.order >= newOrdinal).forEach(e => e.order += 1);
// Add new element
const newEl = elementFactory.createElement(elementType || elementFactory.gloss, newOrdinal);
this.elements.push(newEl);
console.log("add", order, elementType);
this.save();
},
remove(element) {
const removeIndex = this.elements.findIndex(e => e.id === element.id);
console.log("remove removeIndex", removeIndex, this.elements)
this.elements.splice(removeIndex, 1);
// Renumber elements to remove ordinal gaps
this.elements.sort((a, b) => a.order - b.order).forEach((el, index) => {
el.order = index;
});
this.save();
},
addWord(element) {
const newWord = elementFactory.createWord();
element.words.push(newWord);
this.select(element, newWord);
this.save();
},
removeWord(element, wordId) {
const removeIndex = element.words.findIndex(w => w.id === wordId);
console.log("removeWord removeIndex", removeIndex, element.words)
element.words.splice(removeIndex, 1);
this.save();
},
moveUp(element) {
const newOrder = element.order - 1;
const swapElement = this.elements.find(e => e.order === newOrder);
if (swapElement) swapElement.order += 1;
element.order = newOrder;
},
moveDown(element) {
const newOrder = element.order + 1;
const swapElement = this.elements.find(e => e.order === newOrder);
if (swapElement) swapElement.order -= 1;
element.order = newOrder;
},
reset() {
var vm = this;
this.resetClicks++;
if (this.resetClicks === 2) {
this.elements = elementFactory.initElements();
this.save();
setTimeout(function() {
vm.resetClicks = 0;
}, 1000);
}
if (this.resetClicks > 2) this.resetClicks = 0;
},
blur() {
this.save();
},
save() {
console.log("Save");
this.resetClicks = 0;
// Save to LS
const json = JSON.stringify(this.elements);
window.localStorage.setItem(CONTENT, json);
},
copy(content, label){
try {
navigator.clipboard.writeText(content);
this.copied = label;
window.setTimeout(() => (this.copied = null), 1000);
} catch (e) {
this.pasteFailContent = content;
this.copied = false;
}
},
copyText() {
const allLinesContent = this.elements.map(e => elementFactory.elementString(e)).join('\r\n');
this.copy(allLinesContent, 'TEXT');
},
copyVocab() {
const allVocabContent = this.allVocab().map(w => elementFactory.vocabString(w)).join('\r\n');
this.copy(allVocabContent, 'VOCAB');
},
copyJson() {
const json = JSON.stringify(this.elements);
this.copy(json, 'JSON');
},
load() {
this.elements = JSON.parse(this.importContent);
this.importing = false;
this.importContent = '';
}
}).mount()
</script>
</body>
</html>