Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): Indent on tabs in expression fields #9659

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading