forked from Manoonchai/kiimo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateChr_background.ts
192 lines (166 loc) · 4.84 KB
/
generateChr_background.ts
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
import {
plainToClass
} from "class-transformer"
import {
validate
} from "class-validator"
import fs from "fs"
import {
Layout,
WindowsAttributes
} from "./main"
export async function generateChr_background(
content: Record < string, unknown > ,
outputPath: string,
): Promise < void > {
const layout = plainToClass(Layout, content)
const errors = await validate(layout)
if (errors.length) {
throw new Error(errors.map((e) => e.toString()).join(", "))
}
const windowsErrors = await validate(
plainToClass(WindowsAttributes, layout.os.windows),
)
if (windowsErrors.length) {
throw new Error(windowsErrors.map((e) => e.toString()).join(", "))
}
function toCheck(str: string) {
// let hex, i
// let result = ""
// for (i = 0; i < str.length; i++) {
// hex = str.charCodeAt(i).toString(16)
// result += "\\u" + ("0000" + hex).slice(-4)
// }
return str.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
}
const klfDefaultLayout = {
"1": "Digit1",
"2": "Digit2",
"3": "Digit3",
"4": "Digit4",
"5": "Digit5",
"6": "Digit6",
"7": "Digit7",
"8": "Digit8",
"9": "Digit9",
"0": "Digit0",
"-": "Minus",
"=": "Equal",
"`": "Backquote",
q: "KeyQ",
w: "KeyW",
e: "KeyE",
r: "KeyR",
t: "KeyT",
y: "KeyY",
u: "KeyU",
i: "KeyI",
o: "KeyO",
p: "KeyP",
"[": "BracketLeft",
"]": "BracketRight",
a: "KeyA",
s: "KeyS",
d: "KeyD",
f: "KeyF",
g: "KeyG",
h: "KeyH",
j: "KeyJ",
k: "KeyK",
l: "KeyL",
";": "Semicolon",
"'": "Quote",
"\\": "Backslash",
z: "KeyZ",
x: "KeyX",
c: "KeyC",
v: "KeyV",
b: "KeyB",
n: "KeyN",
m: "KeyM",
",": "Comma",
".": "Period",
"/": "Slash",
" ": "Space",
KPDL: "NumpadDecimal",
}
const lines = `/*# License: ${layout.license}
Generated via github.com/Manoonchai/kiimo
*/
var AltGr = { PLAIN: "plain", ALTERNATE: "alternate" };
var Shift = { PLAIN: "plain", SHIFTED: "shifted" };
var contextID = -1;
var altGrState = AltGr.PLAIN;
var shiftState = Shift.PLAIN;
var lastRemappedKeyEvent = undefined;
var lut = {`
const endLines = `
};
chrome.input.ime.onFocus.addListener(function(context) {
contextID = context.contextID;
});
function updateAltGrState(keyData) {
altGrState = (keyData.code == "AltRight") ? ((keyData.type == "keydown") ? AltGr.ALTERNATE : AltGr.PLAIN)
: altGrState;
}
function updateShiftState(keyData) {
shiftState = ((keyData.shiftKey && !(keyData.capsLock)) || (!(keyData.shiftKey) && keyData.capsLock)) ?
Shift.SHIFTED : Shift.PLAIN;
}
function isPureModifier(keyData) {
return (keyData.key == "Shift") || (keyData.key == "Ctrl") || (keyData.key == "Alt");
}
function isRemappedEvent(keyData) {
// hack, should check for a sender ID (to be added to KeyData)
return lastRemappedKeyEvent != undefined &&
(lastRemappedKeyEvent.key == keyData.key &&
lastRemappedKeyEvent.code == keyData.code &&
lastRemappedKeyEvent.type == keyData.type
); // requestID would be different so we are not checking for it
}
chrome.input.ime.onKeyEvent.addListener(
function(engineID, keyData) {
var handled = false;
if (isRemappedEvent(keyData)) {
lastRemappedKeyEvent = undefined;
return handled;
}
updateAltGrState(keyData);
updateShiftState(keyData);
if (lut[keyData.code]) {
//avoid hell key:process loop
if (keyData.ctrlKey === true && keyData.code != "Space") {
return;
}
var remappedKeyData = keyData;
remappedKeyData.key = lut[keyData.code][altGrState][shiftState];
remappedKeyData.code = lut[keyData.code].code;
if (chrome.input.ime.sendKeyEvents != undefined) {
chrome.input.ime.sendKeyEvents({"contextID": contextID, "keyData": [remappedKeyData]});
handled = true;
lastRemappedKeyEvent = remappedKeyData;
} else if (keyData.type == "keydown" && !isPureModifier(keyData)) {
chrome.input.ime.commitText({"contextID": contextID, "text": remappedKeyData.key});
handled = true;
}
}
return handled;
});
`
const layoutLines = [""]
Object.entries(klfDefaultLayout).forEach(([key, value]) => {
const extensions =
`: { "plain": {"plain": "${toCheck(layout.keys[key][0])}"` +
`, "shifted": "${toCheck(layout.keys[key][1])}"}` +
`, "alternate": {"plain": "${toCheck(layout.keys[key][3])}"` +
`, "shifted":"${toCheck(layout.keys[key][5])}"}, ` +
`"code": "${value}"},`
layoutLines.push([`"${value}"`, ...extensions].join(""))
})
fs.writeFileSync(
outputPath,
[lines, layoutLines.join("\n"),endLines].join(""), {
encoding: "utf8",
},
)
}