Skip to content

Commit

Permalink
Fixed multiple editor related issues:
Browse files Browse the repository at this point in the history
1. Replace option in query tool edit menu is not working on non-Mac OS. #7262
2. Format SQL shortcut and multiline selection are not working. #7268
3. "Use Spaces?" Preference of Editor is not working. #7269
  • Loading branch information
adityatoshniwal committed Mar 11, 2024
1 parent 9c9766c commit 794c7cb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
3 changes: 3 additions & 0 deletions docs/en_US/release_notes_8_5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ Bug fixes
*********

| `Issue #7229 <https://github.com/pgadmin-org/pgadmin4/issues/7229>`_ - Fix an issue in table dialog where changing column name was not syncing table constraints appropriately.
| `Issue #7262 <https://github.com/pgadmin-org/pgadmin4/issues/7262>`_ - Fix an issue in editor where replace option in query tool edit menu is not working on non-Mac OS.
| `Issue #7268 <https://github.com/pgadmin-org/pgadmin4/issues/7268>`_ - Fix an issue in editor where Format SQL shortcut and multiline selection are not working.
| `Issue #7269 <https://github.com/pgadmin-org/pgadmin4/issues/7269>`_ - Fix an issue in editor where "Use Spaces?" Preference of Editor is not working.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import {
highlightSpecialChars,
drawSelection,
dropCursor,
rectangularSelection,
crosshairCursor,
highlightActiveLine,
EditorView,
keymap,
} from '@codemirror/view';
import { EditorState, Compartment } from '@codemirror/state';
import { history, defaultKeymap, historyKeymap, indentLess, insertTab } from '@codemirror/commands';
import { highlightSelectionMatches } from '@codemirror/search';
import { history, defaultKeymap, historyKeymap, indentLess, indentMore } from '@codemirror/commands';
import { closeBrackets, autocompletion, closeBracketsKeymap, completionKeymap, acceptCompletion } from '@codemirror/autocomplete';
import {
foldGutter,
Expand Down Expand Up @@ -91,25 +92,30 @@ function handlePaste(e) {
checkTrojanSource(copiedText, true);
}


function insertTabWithUnit({ state, dispatch }) {
if (state.selection.ranges.some(r => !r.empty))
return indentMore({ state, dispatch });
dispatch(state.update(state.replaceSelection(state.facet(indentUnit)), { scrollIntoView: true, userEvent: 'input' }));
return true;
}

/* React wrapper for CodeMirror */
const defaultExtensions = [
highlightSpecialChars(),
drawSelection(),
rectangularSelection(),
dropCursor(),
crosshairCursor(),
EditorState.allowMultipleSelections.of(true),
indentOnInput(),
syntaxHighlighting,
highlightSelectionMatches(),
keymap.of([defaultKeymap, closeBracketsKeymap, historyKeymap, foldKeymap, completionKeymap].flat()),
keymap.of([{
key: 'Tab',
preventDefault: true,
run: insertTab,
},
{
key: 'Shift-Tab',
preventDefault: true,
run: indentLess,
run: insertTabWithUnit,
shift: indentLess,
},{
key: 'Tab',
run: acceptCompletion,
Expand Down Expand Up @@ -170,7 +176,6 @@ export default function Editor({
extensions: [
...finalExtns,
configurables.current.of([]),
keymap.of(customKeyMap??[]),
editableConfig.current.of([
EditorView.editable.of(!disabled),
EditorState.readOnly.of(readonly),
Expand Down Expand Up @@ -204,6 +209,7 @@ export default function Editor({
}),
breakpoint ? breakpointGutter : [],
showActiveLine ? highlightActiveLine() : activeLineExtn(),
keymap.of(customKeyMap??[]),
],
});

Expand Down
10 changes: 6 additions & 4 deletions web/pgadmin/static/js/components/ReactCodeMirror/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ CopyButton.propTypes = {
};


export default function CodeMirror({className, currEditor, showCopyBtn=false, ...props}) {
export default function CodeMirror({className, currEditor, showCopyBtn=false, customKeyMap=[], ...props}) {
const classes = useStyles();
const editor = useRef();
const [[showFind, isReplace], setShowFind] = useState([false, false]);
const [showGoto, setShowGoto] = useState(false);
const [showCopy, setShowCopy] = useState(false);

const editMenuKeyMap = useMemo(()=>[{
const finalCustomKeyMap = useMemo(()=>[{
key: 'Mod-f', run: (_view, e) => {
e.preventDefault();
e.stopPropagation();
Expand All @@ -89,7 +89,8 @@ export default function CodeMirror({className, currEditor, showCopyBtn=false, ..
e.stopPropagation();
setShowGoto(true);
},
}], []);
},
...customKeyMap], []);

const closeFind = () => {
setShowFind([false, false]);
Expand All @@ -112,7 +113,7 @@ export default function CodeMirror({className, currEditor, showCopyBtn=false, ..

return (
<div className={clsx(className, classes.root)} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} >
<Editor currEditor={currEditorWrap} customKeyMap={editMenuKeyMap} {...props} />
<Editor currEditor={currEditorWrap} customKeyMap={finalCustomKeyMap} {...props} />
{showCopy && <CopyButton editor={editor.current} />}
<FindDialog editor={editor.current} show={showFind} replace={isReplace} onClose={closeFind} />
<GotoDialog editor={editor.current} show={showGoto} onClose={closeGoto} />
Expand All @@ -124,4 +125,5 @@ CodeMirror.propTypes = {
currEditor: PropTypes.func,
className: CustomPropTypes.className,
showCopyBtn: PropTypes.bool,
customKeyMap: PropTypes.array,
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { InputSelectNonSearch } from '../../../../../../static/js/components/For
import PropTypes from 'prop-types';
import CustomPropTypes from '../../../../../../static/js/custom_prop_types';
import ConfirmTransactionContent from '../dialogs/ConfirmTransactionContent';
import { isMac } from '../../../../../../static/js/keyboard_shortcuts';
import { LayoutDocker } from '../../../../../../static/js/helpers/Layout';
import CloseRunningDialog from '../dialogs/CloseRunningDialog';

Expand Down Expand Up @@ -63,8 +62,8 @@ const FIXED_PREF = {
replace: {
'control': true,
ctrl_is_meta: true,
'shift': isMac() ? false : true,
'alt': isMac() ? true : false,
'shift': false,
'alt': true,
'key': {
'key_code': 70,
'char': 'F',
Expand Down Expand Up @@ -120,7 +119,8 @@ const FIXED_PREF = {
},
format_sql: {
'control': true,
'shift': true,
ctrl_is_meta: true,
'shift': false,
'alt': false,
'key': {
'key_code': 75,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export default function Query() {
eventBus.registerListener(QUERY_TOOL_EVENTS.EDITOR_FIND_REPLACE, (replace=false)=>{
editor.current?.focus();
let key = {
keyCode: 70, metaKey: false, ctrlKey: true, shiftKey: replace, altKey: false,
keyCode: 70, metaKey: false, ctrlKey: true, shiftKey: false, altKey: replace,
};
if(isMac()) {
key.metaKey = true;
Expand Down Expand Up @@ -409,5 +409,14 @@ export default function Query() {
onCursorActivity={cursorActivity}
onChange={change}
autocomplete={true}
customKeyMap={[
{
key: 'Mod-k', run: (_view, e) => {
e.preventDefault();
e.stopPropagation();
eventBus.fireEvent(QUERY_TOOL_EVENTS.TRIGGER_FORMAT_SQL);
},
}
]}
/>;
}

0 comments on commit 794c7cb

Please sign in to comment.