common: getGroupedKeybindings should return an array

this is so the groups are always ordered. It also makes it easier to iterate
over the groups.
This commit is contained in:
Abdullah Atta
2025-07-22 13:26:27 +05:00
parent c41dfa3185
commit 1abf51ae2b
4 changed files with 112 additions and 86 deletions

View File

@@ -1,5 +1,29 @@
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { writeFileSync } from "fs";
import { getGroupedKeybindings, formatKey, macify } from "@notesnook/common";
import {
getGroupedKeybindings,
formatKey,
macify,
CATEGORIES
} from "@notesnook/common";
console.log("Generating keyboard shortcuts documentation...");
@@ -34,42 +58,42 @@ function getGroupedTableKeybindingsMarkdown() {
const header = `| Description | Web | Windows/Linux | Mac |
| --- | --- | --- | --- |`;
return Object.keys({ ...webKeybindings, ...desktopKeybindings })
.map((category) => {
const webShortcuts = webKeybindings[category] || [];
const desktopShortcuts = desktopKeybindings[category] || [];
return CATEGORIES.map((category) => {
const webShortcuts =
webKeybindings.find((g) => g.category === category)?.shortcuts || [];
const desktopShortcuts =
desktopKeybindings.find((g) => g.category === category)?.shortcuts || [];
const mergedShortcuts = {};
const mergedShortcuts = {};
webShortcuts.forEach(({ description, keys }) => {
if (!mergedShortcuts[description]) {
mergedShortcuts[description] = {};
}
mergedShortcuts[description].web = keys;
});
desktopShortcuts.forEach(({ description, keys }) => {
if (!mergedShortcuts[description]) {
mergedShortcuts[description] = {};
}
mergedShortcuts[description].desktop = keys;
});
webShortcuts.forEach(({ description, keys }) => {
if (!mergedShortcuts[description]) {
mergedShortcuts[description] = {};
}
mergedShortcuts[description].web = keys;
});
desktopShortcuts.forEach(({ description, keys }) => {
if (!mergedShortcuts[description]) {
mergedShortcuts[description] = {};
}
mergedShortcuts[description].desktop = keys;
});
const rows = Object.entries(mergedShortcuts)
.map(([description, { web, desktop }]) => {
const webKeys = web?.map((k) => formatKey(k)).join(" / ") || "-";
const windowsLinuxKeys =
desktop?.map((k) => formatKey(k)).join(" / ") || "-";
const macKeys =
desktop
?.map(macify)
.map((k) => formatKey(k, true))
.join(" / ") || "-";
const rows = Object.entries(mergedShortcuts)
.map(([description, { web, desktop }]) => {
const webKeys = web?.map((k) => formatKey(k)).join(" / ") || "-";
const windowsLinuxKeys =
desktop?.map((k) => formatKey(k)).join(" / ") || "-";
const macKeys =
desktop
?.map(macify)
.map((k) => formatKey(k, true))
.join(" / ") || "-";
return `| ${description} | ${webKeys} | ${windowsLinuxKeys} | ${macKeys} |`;
})
.join("\n");
return `| ${description} | ${webKeys} | ${windowsLinuxKeys} | ${macKeys} |`;
})
.join("\n");
return `### ${category}\n\n${header}\n${rows}`;
})
.join("\n\n");
return `### ${category}\n\n${header}\n${rows}`;
}).join("\n\n");
}