Repository navigation
Expand file tree
/
Copy pathkeyboard-config.js
More file actions
145 lines (124 loc) · 5.11 KB
/
Copy pathkeyboard-config.js
File metadata and controls
145 lines (124 loc) · 5.11 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
let utils = require('./utils');
const { Cc, Ci, Cr, Cu, CC } = require('chrome');
Cu.import('resource://gre/modules/Services.jsm');
exports.copyLayoutsAndResources = copyLayoutsAndResources;
exports.addEntryPointsToManifest = addEntryPointsToManifest;
function copyLayoutsAndResources(config) {
// This is the source dir for the keyboard app
let appDir = utils.getFile(config.GAIA_DIR, 'apps', 'keyboard');
// Here is where the layouts and dictionaries get copied to
let layoutDest = utils.getFile(appDir.path, 'js', 'layouts');
let dictDest = utils.getFile(appDir.path,
'js', 'imes', 'latin', 'dictionaries');
let imeDest = utils.getFile(appDir.path, 'js', 'imes');
// First delete any layouts or dictionaries that are in the src dir
// from the last time.
utils.ensureFolderExists(layoutDest);
utils.ensureFolderExists(dictDest);
utils.ls(layoutDest, false).forEach(function(f) { f.remove(false); });
utils.ls(dictDest).forEach(function(f) { f.remove(false); });
utils.ls(imeDest).forEach(function(f) {
if (f.leafName !== 'latin')
f.remove(true);
});
// Now get the set of layouts for this build
let layouts = getLayouts(config);
// Loop through the layouts and copy the layout file and dictionary file
layouts.forEach(function(layout) {
// Copy the layout file to where it needs to go
layout.file.copyTo(layoutDest, layout.file.leafName);
try {
if (layout.imEngineDir)
layout.imEngineDir.copyTo(imeDest, layout.imEngineDir.leafName);
}
catch(e) {
throw new Error('Unknown ime directory ' + layout.imEngineDir.path +
' for keyboard layout ' + layout.name);
}
// Not to copy dictionies for v1.3t, to disable the word suggestion
// and save ROM space. See Bug 991627.
/*
try {
if (layout.dictFile)
layout.dictFile.copyTo(dictDest, layout.dictFile.leafName);
}
catch(e) {
throw new Error('Unknown dictionary file ' + layout.dictFile.path +
' for keyboard layout ' + layout.name);
}
*/
});
}
function addEntryPointsToManifest(config, manifest) {
// Get the set of layouts
let layouts = getLayouts(config);
// The inputs property of the manifest object has one property
// for each keyboard layout we support. The manifest file has a hard-coded
// entry for the numeric layout, but we have to add each additional layout.
layouts.forEach(function(layout) {
manifest.inputs[layout.name] = {
launch_path: '/index.html#' + layout.name,
name: layout.label,
description: layout.label,
types: layout.types
};
});
// Finally, save the modified manifest into the target directory
return manifest;
}
// Read the keyboard layout file for each of the named keyboard layouts in
// GAIA_KEYBOARD_LAYOUTS, and return an array of layout objects
function getLayouts(config) {
// These are the keyboard layouts requested at build time
let layoutNames = config.GAIA_KEYBOARD_LAYOUTS.split(',');
// Here is where the layouts and dictionaries come from
let layoutSrc = utils.getFile(config.GAIA_DIR, 'keyboard', 'layouts');
let dictSrc = utils.getFile(config.GAIA_DIR, 'keyboard', 'dictionaries');
let imeSrc = utils.getFile(config.GAIA_DIR, 'keyboard', 'imes');
// Read the layout files and find their names and dictionaries,
// and copy them into the app package
let layouts = layoutNames.map(function(layoutName) {
let layoutFile = utils.getFile(layoutSrc.path, layoutName + '.js');
try {
return getLayoutDetails(layoutName, layoutFile);
}
catch(e) {
// keep the original Error with its stack, just annotate which
// keyboard failed.
e.message = 'Problem with keyboard layout "' + layoutName +
'" in GAIA_KEYBOARD_LAYOUTS\n' + e.message;
throw e;
}
});
return layouts;
// Read the .js file for the named keyboard layout and extract
// the language name and auto-correct dictionary name.
function getLayoutDetails(layoutName, layoutFile) {
// The keyboard layout files are JavaScript files that add properties
// to the Keybords object. They are not clean JSON, so we have to use
// use the scriptloader to load them. That also gives stacktraces for
// errors inside the keyboard file.
// They reference globals KeyEvent and KeyboardEvent, so we
// have to define those on the context object.
var win = {Keyboards: {},
KeyEvent: {},
KeyboardEvent: {}};
Services.scriptloader.loadSubScript('file://' + layoutFile.path, win, 'UTF-8');
let dictName = win.Keyboards[layoutName].autoCorrectLanguage;
let dictFile = dictName
? utils.getFile(dictSrc.path, dictName + '.dict')
: null;
let imEngineName = win.Keyboards[layoutName].imEngine;
let imEngineDir = (imEngineName && imEngineName !== 'latin')
? utils.getFile(imeSrc.path, imEngineName)
: null;
return {
name: layoutName,
label: win.Keyboards[layoutName].menuLabel,
file: layoutFile,
types: win.Keyboards[layoutName].types,
dictFile: dictFile,
imEngineDir: imEngineDir
};
}
}