| 1 |
import { __, _x } from '@wordpress/i18n' |
| 2 |
import classnames from 'classnames' |
| 3 |
import React from 'react' |
| 4 |
import { KEYBOARD_KEYS } from '../../../types/KeyboardShortcut' |
| 5 |
import { isMacOS } from '../../../utils/screen' |
| 6 |
import type { KeyboardKey, KeyboardShortcut } from '../../../types/KeyboardShortcut' |
| 7 |
|
| 8 |
const shortcuts: Record<string, KeyboardShortcut> = { |
| 9 |
saveChanges: { |
| 10 |
label: __('Save changes', 'code-snippets'), |
| 11 |
mod: 'Cmd', |
| 12 |
key: 'S' |
| 13 |
}, |
| 14 |
selectAll: { |
| 15 |
label: __('Select all', 'code-snippets'), |
| 16 |
mod: 'Cmd', |
| 17 |
key: 'A' |
| 18 |
}, |
| 19 |
beginSearch: { |
| 20 |
label: __('Begin searching', 'code-snippets'), |
| 21 |
mod: 'Cmd', |
| 22 |
key: 'F' |
| 23 |
}, |
| 24 |
findNext: { |
| 25 |
label: __('Find next', 'code-snippets'), |
| 26 |
mod: 'Cmd', |
| 27 |
key: 'G' |
| 28 |
}, |
| 29 |
findPrevious: { |
| 30 |
label: __('Find previous', 'code-snippets'), |
| 31 |
mod: ['Shift', 'Cmd'], |
| 32 |
key: 'G' |
| 33 |
}, |
| 34 |
replace: { |
| 35 |
label: __('Replace', 'code-snippets'), |
| 36 |
mod: ['Shift', 'Cmd'], |
| 37 |
key: 'F' |
| 38 |
}, |
| 39 |
replaceAll: { |
| 40 |
label: __('Replace all', 'code-snippets'), |
| 41 |
mod: ['Shift', 'Cmd', 'Option'], |
| 42 |
key: 'R' |
| 43 |
}, |
| 44 |
search: { |
| 45 |
label: __('Persistent search', 'code-snippets'), |
| 46 |
mod: 'Alt', |
| 47 |
key: 'F' |
| 48 |
}, |
| 49 |
toggleComment: { |
| 50 |
label: __('Toggle comment', 'code-snippets'), |
| 51 |
mod: 'Cmd', |
| 52 |
key: '/' |
| 53 |
}, |
| 54 |
swapLineUp: { |
| 55 |
label: __('Swap line up', 'code-snippets'), |
| 56 |
mod: 'Option', |
| 57 |
key: 'Up' |
| 58 |
}, |
| 59 |
swapLineDown: { |
| 60 |
label: __('Swap line down', 'code-snippets'), |
| 61 |
mod: 'Option', |
| 62 |
key: 'Down' |
| 63 |
}, |
| 64 |
autoIndent: { |
| 65 |
label: __('Auto-indent current line or selection', 'code-snippets'), |
| 66 |
mod: 'Shift', |
| 67 |
key: 'Tab' |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
const SEP = _x('-', 'keyboard shortcut separator', 'code-snippets') |
| 72 |
|
| 73 |
const ModifierKey: React.FC<{ modifier: KeyboardKey }> = ({ modifier }) => { |
| 74 |
switch (modifier) { |
| 75 |
case 'Ctrl': |
| 76 |
case 'Cmd': |
| 77 |
return ( |
| 78 |
<> |
| 79 |
<kbd className="pc-key">{KEYBOARD_KEYS.Ctrl}</kbd> |
| 80 |
<kbd className="mac-key">{KEYBOARD_KEYS.Cmd}</kbd> |
| 81 |
{SEP} |
| 82 |
</> |
| 83 |
) |
| 84 |
|
| 85 |
case 'Option': |
| 86 |
return ( |
| 87 |
<span className="mac-key"> |
| 88 |
<kbd className="mac-key">{KEYBOARD_KEYS.Option}</kbd>{SEP} |
| 89 |
</span> |
| 90 |
) |
| 91 |
|
| 92 |
default: |
| 93 |
return <><kbd>{KEYBOARD_KEYS[modifier]}</kbd>{SEP}</> |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
export interface CodeEditorShortcutsProps { |
| 98 |
editorTheme: string |
| 99 |
} |
| 100 |
|
| 101 |
export const CodeEditorShortcuts: React.FC<CodeEditorShortcutsProps> = ({ editorTheme }) => |
| 102 |
<div className="snippet-editor-help tooltip tooltip-inline tooltip-start"> |
| 103 |
<span className={`dashicons dashicons-editor-help cm-s-${editorTheme}`}></span> |
| 104 |
|
| 105 |
<div className={classnames('tooltip-content', { 'platform-mac': isMacOS() })}> |
| 106 |
<table> |
| 107 |
<tbody> |
| 108 |
{Object.entries(shortcuts).map(([name, { label, mod, key }]) => |
| 109 |
<tr key={name}> |
| 110 |
<td>{label}</td> |
| 111 |
<td> |
| 112 |
{(Array.isArray(mod) ? mod : [mod]).map(modifier => |
| 113 |
<span key={modifier}> |
| 114 |
<ModifierKey modifier={modifier} /> |
| 115 |
</span> |
| 116 |
)} |
| 117 |
<kbd>{KEYBOARD_KEYS[key]}</kbd> |
| 118 |
</td> |
| 119 |
</tr>)} |
| 120 |
</tbody> |
| 121 |
</table> |
| 122 |
</div> |
| 123 |
</div> |
| 124 |
|