在 ace 编辑器中仅使最后一行可编辑
Make only last line editable in ace editor
王牌编辑器
需要只读区域
如果选择了错误的行,您可以在 editor.commands
上使用 exec
事件来禁用非只读命令
body,html {
height: 90%
}
<script src=https://ajaxorg.github.io/ace-builds/src/ace.js></script>
<script>
editor = ace.edit(document.body)
editor.setOptions({
mode: "ace/mode/javascript"
});
editor.commands.on("exec", function(e) {
if (e.command.readOnly)
return;
var editableRow = editor.session.getLength() - 1;
var deletesLeft = e.command.name == "backspace" || e.command.name == "removewordleft";
debugger
var notEditable = editor.selection.getAllRanges().some(function(r) {
if (deletesLeft && r.start.column == 0 && r.end.column == 0) return true;
return r.start.row != editableRow || r.end.row != editableRow;
});
if (notEditable)
e.preventDefault();
});
</script>
王牌编辑器 需要只读区域
如果选择了错误的行,您可以在 editor.commands
上使用 exec
事件来禁用非只读命令
body,html {
height: 90%
}
<script src=https://ajaxorg.github.io/ace-builds/src/ace.js></script>
<script>
editor = ace.edit(document.body)
editor.setOptions({
mode: "ace/mode/javascript"
});
editor.commands.on("exec", function(e) {
if (e.command.readOnly)
return;
var editableRow = editor.session.getLength() - 1;
var deletesLeft = e.command.name == "backspace" || e.command.name == "removewordleft";
debugger
var notEditable = editor.selection.getAllRanges().some(function(r) {
if (deletesLeft && r.start.column == 0 && r.end.column == 0) return true;
return r.start.row != editableRow || r.end.row != editableRow;
});
if (notEditable)
e.preventDefault();
});
</script>