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

LWS-261: Codemirror extension for preventing newlines #1164

Merged
merged 17 commits into from
Nov 14, 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
17 changes: 17 additions & 0 deletions packages/supersearch/e2e/supersearch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,20 @@ test('submits form identified by form attribute on enter key press', async ({ pa
await page.keyboard.press('Enter');
await expect(page).toHaveURL('/test2?q=hello+world');
});

test('prevents new line characters (e.g. when pasting multi-lined text', async ({
page,
context
}) => {
await context.grantPermissions(['clipboard-read', 'clipboard-write']);
await page.locator('[data-test-id="test1"]').getByRole('textbox').locator('div').click();
await page.evaluate(() =>
navigator.clipboard.writeText(`One
two
three`)
);
await page.keyboard.press(`ControlOrMeta+v`);
await expect(page.locator('[data-test-id="test1"]').getByRole('textbox').locator('div')).toHaveText(
'One two three'
);
});
2 changes: 2 additions & 0 deletions packages/supersearch/src/lib/components/SuperSearch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Compartment } from '@codemirror/state';
import { type LRLanguage } from '@codemirror/language';
import submitFormOnEnterKey from '$lib/extensions/submitFormOnEnterKey.js';
import preventNewLine from '$lib/extensions/preventNewLine.js';

interface Props {
name: string;
Expand All @@ -22,6 +23,7 @@

const extensions = [
submitFormOnEnterKey(form),
preventNewLine({ replaceWithSpace: true }),
...(language ? [language] : []),
placeholderCompartment.of(placeholderExtension(placeholder))
];
Expand Down
30 changes: 30 additions & 0 deletions packages/supersearch/src/lib/extensions/preventNewLine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { EditorState, Prec } from '@codemirror/state';

/**
* CodeMirror extension thats prevents inserted newlines (either by typing or pasting).
*
* @param {boolean} replaceWithSpace Controls if newline characters should be replaced with spaces.
*/

const preventNewLine = ({ replaceWithSpace = false }: { replaceWithSpace: boolean }) =>
Prec.highest(
EditorState.transactionFilter.of((tr) => {
if (tr.newDoc.lines > 1) {
return [
tr,
{
changes: {
from: 0,
to: tr.newDoc.length,
insert: tr.newDoc.sliceString(0, undefined, replaceWithSpace ? ' ' : '') // flatten multi-lined text
},
sequential: true
}
];
} else {
return [tr];
}
})
);

export default preventNewLine;
Loading