Skip to content

Commit fb576ed

Browse files
committed
Introduce helper function for outputing keyboard shortcuts
1 parent 8d68000 commit fb576ed

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

php/admin-menus/class-edit-menu.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,4 +734,47 @@ public function render_submit_buttons( $snippet, $size = '', $extra_actions = tr
734734
}
735735
}
736736
}
737+
738+
/**
739+
* Render a keyboard shortcut as HTML.
740+
*
741+
* @param array|string $modifiers Modifier keys. Can be 'Cmd', 'Ctrl', 'Shift', 'Option', 'Alt'.
742+
* @param string $key Keyboard key.
743+
*
744+
* @return void
745+
*/
746+
protected function render_keyboard_shortcut( $modifiers, $key ) {
747+
static $keys = null;
748+
749+
if ( is_null( $keys ) ) {
750+
$keys = array(
751+
'Cmd' => _x( 'Cmd', 'keyboard key', 'code-snippets' ),
752+
'Ctrl' => _x( 'Ctrl', 'keyboard key', 'code-snippets' ),
753+
'Shift' => _x( 'Shift', 'keyboard key', 'code-snippets' ),
754+
'Option' => _x( 'Option', 'keyboard key', 'code-snippets' ),
755+
'Alt' => _x( 'Alt', 'keyboard key', 'code-snippets' ),
756+
'F' => _x( 'F', 'keyboard key', 'code-snippets' ),
757+
'G' => _x( 'G', 'keyboard key', 'code-snippets' ),
758+
'R' => _x( 'R', 'keyboard key', 'code-snippets' ),
759+
'S' => _x( 'S', 'keyboard key', 'code-snippets' ),
760+
);
761+
}
762+
763+
if ( ! is_array( $modifiers ) ) {
764+
$modifiers = array( $modifiers );
765+
}
766+
767+
foreach ( $modifiers as $modifier ) {
768+
if ( 'Ctrl' === $modifier || 'Cmd' === $modifier ) {
769+
echo '<kbd class="pc-key">', esc_html( $keys['Ctrl'] ), '</kbd>';
770+
echo '<kbd class="mac-key">', esc_html( $keys['Cmd'] ), '</kbd>&hyphen;';
771+
} elseif ( 'Option' === $modifier ) {
772+
echo '<span class="mac-key"><kbd class="mac-key">', esc_html( $keys['Option'] ), '</kbd>&hyphen;</span>';
773+
} else {
774+
echo '<kbd>', esc_html( $keys[ $modifier ] ), '</kbd>&hyphen;';
775+
}
776+
}
777+
778+
echo '<kbd>', esc_html( $keys[ $key ] ), '</kbd>';
779+
}
737780
}

php/views/edit.php

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -103,76 +103,35 @@ class="<?php echo implode( ' ', $classes ); ?>">
103103
echo esc_attr( code_snippets_get_setting( 'editor', 'theme' ) ); ?>"><?php
104104
echo esc_html_x( '?', 'help tooltip', 'code-snippets' ); ?></div>
105105

106-
<?php
107-
108-
$keys = array(
109-
'Cmd' => _x( 'Cmd', 'keyboard key', 'code-snippets' ),
110-
'Ctrl' => _x( 'Ctrl', 'keyboard key', 'code-snippets' ),
111-
'Shift' => _x( 'Shift', 'keyboard key', 'code-snippets' ),
112-
'Option' => _x( 'Option', 'keyboard key', 'code-snippets' ),
113-
'Alt' => _x( 'Alt', 'keyboard key', 'code-snippets' ),
114-
'F' => _x( 'F', 'keyboard key', 'code-snippets' ),
115-
'G' => _x( 'G', 'keyboard key', 'code-snippets' ),
116-
'R' => _x( 'R', 'keyboard key', 'code-snippets' ),
117-
'S' => _x( 'S', 'keyboard key', 'code-snippets' ),
118-
);
119-
120-
?>
121-
122106
<div class="editor-help-text">
123107
<table>
124108
<tr>
125109
<td><?php esc_html_e( 'Save changes', 'code-snippets' ); ?></td>
126-
<td>
127-
<kbd class="pc-key"><?php echo esc_html( $keys['Ctrl'] ); ?></kbd><kbd
128-
class="mac-key"><?php echo esc_html( $keys['Cmd'] ); ?></kbd>&hyphen;<kbd><?php echo esc_html( $keys['S'] ); ?></kbd>
129-
</td>
110+
<td><?php $this->render_keyboard_shortcut( 'Cmd', 'S' ); ?></td>
130111
</tr>
131112
<tr>
132113
<td><?php esc_html_e( 'Begin searching', 'code-snippets' ); ?></td>
133-
<td>
134-
<kbd class="pc-key"><?php echo esc_html( $keys['Ctrl'] ); ?></kbd><kbd
135-
class="mac-key"><?php echo esc_html( $keys['Cmd'] ); ?></kbd>&hyphen;<kbd><?php echo esc_html( $keys['F'] ); ?></kbd>
136-
</td>
114+
<td><?php $this->render_keyboard_shortcut( 'Cmd', 'F' ); ?></td>
137115
</tr>
138116
<tr>
139117
<td><?php esc_html_e( 'Find next', 'code-snippets' ); ?></td>
140-
<td>
141-
<kbd class="pc-key"><?php echo esc_html( $keys['Ctrl'] ); ?></kbd><kbd
142-
class="mac-key"><?php echo esc_html( $keys['Cmd'] ); ?></kbd>&hyphen;<kbd><?php echo esc_html( $keys['G'] ); ?></kbd>
143-
</td>
118+
<td><?php $this->render_keyboard_shortcut( 'Cmd', 'G' ); ?></td>
144119
</tr>
145120
<tr>
146121
<td><?php esc_html_e( 'Find previous', 'code-snippets' ); ?></td>
147-
<td>
148-
<kbd><?php echo esc_html( $keys['Shift'] ); ?></kbd>-<kbd
149-
class="pc-key"><?php echo esc_html( $keys['Ctrl'] ); ?></kbd><kbd
150-
class="mac-key"><?php echo esc_html( $keys['Cmd'] ); ?></kbd>&hyphen;<kbd><?php echo esc_html( $keys['G'] ); ?></kbd>
151-
</td>
122+
<td><?php $this->render_keyboard_shortcut( array( 'Shift', 'Cmd' ), 'G' ); ?></td>
152123
</tr>
153124
<tr>
154125
<td><?php esc_html_e( 'Replace', 'code-snippets' ); ?></td>
155-
<td>
156-
<kbd><?php echo esc_html( $keys['Shift'] ); ?></kbd>&hyphen;<kbd
157-
class="pc-key"><?php echo esc_html( $keys['Ctrl'] ); ?></kbd><kbd
158-
class="mac-key"><?php echo esc_html( $keys['Cmd'] ); ?></kbd>&hyphen;<kbd><?php echo esc_html( $keys['F'] ); ?></kbd>
159-
</td>
126+
<td><?php $this->render_keyboard_shortcut( array( 'Shift', 'Cmd' ), 'F' ); ?></td>
160127
</tr>
161128
<tr>
162129
<td><?php esc_html_e( 'Replace all', 'code-snippets' ); ?></td>
163-
<td>
164-
<kbd><?php echo esc_html( $keys['Shift'] ); ?></kbd>&hyphen;<kbd
165-
class="pc-key"><?php echo esc_html( $keys['Ctrl'] ); ?></kbd><kbd
166-
class="mac-key"><?php echo esc_html( $keys['Cmd'] ); ?></kbd><span
167-
class="mac-key">&hyphen;</span><kbd
168-
class="mac-key"><?php echo esc_html( $keys['Option'] ); ?></kbd>&hyphen;<kbd><?php echo esc_html( $keys['R'] ); ?></kbd>
169-
</td>
130+
<td><?php $this->render_keyboard_shortcut( array( 'Shift', 'Cmd', 'Option' ), 'R' ); ?></td>
170131
</tr>
171132
<tr>
172133
<td><?php esc_html_e( 'Persistent search', 'code-snippets' ); ?></td>
173-
<td>
174-
<kbd><?php echo esc_html( $keys['Alt'] ); ?></kbd>&hyphen;<kbd><?php echo esc_html( $keys['F'] ); ?></kbd>
175-
</td>
134+
<td><?php $this->render_keyboard_shortcut( 'Alt', 'F' ); ?></td>
176135
</tr>
177136
</table>
178137
</div>

0 commit comments

Comments
 (0)