mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 17:46:45 +00:00
fix(editor): Open autocompletion when starting an expression (#13249)
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { EditorView } from '@codemirror/view';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { expressionCloseBrackets } from './expressionCloseBrackets';
|
||||
import { n8nAutocompletion, n8nLang } from './n8nLang';
|
||||
import { completionStatus } from '@codemirror/autocomplete';
|
||||
import { EditorSelection } from '@codemirror/state';
|
||||
import { setActivePinia } from 'pinia';
|
||||
import { createTestingPinia } from '@pinia/testing';
|
||||
|
||||
describe('expressionCloseBrackets', () => {
|
||||
const createEditor = () => {
|
||||
const parent = document.createElement('div');
|
||||
document.body.appendChild(parent);
|
||||
const editor = new EditorView({
|
||||
parent,
|
||||
extensions: [expressionCloseBrackets(), n8nLang(), n8nAutocompletion()],
|
||||
});
|
||||
return editor;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
setActivePinia(createTestingPinia());
|
||||
});
|
||||
|
||||
it('should complete {{| to {{ | }} and open autocomplete', async () => {
|
||||
const editor = createEditor();
|
||||
// '{' is an escape character: '{{' === '{'
|
||||
await userEvent.type(editor.contentDOM, '{{{{');
|
||||
expect(editor.state.doc.toString()).toEqual('{{ }}');
|
||||
expect(editor.state.selection).toEqual(EditorSelection.single(3));
|
||||
expect(completionStatus(editor.state)).not.toBeNull();
|
||||
});
|
||||
|
||||
it('should type over auto-closed brackets', async () => {
|
||||
const editor = createEditor();
|
||||
await userEvent.type(editor.contentDOM, 'foo()');
|
||||
// no extra closing bracket foo())
|
||||
expect(editor.state.doc.toString()).toEqual('foo()');
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ char: '"', expected: '""' },
|
||||
{ char: "'", expected: "''" },
|
||||
{ char: '(', expected: '()' },
|
||||
{ char: '{{}', expected: '{}' },
|
||||
{ char: '{[}', expected: '[]' },
|
||||
])('should auto-close $expected', async ({ expected, char }) => {
|
||||
const editor = createEditor();
|
||||
await userEvent.type(editor.contentDOM, char);
|
||||
expect(editor.state.doc.toString()).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
closeBrackets,
|
||||
closeBracketsKeymap,
|
||||
startCompletion,
|
||||
type CloseBracketConfig,
|
||||
} from '@codemirror/autocomplete';
|
||||
import { EditorSelection, Text } from '@codemirror/state';
|
||||
@@ -21,6 +22,7 @@ const expressionBracketSpacing = EditorView.updateListener.of((update) => {
|
||||
changes: [{ from: fromB + 1, insert: ' ' }],
|
||||
selection: EditorSelection.cursor(toB),
|
||||
});
|
||||
startCompletion(update.view);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user