AceEditor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Bridge.Html5;
  2. using Retyped;
  3. using System;
  4. using Bridge;
  5. using static Retyped.ace;
  6. namespace LearnBridgeNet.Components
  7. {
  8. public class AceEditor
  9. {
  10. private readonly HTMLDivElement _dom;
  11. private readonly AceAjax.Editor _aceInstance;
  12. private AceEditorTheme _theme;
  13. private AceEditorMode _mode;
  14. private int? _width;
  15. private int? _height;
  16. private CssUnit _widthUnit = CssUnit.Pixel;
  17. private CssUnit _heightUnit = CssUnit.Pixel;
  18. private int _fontSize;
  19. private CssUnit _fontSizeUnit;
  20. [Init(InitPosition.Top)]
  21. public static void Patch()
  22. {
  23. //A pacth of retyped.ace type define
  24. /*@
  25. if (window.AceAjax === undefined) window.AceAjax = {};
  26. if (window.AceAjax.Editor === undefined) window.AceAjax.Editor = function () { };
  27. */
  28. }
  29. public AceEditor(HTMLDivElement dom)
  30. {
  31. DebugSupport.Add(this);
  32. _dom = dom;
  33. // ReSharper disable once SuspiciousTypeConversion.Global
  34. _aceInstance = ace2.edit((dom.HTMLDivElement)(object)dom);
  35. SizetUnit = FontSizeUnit = CssUnit.Pixel;
  36. Width = 800;
  37. Height = 600;
  38. FontSize = 16;
  39. _aceInstance.resize();
  40. Theme = AceEditorTheme.Chrome;
  41. Mode = AceEditorMode.Markdown;
  42. _aceInstance.on("change", o =>
  43. {
  44. OnChange();
  45. return null;
  46. });
  47. }
  48. public event EventHandler Change;
  49. public string Value
  50. {
  51. get => _aceInstance.getValue();
  52. set => _aceInstance.setValue(value);
  53. }
  54. public void AddClass(string name) => _dom.ClassList.Add(name);
  55. public void RemoveClass(string name) => _dom.ClassList.Remove(name);
  56. public AceEditorTheme Theme { set => _aceInstance.setTheme("ace/theme/" + (_theme = value)); get => _theme; }
  57. public AceEditorMode Mode { set => _aceInstance.session.setMode("ace/mode/" + (_mode = value)); get => _mode; }
  58. public int? Width
  59. {
  60. get => _width;
  61. set
  62. {
  63. if (null == value)
  64. {
  65. _width = null;
  66. _dom.Style.Width = null;
  67. return;
  68. }
  69. _dom.Style.Width = (_width = value).Value + _widthUnit.ToString();
  70. }
  71. }
  72. public CssUnit WidthUnit
  73. {
  74. get => _widthUnit;
  75. set
  76. {
  77. _widthUnit = value;
  78. _dom.Style.Width = null == _width ? null : _width.Value + _widthUnit.ToString();
  79. }
  80. }
  81. public int? Height
  82. {
  83. get => _height;
  84. set
  85. {
  86. if (null == value)
  87. {
  88. _width = null;
  89. _dom.Style.Height = null;
  90. return;
  91. }
  92. _dom.Style.Height = (_height = value).Value + _heightUnit.ToString();
  93. }
  94. }
  95. public CssUnit HeightUnit
  96. {
  97. get => _heightUnit;
  98. set
  99. {
  100. _heightUnit = value;
  101. _dom.Style.Width = null == _height ? null : _height.Value + _heightUnit.ToString();
  102. }
  103. }
  104. public CssUnit? SizetUnit
  105. {
  106. get => _widthUnit == _heightUnit ? (CssUnit?)_widthUnit : null;
  107. set
  108. {
  109. if (null == value) return;
  110. _heightUnit = value.Value;
  111. _widthUnit = value.Value;
  112. _dom.Style.Width = null == _width ? null : _width.Value + _widthUnit.ToString();
  113. _dom.Style.Width = null == _height ? null : _height.Value + _heightUnit.ToString();
  114. }
  115. }
  116. public int FontSize { set => _aceInstance.setFontSize((_fontSize = value) + _fontSizeUnit.ToString()); get => _fontSize; }
  117. public CssUnit FontSizeUnit { set => _aceInstance.setFontSize(_fontSize + (_fontSizeUnit = value).ToString()); get => _fontSizeUnit; }
  118. protected virtual void OnChange()
  119. {
  120. Change?.Invoke(this, EventArgs.Empty);
  121. }
  122. }
  123. }