-
Notifications
You must be signed in to change notification settings - Fork 21
/
build-shortcuts.js
119 lines (99 loc) · 2.95 KB
/
build-shortcuts.js
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
"use strict";
/**
*
* Builds the keyboard shortcuts help page.
*
* node build-shortcuts.js [-i] grist-root
*
* Reads the grist shortcuts from the grist source tree specified by `grist-root` and generates a
* formatted content, which is then inserted into the target file (`help/en/docs/keyboard-shortcuts.md`)
* in-between the two markers `<!-- START -->` and `<!-- END -->`. Logs the resulting page to
* standard output, or save to the target file if `-i` (the edit in place option) is passed.
*
*/
const fs = require('fs');
const path = require('path');
const TARGET_FILE = __dirname + '/help/en/docs/keyboard-shortcuts.md';
const KEY_MAP_MAC = {
Mod: '⌘',
Alt: '⌥',
Shift: '⇧',
Ctrl: '⌃',
Left: '←',
Right: '→',
Up: '↑',
Down: '↓',
};
const KEY_MAP_WIN = {
Mod: 'Ctrl',
Left: '←',
Right: '→',
Up: '↑',
Down: '↓',
};
function getHumanKey(key, isMac) {
const keyMap = isMac ? KEY_MAP_MAC : KEY_MAP_WIN;
let keys = key.split('+').map(s => s.trim());
keys = keys.map(k => {
if (k in keyMap) { return keyMap[k]; }
if (k.length === 1) { return k.toUpperCase(); }
return k;
});
keys = keys.map(k => `*${k}*`);
return keys.join(isMac ? ' ' : ' + ');
}
function dumpKeys(groups) {
let content = '';
groups.forEach((group) => {
// Build the table content
let tableContent = '';
group.commands.forEach((cmd) => {
if (!cmd.keys || !cmd.keys.length || !cmd.desc) { return; }
const macHumanKeys = cmd.keys.map((key) => `<code class="keys">${getHumanKey(key, true)}</code>`).join(',');
const winHumanKeys = cmd.keys.map((key) => `<code class="keys">${getHumanKey(key, false)}</code>`).join(',');
tableContent += `| ${macHumanKeys} | ${winHumanKeys} | ${cmd.desc} |\n`;
});
if (!tableContent) { return ;}
// Adds group header
content += `###${group.group}\n`;
// Adds tables header
content += `| Key (Mac) | Key (Windows) | Description | \n`;
content += `| - | - | - | \n`;
content += tableContent;
content += '\n';
});
return content;
}
function main() {
const argv = process.argv.slice(2);
let i = 0;
let editInPlace = false;
let gristAppRoot;
for (; i < argv.length; ++i){
if (argv[i] === '-i') {
editInPlace = true;
} else {
gristAppRoot = argv[i];
}
}
if (!gristAppRoot) {
console.log('Missing file argument');
process.exit(1);
}
// loads commands from grist app source tree.
const {groups} = require(path.join(gristAppRoot, 'app/client/components/commandList'));
let content = '';
content += dumpKeys(groups);
const data = fs.readFileSync(TARGET_FILE, 'utf8');
const newFileContent = data.replace(/(?<=<!-- START -->\n).*(?=<!-- END -->)/s, content);
if (editInPlace) {
// write to target file
fs.writeFileSync(TARGET_FILE, newFileContent, 'utf8');
} else {
// dump to the std output
console.log(newFileContent);
}
}
if (require.main === module) {
main();
}