-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(supersearch): Codemirror extension for preventing newlines (#1164)
* Add CodeMirror extension for preventing newlines * Add test * Add hidden textarea inside component for name and value attributes
- Loading branch information
1 parent
8a2431e
commit d8ebe00
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |