Refactored handlers into reusable functions for readability. (#2744)

This commit is contained in:
Natalie Carey 2024-08-02 07:40:04 +01:00 committed by GitHub
parent 8f920a90c7
commit 7c33fd413e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -26,6 +26,18 @@ class SingleLineEditor extends Component {
/** @type {import("codemirror").Editor} */ /** @type {import("codemirror").Editor} */
const variables = getAllVariables(this.props.collection, this.props.item); const variables = getAllVariables(this.props.collection, this.props.item);
const runHandler = () => {
if (this.props.onRun) {
this.props.onRun();
}
};
const saveHandler = () => {
if (this.props.onSave) {
this.props.onSave();
}
};
const noopHandler = () => {};
this.editor = CodeMirror(this.editorRef.current, { this.editor = CodeMirror(this.editorRef.current, {
lineWrapping: false, lineWrapping: false,
lineNumbers: false, lineNumbers: false,
@ -37,21 +49,9 @@ class SingleLineEditor extends Component {
scrollbarStyle: null, scrollbarStyle: null,
tabindex: 0, tabindex: 0,
extraKeys: { extraKeys: {
Enter: () => { Enter: runHandler,
if (this.props.onRun) { 'Ctrl-Enter': runHandler,
this.props.onRun(); 'Cmd-Enter': runHandler,
}
},
'Ctrl-Enter': () => {
if (this.props.onRun) {
this.props.onRun();
}
},
'Cmd-Enter': () => {
if (this.props.onRun) {
this.props.onRun();
}
},
'Alt-Enter': () => { 'Alt-Enter': () => {
if (this.props.allowNewlines) { if (this.props.allowNewlines) {
this.editor.setValue(this.editor.getValue() + '\n'); this.editor.setValue(this.editor.getValue() + '\n');
@ -60,23 +60,11 @@ class SingleLineEditor extends Component {
this.props.onRun(); this.props.onRun();
} }
}, },
'Shift-Enter': () => { 'Shift-Enter': runHandler,
if (this.props.onRun) { 'Cmd-S': saveHandler,
this.props.onRun(); 'Ctrl-S': saveHandler,
} 'Cmd-F': noopHandler,
}, 'Ctrl-F': noopHandler,
'Cmd-S': () => {
if (this.props.onSave) {
this.props.onSave();
}
},
'Ctrl-S': () => {
if (this.props.onSave) {
this.props.onSave();
}
},
'Cmd-F': () => {},
'Ctrl-F': () => {},
// Tabbing disabled to make tabindex work // Tabbing disabled to make tabindex work
Tab: false, Tab: false,
'Shift-Tab': false 'Shift-Tab': false