Skip to content

Commit

Permalink
fix(editor): Indent on tabs in expression fields (#9659)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiloradFilipovic authored Jun 7, 2024
1 parent ebba7c8 commit bb7227d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const ndvStore = useNDVStore();
const root = ref<HTMLElement>();
const extensions = computed(() => [
Prec.highest(
keymap.of([...tabKeyMap(true), ...enterKeyMap, ...autocompleteKeyMap, ...historyKeyMap]),
keymap.of([...tabKeyMap(false), ...enterKeyMap, ...autocompleteKeyMap, ...historyKeyMap]),
),
n8nLang(),
n8nAutocompletion(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as workflowHelpers from '@/composables/useWorkflowHelpers';
import { EditorView } from '@codemirror/view';
import { EditorView, keymap } from '@codemirror/view';
import { createTestingPinia } from '@pinia/testing';
import { waitFor, fireEvent } from '@testing-library/vue';
import { setActivePinia } from 'pinia';
Expand All @@ -11,6 +11,7 @@ import { useRouter } from 'vue-router';
import { EditorSelection } from '@codemirror/state';
import userEvent from '@testing-library/user-event';
import { renderComponent } from '@/__tests__/render';
import { tabKeyMap } from '@/plugins/codemirror/keymap';

vi.mock('@/composables/useAutocompleteTelemetry', () => ({
useAutocompleteTelemetry: vi.fn(),
Expand Down Expand Up @@ -254,4 +255,32 @@ describe('useExpressionEditor', () => {
expect(expressionEditor.editor.value?.hasFocus).toBe(true);
});
});

describe('keymap', () => {
const TEST_EDITOR_VALUE = '{{ { "foo": "bar" } }}';

test('should indent on tab if blurOnTab is false', async () => {
const { renderResult, expressionEditor } = await renderExpressionEditor({
editorValue: TEST_EDITOR_VALUE,
extensions: [keymap.of([...tabKeyMap(false)])],
});
const root = renderResult.getByTestId('editor-root');
const input = root.querySelector('.cm-line') as HTMLDivElement;

await userEvent.type(input, '{tab}');
expect(expressionEditor.editor.value?.state.doc.toString()).toEqual(` ${TEST_EDITOR_VALUE}`);
});

test('should NOT indent on tab if blurOnTab is true', async () => {
const { renderResult, expressionEditor } = await renderExpressionEditor({
editorValue: TEST_EDITOR_VALUE,
extensions: [keymap.of([...tabKeyMap(true)])],
});
const root = renderResult.getByTestId('editor-root');
const input = root.querySelector('.cm-line') as HTMLDivElement;

await userEvent.type(input, '{tab}');
expect(expressionEditor.editor.value?.state.doc.toString()).toEqual(TEST_EDITOR_VALUE);
});
});
});
4 changes: 2 additions & 2 deletions packages/editor-ui/src/plugins/codemirror/keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { indentLess, indentMore, insertNewlineAndIndent, redo, undo } from '@codemirror/commands';
import type { EditorView, KeyBinding } from '@codemirror/view';

export const tabKeyMap = (singleLine = false): KeyBinding[] => [
export const tabKeyMap = (blurOnTab = false): KeyBinding[] => [
{
any(view, event) {
if (
Expand All @@ -27,7 +27,7 @@ export const tabKeyMap = (singleLine = false): KeyBinding[] => [
return acceptCompletion(view);
}

if (!singleLine) return indentMore(view);
if (!blurOnTab) return indentMore(view);
return false;
},
},
Expand Down

0 comments on commit bb7227d

Please sign in to comment.