Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions lib/ui/app/mod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ import {Node} from "../../manifold/mod.tsx";
import {Menu} from "../menu/mod.tsx";
import { CommandPalette } from "../palette/mod.tsx";

// Run this only once, it's unlikely the OS will change without a reload of the page
const osType = (() => {
if (navigator.userAgent.toLowerCase().indexOf("win") != -1) return "win";
if (navigator.userAgent.toLowerCase().indexOf("mac") != -1) return "mac";
if (navigator.userAgent.toLowerCase().indexOf("linux") != -1) return "linux";
if (navigator.userAgent.toLowerCase().indexOf("x11") != -1) return "unix";
return "unknown";
})();

// Returns an os-specific key from an object, the wildcard, or the first
// Allows to define configurations that differ based on the OS the page is viewed from
const osSpecific = function<T = unknown>(options: Record<string, T>): T {

// Sanity checking
const keys = Object.keys(options);
if (keys.length <= 0) {
throw new Error("Invalid options record object given, must contain at least one option");
}

// Return os-specific, fallback, or first, in that order of preference
if (keys.includes(osType)) return options[osType];
if (keys.includes("*")) return options["*"];
return options[keys[0]];
};

window.env = new Environment(new LocalStorageStore());
env.commands.registerCommand({
Expand All @@ -16,7 +40,7 @@ env.commands.registerCommand({
m.redraw();
}
});
env.keybindings.registerBinding({command: "expand", key: "meta+arrowdown"});
env.keybindings.registerBinding({command: "expand", key: osSpecific({ "mac": "meta+arrowdown", "*": "ctrl+arrowdown" }) });
env.commands.registerCommand({
id: "collapse",
title: "Collapse",
Expand All @@ -26,7 +50,7 @@ env.commands.registerCommand({
m.redraw();
}
});
env.keybindings.registerBinding({command: "collapse", key: "meta+arrowup"});
env.keybindings.registerBinding({command: "collapse", key: osSpecific({ "mac": "meta+arrowup", "*": "ctrl+arrowup" }) });
env.commands.registerCommand({
id: "indent",
title: "Indent",
Expand Down Expand Up @@ -106,7 +130,7 @@ env.commands.registerCommand({
}
}
});
env.keybindings.registerBinding({command: "delete", key: "shift+meta+backspace"});
env.keybindings.registerBinding({command: "delete", key: osSpecific({ "mac": "shift+meta+backspace", "*": "shift+ctrl+backspace" }) });
env.commands.registerCommand({
id: "prev",
action: (ctx: Context) => {
Expand Down Expand Up @@ -180,4 +204,4 @@ export const App: m.Component = {
{env.workspace.palette && <CommandPalette {...env.workspace.palette} />}
</main>
}
};
};