CodeEditor.hx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package hide.comp;
  2. class CodeEditor extends Component {
  3. var editor : monaco.Editor;
  4. var errorMessage : Element;
  5. var currrentDecos : Array<String> = [];
  6. public var code(get,never) : String;
  7. public function new( code : String, lang : String, ?parent : Element, ?root : Element ) {
  8. super(parent,root);
  9. var root = element;
  10. root.addClass("codeeditor");
  11. root.on("keydown", function(e) { if( e.keyCode == 27 && root.find(".suggest-widget.visible").length == 0 ) onClose(); e.stopPropagation(); });
  12. editor = monaco.Editor.create(root[0],{
  13. value : code,
  14. language : lang,
  15. automaticLayout : true,
  16. wordWrap : true,
  17. minimap : { enabled : false },
  18. theme : "vs-dark",
  19. });
  20. var model = editor.getModel();
  21. (model : Dynamic).__comp__ = this;
  22. model.updateOptions({ insertSpaces:false, trimAutoWhitespace:true });
  23. editor.onDidChangeModelContent(function() onChanged());
  24. editor.addCommand(monaco.KeyCode.KEY_S | monaco.KeyMod.CtrlCmd, function() { clearSpaces(); onSave(); });
  25. errorMessage = new Element('<div class="codeErrorMessage"></div>').appendTo(root).hide();
  26. }
  27. function clearSpaces() {
  28. var code = code;
  29. var newCode = [for( l in StringTools.trim(code).split("\n") ) StringTools.rtrim(l)].join("\n");
  30. if( newCode != code ) {
  31. var p = editor.getPosition();
  32. setCode(newCode);
  33. editor.setPosition(p);
  34. }
  35. }
  36. function get_code() {
  37. return editor.getValue({preserveBOM:true});
  38. }
  39. public function setCode( code : String ) {
  40. editor.setValue(code);
  41. }
  42. public function focus() {
  43. editor.focus();
  44. }
  45. public dynamic function onChanged() {
  46. }
  47. public dynamic function onSave() {
  48. }
  49. public dynamic function onClose() {
  50. }
  51. public function clearError() {
  52. if( currrentDecos.length != 0 )
  53. currrentDecos = editor.deltaDecorations(currrentDecos,[]);
  54. errorMessage.toggle(false);
  55. }
  56. public function setError( msg : String, line : Int, pmin : Int, pmax : Int ) {
  57. var linePos = code.substr(0,pmin).lastIndexOf("\n");
  58. if( linePos < 0 ) linePos = 0 else linePos++;
  59. var range = new monaco.Range(line,pmin + 1 - linePos,line,pmax + 2 - linePos);
  60. currrentDecos = editor.deltaDecorations(currrentDecos,[
  61. { range : range, options : { inlineClassName: "codeErrorContentLine", isWholeLine : true } },
  62. { range : range, options : { linesDecorationsClassName: "codeErrorLine", inlineClassName: "codeErrorContent" } }
  63. ]);
  64. errorMessage.html([for( l in msg.split("\n") ) StringTools.htmlEscape(l)].join("<br/>"));
  65. errorMessage.toggle(true);
  66. var rect = errorMessage[0].getBoundingClientRect();
  67. if( rect.bottom > js.Browser.window.innerHeight )
  68. errorMessage[0].scrollIntoView(false);
  69. }
  70. }