-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: make CodeEditor toggle-comment shortcut configurable #7896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| */ | ||
|
|
||
| import React, { createRef } from 'react'; | ||
| import { connect } from 'react-redux'; | ||
| import { isEqual, escapeRegExp } from 'lodash'; | ||
| import { defineCodeMirrorBrunoVariablesMode } from 'utils/common/codemirror'; | ||
| import { setupAutoComplete, showRootHints } from 'utils/codemirror/autocomplete'; | ||
|
|
@@ -17,14 +18,15 @@ import { getAllVariables } from 'utils/collections'; | |
| import { setupLinkAware } from 'utils/codemirror/linkAware'; | ||
| import { setupLintErrorTooltip } from 'utils/codemirror/lint-errors'; | ||
| import CodeMirrorSearch from 'components/CodeMirrorSearch/index'; | ||
| import { getCodeMirrorKeyForAction } from 'providers/Hotkeys/keyMappings'; | ||
|
|
||
| const CodeMirror = require('codemirror'); | ||
| window.jsonlint = jsonlint; | ||
| window.JSHINT = JSHINT; | ||
|
|
||
| const TAB_SIZE = 2; | ||
|
|
||
| export default class CodeEditor extends React.Component { | ||
| class CodeEditor extends React.Component { | ||
| constructor(props) { | ||
| super(props); | ||
|
|
||
|
|
@@ -48,6 +50,14 @@ export default class CodeEditor extends React.Component { | |
| }; | ||
| } | ||
|
|
||
| toggleComment = () => { | ||
| if (['application/ld+json', 'application/json'].includes(this.props.mode)) { | ||
| this.editor.toggleComment({ lineComment: '//', blockComment: '/*' }); | ||
| } else { | ||
| this.editor.toggleComment(); | ||
| } | ||
| }; | ||
|
|
||
| componentDidMount() { | ||
| const variables = getAllVariables(this.props.collection, this.props.item); | ||
| const runShortcut = () => { | ||
|
|
@@ -111,20 +121,7 @@ export default class CodeEditor extends React.Component { | |
| 'Cmd-Y': 'foldAll', | ||
| 'Ctrl-I': 'unfoldAll', | ||
| 'Cmd-I': 'unfoldAll', | ||
| 'Ctrl-/': () => { | ||
| if (['application/ld+json', 'application/json'].includes(this.props.mode)) { | ||
| this.editor.toggleComment({ lineComment: '//', blockComment: '/*' }); | ||
| } else { | ||
| this.editor.toggleComment(); | ||
| } | ||
| }, | ||
| 'Cmd-/': () => { | ||
| if (['application/ld+json', 'application/json'].includes(this.props.mode)) { | ||
| this.editor.toggleComment({ lineComment: '//', blockComment: '/*' }); | ||
| } else { | ||
| this.editor.toggleComment(); | ||
| } | ||
| }, | ||
| ...(this.props.commentToggleKey ? { [this.props.commentToggleKey]: this.toggleComment } : {}), | ||
| 'Esc': () => { | ||
| if (this.state.searchBarVisible) { | ||
| this.setState({ searchBarVisible: false }); | ||
|
|
@@ -280,6 +277,13 @@ export default class CodeEditor extends React.Component { | |
| this.editor.setOption('readOnly', this.props.readOnly); | ||
| } | ||
|
|
||
| if (this.props.commentToggleKey !== prevProps.commentToggleKey && this.editor) { | ||
| const extraKeys = { ...(this.editor.getOption('extraKeys') || {}) }; | ||
| if (prevProps.commentToggleKey) delete extraKeys[prevProps.commentToggleKey]; | ||
| if (this.props.commentToggleKey) extraKeys[this.props.commentToggleKey] = this.toggleComment; | ||
| this.editor.setOption('extraKeys', extraKeys); | ||
| } | ||
|
|
||
| this.ignoreChangeEvent = false; | ||
| } | ||
|
|
||
|
|
@@ -355,3 +359,9 @@ export default class CodeEditor extends React.Component { | |
| } | ||
| }; | ||
| } | ||
|
|
||
| const mapStateToProps = (state) => ({ | ||
| commentToggleKey: getCodeMirrorKeyForAction('toggleComment', state.app.preferences?.keyBindings) | ||
| }); | ||
|
|
||
| export default connect(mapStateToProps, null, null, { forwardRef: true })(CodeEditor); | ||
|
Comment on lines
+363
to
+367
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -69,6 +69,12 @@ export const KEY_BINDING_SECTIONS = [ | |||||||
| openTerminal: { mac: 'command+bind+t', windows: 'ctrl+bind+t', name: 'Open in Terminal' } // D | ||||||||
| } | ||||||||
| }, | ||||||||
| { | ||||||||
| heading: 'Editor', | ||||||||
| bindings: { | ||||||||
| toggleComment: { mac: 'command+bind+/', windows: 'ctrl+bind+/', name: 'Toggle Comment' } | ||||||||
| } | ||||||||
| }, | ||||||||
| { | ||||||||
| heading: 'Others', | ||||||||
| bindings: { | ||||||||
|
|
@@ -171,3 +177,30 @@ export const getKeyBindingsForActionAllOS = (action, userKeyBindings) => { | |||||||
| // console.log('[keyMappings] getKeyBindingsForActionAllOS:', action, '->', combos); | ||||||||
| return combos.length > 0 ? combos : null; | ||||||||
| }; | ||||||||
|
|
||||||||
| const CM_MODIFIERS = { command: 'Cmd', ctrl: 'Ctrl', alt: 'Alt', shift: 'Shift' }; | ||||||||
| const CM_MODIFIER_ORDER = ['Shift', 'Cmd', 'Ctrl', 'Alt']; | ||||||||
| const CM_SPECIAL_KEYS = { | ||||||||
| enter: 'Enter', esc: 'Esc', escape: 'Esc', space: 'Space', tab: 'Tab', | ||||||||
| backspace: 'Backspace', delete: 'Delete', insert: 'Insert', | ||||||||
| arrowup: 'Up', arrowdown: 'Down', arrowleft: 'Left', arrowright: 'Right', | ||||||||
| pageup: 'PageUp', pagedown: 'PageDown', home: 'Home', end: 'End' | ||||||||
| }; | ||||||||
|
|
||||||||
| export const getCodeMirrorKeyForAction = (action, userKeyBindings) => { | ||||||||
| const binding = getMergedKeyBindings(userKeyBindings)[action]; | ||||||||
| if (!binding) return null; | ||||||||
| const isMac = navigator.platform.toLowerCase().includes('mac'); | ||||||||
|
||||||||
| const isMac = navigator.platform.toLowerCase().includes('mac'); | |
| const platform = typeof navigator !== 'undefined' && typeof navigator.platform === 'string' ? navigator.platform : ''; | |
| const isMac = platform.toLowerCase().includes('mac'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The app has a
preferences.keybindingsEnabledtoggle (used byHotkeysProvider/useKeybindingto disable shortcuts), butCodeEditorwill still bind the comment-toggle shortcut even when keybindings are disabled. SincetoggleCommentis now part ofKEY_BINDING_SECTIONS, consider also selectingkeybindingsEnabledhere and returningnullforcommentToggleKeywhen disabled so the preference toggle consistently disables this shortcut too.