Single line Ace editor
you can use the following code to make editor behave similar to input type="text" (mostly taken from https://github.com/ajaxorg/ace/blob/v1.2.0/demo/kitchen-sink/layout.js#L103)
var el = document.getElementById("textbox")
var editor = ace.edit(el);
editor.setOptions({
maxLines: 1, // make it 1 line
autoScrollEditorIntoView: true,
highlightActiveLine: false,
printMargin: false,
showGutter: false,
mode: "ace/mode/javascript",
theme: "ace/theme/tomorrow_night_eighties"
});
// remove newlines in pasted text
editor.on("paste", function(e) {
e.text = e.text.replace(/[\r\n]+/g, " ");
});
// make mouse position clipping nicer
editor.renderer.screenToTextCoordinates = function(x, y) {
var pos = this.pixelToScreenCoordinates(x, y);
return this.session.screenToDocumentPosition(
Math.min(this.session.getScreenLength() - 1, Math.max(pos.row, 0)),
Math.max(pos.column, 0)
);
};
// disable Enter Shift-Enter keys
editor.commands.bindKey("Enter|Shift-Enter", "null")
#textbox {
font-size: 30px;
border:solid 2px gray;
}
body{
background: #161619;
padding: 40px 20px
}
<script src="https://ajaxorg.github.io/ace-builds/src/ace.js"></script>
<div id=textbox>var a = 1</div>
For some reason neither e.preventDefault
nor e.stopPropagation
works in change event handler. But you can do find-replace.
See fiddle: http://jsfiddle.net/vittore/3rLfdtxb/
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/javascript");
editor.setFontSize(30)
editor.getSession().on('change', function(e) {
console.log(e)
if (e.data.text.charCodeAt(0) === 10 && e.data.action == "insertText") {
console.log('cancel event')
//e.preventDefault() // doesnt work
//e.stopPropagation() // doesnt work
editor.find(String.fromCharCode(10))
editor.replaceAll(''); // this work
}
})
You can even remove if
statement from handler and replace line break on any change, regardless.
When you find-replace in change, you got line from cursor to the end of line selected. In order to deselect it after that use :
editor.selection.clearSelection()