]> BookStack Code Mirror - bookstack/blob - app/Settings/UserShortcutMap.php
Started interface user shortcut form interface
[bookstack] / app / Settings / UserShortcutMap.php
1 <?php
2
3 namespace BookStack\Settings;
4
5 class UserShortcutMap
6 {
7     protected const DEFAULTS = [
8         // Header actions
9         "home_view" => "1",
10         "shelves_view" => "2",
11         "books_view" => "3",
12         "settings_view" => "4",
13         "favourites_view" => "5",
14         "profile_view" => "6",
15         "global_search" => "/",
16         "logout" => "0",
17
18         // Common actions
19         "edit" => "e",
20         "new" => "n",
21         "copy" => "c",
22         "delete" => "d",
23         "favourite" => "f",
24         "export" => "x",
25         "sort" => "s",
26         "permissions" => "p",
27         "move" => "m",
28         "revisions" => "r",
29
30         // Navigation
31         "next" => "ArrowRight",
32         "previous" => "ArrowLeft",
33     ];
34
35     /**
36      * @var array<string, string>
37      */
38     protected array $mapping;
39
40     public function __construct(array $map)
41     {
42         $this->mapping = static::DEFAULTS;
43         $this->merge($map);
44     }
45
46     /**
47      * Merge the given map into the current shortcut mapping.
48      */
49     protected function merge(array $map): void
50     {
51         foreach ($map as $key => $value) {
52             if (is_string($value) && isset($this->mapping[$key])) {
53                 $this->mapping[$key] = $value;
54             }
55         }
56     }
57
58     /**
59      * Get the shortcut defined for the given ID.
60      */
61     public function getShortcut(string $id): string
62     {
63         return $this->mapping[$id] ?? '';
64     }
65
66     /**
67      * Convert this mapping to JSON.
68      */
69     public function toJson(): string
70     {
71         return json_encode($this->mapping);
72     }
73
74     /**
75      * Create a new instance from the current user's preferences.
76      */
77     public static function fromUserPreferences(): self
78     {
79         $userKeyMap = setting()->getForCurrentUser('ui-shortcuts');
80         return new self(json_decode($userKeyMap, true) ?: []);
81     }
82 }