-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.js
More file actions
236 lines (215 loc) · 5.88 KB
/
Copy pathfunctions.js
File metadata and controls
236 lines (215 loc) · 5.88 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
const fullRotations = [
"right;top;left;bottom;right",
"right;bottom;left;top;right",
"top;right;bottom;left;top",
"top;left;bottom;right;top",
"bottom;right;top;left;bottom",
"bottom;left;top;right;bottom",
"left;top;right;bottom;left",
"left;bottom;right;top;left",
];
const properties = {
keyboardActionType: "type",
movementSequence: "movement_sequence",
inputString: "lower_case",
inputCapsLockString: "upper_case",
inputKey: "key_code",
};
export function cleanYaml(yaml) {
if (!yaml.layers.default || !yaml.layers.default.sectors) return;
for (const sectorName in yaml.layers.default.sectors) {
const sector = yaml.layers.default.sectors[sectorName];
if (!sector.parts) {
continue;
}
for (const part in sector.parts) {
const actions = sector.parts[part];
while (actions[actions.length - 1] === null) {
actions.pop();
}
}
}
}
export function processAction(elements, yaml, excessive) {
const data = {};
Object.entries(properties).forEach(([prop, entry]) => {
const find = elements.find(({ name }) => name === prop);
if (!find) return;
let text = " ";
if (find.elements) {
text = find.elements[0].text;
}
if (!text) return;
switch (entry) {
case "type":
case "key_code":
text = text.toLowerCase();
break;
case "movement_sequence":
text = text
.toLowerCase()
.split(";")
.filter((s) => s !== "");
break;
default:
}
data[entry] = text;
});
const find = elements.find(({ name }) => name === "flags");
if (find) {
const flags = find.elements
.filter(({ name }) => name === "flag")
.reduce(
(flags, { elements: [{ text }] }) => flags | parseInt(text, 10),
0
);
if (flags) {
data.flags = flags;
}
}
switch (data.type) {
case "input_key":
if (!yaml.layers.hidden) {
yaml.layers.hidden = [];
}
yaml.layers.hidden.push(data);
break;
case "input_text":
if (!data.lower_case || (!excessive && isUpperCaseInput(data, yaml))) {
return;
}
delete data.type;
delete data.flags;
if (data.lower_case.toUpperCase() === data.upper_case) {
delete data.upper_case;
}
const movementSequence = Array.from(data.movement_sequence);
const isFullRotation = detectFullRotation(movementSequence);
const quadrant = detectQuadrant(movementSequence);
if (!excessive && isFullRotation && quadrant) {
return;
} else if (!isFullRotation && quadrant) {
if (!yaml.layers.default) {
yaml.layers.default = { sectors: {} };
}
const [sector, part, position] = quadrant;
if (!yaml.layers.default.sectors[sector]) {
yaml.layers.default.sectors[sector] = { parts: {} };
}
if (!yaml.layers.default.sectors[sector].parts[part]) {
yaml.layers.default.sectors[sector].parts[part] = [
null,
null,
null,
null,
];
}
const pospart = {};
if(data['lower_case']) {
pospart['lower_case'] = data['lower_case'];
}
if(data['upper_case']) {
pospart['upper_case'] = data['upper_case'];
}
yaml.layers.default.sectors[sector].parts[part][position] = pospart;
}
if(excessive || (!isFullRotation && !quadrant)) {
if (!yaml.layers.hidden) {
yaml.layers.hidden = [];
}
yaml.layers.hidden.push(data);
}
break;
}
}
function detectFullRotation(movementSequence) {
if (
movementSequence[0] !== "inside_circle" ||
movementSequence[movementSequence.length - 1] !== "inside_circle"
)
return false;
movementSequence.pop();
movementSequence.shift();
if (movementSequence.length < 6) return false;
const movementSequenceString = movementSequence.join(";");
if (
!fullRotations.find((sequence) =>
movementSequenceString.startsWith(sequence)
)
)
return false;
const start = movementSequence.shift();
while (movementSequence[0] !== start) {
movementSequence.shift();
}
return true;
}
function detectQuadrant(movementSequence) {
if (movementSequence.length < 2) return null;
const sector = movementSequence.shift();
const part = movementSequence.shift();
let last = part;
let position = 0;
while (movementSequence.length) {
if (position > 3) {
return null;
}
const expected = nextPosition(sector, part, last);
const next = movementSequence.shift();
if (expected !== next) {
return null;
}
last = next;
position++;
}
return [sector, part, position];
}
function nextPosition(sector, part, last) {
const oppositeSector = opposite(sector);
const oppositePart = opposite(part);
if (last === oppositePart) {
return sector;
} else if (last === oppositeSector) {
return oppositePart;
} else if (last === part) {
return oppositeSector;
} else {
return part;
}
}
function opposite(part) {
switch (part) {
case "right":
return "left";
case "left":
return "right";
case "top":
return "bottom";
case "bottom":
return "top";
}
}
function isUpperCaseInput(data, yaml) {
const find = ({ lower_case, upper_case }) => data.upper_case === lower_case;
if (yaml.layers.hidden) {
if (
yaml.layers.hidden.filter(({ type }) => type !== "input_key").find(find)
) {
return true;
}
}
if (yaml.layers.default && yaml.layers.default.sectors) {
for (const sectorName in yaml.layers.default.sectors) {
const sector = yaml.layers.default.sectors[sectorName];
if (!sector.parts) {
continue;
}
for (const part in sector.parts) {
if (sector.parts[part].filter((action) => !!action).find(find)) {
return true;
}
}
}
}
return false;
}