Skip to content
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

fix(CodeEditor) support Mac and PC shortcuts in example #10472

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
import React from 'react';
import { CodeEditor, Language } from '@patternfly/react-code-editor';
import { Grid, GridItem, Chip } from '@patternfly/react-core';
import { Grid, GridItem, Chip, Radio } from '@patternfly/react-core';

export const CodeEditorShortcutMainHeader: React.FunctionComponent = () => {
type ShortcutMode = 'PC' | 'Mac';

const [currentShortcutMode, setCurrentShortcutMode] = React.useState<ShortcutMode>('PC');

const onEditorDidMount = (editor, monaco) => {
editor.layout();
editor.focus();
monaco.editor.getModels()[0].updateOptions({ tabSize: 5 });
};

const onChange = (value) => {
const onChange = (value: string) => {
// eslint-disable-next-line no-console
console.log(value);
};

const onShortcutModeChange = (event: React.FormEvent<HTMLInputElement>, checked: boolean) => {
if (checked) {
const newMode = event.currentTarget.value as ShortcutMode;
setCurrentShortcutMode(newMode);
}
};

const shortcuts = [
{
keys: ['Opt', 'F1'],
PC: ['Alt', 'F1'],
Mac: ['Opt', 'F1'],
description: 'Accessibility helps'
},
{
keys: ['F1'],
PC: ['F1'],
Mac: ['F1'],
description: 'View all editor shortcuts'
},
{
keys: ['Ctrl', 'Space'],
PC: ['Ctrl', 'Space'],
Mac: ['Opt', 'Esc'],
description: 'Activate auto complete'
},
{
keys: ['Cmd', 'S'],
PC: ['Ctrl', 'S'],
Mac: ['Cmd', 'S'],
description: 'Save'
}
];
Expand All @@ -38,7 +53,7 @@ export const CodeEditorShortcutMainHeader: React.FunctionComponent = () => {
{shortcuts.map((shortcut, index) => (
<React.Fragment key={index}>
<GridItem style={{ textAlign: 'right', marginRight: '1em' }}>
{shortcut.keys
{shortcut[currentShortcutMode]
.map((key) => (
<Chip key={key} isReadOnly>
{key}
Expand All @@ -57,14 +72,32 @@ export const CodeEditorShortcutMainHeader: React.FunctionComponent = () => {
};

return (
<CodeEditor
shortcutsPopoverProps={shortcutsPopoverProps}
isLanguageLabelVisible
code="Some example content"
onChange={onChange}
language={Language.javascript}
onEditorDidMount={onEditorDidMount}
height="400px"
/>
<>
<Radio
checked={currentShortcutMode === 'PC'}
onChange={onShortcutModeChange}
label="PC shortcuts"
name="shortcut-radio"
id="pc-shortcuts"
value="PC"
/>
<Radio
checked={currentShortcutMode === 'Mac'}
onChange={onShortcutModeChange}
label="Mac shortcuts"
name="shortcut-radio"
id="mac-shortcuts"
value="Mac"
/>
<CodeEditor
shortcutsPopoverProps={shortcutsPopoverProps}
isLanguageLabelVisible
code="Some example content"
onChange={onChange}
language={Language.javascript}
onEditorDidMount={onEditorDidMount}
height="400px"
/>
</>
);
};
Loading