-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelements.js
More file actions
64 lines (55 loc) · 1.61 KB
/
elements.js
File metadata and controls
64 lines (55 loc) · 1.61 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
var ElementFactory = function () {
this.ab = "ab";
this.gloss = "gloss";
this.mnem = "mnem";
this.usage = "usage";
this.h1 = "h1";
this.createElement = function (elementType, order, local, foreign) {
const el = {
"id": cuid(),
"elementType": elementType,
"order": order,
"alignment": 0,
"words": [this.createWord(local, foreign)]
}
console.log("createElement", el);
return el;
}
this.createWord = function (local, foreign) {
return {
"id" : cuid(),
"local": local || "",
"foreign": foreign || "",
"phrase" : "",
}
}
this.initElements = function () {
return [
this.createElement(this.h1, 0, "Greetings"),
this.createElement(this.gloss, 1, "Hello", "Hola")
]
}
this.elementString = function(element) {
return element.words
.flatMap(w => this.wordString(w))
.join('\r\n');
}
this.wordString = function(word) {
const def = word.local
? ` - ${word.local}`
: '';
if (word.phrase)
return `${word.phrase}${def}`;
if (word.foreign)
return `${word.foreign}${def}`;
return word.local;
}
this.vocabString = function(word) {
const baseString = this.wordString(word);
if (word.usages) {
const usageStrings = word.usages.map(u => u.phrase).join('\r\n');
return [baseString, usageStrings].join('\r\n');
}
return baseString;
}
}