Skip to content

Commit

Permalink
feat(editor, query-bar): on query history autocomplete move cursor to…
Browse files Browse the repository at this point in the history
… end of content, small ux updates (#6199)
  • Loading branch information
Anemy authored Sep 5, 2024
1 parent 69ce0b2 commit 7d60fbd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,16 @@ function getMatchingQueryHistoryItemsForInput({
const fieldStringSimplified =
simplifyQueryStringForAutocomplete(fieldString);

if (input === fieldStringSimplified) {
// Don't show an option if the user has typed the whole field.
return false;
}

if (fieldStringSimplified.startsWith(input)) {
// When the user is typing their first field, we can return early.
return true;
}

const inputIndex = inputToMatch.indexOf(fieldStringSimplified);
if (inputIndex !== -1) {
inputToMatch = inputToMatch.replace(fieldStringSimplified, '');
Expand Down
13 changes: 12 additions & 1 deletion packages/compass-editor/src/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
foldEffect,
} from '@codemirror/language';
import {
cursorDocEnd,
defaultKeymap,
history,
historyKeymap,
Expand Down Expand Up @@ -433,7 +434,7 @@ function getStylesForTheme(theme: CodemirrorThemeType) {
marginTop: 0,
paddingTop: 0,
fontSize: '12px',
maxHeight: '70vh',
maxHeight: `${spacing[1600] * 5}px`,
},
'& .cm-tooltip .completion-info p': {
margin: 0,
Expand Down Expand Up @@ -698,6 +699,7 @@ export type EditorRef = {
prettify: () => boolean;
applySnippet: (template: string) => boolean;
focus: () => boolean;
cursorDocEnd: () => boolean;
startCompletion: () => boolean;
readonly editorContents: string | null;
readonly editor: EditorView | null;
Expand Down Expand Up @@ -808,6 +810,12 @@ const BaseEditor = React.forwardRef<EditorRef, EditorProps>(function BaseEditor(
editorViewRef.current.focus();
return true;
},
cursorDocEnd() {
if (!editorViewRef.current) {
return false;
}
return cursorDocEnd(editorViewRef.current);
},
startCompletion() {
if (!editorViewRef.current) {
return false;
Expand Down Expand Up @@ -1455,6 +1463,9 @@ const MultilineEditor = React.forwardRef<EditorRef, MultilineEditorProps>(
applySnippet(template: string) {
return editorRef.current?.applySnippet(template) ?? false;
},
cursorDocEnd() {
return editorRef.current?.cursorDocEnd() ?? false;
},
startCompletion() {
return editorRef.current?.startCompletion() ?? false;
},
Expand Down
33 changes: 29 additions & 4 deletions packages/compass-query-bar/src/components/option-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
rafraf,
useDarkMode,
} from '@mongodb-js/compass-components';
import type { Command, EditorRef } from '@mongodb-js/compass-editor';
import type {
Command,
EditorRef,
SavedQuery,
} from '@mongodb-js/compass-editor';
import {
CodemirrorInlineEditor as InlineEditor,
createQueryAutocompleter,
Expand All @@ -23,8 +27,9 @@ import type { RootState } from '../stores/query-bar-store';
import { useAutocompleteFields } from '@mongodb-js/compass-field-store';
import { applyFromHistory } from '../stores/query-bar-reducer';
import { getQueryAttributes } from '../utils';
import type { BaseQuery } from '../constants/query-properties';
import type { SavedQuery } from '@mongodb-js/compass-editor';
import type { BaseQuery, QueryFormFields } from '../constants/query-properties';
import { mapQueryToFormFields } from '../utils/query';
import { DEFAULT_FIELD_VALUES } from '../constants/query-bar-store';

const editorContainerStyles = css({
position: 'relative',
Expand Down Expand Up @@ -155,6 +160,7 @@ export const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
}, []);

const schemaFields = useAutocompleteFields(namespace);
const maxTimeMSPreference = usePreference('maxTimeMS');

const completer = useMemo(() => {
return isQueryHistoryAutocompleteEnabled
Expand All @@ -179,14 +185,33 @@ export const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
fields: schemaFields,
serverVersion,
},
onApply: onApplyQuery,
onApply: (query: SavedQuery['queryProperties']) => {
onApplyQuery(query);
if (!query[optionName]) {
return;
}
const formFields = mapQueryToFormFields(
{ maxTimeMS: maxTimeMSPreference },
{
...DEFAULT_FIELD_VALUES,
...query,
}
);
const optionFormField =
formFields[optionName as keyof QueryFormFields];
if (optionFormField?.string) {
// When we did apply something we want to move the cursor to the end of the input.
editorRef.current?.cursorDocEnd();
}
},
theme: darkMode ? 'dark' : 'light',
})
: createQueryAutocompleter({
fields: schemaFields,
serverVersion,
});
}, [
maxTimeMSPreference,
savedQueries,
schemaFields,
serverVersion,
Expand Down

0 comments on commit 7d60fbd

Please sign in to comment.