openproject-primer_view_components 0.94.0 → 0.96.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/app/assets/javascripts/primer_view_components.js +11 -1
  4. data/app/assets/javascripts/primer_view_components.js.map +1 -1
  5. data/app/assets/styles/primer_view_components.css +1 -1
  6. data/app/assets/styles/primer_view_components.css.map +1 -1
  7. data/app/components/primer/alpha/action_bar.css +1 -1
  8. data/app/components/primer/alpha/action_bar.css.map +1 -1
  9. data/app/components/primer/alpha/action_list/item.rb +5 -1
  10. data/app/components/primer/alpha/action_list.css +1 -1
  11. data/app/components/primer/alpha/action_list.css.json +13 -11
  12. data/app/components/primer/alpha/action_list.css.map +1 -1
  13. data/app/components/primer/alpha/action_list.pcss +12 -0
  14. data/app/components/primer/alpha/action_menu/action_menu_element.d.ts +6 -0
  15. data/app/components/primer/alpha/action_menu/action_menu_element.js +389 -84
  16. data/app/components/primer/alpha/action_menu/action_menu_element.ts +439 -78
  17. data/app/components/primer/alpha/action_menu/action_menu_focus_zone_stack.d.ts +4 -1
  18. data/app/components/primer/alpha/action_menu/action_menu_focus_zone_stack.js +37 -13
  19. data/app/components/primer/alpha/action_menu/action_menu_focus_zone_stack.ts +41 -12
  20. data/app/components/primer/alpha/action_menu/menu.html.erb +27 -19
  21. data/app/components/primer/alpha/action_menu/menu.rb +7 -0
  22. data/app/components/primer/alpha/action_menu/primary_menu.rb +15 -0
  23. data/app/components/primer/alpha/action_menu.rb +55 -0
  24. data/app/components/primer/alpha/auto_complete.css +1 -1
  25. data/app/components/primer/alpha/auto_complete.css.map +1 -1
  26. data/app/components/primer/alpha/banner.css +1 -1
  27. data/app/components/primer/alpha/banner.css.map +1 -1
  28. data/app/components/primer/alpha/button_marketing.css +1 -1
  29. data/app/components/primer/alpha/button_marketing.css.map +1 -1
  30. data/app/components/primer/alpha/dialog.css +1 -1
  31. data/app/components/primer/alpha/dialog.css.map +1 -1
  32. data/app/components/primer/alpha/dropdown/menu.rb +1 -1
  33. data/app/components/primer/alpha/dropdown.css +1 -1
  34. data/app/components/primer/alpha/dropdown.css.json +1 -1
  35. data/app/components/primer/alpha/dropdown.css.map +1 -1
  36. data/app/components/primer/alpha/layout.css +1 -1
  37. data/app/components/primer/alpha/layout.css.json +3 -3
  38. data/app/components/primer/alpha/layout.css.map +1 -1
  39. data/app/components/primer/alpha/menu.css +1 -1
  40. data/app/components/primer/alpha/menu.css.json +3 -3
  41. data/app/components/primer/alpha/menu.css.map +1 -1
  42. data/app/components/primer/alpha/overlay.css +1 -1
  43. data/app/components/primer/alpha/overlay.css.map +1 -1
  44. data/app/components/primer/alpha/segmented_control.css +1 -1
  45. data/app/components/primer/alpha/segmented_control.css.json +1 -1
  46. data/app/components/primer/alpha/segmented_control.css.map +1 -1
  47. data/app/components/primer/alpha/select_panel.css +1 -1
  48. data/app/components/primer/alpha/select_panel.css.map +1 -1
  49. data/app/components/primer/alpha/skeleton_box.css +1 -1
  50. data/app/components/primer/alpha/skeleton_box.css.map +1 -1
  51. data/app/components/primer/alpha/stack.css +1 -1
  52. data/app/components/primer/alpha/stack.css.map +1 -1
  53. data/app/components/primer/alpha/stack_item.css +1 -1
  54. data/app/components/primer/alpha/stack_item.css.map +1 -1
  55. data/app/components/primer/alpha/tab_nav.css +1 -1
  56. data/app/components/primer/alpha/tab_nav.css.map +1 -1
  57. data/app/components/primer/alpha/text_field.css +1 -1
  58. data/app/components/primer/alpha/text_field.css.map +1 -1
  59. data/app/components/primer/alpha/toggle_switch.css +1 -1
  60. data/app/components/primer/alpha/toggle_switch.css.map +1 -1
  61. data/app/components/primer/alpha/tree_view.css +1 -1
  62. data/app/components/primer/alpha/tree_view.css.json +3 -3
  63. data/app/components/primer/alpha/tree_view.css.map +1 -1
  64. data/app/components/primer/alpha/underline_nav.css +1 -1
  65. data/app/components/primer/alpha/underline_nav.css.map +1 -1
  66. data/app/components/primer/beta/border_box.css +1 -1
  67. data/app/components/primer/beta/border_box.css.json +5 -5
  68. data/app/components/primer/beta/border_box.css.map +1 -1
  69. data/app/components/primer/beta/button.css +1 -1
  70. data/app/components/primer/beta/button.css.map +1 -1
  71. data/app/components/primer/beta/button.pcss +97 -0
  72. data/app/components/primer/beta/button.rb +15 -3
  73. data/app/components/primer/beta/counter.css +1 -1
  74. data/app/components/primer/beta/counter.css.map +1 -1
  75. data/app/components/primer/beta/flash.css +1 -1
  76. data/app/components/primer/beta/flash.css.map +1 -1
  77. data/app/components/primer/beta/link.css +1 -1
  78. data/app/components/primer/beta/link.css.json +1 -1
  79. data/app/components/primer/beta/link.css.map +1 -1
  80. data/app/components/primer/beta/popover.css +1 -1
  81. data/app/components/primer/beta/popover.css.map +1 -1
  82. data/app/components/primer/beta/progress_bar.css +1 -1
  83. data/app/components/primer/beta/progress_bar.css.map +1 -1
  84. data/app/components/primer/beta/relative_time.rb +7 -1
  85. data/app/components/primer/beta/state.css +1 -1
  86. data/app/components/primer/beta/state.css.json +2 -2
  87. data/app/components/primer/beta/state.css.map +1 -1
  88. data/app/components/primer/beta/timeline_item.css +1 -1
  89. data/app/components/primer/beta/timeline_item.css.map +1 -1
  90. data/app/components/primer/open_project/border_grid.css +1 -1
  91. data/app/components/primer/open_project/border_grid.css.map +1 -1
  92. data/app/components/primer/open_project/color_field.css +1 -1
  93. data/app/components/primer/open_project/color_field.css.map +1 -1
  94. data/app/components/primer/open_project/color_swatch.css +1 -0
  95. data/app/components/primer/open_project/color_swatch.css.json +6 -0
  96. data/app/components/primer/open_project/color_swatch.css.map +1 -0
  97. data/app/components/primer/open_project/color_swatch.pcss +8 -0
  98. data/app/components/primer/open_project/color_swatch.rb +57 -0
  99. data/app/components/primer/open_project/inline_message.css +1 -1
  100. data/app/components/primer/open_project/inline_message.css.map +1 -1
  101. data/app/components/primer/open_project/input_group.css +1 -1
  102. data/app/components/primer/open_project/input_group.css.map +1 -1
  103. data/app/components/primer/open_project/inset_box.css +1 -0
  104. data/app/components/primer/open_project/inset_box.css.json +9 -0
  105. data/app/components/primer/open_project/inset_box.css.map +1 -0
  106. data/app/components/primer/open_project/inset_box.html.erb +46 -0
  107. data/app/components/primer/open_project/inset_box.pcss +24 -0
  108. data/app/components/primer/open_project/inset_box.rb +63 -0
  109. data/app/components/primer/open_project/keybinding_hint/chords.d.ts +5 -0
  110. data/app/components/primer/open_project/keybinding_hint/chords.js +29 -0
  111. data/app/components/primer/open_project/keybinding_hint/chords.ts +38 -0
  112. data/app/components/primer/open_project/keybinding_hint/key_names.d.ts +16 -0
  113. data/app/components/primer/open_project/keybinding_hint/key_names.js +107 -0
  114. data/app/components/primer/open_project/keybinding_hint/key_names.ts +116 -0
  115. data/app/components/primer/open_project/keybinding_hint/localized_names.d.ts +22 -0
  116. data/app/components/primer/open_project/keybinding_hint/localized_names.js +47 -0
  117. data/app/components/primer/open_project/keybinding_hint/localized_names.ts +71 -0
  118. data/app/components/primer/open_project/keybinding_hint/names.rb +92 -0
  119. data/app/components/primer/open_project/keybinding_hint/names_from_scope.d.ts +21 -0
  120. data/app/components/primer/open_project/keybinding_hint/names_from_scope.js +81 -0
  121. data/app/components/primer/open_project/keybinding_hint/names_from_scope.ts +96 -0
  122. data/app/components/primer/open_project/keybinding_hint/platform.d.ts +10 -0
  123. data/app/components/primer/open_project/keybinding_hint/platform.js +24 -0
  124. data/app/components/primer/open_project/keybinding_hint/platform.ts +34 -0
  125. data/app/components/primer/open_project/keybinding_hint/platform_override.d.ts +2 -0
  126. data/app/components/primer/open_project/keybinding_hint/platform_override.js +4 -0
  127. data/app/components/primer/open_project/keybinding_hint/platform_override.ts +7 -0
  128. data/app/components/primer/open_project/keybinding_hint.css +1 -0
  129. data/app/components/primer/open_project/keybinding_hint.css.json +10 -0
  130. data/app/components/primer/open_project/keybinding_hint.css.map +1 -0
  131. data/app/components/primer/open_project/keybinding_hint.html.erb +4 -0
  132. data/app/components/primer/open_project/keybinding_hint.pcss +65 -0
  133. data/app/components/primer/open_project/keybinding_hint.rb +112 -0
  134. data/app/components/primer/open_project/keybinding_hint_element.d.ts +31 -0
  135. data/app/components/primer/open_project/keybinding_hint_element.js +183 -0
  136. data/app/components/primer/open_project/keybinding_hint_element.ts +152 -0
  137. data/app/components/primer/open_project/page_header.css +1 -1
  138. data/app/components/primer/open_project/page_header.css.json +2 -2
  139. data/app/components/primer/open_project/page_header.css.map +1 -1
  140. data/app/components/primer/open_project/pagination.css +1 -1
  141. data/app/components/primer/open_project/pagination.css.json +1 -1
  142. data/app/components/primer/open_project/pagination.css.map +1 -1
  143. data/app/components/primer/open_project/sub_header.css +1 -1
  144. data/app/components/primer/open_project/sub_header.css.map +1 -1
  145. data/app/components/primer/primer.d.ts +1 -0
  146. data/app/components/primer/primer.js +1 -0
  147. data/app/components/primer/primer.pcss +3 -0
  148. data/app/components/primer/primer.ts +1 -0
  149. data/app/components/primer/truncate.css +1 -1
  150. data/app/components/primer/truncate.css.json +3 -4
  151. data/app/components/primer/truncate.css.map +1 -1
  152. data/app/controllers/primer/view_components/action_menu_controller.rb +35 -0
  153. data/app/views/primer/view_components/action_menu/deferred.html.erb +25 -11
  154. data/app/views/primer/view_components/action_menu/deferred_form.html.erb +10 -0
  155. data/app/views/primer/view_components/action_menu/deferred_preload.html.erb +7 -1
  156. data/config/locales/en.yml +179 -0
  157. data/config/routes.rb +1 -0
  158. data/lib/primer/accessibility.rb +1 -2
  159. data/lib/primer/classify.rb +1 -2
  160. data/lib/primer/view_components/version.rb +1 -1
  161. data/previews/primer/alpha/action_menu_preview/deferred_refresh.html.erb +34 -0
  162. data/previews/primer/alpha/action_menu_preview/deferred_refresh_form.html.erb +9 -0
  163. data/previews/primer/alpha/action_menu_preview.rb +14 -0
  164. data/previews/primer/open_project/color_swatch_preview.rb +27 -0
  165. data/previews/primer/open_project/inset_box_preview.rb +85 -0
  166. data/previews/primer/open_project/keybinding_hint_preview/full_format.html.erb +3 -0
  167. data/previews/primer/open_project/keybinding_hint_preview/in_action_list.html.erb +12 -0
  168. data/previews/primer/open_project/keybinding_hint_preview/in_button.html.erb +24 -0
  169. data/previews/primer/open_project/keybinding_hint_preview/platforms.html.erb +8 -0
  170. data/previews/primer/open_project/keybinding_hint_preview/playground.html.erb +4 -0
  171. data/previews/primer/open_project/keybinding_hint_preview/prose.html.erb +3 -0
  172. data/previews/primer/open_project/keybinding_hint_preview/schemes.html.erb +6 -0
  173. data/previews/primer/open_project/keybinding_hint_preview/sequence.html.erb +1 -0
  174. data/previews/primer/open_project/keybinding_hint_preview/sizes.html.erb +4 -0
  175. data/previews/primer/open_project/keybinding_hint_preview.rb +71 -0
  176. data/static/arguments.json +109 -1
  177. data/static/audited_at.json +3 -0
  178. data/static/classes.json +27 -0
  179. data/static/classnames.cjs +9 -0
  180. data/static/classnames.js +9 -0
  181. data/static/constants.json +63 -0
  182. data/static/info_arch.json +523 -8
  183. data/static/previews.json +310 -0
  184. data/static/statuses.json +3 -0
  185. metadata +55 -5
  186. data/app/components/primer/alpha/text_field.css.json +0 -134
  187. data/app/components/primer/alpha/toggle_switch.css.json +0 -40
  188. data/app/components/primer/alpha/underline_nav.css.json +0 -26
  189. data/app/components/primer/beta/button.css.json +0 -86
@@ -0,0 +1,116 @@
1
+ import type {Platform} from './platform'
2
+
3
+ /** Converts the first character of the string to upper case and the remaining to lower case. */
4
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
5
+ const capitalize = ([first, ...rest]: string) => (first?.toUpperCase() ?? '') + rest.join('').toLowerCase()
6
+
7
+ // In the below records, we don't intend to cover every single possible key - only those that
8
+ // would be realistically used in shortcuts. For example, the Pause/Break key is not necessary
9
+ // because it is not found on many keyboards.
10
+
11
+ /**
12
+ * Short-form iconic versions of keys. These should be intuitive (not archaic) and match icons on keyboards.
13
+ */
14
+ export const condensedKeyName = (key: string, platform: Platform) =>
15
+ ({
16
+ alt: platform === 'apple' ? '⌥' : 'Alt', // the alt key _is_ the option key on MacOS - in the browser there is no "option" key
17
+ control: '⌃',
18
+ shift: '⇧',
19
+ meta: platform === 'apple' ? '⌘' : platform === 'windows' ? 'Win' : 'Meta',
20
+ mod: platform === 'apple' ? '⌘' : '⌃',
21
+ pageup: 'PgUp',
22
+ pagedown: 'PgDn',
23
+ arrowup: '↑',
24
+ arrowdown: '↓',
25
+ arrowleft: '←',
26
+ arrowright: '→',
27
+ plus: '+', // needed to allow +-separated chords
28
+ backspace: '⌫',
29
+ delete: 'Del',
30
+ space: '␣', // needed to allow space-separated sequences
31
+ tab: '⇥',
32
+ enter: '⏎',
33
+ escape: 'Esc',
34
+ function: 'Fn',
35
+ capslock: 'CapsLock',
36
+ insert: 'Ins',
37
+ printscreen: 'PrtScn',
38
+ })[key] ?? capitalize(key)
39
+
40
+ /**
41
+ * Specific key displays for 'full' format. We still do show some icons (ie punctuation)
42
+ * because that's more intuitive, but for the rest of keys we show the standard key name.
43
+ */
44
+ export const fullKeyName = (key: string, platform: Platform) =>
45
+ ({
46
+ alt: platform === 'apple' ? 'Option' : 'Alt',
47
+ meta: platform === 'apple' ? 'Command' : platform === 'windows' ? 'Windows' : 'Meta',
48
+ mod: platform === 'apple' ? 'Command' : 'Control',
49
+ '+': 'Plus',
50
+ pageup: 'Page Up',
51
+ pagedown: 'Page Down',
52
+ arrowup: 'Up Arrow',
53
+ arrowdown: 'Down Arrow',
54
+ arrowleft: 'Left Arrow',
55
+ arrowright: 'Right Arrow',
56
+ capslock: 'Caps Lock',
57
+ printscreen: 'Print Screen',
58
+ })[key] ?? capitalize(key)
59
+
60
+ /**
61
+ * Accessible key names intended to be read by a screen reader. This prevents screen
62
+ * readers from expressing punctuation in speech, ie, reading a long pause instead of the
63
+ * word "period".
64
+ */
65
+ export const accessibleKeyName = (key: string, platform: Platform) =>
66
+ ({
67
+ alt: platform === 'apple' ? 'option' : 'alt',
68
+ meta: platform === 'apple' ? 'command' : platform === 'windows' ? 'Windows' : 'meta',
69
+ mod: platform === 'apple' ? 'command' : 'control',
70
+ // Screen readers may not be able to pronounce concatenated words - this provides a better experience
71
+ pageup: 'page up',
72
+ pagedown: 'page down',
73
+ arrowup: 'up arrow',
74
+ arrowdown: 'down arrow',
75
+ arrowleft: 'left arrow',
76
+ arrowright: 'right arrow',
77
+ capslock: 'caps lock',
78
+ printscreen: 'print screen',
79
+ // We don't need to represent _every_ symbol - only those found on standard keyboards.
80
+ // Other symbols should be avoided as keyboard shortcuts anyway.
81
+ // These should match the colloquial names of the keys, not the names of the symbols. Ie,
82
+ // "Equals" not "Equal Sign", "Dash" not "Minus", "Period" not "Dot", etc.
83
+ '`': 'backtick',
84
+ '~': 'tilde',
85
+ '!': 'exclamation point',
86
+ '@': 'at',
87
+ '#': 'hash',
88
+ $: 'dollar sign',
89
+ '%': 'percent',
90
+ '^': 'caret',
91
+ '&': 'ampersand',
92
+ '*': 'asterisk',
93
+ '(': 'left parenthesis',
94
+ ')': 'right parenthesis',
95
+ _: 'underscore',
96
+ '-': 'dash',
97
+ '+': 'plus',
98
+ '=': 'equals',
99
+ '[': 'left bracket',
100
+ '{': 'left curly brace',
101
+ ']': 'right bracket',
102
+ '}': 'right curly brace',
103
+ '\\': 'backslash',
104
+ '|': 'pipe',
105
+ ';': 'semicolon',
106
+ ':': 'colon',
107
+ "'": 'single quote',
108
+ '"': 'double quote',
109
+ ',': 'comma',
110
+ '<': 'left angle bracket',
111
+ '.': 'period',
112
+ '>': 'right angle bracket',
113
+ '/': 'forward slash',
114
+ '?': 'question mark',
115
+ ' ': 'space',
116
+ })[key] ?? key.toLowerCase()
@@ -0,0 +1,22 @@
1
+ import type { Platform } from './platform';
2
+ export type NameFormat = 'condensed' | 'full' | 'accessible';
3
+ type PlatformNames = Partial<Record<Platform, string>>;
4
+ /** `separator` is the JSON `then` (the word spoken between chords). */
5
+ export type KeyNames = {
6
+ separator?: string;
7
+ keys: Record<string, Partial<Record<NameFormat, PlatformNames>>>;
8
+ };
9
+ /** Same notion of blank as Ruby's `String#blank?` (`[[:space:]]`, i.e. Unicode White_Space). */
10
+ export declare const nonBlank: (value: unknown) => string | undefined;
11
+ /** Parses the `data-names` attribute; anything malformed yields no names. */
12
+ export declare function parseKeyNames(json: string): KeyNames;
13
+ /** Exact platform, then `other`; `undefined` when no usable name was given. */
14
+ export declare function localizedKeyName(names: KeyNames, key: string, format: NameFormat, platform: Platform): string | undefined;
15
+ /**
16
+ * Upstream's English name. The mirrored tables index plain object literals, so
17
+ * a key such as `constructor` would return an `Object.prototype` member;
18
+ * those keys get upstream's default spelling instead.
19
+ */
20
+ export declare function englishKeyName(key: string, format: NameFormat, platform: Platform): string;
21
+ export declare function keyName(names: KeyNames, key: string, format: NameFormat, platform: Platform): string;
22
+ export {};
@@ -0,0 +1,47 @@
1
+ import { accessibleKeyName, condensedKeyName, fullKeyName } from './key_names';
2
+ const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
3
+ const own = (record, key) => Object.hasOwn(record, key) ? record[key] : undefined;
4
+ /** Same notion of blank as Ruby's `String#blank?` (`[[:space:]]`, i.e. Unicode White_Space). */
5
+ export const nonBlank = (value) => typeof value === 'string' && !/^\p{White_Space}*$/u.test(value) ? value : undefined;
6
+ /** Parses the `data-names` attribute; anything malformed yields no names. */
7
+ export function parseKeyNames(json) {
8
+ try {
9
+ const parsed = JSON.parse(json);
10
+ if (isRecord(parsed) && isRecord(parsed.keys)) {
11
+ return { separator: nonBlank(parsed['then']), keys: parsed.keys };
12
+ }
13
+ }
14
+ catch {
15
+ // Fall through to no names: upstream English applies.
16
+ }
17
+ return { keys: {} };
18
+ }
19
+ /** Exact platform, then `other`; `undefined` when no usable name was given. */
20
+ export function localizedKeyName(names, key, format, platform) {
21
+ const entry = own(names.keys, key);
22
+ if (!isRecord(entry))
23
+ return undefined;
24
+ const platforms = own(entry, format);
25
+ if (!isRecord(platforms))
26
+ return undefined;
27
+ return nonBlank(own(platforms, platform)) ?? nonBlank(own(platforms, 'other'));
28
+ }
29
+ const upstreamNames = {
30
+ condensed: condensedKeyName,
31
+ full: fullKeyName,
32
+ accessible: accessibleKeyName,
33
+ };
34
+ const capitalize = (key) => key.charAt(0).toUpperCase() + key.slice(1).toLowerCase();
35
+ /**
36
+ * Upstream's English name. The mirrored tables index plain object literals, so
37
+ * a key such as `constructor` would return an `Object.prototype` member;
38
+ * those keys get upstream's default spelling instead.
39
+ */
40
+ export function englishKeyName(key, format, platform) {
41
+ if (key in Object.prototype)
42
+ return format === 'accessible' ? key.toLowerCase() : capitalize(key);
43
+ return upstreamNames[format](key, platform);
44
+ }
45
+ export function keyName(names, key, format, platform) {
46
+ return localizedKeyName(names, key, format, platform) ?? englishKeyName(key, format, platform);
47
+ }
@@ -0,0 +1,71 @@
1
+ import type {Platform} from './platform'
2
+ import {accessibleKeyName, condensedKeyName, fullKeyName} from './key_names'
3
+
4
+ export type NameFormat = 'condensed' | 'full' | 'accessible'
5
+
6
+ type PlatformNames = Partial<Record<Platform, string>>
7
+
8
+ /** `separator` is the JSON `then` (the word spoken between chords). */
9
+ export type KeyNames = {
10
+ separator?: string
11
+ keys: Record<string, Partial<Record<NameFormat, PlatformNames>>>
12
+ }
13
+
14
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
15
+ typeof value === 'object' && value !== null && !Array.isArray(value)
16
+
17
+ const own = (record: Record<string, unknown>, key: string): unknown =>
18
+ Object.hasOwn(record, key) ? record[key] : undefined
19
+
20
+ /** Same notion of blank as Ruby's `String#blank?` (`[[:space:]]`, i.e. Unicode White_Space). */
21
+ export const nonBlank = (value: unknown): string | undefined =>
22
+ typeof value === 'string' && !/^\p{White_Space}*$/u.test(value) ? value : undefined
23
+
24
+ /** Parses the `data-names` attribute; anything malformed yields no names. */
25
+ export function parseKeyNames(json: string): KeyNames {
26
+ try {
27
+ const parsed: unknown = JSON.parse(json)
28
+ if (isRecord(parsed) && isRecord(parsed.keys)) {
29
+ return {separator: nonBlank(parsed['then']), keys: parsed.keys as KeyNames['keys']}
30
+ }
31
+ } catch {
32
+ // Fall through to no names: upstream English applies.
33
+ }
34
+ return {keys: {}}
35
+ }
36
+
37
+ /** Exact platform, then `other`; `undefined` when no usable name was given. */
38
+ export function localizedKeyName(
39
+ names: KeyNames,
40
+ key: string,
41
+ format: NameFormat,
42
+ platform: Platform,
43
+ ): string | undefined {
44
+ const entry = own(names.keys, key)
45
+ if (!isRecord(entry)) return undefined
46
+ const platforms = own(entry, format)
47
+ if (!isRecord(platforms)) return undefined
48
+ return nonBlank(own(platforms, platform)) ?? nonBlank(own(platforms, 'other'))
49
+ }
50
+
51
+ const upstreamNames: Record<NameFormat, (key: string, platform: Platform) => string> = {
52
+ condensed: condensedKeyName,
53
+ full: fullKeyName,
54
+ accessible: accessibleKeyName,
55
+ }
56
+
57
+ const capitalize = (key: string) => key.charAt(0).toUpperCase() + key.slice(1).toLowerCase()
58
+
59
+ /**
60
+ * Upstream's English name. The mirrored tables index plain object literals, so
61
+ * a key such as `constructor` would return an `Object.prototype` member;
62
+ * those keys get upstream's default spelling instead.
63
+ */
64
+ export function englishKeyName(key: string, format: NameFormat, platform: Platform): string {
65
+ if (key in Object.prototype) return format === 'accessible' ? key.toLowerCase() : capitalize(key)
66
+ return upstreamNames[format](key, platform)
67
+ }
68
+
69
+ export function keyName(names: KeyNames, key: string, format: NameFormat, platform: Platform): string {
70
+ return localizedKeyName(names, key, format, platform) ?? englishKeyName(key, format, platform)
71
+ }
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ module OpenProject
5
+ class KeybindingHint < Primer::Component
6
+ # Translated key names for the keys of one hint, in the shape
7
+ # `<keybinding-hint>` reads from its `data-names` attribute:
8
+ #
9
+ # { "then" => "dann", "keys" => { "control" => { "condensed" => { "other" => "Strg" } } } }
10
+ #
11
+ # Entries come from the `keybinding_hint` locale scope. Missing
12
+ # translations fall back to the English defaults in PVC's `en.yml`.
13
+ module Names
14
+ module_function
15
+
16
+ FORMATS = %w[condensed full accessible].freeze
17
+ PLATFORMS = %w[apple windows other].freeze
18
+
19
+ def for(keys)
20
+ english = scope_for(:en)
21
+ locale = I18n.locale == :en ? {} : scope_for(I18n.locale)
22
+ index = token_index(english["keys"])
23
+
24
+ {
25
+ "then" => present(locale["then"]) || english["then"],
26
+ "keys" => tokens(keys).each_with_object({}) do |token, names|
27
+ entry = index[token]
28
+ next unless entry
29
+
30
+ names[token] = entry_names(english["keys"][entry], hash_at(locale, "keys", entry))
31
+ end
32
+ }
33
+ end
34
+
35
+ def tokens(keys)
36
+ keys.to_s.split(/\s+/)
37
+ .flat_map { |chord| chord.split("+") }
38
+ .reject(&:empty?)
39
+ .map(&:downcase)
40
+ .flat_map { |token| token == "mod" ? %w[control meta] : [token] }
41
+ .uniq
42
+ end
43
+
44
+ # I18n keys cannot contain `.`, so punctuation entries carry their
45
+ # literal key in a `key` field. Built from English only: the field is
46
+ # not translatable.
47
+ def token_index(entries)
48
+ entries.each_with_object({}) do |(name, entry), index|
49
+ index[entry.fetch("key", name)] = name if entry.is_a?(Hash)
50
+ end
51
+ end
52
+
53
+ # Locale data comes from translators; anything that is not a
54
+ # non-blank string in the expected place is ignored, and symbols such
55
+ # as ⌘ cannot be replaced.
56
+ def entry_names(english_entry, locale_entry)
57
+ FORMATS.each_with_object({}) do |format, names|
58
+ english = platform_names(english_entry[format])
59
+ translated = platform_names(hash_at(locale_entry, format)).reject { |platform, _| symbol?(english[platform]) }
60
+ platforms = english.merge(translated)
61
+ names[format] = platforms if platforms.any?
62
+ end
63
+ end
64
+
65
+ def symbol?(name)
66
+ name.present? && !name.match?(/\p{L}/)
67
+ end
68
+
69
+ def platform_names(value)
70
+ return {} unless value.is_a?(Hash)
71
+
72
+ value.each_with_object({}) do |(platform, name), names|
73
+ names[platform] = name if PLATFORMS.include?(platform) && present(name)
74
+ end
75
+ end
76
+
77
+ def hash_at(hash, *path)
78
+ path.reduce(hash) { |node, key| node.is_a?(Hash) ? node[key] : nil }.then { |node| node.is_a?(Hash) ? node : {} }
79
+ end
80
+
81
+ def present(value)
82
+ value if value.is_a?(String) && value.present?
83
+ end
84
+
85
+ def scope_for(locale)
86
+ scope = I18n.t(:keybinding_hint, locale: locale, default: {})
87
+ scope.is_a?(Hash) ? scope.deep_stringify_keys : {}
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,21 @@
1
+ import type { Platform } from './platform';
2
+ import type { NameFormat } from './localized_names';
3
+ type PlatformNames = Partial<Record<Platform, string>>;
4
+ type EntryNames = Partial<Record<NameFormat, PlatformNames>>;
5
+ /** The `data-names` JSON shape; identical to Ruby's `KeybindingHint::Names.for`. */
6
+ export type KeyNamesData = {
7
+ then: string | undefined;
8
+ keys: Record<string, EntryNames>;
9
+ };
10
+ /**
11
+ * Keys as Ruby tokenises them: the server cannot know the platform, so `mod`
12
+ * contributes both `control` and `meta`. Whitespace matches Ruby's `\s`.
13
+ */
14
+ export declare const tokens: (keys: string) => string[];
15
+ /**
16
+ * Builds `data-names` for `keys` from the `keybinding_hint` locale scopes, as
17
+ * i18n-js returns them. Translator data is untrusted: only non-blank strings in
18
+ * the expected place are used, and symbols such as ⌘ cannot be replaced.
19
+ */
20
+ export declare function buildKeyNames(keys: string, localeScope: unknown, englishScope: unknown): KeyNamesData;
21
+ export {};
@@ -0,0 +1,81 @@
1
+ import { nonBlank } from './localized_names';
2
+ const FORMATS = ['condensed', 'full', 'accessible'];
3
+ const PLATFORMS = ['apple', 'windows', 'other'];
4
+ const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
5
+ const own = (value, key) => isRecord(value) && Object.hasOwn(value, key) ? value[key] : undefined;
6
+ const isSymbol = (name) => name !== undefined && !/\p{L}/u.test(name);
7
+ /**
8
+ * Keys as Ruby tokenises them: the server cannot know the platform, so `mod`
9
+ * contributes both `control` and `meta`. Whitespace matches Ruby's `\s`.
10
+ */
11
+ export const tokens = (keys) => [
12
+ ...new Set(keys
13
+ .split(/[ \t\r\n\f\v]+/)
14
+ .flatMap(chord => chord.split('+'))
15
+ .filter(token => token !== '')
16
+ .map(token => token.toLowerCase())
17
+ .flatMap(token => (token === 'mod' ? ['control', 'meta'] : [token]))),
18
+ ];
19
+ /** I18n keys cannot contain `.`, so punctuation entries carry their key in `key`. */
20
+ const tokenIndex = (entries) => {
21
+ const index = new Map();
22
+ if (!isRecord(entries))
23
+ return index;
24
+ for (const [name, entry] of Object.entries(entries)) {
25
+ if (!isRecord(entry))
26
+ continue;
27
+ const key = own(entry, 'key');
28
+ index.set(typeof key === 'string' ? key : name, name);
29
+ }
30
+ return index;
31
+ };
32
+ const platformNames = (value) => {
33
+ const names = {};
34
+ for (const platform of PLATFORMS) {
35
+ const name = nonBlank(own(value, platform));
36
+ if (name !== undefined)
37
+ names[platform] = name;
38
+ }
39
+ return names;
40
+ };
41
+ const entryNames = (englishEntry, localeEntry) => {
42
+ const names = {};
43
+ for (const format of FORMATS) {
44
+ const english = platformNames(own(englishEntry, format));
45
+ const translated = platformNames(own(localeEntry, format));
46
+ for (const platform of PLATFORMS) {
47
+ if (isSymbol(english[platform]))
48
+ delete translated[platform];
49
+ }
50
+ const merged = { ...english, ...translated };
51
+ if (Object.keys(merged).length > 0)
52
+ names[format] = merged;
53
+ }
54
+ return names;
55
+ };
56
+ /**
57
+ * Builds `data-names` for `keys` from the `keybinding_hint` locale scopes, as
58
+ * i18n-js returns them. Translator data is untrusted: only non-blank strings in
59
+ * the expected place are used, and symbols such as ⌘ cannot be replaced.
60
+ */
61
+ export function buildKeyNames(keys, localeScope, englishScope) {
62
+ const englishKeys = own(englishScope, 'keys');
63
+ const index = tokenIndex(englishKeys);
64
+ const result = {
65
+ then: nonBlank(own(localeScope, 'then')) ?? nonBlank(own(englishScope, 'then')),
66
+ keys: {},
67
+ };
68
+ for (const token of tokens(keys)) {
69
+ const entry = index.get(token);
70
+ if (entry === undefined)
71
+ continue;
72
+ // defineProperty, not assignment: a `__proto__` token must stay an own key.
73
+ Object.defineProperty(result.keys, token, {
74
+ value: entryNames(own(englishKeys, entry), own(own(localeScope, 'keys'), entry)),
75
+ enumerable: true,
76
+ writable: true,
77
+ configurable: true,
78
+ });
79
+ }
80
+ return result;
81
+ }
@@ -0,0 +1,96 @@
1
+ import type {Platform} from './platform'
2
+ import {nonBlank} from './localized_names'
3
+ import type {NameFormat} from './localized_names'
4
+
5
+ type PlatformNames = Partial<Record<Platform, string>>
6
+ type EntryNames = Partial<Record<NameFormat, PlatformNames>>
7
+
8
+ /** The `data-names` JSON shape; identical to Ruby's `KeybindingHint::Names.for`. */
9
+ export type KeyNamesData = {then: string | undefined; keys: Record<string, EntryNames>}
10
+
11
+ const FORMATS: readonly NameFormat[] = ['condensed', 'full', 'accessible']
12
+ const PLATFORMS: readonly Platform[] = ['apple', 'windows', 'other']
13
+
14
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
15
+ typeof value === 'object' && value !== null && !Array.isArray(value)
16
+
17
+ const own = (value: unknown, key: string): unknown =>
18
+ isRecord(value) && Object.hasOwn(value, key) ? value[key] : undefined
19
+
20
+ const isSymbol = (name: string | undefined) => name !== undefined && !/\p{L}/u.test(name)
21
+
22
+ /**
23
+ * Keys as Ruby tokenises them: the server cannot know the platform, so `mod`
24
+ * contributes both `control` and `meta`. Whitespace matches Ruby's `\s`.
25
+ */
26
+ export const tokens = (keys: string): string[] => [
27
+ ...new Set(
28
+ keys
29
+ .split(/[ \t\r\n\f\v]+/)
30
+ .flatMap(chord => chord.split('+'))
31
+ .filter(token => token !== '')
32
+ .map(token => token.toLowerCase())
33
+ .flatMap(token => (token === 'mod' ? ['control', 'meta'] : [token])),
34
+ ),
35
+ ]
36
+
37
+ /** I18n keys cannot contain `.`, so punctuation entries carry their key in `key`. */
38
+ const tokenIndex = (entries: unknown): Map<string, string> => {
39
+ const index = new Map<string, string>()
40
+ if (!isRecord(entries)) return index
41
+ for (const [name, entry] of Object.entries(entries)) {
42
+ if (!isRecord(entry)) continue
43
+ const key = own(entry, 'key')
44
+ index.set(typeof key === 'string' ? key : name, name)
45
+ }
46
+ return index
47
+ }
48
+
49
+ const platformNames = (value: unknown): PlatformNames => {
50
+ const names: PlatformNames = {}
51
+ for (const platform of PLATFORMS) {
52
+ const name = nonBlank(own(value, platform))
53
+ if (name !== undefined) names[platform] = name
54
+ }
55
+ return names
56
+ }
57
+
58
+ const entryNames = (englishEntry: unknown, localeEntry: unknown): EntryNames => {
59
+ const names: EntryNames = {}
60
+ for (const format of FORMATS) {
61
+ const english = platformNames(own(englishEntry, format))
62
+ const translated = platformNames(own(localeEntry, format))
63
+ for (const platform of PLATFORMS) {
64
+ if (isSymbol(english[platform])) delete translated[platform]
65
+ }
66
+ const merged = {...english, ...translated}
67
+ if (Object.keys(merged).length > 0) names[format] = merged
68
+ }
69
+ return names
70
+ }
71
+
72
+ /**
73
+ * Builds `data-names` for `keys` from the `keybinding_hint` locale scopes, as
74
+ * i18n-js returns them. Translator data is untrusted: only non-blank strings in
75
+ * the expected place are used, and symbols such as ⌘ cannot be replaced.
76
+ */
77
+ export function buildKeyNames(keys: string, localeScope: unknown, englishScope: unknown): KeyNamesData {
78
+ const englishKeys = own(englishScope, 'keys')
79
+ const index = tokenIndex(englishKeys)
80
+ const result: KeyNamesData = {
81
+ then: nonBlank(own(localeScope, 'then')) ?? nonBlank(own(englishScope, 'then')),
82
+ keys: {},
83
+ }
84
+ for (const token of tokens(keys)) {
85
+ const entry = index.get(token)
86
+ if (entry === undefined) continue
87
+ // defineProperty, not assignment: a `__proto__` token must stay an own key.
88
+ Object.defineProperty(result.keys, token, {
89
+ value: entryNames(own(englishKeys, entry), own(own(localeScope, 'keys'), entry)),
90
+ enumerable: true,
91
+ writable: true,
92
+ configurable: true,
93
+ })
94
+ }
95
+ return result
96
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The platform categories that affect how keyboard shortcut keys are displayed.
3
+ *
4
+ * - `apple`: Apple platforms (macOS and iOS/iPadOS), which use the Command and Option keys.
5
+ * - `windows`: Windows, which uses the Windows (Meta) key.
6
+ * - `other`: Any other platform (e.g. Linux, Android), where the Meta key does not map to a
7
+ * consistent label.
8
+ */
9
+ export type Platform = 'apple' | 'windows' | 'other';
10
+ export declare const detectPlatform: () => Platform;
@@ -0,0 +1,24 @@
1
+ // Mirrors primer-react `KeybindingHint/platform.ts`. The React-only parts are
2
+ // replaced: `getSnapshot` is exported as `detectPlatform`, and the element's
3
+ // `data-platform` attribute takes the place of `PlatformOverrideProvider`.
4
+ import { isMacOS as ssrUnsafeIsMacOS } from '@primer/behaviors/utils';
5
+ /** SSR-unsafe detection of iOS/iPadOS (in addition to macOS, which is detected separately). */
6
+ const ssrUnsafeIsIOS = () => {
7
+ if (typeof navigator === 'undefined')
8
+ return false;
9
+ return /iphone|ipad|ipod/i.test(navigator.platform) || /iphone|ipad|ipod/i.test(navigator.userAgent);
10
+ };
11
+ /** SSR-unsafe detection of Windows. */
12
+ const ssrUnsafeIsWindows = () => {
13
+ if (typeof navigator === 'undefined')
14
+ return false;
15
+ return /^win/i.test(navigator.platform);
16
+ };
17
+ const getSnapshot = () => {
18
+ if (ssrUnsafeIsMacOS() || ssrUnsafeIsIOS())
19
+ return 'apple';
20
+ if (ssrUnsafeIsWindows())
21
+ return 'windows';
22
+ return 'other';
23
+ };
24
+ export const detectPlatform = getSnapshot;
@@ -0,0 +1,34 @@
1
+ // Mirrors primer-react `KeybindingHint/platform.ts`. The React-only parts are
2
+ // replaced: `getSnapshot` is exported as `detectPlatform`, and the element's
3
+ // `data-platform` attribute takes the place of `PlatformOverrideProvider`.
4
+ import {isMacOS as ssrUnsafeIsMacOS} from '@primer/behaviors/utils'
5
+
6
+ /**
7
+ * The platform categories that affect how keyboard shortcut keys are displayed.
8
+ *
9
+ * - `apple`: Apple platforms (macOS and iOS/iPadOS), which use the Command and Option keys.
10
+ * - `windows`: Windows, which uses the Windows (Meta) key.
11
+ * - `other`: Any other platform (e.g. Linux, Android), where the Meta key does not map to a
12
+ * consistent label.
13
+ */
14
+ export type Platform = 'apple' | 'windows' | 'other'
15
+
16
+ /** SSR-unsafe detection of iOS/iPadOS (in addition to macOS, which is detected separately). */
17
+ const ssrUnsafeIsIOS = () => {
18
+ if (typeof navigator === 'undefined') return false
19
+ return /iphone|ipad|ipod/i.test(navigator.platform) || /iphone|ipad|ipod/i.test(navigator.userAgent)
20
+ }
21
+
22
+ /** SSR-unsafe detection of Windows. */
23
+ const ssrUnsafeIsWindows = () => {
24
+ if (typeof navigator === 'undefined') return false
25
+ return /^win/i.test(navigator.platform)
26
+ }
27
+
28
+ const getSnapshot = (): Platform => {
29
+ if (ssrUnsafeIsMacOS() || ssrUnsafeIsIOS()) return 'apple'
30
+ if (ssrUnsafeIsWindows()) return 'windows'
31
+ return 'other'
32
+ }
33
+
34
+ export const detectPlatform = getSnapshot
@@ -0,0 +1,2 @@
1
+ import type { Platform } from './platform';
2
+ export declare function isPlatform(value: string): value is Platform;
@@ -0,0 +1,4 @@
1
+ const PLATFORMS = ['apple', 'windows', 'other'];
2
+ export function isPlatform(value) {
3
+ return PLATFORMS.includes(value);
4
+ }
@@ -0,0 +1,7 @@
1
+ import type {Platform} from './platform'
2
+
3
+ const PLATFORMS: readonly string[] = ['apple', 'windows', 'other']
4
+
5
+ export function isPlatform(value: string): value is Platform {
6
+ return PLATFORMS.includes(value)
7
+ }
@@ -0,0 +1 @@
1
+ keybinding-hint{display:contents}.KeybindingHint-kbd{position:relative;padding:0;overflow:visible;font-family:inherit;font-size:inherit;line-height:unset;color:inherit;white-space:nowrap;vertical-align:initial;background:none;border:none;box-shadow:none}.KeybindingHint-chord{display:inline-flex;justify-content:center;min-width:var(--base-size-20);padding:var(--base-size-4);overflow:hidden;font-size:var(--text-body-size-small);font-weight:var(--base-text-weight-normal);line-height:10px;color:var(--fgColor-muted);vertical-align:initial;background:var(--bgColor-transparent);border:var(--borderWidth-thin) solid var(--borderColor-default);border-radius:var(--borderRadius-medium);box-shadow:none;gap:.5ch;transition:80ms ease-in;transition-property:color,background-color,border-color}.KeybindingHint-chord--emphasis{color:var(--fgColor-onEmphasis);background:var(--counter-bgColor-emphasis);border-color:#0000;border-radius:var(--borderRadius-default);min-width:auto}.KeybindingHint-chord--small{min-width:var(--base-size-16);padding:var(--base-size-2);font-size:11px;line-height:var(--base-size-8);border-radius:var(--borderRadius-small)}
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "open_project/keybinding_hint",
3
+ "selectors": [
4
+ "keybinding-hint",
5
+ ".KeybindingHint-kbd",
6
+ ".KeybindingHint-chord",
7
+ ".KeybindingHint-chord--emphasis",
8
+ ".KeybindingHint-chord--small"
9
+ ]
10
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["keybinding_hint.pcss"],"names":[],"mappings":"AAIA,gBACE,gBACF,CAGA,oBACE,iBAAkB,CAClB,SAAU,CACV,gBAAiB,CACjB,mBAAoB,CACpB,iBAAkB,CAElB,iBAAkB,CAClB,aAAc,CACd,kBAAmB,CACnB,sBAAwB,CACxB,eAAgB,CAChB,WAAY,CACZ,eACF,CAGA,sBACE,mBAAoB,CACpB,sBAAuB,CACvB,6BAA8B,CAC9B,0BAA2B,CAC3B,eAAgB,CAChB,qCAAsC,CACtC,0CAA2C,CAE3C,gBAAiB,CACjB,0BAA2B,CAC3B,sBAAwB,CACxB,qCAAsC,CACtC,+DAAgE,CAChE,wCAAyC,CACzC,eAAgB,CAChB,QAAU,CACV,uBAAwB,CACxB,uDACF,CAEA,gCACE,+BAAgC,CAChC,0CAA2C,CAC3C,kBAAyB,CAEzB,yCAA0C,CAC1C,cACF,CAEA,6BACE,6BAA8B,CAC9B,0BAA2B,CAE3B,cAAe,CAEf,8BAA+B,CAC/B,uCACF","file":"keybinding_hint.css","sourcesContent":["/* KeybindingHint (port of Primer React KeybindingHint at 6794582843) */\n\n/* The host carries no semantics; let the <kbd> be the flex/grid item, as in React. */\n/* stylelint-disable-next-line selector-max-type */\nkeybinding-hint {\n display: contents;\n}\n\n/* Reset user-agent <kbd> styling; matches KeybindingHint.module.css */\n.KeybindingHint-kbd {\n position: relative;\n padding: 0;\n overflow: visible;\n font-family: inherit;\n font-size: inherit;\n /* stylelint-disable-next-line primer/typography */\n line-height: unset;\n color: inherit;\n white-space: nowrap;\n vertical-align: baseline;\n background: none;\n border: none;\n box-shadow: none;\n}\n\n/* One bordered box per chord; matches Chord.module.css */\n.KeybindingHint-chord {\n display: inline-flex;\n justify-content: center;\n min-width: var(--base-size-20);\n padding: var(--base-size-4);\n overflow: hidden;\n font-size: var(--text-body-size-small);\n font-weight: var(--base-text-weight-normal);\n /* stylelint-disable-next-line primer/typography */\n line-height: 10px;\n color: var(--fgColor-muted);\n vertical-align: baseline;\n background: var(--bgColor-transparent);\n border: var(--borderWidth-thin) solid var(--borderColor-default);\n border-radius: var(--borderRadius-medium);\n box-shadow: none;\n gap: 0.5ch;\n transition: 80ms ease-in;\n transition-property: color, background-color, border-color;\n}\n\n.KeybindingHint-chord--emphasis {\n color: var(--fgColor-onEmphasis);\n background: var(--counter-bgColor-emphasis);\n border-color: transparent;\n /* React applies the medium radius and the 20px floor only to the default scheme */\n border-radius: var(--borderRadius-default);\n min-width: auto;\n}\n\n.KeybindingHint-chord--small {\n min-width: var(--base-size-16);\n padding: var(--base-size-2);\n /* stylelint-disable-next-line primer/typography */\n font-size: 11px;\n /* stylelint-disable-next-line primer/typography */\n line-height: var(--base-size-8);\n border-radius: var(--borderRadius-small);\n}\n"]}