I'm trying to set up an Ace editor with only one single line of text.
The idea is to mimic the behaviour of an <input type="text">
box, but with syntax colouring:
Currently if the user presses Enter
while in the editor, it creates a new line:
So my question is:
How can I set up Ace to allow one single line only, like a standard text input box?
Below is what I've tried so far, and the reasons why it didn't succeed.
Calling
editor.undo()
onchange
ife.lines.length > 1
The problem is,
change
gets triggered before the actual change is applied in the deltas, so theundo()
doesn't work here (or it concerns the previous delta)Cancelling
keypress
ifEvent.which = 13
It kind of works but is very dirty, and it does not handle the case where multiple-lines text is pasted, so we'd need to handle
paste
event as well - which would make this solution even dirtier. I'm also pretty confident there would be even more edge cases to take into account.Trying to "empty"
e
inon("change", function(e) { ... })
For instance, saying that
e = {}
in the callback function, provided thate
is just a reference to the actual object. No effect whatsoever.Trying to find a built-in parameter in Ace editor to do that
No success in finding such a parameter yet...
e
is a reference, so changing it wont affect the original event. Have you tried just modifying whichever property one
holds the new text? – Kyeoticchange
effectively gets triggered before the delta is inserted in theundo
chain, it's nevertheless triggered after the change has been made into the editor - so @vittore 's solution works like a charm – Jivan