Skip to content

Commit

Permalink
feat(query-bar): autoinsert empty document for all fields in the quer…
Browse files Browse the repository at this point in the history
…y bar (#5135)

* feat(query-bar): autofix query in all editor inputs in query bar

* fix(editor): fix bracket keymap order; rename json-editor to editor

* chore(query-bar): rename prop
  • Loading branch information
gribnoysup authored Nov 20, 2023
1 parent 23d5a33 commit cf521a9
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CompletionSource } from '@codemirror/autocomplete';
import { completer, wrapField } from '../autocompleter';
import { languageName } from '../json-editor';
import { languageName } from '../editor';
import { resolveTokenAtCursor, completeWordsInString } from './utils';
import type { Token } from './utils';

Expand Down
2 changes: 1 addition & 1 deletion packages/compass-editor/src/codemirror/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { languages } from '../json-editor';
import { languages } from '../editor';
import { EditorView } from '@codemirror/view';
import { getAncestryOfToken, resolveTokenAtCursor } from './utils';
import { CompletionContext } from '@codemirror/autocomplete';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,8 +877,10 @@ const BaseEditor = React.forwardRef<EditorRef, EditorProps>(function BaseEditor(
key: 'Ctrl-Shift-c',
run: copyAll,
},
...defaultKeymap,
// Close brackets keymap overrides default backspace handler for
// matching bracket case
...closeBracketsKeymap,
...defaultKeymap,
...historyKeymap,
...foldKeymap,
...completionKeymap,
Expand Down
4 changes: 2 additions & 2 deletions packages/compass-editor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export {
CodemirrorMultilineEditor,
setCodemirrorEditorValue,
getCodemirrorEditorValue,
} from './json-editor';
} from './editor';
export type {
EditorView,
Command,
Annotation,
Action,
EditorRef,
Completer,
} from './json-editor';
} from './editor';
export { createDocumentAutocompleter } from './codemirror/document-autocompleter';
export { createValidationAutocompleter } from './codemirror/validation-autocompleter';
export { createQueryAutocompleter } from './codemirror/query-autocompleter';
Expand Down
18 changes: 11 additions & 7 deletions packages/compass-editor/test/completer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import type {
CompletionResult,
} from '@codemirror/autocomplete';
import { CompletionContext } from '@codemirror/autocomplete';
import { languages } from '../src/json-editor';
import { languages } from '../src/editor';

export const setupCodemirrorCompleter = <
T extends (...args: any[]) => CompletionSource
>(
completer: T
) => {
const el = window.document.createElement('div');
window.document.body.appendChild(el);
const editor = new EditorView({
doc: '',
extensions: [languages['javascript-expression']()],
parent: el,
let el: HTMLDivElement;
let editor: EditorView;
before(function () {
el = window.document.createElement('div');
window.document.body.appendChild(el);
editor = new EditorView({
doc: '',
extensions: [languages['javascript-expression']()],
parent: el,
});
});
const getCompletions = (text = '', ...args: Parameters<T>) => {
editor.dispatch({
Expand Down
26 changes: 21 additions & 5 deletions packages/compass-query-bar/src/components/option-editor.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ describe('OptionEditor', function () {
describe('with autofix enabled', function () {
it('fills the input with an empty object "{}" when empty on focus', async function () {
render(
<OptionEditor hasAutofix onChange={() => {}} value=""></OptionEditor>
<OptionEditor
insertEmptyDocOnFocus
onChange={() => {}}
value=""
></OptionEditor>
);

expect(screen.getByRole('textbox').textContent).to.eq('');
Expand All @@ -45,7 +49,7 @@ describe('OptionEditor', function () {
it('does not change input value when empty on focus', async function () {
render(
<OptionEditor
hasAutofix
insertEmptyDocOnFocus
onChange={() => {}}
value="{ foo: 1 }"
></OptionEditor>
Expand All @@ -62,7 +66,11 @@ describe('OptionEditor', function () {

it('should adjust pasted query if pasting over empty brackets with the cursor in the middle', async function () {
render(
<OptionEditor hasAutofix onChange={() => {}} value=""></OptionEditor>
<OptionEditor
insertEmptyDocOnFocus
onChange={() => {}}
value=""
></OptionEditor>
);

userEvent.tab();
Expand All @@ -82,7 +90,11 @@ describe('OptionEditor', function () {

it('should not modify user text whe pasting when cursor moved', async function () {
render(
<OptionEditor hasAutofix onChange={() => {}} value=""></OptionEditor>
<OptionEditor
insertEmptyDocOnFocus
onChange={() => {}}
value=""
></OptionEditor>
);

userEvent.tab();
Expand All @@ -104,7 +116,11 @@ describe('OptionEditor', function () {

it('should not modify user text when pasting in empty input', async function () {
render(
<OptionEditor hasAutofix onChange={() => {}} value=""></OptionEditor>
<OptionEditor
insertEmptyDocOnFocus
onChange={() => {}}
value=""
></OptionEditor>
);

userEvent.tab();
Expand Down
12 changes: 8 additions & 4 deletions packages/compass-query-bar/src/components/option-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ const insightsBadgeStyles = css({
type OptionEditorProps = {
id?: string;
hasError?: boolean;
hasAutofix?: boolean;
/**
* When `true` will insert an empty document in the input on focus and put
* cursor in the middle of the inserted string. Default is `true`
*/
insertEmptyDocOnFocus?: boolean;
onChange: (value: string) => void;
onApply?(): void;
onBlur?(): void;
Expand All @@ -97,7 +101,7 @@ type OptionEditorProps = {
export const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
id,
hasError = false,
hasAutofix = false,
insertEmptyDocOnFocus = true,
onChange,
onApply,
onBlur,
Expand Down Expand Up @@ -150,7 +154,7 @@ export const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
}, [schemaFields, serverVersion]);

const onFocus = () => {
if (hasAutofix) {
if (insertEmptyDocOnFocus) {
rafraf(() => {
if (editorRef.current?.editorContents === '') {
editorRef.current?.applySnippet('\\{${}}');
Expand All @@ -160,7 +164,7 @@ export const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
};

const onPaste = (event: React.ClipboardEvent<HTMLDivElement>) => {
if (hasAutofix && editorRef.current) {
if (insertEmptyDocOnFocus && editorRef.current) {
const { main: currentSelection } =
editorRef.current.editor?.state.selection ?? {};
const currentContents = editorRef.current.editorContents;
Expand Down
1 change: 0 additions & 1 deletion packages/compass-query-bar/src/components/query-option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ const QueryOption: React.FunctionComponent<QueryOptionProps> = ({
data-testid={`query-bar-option-${name}-input`}
onApply={onApply}
insights={insights}
hasAutofix={name === 'filter'}
/>
) : (
<WithOptionDefinitionTextInputProps definition={optionDefinition}>
Expand Down

0 comments on commit cf521a9

Please sign in to comment.