type/main.js

37 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

2016-09-30 01:31:36 +00:00
var editor = ace.edit("editor");
editor.setReadOnly(true);
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/javascript");
editor.getSession().setValue("function foo(items) {\n\tvar x = \"All this is syntax highlighted\";\n\treturn x;\n}");
// disable all keybindings
editor.commands.commandKeyBinding = {};
// prevent mouse interaction
editor.on("mousedown", (event) => {
event.preventDefault();
2016-09-28 22:33:02 +00:00
});
2016-09-30 01:31:36 +00:00
// handle text typing
editor.on("keydown", (event) => {
console.log(`Key pressed (focused: ${editor.isFocused()}, char: ${String.fromCharCode(event.charCode)}`);
if (editor.isFocused()) {
2016-09-28 22:33:02 +00:00
event.preventDefault();
2016-09-29 15:31:59 +00:00
2016-09-30 01:31:36 +00:00
let pos = editor.getCursorPosition();
let line = editor.getLine(pos.row);
let char = line.charCodeAt(pos.column);
2016-09-28 22:33:02 +00:00
2016-09-30 01:31:36 +00:00
if (event.charCode != char) {
// TODO: mark as invalid
2016-09-28 22:33:02 +00:00
}
2016-09-30 01:31:36 +00:00
editor.moveCursorTo(pos.row, pos.column + 1);
// TODO: update incomplete mark
2016-09-28 22:33:02 +00:00
}
2016-09-30 01:31:36 +00:00
});
2016-09-28 22:33:02 +00:00
String.prototype.hasOnlyWhiteSpaceBeforeIndex = function(index) {
return this.substring(index) == this.trim();
2016-09-30 01:31:36 +00:00
}